Skip to main content

ReductionContext

Struct ReductionContext 

Source
pub struct ReductionContext {
    pub m: BigNumber,
    pub mont: Option<Montgomery>,
    /* private fields */
}
Expand description

Context for performing modular reduction operations.

Mirrors the TS SDK’s ReductionContext class. Can be constructed with an arbitrary modulus or with the string “k256” to use the secp256k1 prime.

Fields§

§m: BigNumber

The modulus used for reduction.

§mont: Option<Montgomery>

Optional Montgomery context for the modulus (available for K256).

Implementations§

Source§

impl ReductionContext

Source

pub fn new(m: BigNumber) -> Arc<Self>

Create a new ReductionContext with the given modulus.

Source

pub fn k256() -> Arc<Self>

Create a ReductionContext for the secp256k1 field prime (k256). Includes a Montgomery context for use by callers needing Montgomery form.

Source

pub fn with_prime(prime: Box<dyn MersennePrime>) -> Arc<Self>

Create a new ReductionContext with a Mersenne prime.

Source

pub fn imod(&self, a: &BigNumber) -> BigNumber

Reduce a BigNumber modulo m.

Source

pub fn convert_to(&self, num: &BigNumber) -> BigNumber

Convert a BigNumber into this reduction context (reduce mod m).

Source

pub fn convert_from(&self, num: &BigNumber) -> BigNumber

Convert a BigNumber from this reduction context (just clone).

Source

pub fn neg(&self, a: &BigNumber) -> BigNumber

Negate a in the context of modulus m.

Source

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

Add two BigNumbers mod m.

Source

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

Subtract b from a mod m.

Source

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

Multiply two BigNumbers mod m. For K256 with 4-limb operands, uses Karatsuba mul_4x4 followed by limb-level K256 reduction, avoiding all BigNumber temporary allocations.

Source

pub fn sqr(&self, a: &BigNumber) -> BigNumber

Square a BigNumber mod m. For K256 with 4-limb operands, uses sqr_4x4 followed by limb-level K256 reduction.

Source

pub fn invm(&self, a: &BigNumber) -> BigNumber

Modular inverse in context.

Source

pub fn pow(&self, a: &BigNumber, exp: &BigNumber) -> BigNumber

Modular exponentiation: a^exp mod m.

Source

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

Modular square root (Tonelli-Shanks for p % 4 == 3).

Trait Implementations§

Source§

impl Debug for ReductionContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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>,

Source§

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>,

Source§

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.