This module implements arithmetic operations in a constrained 256-bit integer field. Operations are performed modulo , providing a field for cryptographic operations that require 32-byte values.
The module provides functionality for basic arithmetic operations (addition, subtraction, multiplication) within this constrained field, as well as conversion functions between different representations.
Types
Bits256
State
Alias
State = bitwise.State<Bits256>Constants
field
field: IntThe prime defining the 256-bit integer field
field_size
field_size: Int = 32Functions
Constructing
from_bytearray_big_endian
from_bytearray_big_endian(bytes: ByteArray) -> StateConstructs a new Bits256 element from a Big-Endian (most-significant bits first) ByteArray.
from_bytearray_little_endian
from_bytearray_little_endian(bytes: ByteArray) -> StateConstructs a new Bits256 element from a Little-Endian (least-significant bits first) ByteArray.
from_int
from_int(int: Int) -> StateConstructs a new Bits256 element from an integer, ensuring it’s within the valid range of the field.
Modifying
scale
scale(self: State, e: Int) -> StateExponentiates a Bits256 element by a non-negative integer exponent, using repeated squaring.
Note that this function returns zero for negative exponents.
scale2
scale2(self: State, k: Int) -> StateA faster version of scale for the case where the exponent is a power of two.
That is, the exponent for some non-negative integer .
Combining
add
add(left: State, right: State) -> StateAdds two Bits256 elements, ensuring the result stays within the finite field range.
add_bytes
add_bytes(self: State, bytes: ByteArray) -> StateAdds a ByteArray to a Bits256 element, interpreting bytes as a big-endian number.
add_int
add_int(self: State, int: Int) -> StateAdds an integer to a Bits256 element.
mul
mul(left: State, right: State) -> StateMultiplies two Bits256 elements, with the result constrained within the finite field.
mul_bytes
mul_bytes(self: State, bytes: ByteArray) -> StateMultiplies a Bits256 element by a ByteArray, interpreting bytes as a big-endian number.
mul_int
mul_int(self: State, int: Int) -> StateMultiplies a Bits256 element by an integer.
neg
neg(self: State) -> StateCalculates the additive inverse of a Bits256 element.
sub
sub(left: State, right: State) -> StateSubtracts one Bits256 element from another, with the result wrapped within the finite field range.
sub_bytes
sub_bytes(self: State, bytes: ByteArray) -> StateSubtracts a ByteArray from a Bits256 element, interpreting bytes as a big-endian number.
sub_int
sub_int(self: State, int: Int) -> StateSubtracts an integer from a Bits256 element.
Transforming
to_bytearray_big_endian
to_bytearray_big_endian(self: State) -> ByteArrayConverts a Bits256 element to a Big-Endian (most-significant bits first) ByteArray.
to_bytearray_little_endian
to_bytearray_little_endian(self: State) -> ByteArrayConverts a Bits256 element to a Little-Endian (least-significant bits first) ByteArray.
to_int
to_int(self: State) -> IntConverts a Bits256 element back to its integer representation.