-- editorconfig-checker-disable-file

-- | Primitive names and functions for working with Plutus Core builtins.
module PlutusTx.Builtins (
                          -- * Bytestring builtins
                          BuiltinByteString
                         , appendByteString
                         , consByteString
                         , sliceByteString
                         , lengthOfByteString
                         , indexByteString
                         , emptyByteString
                         , equalsByteString
                         , lessThanByteString
                         , lessThanEqualsByteString
                         , greaterThanByteString
                         , greaterThanEqualsByteString
                         , sha2_256
                         , sha3_256
                         , blake2b_224
                         , blake2b_256
                         , keccak_256
                         , ripemd_160
                         , verifyEd25519Signature
                         , verifyEcdsaSecp256k1Signature
                         , verifySchnorrSecp256k1Signature
                         , decodeUtf8
                         -- * Integer builtins
                         , Integer
                         , addInteger
                         , subtractInteger
                         , multiplyInteger
                         , divideInteger
                         , modInteger
                         , quotientInteger
                         , remainderInteger
                         , greaterThanInteger
                         , greaterThanEqualsInteger
                         , lessThanInteger
                         , lessThanEqualsInteger
                         , equalsInteger
                         , expModInteger
                         -- * Error
                         , error
                         -- * Data
                         , BuiltinData
                         , chooseData
                         , BI.caseData'
                         , caseData
                         , matchData
                         , matchData'
                         , equalsData
                         , serialiseData
                         , mkConstr
                         , mkMap
                         , mkList
                         , mkI
                         , mkB
                         , unsafeDataAsConstr
                         , unsafeDataAsMap
                         , unsafeDataAsList
                         , unsafeDataAsI
                         , unsafeDataAsB
                         , BI.builtinDataToData
                         , BI.dataToBuiltinData
                         -- * Strings
                         , BuiltinString
                         , appendString
                         , emptyString
                         , equalsString
                         , encodeUtf8
                         -- * Pairs
                         , pairToPair
                         -- * Lists
                         , mkNil
                         , mkNilOpaque
                         , null
                         , BI.caseList'
                         , caseList
                         , matchList
                         , matchList'
                         , headMaybe
                         , BI.head
                         , BI.tail
                         , uncons
                         , unsafeUncons
                         -- * Tracing
                         , trace
                         -- * BLS12_381
                         , BuiltinBLS12_381_G1_Element
                         , bls12_381_G1_equals
                         , bls12_381_G1_add
                         , bls12_381_G1_scalarMul
                         , bls12_381_G1_neg
                         , bls12_381_G1_compress
                         , bls12_381_G1_uncompress
                         , bls12_381_G1_hashToGroup
                         , bls12_381_G1_compressed_zero
                         , bls12_381_G1_compressed_generator
                         , BuiltinBLS12_381_G2_Element
                         , bls12_381_G2_equals
                         , bls12_381_G2_add
                         , bls12_381_G2_scalarMul
                         , bls12_381_G2_neg
                         , bls12_381_G2_compress
                         , bls12_381_G2_uncompress
                         , bls12_381_G2_hashToGroup
                         , bls12_381_G2_compressed_zero
                         , bls12_381_G2_compressed_generator
                         , BuiltinBLS12_381_MlResult
                         , bls12_381_millerLoop
                         , bls12_381_mulMlResult
                         , bls12_381_finalVerify
                         -- * Conversions
                         , fromOpaque
                         , toOpaque
                         , useToOpaque
                         , useFromOpaque
                         , fromBuiltin
                         , toBuiltin
                         -- * Logical
                         , ByteOrder (..)
                         , integerToByteString
                         , byteStringToInteger
                         , andByteString
                         , orByteString
                         , xorByteString
                         , complementByteString
                         , readBit
                         , writeBits
                         , replicateByte
                         -- * Bitwise
                         , shiftByteString
                         , rotateByteString
                         , countSetBits
                         , findFirstSetBit
                         ) where

import Data.Maybe
import PlutusTx.Bool (Bool (..))
import PlutusTx.Builtins.HasBuiltin
import PlutusTx.Builtins.HasOpaque
import PlutusTx.Builtins.Internal (BuiltinBLS12_381_G1_Element (..),
                                   BuiltinBLS12_381_G2_Element (..), BuiltinBLS12_381_MlResult (..),
                                   BuiltinByteString (..), BuiltinData, BuiltinString)
import PlutusTx.Builtins.Internal qualified as BI
import PlutusTx.Integer (Integer)

import GHC.ByteOrder (ByteOrder (BigEndian, LittleEndian))

