Keyboard shortcuts

Press or to navigate between chapters

Press ? to show this help

Press Esc to hide this help

alexandria_math

Fully qualified path: alexandria_math

Modules

aliquot_sum
armstrong_number
bitmap
collatz_sequence
const_pow
decimal
ed25519
extended_euclidean_algorithm
fast_power
fast_root
fibonacci
gcd_of_n_numbers
i257
is_power_of_two
is_prime
karatsuba
keccak256
lcm_of_n_numbers
mod_arithmetics
opt_math
perfect_number
pow_macro
ripemd160
sha256
sha512
trigonometry
u512_arithmetics
wad_ray_mathProvides functions to perform calculations with Wad and Ray units @dev Provides mul and div function for wads (decimal numbers with 18 digits of precision) and…
zellers_congruenceZeller’s congruence is an algorithm devised by Christian Zeller in the 19th century to calculate the day of the week for any Julian or Gregorian calendar date….

Free functions

powRaise a number to a power. O(log n) time complexity….
count_digits_of_baseFunction to count the number of digits in a number….

Traits

BitShift
BitRotateRotate the bits of an unsigned integer of type T
WrappingMath

Impls


Re-exports:

BoundedA trait defining minimum and maximum bounds for numeric types. This trait only supports types that can have constant values.
OverflowingMulPerforms multiplication with a flag for overflow….
WideMulA trait for types that can be multiplied together to produce a wider type. This trait enables multiplication operations where the result type has double…
WrappingAddPerforms addition that wraps around on overflow….
WrappingMulPerforms multiplication that wraps around on overflow….
WrappingSubPerforms subtraction that wraps around on overflow….

Modules

Modules

aliquot_sum
armstrong_number
bitmap
collatz_sequence
const_pow
decimal
ed25519
extended_euclidean_algorithm
fast_power
fast_root
fibonacci
gcd_of_n_numbers
i257
is_power_of_two
is_prime
karatsuba
keccak256
lcm_of_n_numbers
mod_arithmetics
opt_math
perfect_number
pow_macro
ripemd160
sha256
sha512
trigonometry
u512_arithmetics
wad_ray_mathProvides functions to perform calculations with Wad and Ray units @dev Provides mul and div function for wads (decimal numbers with 18 digits of precision) and…
zellers_congruenceZeller’s congruence is an algorithm devised by Christian Zeller in the 19th century to calculate the day of the week for any Julian or Gregorian calendar date….

aliquot_sum

Aliquot Sum

Fully qualified path: alexandria_math::aliquot_sum

Free functions

aliquot_sumCalculates the aliquot sum of a given number….

Free functions

Free functions

aliquot_sumCalculates the aliquot sum of a given number….

aliquot_sum

Calculates the aliquot sum of a given number.

Arguments

  • number - The number to calculate the aliquot sum for.

Returns

  • felt252 - The aliquot sum of the input number.

Fully qualified path: alexandria_math::aliquot_sum::aliquot_sum

pub fn aliquot_sum(number: u128) -> u128

armstrong_number

Armstrong Number Algorithm.

Fully qualified path: alexandria_math::armstrong_number

Free functions

is_armstrong_numberArmstrong Number Algorithm….

Free functions

Free functions

is_armstrong_numberArmstrong Number Algorithm….

is_armstrong_number

Armstrong Number Algorithm.

Arguments

  • num - The number to be evaluated.

Returns

  • bool - A boolean value indicating is Armstrong Number.

Fully qualified path: alexandria_math::armstrong_number::is_armstrong_number

pub fn is_armstrong_number(mut num: u128) -> bool

bitmap

Fully qualified path: alexandria_math::bitmap

Traits

Traits

Traits

BitmapTrait

Fully qualified path: alexandria_math::bitmap::BitmapTrait

pub trait BitmapTrait<
    T,
    +Add<T>,
    +Sub<T>,
    +Mul<T>,
    +Div<T>,
    +DivAssign<T, T>,
    +Rem<T>,
    +BitAnd<T>,
    +BitOr<T>,
    +BitNot<T>,
    +PartialEq<T>,
    +PartialOrd<T>,
    +Into<u8, T>,
    +Into<T, u256>,
    +TryInto<u256, T>,
    +Drop<T>,
    +Copy<T>,
>

Trait functions

get_bit_at

The bit value at the provided index of a number.

Arguments

  • x - The value for which to extract the bit value.
  • i - The index.

Returns

  • The value at index.

Fully qualified path: alexandria_math::bitmap::BitmapTrait::get_bit_at

fn get_bit_at(x: T, i: u8) -> bool

set_bit_at

Set the bit to value at the provided index of a number.

Arguments

  • x - The value for which to extract the bit value.
  • i - The index.
  • value - The value to set the bit to.

Returns

  • The value with the bit set to value.

Fully qualified path: alexandria_math::bitmap::BitmapTrait::set_bit_at

fn set_bit_at(x: T, i: u8, value: bool) -> T

most_significant_bit

The index of the most significant bit of the number, where the least significant bit is at index 0 and the most significant bit is at index 255

Arguments

  • x - The value for which to compute the most significant bit, must be greater than 0.

Returns

  • The index of the most significant bit

Fully qualified path: alexandria_math::bitmap::BitmapTrait::most_significant_bit

fn most_significant_bit(x: T) -> Option<u8>

least_significant_bit

The index of the least significant bit of the number, where the least significant bit is at index 0 and the most significant bit is at index 255

Arguments

  • x - The value for which to compute the least significant bit, must be greater than 0.

Returns

  • The index of the least significant bit

Fully qualified path: alexandria_math::bitmap::BitmapTrait::least_significant_bit

fn least_significant_bit(x: T) -> Option<u8>

nearest_left_significant_bit

The index of the nearest left significant bit to the index of a number.

Arguments

  • x - The value for which to compute the most significant bit.
  • i - The index for which to start the search.

Returns

  • The index of the nearest left significant bit, None is returned if no significant bit is found.

Fully qualified path: alexandria_math::bitmap::BitmapTrait::nearest_left_significant_bit

fn nearest_left_significant_bit(x: T, i: u8) -> Option<u8>

nearest_right_significant_bit

The index of the nearest right significant bit to the index of a number.

Arguments

  • x - The value for which to compute the most significant bit.
  • i - The index for which to start the search.

Returns

  • The index of the nearest right significant bit, None is returned if no significant bit is found.

Fully qualified path: alexandria_math::bitmap::BitmapTrait::nearest_right_significant_bit

fn nearest_right_significant_bit(x: T, i: u8) -> Option<u8>

nearest_significant_bit

The index of the nearest significant bit to the index of a number, where the least significant bit is at index 0 and the most significant bit is at index 255

Arguments

  • x - The value for which to compute the most significant bit, must be greater than 0.
  • i - The index for which to start the search.
  • priority - if priority is set to true then right is prioritized over left, left over right otherwise.

Returns

  • The index of the nearest significant bit, None is returned if no significant bit is found.

Fully qualified path: alexandria_math::bitmap::BitmapTrait::nearest_significant_bit

fn nearest_significant_bit(x: T, i: u8, priority: bool) -> Option<u8>

collatz_sequence

Collatz Sequence

Fully qualified path: alexandria_math::collatz_sequence

Free functions

sequenceGenerates the Collatz sequence for a given number….

Free functions

Free functions

sequenceGenerates the Collatz sequence for a given number….

sequence

Generates the Collatz sequence for a given number.

Arguments

  • number - The number to generate the Collatz sequence for.

Returns

  • Array - The Collatz sequence as an array of felt252 numbers.

Fully qualified path: alexandria_math::collatz_sequence::sequence

pub fn sequence(mut number: u128) -> Array<u128>

const_pow

Fully qualified path: alexandria_math::const_pow

Free functions

pow2_u256Calculate 2 raised to the power of the given exponent using a pre-computed lookup table…
pow2Calculate 2 raised to the power of the given exponent using a pre-computed lookup table…
pow2_felt252Calculate 2 raised to the power of the given exponent using a pre-computed lookup table…
pow10Calculate 10 raised to the power of the given exponent using a pre-computed lookup table…
pow10_u256Calculate 10 raised to the power of the given exponent using a pre-computed lookup table…

Free functions

Free functions

pow2_u256Calculate 2 raised to the power of the given exponent using a pre-computed lookup table…
pow2Calculate 2 raised to the power of the given exponent using a pre-computed lookup table…
pow2_felt252Calculate 2 raised to the power of the given exponent using a pre-computed lookup table…
pow10Calculate 10 raised to the power of the given exponent using a pre-computed lookup table…
pow10_u256Calculate 10 raised to the power of the given exponent using a pre-computed lookup table…

pow2_u256

Calculate 2 raised to the power of the given exponent using a pre-computed lookup table

Arguments

  • exponent - The exponent to raise 2 to

Returns

  • u256 - The result of 2^exponent

Panics

  • If exponent is greater than 255 (out of the supported range)

Fully qualified path: alexandria_math::const_pow::pow2_u256

pub fn pow2_u256(exponent: u32) -> u256

pow2

Calculate 2 raised to the power of the given exponent using a pre-computed lookup table

Arguments

  • exponent - The exponent to raise 2 to

Returns

  • u128 - The result of 2^exponent

Panics

  • If exponent is greater than 127 (out of the supported range)

Fully qualified path: alexandria_math::const_pow::pow2

pub fn pow2(exponent: u32) -> u128

pow2_felt252

Calculate 2 raised to the power of the given exponent using a pre-computed lookup table

Arguments

  • exponent - The exponent to raise 2 to

Returns

  • felt252 - The result of 2^exponent

Panics

  • If exponent is greater than 251 (out of the supported range)

Fully qualified path: alexandria_math::const_pow::pow2_felt252

pub fn pow2_felt252(exponent: u32) -> felt252

pow10

Calculate 10 raised to the power of the given exponent using a pre-computed lookup table

Arguments

  • exponent - The exponent to raise 10 to

Returns

  • u128 - The result of 10^exponent

Panics

  • If exponent is greater than 38 (out of the supported range)

Fully qualified path: alexandria_math::const_pow::pow10

pub fn pow10(exponent: u32) -> u128

pow10_u256

Calculate 10 raised to the power of the given exponent using a pre-computed lookup table

Arguments

  • exponent - The exponent to raise 10 to

Returns

  • u128 - The result of 10^exponent

Panics

  • If exponent is greater than 77 (out of the supported range)

Fully qualified path: alexandria_math::const_pow::pow10_u256

pub fn pow10_u256(exponent: u32) -> u256

decimal

Fully qualified path: alexandria_math::decimal

Constants

Structs

DecimalFixed-point decimal number using separate 64-bit fields int_part: integer portion (0 to 2^64-1) frac_part: fractional portion in raw DECIMAL_SCALE units (0 to 10^18-1)…

Traits

Impls

Constants

Constants

DECIMAL_SCALE

Fully qualified path: alexandria_math::decimal::DECIMAL_SCALE

pub const DECIMAL_SCALE: u128 = 1000000000000000000;

Structs

Structs

DecimalFixed-point decimal number using separate 64-bit fields int_part: integer portion (0 to 2^64-1) frac_part: fractional portion in raw DECIMAL_SCALE units (0 to 10^18-1)…

Decimal

Fixed-point decimal number using separate 64-bit fields int_part: integer portion (0 to 2^64-1) frac_part: fractional portion in raw DECIMAL_SCALE units (0 to 10^18-1) is_negative: sign of the decimal number

Fully qualified path: alexandria_math::decimal::Decimal

[derive(Drop, Copy, PartialEq, Debug)]
pub struct Decimal {
    pub int_part: u64,
    pub frac_part: u64,
    pub is_negative: bool,
}

Members

int_part

Fully qualified path: alexandria_math::decimal::Decimal::int_part

pub int_part: u64

frac_part

Fully qualified path: alexandria_math::decimal::Decimal::frac_part

pub frac_part: u64

is_negative

Fully qualified path: alexandria_math::decimal::Decimal::is_negative

pub is_negative: bool

Traits

Traits

DecimalTrait

Fully qualified path: alexandria_math::decimal::DecimalTrait

pub trait DecimalTrait

Trait functions

from_int

Create a decimal from an integer part

Fully qualified path: alexandria_math::decimal::DecimalTrait::from_int

fn from_int(int_part: u64) -> Decimal

from_parts

Create a decimal from integer and decimal parts (user-friendly) int_part: integer portion (e.g., 3 for 3.35) decimal_part: decimal portion as integer (e.g., 35 for 0.35) Example: from_parts(3, 35) creates 3.35, from_parts(56, 678) creates 56.678

Fully qualified path: alexandria_math::decimal::DecimalTrait::from_parts

fn from_parts(int_part: u64, decimal_part: u64) -> Decimal

from_raw_parts

Create a decimal from integer and raw fractional parts (internal use) frac_part should be in range [0, 10^18) - raw DECIMAL_SCALE units

Fully qualified path: alexandria_math::decimal::DecimalTrait::from_raw_parts

fn from_raw_parts(int_part: u64, frac_part: u64) -> Decimal

from_raw_parts_signed

Create a signed decimal from integer and raw fractional parts (internal use) frac_part should be in range [0, 10^18) - raw DECIMAL_SCALE units

Fully qualified path: alexandria_math::decimal::DecimalTrait::from_raw_parts_signed

