U512
Overview
License: MIT
library U512
Low-level library that implements unsigned 512-bit arithmetic.
| Statistic | Avg |
|---|---|
| add | 269 gas |
| sub | 278 gas |
| mul | 353 gas |
| mod | 682 gas |
| modinv | 6083 gas |
| modadd | 780 gas |
| redadd | 590 gas |
| modmul | 1176 gas |
| modsub | 1017 gas |
| redsub | 533 gas |
| modexp | 5981 gas |
| modexpU256 | 692 gas |
| moddiv | 7092 gas |
| and | 251 gas |
| or | 251 gas |
| xor | 251 gas |
| not | 216 gas |
| shl | 272 gas |
| shr | 272 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A 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:
| Name | Type | Description |
|---|---|---|
| u256_ | uint256 | The 256-bit unsigned integer to convert. |
Return values:
| Name | Type | Description |
|---|---|---|
| u512_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| bytes_ | bytes | The byte array to convert. |
Return values:
| Name | Type | Description |
|---|---|---|
| u512_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| u512_ | uint512 | The 512-bit unsigned integer to copy. |
Return values:
| Name | Type | Description |
|---|---|---|
| u512Copy_ | uint512 | A 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:
| Name | Type | Description |
|---|---|---|
| u512_ | uint512 | The 512-bit unsigned integer to assign. |
| to_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| u512_ | uint512 | The 512-bit unsigned integer to convert. |
Return values:
| Name | Type | Description |
|---|---|---|
| bytes_ | bytes | A 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:
| Name | Type | Description |
|---|---|---|
| u512_ | uint512 | The uint512 pointer to check. |
Return values:
| Name | Type | Description |
|---|---|---|
| isNull_ | bool | True 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The first 512-bit unsigned integer. |
| b_ | uint512 | The second 512-bit unsigned integer. |
Return values:
| Name | Type | Description |
|---|---|---|
| eq_ | bool | True 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The 512-bit unsigned integer. |
| u256_ | uint256 | The 256-bit unsigned integer. |
Return values:
| Name | Type | Description |
|---|---|---|
| eq_ | bool | True 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The first 512-bit unsigned integer. |
| b_ | uint512 | The second 512-bit unsigned integer. |
Return values:
| Name | Type | Description |
|---|---|---|
| [0] | int256 | 1 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| a_ | uint512 | The dividend. |
| m_ | uint512 | The modulus. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The dividend. |
| m_ | uint512 | The modulus. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| a_ | uint512 | The dividend. |
| m_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| a_ | uint512 | The dividend. |
| m_ | uint512 | The modulus. |
| to_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| a_ | uint512 | The 512-bit unsigned integer to invert. |
| m_ | uint512 | The modulus. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The 512-bit unsigned integer to invert. |
| m_ | uint512 | The modulus. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| a_ | uint512 | The 512-bit unsigned integer to invert. |
| m_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| a_ | uint512 | The 512-bit unsigned integer to invert. |
| m_ | uint512 | The modulus. |
| to_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| b_ | uint512 | The base. |
| e_ | uint512 | The exponent. |
| m_ | uint512 | The modulus. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| b_ | uint512 | The base. |
| e_ | uint512 | The exponent. |
| m_ | uint512 | The modulus. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| b_ | uint512 | The base. |
| e_ | uint512 | The exponent. |
| m_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| b_ | uint512 | The base. |
| e_ | uint512 | The exponent. |
| m_ | uint512 | The modulus. |
| to_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| b_ | uint512 | The base. |
| e_ | uint256 | The exponent. |
| m_ | uint512 | The modulus. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| b_ | uint512 | The base. |
| e_ | uint256 | The exponent. |
| m_ | uint512 | The modulus. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| b_ | uint512 | The base. |
| e_ | uint256 | The exponent. |
| m_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| b_ | uint512 | The base. |
| e_ | uint256 | The exponent. |
| m_ | uint512 | The modulus. |
| to_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| a_ | uint512 | The first addend. |
| b_ | uint512 | The second addend. |
| m_ | uint512 | The modulus. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The first addend. |
| b_ | uint512 | The second addend. |
| m_ | uint512 | The modulus. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| a_ | uint512 | The first addend. |
| b_ | uint512 | The second addend. |
| m_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| a_ | uint512 | The first addend. |
| b_ | uint512 | The second addend. |
| m_ | uint512 | The modulus. |
| to_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The first addend. |
| b_ | uint512 | The second addend. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The first addend. |
| b_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The first addend. |
| b_ | uint512 | The second addend. |
| to_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| a_ | uint512 | The first addend, reduced by m_. |
| b_ | uint512 | The second addend, reduced by m_. |
| m_ | uint512 | The modulus. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The first addend, reduced by m_. |
| b_ | uint512 | The second addend, reduced by m_. |
| m_ | uint512 | The modulus. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| a_ | uint512 | The first addend, reduced by m_. |
| b_ | uint512 | The second addend, reduced by m_. |
| m_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| a_ | uint512 | The first addend, reduced by m_. |
| b_ | uint512 | The second addend, reduced by m_. |
| m_ | uint512 | The modulus. |
| to_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| a_ | uint512 | The minuend. |
| b_ | uint512 | The subtrahend. |
| m_ | uint512 | The modulus. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The minuend. |
| b_ | uint512 | The subtrahend. |
| m_ | uint512 | The modulus. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| a_ | uint512 | The minuend. |
| b_ | uint512 | The subtrahend. |
| m_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| a_ | uint512 | The minuend. |
| b_ | uint512 | The subtrahend. |
| m_ | uint512 | The modulus. |
| to_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The minuend. |
| b_ | uint512 | The subtrahend. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The minuend. |
| b_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The minuend. |
| b_ | uint512 | The subtrahend. |
| to_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| a_ | uint512 | The minuend, reduced by m_. |
| b_ | uint512 | The subtrahend, reduced by m_. |
| m_ | uint512 | The modulus. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The minuend, reduced by m_. |
| b_ | uint512 | The subtrahend, reduced by m_. |
| m_ | uint512 | The modulus. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| a_ | uint512 | The minuend, reduced by m_. |
| b_ | uint512 | The subtrahend, reduced by m_. |
| m_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| a_ | uint512 | The minuend, reduced by m_. |
| b_ | uint512 | The subtrahend, reduced by m_. |
| m_ | uint512 | The modulus. |
| to_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| a_ | uint512 | The first factor. |
| b_ | uint512 | The second factor. |
| m_ | uint512 | The modulus. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The first factor. |
| b_ | uint512 | The second factor. |
| m_ | uint512 | The modulus. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| a_ | uint512 | The first factor. |
| b_ | uint512 | The second factor. |
| m_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| a_ | uint512 | The first factor. |
| b_ | uint512 | The second factor. |
| m_ | uint512 | The modulus. |
| to_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The first factor. |
| b_ | uint512 | The second factor. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The first factor. |
| b_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The first factor. |
| b_ | uint512 | The second factor. |
| to_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| a_ | uint512 | The dividend. |
| b_ | uint512 | The divisor. |
| m_ | uint512 | The modulus. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The dividend. |
| b_ | uint512 | The divisor. |
| m_ | uint512 | The modulus. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| a_ | uint512 | The dividend. |
| b_ | uint512 | The divisor. |
| m_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| call_ | call512 | A memory pointer for precompile call arguments. |
| a_ | uint512 | The dividend. |
| b_ | uint512 | The divisor. |
| m_ | uint512 | The modulus. |
| to_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The first 512-bit unsigned integer. |
| b_ | uint512 | The second 512-bit unsigned integer. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The first 512-bit unsigned integer. |
| b_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The first 512-bit unsigned integer. |
| b_ | uint512 | The second 512-bit unsigned integer. |
| to_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The first 512-bit unsigned integer. |
| b_ | uint512 | The second 512-bit unsigned integer. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The first 512-bit unsigned integer. |
| b_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The first 512-bit unsigned integer. |
| b_ | uint512 | The second 512-bit unsigned integer. |
| to_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The first 512-bit unsigned integer. |
| b_ | uint512 | The second 512-bit unsigned integer. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The first 512-bit unsigned integer. |
| b_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The first 512-bit unsigned integer. |
| b_ | uint512 | The second 512-bit unsigned integer. |
| to_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The 512-bit unsigned integer. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The 512-bit unsigned integer. |
| to_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The 512-bit unsigned integer to shift. |
| b_ | uint8 | The number of bits to shift by. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The 512-bit unsigned integer to shift. |
| b_ | uint8 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The 512-bit unsigned integer to shift. |
| b_ | uint8 | The number of bits to shift by. |
| to_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The 512-bit unsigned integer to shift. |
| b_ | uint8 | The number of bits to shift by. |
Return values:
| Name | Type | Description |
|---|---|---|
| r_ | uint512 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The 512-bit unsigned integer to shift. |
| b_ | uint8 | The 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:
| Name | Type | Description |
|---|---|---|
| a_ | uint512 | The 512-bit unsigned integer to shift. |
| b_ | uint8 | The number of bits to shift by. |
| to_ | uint512 | The target 512-bit unsigned integer to store the result. |