Skip to main content

Residue

Struct Residue 

pub struct Residue<MOD, const LIMBS: usize>
where MOD: ResidueParams<LIMBS>,
{ /* private fields */ }
Expand description

A residue mod MOD, represented using LIMBS limbs. The modulus of this residue is constant, so it cannot be set at runtime. Internally, the value is stored in Montgomery form (multiplied by MOD::R) until it is retrieved.

Implementations§

§

impl<MOD, const LIMBS: usize> Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

pub const fn add(&self, rhs: &Residue<MOD, LIMBS>) -> Residue<MOD, LIMBS>

Adds rhs.

§

impl<MOD, const LIMBS: usize> Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

pub const fn invert(&self) -> (Residue<MOD, LIMBS>, CtChoice)

Computes the residue 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).

§

impl<MOD, const LIMBS: usize> Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

pub const fn mul(&self, rhs: &Residue<MOD, LIMBS>) -> Residue<MOD, LIMBS>

Multiplies by rhs.

pub const fn square(&self) -> Residue<MOD, LIMBS>

Computes the (reduced) square of a residue.

§

impl<MOD, const LIMBS: usize> Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

pub const fn neg(&self) -> Residue<MOD, LIMBS>

Negates the number.

§

impl<MOD, const LIMBS: usize> Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

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

Raises to the exponent power.

pub const fn pow_bounded_exp<const RHS_LIMBS: usize>( &self, exponent: &Uint<RHS_LIMBS>, exponent_bits: usize, ) -> Residue<MOD, LIMBS>

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.

§

impl<MOD, const LIMBS: usize> Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

pub const fn sub(&self, rhs: &Residue<MOD, LIMBS>) -> Residue<MOD, LIMBS>

Subtracts rhs.

§

impl<MOD, const LIMBS: usize> Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

pub const ZERO: Residue<MOD, LIMBS>

The representation of 0 mod MOD.

pub const ONE: Residue<MOD, LIMBS>

The representation of 1 mod MOD.

pub const fn new(integer: &Uint<LIMBS>) -> Residue<MOD, LIMBS>

Instantiates a new Residue that represents this integer mod MOD. If the modulus represented by MOD is not odd, this function will panic; use new_checked if you want to be able to detect an invalid modulus.

pub fn new_checked(integer: &Uint<LIMBS>) -> CtOption<Residue<MOD, LIMBS>>

Instantiates a new Residue that represents this integer mod MOD if the modulus is odd. Returns a CtOption that is None if the provided modulus is not odd; this is a safer version of new, which can panic.

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

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

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

Access the Residue value in Montgomery form.

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

Mutably access the Residue value in Montgomery form.

pub const fn from_montgomery(integer: Uint<LIMBS>) -> Residue<MOD, LIMBS>

Create a Residue from a value in Montgomery form.

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

Extract the value from the Residue in Montgomery form.

pub fn div_by_2(&self) -> Residue<MOD, LIMBS>

Performs the modular division by 2, that is for given x returns y such that y * 2 = x mod p. This means:

  • if x is even, returns x / 2,
  • if x is odd, returns (x + p) / 2 (since the modulus p in Montgomery form is always odd, this divides entirely).

Trait Implementations§

§

impl<MOD, const LIMBS: usize> Add<&Residue<MOD, LIMBS>> for &Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

type Output = Residue<MOD, LIMBS>

The resulting type after applying the + operator.
§

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

Performs the + operation. Read more
§

impl<MOD, const LIMBS: usize> Add<&Residue<MOD, LIMBS>> for Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

type Output = Residue<MOD, LIMBS>

The resulting type after applying the + operator.
§

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

Performs the + operation. Read more
§

impl<MOD, const LIMBS: usize> Add<Residue<MOD, LIMBS>> for &Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

type Output = Residue<MOD, LIMBS>

The resulting type after applying the + operator.
§

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

Performs the + operation. Read more
§

impl<MOD, const LIMBS: usize> Add for Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

type Output = Residue<MOD, LIMBS>

The resulting type after applying the + operator.
§

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

Performs the + operation. Read more
§

impl<MOD, const LIMBS: usize> AddAssign<&Residue<MOD, LIMBS>> for Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

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

Performs the += operation. Read more
§

impl<MOD, const LIMBS: usize> AddAssign for Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

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

Performs the += operation. Read more
§

impl<MOD, const LIMBS: usize> Clone for Residue<MOD, LIMBS>
where MOD: Clone + ResidueParams<LIMBS>,

