Modulus

Struct Modulus 

Source
pub struct Modulus { /* private fields */ }
Expand description

Structure encapsulating an integer modulus up to 62 bits.

Implementations§

Source§

impl Modulus

Source

pub fn new(p: u64) -> Result<Self>

Create a modulus from an integer of at most 62 bits.

Source

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

Performs the modular addition of a and b in constant time. Aborts if a >= p or b >= p in debug mode.

Source

pub const unsafe fn add_vt(&self, a: u64, b: u64) -> u64

Performs the modular addition of a and b in variable time. Aborts if a >= p or b >= p in debug mode.

§Safety

This function is not constant time and its timing may reveal information about the values being added.

Source

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

Performs the modular subtraction of a and b in constant time. Aborts if a >= p or b >= p in debug mode.

Source

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

Performs the modular multiplication of a and b in constant time. Aborts if a >= p or b >= p in debug mode.

Source

pub const fn mul_opt(&self, a: u64, b: u64) -> u64

Optimized modular multiplication of a and b in constant time.

Aborts if a >= p or b >= p in debug mode.

Source

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

Modular negation in constant time.

Aborts if a >= p in debug mode.

Source

pub const fn shoup(&self, a: u64) -> u64

Compute the Shoup representation of a.

Aborts if a >= p in debug mode.

Source

pub const fn mul_shoup(&self, a: u64, b: u64, b_shoup: u64) -> u64

Shoup multiplication of a and b in constant time.

Aborts if b >= p or b_shoup != shoup(b) in debug mode.

Source

pub const fn lazy_mul_shoup(&self, a: u64, b: u64, b_shoup: u64) -> u64

Lazy Shoup multiplication of a and b in constant time. The output is in the interval [0, 2 * p).

Aborts if b >= p or b_shoup != shoup(b) in debug mode.

Source

pub fn add_vec(&self, a: &mut [u64], b: &[u64])

Modular addition of vectors in place in constant time.

Aborts if a and b differ in size, and if any of their values is >= p in debug mode.

Source

pub unsafe fn add_vec_vt(&self, a: &mut [u64], b: &[u64])

Modular addition of vectors in place in variable time. Aborts if a and b differ in size, and if any of their values is >= p in debug mode.

§Safety

This function is not constant time and its timing may reveal information about the values being added.

Source

pub fn sub_vec(&self, a: &mut [u64], b: &[u64])

Modular subtraction of vectors in place in constant time.

Aborts if a and b differ in size, and if any of their values is >= p in debug mode.

Source

pub unsafe fn sub_vec_vt(&self, a: &mut [u64], b: &[u64])

Modular subtraction of vectors in place in variable time. Aborts if a and b differ in size, and if any of their values is >= p in debug mode.

§Safety

This function is not constant time and its timing may reveal information about the values being subtracted.

Source

pub fn mul_vec(&self, a: &mut [u64], b: &[u64])

Modular multiplication of vectors in place in constant time.

Aborts if a and b differ in size, and if any of their values is >= p in debug mode.

Source

pub fn scalar_mul_vec(&self, a: &mut [u64], b: u64)

Modular scalar multiplication of vectors in place in constant time.

Aborts if any of the values in a is >= p in debug mode.

Source

pub unsafe fn scalar_mul_vec_vt(&self, a: &mut [u64], b: u64)

Modular scalar multiplication of vectors in place in variable time. Aborts if any of the values in a is >= p in debug mode.

§Safety

This function is not constant time and its timing may reveal information about the values being multiplied.

Source

pub unsafe fn mul_vec_vt(&self, a: &mut [u64], b: &[u64])

Modular multiplication of vectors in place in variable time. Aborts if a and b differ in size, and if any of their values is >= p in debug mode.

§Safety

This function is not constant time and its timing may reveal information about the values being subtracted.

Source

pub fn shoup_vec(&self, a: &[u64]) -> Vec<u64>

Compute the Shoup representation of a vector.

Aborts if any of the values of the vector is >= p in debug mode.

Source

pub fn mul_shoup_vec(&self, a: &mut [u64], b: &[u64], b_shoup: &[u64])

Shoup modular multiplication of vectors in place in constant time.

Aborts if a and b differ in size, and if any of their values is >= p in debug mode.

Source

pub unsafe fn mul_shoup_vec_vt(&self, a: &mut [u64], b: &[u64], b_shoup: &[u64])

Shoup modular multiplication of vectors in place in variable time. Aborts if a and b differ in size, and if any of their values is >= p in debug mode.

§Safety

This function is not constant time and its timing may reveal information about the values being multiplied.

Source

pub fn reduce_vec(&self, a: &mut [u64])

Reduce a vector in place in constant time.

Source

pub unsafe fn center_vec_vt(&self, a: &[u64]) -> Vec<i64>

Center a vector in variable time.

§Safety

This function is not constant time and its timing may reveal information about the values being centered.

Source

pub unsafe fn reduce_vec_vt(&self, a: &mut [u64])

Reduce a vector in place in variable time.

§Safety

This function is not constant time and its timing may reveal information about the values being reduced.

Source

pub fn reduce_vec_i64(&self, a: &[i64]) -> Vec<u64>

Reduce a vector in place in constant time.

Source

pub unsafe fn reduce_vec_i64_vt(&self, a: &[i64]) -> Vec<u64>

Reduce a vector in place in variable time.

§Safety

This function is not constant time and its timing may reveal information about the values being reduced.

Source

pub fn reduce_vec_new(&self, a: &[u64]) -> Vec<u64>

Reduce a vector in constant time.

Source

pub unsafe fn reduce_vec_new_vt(&self, a: &[u64]) -> Vec<u64>

Reduce a vector in variable time.

§Safety

This function is not constant time and its timing may reveal information about the values being reduced.

Source

pub fn neg_vec(&self, a: &mut [u64])

Modular negation of a vector in place in constant time.

Aborts if any of the values in the vector is >= p in debug mode.

Source

pub unsafe fn neg_vec_vt(&self, a: &mut [u64])

Modular negation of a vector in place in variable time. Aborts if any of the values in the vector is >= p in debug mode.

§Safety

This function is not constant time and its timing may reveal information about the values being negated.

Source

pub fn pow(&self, a: u64, n: u64) -> u64

Modular exponentiation in variable time.

Aborts if a >= p or n >= p in debug mode.

Source

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

Modular inversion in variable time.

Returns None if p is not prime or a = 0. Aborts if a >= p in debug mode.

Source

pub const fn reduce_u128(&self, a: u128) -> u64

Modular reduction of a u128 in constant time.

Source

pub const unsafe fn reduce_u128_vt(&self, a: u128) -> u64

Modular reduction of a u128 in variable time.

§Safety

This function is not constant time and its timing may reveal information about the value being reduced.

Source

pub const fn reduce(&self, a: u64) -> u64

Modular reduction of a u64 in constant time.

Source

pub const unsafe fn reduce_vt(&self, a: u64) -> u64

Modular reduction of a u64 in variable time.

§Safety

This function is not constant time and its timing may reveal information about the value being reduced.

Source

pub const fn reduce_opt_u128(&self, a: u128) -> u64

Optimized modular reduction of a u128 in constant time.

Source

pub const fn reduce_opt(&self, a: u64) -> u64

Optimized modular reduction of a u64 in constant time.

Source

pub const unsafe fn reduce_opt_vt(&self, a: u64) -> u64

Optimized modular reduction of a u64 in variable time.

§Safety

This function is not constant time and its timing may reveal information about the value being reduced.

Source

pub const fn lazy_reduce_u128(&self, a: u128) -> u64

Lazy modular reduction of a in constant time. The output is in the interval [0, 2 * p).

Source

pub const fn lazy_reduce(&self, a: u64) -> u64

Lazy modular reduction of a in constant time. The output is in the interval [0, 2 * p).

Source

pub const fn lazy_reduce_opt_u128(&self, a: u128) -> u64

Lazy optimized modular reduction of a in constant time. The output is in the interval [0, 2 * p).

Aborts if the input is >= p ^ 2 in debug mode.

Source

pub fn lazy_reduce_vec(&self, a: &mut [u64])

Lazy modular reduction of a vector in constant time. The output coefficients are in the interval [0, 2 * p).

Source

pub fn random_vec<R: RngCore + CryptoRng>( &self, size: usize, rng: &mut R, ) -> Vec<u64>

Returns a random vector.

Source

pub const fn serialization_length(&self, size: usize) -> usize

Length of the serialization of a vector of size size.

Panics if the size is not a multiple of 8.

Source

pub fn serialize_vec(&self, a: &[u64]) -> Vec<u8>

Serialize a vector of elements of length a multiple of 8.

Panics if the length of the vector is not a multiple of 8.

Source

pub fn deserialize_vec(&self, b: &[u8]) -> Vec<u64>

Deserialize a vector of bytes into a vector of elements mod p.

Methods from Deref<Target = u64>§

1.43.0 · Source

pub const MIN: u64 = 0u64

1.43.0 · Source

pub const MAX: u64 = 18_446_744_073_709_551_615u64

1.53.0 · Source

pub const BITS: u32 = 64u32

Trait Implementations§

Source§

impl Clone for Modulus

Source§

fn clone(&self) -> Modulus

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

impl Debug for Modulus

Source§

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

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

impl Deref for Modulus

Source§

type Target = u64

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl PartialEq for Modulus

Source§

fn eq(&self, other: &Self) -> 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 Eq for Modulus

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

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.
Source§

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

Source§

fn vzip(self) -> V