fn from_raw_parts_signed(int_part: u64, frac_part: u64, is_negative: bool) -> Decimal

from_felt

Create a decimal from a felt252 (treating it as integer) Note: This method treats all felt252 values as positive For negative values, use from_felt_signed or other constructors

Fully qualified path: alexandria_math::decimal::DecimalTrait::from_felt

fn from_felt(value: felt252) -> Decimal

from_felt_signed

Create a signed decimal from a felt252

Fully qualified path: alexandria_math::decimal::DecimalTrait::from_felt_signed

fn from_felt_signed(value: felt252, is_negative: bool) -> Decimal

from_parts_signed

Create a signed decimal from integer and decimal parts (user-friendly) int_part: integer portion (e.g., 3 for 3.35) decimal_part: decimal portion as integer (e.g., 35 for 0.35) is_negative: sign of the number Example: from_parts_signed(3, 35, true) creates -3.35

Fully qualified path: alexandria_math::decimal::DecimalTrait::from_parts_signed

fn from_parts_signed(int_part: u64, decimal_part: u64, is_negative: bool) -> Decimal

int_part

Get the integer part

Fully qualified path: alexandria_math::decimal::DecimalTrait::int_part

fn int_part(self: @Decimal) -> u64

frac_part

Get the fractional part

Fully qualified path: alexandria_math::decimal::DecimalTrait::frac_part

fn frac_part(self: @Decimal) -> u64

is_negative

Get the sign (true if negative)

Fully qualified path: alexandria_math::decimal::DecimalTrait::is_negative

fn is_negative(self: @Decimal) -> bool

to_felt

Convert to felt252 (truncates fractional part)

Fully qualified path: alexandria_math::decimal::DecimalTrait::to_felt

fn to_felt(self: @Decimal) -> felt252

add

Add two decimals

Fully qualified path: alexandria_math::decimal::DecimalTrait::add

fn add(self: @Decimal, other: @Decimal) -> Decimal

sub

Subtract two decimals

Fully qualified path: alexandria_math::decimal::DecimalTrait::sub

fn sub(self: @Decimal, other: @Decimal) -> Decimal

mul

Multiply two decimals

Fully qualified path: alexandria_math::decimal::DecimalTrait::mul

fn mul(self: @Decimal, other: @Decimal) -> Decimal

div

Divide two decimals

Fully qualified path: alexandria_math::decimal::DecimalTrait::div

fn div(self: @Decimal, other: @Decimal) -> Decimal

to_string

Convert to string representation

Fully qualified path: alexandria_math::decimal::DecimalTrait::to_string

fn to_string(self: @Decimal) -> ByteArray

from_string

Parse a decimal from string (basic implementation)

Fully qualified path: alexandria_math::decimal::DecimalTrait::from_string

fn from_string(s: ByteArray) -> Option<Decimal>

Impls

Impls

DecimalImpl

Fully qualified path: alexandria_math::decimal::DecimalImpl

pub impl DecimalImpl of DecimalTrait;

Impl functions

from_int

Create a decimal from an integer part

Fully qualified path: alexandria_math::decimal::DecimalImpl::from_int

fn from_int(int_part: u64) -> Decimal

from_parts

Create a decimal from integer and decimal parts (user-friendly) int_part: integer portion (e.g., 3 for 3.35) decimal_part: decimal portion as integer (e.g., 35 for 0.35) Example: from_parts(3, 35) creates 3.35, from_parts(56, 678) creates 56.678

Fully qualified path: alexandria_math::decimal::DecimalImpl::from_parts

fn from_parts(int_part: u64, decimal_part: u64) -> Decimal

from_raw_parts

Create a decimal from integer and raw fractional parts (internal use) frac_part should be in range [0, 10^18) - raw DECIMAL_SCALE units

Fully qualified path: alexandria_math::decimal::DecimalImpl::from_raw_parts

fn from_raw_parts(int_part: u64, frac_part: u64) -> Decimal

from_raw_parts_signed

Create a signed decimal from integer and raw fractional parts (internal use) frac_part should be in range [0, 10^18) - raw DECIMAL_SCALE units

Fully qualified path: alexandria_math::decimal::DecimalImpl::from_raw_parts_signed

fn from_raw_parts_signed(int_part: u64, frac_part: u64, is_negative: bool) -> Decimal

from_felt

Create a decimal from a felt252 (treating it as integer) Note: This method treats all felt252 values as positive For negative values, use from_felt_signed or other constructors

Fully qualified path: alexandria_math::decimal::DecimalImpl::from_felt

fn from_felt(value: felt252) -> Decimal

from_felt_signed

Create a signed decimal from a felt252

Fully qualified path: alexandria_math::decimal::DecimalImpl::from_felt_signed

fn from_felt_signed(value: felt252, is_negative: bool) -> Decimal

from_parts_signed

Create a signed decimal from integer and decimal parts (user-friendly) int_part: integer portion (e.g., 3 for 3.35) decimal_part: decimal portion as integer (e.g., 35 for 0.35) is_negative: sign of the number Example: from_parts_signed(3, 35, true) creates -3.35

Fully qualified path: alexandria_math::decimal::DecimalImpl::from_parts_signed

fn from_parts_signed(int_part: u64, decimal_part: u64, is_negative: bool) -> Decimal

int_part

Get the integer part

Fully qualified path: alexandria_math::decimal::DecimalImpl::int_part

fn int_part(self: @Decimal) -> u64

frac_part

Get the fractional part

Fully qualified path: alexandria_math::decimal::DecimalImpl::frac_part

fn frac_part(self: @Decimal) -> u64

is_negative

Get the sign (true if negative)

Fully qualified path: alexandria_math::decimal::DecimalImpl::is_negative

fn is_negative(self: @Decimal) -> bool

to_felt

Convert to felt252 (truncates fractional part)

Fully qualified path: alexandria_math::decimal::DecimalImpl::to_felt

fn to_felt(self: @Decimal) -> felt252

add

Add two decimals

Fully qualified path: alexandria_math::decimal::DecimalImpl::add

fn add(self: @Decimal, other: @Decimal) -> Decimal

sub

Subtract two decimals

Fully qualified path: alexandria_math::decimal::DecimalImpl::sub

fn sub(self: @Decimal, other: @Decimal) -> Decimal

mul

Multiply two decimals

Fully qualified path: alexandria_math::decimal::DecimalImpl::mul

fn mul(self: @Decimal, other: @Decimal) -> Decimal

div

Divide two decimals

Fully qualified path: alexandria_math::decimal::DecimalImpl::div

fn div(self: @Decimal, other: @Decimal) -> Decimal

to_string

Convert to string representation

Fully qualified path: alexandria_math::decimal::DecimalImpl::to_string

fn to_string(self: @Decimal) -> ByteArray

from_string

Parse a decimal from string (basic implementation)

Fully qualified path: alexandria_math::decimal::DecimalImpl::from_string

fn from_string(s: ByteArray) -> Option<Decimal>

ed25519

Fully qualified path: alexandria_math::ed25519

Constants

p
p_non_zero
p2x
a
c
d
d2x
l
w

Free functions

point_mult_double_and_addFunction that performs point multiplication for an Elliptic Curve point using the double and add method….
verify_signatureExperimental feature: use with caution. Not recommended for production. Verifies an Ed25519 signature against a message and public key….

Structs

Point

Traits

Constants

Constants

p
p_non_zero
p2x
a
c
d
d2x
l
w

p

Fully qualified path: alexandria_math::ed25519::p

pub const p: u256 = 57896044618658097711785492504343953926634992332820282019728792003956564819949;

p_non_zero

Fully qualified path: alexandria_math::ed25519::p_non_zero

pub const p_non_zero: NonZero<u256> =
    57896044618658097711785492504343953926634992332820282019728792003956564819949;

p2x

Fully qualified path: alexandria_math::ed25519::p2x

pub const p2x: u256 =
    115792089237316195423570985008687907853269984665640564039457584007913129639898;

a

Fully qualified path: alexandria_math::ed25519::a

pub const a: u256 = 57896044618658097711785492504343953926634992332820282019728792003956564819948;

c

Fully qualified path: alexandria_math::ed25519::c

pub const c: u256 = 3;

d

Fully qualified path: alexandria_math::ed25519::d

pub const d: u256 = 37095705934669439343138083508754565189542113879843219016388785533085940283555;

d2x

Fully qualified path: alexandria_math::ed25519::d2x

pub const d2x: u256 = 74191411869338878686276167017509130379084227759686438032777571066171880567110;

l

Fully qualified path: alexandria_math::ed25519::l

pub const l: u256 = 7237005577332262213973186563042994240857116359379907606001950938285454250989;

w

Fully qualified path: alexandria_math::ed25519::w

pub const w: u256 = 4;

Free functions

Free functions

point_mult_double_and_addFunction that performs point multiplication for an Elliptic Curve point using the double and add method….
verify_signatureExperimental feature: use with caution. Not recommended for production. Verifies an Ed25519 signature against a message and public key….

point_mult_double_and_add

Function that performs point multiplication for an Elliptic Curve point using the double and add method.

Arguments

  • scalar - Scalar such that scalar * P = P + P + P + … + P.
  • P - Elliptic Curve point
  • prime_nz - Field prime in NonZero form.

Returns

  • u256 - Resulting point

Fully qualified path: alexandria_math::ed25519::point_mult_double_and_add

pub fn point_mult_double_and_add(mut scalar: u256, mut P: Point, prime_nz: NonZero<u256>) -> Point

verify_signature

Experimental feature: use with caution. Not recommended for production. Verifies an Ed25519 signature against a message and public key.

Arguments

  • msg - The message that was signed as a span of bytes
  • signature - The signature as a span of two u256 values R, S
  • pub_key - The public key as a u256 value

Returns

  • bool - true if the signature is valid, false otherwise

Fully qualified path: alexandria_math::ed25519::verify_signature

pub fn verify_signature(msg: Span<u8>, signature: Span<u256>, pub_key: u256) -> bool

Structs

Structs

Point

Point

Fully qualified path: alexandria_math::ed25519::Point

[derive(Drop, Copy)]
pub struct Point {
    pub x: u256,
    pub y: u256,
}

Members

x

Fully qualified path: alexandria_math::ed25519::Point::x

pub x: u256

y

Fully qualified path: alexandria_math::ed25519::Point::y

pub y: u256

Traits

Traits

PointOperations

Fully qualified path: alexandria_math::ed25519::PointOperations

pub trait PointOperations<T>

Trait functions

double

Fully qualified path: alexandria_math::ed25519::PointOperations::double

fn double(self: T, prime_nz: NonZero<u256>) -> T

add

Fully qualified path: alexandria_math::ed25519::PointOperations::add

fn add(self: T, rhs: T, prime_nz: NonZero<u256>) -> T

extended_euclidean_algorithm

Extended Euclidean Algorithm.

Fully qualified path: alexandria_math::extended_euclidean_algorithm

Free functions

extended_euclidean_algorithmExtended Euclidean Algorithm….

Free functions

Free functions

extended_euclidean_algorithmExtended Euclidean Algorithm….

extended_euclidean_algorithm

Extended Euclidean Algorithm.

Arguments

  • a - First number.
  • b - Second number.

Returns

  • gcd - Greatest common divisor.
  • x - First Bezout coefficient.
  • y - Second Bezout coefficient.

Fully qualified path: alexandria_math::extended_euclidean_algorithm::extended_euclidean_algorithm

pub fn extended_euclidean_algorithm(a: u128, b: u128) -> (u128, u128, u128)

fast_power

Fast power algorithm

Fully qualified path: alexandria_math::fast_power

Free functions

fast_powerCalculate the base ^ power using the fast powering algorithm…
fast_power_modCalculate the ( base ^ power ) mod modulus using the fast powering algorithm…

Free functions

Free functions

fast_powerCalculate the base ^ power using the fast powering algorithm…
fast_power_modCalculate the ( base ^ power ) mod modulus using the fast powering algorithm…

fast_power

Calculate the base ^ power using the fast powering algorithm

Arguments

  • base - The base of the exponentiation
  • power - The power of the exponentiation

Returns

  • T - The result of base ^ power

Panics

  • base is 0

Fully qualified path: alexandria_math::fast_power::fast_power

pub fn fast_power<
    T,
    +Div<T>,
    +DivAssign<T, T>,
    +Rem<T>,
    +Into<u8, T>,
    +Into<T, u256>,
    +TryInto<u256, T>,
    +PartialEq<T>,
    +Copy<T>,
    +Drop<T>,
>(
    base: T, mut power: T,
) -> T

fast_power_mod

Calculate the ( base ^ power ) mod modulus using the fast powering algorithm

Arguments

  • base - The base of the exponentiation
  • power - The power of the exponentiation
  • modulus - The modulus used in the calculation

Returns

  • T - The result of ( base ^ power ) mod modulus

Panics

  • base is 0

Fully qualified path: alexandria_math::fast_power::fast_power_mod

pub fn fast_power_mod<
    T,
    +Div<T>,
    +DivAssign<T, T>,
    +Rem<T>,
    +Into<u8, T>,
    +Into<T, u256>,
    +TryInto<u256, T>,
    +PartialEq<T>,
    +Copy<T>,
    +Drop<T>,
>(
    base: T, mut power: T, modulus: T,
) -> T

fast_root

Fast root algorithm using the Newton-Raphson method

