Skip to content

Commit

Permalink
add explicit type application on bls operations for G1/G2 (#6666)
Browse files Browse the repository at this point in the history
It's got through CI now so I'll merge it.  Thanks again.
  • Loading branch information
perturbing authored Nov 16, 2024
1 parent 1d9a758 commit 2e5165f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
18 changes: 9 additions & 9 deletions plutus-core/plutus-core/src/PlutusCore/Crypto/BLS12_381/G1.hs
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,19 @@ instance Hashable Element where
-- | Add two G1 group elements
{-# INLINE add #-}
add :: Element -> Element -> Element
add = coerce BlstBindings.blsAddOrDouble
add = coerce (BlstBindings.blsAddOrDouble @BlstBindings.Curve1)

-- | Negate a G1 group element
{-# INLINE neg #-}
neg :: Element -> Element
neg = coerce BlstBindings.blsNeg
neg = coerce (BlstBindings.blsNeg @BlstBindings.Curve1)

-- | Multiplication of group elements by scalars. In the blst library the
-- arguments are the other way round, but scalars acting on the left is more
-- consistent with standard mathematical practice.
{-# INLINE scalarMul #-}
scalarMul :: Integer -> Element -> Element
scalarMul = coerce $ flip BlstBindings.blsMult
scalarMul = coerce $ flip (BlstBindings.blsMult @BlstBindings.Curve1)

{- | Compress a G1 element to a bytestring. This serialises a curve point to its
x coordinate only. The compressed bytestring is 48 bytes long, with three
Expand All @@ -99,7 +99,7 @@ scalarMul = coerce $ flip BlstBindings.blsMult
-}
{-# INLINE compress #-}
compress :: Element -> ByteString
compress = coerce BlstBindings.blsCompress
compress = coerce (BlstBindings.blsCompress @BlstBindings.Curve1)

{- | Uncompress a bytestring to get a G1 point. This will fail if any of the
following are true.
Expand All @@ -112,7 +112,7 @@ compress = coerce BlstBindings.blsCompress
-}
{-# INLINE uncompress #-}
uncompress :: ByteString -> Either BlstBindings.BLSTError Element
uncompress = coerce BlstBindings.blsUncompress
uncompress = coerce (BlstBindings.blsUncompress @BlstBindings.Curve1)

{- Note [Hashing and Domain Separation Tags]. The hashToGroup functions take a
bytestring and hash it to obtain an element in the relevant group, as
Expand Down Expand Up @@ -143,23 +143,23 @@ hashToGroup :: ByteString -> ByteString -> Either BLS12_381_Error Element
hashToGroup msg dst =
if Data.ByteString.length dst > 255
then Left HashToCurveDstTooBig
else Right . Element $ BlstBindings.blsHash msg (Just dst) Nothing
else Right . Element $ BlstBindings.blsHash @BlstBindings.Curve1 msg (Just dst) Nothing

-- | The zero element of G1. This cannot be flat-serialised and is provided
-- only for off-chain testing.
offchain_zero :: Element
offchain_zero = coerce BlstBindings.Internal.blsZero
offchain_zero = coerce (BlstBindings.Internal.blsZero @BlstBindings.Curve1)

-- | The zero element of G1 compressed into a bytestring. This is provided for
-- convenience in PlutusTx and is not exported as a builtin.
{-# INLINABLE compressed_zero #-}
compressed_zero :: ByteString
compressed_zero = compress $ coerce BlstBindings.Internal.blsZero
compressed_zero = compress $ coerce (BlstBindings.Internal.blsZero @BlstBindings.Curve1)

-- | The standard generator of G1 compressed into a bytestring. This is
-- provided for convenience in PlutusTx and is not exported as a builtin.
compressed_generator :: ByteString
compressed_generator = compress $ coerce BlstBindings.Internal.blsGenerator
compressed_generator = compress $ coerce (BlstBindings.Internal.blsGenerator @BlstBindings.Curve1)

-- Utilities (not exposed as builtins)

Expand Down
18 changes: 9 additions & 9 deletions plutus-core/plutus-core/src/PlutusCore/Crypto/BLS12_381/G2.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ instance Hashable Element where
-- | Add two G2 group elements
{-# INLINE add #-}
add :: Element -> Element -> Element
add = coerce BlstBindings.blsAddOrDouble
add = coerce (BlstBindings.blsAddOrDouble @BlstBindings.Curve2)

-- | Negate a G2 group element
{-# INLINE neg #-}
neg :: Element -> Element
neg = coerce BlstBindings.blsNeg
neg = coerce (BlstBindings.blsNeg @BlstBindings.Curve2)

{-# INLINE scalarMul #-}
scalarMul :: Integer -> Element -> Element -- Other way round from library function
scalarMul = coerce $ flip BlstBindings.blsMult
scalarMul = coerce $ flip (BlstBindings.blsMult @BlstBindings.Curve2)

{- | Compress a G2 element to a bytestring. This serialises a curve point to its x
coordinate only, using an extra bit to determine which of two possible y
Expand All @@ -80,7 +80,7 @@ scalarMul = coerce $ flip BlstBindings.blsMult
-}
{-# INLINE compress #-}
compress :: Element -> ByteString
compress = coerce BlstBindings.blsCompress
compress = coerce (BlstBindings.blsCompress @BlstBindings.Curve2)

{- | Uncompress a bytestring to get a G2 point. This will fail if any of the
following are true:
Expand All @@ -93,30 +93,30 @@ compress = coerce BlstBindings.blsCompress
-}
{-# INLINE uncompress #-}
uncompress :: ByteString -> Either BlstBindings.BLSTError Element
uncompress = coerce BlstBindings.blsUncompress
uncompress = coerce (BlstBindings.blsUncompress @BlstBindings.Curve2)

-- Take an arbitrary bytestring and a Domain Separation Tag and hash them to a
-- get point in G2. See Note [Hashing and Domain Separation Tags].
hashToGroup :: ByteString -> ByteString -> Either BLS12_381_Error Element
hashToGroup msg dst =
if Data.ByteString.length dst > 255
then Left HashToCurveDstTooBig
else Right . Element $ BlstBindings.blsHash msg (Just dst) Nothing
else Right . Element $ BlstBindings.blsHash @BlstBindings.Curve2 msg (Just dst) Nothing

-- | The zero element of G2. This cannot be flat-serialised and is provided
-- only for off-chain testing.
offchain_zero :: Element
offchain_zero = coerce BlstBindings.Internal.blsZero
offchain_zero = coerce (BlstBindings.Internal.blsZero @BlstBindings.Curve2)

-- | The zero element of G2 compressed into a bytestring. This is provided for
-- convenience in PlutusTx and is not exported as a builtin.
compressed_zero :: ByteString
compressed_zero = compress $ coerce BlstBindings.Internal.blsZero
compressed_zero = compress $ coerce (BlstBindings.Internal.blsZero @BlstBindings.Curve2)

-- | The standard generator of G2 compressed into a bytestring. This is
-- provided for convenience in PlutusTx and is not exported as a builtin.
compressed_generator :: ByteString
compressed_generator = compress $ coerce BlstBindings.Internal.blsGenerator
compressed_generator = compress $ coerce (BlstBindings.Internal.blsGenerator @BlstBindings.Curve2)

-- Utilities (not exposed as builtins)

Expand Down

1 comment on commit 2e5165f

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Plutus Benchmarks'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.05.

Benchmark suite Current: 2e5165f Previous: 1d9a758 Ratio
validation-auction_2-4 754.7 μs 660.4 μs 1.14
validation-auction_2-5 330.1 μs 241.2 μs 1.37
validation-crowdfunding-success-1 267.8 μs 219.1 μs 1.22
validation-currency-1 305.1 μs 262 μs 1.16
validation-escrow-redeem_1-1 438.9 μs 366.5 μs 1.20
validation-game-sm-success_1-1 551.7 μs 517 μs 1.07
validation-game-sm-success_1-2 287.2 μs 209.9 μs 1.37
validation-game-sm-success_1-3 798.5 μs 664.4 μs 1.20
validation-game-sm-success_2-1 553.3 μs 511.1 μs 1.08
validation-multisig-sm-1 561.6 μs 440.4 μs 1.28
validation-multisig-sm-2 458.4 μs 400.4 μs 1.14
validation-uniswap-5 1644 μs 1254 μs 1.31
validation-uniswap-6 456.3 μs 333.7 μs 1.37
validation-vesting-1 476.8 μs 363.5 μs 1.31
validation-decode-future-pay-out-4 863.9 μs 683.9 μs 1.26
validation-decode-game-sm-success_2-4 174.1 μs 163.6 μs 1.06
validation-decode-multisig-sm-10 833.9 μs 599.2 μs 1.39
validation-decode-ping-pong-1 623.7 μs 515 μs 1.21
validation-decode-token-account-1 339 μs 243.2 μs 1.39
validation-decode-token-account-2 284.2 μs 221.6 μs 1.28
validation-decode-uniswap-1 263.9 μs 251 μs 1.05
validation-decode-uniswap-2 302.5 μs 249.7 μs 1.21
validation-decode-uniswap-6 231 μs 183.9 μs 1.26
nofib-clausify/formula1 4318 μs 3240 μs 1.33
nofib-queens5x5/bm 83960 μs 79770 μs 1.05
nofib-queens5x5/bjbt1 97900 μs 90520 μs 1.08
nofib-queens5x5/bjbt2 118700 μs 86930 μs 1.37
nofib-queens5x5/fc 249700 μs 182700 μs 1.37
marlowe-semantics/0000020002010200020101020201000100010001020101020201010000020102 411.1 μs 340.7 μs 1.21
marlowe-semantics/0001000101000000010101000001000001010101010100000001000001010000 650.2 μs 467 μs 1.39
marlowe-semantics/64c3d5b43f005855ffc4d0950a02fd159aa1575294ea39061b81a194ebb9eaae 831.7 μs 728.2 μs 1.14
marlowe-semantics/65bc4b69b46d18fdff0fadbf00dd5ec2b3e03805fac9d5fb4ff2d3066e53fc7e 3406 μs 2546 μs 1.34
marlowe-semantics/66af9e473d75e3f464971f6879cc0f2ef84bafcb38fbfa1dbc31ac2053628a38 1867 μs 1369 μs 1.36
marlowe-semantics/cdb9d5c233b288a5a9dcfbd8d5c1831a0bb46eec7a26fa31b80ae69d44805efc 1308 μs 956.9 μs 1.37
marlowe-semantics/cf542b7df466b228ca2197c2aaa89238a8122f3330fe5b77b3222f570395d9f5 740.8 μs 540.7 μs 1.37
marlowe-semantics/d1ab832dfab25688f8845bec9387e46ee3f00ba5822197ade7dd540489ec5e95 48060 μs 37700 μs 1.27
marlowe-semantics/d1c03759810747b7cab38c4296593b38567e11195d161b5bb0a2b58f89b2c65a 1529 μs 1118 μs 1.37
marlowe-role-payout/031d56d71454e2c4216ffaa275c4a8b3eb631109559d0e56f44ea8489f57ba97 317.5 μs 232.3 μs 1.37
marlowe-role-payout/03d730a62332c51c7b70c16c64da72dd1c3ea36c26b41cd1a1e00d39fda3d6cc 295.8 μs 217.8 μs 1.36
marlowe-role-payout/0403020000030204010000030001000202010101000304030001040404030100 274.3 μs 201.5 μs 1.36
marlowe-role-payout/0405010105020401010304080005050800040301010800080207080704020206 305.4 μs 223.6 μs 1.37
marlowe-role-payout/041a2c3b111139201a3a2c173c392b170e16370d300f2d28342d0f2f0e182e01 298.1 μs 221.2 μs 1.35
marlowe-role-payout/49b8275d0cb817be40865694ab05e3cfe5fc35fb43b78e7de68c1f3519b536bd 268.6 μs 196.2 μs 1.37
marlowe-role-payout/4dd7755b6ca1f0c9747c1fc0ee4da799f6f1c07108e980bd9f820911ad711ff2 339.5 μs 248.7 μs 1.37
marlowe-role-payout/4fbcfdb577a56b842d6f6938187a783f71d9da7519353e3da3ef0c564e1eb344 316.9 μs 232.4 μs 1.36
marlowe-role-payout/5a0725d49c733130eda8bc6ed5234f7f6ff8c9dd2d201e8806125e5fbcc081f9 275.4 μs 201.7 μs 1.37
marlowe-role-payout/5a2aae344e569a2c644dd9fa8c7b1f129850937eb562b7748c275f9e40bed596 259.5 μs 189.9 μs 1.37
marlowe-role-payout/5ade103e9530dd0d572fe1b053ea65ad925c6ebbe321e873ace8b804363fa82c 363.5 μs 266.1 μs 1.37
marlowe-role-payout/5d4c62a0671c65a14f6a15093e3efc4f1816d95a5a58fd92486bedaae8d9526b 300.8 μs 219.9 μs 1.37
marlowe-role-payout/5efe992e306e31cc857c64a62436ad2f9325acc5b4a74a8cebccdfd853ce63d2 269.5 μs 198 μs 1.36
marlowe-role-payout/622a7f3bc611b5149253c9189da022a9ff296f60a5b7c172a6dc286faa7284fa 314.7 μs 230.8 μs 1.36
marlowe-role-payout/6621a69217f09d91f42876a9c0cecf79de0e29bdd5b16c82c6c52cf959092ec4 288.7 μs 212.2 μs 1.36
marlowe-role-payout/674b0577409957172ad85223c765d17e94c27714276c49c38dfae0a47a561a1e 252.4 μs 185.2 μs 1.36
marlowe-role-payout/6b7bc2b9002a71b33cfd535d43f26334a283d0b9ad189b7cd74baac232c3b9fc 249.9 μs 183 μs 1.37
marlowe-role-payout/6c364699767a84059ffd99cf718562a8c09d96e343f23dc481e8ffda13af424f 259.4 μs 190.3 μs 1.36
marlowe-role-payout/6d66bddb4269bdf77392d3894da5341cf019d39787522af4f83f01285991e93c 262.3 μs 191.6 μs 1.37
marlowe-role-payout/73f044f34a30f26639c58bafe952047f74c7bf1eafebab5aadf5b73cfb9024ed 260.1 μs 190.2 μs 1.37
marlowe-role-payout/7b1dd76edc27f00eb382bf996378155baf74d6a7c6f3d5ec837c39d29784aade 262 μs 191.8 μs 1.37
marlowe-role-payout/803eae94d62e2afc0e835c204af8362170301bc329e2d849d5f5a47dddf479ec 292.6 μs 214 μs 1.37
marlowe-role-payout/87167fc5469adac97c1be749326fa79a6b7862ce68aa4abcb438e3c034bd0899 307.4 μs 226 μs 1.36
marlowe-role-payout/8c0fa5d9d6724c5c72c67e055d4bfc36a385ded7c3c81c08cdbd8705829af6e6 308.5 μs 226.8 μs 1.36
marlowe-role-payout/962c2c658b19904372984a56409707401e64e9b03c1986647134cfd329ec5139 281.3 μs 206.3 μs 1.36
marlowe-role-payout/996804e90f2c75fe68886fc8511304b8ab9b36785f8858f5cb098e91c159dde9 265.4 μs 194.3 μs 1.37
marlowe-role-payout/a004a989c005d59043f996500e110fa756ad1b85800b889d5815a0106388e1d7 275.1 μs 201 μs 1.37
marlowe-role-payout/a0fba5740174b5cd24036c8b008cb1efde73f1edae097b9325c6117a0ff40d3b 290.2 μs 212.4 μs 1.37
marlowe-role-payout/a1b25347409c3993feca1a60b6fcaf93d1d4bbaae19ab06fdf50cedc26cee68d 249.6 μs 183.1 μs 1.36
marlowe-role-payout/a27524cfad019df45e4e8316f927346d4cc39da6bdd294fb2c33c3f58e6a8994 259.6 μs 190.1 μs 1.37
marlowe-role-payout/a6664a2d2a82f370a34a36a45234f6b33120a39372331678a3b3690312560ce9 319 μs 233.4 μs 1.37
marlowe-role-payout/a6f064b83b31032ea7f25921364727224707268e472a569f584cc6b1d8c017e8 261.1 μs 192.1 μs 1.36
marlowe-role-payout/a7cb09f417c3f089619fe25b7624392026382b458486129efcff18f8912bf302 260.2 μs 189.7 μs 1.37
marlowe-role-payout/a92b4072cb8601fa697e1150c08463b14ffced54eb963df08d322216e27373cb 261.2 μs 191.5 μs 1.36
marlowe-role-payout/af2e072b5adfaa7211e0b341e1f7319c4f4e7364a4247c9247132a927e914753 305.7 μs 223.8 μs 1.37
marlowe-role-payout/b43564af5f13cc5208b92b1ad6d45369446f378d3891e5cb3e353b30d4f3fb10 261.2 μs 192.4 μs 1.36
marlowe-role-payout/b6243a5b4c353ce4852aa41705111d57867d2783eeef76f6d59beb2360da6e90 350 μs 255.7 μs 1.37
marlowe-role-payout/b869f3928200061abb1c3060425b9354b0e08cbf4400b340b8707c14b34317cd 341.3 μs 277.1 μs 1.23

This comment was automatically generated by workflow using github-action-benchmark.

CC: @IntersectMBO/plutus-core

Please sign in to comment.