Skip to main content

U512

Overview​

License: MIT​

library U512

Low-level library that implements unsigned 512-bit arithmetic.

StatisticAvg
add269 gas
sub278 gas
mul353 gas
mod682 gas
modinv6083 gas
modadd780 gas
redadd590 gas
modmul1176 gas
modsub1017 gas
redsub533 gas
modexp5981 gas
modexpU256692 gas
moddiv7092 gas
and251 gas
or251 gas
xor251 gas
not216 gas
shl272 gas
shr272 gas

Imports:​

First import the library and all the necessary types.

import {U512, uint512, call512} from "U512.sol";

Usage example:​

using U512 for uint512;

uint512 a_ = U512.fromUint256(3);
uint512 b_ = U512.fromUint256(6);
uint512 m_ = U512.fromUint256(5);
uint512 r_ = a.modadd(b_, m_);
r_.eq(U512.fromUint256(4)); // true

Note that each mod call allocates extra memory for invoking the precompile. This is fine for lightweight functions. However, for heavy functions, consider allocating memory once and reusing it in subsequent calls. This approach can help reduce gas costs. Additionally, use assignment functions to avoid allocating memory for new local variables, instead assigning values to existing ones.

using U512 for uint512;

call512 call_ = U512.initCall();
uint512 a_ = U512.fromUint256(3);
uint512 b_ = U512.fromUint256(6);
uint512 m_ = U512.fromUint256(5);
uint512 r_ = a.modadd(call_, b_, m_); // 4
r_.modmulAssign(a_, m_); // 2
r_.eq(U512.fromUint256(2)); // true
r_.toBytes(); // "0x00..02"

Functions info​

initCall​

function initCall() internal pure returns (call512 call_)

Initializes a memory pointer for precompile call arguments.

Return values:

NameTypeDescription
call_call512A memory pointer for precompile operations.

fromUint256​

function fromUint256(uint256 u256_) internal pure returns (uint512 u512_)

Converts a 256-bit unsigned integer to a 512-bit unsigned integer.

Parameters:

NameTypeDescription
u256_uint256The 256-bit unsigned integer to convert.

Return values:

NameTypeDescription
u512_uint512The 512-bit representation of the input.

fromBytes​

function fromBytes(bytes memory bytes_) internal view returns (uint512 u512_)

Converts a byte array to a 512-bit unsigned integer.

The byte array must be less than 65 bytes.

Parameters:

NameTypeDescription
bytes_bytesThe byte array to convert.

Return values:

NameTypeDescription
u512_uint512The 512-bit representation of the byte array.

copy​

function copy(uint512 u512_) internal pure returns (uint512 u512Copy_)

Copies a 512-bit unsigned integer to a new memory location.

Parameters:

NameTypeDescription
u512_uint512The 512-bit unsigned integer to copy.

Return values:

NameTypeDescription
u512Copy_uint512A pointer to the new copy of the 512-bit unsigned integer.

assign​

function assign(uint512 u512_, uint512 to_) internal pure

Assigns a 512-bit unsigned integer to another.

Parameters:

NameTypeDescription
u512_uint512The 512-bit unsigned integer to assign.
to_uint512The target 512-bit unsigned integer to store the result.

toBytes​

function toBytes(uint512 u512_) internal pure returns (bytes memory bytes_)

Converts a 512-bit unsigned integer to a byte array.

Parameters:

NameTypeDescription
u512_uint512The 512-bit unsigned integer to convert.

Return values:

NameTypeDescription
bytes_bytesA byte array representation of the 512-bit unsigned integer.

isNull​

function isNull(uint512 u512_) internal pure returns (bool isNull_)

Checks if a uint512 pointer is null.

Parameters:

NameTypeDescription
u512_uint512The uint512 pointer to check.

Return values:

NameTypeDescription
isNull_boolTrue if the pointer is null, false otherwise.

eq​

function eq(uint512 a_, uint512 b_) internal pure returns (bool eq_)