Fully qualified path: alexandria_math::fast_root

Free functions

fast_nr_optimizeNewton-Raphson optimization to solve the equation a^r = x. The optimization has a quadratic convergence rate….
fast_sqrtCalculate the sqrt(x)…
fast_cbrtCalculate the cubic root of x…
round_divCalculate the division of a by b with rounding…

Free functions

Free functions

fast_nr_optimizeNewton-Raphson optimization to solve the equation a^r = x. The optimization has a quadratic convergence rate….
fast_sqrtCalculate the sqrt(x)…
fast_cbrtCalculate the cubic root of x…
round_divCalculate the division of a by b with rounding…

fast_nr_optimize

Newton-Raphson optimization to solve the equation a^r = x. The optimization has a quadratic convergence rate.

Arguments

  • x - The number to calculate the root of
  • r - The root to calculate
  • iter - The number of iterations to run the algorithm

Returns

  • u128 - The root of x with rounding. (e.g., sqrt(5) = 2.24 -> 2, sqrt(7) = 2.65 -> 3)

Fully qualified path: alexandria_math::fast_root::fast_nr_optimize

pub fn fast_nr_optimize(x: u128, r: u128, iter: u32) -> u128

fast_sqrt

Calculate the sqrt(x)

Arguments

  • x - The number to calculate the sqrt of
  • iter - The number of iterations to run the algorithm

Returns

  • u128 - The sqrt of x with rounding (e.g., sqrt(5) = 2.24 -> 2, sqrt(7) = 2.65 -> 3)

Fully qualified path: alexandria_math::fast_root::fast_sqrt

pub fn fast_sqrt(x: u128, iter: u32) -> u128

fast_cbrt

Calculate the cubic root of x

Arguments

  • x - The number to calculate the cubic root of
  • iter - The number of iterations to run the algorithm

Returns

  • u128 - The cubic root of x with rounding (e.g., cbrt(4) = 1.59 -> 2, cbrt(5) = 1.71 -> 2)

Fully qualified path: alexandria_math::fast_root::fast_cbrt

pub fn fast_cbrt(x: u128, iter: u32) -> u128

round_div

Calculate the division of a by b with rounding

Arguments

  • a - The dividend
  • b - The divisor

Returns

  • u128 - The result of the division with rounding (e.g., 5/3 = 2, 7/3 = 2, 8/3 = 3)

Fully qualified path: alexandria_math::fast_root::round_div

pub fn round_div(a: u128, b: u128) -> u128

fibonacci

Fully qualified path: alexandria_math::fibonacci

Free functions

fibCalculate fibonacci sequence value at the nth position This function computes the fibonacci number at position n using a recursive approach….

Free functions

Free functions

fibCalculate fibonacci sequence value at the nth position This function computes the fibonacci number at position n using a recursive approach….

fib

Calculate fibonacci sequence value at the nth position

This function computes the fibonacci number at position n using a recursive approach. The sequence starts with the provided initial values a and b, and continues according to the fibonacci rule where each number is the sum of the two preceding ones.

Arguments

  • a - The first number in the sequence (F₀)
  • b - The second number in the sequence (F₁)
  • n - The position in the sequence to calculate (0-indexed)

Returns

  • felt252 - The nth number in the fibonacci sequence

Fully qualified path: alexandria_math::fibonacci::fib

pub fn fib(a: felt252, b: felt252, n: felt252) -> felt252

gcd_of_n_numbers

GCD for N numbers

Fully qualified path: alexandria_math::gcd_of_n_numbers

Free functions

gcdCalculate the greatest common divisor for n numbers…
gcd_two_numbersInternal function to calculate the gcd between two numbers…

Free functions

Free functions

gcdCalculate the greatest common divisor for n numbers…
gcd_two_numbersInternal function to calculate the gcd between two numbers…

gcd

Calculate the greatest common divisor for n numbers

Arguments

  • n - The array of numbers to calculate the gcd for

Returns

  • felt252 - The gcd of input numbers

Fully qualified path: alexandria_math::gcd_of_n_numbers::gcd

pub fn gcd(mut n: Span<u128>) -> u128

gcd_two_numbers

Internal function to calculate the gcd between two numbers

Arguments

  • a - The first number for which to calculate the gcd
  • b - The first number for which to calculate the gcd

Returns

  • felt252 - The gcd of a and b

Fully qualified path: alexandria_math::gcd_of_n_numbers::gcd_two_numbers

pub fn gcd_two_numbers(mut a: u128, mut b: u128) -> u128

i257

Fully qualified path: alexandria_math::i257

Free functions

i257_div_remCalculates both the quotient and the remainder of the division of a first i257 by a second i257….
i257_assert_no_negative_zeroChecks if the given i257 integer is zero and has the correct sign….
i257_absComputes the absolute value of the given i257 integer….

Structs

i257i257 represents a 129-bit integer. The abs field holds the absolute value of the integer. The is_negative field is true for negative integers, and false for non-negative integers.

Traits

Impls

I257Impl
i257Zeroable
DisplayI257ImplImplements the Display trait for i257.

Free functions

Free functions

i257_div_remCalculates both the quotient and the remainder of the division of a first i257 by a second i257….
i257_assert_no_negative_zeroChecks if the given i257 integer is zero and has the correct sign….
i257_absComputes the absolute value of the given i257 integer….

i257_div_rem

Calculates both the quotient and the remainder of the division of a first i257 by a second i257.

Arguments

  • lhs - The i257 dividend.
  • rhs - The i257 divisor.

Returns

  • (i257, i257) - A tuple containing the quotient and the remainder of dividing lhs by rhs.

Fully qualified path: alexandria_math::i257::i257_div_rem

pub fn i257_div_rem(lhs: i257, rhs: i257) -> (i257, i257)

i257_assert_no_negative_zero

Checks if the given i257 integer is zero and has the correct sign.

Arguments

  • x - The i257 integer to check.

Panics

Panics if x is zero and is negative

Fully qualified path: alexandria_math::i257::i257_assert_no_negative_zero

pub fn i257_assert_no_negative_zero(x: i257)

i257_abs

Computes the absolute value of the given i257 integer.

Arguments

  • x - The i257 integer to compute the absolute value of.

Returns

  • i257 - The absolute value of x.

Fully qualified path: alexandria_math::i257::i257_abs

pub fn i257_abs(x: i257) -> i257

Structs

Structs

i257i257 represents a 129-bit integer. The abs field holds the absolute value of the integer. The is_negative field is true for negative integers, and false for non-negative integers.

i257

i257 represents a 129-bit integer. The abs field holds the absolute value of the integer. The is_negative field is true for negative integers, and false for non-negative integers.

Fully qualified path: alexandria_math::i257::i257

[derive(Serde, Copy, Drop, Hash)]
pub struct i257 { /* private fields */ }

Traits

Traits

I257Trait

Fully qualified path: alexandria_math::i257::I257Trait

pub trait I257Trait

Trait functions

new

Creates a new i257 from an absolute value and sign. Ensures zero is always represented as positive.

Arguments

  • abs - The absolute value as a u256
  • is_negative - Whether the number is negative

Returns

  • i257 - The constructed signed integer

Fully qualified path: alexandria_math::i257::I257Trait::new

fn new(abs: u256, is_negative: bool) -> i257

is_negative

Returns whether the i257 is negative.

Arguments

  • self - The i257 to check

Returns

  • bool - true if negative, false if positive or zero

Fully qualified path: alexandria_math::i257::I257Trait::is_negative

fn is_negative(self: i257) -> bool

abs

Returns the absolute value of the i257.

Arguments

  • self - The i257 to get absolute value from

Returns

  • u256 - The absolute value

Fully qualified path: alexandria_math::i257::I257Trait::abs

fn abs(self: i257) -> u256

Impls

Impls

I257Impl
i257Zeroable
DisplayI257ImplImplements the Display trait for i257.

I257Impl

Fully qualified path: alexandria_math::i257::I257Impl

pub impl I257Impl of I257Trait;

Impl functions

new

Creates a new i257 from an absolute value and sign. Ensures zero is always represented as positive.

Arguments

  • abs - The absolute value as a u256
  • is_negative - Whether the number is negative

Returns

  • i257 - The constructed signed integer

Fully qualified path: alexandria_math::i257::I257Impl::new

fn new(abs: u256, is_negative: bool) -> i257

is_negative

Returns whether the i257 is negative.

Arguments

  • self - The i257 to check

Returns

  • bool - true if negative, false if positive or zero

Fully qualified path: alexandria_math::i257::I257Impl::is_negative

fn is_negative(self: i257) -> bool

abs

Returns the absolute value of the i257.

Arguments

  • self - The i257 to get absolute value from

Returns

  • u256 - The absolute value

Fully qualified path: alexandria_math::i257::I257Impl::abs

fn abs(self: i257) -> u256

i257Zeroable

Fully qualified path: alexandria_math::i257::i257Zeroable

pub impl i257Zeroable of Zero<i257>;

Impl functions

zero

Fully qualified path: alexandria_math::i257::i257Zeroable::zero

fn zero() -> i257

is_zero

Fully qualified path: alexandria_math::i257::i257Zeroable::is_zero

fn is_zero(self: @i257) -> bool

is_non_zero

Fully qualified path: alexandria_math::i257::i257Zeroable::is_non_zero

fn is_non_zero(self: @i257) -> bool

DisplayI257Impl

Implements the Display trait for i257.

Fully qualified path: alexandria_math::i257::DisplayI257Impl

pub impl DisplayI257Impl of Display<i257>;

Impl functions

fmt

Fully qualified path: alexandria_math::i257::DisplayI257Impl::fmt

fn fmt(self: @i257, ref f: Formatter) -> Result<(), Error>

is_power_of_two

Fully qualified path: alexandria_math::is_power_of_two

Free functions

is_power_of_twoCheck if the given number is power of 2…

Free functions

Free functions

is_power_of_twoCheck if the given number is power of 2…

is_power_of_two

Check if the given number is power of 2

Arguments

  • n - The given number

Returns

  • bool - if the given number is power of 2

Fully qualified path: alexandria_math::is_power_of_two::is_power_of_two

pub fn is_power_of_two(n: u128) -> bool

is_prime

Fully qualified path: alexandria_math::is_prime

Free functions

is_primeCheck if the given number is prime…

Free functions

Free functions

is_primeCheck if the given number is prime…

is_prime

Check if the given number is prime

Arguments

  • n - The given number
  • iter - The number of iterations to run when sqrting the number, the higher the more accurate (usually 10 is enough)

Returns

  • bool - if the given number is prime

Fully qualified path: alexandria_math::is_prime::is_prime

pub fn is_prime(n: u128, iter: u32) -> bool

karatsuba

Karatsuba Multiplication.

Fully qualified path: alexandria_math::karatsuba

Free functions

multiplyAlgorithm to multiply two numbers in O(n^1.6) running time…

Free functions

Free functions

multiplyAlgorithm to multiply two numbers in O(n^1.6) running time…

multiply

Algorithm to multiply two numbers in O(n^1.6) running time

Arguments

  • x - First number to multiply.
  • y - Second number to multiply.

Returns

  • u128 - The product between x and y

Fully qualified path: alexandria_math::karatsuba::multiply

pub fn multiply(x: u128, y: u128) -> u128

keccak256

Fully qualified path: alexandria_math::keccak256

Free functions

keccak256Computes the Solidity-compatible Keccak hash of an array of bytes….

Free functions

Free functions

keccak256Computes the Solidity-compatible Keccak hash of an array of bytes….

keccak256

Computes the Solidity-compatible Keccak hash of an array of bytes.

Arguments

  • self - A Array<u8> of bytes.

Returns

A u256 value representing the Keccak hash of the input bytes array.

Fully qualified path: alexandria_math::keccak256::keccak256

pub fn keccak256(mut self: Span<u8>) -> u256

lcm_of_n_numbers

LCM for N numbers

Fully qualified path: alexandria_math::lcm_of_n_numbers

Free functions

lcmCalculate the lowest common multiple for n numbers…

Enums

Free functions

Free functions

lcmCalculate the lowest common multiple for n numbers…

lcm

Calculate the lowest common multiple for n numbers

Arguments

  • n - The array of numbers to calculate the lcm for

Returns

  • Result<T, LCMError> - The lcm of input numbers

Fully qualified path: alexandria_math::lcm_of_n_numbers::lcm

pub fn lcm<T, +Into<T, u128>, +Into<u128, T>, +Mul<T>, +Div<T>, +Copy<T>, +Drop<T>>(
    mut n: Span<T>,
) -> Result<T, LCMError>

Enums

Enums

LCMError

Fully qualified path: alexandria_math::lcm_of_n_numbers::LCMError

pub enum LCMError {
    EmptyInput,
}

Variants

EmptyInput

Fully qualified path: alexandria_math::lcm_of_n_numbers::LCMError::EmptyInput

EmptyInput

mod_arithmetics

Fully qualified path: alexandria_math::mod_arithmetics

Free functions

add_modFunction that performs modular addition. Will panick if result is > u256 max…
mult_inverseFunction that return the modular multiplicative inverse. Disclaimer: this function should only be used with a prime modulo….
add_inverse_modFunction that return the modular additive inverse….
sub_modFunction that performs modular subtraction….
mult_modFunction that performs modular multiplication….
u256_wide_sqrComputes the square of a u256 value and returns the result as u512 to handle overflow This function performs wide multiplication to compute a^2 without losing precision,…
sqr_modFunction that performs modular multiplication….
div_modFunction that performs modular division….
pow_modFunction that performs modular exponentiation….
equality_modChecks if two u256 values are congruent modulo a given modulus This function computes whether a ≡ b (mod modulo) by comparing their remainders…

