Description
The PlutusTx Prelude is a replacement for the Haskell Prelude that works
better with Plutus Tx. You should use it if you're writing code that
will be compiled with the Plutus Tx compiler.
:set -XNoImplicitPrelude
import PlutusTx.Prelude
Classes
Monad
(>>=) :: Monad m => m a -> (a -> m b) -> m b infixl 1 #
Sequentially compose two actions, passing any value produced
by the first as an argument to the second.
'as >>= bs' can be understood as the do expression
do a <- as
bs a
(=<<) :: Monad m => (a -> m b) -> m a -> m b infixr 1 #
Same as >>=, but with the arguments interchanged.
(>>) :: Monad m => m a -> m b -> m b infixl 1 #
Sequentially compose two actions, discarding any value produced
by the first, like sequencing operators (such as the semicolon)
in imperative languages.
'as >> bs' can be understood as the do expression
do as
bs
Error
error :: () -> a #
Aborts evaluation with an error.
Booleans
Integer numbers
data Integer #
Arbitrary precision integers. In contrast with fixed-size integral types
such as Int, the Integer type represents the entire infinite range of
integers.
Integers are stored in a kind of sign-magnitude form, hence do not expect
two's complement form when using bit operations.
If the value is small (fit into an Int), IS constructor is used.
Otherwise Integer and IN constructors are used to store a BigNat
representing respectively the positive or the negative value magnitude.
Invariant: Integer and Pretty Integer>>> pretty (2^123 :: Integer)
10633823966279326983230456482242756608
| Instance detailsDefined in PrettyBy config Integer | >>> prettyBy () (2^(123 :: Int) :: Integer)
10633823966279326983230456482242756608
|
Instance detailsDefined in DefaultUni Integer) |
Maybe
Either
ByteStrings
data BuiltinByteString #
An opaque type representing Plutus Core ByteStrings.
Instances
Instances details
Bit operations
writeBits :: BuiltinByteString -> [Integer] -> Bool -> BuiltinByteString #
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;
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
replicateByte :: Integer -> Integer -> BuiltinByteString #
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
Hashes and Signatures
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.
verifyEcdsaSecp256k1Signature #
Given an ECDSA SECP256k1 verification key, an ECDSA SECP256k1 signature,
and an ECDSA SECP256k1 message hash (all as BuiltinByteStrings), 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. Other than
length, we make no requirements of what hash gets used.
See also
verifySchnorrSecp256k1Signature #
Given a Schnorr SECP256k1 verification key, a Schnorr SECP256k1 signature,
and a message (all as BuiltinByteStrings), 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
Rational numbers
data Rational #
Represents an arbitrary-precision ratio.
The following two invariants are maintained:
- The denominator is greater than zero.
- The numerator and denominator are coprime.
Instances
Instances details
unsafeRatio :: Integer -> Integer -> Rational #
Makes a Rational from a numerator and a denominator.
Important note
If given a zero denominator, this function will error. If you don't mind a
size increase, and care about safety, use ratio instead.
Other builtin Types
data BuiltinData #
A type corresponding to the Plutus Core builtin equivalent of Data.
The point of this type is to be an opaque equivalent of Data, so as to
ensure that it is only used in ways that the compiler can handle.
As such, you should use this type in your on-chain code, and in any data structures
that you want to be representable on-chain.
For off-chain usage, there are conversion functions builtinDataToData and
dataToBuiltinData, but note that these will not work on-chain.
Instances
Instances details
To/from Data
class ToData (a :: Type) where #
A typeclass for types that can be converted to and from BuiltinData.
Instances
Instances details
| ToData Void # | |
Instance detailsDefined in PlutusTx.IsData.Class |
| ToData BuiltinBLS12_381_G1_Element # | For the BLS12-381 G1 and G2 types we use the compress functions to convert
to a ByteString and then encode that as Data as usual. We have to be more
careful going the other way because we decode a Data object to (possibly) get
a BuiltinByteString and then uncompress the underlying ByteString to get a
group element. However uncompression can fail so we have to check what
happens: we don't use bls12_381_G?_uncompress because that invokes error if
something goes wrong (but we do use it for unsafeFromData). |
Instance detailsDefined in PlutusTx.IsData.Class |
| ToData BuiltinBLS12_381_G2_Element # | |
Instance detailsDefined in PlutusTx.IsData.Class |
| (TypeError ('Text "toBuiltinData is not supported for BuiltinBLS12_381_MlResult") :: Constraint) => ToData BuiltinBLS12_381_MlResult # | We do not provide instances of any of these classes for
BuiltinBLS12_381_MlResult since there is no serialisation format: we expect
that values of that type will only occur as the result of on-chain
computations. |
Instance detailsDefined in PlutusTx.IsData.Class |
| ToData BuiltinByteString # | |
Instance detailsDefined in PlutusTx.IsData.Class |
| ToData BuiltinData # | |
Instance detailsDefined in PlutusTx.IsData.Class |
| ToData Rational # | |
Instance detailsDefined in PlutusTx.Ratio |
| ToData Sqrt # | |
Instance detailsDefined in PlutusTx.Sqrt |
| ToData Integer # | |
Instance detailsDefined in PlutusTx.IsData.Class |
| ToData () # | |
Instance detailsDefined in PlutusTx.IsData.Instances |
| ToData Bool # | |
Instance detailsDefined in PlutusTx.IsData.Instances |
| (TypeError ('Text "Int is not supported, use Integer instead") :: Constraint) => ToData Int # | |
Instance detailsDefined in PlutusTx.IsData.Class |
| ToData (List a) # | |
Instance detailsDefined in PlutusTx.Data.List |
| ToData a => ToData (Maybe a) # | |
Instance detailsDefined in PlutusTx.IsData.Instances |
| ToData a => ToData [a] # | |
Instance detailsDefined in PlutusTx.IsData.Class |
| (ToData a, ToData b) => ToData (Either a b) # | |
Instance detailsDefined in PlutusTx.IsData.Instances |
| (ToData k, ToData v) => ToData (Map k v) # | Hand-written instances to use the underlying Map type in Data, and
to be reasonably efficient. |
Instance detailsDefined in PlutusTx.AssocMap |
| ToData (Map k a) # | |
Instance detailsDefined in PlutusTx.Data.AssocMap |
| (ToData a, ToData b) => ToData (These a b) # | |
Instance detailsDefined in PlutusTx.These |
| (ToData a, ToData b) => ToData (a, b) # | |
Instance detailsDefined in PlutusTx.IsData.Instances |
| (ToData a, ToData b, ToData c) => ToData (a, b, c) # | |
Instance detailsDefined in PlutusTx.IsData.Instances |
| (ToData a, ToData b, ToData c, ToData d) => ToData (a, b, c, d) # | |
Instance detailsDefined in PlutusTx.IsData.Instances |
class FromData (a :: Type) where #
Instances
Instances details
| FromData Void # | |
Instance detailsDefined in PlutusTx.IsData.Class |
| FromData BuiltinBLS12_381_G1_Element # | |
Instance detailsDefined in PlutusTx.IsData.Class |
| FromData BuiltinBLS12_381_G2_Element # | |
Instance detailsDefined in PlutusTx.IsData.Class |
| (TypeError ('Text "fromBuiltinData is not supported for BuiltinBLS12_381_MlResult") :: Constraint) => FromData BuiltinBLS12_381_MlResult # | |
Instance detailsDefined in PlutusTx.IsData.Class |
| FromData BuiltinByteString # | |
Instance detailsDefined in PlutusTx.IsData.Class |
| FromData BuiltinData # | |
Instance detailsDefined in PlutusTx.IsData.Class |
| FromData Rational # | |
Instance detailsDefined in PlutusTx.Ratio |
| FromData Sqrt # | |
Instance detailsDefined in PlutusTx.Sqrt |
| FromData Integer # | |
Instance detailsDefined in PlutusTx.IsData.Class |
| FromData () # | |
Instance detailsDefined in PlutusTx.IsData.Instances |
| FromData Bool # | |
Instance detailsDefined in PlutusTx.IsData.Instances |
| (TypeError ('Text "Int is not supported, use Integer instead") :: Constraint) => FromData Int # | |
Instance detailsDefined in PlutusTx.IsData.Class |
| FromData (List a) # | |
Instance detailsDefined in PlutusTx.Data.List |
| FromData a => FromData (Maybe a) # | |
Instance detailsDefined in PlutusTx.IsData.Instances |
| FromData a => FromData [a] # | |
Instance detailsDefined in PlutusTx.IsData.Class |
| (FromData a, FromData b) => FromData (Either a b) # | |
Instance detailsDefined in PlutusTx.IsData.Instances |
| (FromData k, FromData v) => FromData (Map k v) # | A hand-written transformation from Data to Map. Compared to unsafeFromBuiltinData,
it is safe to call when it is unknown if the Data is built with Datas Map constructor.
Note that it is, however, unsafe in the sense that it assumes that any map
encoded in the Data is well-formed, i.e. fromBuiltinData does not perform any
deduplication of keys or of key-value pairs! |
Instance detailsDefined in PlutusTx.AssocMap |
| FromData (Map k a) # | |
Instance detailsDefined in PlutusTx.Data.AssocMap |
| (FromData a, FromData b) => FromData (These a b) # | |
Instance detailsDefined in PlutusTx.These |
| (FromData a, FromData b) => FromData (a, b) # | |
Instance detailsDefined in PlutusTx.IsData.Instances |
| (FromData a, FromData b, FromData c) => FromData (a, b, c) # | |
Instance detailsDefined in PlutusTx.IsData.Instances |
| (FromData a, FromData b, FromData c, FromData d) => FromData (a, b, c, d) # | |
Instance detailsDefined in PlutusTx.IsData.Instances |
class UnsafeFromData (a :: Type) where #
Instances
Instances details
| UnsafeFromData Void # | |
Instance detailsDefined in PlutusTx.IsData.Class |
| UnsafeFromData BuiltinBLS12_381_G1_Element # | |
Instance detailsDefined in PlutusTx.IsData.Class |
| UnsafeFromData BuiltinBLS12_381_G2_Element # | |
Instance detailsDefined in PlutusTx.IsData.Class |
| (TypeError ('Text "unsafeFromBuiltinData is not supported for BuiltinBLS12_381_MlResult") :: Constraint) => UnsafeFromData BuiltinBLS12_381_MlResult # | |
Instance detailsDefined in PlutusTx.IsData.Class |
| UnsafeFromData BuiltinByteString # | |
Instance detailsDefined in PlutusTx.IsData.Class |
| UnsafeFromData BuiltinData # | |
Instance detailsDefined in PlutusTx.IsData.Class |
| UnsafeFromData Rational # | |
Instance detailsDefined in PlutusTx.Ratio |
| UnsafeFromData Sqrt # | |
Instance detailsDefined in PlutusTx.Sqrt |
| UnsafeFromData Integer # | |
Instance detailsDefined in PlutusTx.IsData.Class |
| UnsafeFromData () # | |
Instance detailsDefined in PlutusTx.IsData.Instances |
| UnsafeFromData Bool # | |
Instance detailsDefined in PlutusTx.IsData.Instances |
| (TypeError ('Text "Int is not supported, use Integer instead") :: Constraint) => UnsafeFromData Int # | |
Instance detailsDefined in PlutusTx.IsData.Class |
| UnsafeFromData (List a) # | |
Instance detailsDefined in PlutusTx.Data.List |
| UnsafeFromData a => UnsafeFromData (Maybe a) # | |
Instance detailsDefined in PlutusTx.IsData.Instances |
| UnsafeFromData a => UnsafeFromData [a] # | |
Instance detailsDefined in PlutusTx.IsData.Class |
| (UnsafeFromData a, UnsafeFromData b) => UnsafeFromData (Either a b) # | |
Instance detailsDefined in PlutusTx.IsData.Instances |
| (UnsafeFromData k, UnsafeFromData v) => UnsafeFromData (Map k v) # | A hand-written transformation from Data to Map. It is unsafe because the
caller must provide the guarantee that the Data is constructed using the Datas
Map constructor.
Note that it assumes, like the fromBuiltinData transformation, that the map
encoded in the Data is well-formed, i.e. unsafeFromBuiltinData does not perform
any deduplication of keys or of key-value pairs! |
Instance detailsDefined in PlutusTx.AssocMap |
| UnsafeFromData (Map k a) # | |
Instance detailsDefined in PlutusTx.Data.AssocMap |
| (UnsafeFromData a, UnsafeFromData b) => UnsafeFromData (These a b) # | |
Instance detailsDefined in PlutusTx.These |
| (UnsafeFromData a, UnsafeFromData b) => UnsafeFromData (a, b) # | |
Instance detailsDefined in PlutusTx.IsData.Instances |
| (UnsafeFromData a, UnsafeFromData b, UnsafeFromData c) => UnsafeFromData (a, b, c) # | |
Instance detailsDefined in PlutusTx.IsData.Instances |
| (UnsafeFromData a, UnsafeFromData b, UnsafeFromData c, UnsafeFromData d) => UnsafeFromData (a, b, c, d) # | |
Instance detailsDefined in PlutusTx.IsData.Instances |
BLS12_381
Conversions
integerToByteString :: ByteOrder -> Integer -> Integer -> BuiltinByteString #
Convert a BuiltinInteger into a BuiltinByteString, as described in
CIP-121.
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.