Struct rsa_cortex_m4::numbers::Convenient [−][src]
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]
&self,
n: &'n Convenient<F, G>
) -> Modular<'n, F, G>
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]
&self,
p: &'p Prime<F, G>
) -> PrimeModular<'p, F, G>
pub fn reduce<const F: usize, const G: usize>(
&self,
n: &Unsigned<F, G>
) -> Unsigned<F, G>
[src]
&self,
n: &Unsigned<F, G>
) -> Unsigned<F, G>
The canonical (completely) reduced representative of the associated residue class modulo $n$.
Cf. Modular
.
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]
&self
) -> Result<Unsigned<M, N>>
Trait Implementations
impl<const D: usize, const E: usize> AsRef<Convenient<D, E>> for Prime<D, E>
[src]
fn as_ref(&self) -> &Convenient<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]
fn clone(&self) -> Convenient<D, E>
[src]
pub fn clone_from(&mut self, source: &Self)
1.0.0[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.
fn deref(&self) -> &Self::Target
[src]
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]
fn from(convenient: Convenient<D, E>) -> Self
[src]
impl<T, const D: usize, const E: usize> PartialEq<T> for Convenient<D, E> where
T: Number,
[src]
T: Number,
impl<const D: usize, const E: usize> RefCast for Convenient<D, E>
[src]
type From = Odd<D, E>
fn ref_cast(_from: &Self::From) -> &Self
[src]
fn ref_cast_mut(_from: &mut Self::From) -> &mut Self
[src]
impl<const D: usize, const E: usize> TryFrom<Unsigned<D, E>> for Convenient<D, E>
[src]
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]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
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]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,