Free functions

Free functions

add_modFunction that performs modular addition. Will panick if result is > u256 max…
mult_inverseFunction that return the modular multiplicative inverse. Disclaimer: this function should only be used with a prime modulo….
add_inverse_modFunction that return the modular additive inverse….
sub_modFunction that performs modular subtraction….
mult_modFunction that performs modular multiplication….
u256_wide_sqrComputes the square of a u256 value and returns the result as u512 to handle overflow This function performs wide multiplication to compute a^2 without losing precision,…
sqr_modFunction that performs modular multiplication….
div_modFunction that performs modular division….
pow_modFunction that performs modular exponentiation….
equality_modChecks if two u256 values are congruent modulo a given modulus This function computes whether a ≡ b (mod modulo) by comparing their remainders…

add_mod

Function that performs modular addition. Will panick if result is > u256 max

Arguments

  • a - Left hand side of addition.
  • b - Right hand side of addition.
  • modulo - modulo.

Returns

  • u256 - result of modular addition

Fully qualified path: alexandria_math::mod_arithmetics::add_mod

pub fn add_mod(a: u256, b: u256, modulo: u256) -> u256

mult_inverse

Function that return the modular multiplicative inverse. Disclaimer: this function should only be used with a prime modulo.

Arguments

  • b - Number of which to find the multiplicative inverse of.
  • modulo - modulo.

Returns

  • u256 - modular multiplicative inverse

Fully qualified path: alexandria_math::mod_arithmetics::mult_inverse

pub fn mult_inverse(b: u256, mod_non_zero: NonZero<u256>) -> u256

add_inverse_mod

Function that return the modular additive inverse.

Arguments

  • b - Number of which to find the additive inverse of.
  • modulo - modulo.

Returns

  • u256 - modular additive inverse

Fully qualified path: alexandria_math::mod_arithmetics::add_inverse_mod

pub fn add_inverse_mod(b: u256, modulo: u256) -> u256

sub_mod

Function that performs modular subtraction.

Arguments

  • a - Left hand side of subtraction.
  • b - Right hand side of subtraction.
  • modulo - modulo.

Returns

  • u256 - result of modular subtraction

Fully qualified path: alexandria_math::mod_arithmetics::sub_mod

pub fn sub_mod(mut a: u256, mut b: u256, modulo: u256) -> u256

mult_mod

Function that performs modular multiplication.

Arguments

  • a - Left hand side of multiplication.
  • b - Right hand side of multiplication.
  • modulo - modulo.

Returns

  • u256 - result of modular multiplication

Fully qualified path: alexandria_math::mod_arithmetics::mult_mod

pub fn mult_mod(a: u256, b: u256, mod_non_zero: NonZero<u256>) -> u256

u256_wide_sqr

Computes the square of a u256 value and returns the result as u512 to handle overflow

This function performs wide multiplication to compute a^2 without losing precision, using the identity (a.high * 2^128 + a.low)^2 to handle the full 512-bit result.

Arguments

  • a - The u256 value to square

Returns

  • u512 - The square of the input value as a 512-bit result

Fully qualified path: alexandria_math::mod_arithmetics::u256_wide_sqr

pub fn u256_wide_sqr(a: u256) -> u512

sqr_mod

Function that performs modular multiplication.

Arguments

  • a - Left hand side of multiplication.
  • b - Right hand side of multiplication.
  • modulo - modulo.

Returns

  • u256 - result of modular multiplication

Fully qualified path: alexandria_math::mod_arithmetics::sqr_mod

pub fn sqr_mod(a: u256, mod_non_zero: NonZero<u256>) -> u256

div_mod

Function that performs modular division.

Arguments

  • a - Left hand side of division.
  • b - Right hand side of division.
  • modulo - modulo.

Returns

  • u256 - result of modular division

Fully qualified path: alexandria_math::mod_arithmetics::div_mod

pub fn div_mod(a: u256, b: u256, mod_non_zero: NonZero<u256>) -> u256

pow_mod

Function that performs modular exponentiation.

Arguments

  • base - Base of exponentiation.
  • pow - Power of exponentiation.
  • modulo - modulo.

Returns

  • u256 - result of modular exponentiation

Fully qualified path: alexandria_math::mod_arithmetics::pow_mod

pub fn pow_mod(mut base: u256, mut pow: u256, mod_non_zero: NonZero<u256>) -> u256

equality_mod

Checks if two u256 values are congruent modulo a given modulus

This function computes whether a ≡ b (mod modulo) by comparing their remainders when divided by the modulus. Two numbers are congruent modulo m if they have the same remainder when divided by m.

Arguments

  • a - The first u256 value to compare
  • b - The second u256 value to compare
  • modulo - The modulus for the congruence test

Returns

  • bool - true if a ≡ b (mod modulo), false otherwise

Fully qualified path: alexandria_math::mod_arithmetics::equality_mod

pub fn equality_mod(a: u256, b: u256, modulo: u256) -> bool

opt_math

Fully qualified path: alexandria_math::opt_math

Free functions

shr256Optimized right bit shift of b by a over u256….
shl256Optimized left bit shift of b by a over u256….
shr128Optimized right bit shift of b by a over u128….
shl128Optimized left bit shift of b by a over u128….
shr64Optimized right bit shift of b by a over u64….
shl64Optimized left bit shift of b by a over u64….
shr32Optimized right bit shift of b by a over u32….
shl32Optimized left bit shift of b by a over u64….
shr16Optimized right bit shift of b by a over u16….
shl16Optimized left bit shift of b by a over u64….
shr8Optimized right bit shift of b by a over u8….
shl8Optimized left bit shift of b by a over u64….
rotl256Optimized left bit rotate of b by a over u256….
rotr256Optimized right bit rotate of b by a over u256….
rotl128Optimized left bit rotate of b by a over u128….
rotr128Optimized right bit rotate of b by a over u128….
rotl64Optimized left bit rotate of b by a over u64….
rotr64Optimized right bit rotate of b by a over u64….
rotl32Optimized left bit rotate of b by a over u32….
rotr32Optimized right bit rotate of b by a over u32….
rotl16Optimized left bit rotate of b by a over u16….
rotr16Optimized right bit rotate of b by a over u16….
rotl8Optimized left bit rotate of b by a over u8….
rotr8Optimized right bit rotate of b by a over u8….

Traits

OptWrappingOptimized opt_wrapping math trait (overflowing add, sub, mul).
OptBitShiftOptimized bit shift trait.
OptBitRotateOptimized bit rotation trait.

Impls

Free functions

Free functions

shr256Optimized right bit shift of b by a over u256….
shl256Optimized left bit shift of b by a over u256….
shr128Optimized right bit shift of b by a over u128….
shl128Optimized left bit shift of b by a over u128….
shr64Optimized right bit shift of b by a over u64….
shl64Optimized left bit shift of b by a over u64….
shr32Optimized right bit shift of b by a over u32….
shl32Optimized left bit shift of b by a over u64….
shr16Optimized right bit shift of b by a over u16….
shl16Optimized left bit shift of b by a over u64….
shr8Optimized right bit shift of b by a over u8….
shl8Optimized left bit shift of b by a over u64….
rotl256Optimized left bit rotate of b by a over u256….
rotr256Optimized right bit rotate of b by a over u256….
rotl128Optimized left bit rotate of b by a over u128….
rotr128Optimized right bit rotate of b by a over u128….
rotl64Optimized left bit rotate of b by a over u64….
rotr64Optimized right bit rotate of b by a over u64….
rotl32Optimized left bit rotate of b by a over u32….
rotr32Optimized right bit rotate of b by a over u32….
rotl16Optimized left bit rotate of b by a over u16….
rotr16Optimized right bit rotate of b by a over u16….
rotl8Optimized left bit rotate of b by a over u8….
rotr8Optimized right bit rotate of b by a over u8….

shr256

Optimized right bit shift of b by a over u256.

Arguments

  • a - Number of shifts (must be <= 255).
  • b - Value to be shifted.

Returns

  • u256 - result of right shift

Fully qualified path: alexandria_math::opt_math::shr256

pub fn shr256(a: u8, b: u256) -> u256

shl256

Optimized left bit shift of b by a over u256.

Arguments

  • a - Number of shifts (must be <= 255).
  • b - Value to be shifted.

Returns

  • u256 - result of left shift

Fully qualified path: alexandria_math::opt_math::shl256

pub fn shl256(a: u8, b: u256) -> u256

shr128

Optimized right bit shift of b by a over u128.

Arguments

  • a - Number of shifts (must be <= 127).
  • b - Value to be shifted.

Returns

  • u128 - result of right shift

Fully qualified path: alexandria_math::opt_math::shr128

pub fn shr128(a: u8, b: u128) -> u128

shl128

Optimized left bit shift of b by a over u128.

Arguments

  • a - Number of shifts (must be <= 127).
  • b - Value to be shifted.

Returns

  • u128 - result of left shift

Fully qualified path: alexandria_math::opt_math::shl128

pub fn shl128(a: u8, b: u128) -> u128

shr64

Optimized right bit shift of b by a over u64.

Arguments

  • a - Number of shifts (must be <= 63).
  • b - Value to be shifted.

Returns

  • u64 - result of right shift

Fully qualified path: alexandria_math::opt_math::shr64

pub fn shr64(a: u8, b: u64) -> u64

shl64

Optimized left bit shift of b by a over u64.

Arguments

  • a - Number of shifts (must be <= 63).
  • b - Value to be shifted.

Returns

  • u64 - result of left shift

Fully qualified path: alexandria_math::opt_math::shl64

pub fn shl64(a: u8, b: u64) -> u64

shr32

Optimized right bit shift of b by a over u32.

Arguments

  • a - Number of shifts (must be <= 31).
  • b - Value to be shifted.

Returns

  • u32 - result of right shift

Fully qualified path: alexandria_math::opt_math::shr32

pub fn shr32(a: u8, b: u32) -> u32

shl32

Optimized left bit shift of b by a over u64.

Arguments

  • a - Number of shifts (must be <= 31).
  • b - Value to be shifted.

Returns

  • u32 - result of left shift

Fully qualified path: alexandria_math::opt_math::shl32

pub fn shl32(a: u8, b: u32) -> u32

shr16

Optimized right bit shift of b by a over u16.

Arguments

  • a - Number of shifts (must be <= 15).
  • b - Value to be shifted.

Returns

  • u16 - result of right shift

Fully qualified path: alexandria_math::opt_math::shr16

pub fn shr16(a: u8, b: u16) -> u16

shl16

Optimized left bit shift of b by a over u64.

Arguments

  • a - Number of shifts (must be <= 15).
  • b - Value to be shifted.

Returns

  • u16 - result of left shift

Fully qualified path: alexandria_math::opt_math::shl16

pub fn shl16(a: u8, b: u16) -> u16

shr8

Optimized right bit shift of b by a over u8.

Arguments

  • a - Number of shifts (must be <= 7).
  • b - Value to be shifted.

Returns

  • u8 - result of right shift

Fully qualified path: alexandria_math::opt_math::shr8

pub fn shr8(a: u8, b: u8) -> u8

shl8

Optimized left bit shift of b by a over u64.

Arguments

  • a - Number of shifts (must be <= 7).
  • b - Value to be shifted.

Returns

  • u8 - result of left shift

Fully qualified path: alexandria_math::opt_math::shl8

pub fn shl8(a: u8, b: u8) -> u8

rotl256

Optimized left bit rotate of b by a over u256.

Arguments

  • a - Number of rotations (0 < a <= 255).
  • b - Value to be rotated.

Returns

  • u256 - result of left rotate

Fully qualified path: alexandria_math::opt_math::rotl256

pub fn rotl256(a: u8, b: u256) -> u256

rotr256

Optimized right bit rotate of b by a over u256.

Arguments

  • a - Number of rotations (0 < a <= 255).
  • b - Value to be rotated.

Returns

  • u256 - result of left rotate

Fully qualified path: alexandria_math::opt_math::rotr256

pub fn rotr256(a: u8, b: u256) -> u256

rotl128

Optimized left bit rotate of b by a over u128.

Arguments

  • a - Number of rotations (0 < a <= 127).
  • b - Value to be rotated.

Returns

  • u128 - result of left rotate

Fully qualified path: alexandria_math::opt_math::rotl128

pub fn rotl128(a: u8, b: u128) -> u128

rotr128

Optimized right bit rotate of b by a over u128.

Arguments

  • a - Number of rotations (0 < a <= 127).
  • b - Value to be rotated.

Returns

  • u128 - result of left rotate

Fully qualified path: alexandria_math::opt_math::rotr128

pub fn rotr128(a: u8, b: u128) -> u128

rotl64

Optimized left bit rotate of b by a over u64.

Arguments

  • a - Number of rotations (0 < a <= 63).
  • b - Value to be rotated.

Returns

  • u64 - result of left rotate

Fully qualified path: alexandria_math::opt_math::rotl64

pub fn rotl64(a: u8, b: u64) -> u64

rotr64

Optimized right bit rotate of b by a over u64.