{-# INLINABLE appendByteString #-}
-- | Concatenates two 'ByteString's.
appendByteString :: BuiltinByteString -> BuiltinByteString -> BuiltinByteString
appendByteString :: BuiltinByteString -> BuiltinByteString -> BuiltinByteString
appendByteString = BuiltinByteString -> BuiltinByteString -> BuiltinByteString
BI.appendByteString

{-# INLINABLE consByteString #-}
-- | Adds a byte to the front of a 'ByteString'.
consByteString :: Integer -> BuiltinByteString -> BuiltinByteString
consByteString :: Integer -> BuiltinByteString -> BuiltinByteString
consByteString Integer
n BuiltinByteString
bs = Integer -> BuiltinByteString -> BuiltinByteString
BI.consByteString (Integer -> Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Integer
n) BuiltinByteString
bs

{-# INLINABLE sliceByteString #-}
-- | Returns the substring of a 'ByteString' from index 'start' of length 'n'.
sliceByteString :: Integer -> Integer -> BuiltinByteString -> BuiltinByteString
sliceByteString :: Integer -> Integer -> BuiltinByteString -> BuiltinByteString
sliceByteString Integer
start Integer
n BuiltinByteString
bs = Integer -> Integer -> BuiltinByteString -> BuiltinByteString
BI.sliceByteString (Integer -> Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Integer
start) (Integer -> Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Integer
n) BuiltinByteString
bs

{-# INLINABLE lengthOfByteString #-}
-- | Returns the length of a 'ByteString'.
lengthOfByteString :: BuiltinByteString -> Integer
lengthOfByteString :: BuiltinByteString -> Integer
lengthOfByteString = BuiltinByteString -> Integer
BI.lengthOfByteString

{-# INLINABLE indexByteString #-}
-- | Returns the byte of a 'ByteString' at index.
indexByteString :: BuiltinByteString -> Integer -> Integer
indexByteString :: BuiltinByteString -> Integer -> Integer
indexByteString BuiltinByteString
b Integer
n = BuiltinByteString -> Integer -> Integer
BI.indexByteString BuiltinByteString
b (Integer -> Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Integer
n)

{-# INLINABLE emptyByteString #-}
-- | An empty 'ByteString'.
emptyByteString :: BuiltinByteString
emptyByteString :: BuiltinByteString
emptyByteString = BuiltinByteString
BI.emptyByteString

{-# INLINABLE sha2_256 #-}
-- | The SHA2-256 hash of a 'ByteString'
sha2_256 :: BuiltinByteString -> BuiltinByteString
sha2_256 :: BuiltinByteString -> BuiltinByteString
sha2_256 = BuiltinByteString -> BuiltinByteString
BI.sha2_256

{-# INLINABLE sha3_256 #-}
-- | The SHA3-256 hash of a 'ByteString'
sha3_256 :: BuiltinByteString -> BuiltinByteString
sha3_256 :: BuiltinByteString -> BuiltinByteString
sha3_256 = BuiltinByteString -> BuiltinByteString
BI.sha3_256

{-# INLINABLE blake2b_224 #-}
-- | The BLAKE2B-224 hash of a 'ByteString'
blake2b_224 :: BuiltinByteString -> BuiltinByteString
blake2b_224 :: BuiltinByteString -> BuiltinByteString
blake2b_224 = BuiltinByteString -> BuiltinByteString
BI.blake2b_224

{-# INLINABLE blake2b_256 #-}
-- | The BLAKE2B-256 hash of a 'ByteString'
blake2b_256 :: BuiltinByteString -> BuiltinByteString
blake2b_256 :: BuiltinByteString -> BuiltinByteString
blake2b_256 = BuiltinByteString -> BuiltinByteString
BI.blake2b_256

{-# INLINABLE keccak_256 #-}
-- | The KECCAK-256 hash of a 'ByteString'
keccak_256 :: BuiltinByteString -> BuiltinByteString
keccak_256 :: BuiltinByteString -> BuiltinByteString
keccak_256 = BuiltinByteString -> BuiltinByteString
BI.keccak_256

{-# INLINABLE ripemd_160 #-}
-- | The RIPEMD-160 hash of a 'ByteString'
ripemd_160 :: BuiltinByteString -> BuiltinByteString
ripemd_160 :: BuiltinByteString -> BuiltinByteString
ripemd_160 = BuiltinByteString -> BuiltinByteString
BI.ripemd_160

{-# INLINABLE verifyEd25519Signature #-}
-- | Ed25519 signature verification. Verify that the signature is a signature of
-- the message by the public key. This will fail if key or the signature are not
-- of the expected length.
verifyEd25519Signature
    :: BuiltinByteString  -- ^ Public Key (32 bytes)
    -> BuiltinByteString  -- ^ Message    (arbirtary length)
    -> BuiltinByteString  -- ^ Signature  (64 bytes)
    -> Bool
verifyEd25519Signature :: BuiltinByteString -> BuiltinByteString -> BuiltinByteString -> Bool
verifyEd25519Signature BuiltinByteString
pubKey BuiltinByteString
message BuiltinByteString
signature =
    BuiltinBool -> Bool
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (BuiltinByteString
-> BuiltinByteString -> BuiltinByteString -> BuiltinBool
BI.verifyEd25519Signature BuiltinByteString
pubKey BuiltinByteString
message BuiltinByteString
signature)

{-# INLINABLE equalsByteString #-}
-- | Check if two 'ByteString's are equal.
equalsByteString :: BuiltinByteString -> BuiltinByteString -> Bool
equalsByteString :: BuiltinByteString -> BuiltinByteString -> Bool
equalsByteString BuiltinByteString
x BuiltinByteString
y = BuiltinBool -> Bool
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (BuiltinByteString -> BuiltinByteString -> BuiltinBool
BI.equalsByteString BuiltinByteString
x BuiltinByteString
y)

{-# INLINABLE lessThanByteString #-}
-- | Check if one 'ByteString' is less than another.
lessThanByteString :: BuiltinByteString -> BuiltinByteString -> Bool
lessThanByteString :: BuiltinByteString -> BuiltinByteString -> Bool
lessThanByteString BuiltinByteString
x BuiltinByteString
y = BuiltinBool -> Bool
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (BuiltinByteString -> BuiltinByteString -> BuiltinBool
BI.lessThanByteString BuiltinByteString
x BuiltinByteString
y)

{-# INLINABLE lessThanEqualsByteString #-}
-- | Check if one 'ByteString' is less than or equal to another.
lessThanEqualsByteString :: BuiltinByteString -> BuiltinByteString -> Bool
lessThanEqualsByteString :: BuiltinByteString -> BuiltinByteString -> Bool
lessThanEqualsByteString BuiltinByteString
x BuiltinByteString
y = BuiltinBool -> Bool
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (BuiltinByteString -> BuiltinByteString -> BuiltinBool
BI.lessThanEqualsByteString BuiltinByteString
x BuiltinByteString
y)

{-# INLINABLE greaterThanByteString #-}
-- | Check if one 'ByteString' is greater than another.
greaterThanByteString :: BuiltinByteString -> BuiltinByteString -> Bool
greaterThanByteString :: BuiltinByteString -> BuiltinByteString -> Bool
greaterThanByteString BuiltinByteString
x BuiltinByteString
y = BuiltinBool -> Bool -> Bool -> Bool
forall a. BuiltinBool -> a -> a -> a
BI.ifThenElse (BuiltinByteString -> BuiltinByteString -> BuiltinBool
BI.lessThanEqualsByteString BuiltinByteString
x BuiltinByteString
y) Bool
False Bool
True

{-# INLINABLE greaterThanEqualsByteString #-}
-- | Check if one 'ByteString' is greater than another.
greaterThanEqualsByteString :: BuiltinByteString -> BuiltinByteString -> Bool
greaterThanEqualsByteString :: BuiltinByteString -> BuiltinByteString -> Bool
greaterThanEqualsByteString BuiltinByteString
x BuiltinByteString
y = BuiltinBool -> Bool -> Bool -> Bool
forall a. BuiltinBool -> a -> a -> a
BI.ifThenElse (BuiltinByteString -> BuiltinByteString -> BuiltinBool
BI.lessThanByteString BuiltinByteString
x BuiltinByteString
y) Bool
False Bool
True

{-# INLINABLE decodeUtf8 #-}
-- | Converts a ByteString to a String.
decodeUtf8 :: BuiltinByteString -> BuiltinString
decodeUtf8 :: BuiltinByteString -> BuiltinString
decodeUtf8 = BuiltinByteString -> BuiltinString
BI.decodeUtf8

{-# INLINEABLE verifyEcdsaSecp256k1Signature #-}
-- | Given an ECDSA SECP256k1 verification key, an ECDSA SECP256k1 signature,
-- and an ECDSA SECP256k1 message hash (all as 'BuiltinByteString's), verify the
-- hash with that key and signature.
--
-- = Note
--
-- There are additional well-formation requirements for the arguments beyond
-- their length:
--
-- * The first byte of the public key must correspond to the sign of the /y/
-- coordinate: this is @0x02@ if /y/ is even, and @0x03@ otherwise.
-- * The remaining bytes of the public key must correspond to the /x/
-- coordinate, as a big-endian integer.
-- * The first 32 bytes of the signature must correspond to the big-endian
-- integer representation of _r_.
-- * The last 32 bytes of the signature must correspond to the big-endian
-- integer representation of _s_.
--
-- While this primitive /accepts/ a hash, any caller should only pass it hashes
-- that they computed themselves: specifically, they should receive the
-- /message/ from a sender and hash it, rather than receiving the /hash/ from
-- said sender. Failure to do so can be
-- [dangerous](https://bitcoin.stackexchange.com/a/81116/35586). Other than
-- length, we make no requirements of what hash gets used.
--
-- = See also
--
-- *
-- [@secp256k1_ec_pubkey_serialize@](https://github.com/bitcoin-core/secp256k1/blob/master/include/secp256k1.h#L394);
-- this implements the format for the verification key that we accept, given a
-- length argument of 33 and the @SECP256K1_EC_COMPRESSED@ flag.
-- *
-- [@secp256k1_ecdsa_serialize_compact@](https://github.com/bitcoin-core/secp256k1/blob/master/include/secp256k1.h#L487);
-- this implements the format for the signature that we accept.
verifyEcdsaSecp256k1Signature
  :: BuiltinByteString -- ^ Verification key (33 bytes)
  -> BuiltinByteString -- ^ Message hash (32 bytes)
  -> BuiltinByteString -- ^ Signature (64 bytes)
  -> Bool
verifyEcdsaSecp256k1Signature :: BuiltinByteString -> BuiltinByteString -> BuiltinByteString -> Bool
verifyEcdsaSecp256k1Signature BuiltinByteString
vk BuiltinByteString
msg BuiltinByteString
sig =
  BuiltinBool -> Bool
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (BuiltinByteString
-> BuiltinByteString -> BuiltinByteString -> BuiltinBool
BI.verifyEcdsaSecp256k1Signature BuiltinByteString
vk BuiltinByteString
msg BuiltinByteString
sig)

{-# INLINEABLE verifySchnorrSecp256k1Signature #-}
-- | Given a Schnorr SECP256k1 verification key, a Schnorr SECP256k1 signature,
-- and a message (all as 'BuiltinByteString's), verify the message with that key
-- and signature.
--
-- = Note
--
-- There are additional well-formation requirements for the arguments beyond
-- their length. Throughout, we refer to co-ordinates of the point @R@.
--
-- * The bytes of the public key must correspond to the /x/ coordinate, as a
-- big-endian integer, as specified in BIP-340.
-- * The first 32 bytes of the signature must correspond to the /x/ coordinate,
-- as a big-endian integer, as specified in BIP-340.
-- * The last 32 bytes of the signature must correspond to the bytes of /s/, as
-- a big-endian integer, as specified in BIP-340.
--
-- = See also
--
-- * [BIP-340](https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki)
-- *
-- [@secp256k1_xonly_pubkey_serialize@](https://github.com/bitcoin-core/secp256k1/blob/master/include/secp256k1_extrakeys.h#L61);
-- this implements the format for the verification key that we accept.
-- *
-- [@secp256k1_schnorrsig_sign@](https://github.com/bitcoin-core/secp256k1/blob/master/include/secp256k1_schnorrsig.h#L129);
-- this implements the signing logic for signatures this builtin can verify.
verifySchnorrSecp256k1Signature
  :: BuiltinByteString -- ^ Verification key (32 bytes)
  -> BuiltinByteString -- ^ Message (arbitrary length)
  -> BuiltinByteString -- ^ Signature (64 bytes)
  -> Bool
verifySchnorrSecp256k1Signature :: BuiltinByteString -> BuiltinByteString -> BuiltinByteString -> Bool
verifySchnorrSecp256k1Signature BuiltinByteString
vk BuiltinByteString
msg BuiltinByteString
sig =
  BuiltinBool -> Bool
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (BuiltinByteString
-> BuiltinByteString -> BuiltinByteString -> BuiltinBool
BI.verifySchnorrSecp256k1Signature BuiltinByteString
vk BuiltinByteString
msg BuiltinByteString
sig)

{-# INLINABLE addInteger #-}
-- | Add two 'Integer's.
addInteger :: Integer -> Integer -> Integer
addInteger :: Integer -> Integer -> Integer
addInteger Integer
x Integer
y = Integer -> Integer
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (Integer -> Integer -> Integer
BI.addInteger (Integer -> Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Integer
x) (Integer -> Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Integer
y))

{-# INLINABLE subtractInteger #-}
-- | Subtract two 'Integer's.
subtractInteger :: Integer -> Integer -> Integer
subtractInteger :: Integer -> Integer -> Integer
subtractInteger Integer
x Integer
y = Integer -> Integer
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (Integer -> Integer -> Integer
BI.subtractInteger (Integer -> Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Integer
x) (Integer -> Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Integer
y))

{-# INLINABLE multiplyInteger #-}
-- | Multiply two 'Integer's.
multiplyInteger :: Integer -> Integer -> Integer
multiplyInteger :: Integer -> Integer -> Integer
multiplyInteger Integer
x Integer
y = Integer -> Integer
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (Integer -> Integer -> Integer
BI.multiplyInteger (Integer -> Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Integer
x) (Integer -> Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Integer
y))

{-# INLINABLE divideInteger #-}
-- | Divide two integers.
divideInteger :: Integer -> Integer -> Integer
divideInteger :: Integer -> Integer -> Integer
divideInteger Integer
x Integer
y = Integer -> Integer
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (Integer -> Integer -> Integer
BI.divideInteger (Integer -> Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Integer
x) (Integer -> Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Integer
y))

{-# INLINABLE modInteger #-}
-- | Integer modulo operation.
modInteger :: Integer -> Integer -> Integer
modInteger :: Integer -> Integer -> Integer
modInteger Integer
x Integer
y = Integer -> Integer
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (Integer -> Integer -> Integer
BI.modInteger (Integer -> Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Integer
x) (Integer -> Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Integer
y))

{-# INLINABLE quotientInteger #-}
-- | Quotient of two integers.
quotientInteger :: Integer -> Integer -> Integer
quotientInteger :: Integer -> Integer -> Integer
quotientInteger Integer
x Integer
y = Integer -> Integer
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (Integer -> Integer -> Integer
BI.quotientInteger (Integer -> Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Integer
x) (Integer -> Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Integer
y))

{-# INLINABLE remainderInteger #-}
-- | Take the remainder of dividing two 'Integer's.
remainderInteger :: Integer -> Integer -> Integer
remainderInteger :: Integer -> Integer -> Integer
remainderInteger Integer
x Integer
y = Integer -> Integer
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (Integer -> Integer -> Integer
BI.remainderInteger (Integer -> Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Integer
x) (Integer -> Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Integer
y))

{-# INLINABLE greaterThanInteger #-}
-- | Check whether one 'Integer' is greater than another.
greaterThanInteger :: Integer -> Integer -> Bool
greaterThanInteger :: Integer -> Integer -> Bool
greaterThanInteger Integer
x Integer
y = BuiltinBool -> Bool -> Bool -> Bool
forall a. BuiltinBool -> a -> a -> a
BI.ifThenElse (Integer -> Integer -> BuiltinBool
BI.lessThanEqualsInteger Integer
x Integer
y) Bool
False Bool
True

{-# INLINABLE greaterThanEqualsInteger #-}
-- | Check whether one 'Integer' is greater than or equal to another.
greaterThanEqualsInteger :: Integer -> Integer -> Bool
greaterThanEqualsInteger :: Integer -> Integer -> Bool
greaterThanEqualsInteger Integer
x Integer
y = BuiltinBool -> Bool -> Bool -> Bool
forall a. BuiltinBool -> a -> a -> a
BI.ifThenElse (Integer -> Integer -> BuiltinBool
BI.lessThanInteger Integer
x Integer
y) Bool
False Bool
True

{-# INLINABLE lessThanInteger #-}
-- | Check whether one 'Integer' is less than another.
lessThanInteger :: Integer -> Integer -> Bool
lessThanInteger :: Integer -> Integer -> Bool
lessThanInteger Integer
x Integer
y = BuiltinBool -> Bool
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (Integer -> Integer -> BuiltinBool
BI.lessThanInteger (Integer -> Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Integer
x) (Integer -> Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Integer
y))

{-# INLINABLE lessThanEqualsInteger #-}
-- | Check whether one 'Integer' is less than or equal to another.
lessThanEqualsInteger :: Integer -> Integer -> Bool
lessThanEqualsInteger :: Integer -> Integer -> Bool
lessThanEqualsInteger Integer
x Integer
y = BuiltinBool -> Bool
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (Integer -> Integer -> BuiltinBool
BI.lessThanEqualsInteger (Integer -> Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Integer
x) (Integer -> Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Integer
y))

{-# INLINABLE equalsInteger #-}
-- | Check if two 'Integer's are equal.
equalsInteger :: Integer -> Integer -> Bool
equalsInteger :: Integer -> Integer -> Bool
equalsInteger Integer
x Integer
y = BuiltinBool -> Bool
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (Integer -> Integer -> BuiltinBool
BI.equalsInteger (Integer -> Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Integer
x) (Integer -> Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Integer
y))

{-# INLINABLE error #-}
-- | Aborts evaluation with an error.
error :: () -> a
error :: forall a. () -> a
error ()
x = BuiltinUnit -> a
forall a. BuiltinUnit -> a
BI.error (() -> BuiltinUnit
forall a arep. HasToOpaque a arep => a -> arep
toOpaque ()
x)

{-# INLINABLE appendString #-}
-- | Append two 'String's.
appendString :: BuiltinString -> BuiltinString -> BuiltinString
appendString :: BuiltinString -> BuiltinString -> BuiltinString
appendString = BuiltinString -> BuiltinString -> BuiltinString
BI.appendString

{-# INLINABLE emptyString #-}
-- | An empty 'String'.
emptyString :: BuiltinString
emptyString :: BuiltinString
emptyString = BuiltinString
BI.emptyString

{-# INLINABLE equalsString #-}
-- | Check if two strings are equal
equalsString :: BuiltinString -> BuiltinString -> Bool
equalsString :: BuiltinString -> BuiltinString -> Bool
equalsString BuiltinString
x BuiltinString
y = BuiltinBool -> Bool
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (BuiltinString -> BuiltinString -> BuiltinBool
BI.equalsString BuiltinString
x BuiltinString
y)

{-# INLINABLE trace #-}
-- | Emit the given string as a trace message before evaluating the argument.
trace :: BuiltinString -> a -> a
trace :: forall a. BuiltinString -> a -> a
trace = BuiltinString -> a -> a
forall a. BuiltinString -> a -> a
BI.trace

{-# INLINABLE encodeUtf8 #-}
-- | Convert a String into a ByteString.
encodeUtf8 :: BuiltinString -> BuiltinByteString
encodeUtf8 :: BuiltinString -> BuiltinByteString
encodeUtf8 = BuiltinString -> BuiltinByteString
BI.encodeUtf8

{-# INLINABLE null #-}
null :: forall a. BI.BuiltinList a -> Bool
null :: forall a. BuiltinList a -> Bool
null BuiltinList a
l = BuiltinBool -> Bool
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (BuiltinList a -> BuiltinBool
forall a. BuiltinList a -> BuiltinBool
BI.null BuiltinList a
l)

{-# INLINABLE caseList #-}
caseList :: forall a r . (() -> r) -> (a -> BI.BuiltinList a -> r) -> BI.BuiltinList a -> r
caseList :: forall a r.
(() -> r) -> (a -> BuiltinList a -> r) -> BuiltinList a -> r
caseList () -> r
nilCase a -> BuiltinList a -> r
consCase BuiltinList a
l = (() -> r)
-> (a -> BuiltinList a -> () -> r) -> BuiltinList a -> () -> r
forall a r. r -> (a -> BuiltinList a -> r) -> BuiltinList a -> r
BI.caseList' () -> r
nilCase (\a
x BuiltinList a
xs ()
_ -> a -> BuiltinList a -> r
consCase a
x BuiltinList a
xs) BuiltinList a
l ()

{-# INLINABLE matchList #-}
matchList :: forall a r . BI.BuiltinList a -> (() -> r) -> (a -> BI.BuiltinList a -> r) -> r
matchList :: forall a r.
BuiltinList a -> (() -> r) -> (a -> BuiltinList a -> r) -> r
matchList BuiltinList a
l () -> r
nilCase a -> BuiltinList a -> r
consCase = (() -> r) -> (a -> BuiltinList a -> r) -> BuiltinList a -> r
forall a r.
(() -> r) -> (a -> BuiltinList a -> r) -> BuiltinList a -> r
caseList () -> r
nilCase a -> BuiltinList a -> r
consCase BuiltinList a
l

{-# INLINABLE matchList' #-}
matchList' :: forall a r . BI.BuiltinList a -> r -> (a -> BI.BuiltinList a -> r) -> r
matchList' :: forall a r. BuiltinList a -> r -> (a -> BuiltinList a -> r) -> r
matchList' BuiltinList a
l r
nilCase a -> BuiltinList a -> r
consCase = r -> (a -> BuiltinList a -> r) -> BuiltinList a -> r
forall a r. r -> (a -> BuiltinList a -> r) -> BuiltinList a -> r
BI.caseList' r
nilCase a -> BuiltinList a -> r
consCase BuiltinList a
l

{-# INLINE headMaybe #-}
headMaybe :: BI.BuiltinList a -> Maybe a
headMaybe :: forall a. BuiltinList a -> Maybe a
headMaybe = Maybe a
-> (a -> BuiltinList a -> Maybe a) -> BuiltinList a -> Maybe a
forall a r. r -> (a -> BuiltinList a -> r) -> BuiltinList a -> r
BI.caseList' Maybe a
forall a. Maybe a
Nothing (\a
h BuiltinList a
_ -> a -> Maybe a
forall a. a -> Maybe a
Just a
h)

{-# INLINE uncons #-}
-- | Uncons a builtin list, failing if the list is empty, useful in patterns.
uncons :: BI.BuiltinList a -> Maybe (a, BI.BuiltinList a)
uncons :: forall a. BuiltinList a -> Maybe (a, BuiltinList a)
uncons = Maybe (a, BuiltinList a)
-> (a -> BuiltinList a -> Maybe (a, BuiltinList a))
-> BuiltinList a
-> Maybe (a, BuiltinList a)
forall a r. r -> (a -> BuiltinList a -> r) -> BuiltinList a -> r
BI.caseList' Maybe (a, BuiltinList a)
forall a. Maybe a
Nothing (\a
h BuiltinList a
t -> (a, BuiltinList a) -> Maybe (a, BuiltinList a)
forall a. a -> Maybe a
Just (a
h, BuiltinList a
t))

{-# INLINE unsafeUncons #-}
-- | Uncons a builtin list, failing if the list is empty, useful in patterns.
unsafeUncons :: BI.BuiltinList a -> (a, BI.BuiltinList a)
unsafeUncons :: forall a. BuiltinList a -> (a, BuiltinList a)
unsafeUncons BuiltinList a
l = (BuiltinList a -> a
forall a. BuiltinList a -> a
BI.head BuiltinList a
l, BuiltinList a -> BuiltinList a
forall a. BuiltinList a -> BuiltinList a
BI.tail BuiltinList a
l)

{-# INLINE pairToPair #-}
-- | Turn a builtin pair into a normal pair, useful in patterns.
pairToPair :: BI.BuiltinPair a b -> (a, b)
pairToPair :: forall a b. BuiltinPair a b -> (a, b)
pairToPair BuiltinPair a b
tup = (BuiltinPair a b -> a
forall a b. BuiltinPair a b -> a
BI.fst BuiltinPair a b
tup, BuiltinPair a b -> b
forall a b. BuiltinPair a b -> b
BI.snd BuiltinPair a b
tup)

{-# INLINABLE chooseData #-}
-- | Given five values for the five different constructors of 'BuiltinData', selects
-- one depending on which corresponds to the actual constructor of the given value.
chooseData :: forall a . BuiltinData -> a -> a -> a -> a -> a -> a
chooseData :: forall a. BuiltinData -> a -> a -> a -> a -> a -> a
chooseData = BuiltinData -> a -> a -> a -> a -> a -> a
forall a. BuiltinData -> a -> a -> a -> a -> a -> a
BI.chooseData

{-# INLINABLE serialiseData #-}
-- | Convert a String into a ByteString.
serialiseData :: BuiltinData -> BuiltinByteString
serialiseData :: BuiltinData -> BuiltinByteString
serialiseData = BuiltinData -> BuiltinByteString
BI.serialiseData

{-# INLINABLE mkConstr #-}
-- | Constructs a 'BuiltinData' value with the @Constr@ constructor.
mkConstr :: Integer -> [BuiltinData] -> BuiltinData
mkConstr :: Integer -> [BuiltinData] -> BuiltinData
mkConstr Integer
i [BuiltinData]
args = Integer -> BuiltinList BuiltinData -> BuiltinData
BI.mkConstr (Integer -> Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Integer
i) ([BuiltinData] -> BuiltinList BuiltinData
forall a arep. HasToOpaque a arep => a -> arep
toOpaque [BuiltinData]
args)

{-# INLINABLE mkMap #-}
-- | Constructs a 'BuiltinData' value with the @Map@ constructor.
mkMap :: [(BuiltinData, BuiltinData)] -> BuiltinData
mkMap :: [(BuiltinData, BuiltinData)] -> BuiltinData
mkMap [(BuiltinData, BuiltinData)]
es = BuiltinList (BuiltinPair BuiltinData BuiltinData) -> BuiltinData
BI.mkMap ([(BuiltinData, BuiltinData)]
-> BuiltinList (BuiltinPair BuiltinData BuiltinData)
forall a arep. HasToOpaque a arep => a -> arep
toOpaque [(BuiltinData, BuiltinData)]
es)

{-# INLINABLE mkList #-}
-- | Constructs a 'BuiltinData' value with the @List@ constructor.
mkList :: [BuiltinData] -> BuiltinData
mkList :: [BuiltinData] -> BuiltinData
mkList [BuiltinData]
l = BuiltinList BuiltinData -> BuiltinData
BI.mkList ([BuiltinData] -> BuiltinList BuiltinData
forall a arep. HasToOpaque a arep => a -> arep
toOpaque [BuiltinData]
l)

{-# INLINABLE mkI #-}
-- | Constructs a 'BuiltinData' value with the @I@ constructor.
mkI :: Integer -> BuiltinData
mkI :: Integer -> BuiltinData
mkI = Integer -> BuiltinData
BI.mkI

{-# INLINABLE mkB #-}
-- | Constructs a 'BuiltinData' value with the @B@ constructor.
mkB :: BuiltinByteString -> BuiltinData
mkB :: BuiltinByteString -> BuiltinData
mkB = BuiltinByteString -> BuiltinData
BI.mkB

{-# INLINABLE unsafeDataAsConstr #-}
-- | Deconstructs a 'BuiltinData' as a @Constr@, or fails if it is not one.
unsafeDataAsConstr :: BuiltinData -> (Integer, [BuiltinData])
unsafeDataAsConstr :: BuiltinData -> (Integer, [BuiltinData])
unsafeDataAsConstr BuiltinData
d = BuiltinPair Integer (BuiltinList BuiltinData)
-> (Integer, [BuiltinData])
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (BuiltinData -> BuiltinPair Integer (BuiltinList BuiltinData)
BI.unsafeDataAsConstr BuiltinData
d)

{-# INLINABLE unsafeDataAsMap #-}
-- | Deconstructs a 'BuiltinData' as a @Map@, or fails if it is not one.
unsafeDataAsMap :: BuiltinData -> [(BuiltinData, BuiltinData)]
unsafeDataAsMap :: BuiltinData -> [(BuiltinData, BuiltinData)]
unsafeDataAsMap BuiltinData
d = BuiltinList (BuiltinPair BuiltinData BuiltinData)
-> [(BuiltinData, BuiltinData)]
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (BuiltinData -> BuiltinList (BuiltinPair BuiltinData BuiltinData)
BI.unsafeDataAsMap BuiltinData
d)

{-# INLINABLE unsafeDataAsList #-}
-- | Deconstructs a 'BuiltinData' as a @List@, or fails if it is not one.
unsafeDataAsList :: BuiltinData -> [BuiltinData]
unsafeDataAsList :: BuiltinData -> [BuiltinData]
unsafeDataAsList BuiltinData
d = BuiltinList BuiltinData -> [BuiltinData]
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (BuiltinData -> BuiltinList BuiltinData
BI.unsafeDataAsList BuiltinData
d)

{-# INLINABLE unsafeDataAsI #-}
-- | Deconstructs a 'BuiltinData' as an @I@, or fails if it is not one.
unsafeDataAsI :: BuiltinData -> Integer
unsafeDataAsI :: BuiltinData -> Integer
unsafeDataAsI BuiltinData
d = Integer -> Integer
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (BuiltinData -> Integer
BI.unsafeDataAsI BuiltinData
d)

{-# INLINABLE unsafeDataAsB #-}
-- | Deconstructs a 'BuiltinData' as a @B@, or fails if it is not one.
unsafeDataAsB :: BuiltinData -> BuiltinByteString
unsafeDataAsB :: BuiltinData -> BuiltinByteString
unsafeDataAsB = BuiltinData -> BuiltinByteString
BI.unsafeDataAsB

{-# INLINABLE equalsData #-}
-- | Check if two 'BuiltinData's are equal.
equalsData :: BuiltinData -> BuiltinData -> Bool
equalsData :: BuiltinData -> BuiltinData -> Bool
equalsData BuiltinData
d1 BuiltinData
d2 = BuiltinBool -> Bool
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (BuiltinData -> BuiltinData -> BuiltinBool
BI.equalsData BuiltinData
d1 BuiltinData
d2)

{-# INLINABLE caseData #-}
caseData
    :: (Integer -> [BuiltinData] -> r)
    -> ([(BuiltinData, BuiltinData)] -> r)
    -> ([BuiltinData] -> r)
    -> (Integer -> r)
    -> (BuiltinByteString -> r)
    -> BuiltinData
    -> r
caseData :: forall r.
(Integer -> [BuiltinData] -> r)
-> ([(BuiltinData, BuiltinData)] -> r)
-> ([BuiltinData] -> r)
-> (Integer -> r)
-> (BuiltinByteString -> r)
-> BuiltinData
-> r
caseData Integer -> [BuiltinData] -> r
constrCase [(BuiltinData, BuiltinData)] -> r
mapCase [BuiltinData] -> r
listCase Integer -> r
iCase BuiltinByteString -> r
bCase =
   (Integer -> BuiltinList BuiltinData -> r)
-> (BuiltinList (BuiltinPair BuiltinData BuiltinData) -> r)
-> (BuiltinList BuiltinData -> r)
-> (Integer -> r)
-> (BuiltinByteString -> r)
-> BuiltinData
-> r
forall r.
(Integer -> BuiltinList BuiltinData -> r)
-> (BuiltinList (BuiltinPair BuiltinData BuiltinData) -> r)
-> (BuiltinList BuiltinData -> r)
-> (Integer -> r)
-> (BuiltinByteString -> r)
-> BuiltinData
-> r
BI.caseData'
     (\Integer
i BuiltinList BuiltinData
ds -> Integer -> [BuiltinData] -> r
constrCase Integer
i (BuiltinList BuiltinData -> [BuiltinData]
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque BuiltinList BuiltinData
ds))
     (\BuiltinList (BuiltinPair BuiltinData BuiltinData)
ps -> [(BuiltinData, BuiltinData)] -> r
mapCase (BuiltinList (BuiltinPair BuiltinData BuiltinData)
-> [(BuiltinData, BuiltinData)]
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque BuiltinList (BuiltinPair BuiltinData BuiltinData)
ps))
     (\BuiltinList BuiltinData
ds -> [BuiltinData] -> r
listCase (BuiltinList BuiltinData -> [BuiltinData]
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque BuiltinList BuiltinData
ds))
     Integer -> r
iCase
     BuiltinByteString -> r
bCase

{-# INLINABLE matchData' #-}
matchData'
    :: BuiltinData
    -> (Integer -> BI.BuiltinList BuiltinData -> r)
    -> (BI.BuiltinList (BI.BuiltinPair BuiltinData BuiltinData) -> r)
    -> (BI.BuiltinList BuiltinData -> r)
    -> (Integer -> r)
    -> (BuiltinByteString -> r)
    -> r
matchData' :: forall r.
BuiltinData
-> (Integer -> BuiltinList BuiltinData -> r)
-> (BuiltinList (BuiltinPair BuiltinData BuiltinData) -> r)
-> (BuiltinList BuiltinData -> r)
-> (Integer -> r)
-> (BuiltinByteString -> r)
-> r
matchData' BuiltinData
d Integer -> BuiltinList BuiltinData -> r
constrCase BuiltinList (BuiltinPair BuiltinData BuiltinData) -> r
mapCase BuiltinList BuiltinData -> r
listCase Integer -> r
iCase BuiltinByteString -> r
bCase =
    (Integer -> BuiltinList BuiltinData -> r)
-> (BuiltinList (BuiltinPair BuiltinData BuiltinData) -> r)
-> (BuiltinList BuiltinData -> r)
-> (Integer -> r)
-> (BuiltinByteString -> r)
-> BuiltinData
-> r
forall r.
(Integer -> BuiltinList BuiltinData -> r)
-> (BuiltinList (BuiltinPair BuiltinData BuiltinData) -> r)
-> (BuiltinList BuiltinData -> r)
-> (Integer -> r)
-> (BuiltinByteString -> r)
-> BuiltinData
-> r
BI.caseData' Integer -> BuiltinList BuiltinData -> r
constrCase BuiltinList (BuiltinPair BuiltinData BuiltinData) -> r
mapCase BuiltinList BuiltinData -> r
listCase Integer -> r
iCase BuiltinByteString -> r
bCase BuiltinData
d

{-# INLINABLE matchData #-}
-- | Given a 'BuiltinData' value and matching functions for the five constructors,
-- applies the appropriate matcher to the arguments of the constructor and returns the result.
matchData
    :: BuiltinData
    -> (Integer -> [BuiltinData] -> r)
    -> ([(BuiltinData, BuiltinData)] -> r)
    -> ([BuiltinData] -> r)
    -> (Integer -> r)
    -> (BuiltinByteString -> r)
    -> r
matchData :: forall r.
BuiltinData
-> (Integer -> [BuiltinData] -> r)
-> ([(BuiltinData, BuiltinData)] -> r)
-> ([BuiltinData] -> r)
-> (Integer -> r)
-> (BuiltinByteString -> r)
-> r
matchData BuiltinData
d Integer -> [BuiltinData] -> r
constrCase [(BuiltinData, BuiltinData)] -> r
mapCase [BuiltinData] -> r
listCase Integer -> r
iCase BuiltinByteString -> r
bCase =
   (Integer -> [BuiltinData] -> r)
-> ([(BuiltinData, BuiltinData)] -> r)
-> ([BuiltinData] -> r)
-> (Integer -> r)
-> (BuiltinByteString -> r)
-> BuiltinData
-> r
forall r.
(Integer -> [BuiltinData] -> r)
-> ([(BuiltinData, BuiltinData)] -> r)
-> ([BuiltinData] -> r)
-> (Integer -> r)
-> (BuiltinByteString -> r)
-> BuiltinData
-> r
caseData Integer -> [BuiltinData] -> r
constrCase [(BuiltinData, BuiltinData)] -> r
mapCase [BuiltinData] -> r
listCase Integer -> r
iCase BuiltinByteString -> r
bCase BuiltinData
d

-- G1 --
{-# INLINABLE bls12_381_G1_equals #-}
bls12_381_G1_equals :: BuiltinBLS12_381_G1_Element -> BuiltinBLS12_381_G1_Element -> Bool
bls12_381_G1_equals :: BuiltinBLS12_381_G1_Element -> BuiltinBLS12_381_G1_Element -> Bool
bls12_381_G1_equals BuiltinBLS12_381_G1_Element
a BuiltinBLS12_381_G1_Element
b = BuiltinBool -> Bool
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (BuiltinBLS12_381_G1_Element
-> BuiltinBLS12_381_G1_Element -> BuiltinBool
BI.bls12_381_G1_equals BuiltinBLS12_381_G1_Element
a BuiltinBLS12_381_G1_Element
b)

{-# INLINABLE bls12_381_G1_add #-}
bls12_381_G1_add :: BuiltinBLS12_381_G1_Element -> BuiltinBLS12_381_G1_Element -> BuiltinBLS12_381_G1_Element
bls12_381_G1_add :: BuiltinBLS12_381_G1_Element
-> BuiltinBLS12_381_G1_Element -> BuiltinBLS12_381_G1_Element
bls12_381_G1_add = BuiltinBLS12_381_G1_Element
-> BuiltinBLS12_381_G1_Element -> BuiltinBLS12_381_G1_Element
BI.bls12_381_G1_add

{-# INLINABLE bls12_381_G1_scalarMul #-}
bls12_381_G1_scalarMul :: Integer -> BuiltinBLS12_381_G1_Element -> BuiltinBLS12_381_G1_Element
bls12_381_G1_scalarMul :: Integer
-> BuiltinBLS12_381_G1_Element -> BuiltinBLS12_381_G1_Element
bls12_381_G1_scalarMul = Integer
-> BuiltinBLS12_381_G1_Element -> BuiltinBLS12_381_G1_Element
BI.bls12_381_G1_scalarMul

{-# INLINABLE bls12_381_G1_neg #-}
bls12_381_G1_neg :: BuiltinBLS12_381_G1_Element -> BuiltinBLS12_381_G1_Element
bls12_381_G1_neg :: BuiltinBLS12_381_G1_Element -> BuiltinBLS12_381_G1_Element
bls12_381_G1_neg = BuiltinBLS12_381_G1_Element -> BuiltinBLS12_381_G1_Element
BI.bls12_381_G1_neg

{-# INLINABLE bls12_381_G1_compress #-}
bls12_381_G1_compress :: BuiltinBLS12_381_G1_Element -> BuiltinByteString
bls12_381_G1_compress :: BuiltinBLS12_381_G1_Element -> BuiltinByteString
bls12_381_G1_compress = BuiltinBLS12_381_G1_Element -> BuiltinByteString
BI.bls12_381_G1_compress

{-# INLINABLE bls12_381_G1_uncompress #-}
bls12_381_G1_uncompress :: BuiltinByteString -> BuiltinBLS12_381_G1_Element
bls12_381_G1_uncompress :: BuiltinByteString -> BuiltinBLS12_381_G1_Element
bls12_381_G1_uncompress = BuiltinByteString -> BuiltinBLS12_381_G1_Element
BI.bls12_381_G1_uncompress

{-# INLINABLE bls12_381_G1_hashToGroup #-}
bls12_381_G1_hashToGroup :: BuiltinByteString -> BuiltinByteString -> BuiltinBLS12_381_G1_Element
bls12_381_G1_hashToGroup :: BuiltinByteString
-> BuiltinByteString -> BuiltinBLS12_381_G1_Element
bls12_381_G1_hashToGroup = BuiltinByteString
-> BuiltinByteString -> BuiltinBLS12_381_G1_Element
BI.bls12_381_G1_hashToGroup

{-# INLINABLE bls12_381_G1_compressed_zero #-}
bls12_381_G1_compressed_zero :: BuiltinByteString
bls12_381_G1_compressed_zero :: BuiltinByteString
bls12_381_G1_compressed_zero = BuiltinByteString
BI.bls12_381_G1_compressed_zero

{-# INLINABLE bls12_381_G1_compressed_generator #-}
bls12_381_G1_compressed_generator :: BuiltinByteString
bls12_381_G1_compressed_generator :: BuiltinByteString
bls12_381_G1_compressed_generator = BuiltinByteString
BI.bls12_381_G1_compressed_generator

-- G2 --
{-# INLINABLE bls12_381_G2_equals #-}
bls12_381_G2_equals :: BuiltinBLS12_381_G2_Element -> BuiltinBLS12_381_G2_Element -> Bool
bls12_381_G2_equals :: BuiltinBLS12_381_G2_Element -> BuiltinBLS12_381_G2_Element -> Bool
bls12_381_G2_equals BuiltinBLS12_381_G2_Element
a BuiltinBLS12_381_G2_Element
b = BuiltinBool -> Bool
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (BuiltinBLS12_381_G2_Element
-> BuiltinBLS12_381_G2_Element -> BuiltinBool
BI.bls12_381_G2_equals BuiltinBLS12_381_G2_Element
a BuiltinBLS12_381_G2_Element
b)

{-# INLINABLE bls12_381_G2_add #-}
bls12_381_G2_add :: BuiltinBLS12_381_G2_Element -> BuiltinBLS12_381_G2_Element -> BuiltinBLS12_381_G2_Element
bls12_381_G2_add :: BuiltinBLS12_381_G2_Element
-> BuiltinBLS12_381_G2_Element -> BuiltinBLS12_381_G2_Element
bls12_381_G2_add = BuiltinBLS12_381_G2_Element
-> BuiltinBLS12_381_G2_Element -> BuiltinBLS12_381_G2_Element
BI.bls12_381_G2_add

{-# INLINABLE bls12_381_G2_scalarMul #-}
bls12_381_G2_scalarMul :: Integer -> BuiltinBLS12_381_G2_Element -> BuiltinBLS12_381_G2_Element
bls12_381_G2_scalarMul :: Integer
-> BuiltinBLS12_381_G2_Element -> BuiltinBLS12_381_G2_Element
bls12_381_G2_scalarMul = Integer
-> BuiltinBLS12_381_G2_Element -> BuiltinBLS12_381_G2_Element
BI.bls12_381_G2_scalarMul

{-# INLINABLE bls12_381_G2_neg #-}
bls12_381_G2_neg :: BuiltinBLS12_381_G2_Element -> BuiltinBLS12_381_G2_Element
bls12_381_G2_neg :: BuiltinBLS12_381_G2_Element -> BuiltinBLS12_381_G2_Element
bls12_381_G2_neg = BuiltinBLS12_381_G2_Element -> BuiltinBLS12_381_G2_Element
BI.bls12_381_G2_neg

{-# INLINABLE bls12_381_G2_compress #-}
bls12_381_G2_compress :: BuiltinBLS12_381_G2_Element -> BuiltinByteString
bls12_381_G2_compress :: BuiltinBLS12_381_G2_Element -> BuiltinByteString
bls12_381_G2_compress = BuiltinBLS12_381_G2_Element -> BuiltinByteString
BI.bls12_381_G2_compress

{-# INLINABLE bls12_381_G2_uncompress #-}
bls12_381_G2_uncompress :: BuiltinByteString -> BuiltinBLS12_381_G2_Element
bls12_381_G2_uncompress :: BuiltinByteString -> BuiltinBLS12_381_G2_Element
bls12_381_G2_uncompress = BuiltinByteString -> BuiltinBLS12_381_G2_Element
BI.bls12_381_G2_uncompress

{-# INLINABLE bls12_381_G2_hashToGroup #-}
bls12_381_G2_hashToGroup :: BuiltinByteString -> BuiltinByteString -> BuiltinBLS12_381_G2_Element
bls12_381_G2_hashToGroup :: BuiltinByteString
-> BuiltinByteString -> BuiltinBLS12_381_G2_Element
bls12_381_G2_hashToGroup = BuiltinByteString
-> BuiltinByteString -> BuiltinBLS12_381_G2_Element
BI.bls12_381_G2_hashToGroup

{-# INLINABLE bls12_381_G2_compressed_zero #-}
bls12_381_G2_compressed_zero :: BuiltinByteString
bls12_381_G2_compressed_zero :: BuiltinByteString
bls12_381_G2_compressed_zero = BuiltinByteString
BI.bls12_381_G2_compressed_zero

{-# INLINABLE bls12_381_G2_compressed_generator #-}
bls12_381_G2_compressed_generator :: BuiltinByteString
bls12_381_G2_compressed_generator :: BuiltinByteString
bls12_381_G2_compressed_generator = BuiltinByteString
BI.bls12_381_G2_compressed_generator

-- Pairing --
{-# INLINABLE bls12_381_millerLoop #-}
bls12_381_millerLoop :: BuiltinBLS12_381_G1_Element -> BuiltinBLS12_381_G2_Element -> BuiltinBLS12_381_MlResult
bls12_381_millerLoop :: BuiltinBLS12_381_G1_Element
-> BuiltinBLS12_381_G2_Element -> BuiltinBLS12_381_MlResult
bls12_381_millerLoop = BuiltinBLS12_381_G1_Element
-> BuiltinBLS12_381_G2_Element -> BuiltinBLS12_381_MlResult
BI.bls12_381_millerLoop

{-# INLINABLE bls12_381_mulMlResult #-}
bls12_381_mulMlResult ::  BuiltinBLS12_381_MlResult -> BuiltinBLS12_381_MlResult -> BuiltinBLS12_381_MlResult
bls12_381_mulMlResult :: BuiltinBLS12_381_MlResult
-> BuiltinBLS12_381_MlResult -> BuiltinBLS12_381_MlResult
bls12_381_mulMlResult = BuiltinBLS12_381_MlResult
-> BuiltinBLS12_381_MlResult -> BuiltinBLS12_381_MlResult
BI.bls12_381_mulMlResult

{-# INLINABLE bls12_381_finalVerify #-}
bls12_381_finalVerify :: BuiltinBLS12_381_MlResult -> BuiltinBLS12_381_MlResult -> Bool
bls12_381_finalVerify :: BuiltinBLS12_381_MlResult -> BuiltinBLS12_381_MlResult -> Bool
bls12_381_finalVerify BuiltinBLS12_381_MlResult
a BuiltinBLS12_381_MlResult
b = BuiltinBool -> Bool
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (BuiltinBLS12_381_MlResult
-> BuiltinBLS12_381_MlResult -> BuiltinBool
BI.bls12_381_finalVerify BuiltinBLS12_381_MlResult
a BuiltinBLS12_381_MlResult
b)

-- Bitwise conversions

-- The PLC builtins take a boolean argument to indicate the endianness of the
-- conversion, but here we use GHC.ByteOrder.ByteOrder for clarity.
byteOrderToBool :: ByteOrder -> Bool
byteOrderToBool :: ByteOrder -> Bool
byteOrderToBool ByteOrder
BigEndian    = Bool
True
byteOrderToBool ByteOrder
LittleEndian = Bool
False
{-# INLINABLE byteOrderToBool #-}

-- | Convert a 'BuiltinInteger' into a 'BuiltinByteString', as described in
-- [CIP-121](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0121).
-- The first argument indicates the endianness of the conversion and the third
-- argument is the integer to be converted, which must be non-negative.  The
-- second argument must also be non-negative and it indicates the required width
-- of the output.  If the width is zero then the output is the smallest
-- bytestring which can contain the converted input (and in this case, the
-- integer 0 encodes to the empty bytestring).  If the width is nonzero then the
-- output bytestring will be padded to the required width with 0x00 bytes (on
-- the left for big-endian conversions and on the right for little-endian
-- conversions); if the input integer is too big to fit into a bytestring of the
-- specified width then the conversion will fail.  Conversion will also fail if
-- the specified width is greater than 8192 or the input integer is too big to
-- fit into a bytestring of length 8192.
{-# INLINABLE integerToByteString #-}
integerToByteString :: ByteOrder -> Integer -> Integer -> BuiltinByteString
integerToByteString :: ByteOrder -> Integer -> Integer -> BuiltinByteString
integerToByteString ByteOrder
endianness = BuiltinBool -> Integer -> Integer -> BuiltinByteString
BI.integerToByteString (Bool -> BuiltinBool
forall a arep. HasToOpaque a arep => a -> arep
toOpaque (ByteOrder -> Bool
byteOrderToBool ByteOrder
endianness))

-- | Convert a 'BuiltinByteString' to a 'BuiltinInteger', as described in
-- [CIP-121](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0121).
-- The first argument indicates the endianness of the conversion and the second
-- is the bytestring to be converted.  There is no limitation on the size of
-- the bytestring.  The empty bytestring is converted to the integer 0.
{-# INLINABLE byteStringToInteger #-}
byteStringToInteger :: ByteOrder -> BuiltinByteString -> Integer
byteStringToInteger :: ByteOrder -> BuiltinByteString -> Integer
byteStringToInteger ByteOrder
endianness =
  BuiltinBool -> BuiltinByteString -> Integer
BI.byteStringToInteger (Bool -> BuiltinBool
forall a arep. HasToOpaque a arep => a -> arep
toOpaque (ByteOrder -> Bool
byteOrderToBool ByteOrder
endianness))

-- Bitwise operations

-- | Shift a 'BuiltinByteString', as per
-- [CIP-123](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0123).
{-# INLINEABLE shiftByteString #-}
shiftByteString :: BuiltinByteString -> Integer -> BuiltinByteString
shiftByteString :: BuiltinByteString -> Integer -> BuiltinByteString
shiftByteString = BuiltinByteString -> Integer -> BuiltinByteString
BI.shiftByteString

-- | Rotate a 'BuiltinByteString', as per
-- [CIP-123](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0123).
{-# INLINEABLE rotateByteString #-}
rotateByteString :: BuiltinByteString -> Integer -> BuiltinByteString
rotateByteString :: BuiltinByteString -> Integer -> BuiltinByteString
rotateByteString = BuiltinByteString -> Integer -> BuiltinByteString
BI.rotateByteString

-- | Count the set bits in a 'BuiltinByteString', as per
-- [CIP-123](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0123).
{-# INLINEABLE countSetBits #-}
countSetBits :: BuiltinByteString -> Integer
countSetBits :: BuiltinByteString -> Integer
countSetBits = BuiltinByteString -> Integer
BI.countSetBits

-- | Find the lowest index of a set bit in a 'BuiltinByteString', as per
-- [CIP-123](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0123).
--
-- If given a 'BuiltinByteString' which consists only of zero bytes (including the empty
-- 'BuiltinByteString', this returns @-1@.
{-# INLINEABLE findFirstSetBit #-}
findFirstSetBit :: BuiltinByteString -> Integer
findFirstSetBit :: BuiltinByteString -> Integer
findFirstSetBit = BuiltinByteString -> Integer
BI.findFirstSetBit

-- Logical operations

-- | Perform logical AND on two 'BuiltinByteString' arguments, as described in
-- [CIP-122](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0122#bitwiselogicaland).
--
-- The first argument indicates whether padding semantics should be used or not;
-- if 'False', truncation semantics will be used instead.
--
-- = See also
--
-- * [Padding and truncation
-- semantics](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0122#padding-versus-truncation-semantics)
-- * [Bit indexing
-- scheme](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0122#bit-indexing-scheme)
{-# INLINEABLE andByteString #-}
andByteString ::
  Bool ->
  BuiltinByteString ->
  BuiltinByteString ->
  BuiltinByteString
andByteString :: Bool -> BuiltinByteString -> BuiltinByteString -> BuiltinByteString
andByteString Bool
b = BuiltinBool
-> BuiltinByteString -> BuiltinByteString -> BuiltinByteString
BI.andByteString (Bool -> BuiltinBool
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Bool
b)

-- | Perform logical OR on two 'BuiltinByteString' arguments, as described
-- [here](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0122#bitwiselogicalor).
--
-- The first argument indicates whether padding semantics should be used or not;
-- if 'False', truncation semantics will be used instead.
--
-- = See also
--
-- * [Padding and truncation
-- semantics](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0122#padding-versus-truncation-semantics)
-- * [Bit indexing
-- scheme](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0122#bit-indexing-scheme)
{-# INLINEABLE orByteString #-}
orByteString ::
  Bool ->
  BuiltinByteString ->
  BuiltinByteString ->
  BuiltinByteString
orByteString :: Bool -> BuiltinByteString -> BuiltinByteString -> BuiltinByteString
orByteString Bool
b = BuiltinBool
-> BuiltinByteString -> BuiltinByteString -> BuiltinByteString
BI.orByteString (Bool -> BuiltinBool
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Bool
b)

-- | Perform logical XOR on two 'BuiltinByteString' arguments, as described
-- [here](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0122#bitwiselogicalxor).
--
-- The first argument indicates whether padding semantics should be used or not;
-- if 'False', truncation semantics will be used instead.
--
-- = See also
--
-- * [Padding and truncation
-- semantics](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0122#padding-versus-truncation-semantics)
-- * [Bit indexing
-- scheme](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0122#bit-indexing-scheme)
{-# INLINEABLE xorByteString #-}
xorByteString ::
  Bool ->
  BuiltinByteString ->
  BuiltinByteString ->
  BuiltinByteString
xorByteString :: Bool -> BuiltinByteString -> BuiltinByteString -> BuiltinByteString
xorByteString Bool
b = BuiltinBool
-> BuiltinByteString -> BuiltinByteString -> BuiltinByteString
BI.xorByteString (Bool -> BuiltinBool
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Bool
b)

-- | Perform logical complement on a 'BuiltinByteString', as described
-- [here](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0122#bitwiselogicalcomplement).
--
-- = See also
--
-- * [Bit indexing
-- scheme](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0122#bit-indexing-scheme)
{-# INLINEABLE complementByteString #-}
complementByteString ::
  BuiltinByteString ->
  BuiltinByteString
complementByteString :: BuiltinByteString -> BuiltinByteString
complementByteString = BuiltinByteString -> BuiltinByteString
BI.complementByteString

-- | Read a bit at the _bit_ index given by the 'Integer' argument in the
-- 'BuiltinByteString' argument. The result will be 'True' if the corresponding bit is set, and
-- 'False' if it is clear. Will error if given an out-of-bounds index argument; that is, if the
-- index is either negative, or equal to or greater than the total number of bits in the
-- 'BuiltinByteString' argument.
--
-- = See also
--
-- * [Bit indexing
-- scheme](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0122#bit-indexing-scheme)
-- * [Operation
-- description](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0122#readbit)
{-# INLINEABLE readBit #-}
readBit ::
  BuiltinByteString ->
  Integer ->
  Bool
readBit :: BuiltinByteString -> Integer -> Bool
readBit BuiltinByteString
bs Integer
i = BuiltinBool -> Bool
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (BuiltinByteString -> Integer -> BuiltinBool
BI.readBit BuiltinByteString
bs Integer
i)

-- | Given a 'BuiltinByteString', a list of indexes to change, and a boolean
-- value 'b' to change those indexes to, set the /bit/ at each of the specified
-- index as follows:
--
-- * If 'b' is 'True', set that bit;
-- * Otherwise, clear that bit.
--
-- Will error if any of the indexes are out-of-bounds: that is, if the index is either negative, or
-- equal to or greater than the total number of bits in the 'BuiltinByteString' argument.
--
-- = Note
--
-- This differs slightly from the description of the [corresponding operation in
-- CIP-122](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0122#writebits);
-- instead of a single changelist argument comprised of pairs, we instead pass a
-- single list of indexes to change, and a single boolean value to change those
-- indexes to. The original proposal allowed one to set and clear bits in a
-- single operation, but constructing the list of boolean values for the updates
-- was somewhat expensive.  If it's really necessary to set some bits and clear
-- others then it is easier to call the function twice, once to set bits and
-- and once to clear them.
--
-- = See also
--
-- * [Bit indexing
-- scheme](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0122#bit-indexing-scheme)
-- * [Operation
-- description](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0122#writebits)
{-# INLINEABLE writeBits #-}
writeBits ::
  BuiltinByteString ->
  [Integer] ->
  Bool ->
  BuiltinByteString
writeBits :: BuiltinByteString -> [Integer] -> Bool -> BuiltinByteString
writeBits BuiltinByteString
bs [Integer]
ixes Bool
bit = BuiltinByteString
-> BuiltinList Integer -> BuiltinBool -> BuiltinByteString
BI.writeBits BuiltinByteString
bs ([Integer] -> BuiltinList Integer
forall a arep. HasToOpaque a arep => a -> arep
toOpaque [Integer]
ixes) (Bool -> BuiltinBool
forall a arep. HasToOpaque a arep => a -> arep
toOpaque Bool
bit)

-- | Given a length (first argument) and a byte (second argument), produce a 'BuiltinByteString' of
-- that length, with that byte in every position. Will error if given a negative length, or a second
-- argument that isn't a byte (less than 0, greater than 255).
--
-- = See also
--
-- * [Operation
-- description](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0122#replicateByteString)
{-# INLINEABLE replicateByte #-}
replicateByte ::
  Integer ->
  Integer ->
  BuiltinByteString
replicateByte :: Integer -> Integer -> BuiltinByteString
replicateByte = Integer -> Integer -> BuiltinByteString
BI.replicateByte


-- | FIXME
--
-- = See also
--
-- * [Operation
-- description](https://github.com/cardano-foundation/CIPs/tree/master/CIP-0109)
{-# INLINEABLE expModInteger #-}
expModInteger ::
  Integer ->
  Integer ->
  Integer ->
  Integer
expModInteger :: Integer -> Integer -> Integer -> Integer
expModInteger = Integer -> Integer -> Integer -> Integer
BI.expModInteger