aiken/crypto/int256

This module implements arithmetic operations in a constrained 256-bit integer field. Operations are performed modulo 22562^{256}, 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

Alias

State = bitwise.State<Bits256>

Constants

field: Int

The prime defining the 256-bit integer field 22562^{256}

field_size: Int = 32

Functions

Constructing

from_bytearray_big_endian(bytes: ByteArray) -> State

Constructs a new Bits256 element from a Big-Endian (most-significant bits first) ByteArray.

from_bytearray_little_endian(bytes: ByteArray) -> State

Constructs a new Bits256 element from a Little-Endian (least-significant bits first) ByteArray.

from_int(int: Int) -> State

Constructs a new Bits256 element from an integer, ensuring it’s within the valid range of the field.

Modifying

scale(self: State, e: Int) -> State

Exponentiates a Bits256 element by a non-negative integer exponent, using repeated squaring. Note that this function returns zero for negative exponents.

scale2(self: State, k: Int) -> State

A faster version of scale for the case where the exponent is a power of two. That is, the exponent e=2ke = 2^k for some non-negative integer kk.

Combining

add(left: State, right: State) -> State

Adds two Bits256 elements, ensuring the result stays within the finite field range.

add_bytes(self: State, bytes: ByteArray) -> State

Adds a ByteArray to a Bits256 element, interpreting bytes as a big-endian number.

add_int(self: State, int: Int) -> State

Adds an integer to a Bits256 element.

mul(left: State, right: State) -> State

Multiplies two Bits256 elements, with the result constrained within the finite field.

mul_bytes(self: State, bytes: ByteArray) -> State

Multiplies a Bits256 element by a ByteArray, interpreting bytes as a big-endian number.

mul_int(self: State, int: Int) -> State

Multiplies a Bits256 element by an integer.

neg(self: State) -> State

Calculates the additive inverse of a Bits256 element.

sub(left: State, right: State) -> State

Subtracts one Bits256 element from another, with the result wrapped within the finite field range.

sub_bytes(self: State, bytes: ByteArray) -> State

Subtracts a ByteArray from a Bits256 element, interpreting bytes as a big-endian number.

sub_int(self: State, int: Int) -> State

Subtracts an integer from a Bits256 element.

Transforming

to_bytearray_big_endian(self: State) -> ByteArray

Converts a Bits256 element to a Big-Endian (most-significant bits first) ByteArray.

to_bytearray_little_endian(self: State) -> ByteArray

Converts a Bits256 element to a Little-Endian (least-significant bits first) ByteArray.

to_int(self: State) -> Int

Converts a Bits256 element back to its integer representation.

Search Document