Arguments

  • a - Number of rotations (0 < a <= 63).
  • b - Value to be rotated.

Returns

  • u64 - result of left rotate

Fully qualified path: alexandria_math::opt_math::rotr64

pub fn rotr64(a: u8, b: u64) -> u64

rotl32

Optimized left bit rotate of b by a over u32.

Arguments

  • a - Number of rotations (0 < a <= 31).
  • b - Value to be rotated.

Returns

  • u32 - result of left rotate

Fully qualified path: alexandria_math::opt_math::rotl32

pub fn rotl32(a: u8, b: u32) -> u32

rotr32

Optimized right bit rotate of b by a over u32.

Arguments

  • a - Number of rotations (0 < a <= 31).
  • b - Value to be rotated.

Returns

  • u32 - result of left rotate

Fully qualified path: alexandria_math::opt_math::rotr32

pub fn rotr32(a: u8, b: u32) -> u32

rotl16

Optimized left bit rotate of b by a over u16.

Arguments

  • a - Number of rotations (0 < a <= 15).
  • b - Value to be rotated.

Returns

  • u16 - result of left rotate

Fully qualified path: alexandria_math::opt_math::rotl16

pub fn rotl16(a: u8, b: u16) -> u16

rotr16

Optimized right bit rotate of b by a over u16.

Arguments

  • a - Number of rotations (0 < a <= 15).
  • b - Value to be rotated.

Returns

  • u16 - result of left rotate

Fully qualified path: alexandria_math::opt_math::rotr16

pub fn rotr16(a: u8, b: u16) -> u16

rotl8

Optimized left bit rotate of b by a over u8.

Arguments

  • a - Number of rotations (0 < a <= 7).
  • b - Value to be rotated.

Returns

  • u8 - result of left rotate

Fully qualified path: alexandria_math::opt_math::rotl8

pub fn rotl8(a: u8, b: u8) -> u8

rotr8

Optimized right bit rotate of b by a over u8.

Arguments

  • a - Number of rotations (0 < a <= 7).
  • b - Value to be rotated.

Returns

  • u8 - result of left rotate

Fully qualified path: alexandria_math::opt_math::rotr8

pub fn rotr8(a: u8, b: u8) -> u8

Traits

Traits

OptWrappingOptimized opt_wrapping math trait (overflowing add, sub, mul).
OptBitShiftOptimized bit shift trait.
OptBitRotateOptimized bit rotation trait.

OptWrapping

Optimized opt_wrapping math trait (overflowing add, sub, mul).

Fully qualified path: alexandria_math::opt_math::OptWrapping

pub trait OptWrapping<T>

Trait functions

opt_wrapping_add

Returns wrapped result of overflowing addition.

Fully qualified path: alexandria_math::opt_math::OptWrapping::opt_wrapping_add

fn opt_wrapping_add(self: T, v: T) -> T

opt_wrapping_sub

Returns wrapped result of overflowing substraction.

Fully qualified path: alexandria_math::opt_math::OptWrapping::opt_wrapping_sub

fn opt_wrapping_sub(self: T, v: T) -> T

opt_wrapping_mul

Returns wrapped result of overflowing multiplication.

Fully qualified path: alexandria_math::opt_math::OptWrapping::opt_wrapping_mul

fn opt_wrapping_mul(self: T, v: T) -> T

OptBitShift

Optimized bit shift trait.

Fully qualified path: alexandria_math::opt_math::OptBitShift

pub trait OptBitShift<T, +WideMul<T, T>>

Trait functions

shl

Optimized left bit shift of x by y up to T.bits - 1.

Arguments

  • x - Value to be shifted.
  • y - Number of shifts.

Returns

  • T - result of left shift

Fully qualified path: alexandria_math::opt_math::OptBitShift::shl

fn shl(x: T, n: u8) -> T

shr

Optimized right bit shift of x by y up to T.bits - 1.

Arguments

  • x - Value to be shifted.
  • y - Number of shifts.

Returns

  • T - result of left shift

Fully qualified path: alexandria_math::opt_math::OptBitShift::shr

fn shr(x: T, n: u8) -> T

OptBitRotate

Optimized bit rotation trait.

Fully qualified path: alexandria_math::opt_math::OptBitRotate

pub trait OptBitRotate<T, +WideMul<T, T>>

Trait functions

rotl

Fully qualified path: alexandria_math::opt_math::OptBitRotate::rotl

fn rotl(x: T, n: u8) -> T

rotr

Fully qualified path: alexandria_math::opt_math::OptBitRotate::rotr

fn rotr(x: T, n: u8) -> T

Impls

Impls

U256OptWrappingImpl

Fully qualified path: alexandria_math::opt_math::U256OptWrappingImpl

pub impl U256OptWrappingImpl of OptWrapping<u256>;

Impl functions

opt_wrapping_add

Fully qualified path: alexandria_math::opt_math::U256OptWrappingImpl::opt_wrapping_add

fn opt_wrapping_add(self: u256, v: u256) -> u256

opt_wrapping_sub

Fully qualified path: alexandria_math::opt_math::U256OptWrappingImpl::opt_wrapping_sub

fn opt_wrapping_sub(self: u256, v: u256) -> u256

opt_wrapping_mul

Fully qualified path: alexandria_math::opt_math::U256OptWrappingImpl::opt_wrapping_mul

fn opt_wrapping_mul(self: u256, v: u256) -> u256

U128OptWrappingImpl

Fully qualified path: alexandria_math::opt_math::U128OptWrappingImpl

pub impl U128OptWrappingImpl of OptWrapping<u128>;

Impl functions

opt_wrapping_add

Fully qualified path: alexandria_math::opt_math::U128OptWrappingImpl::opt_wrapping_add

fn opt_wrapping_add(self: u128, v: u128) -> u128

opt_wrapping_sub

Fully qualified path: alexandria_math::opt_math::U128OptWrappingImpl::opt_wrapping_sub

fn opt_wrapping_sub(self: u128, v: u128) -> u128

opt_wrapping_mul

Fully qualified path: alexandria_math::opt_math::U128OptWrappingImpl::opt_wrapping_mul

fn opt_wrapping_mul(self: u128, v: u128) -> u128

U64OptWrappingAddImpl

Fully qualified path: alexandria_math::opt_math::U64OptWrappingAddImpl

pub impl U64OptWrappingAddImpl of OptWrapping<u64>;

Impl functions

opt_wrapping_add

Fully qualified path: alexandria_math::opt_math::U64OptWrappingAddImpl::opt_wrapping_add

fn opt_wrapping_add(self: u64, v: u64) -> u64

opt_wrapping_sub

Fully qualified path: alexandria_math::opt_math::U64OptWrappingAddImpl::opt_wrapping_sub

fn opt_wrapping_sub(self: u64, v: u64) -> u64

opt_wrapping_mul

Fully qualified path: alexandria_math::opt_math::U64OptWrappingAddImpl::opt_wrapping_mul

fn opt_wrapping_mul(self: u64, v: u64) -> u64

U32OptWrappingImpl

Fully qualified path: alexandria_math::opt_math::U32OptWrappingImpl

pub impl U32OptWrappingImpl of OptWrapping<u32>;

Impl functions

opt_wrapping_add

Fully qualified path: alexandria_math::opt_math::U32OptWrappingImpl::opt_wrapping_add

fn opt_wrapping_add(self: u32, v: u32) -> u32

opt_wrapping_sub

Fully qualified path: alexandria_math::opt_math::U32OptWrappingImpl::opt_wrapping_sub

fn opt_wrapping_sub(self: u32, v: u32) -> u32

opt_wrapping_mul

Fully qualified path: alexandria_math::opt_math::U32OptWrappingImpl::opt_wrapping_mul

fn opt_wrapping_mul(self: u32, v: u32) -> u32

U16OptWrappingmpl

Fully qualified path: alexandria_math::opt_math::U16OptWrappingmpl

pub impl U16OptWrappingmpl of OptWrapping<u16>;

Impl functions

opt_wrapping_add

Fully qualified path: alexandria_math::opt_math::U16OptWrappingmpl::opt_wrapping_add

fn opt_wrapping_add(self: u16, v: u16) -> u16

opt_wrapping_sub

Fully qualified path: alexandria_math::opt_math::U16OptWrappingmpl::opt_wrapping_sub

fn opt_wrapping_sub(self: u16, v: u16) -> u16

opt_wrapping_mul

Fully qualified path: alexandria_math::opt_math::U16OptWrappingmpl::opt_wrapping_mul

fn opt_wrapping_mul(self: u16, v: u16) -> u16

U8OptWrappingImpl

Fully qualified path: alexandria_math::opt_math::U8OptWrappingImpl

pub impl U8OptWrappingImpl of OptWrapping<u8>;

Impl functions

opt_wrapping_add

Fully qualified path: alexandria_math::opt_math::U8OptWrappingImpl::opt_wrapping_add

fn opt_wrapping_add(self: u8, v: u8) -> u8

opt_wrapping_sub

Fully qualified path: alexandria_math::opt_math::U8OptWrappingImpl::opt_wrapping_sub

fn opt_wrapping_sub(self: u8, v: u8) -> u8

opt_wrapping_mul

Fully qualified path: alexandria_math::opt_math::U8OptWrappingImpl::opt_wrapping_mul

fn opt_wrapping_mul(self: u8, v: u8) -> u8

U256OptBitShift

Fully qualified path: alexandria_math::opt_math::U256OptBitShift

pub impl U256OptBitShift of OptBitShift<u256, WideMulU256>;

Impl functions

shl

Fully qualified path: alexandria_math::opt_math::U256OptBitShift::shl

fn shl(x: u256, n: u8) -> u256

shr

Fully qualified path: alexandria_math::opt_math::U256OptBitShift::shr

fn shr(x: u256, n: u8) -> u256

U128OptBitShift

Fully qualified path: alexandria_math::opt_math::U128OptBitShift

pub impl U128OptBitShift of OptBitShift<u128, WideMulU128>;

Impl functions

shl

Fully qualified path: alexandria_math::opt_math::U128OptBitShift::shl

fn shl(x: u128, n: u8) -> u128

shr

Fully qualified path: alexandria_math::opt_math::U128OptBitShift::shr

fn shr(x: u128, n: u8) -> u128

U64OptBitShift

Fully qualified path: alexandria_math::opt_math::U64OptBitShift

pub impl U64OptBitShift of OptBitShift<u64, WideMulU64>;

Impl functions

shl

Fully qualified path: alexandria_math::opt_math::U64OptBitShift::shl

fn shl(x: u64, n: u8) -> u64

shr

Fully qualified path: alexandria_math::opt_math::U64OptBitShift::shr

fn shr(x: u64, n: u8) -> u64

U32OptBitShift

Fully qualified path: alexandria_math::opt_math::U32OptBitShift

pub impl U32OptBitShift of OptBitShift<u32, WideMulU32>;

Impl functions

shl

Fully qualified path: alexandria_math::opt_math::U32OptBitShift::shl

fn shl(x: u32, n: u8) -> u32

shr

Fully qualified path: alexandria_math::opt_math::U32OptBitShift::shr

fn shr(x: u32, n: u8) -> u32

U16OptBitShift

Fully qualified path: alexandria_math::opt_math::U16OptBitShift

pub impl U16OptBitShift of OptBitShift<u16, WideMulU16>;

Impl functions

shl

Fully qualified path: alexandria_math::opt_math::U16OptBitShift::shl

fn shl(x: u16, n: u8) -> u16

shr

Fully qualified path: alexandria_math::opt_math::U16OptBitShift::shr

fn shr(x: u16, n: u8) -> u16

U8OptBitShift

Fully qualified path: alexandria_math::opt_math::U8OptBitShift

pub impl U8OptBitShift of OptBitShift<u8, WideMulU8>;

Impl functions

shl

Fully qualified path: alexandria_math::opt_math::U8OptBitShift::shl

fn shl(x: u8, n: u8) -> u8

shr

Fully qualified path: alexandria_math::opt_math::U8OptBitShift::shr

fn shr(x: u8, n: u8) -> u8

U256OptBitRotate

Does not support 0 rotations (will panic).

Fully qualified path: alexandria_math::opt_math::U256OptBitRotate

pub impl U256OptBitRotate of OptBitRotate<u256, WideMulU256>;

Impl functions

rotl

Fully qualified path: alexandria_math::opt_math::U256OptBitRotate::rotl

fn rotl(x: u256, n: u8) -> u256

rotr

Fully qualified path: alexandria_math::opt_math::U256OptBitRotate::rotr

fn rotr(x: u256, n: u8) -> u256

U128OptBitRotate

Fully qualified path: alexandria_math::opt_math::U128OptBitRotate

pub impl U128OptBitRotate of OptBitRotate<u128, WideMulU128>;

Impl functions

rotl

Fully qualified path: alexandria_math::opt_math::U128OptBitRotate::rotl

fn rotl(x: u128, n: u8) -> u128

rotr

Fully qualified path: alexandria_math::opt_math::U128OptBitRotate::rotr

fn rotr(x: u128, n: u8) -> u128

U64OptBitRotate

Fully qualified path: alexandria_math::opt_math::U64OptBitRotate

pub impl U64OptBitRotate of OptBitRotate<u64, WideMulU64>;

Impl functions

rotl

Fully qualified path: alexandria_math::opt_math::U64OptBitRotate::rotl

