Struct MontyForm

Source
pub struct MontyForm<const LIMBS: usize> { /* private fields */ }
Expand description

An integer in Montgomery form represented using LIMBS limbs. The odd modulus is set at runtime.

Implementations§

Source§

impl<const LIMBS: usize> MontyForm<LIMBS>

Source

pub const fn add(&self, rhs: &Self) -> Self

Adds rhs.

Source

pub const fn double(&self) -> Self

Double self.

Source§

impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize> MontyForm<SAT_LIMBS>
where Odd<Uint<SAT_LIMBS>>: PrecomputeInverter<Inverter = SafeGcdInverter<SAT_LIMBS, UNSAT_LIMBS>, Output = Uint<SAT_LIMBS>>,

Source

pub const fn inv(&self) -> ConstCtOption<Self>

Computes self^-1 representing the multiplicative inverse of self. i.e. self * self^-1 = 1.

If the number was invertible, the second element of the tuple is the truthy value, otherwise it is the falsy value (in which case the first element’s value is unspecified).

Source

pub const fn inv_vartime(&self) -> ConstCtOption<Self>

Computes self^-1 representing the multiplicative inverse of self. i.e. self * self^-1 = 1.

If the number was invertible, the second element of the tuple is the truthy value, otherwise it is the falsy value (in which case the first element’s value is unspecified).

This version is variable-time with respect to the value of self, but constant-time with respect to self’s params.

Source§

impl<const LIMBS: usize> MontyForm<LIMBS>

Source

pub const fn lincomb_vartime(products: &[(&Self, &Self)]) -> Self

Calculate the sum of products of pairs (a, b) in products.

This method is variable time only with the value of the modulus. For a modulus with leading zeros, this method is more efficient than a naive sum of products.

This method will panic if products is empty. All terms must be associated with equivalent MontyParams.

Source§

impl<const LIMBS: usize> MontyForm<LIMBS>

Source

pub const fn mul(&self, rhs: &Self) -> Self

Multiplies by rhs.

Source

pub const fn square(&self) -> Self

Computes the (reduced) square.

Source§

impl<const LIMBS: usize> MontyForm<LIMBS>

Source

pub const fn neg(&self) -> Self

Negates the number.

Source§

impl<const LIMBS: usize> MontyForm<LIMBS>

Source

pub const fn pow<const RHS_LIMBS: usize>( &self, exponent: &Uint<RHS_LIMBS>, ) -> MontyForm<LIMBS>

Raises to the exponent power.

Source

pub const fn pow_bounded_exp<const RHS_LIMBS: usize>( &self, exponent: &Uint<RHS_LIMBS>, exponent_bits: u32, ) -> Self

Raises to the exponent power, with exponent_bits representing the number of (least significant) bits to take into account for the exponent.

NOTE: exponent_bits may be leaked in the time pattern.

Source§

impl<const LIMBS: usize> MontyForm<LIMBS>

Source

pub const fn sub(&self, rhs: &Self) -> Self

Subtracts rhs.

Source§

impl<const LIMBS: usize> MontyForm<LIMBS>

Source

pub const fn new(integer: &Uint<LIMBS>, params: MontyParams<LIMBS>) -> Self

Instantiates a new MontyForm that represents this integer mod MOD.

Source

pub const fn retrieve(&self) -> Uint<LIMBS>

Retrieves the integer currently encoded in this MontyForm, guaranteed to be reduced.

Source

pub const fn zero(params: MontyParams<LIMBS>) -> Self

Instantiates a new MontyForm that represents zero.

Source

pub const fn one(params: MontyParams<LIMBS>) -> Self

Instantiates a new MontyForm that represents 1.

Source

pub const fn params(&self) -> &MontyParams<LIMBS>

Returns the parameter struct used to initialize this object.

Source

pub const fn as_montgomery(&self) -> &Uint<LIMBS>

Access the MontyForm value in Montgomery form.

Source

pub fn as_montgomery_mut(&mut self) -> &mut Uint<LIMBS>

Mutably access the MontyForm value in Montgomery form.

Source

pub const fn from_montgomery( integer: Uint<LIMBS>, params: MontyParams<LIMBS>, ) -> Self

Create a MontyForm from a value in Montgomery form.

Source

pub const fn to_montgomery(&self) -> Uint<LIMBS>

Extract the value from the MontyForm in Montgomery form.

