-- 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,
  BuiltinByteStringHex (..),
  BuiltinByteStringUtf8 (..),

  -- * 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,
  BI.drop,
  uncons,
  unsafeUncons,

  -- * Arrays
  BI.BuiltinArray,
  BI.listToArray,
  sopListToArray,
  BI.lengthOfArray,
  BI.indexArray,

  -- * 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))

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

-- | 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
{-# INLINEABLE consByteString #-}

-- | 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
{-# INLINEABLE sliceByteString #-}

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

-- | 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)
{-# INLINEABLE indexByteString #-}

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

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

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

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

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

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

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

{-| 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)
{-# INLINEABLE verifyEd25519Signature #-}

-- | 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)
{-# INLINEABLE equalsByteString #-}

-- | 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)
{-# INLINEABLE lessThanByteString #-}

-- | 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)
{-# INLINEABLE lessThanEqualsByteString #-}

-- | 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
{-# INLINEABLE greaterThanByteString #-}

-- | 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
{-# INLINEABLE greaterThanEqualsByteString #-}

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

{-| 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 verifyEcdsaSecp256k1Signature #-}

{-| 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)
{-# INLINEABLE verifySchnorrSecp256k1Signature #-}

-- | 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))
{-# INLINEABLE addInteger #-}

-- | 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))
{-# INLINEABLE subtractInteger #-}

-- | 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))
{-# INLINEABLE multiplyInteger #-}

-- | 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))
{-# INLINEABLE divideInteger #-}

-- | 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))
{-# INLINEABLE modInteger #-}

-- | 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))
{-# INLINEABLE quotientInteger #-}

-- | 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))
{-# INLINEABLE remainderInteger #-}

-- | 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
{-# INLINEABLE greaterThanInteger #-}

-- | 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
{-# INLINEABLE greaterThanEqualsInteger #-}

-- | 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))
{-# INLINEABLE lessThanInteger #-}

-- | 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))
{-# INLINEABLE lessThanEqualsInteger #-}

-- | 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))
{-# INLINEABLE equalsInteger #-}

-- | 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)
{-# INLINEABLE error #-}

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

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

-- | 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)
{-# INLINEABLE equalsString #-}

-- | 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
{-# INLINEABLE trace #-}

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

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)
{-# INLINEABLE null #-}

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 ()
{-# INLINEABLE caseList #-}

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
{-# INLINEABLE 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
{-# INLINEABLE matchList' #-}

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 headMaybe #-}

-- | 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 uncons #-}

-- | 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 unsafeUncons #-}

-- | 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)
{-# INLINE pairToPair #-}

sopListToArray :: (HasToOpaque a arep, MkNil arep) => [a] -> BI.BuiltinArray arep
sopListToArray :: forall a arep.
(HasToOpaque a arep, MkNil arep) =>
[a] -> BuiltinArray arep
sopListToArray [a]
l = BuiltinList arep -> BuiltinArray arep
forall a. BuiltinList a -> BuiltinArray a
BI.listToArray ([a] -> BuiltinList arep
forall a arep. HasToOpaque a arep => a -> arep
toOpaque [a]
l)
{-# INLINEABLE sopListToArray #-}

{-| 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
{-# INLINEABLE chooseData #-}

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

-- | 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)
{-# INLINEABLE mkConstr #-}

-- | 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)
{-# INLINEABLE mkMap #-}

-- | 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)
{-# INLINEABLE mkList #-}

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

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

-- | 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)
{-# INLINEABLE unsafeDataAsConstr #-}

-- | 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)
{-# INLINEABLE unsafeDataAsMap #-}

-- | 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)
{-# INLINEABLE unsafeDataAsList #-}

-- | 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)
{-# INLINEABLE unsafeDataAsI #-}

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

-- | 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)
{-# INLINEABLE equalsData #-}

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
{-# INLINEABLE caseData #-}

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
{-# INLINEABLE 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
{-# INLINEABLE matchData #-}

-- G1 --
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)
{-# INLINEABLE bls12_381_G1_equals #-}

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
{-# INLINEABLE bls12_381_G1_add #-}

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
{-# INLINEABLE bls12_381_G1_scalarMul #-}

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
{-# INLINEABLE bls12_381_G1_neg #-}

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
{-# INLINEABLE bls12_381_G1_compress #-}

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
{-# INLINEABLE bls12_381_G1_uncompress #-}

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
{-# INLINEABLE bls12_381_G1_hashToGroup #-}

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

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

-- G2 --
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)
{-# INLINEABLE bls12_381_G2_equals #-}

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
{-# INLINEABLE bls12_381_G2_add #-}

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
{-# INLINEABLE bls12_381_G2_scalarMul #-}

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
{-# INLINEABLE bls12_381_G2_neg #-}

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
{-# INLINEABLE bls12_381_G2_compress #-}

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
{-# INLINEABLE bls12_381_G2_uncompress #-}

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
{-# INLINEABLE bls12_381_G2_hashToGroup #-}

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

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

-- Pairing --
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
{-# INLINEABLE bls12_381_millerLoop #-}

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
{-# INLINEABLE bls12_381_mulMlResult #-}

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)
{-# INLINEABLE bls12_381_finalVerify #-}

-- 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
{-# INLINEABLE 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.
-}
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))
{-# INLINEABLE integerToByteString #-}

{-| 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.
-}
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))
{-# INLINEABLE byteStringToInteger #-}

-- Bitwise operations

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

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

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

{-| 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)
-}
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)
{-# INLINEABLE orByteString #-}

{-| 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)
-}
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)
{-# INLINEABLE xorByteString #-}

{-| 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)
-}
complementByteString
  :: BuiltinByteString
  -> BuiltinByteString
complementByteString :: BuiltinByteString -> BuiltinByteString
complementByteString = BuiltinByteString -> BuiltinByteString
BI.complementByteString
{-# INLINEABLE 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)
-}
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)
{-# INLINEABLE readBit #-}

{-| 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)
-}
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)
{-# INLINEABLE writeBits #-}

{-| 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)
-}
replicateByte
  :: Integer
  -> Integer
  -> BuiltinByteString
replicateByte :: Integer -> Integer -> BuiltinByteString
replicateByte = Integer -> Integer -> BuiltinByteString
BI.replicateByte
{-# INLINEABLE replicateByte #-}

{-| FIXME(https://github.com/IntersectMBO/plutus-private/issues/1609):

= See also

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