Struct modular_math::ModMath

source ·
pub struct ModMath { /* private fields */ }
Expand description

ModMath is a struct that provides modular arithmetic operations.

It operates on unsigned 256-bit integers (U256) and performs operations under a given modulus. The modulus is provided when creating a new ModMath instance and cannot be zero.

Implementations§

source§

impl ModMath

source

pub fn new(modulus: U256) -> Self

Creates a new ModMath instance with the given modulus.

§Panics

Panics if the modulus is zero.

source

pub fn modulus(&self, a: U256) -> U256

source

pub fn add(&self, a: U256, b: U256) -> U256

Adds two U256 numbers under the modulus.

source

pub fn sub(&self, a: U256, b: U256) -> U256

Subtracts the second U256 number from the first one under the modulus.

source

pub fn mul(&self, a: U256, b: U256) -> U256

Multiplies two U256 numbers under the modulus.

source

pub fn exp(&self, base: U256, exponent: U256) -> U256

Raises the base to the power of the exponent under the modulus.

source

pub fn inv(&self, a: U256) -> Option<U256>

Calculates the modular multiplicative inverse of a U256 number under the modulus.

Returns None if the inverse does not exist.

source

pub fn div(&self, a: U256, b: U256) -> U256

Divides the first U256 number by the second one under the modulus.

§Panics

Panics if the second number is zero or if its inverse does not exist under the modulus.

source

pub fn eq(&self, a: U256, b: U256) -> bool

Checks if two U256 numbers are equivalent under the modulus.

source

pub fn square(&self, a: U256) -> U256

Squares a given U256 number under modulus

source

pub fn sqrt(&self, a: U256) -> U256

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V