Source

pub const fn div_by_2(&self) -> Self

Performs division by 2, that is returns x such that x + x = self.

Trait Implementations§

Source§

impl<const LIMBS: usize> Add<&MontyForm<LIMBS>> for &MontyForm<LIMBS>

Source§

type Output = MontyForm<LIMBS>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &MontyForm<LIMBS>) -> MontyForm<LIMBS>

Performs the + operation. Read more
Source§

impl<const LIMBS: usize> Add<&MontyForm<LIMBS>> for MontyForm<LIMBS>

Source§

type Output = MontyForm<LIMBS>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &MontyForm<LIMBS>) -> MontyForm<LIMBS>

Performs the + operation. Read more
Source§

impl<const LIMBS: usize> Add<MontyForm<LIMBS>> for &MontyForm<LIMBS>

Source§

type Output = MontyForm<LIMBS>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: MontyForm<LIMBS>) -> MontyForm<LIMBS>

Performs the + operation. Read more
Source§

impl<const LIMBS: usize> Add for MontyForm<LIMBS>

Source§

type Output = MontyForm<LIMBS>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: MontyForm<LIMBS>) -> MontyForm<LIMBS>

Performs the + operation. Read more
Source§

impl<const LIMBS: usize> AddAssign<&MontyForm<LIMBS>> for MontyForm<LIMBS>

Source§

fn add_assign(&mut self, rhs: &MontyForm<LIMBS>)

Performs the += operation. Read more
Source§

impl<const LIMBS: usize> AddAssign for MontyForm<LIMBS>

Source§

fn add_assign(&mut self, rhs: MontyForm<LIMBS>)

Performs the += operation. Read more
Source§

impl<const LIMBS: usize> Clone for MontyForm<LIMBS>

Source§

fn clone(&self) -> MontyForm<LIMBS>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const LIMBS: usize> ConditionallySelectable for MontyForm<LIMBS>

Source§

fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self

Select a or b according to choice. Read more
Source§

fn conditional_assign(&mut self, other: &Self, choice: Choice)

Conditionally assign other to self, according to choice. Read more
Source§

fn conditional_swap(a: &mut Self, b: &mut Self, choice: Choice)

Conditionally swap self and other if choice == 1; otherwise, reassign both unto themselves. Read more
Source§

impl<const LIMBS: usize> ConstantTimeEq for MontyForm<LIMBS>

Source§

fn ct_eq(&self, other: &Self) -> Choice

Determine if two items are equal. Read more
Source§

fn ct_ne(&self, other: &Self) -> Choice

Determine if two items are NOT equal. Read more
Source§

impl<const LIMBS: usize> Debug for MontyForm<LIMBS>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<const LIMBS: usize, P: ConstMontyParams<LIMBS>> From<&ConstMontyForm<P, LIMBS>> for MontyForm<LIMBS>

Source§

fn from(const_monty_form: &ConstMontyForm<P, LIMBS>) -> Self

Converts to this type from the input type.
Source§

impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize> Invert for MontyForm<SAT_LIMBS>
where Odd<Uint<SAT_LIMBS>>: PrecomputeInverter<Inverter = SafeGcdInverter<SAT_LIMBS, UNSAT_LIMBS>, Output = Uint<SAT_LIMBS>>,

Source§

type Output = CtOption<MontyForm<SAT_LIMBS>>

Output of the inversion.
Source§

fn invert(&self) -> Self::Output

Computes the inverse.
Source§

fn invert_vartime(&self) -> Self::Output

Computes the inverse in variable-time.
Source§

impl<const LIMBS: usize> Monty for MontyForm<LIMBS>

Source§

type Integer = Uint<LIMBS>

The original integer type.
Source§

type Params = MontyParams<LIMBS>

The precomputed data needed for this representation.
Source§

fn new_params_vartime(modulus: Odd<Self::Integer>) -> Self::Params

Create the precomputed data for Montgomery representation of integers modulo modulus, variable time in modulus.
Source§

fn new(value: Self::Integer, params: Self::Params) -> Self

Convert the value into the representation using precomputed data.
Source§

fn zero(params: Self::Params) -> Self

Returns zero in this representation.
Source§

fn one(params: Self::Params) -> Self

Returns one in this representation.
Source§

fn params(&self) -> &Self::Params

Returns the parameter struct used to initialize this object.
Source§