§

fn clone(&self) -> Residue<MOD, LIMBS>

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

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

Performs copy-assignment from source. Read more
§

impl<MOD, const LIMBS: usize> ConditionallySelectable for Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS> + Copy,

§

fn conditional_select( a: &Residue<MOD, LIMBS>, b: &Residue<MOD, LIMBS>, choice: Choice, ) -> Residue<MOD, LIMBS>

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
§

impl<MOD, const LIMBS: usize> ConstantTimeEq for Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

fn ct_eq(&self, other: &Residue<MOD, LIMBS>) -> 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
§

impl<MOD, const LIMBS: usize> Debug for Residue<MOD, LIMBS>
where MOD: Debug + ResidueParams<LIMBS>,

§

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

Formats the value using the given formatter. Read more
§

impl<MOD, const LIMBS: usize> Default for Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

fn default() -> Residue<MOD, LIMBS>

Returns the “default value” for a type. Read more
§

impl<const LIMBS: usize, P> From<&Residue<P, LIMBS>> for DynResidue<LIMBS>
where P: ResidueParams<LIMBS>,

§

fn from(residue: &Residue<P, LIMBS>) -> DynResidue<LIMBS>

Converts to this type from the input type.
§

impl<MOD, const LIMBS: usize> Invert for Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

type Output = CtOption<Residue<MOD, LIMBS>>

Output of the inversion.
§

fn invert(&self) -> <Residue<MOD, LIMBS> as Invert>::Output

Computes the inverse.
§

impl<MOD, const LIMBS: usize> Mul<&Residue<MOD, LIMBS>> for &Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

type Output = Residue<MOD, LIMBS>

The resulting type after applying the * operator.
§

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

Performs the * operation. Read more
§

impl<MOD, const LIMBS: usize> Mul<&Residue<MOD, LIMBS>> for Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

type Output = Residue<MOD, LIMBS>

The resulting type after applying the * operator.
§

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

Performs the * operation. Read more
§

impl<MOD, const LIMBS: usize> Mul<Residue<MOD, LIMBS>> for &Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

type Output = Residue<MOD, LIMBS>

The resulting type after applying the * operator.
§

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

Performs the * operation. Read more
§

impl<MOD, const LIMBS: usize> Mul for Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

type Output = Residue<MOD, LIMBS>

The resulting type after applying the * operator.
§

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

Performs the * operation. Read more
§

impl<MOD, const LIMBS: usize> MulAssign<&Residue<MOD, LIMBS>> for Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

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

Performs the *= operation. Read more
§

impl<MOD, const LIMBS: usize> MulAssign for Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

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

Performs the *= operation. Read more
§

impl<const N: usize, MOD, const LIMBS: usize, const RHS_LIMBS: usize> MultiExponentiateBoundedExp<Uint<RHS_LIMBS>, [(Residue<MOD, LIMBS>, Uint<RHS_LIMBS>); N]> for Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

fn multi_exponentiate_bounded_exp( bases_and_exponents: &[(Residue<MOD, LIMBS>, Uint<RHS_LIMBS>); N], exponent_bits: usize, ) -> Residue<MOD, LIMBS>

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

impl<MOD, const LIMBS: usize> Neg for &Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

type Output = Residue<MOD, LIMBS>

The resulting type after applying the - operator.
§

fn neg(self) -> Residue<MOD, LIMBS>

Performs the unary - operation. Read more
§

impl<MOD, const LIMBS: usize> Neg for Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

type Output = Residue<MOD, LIMBS>

The resulting type after applying the - operator.
§

fn neg(self) -> Residue<MOD, LIMBS>

Performs the unary - operation. Read more
§

impl<MOD, const LIMBS: usize> PartialEq for Residue<MOD, LIMBS>
where MOD: PartialEq + ResidueParams<LIMBS>,

§

fn eq(&self, other: &Residue<MOD, 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.
§

impl<MOD, const LIMBS: usize, const RHS_LIMBS: usize> PowBoundedExp<Uint<RHS_LIMBS>> for Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

fn pow_bounded_exp( &self, exponent: &Uint<RHS_LIMBS>, exponent_bits: usize, ) -> Residue<MOD, LIMBS>

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

impl<MOD, const LIMBS: usize> Random for Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

Available on crate feature rand_core only.
§

fn random(rng: &mut impl CryptoRngCore) -> Residue<MOD, LIMBS>

Generate a cryptographically secure random value.
§

impl<MOD, const LIMBS: usize> Retrieve for Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

type Output = Uint<LIMBS>

The original type.
§

fn retrieve(&self) -> <Residue<MOD, LIMBS> as Retrieve>::Output

Convert the number back from the optimized representation.
§

impl<MOD, const LIMBS: usize> Square for Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

fn square(&self) -> Residue<MOD, LIMBS>

Computes the same as self.mul(self), but may be more efficient.
§

impl<MOD, const LIMBS: usize> Sub<&Residue<MOD, LIMBS>> for &Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

type Output = Residue<MOD, LIMBS>

The resulting type after applying the - operator.
§

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

Performs the - operation. Read more
§

impl<MOD, const LIMBS: usize> Sub<&Residue<MOD, LIMBS>> for Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

type Output = Residue<MOD, LIMBS>

The resulting type after applying the - operator.
§

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

Performs the - operation. Read more
§

impl<MOD, const LIMBS: usize> Sub<Residue<MOD, LIMBS>> for &Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

type Output = Residue<MOD, LIMBS>

The resulting type after applying the - operator.
§

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

Performs the - operation. Read more
§

impl<MOD, const LIMBS: usize> Sub for Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

type Output = Residue<MOD, LIMBS>

The resulting type after applying the - operator.
§

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

Performs the - operation. Read more
§

impl<MOD, const LIMBS: usize> SubAssign<&Residue<MOD, LIMBS>> for Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

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

Performs the -= operation. Read more
§

impl<MOD, const LIMBS: usize> SubAssign for Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

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

Performs the -= operation. Read more
§

impl<MOD, const LIMBS: usize> Zero for Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

§

const ZERO: Residue<MOD, LIMBS> = Self::ZERO

The value 0.
§

fn is_zero(&self) -> Choice

Determine if this value is equal to zero. Read more
§

impl<MOD, const LIMBS: usize> Copy for Residue<MOD, LIMBS>
where MOD: Copy + ResidueParams<LIMBS>,

§

impl<MOD, const LIMBS: usize> DefaultIsZeroes for Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

Available on crate feature zeroize only.
§

impl<MOD, const LIMBS: usize> Eq for Residue<MOD, LIMBS>
where MOD: Eq + ResidueParams<LIMBS>,

§

impl<MOD, const LIMBS: usize> StructuralPartialEq for Residue<MOD, LIMBS>
where MOD: ResidueParams<LIMBS>,

Auto Trait Implementations§

§

impl<MOD, const LIMBS: usize> Freeze for Residue<MOD, LIMBS>

§

impl<MOD, const LIMBS: usize> RefUnwindSafe for Residue<MOD, LIMBS>
where MOD: RefUnwindSafe,

§

impl<MOD, const LIMBS: usize> Send for Residue<MOD, LIMBS>

§

impl<MOD, const LIMBS: usize> Sync for Residue<MOD, LIMBS>

§

impl<MOD, const LIMBS: usize> Unpin for Residue<MOD, LIMBS>
where MOD: Unpin,

§

impl<MOD, const LIMBS: usize> UnwindSafe for Residue<MOD, LIMBS>
where MOD: UnwindSafe,

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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T, Exponent, BasesAndExponents> MultiExponentiate<Exponent, BasesAndExponents> for T
where T: MultiExponentiateBoundedExp<Exponent, BasesAndExponents>, Exponent: Bounded, BasesAndExponents: AsRef<[(T, Exponent)]> + ?Sized,

§

fn multi_exponentiate(bases_and_exponents: &BasesAndExponents) -> T

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

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

§

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.
§

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

§

fn vzip(self) -> V

§

impl<Z> Zeroize for Z
where Z: DefaultIsZeroes,

§

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.
§

impl<T, Rhs, Output> GroupOps<Rhs, Output> for T
where T: Add<Rhs, Output = Output> + Sub<Rhs, Output = Output> + AddAssign<Rhs> + SubAssign<Rhs>,

§

impl<T, Rhs, Output> GroupOpsOwned<Rhs, Output> for T
where T: for<'r> GroupOps<&'r Rhs, Output>,

§

impl<T, Rhs, Output> ScalarMul<Rhs, Output> for T
where T: Mul<Rhs, Output = Output> + MulAssign<Rhs>,

§

impl<T, Rhs, Output> ScalarMulOwned<Rhs, Output> for T
where T: for<'r> ScalarMul<&'r Rhs, Output>,