Struct rsa_cortex_m4::numbers::Convenient[][src]

#[repr(transparent)]pub struct Convenient<const D: usize, const E: usize>(_);

Unsigned numbers with both their top and bottom bits set – highly convenient for modular arithmetic!

In particular, they are odd. But also, $n \ge 2^{m - 1}$, with strict inequality by oddness.

As described in Incomplete reduction in modular arithmetic (2002), it is not necessary to reduce fully modulo n while calculating modular arithmetic. Instead, we can reduce modulo $2^m$, and only "fully" reduce when so desired.

Their arguments apply to non-prime moduli also, and the "convenient" ones have the properties, in their terminology, that $I = 1$ and $J = 2$, hence $F = 2^m - p$ and $G = 2p - 2^m$. Moreover (!!!), the last case/reduction in their modular addition / subtraction never occurs.

E.g., in addition, we have $F = 2^m - n < 2^m - 2^{m - 1} = 2^{m-1}$, and so $T := S + F < (2^m - 2) + 2^{m - 1} < 2^{m - 1}$.

Implementations

impl<const D: usize, const E: usize> Convenient<D, E>[src]

pub fn as_odd(&self) -> &Odd<D, E>[src]

pub fn into_odd(self) -> Odd<D, E>[src]

pub fn as_unsigned(&self) -> &Unsigned<D, E>[src]

pub fn into_unsigned(self) -> Unsigned<D, E>[src]

Methods from Deref<Target = Unsigned<D, E>>

pub fn checked_add(&self, summand: &Self) -> Option<Self>[src]

pub fn wrapping_add_assign(&mut self, summand: &Self)[src]

pub fn wrapping_add(&self, summand: &Self) -> Self[src]

pub fn wrapping_neg(&self) -> Self[src]

pub fn checked_sub(&self, subtrahend: &Self) -> Option<Self>[src]

pub fn wrapping_sub_assign<T: Number>(&mut self, subtrahend: &T)[src]

pub fn wrapping_sub(&self, subtrahend: &Self) -> Self[src]

pub fn wrapping_mul(&self, factor: &Self) -> Self[src]

pub fn wrapping_inv(&self) -> Result<Self>[src]

The wrapping inverse, i.e., the exact inverse w.r.t wrapping multiplication.

Exists if and only if the number is odd.

This uses $\mathcal{O}(\log n)$ loops in Self::BITS, very efficient (!)

Source: Fig. 1 from GCD-Free Algorithms for Computing Modular Inverses (2003)

Note that this source is highly confusing! What they mean to say is to iterate $y \leftarrow y(2 - ey)$ in $\mathbb{Z}/2^{|f|}$, where the output is an inverse of $e$ modulo $2^{2i}$. In other words, the $\text{mod }2^i$ is a typo, and should be $\text{mod }2^{|f|}$.

cf. also Crypto StackExchange.

pub fn modulo<'n, const F: usize, const G: usize>(
    &self,
    n: &'n Convenient<F, G>
) -> Modular<'n, F, G>
[src]

The associated residue class modulo n.

Note that storage requirements of the residue class are the same as the modulus (+ reference to it), not the original integer.

This uses incomplete reduction ([Self::partially_reduce]) for efficiency.

pub fn modulo_prime<'p, const F: usize, const G: usize>(
    &self,
    p: &'p Prime<F, G>
) -> PrimeModular<'p, F, G>
[src]

pub fn reduce<const F: usize, const G: usize>(
    &self,
    n: &Unsigned<F, G>
) -> Unsigned<F, G>
[src]

The canonical (completely) reduced representative of the associated residue class modulo $n$.

Cf. Modular.

pub const fn digit(&self) -> Digit[src]

pub fn to_bytes(&self) -> BigEndian<D, E, 1>[src]

Return buffer that dereferences as big-endian bytes.

pub fn leading_digit(&self) -> Option<Digit>[src]

pub fn significant_digits(&self) -> &[Digit][src]

pub fn to_unsigned<const M: usize, const N: usize>(
    &self
) -> Result<Unsigned<M, N>>
[src]

Trait Implementations

impl<const D: usize, const E: usize> AsRef<Convenient<D, E>> for Prime<D, E>[src]

impl<const D: usize, const E: usize> AsRef<Unsigned<D, E>> for Convenient<D, E>[src]

impl<const D: usize, const E: usize> Clone for Convenient<D, E>[src]

impl<const D: usize, const E: usize> Debug for Convenient<D, E>[src]

impl<const D: usize, const E: usize> Deref for Convenient<D, E>[src]

type Target = Unsigned<D, E>

The resulting type after dereferencing.

impl<const D: usize, const E: usize> DerefMut for Convenient<D, E>[src]

impl<const D: usize, const E: usize> From<Convenient<D, E>> for Unsigned<D, E>[src]

impl<T, const D: usize, const E: usize> PartialEq<T> for Convenient<D, E> where
    T: Number
[src]

impl<const D: usize, const E: usize> RefCast for Convenient<D, E>[src]

type From = Odd<D, E>

impl<const D: usize, const E: usize> TryFrom<Unsigned<D, E>> for Convenient<D, E>[src]

type Error = Error

The type returned in the event of a conversion error.

fn try_from(unsigned: Unsigned<D, E>) -> Result<Self>[src]

Enforces odd parity.

Auto Trait Implementations

impl<const D: usize, const E: usize> Send for Convenient<D, E>[src]

impl<const D: usize, const E: usize> Sync for Convenient<D, E>[src]

impl<const D: usize, const E: usize> Unpin for Convenient<D, E>[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T[src]

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.