fn as_montgomery(&self) -> &Self::Integer

Access the value in Montgomery form.
Source§

fn double(&self) -> Self

Performs doubling, returning self + self.
Source§

fn div_by_2(&self) -> Self

Performs division by 2, that is returns x such that x + x = self.
Source§

fn lincomb_vartime(products: &[(&Self, &Self)]) -> Self

Calculate the sum of products of pairs (a, b) in products. Read more
Source§

impl<const LIMBS: usize> Mul<&MontyForm<LIMBS>> for &MontyForm<LIMBS>

Source§

type Output = MontyForm<LIMBS>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &MontyForm<LIMBS>) -> MontyForm<LIMBS>

Performs the * operation. Read more
Source§

impl<const LIMBS: usize> Mul<&MontyForm<LIMBS>> for MontyForm<LIMBS>

Source§

type Output = MontyForm<LIMBS>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &MontyForm<LIMBS>) -> MontyForm<LIMBS>

Performs the * operation. Read more
Source§

impl<const LIMBS: usize> Mul<MontyForm<LIMBS>> for &MontyForm<LIMBS>

Source§

type Output = MontyForm<LIMBS>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: MontyForm<LIMBS>) -> MontyForm<LIMBS>

Performs the * operation. Read more
Source§

impl<const LIMBS: usize> Mul for MontyForm<LIMBS>

Source§

type Output = MontyForm<LIMBS>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: MontyForm<LIMBS>) -> MontyForm<LIMBS>

Performs the * operation. Read more
Source§

impl<const LIMBS: usize> MulAssign<&MontyForm<LIMBS>> for MontyForm<LIMBS>

Source§

fn mul_assign(&mut self, rhs: &MontyForm<LIMBS>)

Performs the *= operation. Read more
Source§

impl<const LIMBS: usize> MulAssign for MontyForm<LIMBS>

Source§

fn mul_assign(&mut self, rhs: MontyForm<LIMBS>)

Performs the *= operation. Read more
Source§

impl<const LIMBS: usize, const RHS_LIMBS: usize> MultiExponentiateBoundedExp<Uint<RHS_LIMBS>, [(MontyForm<LIMBS>, Uint<RHS_LIMBS>)]> for MontyForm<LIMBS>

Available on crate feature alloc only.
Source§

fn multi_exponentiate_bounded_exp( bases_and_exponents: &[(Self, Uint<RHS_LIMBS>)], exponent_bits: u32, ) -> Self

Calculates x1 ^ k1 * ... * xn ^ kn.
Source§

impl<const N: usize, const LIMBS: usize, const RHS_LIMBS: usize> MultiExponentiateBoundedExp<Uint<RHS_LIMBS>, [(MontyForm<LIMBS>, Uint<RHS_LIMBS>); N]> for MontyForm<LIMBS>

Source§

fn multi_exponentiate_bounded_exp( bases_and_exponents: &[(Self, Uint<RHS_LIMBS>); N], exponent_bits: u32, ) -> Self

Calculates x1 ^ k1 * ... * xn ^ kn.
Source§

impl<const LIMBS: usize> Neg for &MontyForm<LIMBS>

Source§

type Output = MontyForm<LIMBS>

The resulting type after applying the - operator.
Source§

fn neg(self) -> MontyForm<LIMBS>

Performs the unary - operation. Read more
Source§

impl<const LIMBS: usize> Neg for MontyForm<LIMBS>

Source§

type Output = MontyForm<LIMBS>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl<const LIMBS: usize> PartialEq for MontyForm<LIMBS>

Source§

fn eq(&self, other: &MontyForm<LIMBS>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const LIMBS: usize, const RHS_LIMBS: usize> PowBoundedExp<Uint<RHS_LIMBS>> for MontyForm<LIMBS>

Source§

fn pow_bounded_exp( &self, exponent: &Uint<RHS_LIMBS>, exponent_bits: u32, ) -> Self

Raises to the exponent power, with exponent_bits representing the number of (least significant) bits to take into account for the exponent. Read more
Source§

impl<const LIMBS: usize> Retrieve for MontyForm<LIMBS>

Source§

type Output = Uint<LIMBS>

The original type.
Source§

fn retrieve(&self) -> Self::Output

Convert the number back from the optimized representation.
Source§

impl<const LIMBS: usize> Square for MontyForm<LIMBS>

Source§

fn square(&self) -> Self

Computes the same as self * self, but may be more efficient.
Source§

impl<const LIMBS: usize> SquareAssign for MontyForm<LIMBS>

Source§

fn square_assign(&mut self)

Computes the same as self * self, but may be more efficient. Writes the result in self.
Source§

impl<const LIMBS: usize> Sub<&MontyForm<LIMBS>> for &MontyForm<LIMBS>

Source§

type Output = MontyForm<LIMBS>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &MontyForm<LIMBS>) -> MontyForm<LIMBS>