fn rotl(x: u64, n: u8) -> u64

rotr

Fully qualified path: alexandria_math::opt_math::U64OptBitRotate::rotr

fn rotr(x: u64, n: u8) -> u64

U32OptBitRotate

Fully qualified path: alexandria_math::opt_math::U32OptBitRotate

pub impl U32OptBitRotate of OptBitRotate<u32, WideMulU32>;

Impl functions

rotl

Fully qualified path: alexandria_math::opt_math::U32OptBitRotate::rotl

fn rotl(x: u32, n: u8) -> u32

rotr

Fully qualified path: alexandria_math::opt_math::U32OptBitRotate::rotr

fn rotr(x: u32, n: u8) -> u32

U16OptBitRotate

Fully qualified path: alexandria_math::opt_math::U16OptBitRotate

pub impl U16OptBitRotate of OptBitRotate<u16, WideMulU16>;

Impl functions

rotl

Fully qualified path: alexandria_math::opt_math::U16OptBitRotate::rotl

fn rotl(x: u16, n: u8) -> u16

rotr

Fully qualified path: alexandria_math::opt_math::U16OptBitRotate::rotr

fn rotr(x: u16, n: u8) -> u16

U8OptBitRotate

Fully qualified path: alexandria_math::opt_math::U8OptBitRotate

pub impl U8OptBitRotate of OptBitRotate<u8, WideMulU8>;

Impl functions

rotl

Fully qualified path: alexandria_math::opt_math::U8OptBitRotate::rotl

fn rotl(x: u8, n: u8) -> u8

rotr

Fully qualified path: alexandria_math::opt_math::U8OptBitRotate::rotr

fn rotr(x: u8, n: u8) -> u8

perfect_number

Perfect Number.

Fully qualified path: alexandria_math::perfect_number

Free functions

is_perfect_numberAlgorithm to determine if a number is a perfect number…
perfect_numbersAlgorithm to determine all the perfect numbers up to a maximum value…

Free functions

Free functions

is_perfect_numberAlgorithm to determine if a number is a perfect number…
perfect_numbersAlgorithm to determine all the perfect numbers up to a maximum value…

is_perfect_number

Algorithm to determine if a number is a perfect number

Arguments

  • num - The number to be checked.

Returns

  • bool - True if num is a perfect number, false otherwise.

Fully qualified path: alexandria_math::perfect_number::is_perfect_number

pub fn is_perfect_number(num: u128) -> bool

perfect_numbers

Algorithm to determine all the perfect numbers up to a maximum value

Arguments

  • max - The maximum value to check for perfect numbers.

Returns

  • Array - An array of perfect numbers up to the max value.

Fully qualified path: alexandria_math::perfect_number::perfect_numbers

pub fn perfect_numbers(max: u128) -> Array<u128>

pow_macro

Fully qualified path: alexandria_math::pow_macro

Macro declarations

pow_inlineUsage: pow_inline!(base, exponent)

Macro declarations

Macro declarations

pow_inlineUsage: pow_inline!(base, exponent)

pow_inline

Usage: pow_inline!(base, exponent)

Fully qualified path: alexandria_math::pow_macro::pow_inline

macro pow_inline {
    ($base:expr, $exp:expr) => { ... };
}

ripemd160

RIPEMD-160 Hash Function Implementation

This module provides a complete implementation of the RIPEMD-160 cryptographic hash function as specified in the RIPEMD-160 standard. RIPEMD-160 produces a 160-bit (20-byte) hash digest.

Based on the original Cairo implementation by j1mbo64: https://github.com/j1mbo64/ripemd160_cairo

Algorithm Overview

RIPEMD-160 processes input data in 512-bit (64-byte) blocks through:

  • 5 rounds of 16 operations each on the left side
  • 5 rounds of 16 operations each on the right side
  • Final combination of left and right results

Fully qualified path: alexandria_math::ripemd160

Free functions

ripemd160_context_as_bytes
ripemd160_context_as_array
ripemd160_context_as_u256
ripemd160_hashRIPEMD-160 hash function entrypoint Computes the RIPEMD-160 hash of the input data. This implementation is based on the original work by j1mbo64: https://github.com/j1mbo64/ripemd160_cairo…

Structs

Impls

Free functions

Free functions

ripemd160_context_as_bytes
ripemd160_context_as_array
ripemd160_context_as_u256
ripemd160_hashRIPEMD-160 hash function entrypoint Computes the RIPEMD-160 hash of the input data. This implementation is based on the original work by j1mbo64: https://github.com/j1mbo64/ripemd160_cairo…

ripemd160_context_as_bytes

Fully qualified path: alexandria_math::ripemd160::ripemd160_context_as_bytes

pub fn ripemd160_context_as_bytes(ctx: @RIPEMD160Context) -> ByteArray

ripemd160_context_as_array

Fully qualified path: alexandria_math::ripemd160::ripemd160_context_as_array

pub fn ripemd160_context_as_array(ctx: @RIPEMD160Context) -> Array<u32>

ripemd160_context_as_u256

Fully qualified path: alexandria_math::ripemd160::ripemd160_context_as_u256

pub fn ripemd160_context_as_u256(ctx: @RIPEMD160Context) -> u256

ripemd160_hash

RIPEMD-160 hash function entrypoint

Computes the RIPEMD-160 hash of the input data. This implementation is based on the original work by j1mbo64: https://github.com/j1mbo64/ripemd160_cairo

Arguments

  • data - Input data to hash

Returns

  • RIPEMD160Context - Context containing the computed hash

Example

let data: ByteArray = "Hello, World!";
let hash_ctx = ripemd160_hash(@data);
let hash_u256 = ripemd160_context_as_u256(@hash_ctx);

Fully qualified path: alexandria_math::ripemd160::ripemd160_hash

pub fn ripemd160_hash(data: @ByteArray) -> RIPEMD160Context

Structs

Structs

RIPEMD160Context

Fully qualified path: alexandria_math::ripemd160::RIPEMD160Context

[derive(Drop, Clone, Copy)]
pub struct RIPEMD160Context { /* private fields */ }

Impls

Impls

RIPEMD160ContextIntoU256

Fully qualified path: alexandria_math::ripemd160::RIPEMD160ContextIntoU256

pub impl RIPEMD160ContextIntoU256 of Into<RIPEMD160Context, u256>;

Impl functions

into

Fully qualified path: alexandria_math::ripemd160::RIPEMD160ContextIntoU256::into

fn into(self: RIPEMD160Context) -> u256

RIPEMD160ContextIntoBytes

Fully qualified path: alexandria_math::ripemd160::RIPEMD160ContextIntoBytes

pub impl RIPEMD160ContextIntoBytes of Into<RIPEMD160Context, ByteArray>;

Impl functions

into

Fully qualified path: alexandria_math::ripemd160::RIPEMD160ContextIntoBytes::into

fn into(self: RIPEMD160Context) -> ByteArray

RIPEMD160ContextIntoArray

Fully qualified path: alexandria_math::ripemd160::RIPEMD160ContextIntoArray

pub impl RIPEMD160ContextIntoArray of Into<RIPEMD160Context, Array<u32>>;

Impl functions

into

Fully qualified path: alexandria_math::ripemd160::RIPEMD160ContextIntoArray::into

fn into(self: RIPEMD160Context) -> Array<u32>

sha256

Fully qualified path: alexandria_math::sha256

Free functions

sha256Computes SHA-256 hash of the input data This function implements the SHA-256 cryptographic hash algorithm following RFC 6234….

Free functions

Free functions

sha256Computes SHA-256 hash of the input data This function implements the SHA-256 cryptographic hash algorithm following RFC 6234….

sha256

Computes SHA-256 hash of the input data

This function implements the SHA-256 cryptographic hash algorithm following RFC 6234. It processes the input by padding it appropriately, then processing in 512-bit blocks through 64 rounds of operations using the SHA-256 compression function.

Arguments

  • data - Array of bytes to be hashed

Returns

  • Array<u8> - The 32-byte SHA-256 hash digest as an array of bytes

Fully qualified path: alexandria_math::sha256::sha256

pub fn sha256(mut data: Array<u8>) -> Array<u8>

sha512

Fully qualified path: alexandria_math::sha512

Constants

Free functions

fpowCalculates base raised to the power using fast exponentiation…
sha512Computes SHA-512 hash of the input data This function implements the SHA-512 cryptographic hash algorithm following RFC 6234….

Structs

Word64

Traits

WordOperationsTrait defining bitwise operations for word types used in cryptographic algorithms.

Impls

Constants

Constants

SHA512_LEN

Fully qualified path: alexandria_math::sha512::SHA512_LEN

pub const SHA512_LEN: u32 = 64;

U64_BIT_NUM

Fully qualified path: alexandria_math::sha512::U64_BIT_NUM

pub const U64_BIT_NUM: u64 = 64;

TWO_POW_56

Fully qualified path: alexandria_math::sha512::TWO_POW_56

pub const TWO_POW_56: u64 = 72057594037927936;

TWO_POW_48

Fully qualified path: alexandria_math::sha512::TWO_POW_48

pub const TWO_POW_48: u64 = 281474976710656;

TWO_POW_40

Fully qualified path: alexandria_math::sha512::TWO_POW_40

pub const TWO_POW_40: u64 = 1099511627776;

TWO_POW_32

Fully qualified path: alexandria_math::sha512::TWO_POW_32

pub const TWO_POW_32: u64 = 4294967296;

TWO_POW_24

Fully qualified path: alexandria_math::sha512::TWO_POW_24

pub const TWO_POW_24: u64 = 16777216;

TWO_POW_16

Fully qualified path: alexandria_math::sha512::TWO_POW_16

pub const TWO_POW_16: u64 = 65536;

TWO_POW_8

Fully qualified path: alexandria_math::sha512::TWO_POW_8

pub const TWO_POW_8: u64 = 256;

TWO_POW_4

Fully qualified path: alexandria_math::sha512::TWO_POW_4

pub const TWO_POW_4: u64 = 16;

TWO_POW_2

Fully qualified path: alexandria_math::sha512::TWO_POW_2

pub const TWO_POW_2: u64 = 4;

TWO_POW_1

Fully qualified path: alexandria_math::sha512::TWO_POW_1

pub const TWO_POW_1: u64 = 2;

TWO_POW_0

Fully qualified path: alexandria_math::sha512::TWO_POW_0

pub const TWO_POW_0: u64 = 1;

MAX_U8

Fully qualified path: alexandria_math::sha512::MAX_U8

pub const MAX_U8: u64 = 255;

MAX_U64

Fully qualified path: alexandria_math::sha512::MAX_U64

pub const MAX_U64: u128 = 18446744073709551615;

Free functions

Free functions

fpowCalculates base raised to the power using fast exponentiation…
sha512Computes SHA-512 hash of the input data This function implements the SHA-512 cryptographic hash algorithm following RFC 6234….

fpow

Calculates base raised to the power using fast exponentiation

Arguments

  • base - The base value (must be non-zero)
  • power - The exponent

Returns

  • u128 - The result of base^power

Fully qualified path: alexandria_math::sha512::fpow

pub fn fpow(mut base: u128, mut power: u128) -> u128

sha512

Computes SHA-512 hash of the input data

This function implements the SHA-512 cryptographic hash algorithm following RFC 6234. It processes the input by padding it appropriately, then processing in 1024-bit blocks through 80 rounds of operations using the SHA-512 compression function.

Arguments

  • data - Array of bytes to be hashed

Returns

  • Array<u8> - The 64-byte SHA-512 hash digest as an array of bytes

Fully qualified path: alexandria_math::sha512::sha512

pub fn sha512(mut data: Array<u8>) -> Array<u8>

Structs

Structs

Word64

Word64

Fully qualified path: alexandria_math::sha512::Word64

[derive(Drop, Copy)]
pub struct Word64 {
    pub data: u64,
}

Members

data

Fully qualified path: alexandria_math::sha512::Word64::data

pub data: u64

Traits

Traits

WordOperationsTrait defining bitwise operations for word types used in cryptographic algorithms.

WordOperations

Trait defining bitwise operations for word types used in cryptographic algorithms.

Fully qualified path: alexandria_math::sha512::WordOperations

pub trait WordOperations<T>

Trait functions

shr

Performs logical right shift operation.

Arguments

  • self - The value to shift
  • n - Number of positions to shift right

Returns

  • T - The shifted value

Fully qualified path: alexandria_math::sha512::WordOperations::shr

fn shr(self: T, n: u64) -> T

shl

Performs logical left shift operation.

Arguments

  • self - The value to shift
  • n - Number of positions to shift left

Returns

  • T - The shifted value

Fully qualified path: alexandria_math::sha512::WordOperations::shl

fn shl(self: T, n: u64) -> T

rotr_precomputed

Performs rotate right with precomputed power values for efficiency.

Arguments

  • self - The value to rotate
  • two_pow_n - Precomputed value of 2^n
  • two_pow_64_n - Precomputed value of 2^(64-n)

Returns

  • T - The rotated value

Fully qualified path: alexandria_math::sha512::WordOperations::rotr_precomputed

fn rotr_precomputed(self: T, two_pow_n: u64, two_pow_64_n: u64) -> T

rotl

Performs rotate left operation.

Arguments

  • self - The value to rotate
  • n - Number of positions to rotate left

Returns

  • T - The rotated value