Compares two 512-bit unsigned integers for equality.

Parameters:

NameTypeDescription
a_uint512The first 512-bit unsigned integer.
b_uint512The second 512-bit unsigned integer.

Return values:

NameTypeDescription
eq_boolTrue if the integers are equal, false otherwise.

eqU256​

function eqU256(uint512 a_, uint256 u256_) internal pure returns (bool eq_)

Compares a 512-bit unsigned integer with a 256-bit unsigned integer for equality.

Parameters:

NameTypeDescription
a_uint512The 512-bit unsigned integer.
u256_uint256The 256-bit unsigned integer.

Return values:

NameTypeDescription
eq_boolTrue if the integers are equal, false otherwise.

cmp​

function cmp(uint512 a_, uint512 b_) internal pure returns (int256)

Compares two 512-bit unsigned integers.

Parameters:

NameTypeDescription
a_uint512The first 512-bit unsigned integer.
b_uint512The second 512-bit unsigned integer.

Return values:

NameTypeDescription
[0]int2561 if a_ > b_, -1 if a_ < b_, and 0 if they are equal.

mod​

function mod(
call512 call_,
uint512 a_,
uint512 m_
) internal view returns (uint512 r_)

Performs modular arithmetic on 512-bit integers.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
a_uint512The dividend.
m_uint512The modulus.

Return values:

NameTypeDescription
r_uint512The result of the modular operation (a_ % m_).

mod​

function mod(uint512 a_, uint512 m_) internal view returns (uint512 r_)

Performs modular arithmetic on 512-bit integers.

Allocates memory for call every time it's called.

Parameters:

NameTypeDescription
a_uint512The dividend.
m_uint512The modulus.

Return values:

NameTypeDescription
r_uint512The result of the modular operation (a_ % m_).

modAssign​

function modAssign(call512 call_, uint512 a_, uint512 m_) internal view

Performs modular assignment on a 512-bit unsigned integer.

Updates the value of a_ to (a_ % m_).

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
a_uint512The dividend.
m_uint512The modulus.

modAssignTo​

function modAssignTo(
call512 call_,
uint512 a_,
uint512 m_,
uint512 to_
) internal view

Performs modular assignment and stores the result in a separate 512-bit unsigned integer.

Assigns the result (a_ % m_) to to_.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
a_uint512The dividend.
m_uint512The modulus.
to_uint512The target 512-bit unsigned integer to store the result.

modinv​

function modinv(
call512 call_,
uint512 a_,
uint512 m_
) internal view returns (uint512 r_)

Computes the modular inverse of a 512-bit unsigned integer.

IMPORTANT: The modulus m_ must be a prime number

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
a_uint512The 512-bit unsigned integer to invert.
m_uint512The modulus.

Return values:

NameTypeDescription
r_uint512The modular inverse result a_^(-1) % m_.

modinv​

function modinv(uint512 a_, uint512 m_) internal view returns (uint512 r_)

Computes the modular inverse of a 512-bit unsigned integer.

IMPORTANT: The modulus m_ must be a prime number

Allocates memory for call every time it's called.

Parameters:

NameTypeDescription
a_uint512The 512-bit unsigned integer to invert.
m_uint512The modulus.

Return values:

NameTypeDescription
r_uint512The modular inverse result a_^(-1) % m_.

modinvAssign​

function modinvAssign(call512 call_, uint512 a_, uint512 m_) internal view

Performs the modular inverse assignment on a 512-bit unsigned integer.

IMPORTANT: The modulus m_ must be a prime number

Updates the value of a_ to a_^(-1) % m_.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
a_uint512The 512-bit unsigned integer to invert.
m_uint512The modulus.

modinvAssignTo​

function modinvAssignTo(
call512 call_,
uint512 a_,
uint512 m_,
uint512 to_
) internal view

Computes the modular inverse and stores it in a separate 512-bit unsigned integer.

IMPORTANT: The modulus m_ must be a prime number

Assigns the result of a_^(-1) % m_ to to_.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
a_uint512The 512-bit unsigned integer to invert.
m_uint512The modulus.
to_uint512The target 512-bit unsigned integer to store the result.

modexp​

function modexp(
call512 call_,
uint512 b_,
uint512 e_,
uint512 m_
) internal view returns (uint512 r_)

Performs modular exponentiation on 512-bit unsigned integers.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
b_uint512The base.
e_uint512The exponent.
m_uint512The modulus.

Return values:

NameTypeDescription
r_uint512The result of modular exponentiation (b_^e_) % m_.

modexp​

function modexp(
uint512 b_,
uint512 e_,
uint512 m_
) internal view returns (uint512 r_)

Performs modular exponentiation on 512-bit unsigned integers.

Allocates memory for call every time it's called.

Parameters:

NameTypeDescription
b_uint512The base.
e_uint512The exponent.
m_uint512The modulus.

Return values:

NameTypeDescription
r_uint512The result of modular exponentiation (b_^e_) % m_.

modexpAssign​

function modexpAssign(
call512 call_,
uint512 b_,
uint512 e_,
uint512 m_
) internal view

Performs modular exponentiation assignment on the base.

Updates the value of b_ to (b_^e_) % m_.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
b_uint512The base.
e_uint512The exponent.
m_uint512The modulus.

modexpAssignTo​

function modexpAssignTo(
call512 call_,
uint512 b_,
uint512 e_,
uint512 m_,
uint512 to_
) internal view

Performs modular exponentiation and stores the result in a separate 512-bit unsigned integer.

Assigns the result of (b_^e_) % m_ to to_.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
b_uint512The base.
e_uint512The exponent.
m_uint512The modulus.
to_uint512The target 512-bit unsigned integer to store the result.

modexpU256​

function modexpU256(
call512 call_,
uint512 b_,
uint256 e_,
uint512 m_
) internal view returns (uint512 r_)

Performs modular exponentiation on 512-bit unsigned integers.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
b_uint512The base.
e_uint256The exponent.
m_uint512The modulus.

Return values:

NameTypeDescription
r_uint512The result of modular exponentiation (b_^e_) % m_.

modexpU256​

function modexpU256(
uint512 b_,
uint256 e_,
uint512 m_
) internal view returns (uint512 r_)

Performs modular exponentiation of a 512-bit unsigned integer base and a 256-bit unsigned integer exponent.

Allocates memory for call every time it's called.

Parameters:

NameTypeDescription
b_uint512The base.
e_uint256The exponent.
m_uint512The modulus.

Return values:

NameTypeDescription
r_uint512The result of modular exponentiation (b_^e_) % m_.

modexpU256Assign​

function modexpU256Assign(
call512 call_,
uint512 b_,
uint256 e_,
uint512 m_
) internal view

Performs modular exponentiation of a 512-bit unsigned integer base and a 256-bit unsigned integer exponent.

Updates the value of b_ to (b_^e_) % m_.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
b_uint512The base.
e_uint256The exponent.
m_uint512The modulus.

modexpU256AssignTo​

function modexpU256AssignTo(
call512 call_,
uint512 b_,
uint256 e_,
uint512 m_,
uint512 to_
) internal view

Performs modular exponentiation of a 512-bit unsigned integer base and a 256-bit unsigned integer exponent.

Assigns the result of (b_^e_) % m_ to to_.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
b_uint512The base.
e_uint256The exponent.
m_uint512The modulus.
to_uint512The target 512-bit unsigned integer to store the result.

modadd​

function modadd(
call512 call_,
uint512 a_,
uint512 b_,
uint512 m_
) internal view returns (uint512 r_)

Adds two 512-bit unsigned integers under a modulus.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
a_uint512The first addend.
b_uint512The second addend.
m_uint512The modulus.

Return values:

NameTypeDescription
r_uint512The result of the modular addition (a_ + b_) % m_.

modadd​

function modadd(
uint512 a_,
uint512 b_,
uint512 m_
) internal view returns (uint512 r_)

Adds two 512-bit unsigned integers under a modulus.

Allocates memory for call every time it's called.

Parameters:

NameTypeDescription
a_uint512The first addend.
b_uint512The second addend.
m_uint512The modulus.

Return values:

NameTypeDescription
r_uint512The result of the modular addition (a_ + b_) % m_.

modaddAssign​

function modaddAssign(
call512 call_,
uint512 a_,
uint512 b_,
uint512 m_
) internal view

Performs modular addition assignment on the first 512-bit unsigned integer addend.

Updates the value of a_ to (a_ + b_) % m_.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
a_uint512The first addend.
b_uint512The second addend.
m_uint512The modulus.

modaddAssignTo​

function modaddAssignTo(
call512 call_,
uint512 a_,
uint512 b_,
uint512 m_,
uint512 to_
) internal view

Performs modular addition and stores the result in a separate 512-bit unsigned integer.

Assigns the result of (a_ + b_) % m_ to to_.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
a_uint512The first addend.
b_uint512The second addend.
m_uint512The modulus.
to_uint512The target 512-bit unsigned integer to store the result.

add​

function add(uint512 a_, uint512 b_) internal pure returns (uint512 r_)

Adds two 512-bit unsigned integers.

Parameters:

NameTypeDescription
a_uint512The first addend.
b_uint512The second addend.

Return values:

NameTypeDescription
r_uint512The result of the addition.

addAssign​

function addAssign(uint512 a_, uint512 b_) internal pure

Performs addition assignment on the first 512-bit unsigned addend.

Updates the value of a_ to a_ + b_.

Parameters:

NameTypeDescription
a_uint512The first addend.
b_uint512The second addend.

addAssignTo​

function addAssignTo(uint512 a_, uint512 b_, uint512 to_) internal pure

Performs addition and stores the result in a separate 512-bit unsigned integer.

Assigns the result of a_ + b_ to to_.

Parameters:

NameTypeDescription
a_uint512The first addend.
b_uint512The second addend.
to_uint512The target 512-bit unsigned integer to store the result.

redadd​

function redadd(
call512 call_,
uint512 a_,
uint512 b_,
uint512 m_
) internal pure returns (uint512 r_)

Adds two 512-bit unsigned integers under a modulus.

This is an optimized version of modadd where the inputs must be pre-reduced by m_.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
a_uint512The first addend, reduced by m_.
b_uint512The second addend, reduced by m_.
m_uint512The modulus.

Return values:

NameTypeDescription
r_uint512The result of the modular addition (a_ + b_) % m_.

redadd​

function redadd(
uint512 a_,
uint512 b_,
uint512 m_
) internal pure returns (uint512 r_)

Adds two 512-bit unsigned integers under a modulus.

This is an optimized version of modadd where the inputs must be pre-reduced by m_.

Allocates memory for call every time it's called.

Parameters:

NameTypeDescription
a_uint512The first addend, reduced by m_.
b_uint512The second addend, reduced by m_.
m_uint512The modulus.

Return values:

NameTypeDescription
r_uint512The result of the modular addition (a_ + b_) % m_.

redaddAssign​

function redaddAssign(
call512 call_,
uint512 a_,
uint512 b_,
uint512 m_
) internal pure

Performs modular addition assignment on the first 512-bit unsigned integer addend.

This is an optimized version of modaddAssign where the inputs must be pre-reduced by m_.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
a_uint512The first addend, reduced by m_.
b_uint512The second addend, reduced by m_.
m_uint512The modulus.

redaddAssignTo​

function redaddAssignTo(
call512 call_,
uint512 a_,
uint512 b_,
uint512 m_,
uint512 to_
) internal pure

Performs modular addition and stores the result in a separate 512-bit unsigned integer.

This is an optimized version of modaddAssignTo where the inputs must be pre-reduced by m_.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
a_uint512The first addend, reduced by m_.
b_uint512The second addend, reduced by m_.
m_uint512The modulus.
to_uint512The target 512-bit unsigned integer to store the result.

modsub​

function modsub(
call512 call_,
uint512 a_,
uint512 b_,
uint512 m_
) internal view returns (uint512 r_)

Subtracts one 512-bit unsigned integer from another under a modulus.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
a_uint512The minuend.
b_uint512The subtrahend.
m_uint512The modulus.

Return values:

NameTypeDescription
r_uint512The result of the modular subtraction (a_ - b_) % m_.

modsub​

function modsub(
uint512 a_,
uint512 b_,
uint512 m_
) internal view returns (uint512 r_)

Subtracts one 512-bit unsigned integer from another under a modulus.

Allocates memory for call every time it's called.

Parameters:

NameTypeDescription
a_uint512The minuend.
b_uint512The subtrahend.
m_uint512The modulus.

Return values:

NameTypeDescription
r_uint512The result of the modular subtraction (a_ - b_) % m_.

modsubAssign​

function modsubAssign(
call512 call_,
uint512 a_,
uint512 b_,
uint512 m_
) internal view

Performs modular subtraction assignment on the 512-bit unsigned integer minuend.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
a_uint512The minuend.
b_uint512The subtrahend.
m_uint512The modulus.

modsubAssignTo​

function modsubAssignTo(
call512 call_,
uint512 a_,
uint512 b_,
uint512 m_,
uint512 to_
) internal view

Performs modular subtraction and stores the result in a separate 512-bit unsigned integer.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
a_uint512The minuend.
b_uint512The subtrahend.
m_uint512The modulus.
to_uint512The target 512-bit unsigned integer to store the result.

sub​

function sub(uint512 a_, uint512 b_) internal pure returns (uint512 r_)

Subtracts one 512-bit unsigned integer from another.

Parameters:

NameTypeDescription
a_uint512The minuend.
b_uint512The subtrahend.

Return values:

NameTypeDescription
r_uint512The result of the subtraction.

subAssign​

function subAssign(uint512 a_, uint512 b_) internal pure

Performs subtraction assignment on the 512-bit unsigned minuend.

Updates the value of a_ to a_ - b_.

Parameters:

NameTypeDescription
a_uint512The minuend.
b_uint512The subtrahend.

subAssignTo​

function subAssignTo(uint512 a_, uint512 b_, uint512 to_) internal pure

Performs subtraction and stores the result in a separate 512-bit unsigned integer.

Assigns the result of a_ - b_ to to_.

Parameters:

NameTypeDescription
a_uint512The minuend.
b_uint512The subtrahend.
to_uint512The target 512-bit unsigned integer to store the result.

redsub​

function redsub(
call512 call_,
uint512 a_,
uint512 b_,
uint512 m_
) internal pure returns (uint512 r_)

Subtracts one 512-bit unsigned integer from another under a modulus.

This is an optimized version of modsub where the inputs must be pre-reduced by m_.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
a_uint512The minuend, reduced by m_.
b_uint512The subtrahend, reduced by m_.
m_uint512The modulus.

Return values:

NameTypeDescription
r_uint512The result of the modular subtraction (a_ - b_) % m_.

redsub​

function redsub(
uint512 a_,
uint512 b_,
uint512 m_
) internal pure returns (uint512 r_)

Subtracts one 512-bit unsigned integer from another under a modulus.

This is an optimized version of modsub where the inputs must be pre-reduced by m_.

Allocates memory for call every time it's called.

Parameters:

NameTypeDescription
a_uint512The minuend, reduced by m_.
b_uint512The subtrahend, reduced by m_.
m_uint512The modulus.

Return values:

NameTypeDescription
r_uint512The result of the modular subtraction (a_ - b_) % m_.

redsubAssign​

function redsubAssign(
call512 call_,
uint512 a_,
uint512 b_,
uint512 m_
) internal pure

Performs modular subtraction assignment on the 512-bit unsigned integer minuend.

This is an optimized version of modsubAssign where the inputs must be pre-reduced by m_.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
a_uint512The minuend, reduced by m_.
b_uint512The subtrahend, reduced by m_.
m_uint512The modulus.

redsubAssignTo​

function redsubAssignTo(
call512 call_,
uint512 a_,
uint512 b_,
uint512 m_,
uint512 to_
) internal pure

Performs modular subtraction and stores the result in a separate 512-bit unsigned integer.

This is an optimized version of modsubAssignTo where the inputs must be pre-reduced by m_.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
a_uint512The minuend, reduced by m_.
b_uint512The subtrahend, reduced by m_.
m_uint512The modulus.
to_uint512The target 512-bit unsigned integer to store the result.

modmul​

function modmul(
call512 call_,
uint512 a_,
uint512 b_,
uint512 m_
) internal view returns (uint512 r_)

Multiplies two 512-bit unsigned integers under a modulus.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
a_uint512The first factor.
b_uint512The second factor.
m_uint512The modulus.

Return values:

NameTypeDescription
r_uint512The result of the modular multiplication (a_ * b_) % m_.

modmul​

function modmul(
uint512 a_,
uint512 b_,
uint512 m_
) internal view returns (uint512 r_)

Multiplies two 512-bit unsigned integers under a modulus.

Allocates memory for call every time it's called.

Parameters:

NameTypeDescription
a_uint512The first factor.
b_uint512The second factor.
m_uint512The modulus.

Return values:

NameTypeDescription
r_uint512The result of the modular multiplication (a_ * b_) % m_.

modmulAssign​

function modmulAssign(
call512 call_,
uint512 a_,
uint512 b_,
uint512 m_
) internal view

Performs modular multiplication assignment on the first 512-bit unsigned integer factor.

Updates the value of a_ to (a_ * b_) % m_.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
a_uint512The first factor.
b_uint512The second factor.
m_uint512The modulus.

modmulAssignTo​

function modmulAssignTo(
call512 call_,
uint512 a_,
uint512 b_,
uint512 m_,
uint512 to_
) internal view

Performs modular multiplication and stores the result in a separate 512-bit unsigned integer.

Assigns the result of (a_ * b_) % m_ to to_.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
a_uint512The first factor.
b_uint512The second factor.
m_uint512The modulus.
to_uint512The target 512-bit unsigned integer to store the result.

mul​

function mul(uint512 a_, uint512 b_) internal pure returns (uint512 r_)

Multiplies two 512-bit unsigned integers.

Parameters:

NameTypeDescription
a_uint512The first factor.
b_uint512The second factor.

Return values:

NameTypeDescription
r_uint512The result of the multiplication.

mulAssign​

function mulAssign(uint512 a_, uint512 b_) internal pure

Performs multiplication assignment on the first 512-bit unsigned factor.

Updates the value of a_ to a_ * b_.

Parameters:

NameTypeDescription
a_uint512The first factor.
b_uint512The second factor.

mulAssignTo​

function mulAssignTo(uint512 a_, uint512 b_, uint512 to_) internal pure

Performs multiplication and stores the result in a separate 512-bit unsigned integer.

Assigns the result of a_ * b_ to to_.

Parameters:

NameTypeDescription
a_uint512The first factor.
b_uint512The second factor.
to_uint512The target 512-bit unsigned integer to store the result.

moddiv​

function moddiv(
call512 call_,
uint512 a_,
uint512 b_,
uint512 m_
) internal view returns (uint512 r_)

Divides two 512-bit unsigned integers under a modulus.

IMPORTANT: The modulus m_ must be a prime number.

Returns the result of (a_ * b_^(-1)) % m_.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
a_uint512The dividend.
b_uint512The divisor.
m_uint512The modulus.

Return values:

NameTypeDescription
r_uint512The result of the modular division.

moddiv​

function moddiv(
uint512 a_,
uint512 b_,
uint512 m_
) internal view returns (uint512 r_)

Divides two 512-bit unsigned integers under a modulus.

IMPORTANT: The modulus m_ must be a prime number.

