plutus-core-1.36.0.0: Language library for Plutus Core
Safe HaskellSafe-Inferred
LanguageHaskell2010

PlutusCore.Bitwise

Description

Implementations for CIP-121, CIP-122 and CIP-123. Grouped because they all operate on ByteStrings, and require similar functionality.

Synopsis

Documentation

integerToByteString :: Bool -> Integer -> Integer -> BuiltinResult ByteString Source #

Wrapper for unsafeIntegerToByteString to make it more convenient to define as a builtin.

byteStringToInteger :: Bool -> ByteString -> Integer Source #

Conversion from ByteString to Integer, as per CIP-121.

Wrapper for unsafeByteStringToInteger to make it more convenient to define as a builtin.

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.

shiftByteString :: ByteString -> Integer -> ByteString Source #

Wrapper for calling unsafesShiftByteString safely. Specifically, we avoid various edge cases:

  • Empty ByteStrings 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.

rotateByteString :: ByteString -> Integer -> ByteString Source #

Wrapper for calling unsafeRotateByteString safely. Specifically, we avoid various edge cases:

  • Empty ByteStrings and zero moves don't do anything
  • Bit moves whose absolute value is larger than the bit length are reduced modulo the length

Furthermore, we can convert all rotations into positive rotations, by noting that a rotation by b is the same as a rotation by b mod bitLen, where bitLen 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.

readBit :: ByteString -> Int -> BuiltinResult Bool Source #

Bit read at index, as per CIP-122

replicateByte :: Integer -> Word8 -> BuiltinResult ByteString Source #

Byte replication, as per CIP-122 We want to cautious about the allocation of huge amounts of memory so we impose the same length limit that's used in integerToByteString.

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.