Fully qualified path: alexandria_math::sha512::WordOperations::rotl

fn rotl(self: T, n: u64) -> T

Impls

Impls

Word64WordOperations

Fully qualified path: alexandria_math::sha512::Word64WordOperations

pub impl Word64WordOperations of WordOperations<Word64>;

Impl functions

shr

Fully qualified path: alexandria_math::sha512::Word64WordOperations::shr

fn shr(self: Word64, n: u64) -> Word64

shl

Fully qualified path: alexandria_math::sha512::Word64WordOperations::shl

fn shl(self: Word64, n: u64) -> Word64

rotr_precomputed

Fully qualified path: alexandria_math::sha512::Word64WordOperations::rotr_precomputed

fn rotr_precomputed(self: Word64, two_pow_n: u64, two_pow_64_n: u64) -> Word64

rotl

Fully qualified path: alexandria_math::sha512::Word64WordOperations::rotl

fn rotl(self: Word64, n: u64) -> Word64

trigonometry

Fully qualified path: alexandria_math::trigonometry

Free functions

Free functions

Free functions

fast_sin_inner

Fully qualified path: alexandria_math::trigonometry::fast_sin_inner

pub fn fast_sin_inner(x: u64) -> (bool, u64)

fast_sin

Fully qualified path: alexandria_math::trigonometry::fast_sin

pub fn fast_sin(x: i64) -> i64

fast_cos

Fully qualified path: alexandria_math::trigonometry::fast_cos

pub fn fast_cos(x: i64) -> i64

fast_tan

Fully qualified path: alexandria_math::trigonometry::fast_tan

pub fn fast_tan(x: i64) -> i64

u512_arithmetics

Fully qualified path: alexandria_math::u512_arithmetics

Free functions

u512_addAdds two u512 values with overflow panic…
u512_subSubtracts two u512 values with overflow panic…

Structs

u256X2

Impls

Free functions

Free functions

u512_addAdds two u512 values with overflow panic…
u512_subSubtracts two u512 values with overflow panic…

u512_add

Adds two u512 values with overflow panic

Arguments

  • lhs - Left operand (u512)
  • rhs - Right operand (u512)

Returns

  • u512 - Sum of lhs and rhs

Panics

  • Panics if the addition would overflow u512 bounds

Fully qualified path: alexandria_math::u512_arithmetics::u512_add

pub fn u512_add(lhs: u512, rhs: u512) -> u512

u512_sub

Subtracts two u512 values with overflow panic

Arguments

  • lhs - Left operand (u512)
  • rhs - Right operand (u512)

Returns

  • u512 - Difference of lhs and rhs

Panics

  • Panics if the subtraction would underflow (result < 0)

Fully qualified path: alexandria_math::u512_arithmetics::u512_sub

pub fn u512_sub(lhs: u512, rhs: u512) -> u512

Structs

Structs

u256X2

u256X2

Fully qualified path: alexandria_math::u512_arithmetics::u256X2

[derive(Copy, Drop, Hash, PartialEq, Serde)]
pub struct u256X2 { /* private fields */ }

Impls

Impls

U512Intou256X2

Fully qualified path: alexandria_math::u512_arithmetics::U512Intou256X2

pub impl U512Intou256X2 of Into<u512, u256X2>;

Impl functions

into

Fully qualified path: alexandria_math::u512_arithmetics::U512Intou256X2::into

fn into(self: u512) -> u256X2

wad_ray_math

Provides functions to perform calculations with Wad and Ray units @dev Provides mul and div function for wads (decimal numbers with 18 digits of precision) and rays (decimal numbers with 27 digits of precision) Operations are rounded. If a value is >=.5, will be rounded up, otherwise rounded down. https://github.com/aave/aave-v3-core/blob/master/contracts/protocol/libraries/math/WadRayMath.sol

Fully qualified path: alexandria_math::wad_ray_math

Free functions

wadReturn the wad value…
rayReturn the ray value…
half_wadReturn the half wad value…
half_rayReturn the half ray value…
wad_mulMultiplies two wad, rounding half up to the nearest wad…
wad_divDivides two wad, rounding half up to the nearest wad…
ray_mulMultiplies two ray, rounding half up to the nearest ray…
ray_divDivides two ray, rounding half up to the nearest ray…
ray_to_wadCasts ray down to wad…
wad_to_rayConverts wad up to ray…

Free functions

Free functions

wadReturn the wad value…
rayReturn the ray value…
half_wadReturn the half wad value…
half_rayReturn the half ray value…
wad_mulMultiplies two wad, rounding half up to the nearest wad…
wad_divDivides two wad, rounding half up to the nearest wad…
ray_mulMultiplies two ray, rounding half up to the nearest ray…
ray_divDivides two ray, rounding half up to the nearest ray…
ray_to_wadCasts ray down to wad…
wad_to_rayConverts wad up to ray…

wad

Return the wad value

Returns

  • u256 - The value

Fully qualified path: alexandria_math::wad_ray_math::wad

pub fn wad() -> u256

ray

Return the ray value

Returns

  • u256 - The value

Fully qualified path: alexandria_math::wad_ray_math::ray

pub fn ray() -> u256

half_wad

Return the half wad value

Returns

  • u256 - The value

Fully qualified path: alexandria_math::wad_ray_math::half_wad

pub fn half_wad() -> u256

half_ray

Return the half ray value

Returns

  • u256 - The value

Fully qualified path: alexandria_math::wad_ray_math::half_ray

pub fn half_ray() -> u256

wad_mul

Multiplies two wad, rounding half up to the nearest wad

Arguments

  • a Wad
  • b Wad

Returns

  • a*b, in wad

Fully qualified path: alexandria_math::wad_ray_math::wad_mul

pub fn wad_mul(a: u256, b: u256) -> u256

wad_div

Divides two wad, rounding half up to the nearest wad

Arguments

  • a Wad
  • b Wad

Returns

  • a/b, in wad

Fully qualified path: alexandria_math::wad_ray_math::wad_div

pub fn wad_div(a: u256, b: u256) -> u256

ray_mul

Multiplies two ray, rounding half up to the nearest ray

Arguments

  • a Ray
  • b Ray

Returns

  • a raymul b

Fully qualified path: alexandria_math::wad_ray_math::ray_mul

pub fn ray_mul(a: u256, b: u256) -> u256

ray_div

Divides two ray, rounding half up to the nearest ray

Arguments

  • a Ray
  • b Ray

Returns

  • a raydiv b

Fully qualified path: alexandria_math::wad_ray_math::ray_div

pub fn ray_div(a: u256, b: u256) -> u256

ray_to_wad

Casts ray down to wad

Arguments

  • a Ray

Returns

  • a converted to wad, rounded half up to the nearest wad

Fully qualified path: alexandria_math::wad_ray_math::ray_to_wad

pub fn ray_to_wad(a: u256) -> u256

wad_to_ray

Converts wad up to ray

Arguments

  • a Wad

Returns

  • a converted to ray

Fully qualified path: alexandria_math::wad_ray_math::wad_to_ray

pub fn wad_to_ray(a: u256) -> u256

zellers_congruence

Zeller’s congruence is an algorithm devised by Christian Zeller in the 19th century to calculate the day of the week for any Julian or Gregorian calendar date. It can be considered to be based on the conversion between Julian day and the calendar date.

Fully qualified path: alexandria_math::zellers_congruence

Free functions

day_of_weekCompute the day of the week for the given Gregorian date. The returned value is an integer in the range 0 to 6, where 0 is Saturday, 1 is Sunday, 2 is Monday, and so on….
check_input_parametersCheck the input parameters for the day_of_week function….

Free functions

Free functions

day_of_weekCompute the day of the week for the given Gregorian date. The returned value is an integer in the range 0 to 6, where 0 is Saturday, 1 is Sunday, 2 is Monday, and so on….
check_input_parametersCheck the input parameters for the day_of_week function….

day_of_week

Compute the day of the week for the given Gregorian date. The returned value is an integer in the range 0 to 6, where 0 is Saturday, 1 is Sunday, 2 is Monday, and so on.

Arguments

  • date - The date of the month
  • month - The month of the year
  • year - The year

Returns

  • Option::None - If the input parameters are invalid
  • Option::Some(day_of_week) - The day of the week

Examples

use alexandria::math::zellers_congruence::day_of_week;
let day_of_week = day_of_week(1, 1, 2020);

Fully qualified path: alexandria_math::zellers_congruence::day_of_week

pub fn day_of_week(mut date: u128, mut month: u128, mut year: u128) -> Option<u128>

check_input_parameters

Check the input parameters for the day_of_week function.

Arguments

  • date - The date of the month
  • month - The month of the year
  • year - The year

Returns

  • true - If the input parameters are valid
  • false - If the input parameters are invalid

Fully qualified path: alexandria_math::zellers_congruence::check_input_parameters

pub fn check_input_parameters(date: u128, month: u128, year: u128) -> bool

Free functions

Free functions

powRaise a number to a power. O(log n) time complexity….
count_digits_of_baseFunction to count the number of digits in a number….

pow

Raise a number to a power. O(log n) time complexity.

Arguments

  • base - The number to raise.
  • exp - The exponent.

Returns

  • T - The result of base raised to the power of exp.

Fully qualified path: alexandria_math::pow

pub fn pow<T, +Sub<T>, +Mul<T>, +Div<T>, +Rem<T>, +PartialEq<T>, +Into<u8, T>, +Drop<T>, +Copy<T>>(
    base: T, exp: T,
) -> T

count_digits_of_base

Function to count the number of digits in a number.

Arguments

  • num - The number to count the digits of.
  • base - Base in which to count the digits.

Returns

  • u32 - The number of digits in num of base

Fully qualified path: alexandria_math::count_digits_of_base

pub fn count_digits_of_base(mut num: u128, base: u128) -> u32

Traits

Traits

BitShift
BitRotateRotate the bits of an unsigned integer of type T
WrappingMath

BitShift

Fully qualified path: alexandria_math::BitShift

pub trait BitShift<
    T, +Sub<T>, +Mul<T>, +Div<T>, +Rem<T>, +PartialEq<T>, +Into<u8, T>, +Drop<T>, +Copy<T>,
>

Trait functions

shl

Fully qualified path: alexandria_math::BitShift::shl

fn shl(x: T, n: T) -> T

shr

Fully qualified path: alexandria_math::BitShift::shr

fn shr(x: T, n: T) -> T

BitRotate

Rotate the bits of an unsigned integer of type T

Fully qualified path: alexandria_math::BitRotate

pub trait BitRotate<T>

Trait functions

rotate_left

Take the bits of an unsigned integer and rotate in the left direction

Arguments

  • x - rotate its bit representation in the leftward direction
  • n - number of steps to rotate

Returns

  • T - the result of rotating the bits of number x left, n number of steps

Fully qualified path: alexandria_math::BitRotate::rotate_left

fn rotate_left(x: T, n: T) -> T

rotate_right

Take the bits of an unsigned integer and rotate in the right direction

Arguments

  • x - rotate its bit representation in the rightward direction
  • n - number of steps to rotate

Returns

  • T - the result of rotating the bits of number x right, n number of steps

Fully qualified path: alexandria_math::BitRotate::rotate_right

fn rotate_right(x: T, n: T) -> T

WrappingMath

Fully qualified path: alexandria_math::WrappingMath

pub trait WrappingMath<T>

Trait functions

wrapping_add

Fully qualified path: alexandria_math::WrappingMath::wrapping_add

fn wrapping_add(self: T, rhs: T) -> T

wrapping_sub

Fully qualified path: alexandria_math::WrappingMath::wrapping_sub

fn wrapping_sub(self: T, rhs: T) -> T

wrapping_mul

Fully qualified path: alexandria_math::WrappingMath::wrapping_mul

fn wrapping_mul(self: T, rhs: T) -> T

Impls

Impls

U8BitShift

Fully qualified path: alexandria_math::U8BitShift

pub impl U8BitShift of BitShift<
    u8,
    U8Sub,
    U8Mul,
    DivImpl<u8, U8DivRem, U8TryIntoNonZero, u8Drop>,
    RemImpl<u8, U8DivRem, U8TryIntoNonZero, u8Drop>,
    U8PartialEq,
    TIntoT<u8>,
    u8Drop,
    u8Copy,
>;

Impl functions

shl

Fully qualified path: alexandria_math::U8BitShift::shl

fn shl(x: u8, n: u8) -> u8

U16BitShift

Fully qualified path: alexandria_math::U16BitShift

pub impl U16BitShift of BitShift<
    u16,
    U16Sub,
    U16Mul,
    DivImpl<u16, U16DivRem, U16TryIntoNonZero, u16Drop>,
    RemImpl<u16, U16DivRem, U16TryIntoNonZero, u16Drop>,
    U16PartialEq,
    UpcastableInto<u8, u16, UpcastableU8U16>,
    u16Drop,
    u16Copy,
>;

Impl functions

shl

Fully qualified path: alexandria_math::U16BitShift::shl

fn shl(x: u16, n: u16) -> u16

U32BitShift

Fully qualified path: alexandria_math::U32BitShift

pub impl U32BitShift of BitShift<
    u32,
    U32Sub,
    U32Mul,
    DivImpl<u32, U32DivRem, U32TryIntoNonZero, u32Drop>,
    RemImpl<u32, U32DivRem, U32TryIntoNonZero, u32Drop>,
    U32PartialEq,
    UpcastableInto<u8, u32, UpcastableU8U32>,
    u32Drop,
    u32Copy,