Performs the - operation. Read more
Source§

impl<const LIMBS: usize> Sub<&MontyForm<LIMBS>> for MontyForm<LIMBS>

Source§

type Output = MontyForm<LIMBS>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &MontyForm<LIMBS>) -> MontyForm<LIMBS>

Performs the - operation. Read more
Source§

impl<const LIMBS: usize> Sub<MontyForm<LIMBS>> for &MontyForm<LIMBS>

Source§

type Output = MontyForm<LIMBS>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: MontyForm<LIMBS>) -> MontyForm<LIMBS>

Performs the - operation. Read more
Source§

impl<const LIMBS: usize> Sub for MontyForm<LIMBS>

Source§

type Output = MontyForm<LIMBS>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: MontyForm<LIMBS>) -> MontyForm<LIMBS>

Performs the - operation. Read more
Source§

impl<const LIMBS: usize> SubAssign<&MontyForm<LIMBS>> for MontyForm<LIMBS>

Source§

fn sub_assign(&mut self, rhs: &MontyForm<LIMBS>)

Performs the -= operation. Read more
Source§

impl<const LIMBS: usize> SubAssign for MontyForm<LIMBS>

Source§

fn sub_assign(&mut self, rhs: MontyForm<LIMBS>)

Performs the -= operation. Read more
Source§

impl<const LIMBS: usize> Zeroize for MontyForm<LIMBS>

Available on crate feature zeroize only.
Source§

fn zeroize(&mut self)

Zero out this object from memory using Rust intrinsics which ensure the zeroization operation is not “optimized away” by the compiler.
Source§

impl<const LIMBS: usize> Copy for MontyForm<LIMBS>

Source§

impl<const LIMBS: usize> Eq for MontyForm<LIMBS>

Source§

impl<const LIMBS: usize> StructuralPartialEq for MontyForm<LIMBS>

Auto Trait Implementations§

§

impl<const LIMBS: usize> Freeze for MontyForm<LIMBS>

§

impl<const LIMBS: usize> RefUnwindSafe for MontyForm<LIMBS>

§

impl<const LIMBS: usize> Send for MontyForm<LIMBS>

§

impl<const LIMBS: usize> Sync for MontyForm<LIMBS>

§

impl<const LIMBS: usize> Unpin for MontyForm<LIMBS>

§

impl<const LIMBS: usize> UnwindSafe for MontyForm<LIMBS>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> ConditionallyNegatable for T
where T: ConditionallySelectable, &'a T: for<'a> Neg<Output = T>,

Source§

fn conditional_negate(&mut self, choice: Choice)

Negate self if choice == Choice(1); otherwise, leave it unchanged. Read more
Source§

impl<T> ConstantTimeSelect for T

Source§

fn ct_select(a: &T, b: &T, choice: Choice) -> T

Select a or b according to choice. Read more
Source§

fn ct_assign(&mut self, other: &T, choice: Choice)

Conditionally assign other to self, according to choice.
Source§

fn ct_swap(a: &mut T, b: &mut T, choice: Choice)

Conditionally swap self and other if choice == 1; otherwise, reassign both unto themselves.
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, Exponent, BasesAndExponents> MultiExponentiate<Exponent, BasesAndExponents> for T
where T: MultiExponentiateBoundedExp<Exponent, BasesAndExponents>, Exponent: Bounded, BasesAndExponents: AsRef<[(T, Exponent)]> + ?Sized,

Source§

fn multi_exponentiate(bases_and_exponents: &BasesAndExponents) -> T

Calculates x1 ^ k1 * ... * xn ^ kn.
Source§

impl<T, Exponent> Pow<Exponent> for T
where T: PowBoundedExp<Exponent>, Exponent: Bounded,

Source§

fn pow(&self, exponent: &Exponent) -> T

Raises to the exponent power.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.