Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
PlutusCore.Bitwise
Contents
Description
Implementations for CIP-121, CIP-122 and CIP-123. Grouped because they all operate on
ByteString
s, and require similar functionality.
Synopsis
- integerToByteStringWrapper ∷ Bool → Integer → Integer → BuiltinResult ByteString
- byteStringToIntegerWrapper ∷ Bool → ByteString → Integer
- shiftByteStringWrapper ∷ ByteString → Integer → ByteString
- rotateByteStringWrapper ∷ ByteString → Integer → ByteString
- writeBitsWrapper ∷ ByteString → [Integer] → [Bool] → BuiltinResult ByteString
- data IntegerToByteStringError
- integerToByteStringMaximumOutputLength ∷ Integer
- integerToByteString ∷ ByteOrder → Int → Integer → Either IntegerToByteStringError ByteString
- byteStringToInteger ∷ ByteOrder → ByteString → Integer
- andByteString ∷ Bool → ByteString → ByteString → ByteString
- orByteString ∷ Bool → ByteString → ByteString → ByteString
- xorByteString ∷ Bool → ByteString → ByteString → ByteString
- complementByteString ∷ ByteString → ByteString
- readBit ∷ ByteString → Int → BuiltinResult Bool
- writeBits ∷ ByteString → [(Integer, Bool)] → BuiltinResult ByteString
- replicateByte ∷ Int → Word8 → BuiltinResult ByteString
- shiftByteString ∷ ByteString → Int → ByteString
- rotateByteString ∷ ByteString → Int → ByteString
- countSetBits ∷ ByteString → Int
- findFirstSetBit ∷ ByteString → Int
Wrappers
integerToByteStringWrapper ∷ Bool → Integer → Integer → BuiltinResult ByteString Source #
Wrapper for integerToByteString
to make it more convenient to define as a builtin.
byteStringToIntegerWrapper ∷ Bool → ByteString → Integer Source #
Wrapper for byteStringToInteger
to make it more convenient to define as a builtin.
shiftByteStringWrapper ∷ ByteString → Integer → ByteString Source #
Wrapper for calling shiftByteString
safely. Specifically, we avoid various edge cases:
- Empty
ByteString
s and zero moves don't do anything - Bit moves whose absolute value is larger than the bit length produce all-zeroes
This also ensures we don't accidentally hit integer overflow issues.
rotateByteStringWrapper ∷ ByteString → Integer → ByteString Source #
Wrapper for calling rotateByteString
safely. Specifically, we avoid various edge cases:
- Empty
ByteString
s and zero moves don't do anything - Bit moves whose absolute value is larger than the bit length gets modulo reduced
Furthermore, we can convert all rotations into positive rotations, by noting that a rotation by b
is the same as a rotation by b
, where mod
bitLenbitLen
is the length of the ByteString
argument in bits. This value is always non-negative, and if we get 0, we have nothing to do. This
reduction also helps us avoid integer overflow issues.
writeBitsWrapper ∷ ByteString → [Integer] → [Bool] → BuiltinResult ByteString Source #
Needed due to the complexities of passing lists of pairs as arguments. Effectively, we pass the second argument as required by CIP-122 in its 'unzipped' form, truncating mismatches.
Implementation details
data IntegerToByteStringError Source #
Structured type to help indicate conversion errors.
Constructors
NegativeInput | |
NotEnoughDigits |
Instances
Show IntegerToByteStringError Source # | |
Defined in PlutusCore.Bitwise | |
Eq IntegerToByteStringError Source # | |
Defined in PlutusCore.Bitwise |
integerToByteString ∷ ByteOrder → Int → Integer → Either IntegerToByteStringError ByteString Source #
Conversion from Integer
to ByteString
, as per
CIP-121.
For performance and clarity, the endianness argument uses
ByteOrder
, and the length argument is an Int
.
byteStringToInteger ∷ ByteOrder → ByteString → Integer Source #
Conversion from ByteString
to Integer
, as per
CIP-121.
For clarity, the stated endianness argument uses ByteOrder
.
andByteString ∷ Bool → ByteString → ByteString → ByteString Source #
Bitwise logical AND, as per CIP-122.
orByteString ∷ Bool → ByteString → ByteString → ByteString Source #
Bitwise logical OR, as per CIP-122.
xorByteString ∷ Bool → ByteString → ByteString → ByteString Source #
Bitwise logical XOR, as per CIP-122.
complementByteString ∷ ByteString → ByteString Source #
Bitwise logical complement, as per CIP-122.
readBit ∷ ByteString → Int → BuiltinResult Bool Source #
Bit read at index, as per CIP-122
writeBits ∷ ByteString → [(Integer, Bool)] → BuiltinResult ByteString Source #
Bulk bit write, as per CIP-122
replicateByte ∷ Int → Word8 → BuiltinResult ByteString Source #
Byte replication, as per CIP-122
shiftByteString ∷ ByteString → Int → ByteString Source #
Shifts, as per CIP-123.
rotateByteString ∷ ByteString → Int → ByteString Source #
Rotations, as per CIP-123.
countSetBits ∷ ByteString → Int Source #
Counting the number of set bits, as per CIP-123.
findFirstSetBit ∷ ByteString → Int Source #
Finding the first set bit's index, as per CIP-123.