>;

Impl functions

shl

Fully qualified path: alexandria_math::U32BitShift::shl

fn shl(x: u32, n: u32) -> u32

U64BitShift

Fully qualified path: alexandria_math::U64BitShift

pub impl U64BitShift of BitShift<
    u64,
    U64Sub,
    U64Mul,
    DivImpl<u64, U64DivRem, U64TryIntoNonZero, u64Drop>,
    RemImpl<u64, U64DivRem, U64TryIntoNonZero, u64Drop>,
    U64PartialEq,
    UpcastableInto<u8, u64, UpcastableU8U64>,
    u64Drop,
    u64Copy,
>;

Impl functions

shl

Fully qualified path: alexandria_math::U64BitShift::shl

fn shl(x: u64, n: u64) -> u64

U128BitShift

Fully qualified path: alexandria_math::U128BitShift

pub impl U128BitShift of BitShift<
    u128,
    U128Sub,
    U128Mul,
    DivImpl<u128, U128DivRem, U128TryIntoNonZero, u128Drop>,
    RemImpl<u128, U128DivRem, U128TryIntoNonZero, u128Drop>,
    U128PartialEq,
    UpcastableInto<u8, u128, UpcastableU8U128>,
    u128Drop,
    u128Copy,
>;

Impl functions

shl

Fully qualified path: alexandria_math::U128BitShift::shl

fn shl(x: u128, n: u128) -> u128

U256BitShift

Fully qualified path: alexandria_math::U256BitShift

pub impl U256BitShift of BitShift<
    u256,
    U256Sub,
    U256Mul,
    DivImpl<u256, U256DivRem, U256TryIntoNonZero, u256Drop>,
    RemImpl<u256, U256DivRem, U256TryIntoNonZero, u256Drop>,
    u256PartialEq,
    U8IntoU256,
    u256Drop,
    u256Copy,
>;

Impl functions

shl

Fully qualified path: alexandria_math::U256BitShift::shl

fn shl(x: u256, n: u256) -> u256

U8BitRotate

Fully qualified path: alexandria_math::U8BitRotate

pub impl U8BitRotate of BitRotate<u8>;

Impl functions

rotate_left

Fully qualified path: alexandria_math::U8BitRotate::rotate_left

fn rotate_left(x: u8, n: u8) -> u8

rotate_right

Fully qualified path: alexandria_math::U8BitRotate::rotate_right

fn rotate_right(x: u8, n: u8) -> u8

U16BitRotate

Fully qualified path: alexandria_math::U16BitRotate

pub impl U16BitRotate of BitRotate<u16>;

Impl functions

rotate_left

Fully qualified path: alexandria_math::U16BitRotate::rotate_left

fn rotate_left(x: u16, n: u16) -> u16

rotate_right

Fully qualified path: alexandria_math::U16BitRotate::rotate_right

fn rotate_right(x: u16, n: u16) -> u16

U32BitRotate

Fully qualified path: alexandria_math::U32BitRotate

pub impl U32BitRotate of BitRotate<u32>;

Impl functions

rotate_left

Fully qualified path: alexandria_math::U32BitRotate::rotate_left

fn rotate_left(x: u32, n: u32) -> u32

rotate_right

Fully qualified path: alexandria_math::U32BitRotate::rotate_right

fn rotate_right(x: u32, n: u32) -> u32

U64BitRotate

Fully qualified path: alexandria_math::U64BitRotate

pub impl U64BitRotate of BitRotate<u64>;

Impl functions

rotate_left

Fully qualified path: alexandria_math::U64BitRotate::rotate_left

fn rotate_left(x: u64, n: u64) -> u64

rotate_right

Fully qualified path: alexandria_math::U64BitRotate::rotate_right

fn rotate_right(x: u64, n: u64) -> u64

U128BitRotate

Fully qualified path: alexandria_math::U128BitRotate

pub impl U128BitRotate of BitRotate<u128>;

Impl functions

rotate_left

Fully qualified path: alexandria_math::U128BitRotate::rotate_left

fn rotate_left(x: u128, n: u128) -> u128

rotate_right

Fully qualified path: alexandria_math::U128BitRotate::rotate_right

fn rotate_right(x: u128, n: u128) -> u128

U256BitRotate

Fully qualified path: alexandria_math::U256BitRotate

pub impl U256BitRotate of BitRotate<u256>;

Impl functions

rotate_left

Fully qualified path: alexandria_math::U256BitRotate::rotate_left

fn rotate_left(x: u256, n: u256) -> u256

rotate_right

Fully qualified path: alexandria_math::U256BitRotate::rotate_right

fn rotate_right(x: u256, n: u256) -> u256

core

Main entrypoint for the Cairo core library.

Fully qualified path: core

Modules

num

Modules

Modules

num

num

Fully qualified path: core::num

Modules

traits

Modules

Modules

traits

traits

Fully qualified path: core::num::traits

Modules

boundedDefines minimum and maximum values for numeric types.
ops

Modules

Modules

boundedDefines minimum and maximum values for numeric types.
ops

bounded

Defines minimum and maximum values for numeric types.

Fully qualified path: core::num::traits::bounded

Traits

BoundedA trait defining minimum and maximum bounds for numeric types. This trait only supports types that can have constant values.

Traits

Traits

BoundedA trait defining minimum and maximum bounds for numeric types. This trait only supports types that can have constant values.

Bounded

A trait defining minimum and maximum bounds for numeric types.

This trait only supports types that can have constant values.

Fully qualified path: core::num::traits::bounded::Bounded

pub trait Bounded<T>

Trait constants

MIN

Returns the minimum value for type T.

Examples

use core::num::traits::Bounded;

let min = Bounded::<u8>::MIN;
assert!(min == 0);

Fully qualified path: core::num::traits::bounded::Bounded::MIN

const MIN: T;

MAX

Returns the maximum value for type T.

Examples

use core::num::traits::Bounded;

let max = Bounded::<u8>::MAX;
assert!(max == 255);

Fully qualified path: core::num::traits::bounded::Bounded::MAX

const MAX: T;

ops

Fully qualified path: core::num::traits::ops

Modules

overflowingArithmetic operations with overflow detection. This module provides traits for performing arithmetic operations that explicitly track potential numeric overflow conditions.
widemulTrait for performing multiplication that results in a wider type. This module provides the WideMul trait which enables multiplication operations…
wrappingArithmetic operations with overflow and underflow wrapping. This module provides traits for performing arithmetic operations that wrap around at the…

Modules

Modules

overflowingArithmetic operations with overflow detection. This module provides traits for performing arithmetic operations that explicitly track potential numeric overflow conditions.
widemulTrait for performing multiplication that results in a wider type. This module provides the WideMul trait which enables multiplication operations…
wrappingArithmetic operations with overflow and underflow wrapping. This module provides traits for performing arithmetic operations that wrap around at the…

overflowing

Arithmetic operations with overflow detection.

This module provides traits for performing arithmetic operations that explicitly track potential numeric overflow conditions.

Fully qualified path: core::num::traits::ops::overflowing

Traits

OverflowingMulPerforms multiplication with a flag for overflow….

Traits

Traits

OverflowingMulPerforms multiplication with a flag for overflow….

OverflowingMul

Performs multiplication with a flag for overflow.

Examples

use core::num::traits::OverflowingMul;

let (result, is_overflow) = 1_u8.overflowing_mul(2_u8);
assert!(result == 2);
assert!(!is_overflow);

Fully qualified path: core::num::traits::ops::overflowing::OverflowingMul

pub trait OverflowingMul<T>

Trait functions

overflowing_mul

Returns a tuple of the product along with a boolean indicating whether an arithmetic overflow would occur. If an overflow would have occurred then the wrapped value is returned.

Fully qualified path: core::num::traits::ops::overflowing::OverflowingMul::overflowing_mul

fn overflowing_mul(self: T, v: T) -> (T, bool)

widemul

Trait for performing multiplication that results in a wider type.

This module provides the WideMul trait which enables multiplication operations that return a result type with double the bit width of the input types. This is particularly useful when you need to perform multiplication without worrying about overflow, as the result type can hold the full range of possible values.

Examples

use core::num::traits::WideMul;

// Multiplying two `u8` values to get a `u16` result
let a: u8 = 200;
let b: u8 = 100;
let result: u16 = a.wide_mul(b);
assert!(result == 20000);

// Multiplying two `u128` values to get a `u256` result
let x: u128 = 0xffffffffffffffffffffffffffffffff; // max u128
let y: u128 = 2;
let wide_result = x.wide_mul(y); // No overflow occurs
assert!(wide_result == 0x01fffffffffffffffffffffffffffffffe);

Available Implementations

The trait is implemented for the following type pairs:

  • i8i16
  • i16i32
  • i32i64
  • i64i128
  • u8u16
  • u16u32
  • u32u64
  • u64u128
  • u128u256
  • u256u512

Fully qualified path: core::num::traits::ops::widemul

Traits

WideMulA trait for types that can be multiplied together to produce a wider type. This trait enables multiplication operations where the result type has double…

Traits

Traits

WideMulA trait for types that can be multiplied together to produce a wider type. This trait enables multiplication operations where the result type has double…

WideMul

A trait for types that can be multiplied together to produce a wider type.

This trait enables multiplication operations where the result type has double the bit width of the input types, preventing overflow in cases where the result would exceed the input type’s maximum value.

Examples

use core::num::traits::WideMul;

let a: u8 = 255; // maximum value for u8
let b: u8 = 255;
let result: u16 = a.wide_mul(b);
assert!(result == 65025);

Fully qualified path: core::num::traits::ops::widemul::WideMul

pub trait WideMul<Lhs, Rhs>

Trait functions

wide_mul

Multiply two values together, producing a wider type.

Fully qualified path: core::num::traits::ops::widemul::WideMul::wide_mul

fn wide_mul(self: Lhs, other: Rhs) -> WideMul<Lhs, Rhs>Target

Trait types

Target

The type of the result of the multiplication.

Fully qualified path: core::num::traits::ops::widemul::WideMul::Target

type Target;

wrapping

Arithmetic operations with overflow and underflow wrapping.

This module provides traits for performing arithmetic operations that wrap around at the boundary of the type in case of overflow or underflow. This is particularly useful when you want to:

  • Perform arithmetic operations without panicking on overflow/underflow
  • Implement modular arithmetic
  • Handle cases where overflow is expected and desired

Examples

use core::num::traits::{WrappingAdd, WrappingSub, WrappingMul};

// Addition wrapping
let a: u8 = 255;
assert!(a.wrapping_add(1) == 0);

// Subtraction wrapping
let b: u8 = 0;
assert!(b.wrapping_sub(1) == 255);

// Multiplication wrapping
let c: u8 = 200;
assert!(c.wrapping_mul(2) == 144); // (200 * 2) % 256 = 144

Fully qualified path: core::num::traits::ops::wrapping

Traits

WrappingAddPerforms addition that wraps around on overflow….
WrappingMulPerforms multiplication that wraps around on overflow….
WrappingSubPerforms subtraction that wraps around on overflow….

Traits

Traits

WrappingAddPerforms addition that wraps around on overflow….
WrappingMulPerforms multiplication that wraps around on overflow….
WrappingSubPerforms subtraction that wraps around on overflow….

WrappingAdd

Performs addition that wraps around on overflow.

Examples

use core::num::traits::WrappingAdd;

let result = 255_u8.wrapping_add(1);
assert!(result == 0);

let result = 100_u8.wrapping_add(200);
assert!(result == 44); // (100 + 200) % 256 = 44

Fully qualified path: core::num::traits::ops::wrapping::WrappingAdd

pub trait WrappingAdd<T>

Trait functions

wrapping_add

Wrapping (modular) addition. Computes self + other, wrapping around at the boundary of the type.

Fully qualified path: core::num::traits::ops::wrapping::WrappingAdd::wrapping_add

fn wrapping_add(self: T, v: T) -> T

WrappingMul

Performs multiplication that wraps around on overflow.

Examples

use core::num::traits::WrappingMul;

let result = 10_u8.wrapping_mul(30);
assert!(result == 44); // (10 * 30) % 256 = 44

let result = 200_u8.wrapping_mul(2);
assert!(result == 144); // (200 * 2) % 256 = 144

Fully qualified path: core::num::traits::ops::wrapping::WrappingMul

pub trait WrappingMul<T>

Trait functions

wrapping_mul

Wrapping (modular) multiplication. Computes self * other, wrapping around at the boundary of the type.

Fully qualified path: core::num::traits::ops::wrapping::WrappingMul::wrapping_mul

fn wrapping_mul(self: T, v: T) -> T

WrappingSub

Performs subtraction that wraps around on overflow.

Examples

use core::num::traits::WrappingSub;

let result = 0_u8.wrapping_sub(1);
assert!(result == 255);

let result = 100_u8.wrapping_sub(150);
assert!(result == 206);

Fully qualified path: core::num::traits::ops::wrapping::WrappingSub

pub trait WrappingSub<T>

Trait functions

wrapping_sub

Wrapping (modular) subtraction. Computes self - other, wrapping around at the boundary of the type.

Fully qualified path: core::num::traits::ops::wrapping::WrappingSub::wrapping_sub

fn wrapping_sub(self: T, v: T) -> T