Returns the result of (a_ * b_^(-1)) % m_.

Allocates memory for call every time it's called.

Parameters:

NameTypeDescription
a_uint512The dividend.
b_uint512The divisor.
m_uint512The modulus.

Return values:

NameTypeDescription
r_uint512The result of the modular division.

moddivAssign​

function moddivAssign(
call512 call_,
uint512 a_,
uint512 b_,
uint512 m_
) internal view

Performs the modular division assignment on a 512-bit unsigned dividend.

IMPORTANT: The modulus m_ must be a prime number.

Updates the value of a_ to (a_ * b_^(-1)) % m_.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
a_uint512The dividend.
b_uint512The divisor.
m_uint512The modulus.

moddivAssignTo​

function moddivAssignTo(
call512 call_,
uint512 a_,
uint512 b_,
uint512 m_,
uint512 to_
) internal view

Performs the modular division and stores the result in a separate 512-bit unsigned integer.

IMPORTANT: The modulus m_ must be a prime number.

Assigns the result of (a_ * b_^(-1)) % m_ to to_.

Parameters:

NameTypeDescription
call_call512A memory pointer for precompile call arguments.
a_uint512The dividend.
b_uint512The divisor.
m_uint512The modulus.
to_uint512The target 512-bit unsigned integer to store the result.

and​

function and(uint512 a_, uint512 b_) internal pure returns (uint512 r_)

Performs bitwise AND of two 512-bit unsigned integers.

Parameters:

NameTypeDescription
a_uint512The first 512-bit unsigned integer.
b_uint512The second 512-bit unsigned integer.

Return values:

NameTypeDescription
r_uint512The result of the bitwise AND operation.

andAssign​

function andAssign(uint512 a_, uint512 b_) internal pure

Performs bitwise AND assignment on the first 512-bit unsigned integer.

Updates the value of a_ to a_ & b_.

Parameters:

NameTypeDescription
a_uint512The first 512-bit unsigned integer.
b_uint512The second 512-bit unsigned integer.

andAssignTo​

function andAssignTo(uint512 a_, uint512 b_, uint512 to_) internal pure

Performs bitwise AND and stores the result in a separate 512-bit unsigned integer.

Assigns the result of a_ & b_ to to_.

Parameters:

NameTypeDescription
a_uint512The first 512-bit unsigned integer.
b_uint512The second 512-bit unsigned integer.
to_uint512The target 512-bit unsigned integer to store the result.

or​

function or(uint512 a_, uint512 b_) internal pure returns (uint512 r_)

Performs bitwise OR of two 512-bit unsigned integers.

Parameters:

NameTypeDescription
a_uint512The first 512-bit unsigned integer.
b_uint512The second 512-bit unsigned integer.

Return values:

NameTypeDescription
r_uint512The result of the bitwise OR operation.

orAssign​

function orAssign(uint512 a_, uint512 b_) internal pure

Performs bitwise OR assignment on the first 512-bit unsigned integer.

Updates the value of a_ to a_ | b_.

Parameters:

NameTypeDescription
a_uint512The first 512-bit unsigned integer.
b_uint512The second 512-bit unsigned integer.

orAssignTo​

function orAssignTo(uint512 a_, uint512 b_, uint512 to_) internal pure

Performs bitwise OR and stores the result in a separate 512-bit unsigned integer.

Assigns the result of a_ | b_ to to_.

Parameters:

NameTypeDescription
a_uint512The first 512-bit unsigned integer.
b_uint512The second 512-bit unsigned integer.
to_uint512The target 512-bit unsigned integer to store the result.

xor​

function xor(uint512 a_, uint512 b_) internal pure returns (uint512 r_)

Performs bitwise XOR of two 512-bit unsigned integers.

Parameters:

NameTypeDescription
a_uint512The first 512-bit unsigned integer.
b_uint512The second 512-bit unsigned integer.

Return values:

NameTypeDescription
r_uint512The result of the bitwise XOR operation.

xorAssign​

function xorAssign(uint512 a_, uint512 b_) internal pure

Performs bitwise XOR assignment on the first 512-bit unsigned integer.

Updates the value of a_ to a_ ^ b_.

Parameters:

NameTypeDescription
a_uint512The first 512-bit unsigned integer.
b_uint512The second 512-bit unsigned integer.

xorAssignTo​

function xorAssignTo(uint512 a_, uint512 b_, uint512 to_) internal pure

Performs bitwise XOR and stores the result in a separate 512-bit unsigned integer.

Assigns the result of a_ ^ b_ to to_.

Parameters:

NameTypeDescription
a_uint512The first 512-bit unsigned integer.
b_uint512The second 512-bit unsigned integer.
to_uint512The target 512-bit unsigned integer to store the result.

not​

function not(uint512 a_) internal pure returns (uint512 r_)

Performs bitwise NOT of a 512-bit unsigned integer.

Parameters:

NameTypeDescription
a_uint512The 512-bit unsigned integer.

Return values:

NameTypeDescription
r_uint512The result of the bitwise NOT operation.

notAssign​

function notAssign(uint512 a_) internal pure

Performs bitwise NOT assignment on a 512-bit unsigned integer.

Updates the value of a_ to ~a_.

Parameters:

NameTypeDescription
a_uint512The 512-bit unsigned integer.

notAssignTo​

function notAssignTo(uint512 a_, uint512 to_) internal pure

Performs bitwise NOT and stores the result in a separate 512-bit unsigned integer.

Assigns the result of ~a_ to to_.

Parameters:

NameTypeDescription
a_uint512The 512-bit unsigned integer.
to_uint512The target 512-bit unsigned integer to store the result.

shl​

function shl(uint512 a_, uint8 b_) internal pure returns (uint512 r_)

Shifts a 512-bit unsigned integer to the left by a specified number of bits.

Parameters:

NameTypeDescription
a_uint512The 512-bit unsigned integer to shift.
b_uint8The number of bits to shift by.

Return values:

NameTypeDescription
r_uint512The result of the left shift operation.

shlAssign​

function shlAssign(uint512 a_, uint8 b_) internal pure

Shifts a 512-bit unsigned integer to the left by a specified number of bits.

Updates the value of a_ to a_ << b_.

Parameters:

NameTypeDescription
a_uint512The 512-bit unsigned integer to shift.
b_uint8The number of bits to shift by.

shlAssignTo​

function shlAssignTo(uint512 a_, uint8 b_, uint512 to_) internal pure

Shifts a 512-bit unsigned integer to the left by a specified number of bits.

Assigns the result of a_ << b_ to to_.

Parameters:

NameTypeDescription
a_uint512The 512-bit unsigned integer to shift.
b_uint8The number of bits to shift by.
to_uint512The target 512-bit unsigned integer to store the result.

shr​

function shr(uint512 a_, uint8 b_) internal pure returns (uint512 r_)

Shifts a 512-bit unsigned integer to the right by a specified number of bits.

Parameters:

NameTypeDescription
a_uint512The 512-bit unsigned integer to shift.
b_uint8The number of bits to shift by.

Return values:

NameTypeDescription
r_uint512The result of the right shift operation.

shrAssign​

function shrAssign(uint512 a_, uint8 b_) internal pure

Shifts a 512-bit unsigned integer to the right by a specified number of bits.

Updates the value of a_ to a_ >> b_.

Parameters:

NameTypeDescription
a_uint512The 512-bit unsigned integer to shift.
b_uint8The number of bits to shift by.

shrAssignTo​

function shrAssignTo(uint512 a_, uint8 b_, uint512 to_) internal pure

Shifts a 512-bit unsigned integer to the right by a specified number of bits.

Assigns the result of a_ >> b_ to to_.

Parameters:

NameTypeDescription
a_uint512The 512-bit unsigned integer to shift.
b_uint8The number of bits to shift by.
to_uint512The target 512-bit unsigned integer to store the result.