Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add explicit type application on bls operations for G1/G2 #6666

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading