Struct crypto_bigint::UInt
source · pub struct UInt<const LIMBS: usize> { /* private fields */ }
Expand description
Big unsigned integer.
Generic over the given number of LIMBS
Encoding support
This type supports many different types of encodings, either via the
Encoding
trait or various const fn
decoding and
encoding functions that can be used with UInt
constants.
Optional crate features for encoding (off-by-default):
generic-array
: enablesArrayEncoding
trait which can be used toUInt
asGenericArray<u8, N>
and aArrayDecoding
trait which can be used toGenericArray<u8, N>
asUInt
.rlp
: support for Recursive Length Prefix (RLP) encoding.
Implementations§
source§impl<const LIMBS: usize> UInt<LIMBS>
impl<const LIMBS: usize> UInt<LIMBS>
sourcepub const fn adc(&self, rhs: &Self, carry: Limb) -> (Self, Limb)
pub const fn adc(&self, rhs: &Self, carry: Limb) -> (Self, Limb)
Computes a + b + carry
, returning the result along with the new carry.
sourcepub const fn saturating_add(&self, rhs: &Self) -> Self
pub const fn saturating_add(&self, rhs: &Self) -> Self
Perform saturating addition, returning MAX
on overflow.
sourcepub const fn wrapping_add(&self, rhs: &Self) -> Self
pub const fn wrapping_add(&self, rhs: &Self) -> Self
Perform wrapping addition, discarding overflow.
source§impl<const LIMBS: usize> UInt<LIMBS>
impl<const LIMBS: usize> UInt<LIMBS>
sourcepub const fn add_mod(&self, rhs: &UInt<LIMBS>, p: &UInt<LIMBS>) -> UInt<LIMBS>
pub const fn add_mod(&self, rhs: &UInt<LIMBS>, p: &UInt<LIMBS>) -> UInt<LIMBS>
Computes self + rhs mod p
in constant time.
Assumes self + rhs
as unbounded integer is < 2p
.
sourcepub const fn add_mod_special(&self, rhs: &Self, c: Limb) -> Self
pub const fn add_mod_special(&self, rhs: &Self, c: Limb) -> Self
Computes self + rhs mod p
in constant time for the special modulus
p = MAX+1-c
where c
is small enough to fit in a single Limb
.
Assumes self + rhs
as unbounded integer is < 2p
.
source§impl<const LIMBS: usize> UInt<LIMBS>
impl<const LIMBS: usize> UInt<LIMBS>
sourcepub const fn wrapping_and(&self, rhs: &Self) -> Self
pub const fn wrapping_and(&self, rhs: &Self) -> Self
Perform wrapping bitwise AND
.
There’s no way wrapping could ever happen. This function exists so that all operations are accounted for in the wrapping operations
sourcepub fn checked_and(&self, rhs: &Self) -> CtOption<Self>
pub fn checked_and(&self, rhs: &Self) -> CtOption<Self>
Perform checked bitwise AND
, returning a CtOption
which is_some
always
source§impl<const LIMBS: usize> UInt<LIMBS>
impl<const LIMBS: usize> UInt<LIMBS>
sourcepub const fn wrapping_or(&self, rhs: &Self) -> Self
pub const fn wrapping_or(&self, rhs: &Self) -> Self
Perform wrapping bitwise OR
.
There’s no way wrapping could ever happen. This function exists so that all operations are accounted for in the wrapping operations
sourcepub fn checked_or(&self, rhs: &Self) -> CtOption<Self>
pub fn checked_or(&self, rhs: &Self) -> CtOption<Self>
Perform checked bitwise OR
, returning a CtOption
which is_some
always
source§impl<const LIMBS: usize> UInt<LIMBS>
impl<const LIMBS: usize> UInt<LIMBS>
sourcepub const fn wrapping_xor(&self, rhs: &Self) -> Self
pub const fn wrapping_xor(&self, rhs: &Self) -> Self
Perform wrapping bitwise `XOR``.
There’s no way wrapping could ever happen. This function exists so that all operations are accounted for in the wrapping operations
sourcepub fn checked_xor(&self, rhs: &Self) -> CtOption<Self>
pub fn checked_xor(&self, rhs: &Self) -> CtOption<Self>
Perform checked bitwise XOR
, returning a CtOption
which is_some
always
source§impl<const LIMBS: usize> UInt<LIMBS>
impl<const LIMBS: usize> UInt<LIMBS>
sourcepub const fn bit_vartime(self, index: usize) -> Word
pub const fn bit_vartime(self, index: usize) -> Word
Get the value of the bit at position index
, as a 0- or 1-valued Word.
Returns 0 for indices out of range.
sourcepub const fn bits(self) -> usize
👎Deprecated: please use bits_vartime
instead
pub const fn bits(self) -> usize
bits_vartime
insteadCalculate the number of bits needed to represent this number.
sourcepub const fn bits_vartime(self) -> usize
pub const fn bits_vartime(self) -> usize
Calculate the number of bits needed to represent this number.
source§impl<const LIMBS: usize> UInt<LIMBS>
impl<const LIMBS: usize> UInt<LIMBS>
sourcepub const fn reduce2k(&self, k: usize) -> Self
pub const fn reduce2k(&self, k: usize) -> Self
Computes self
% 2^k. Faster than reduce since its a power of 2.
Limited to 2^16-1 since UInt doesn’t support higher.
sourcepub fn div_rem(&self, rhs: &Self) -> CtOption<(Self, Self)>
pub fn div_rem(&self, rhs: &Self) -> CtOption<(Self, Self)>
Computes self / rhs, returns the quotient, remainder if rhs != 0
sourcepub fn reduce(&self, rhs: &Self) -> CtOption<Self>
pub fn reduce(&self, rhs: &Self) -> CtOption<Self>
Computes self % rhs, returns the remainder if rhs != 0
sourcepub const fn wrapping_div(&self, rhs: &Self) -> Self
pub const fn wrapping_div(&self, rhs: &Self) -> Self
Wrapped division is just normal division i.e. self
/ rhs
There’s no way wrapping could ever happen.
This function exists, so that all operations are accounted for in the wrapping operations.
sourcepub fn checked_div(&self, rhs: &Self) -> CtOption<Self>
pub fn checked_div(&self, rhs: &Self) -> CtOption<Self>
Perform checked division, returning a CtOption
which is_some
only if the rhs != 0
sourcepub const fn wrapping_rem(&self, rhs: &Self) -> Self
pub const fn wrapping_rem(&self, rhs: &Self) -> Self
Wrapped (modular) remainder calculation is just self
% rhs
.
There’s no way wrapping could ever happen.
This function exists, so that all operations are accounted for in the wrapping operations.
sourcepub fn checked_rem(&self, rhs: &Self) -> CtOption<Self>
pub fn checked_rem(&self, rhs: &Self) -> CtOption<Self>
Perform checked reduction, returning a CtOption
which is_some
only if the rhs != 0
source§impl<const LIMBS: usize> UInt<LIMBS>
impl<const LIMBS: usize> UInt<LIMBS>
sourcepub const fn from_be_slice(bytes: &[u8]) -> Self
pub const fn from_be_slice(bytes: &[u8]) -> Self
Create a new UInt
from the provided big endian bytes.
sourcepub const fn from_be_hex(hex: &str) -> Self
pub const fn from_be_hex(hex: &str) -> Self
Create a new UInt
from the provided big endian hex string.
sourcepub const fn from_le_slice(bytes: &[u8]) -> Self
pub const fn from_le_slice(bytes: &[u8]) -> Self
Create a new UInt
from the provided little endian bytes.
sourcepub const fn from_le_hex(hex: &str) -> Self
pub const fn from_le_hex(hex: &str) -> Self
Create a new UInt
from the provided little endian hex string.
source§impl<const LIMBS: usize> UInt<LIMBS>
impl<const LIMBS: usize> UInt<LIMBS>
sourcepub const fn from_wide_word(n: WideWord) -> Self
pub const fn from_wide_word(n: WideWord) -> Self
Create a UInt
from a WideWord
(const-friendly)
source§impl<const LIMBS: usize> UInt<LIMBS>
impl<const LIMBS: usize> UInt<LIMBS>
sourcepub const fn inv_mod2k(&self, k: usize) -> Self
pub const fn inv_mod2k(&self, k: usize) -> Self
Computes 1/self
mod 2^k as specified in Algorithm 4 from
A Secure Algorithm for Inversion Modulo 2k by
Sadiel de la Fe and Carles Ferrer. See
https://www.mdpi.com/2410-387X/2/3/23.
Conditions: self
< 2^k and self
must be odd
source§impl<const LIMBS: usize> UInt<LIMBS>
impl<const LIMBS: usize> UInt<LIMBS>
sourcepub const fn mul_wide(&self, rhs: &Self) -> (Self, Self)
pub const fn mul_wide(&self, rhs: &Self) -> (Self, Self)
Compute “wide” multiplication, with a product twice the size of the input.
Returns a tuple containing the (lo, hi)
components of the product.
Ordering note
Releases of crypto-bigint
prior to v0.3 used (hi, lo)
ordering
instead. This has been changed for better consistency with the rest of
the APIs in this crate.
For more info see: https://github.com/RustCrypto/crypto-bigint/issues/4
sourcepub const fn saturating_mul(&self, rhs: &Self) -> Self
pub const fn saturating_mul(&self, rhs: &Self) -> Self
Perform saturating multiplication, returning MAX
on overflow.
sourcepub const fn wrapping_mul(&self, rhs: &Self) -> Self
pub const fn wrapping_mul(&self, rhs: &Self) -> Self
Perform wrapping multiplication, discarding overflow.
source§impl<const LIMBS: usize> UInt<LIMBS>
impl<const LIMBS: usize> UInt<LIMBS>
sourcepub const fn mul_mod_special(&self, rhs: &Self, c: Limb) -> Self
pub const fn mul_mod_special(&self, rhs: &Self, c: Limb) -> Self
Computes self * rhs mod p
in constant time for the special modulus
p = MAX+1-c
where c
is small enough to fit in a single Limb
.
For the modulus reduction, this function implements Algorithm 14.47 from
the “Handbook of Applied Cryptography”, by A. Menezes, P. van Oorschot,
and S. Vanstone, CRC Press, 1996.
source§impl<const LIMBS: usize> UInt<LIMBS>
impl<const LIMBS: usize> UInt<LIMBS>
sourcepub const fn shl_vartime(&self, n: usize) -> Self
pub const fn shl_vartime(&self, n: usize) -> Self
Computes self << shift
.
NOTE: this operation is variable time with respect to n
ONLY.
When used with a fixed n
, this function is constant-time with respect
to self
.
source§impl<const LIMBS: usize> UInt<LIMBS>
impl<const LIMBS: usize> UInt<LIMBS>
sourcepub const fn shr_vartime(&self, shift: usize) -> Self
pub const fn shr_vartime(&self, shift: usize) -> Self
Computes self >> n
.
NOTE: this operation is variable time with respect to n
ONLY.
When used with a fixed n
, this function is constant-time with respect
to self
.
source§impl<const LIMBS: usize> UInt<LIMBS>
impl<const LIMBS: usize> UInt<LIMBS>
sourcepub const fn sqrt(&self) -> Self
pub const fn sqrt(&self) -> Self
Computes √(self
)
Uses Brent & Zimmermann, Modern Computer Arithmetic, v0.5.9, Algorithm 1.13
Callers can check if self
is a square by squaring the result
sourcepub const fn wrapping_sqrt(&self) -> Self
pub const fn wrapping_sqrt(&self) -> Self
Wrapped sqrt is just normal √(self
)
There’s no way wrapping could ever happen.
This function exists, so that all operations are accounted for in the wrapping operations.
sourcepub fn checked_sqrt(&self) -> CtOption<Self>
pub fn checked_sqrt(&self) -> CtOption<Self>
Perform checked sqrt, returning a CtOption
which is_some
only if the √(self
)² == self
source§impl<const LIMBS: usize> UInt<LIMBS>
impl<const LIMBS: usize> UInt<LIMBS>
sourcepub const fn sbb(&self, rhs: &Self, borrow: Limb) -> (Self, Limb)
pub const fn sbb(&self, rhs: &Self, borrow: Limb) -> (Self, Limb)
Computes a - (b + borrow)
, returning the result along with the new borrow.
sourcepub const fn saturating_sub(&self, rhs: &Self) -> Self
pub const fn saturating_sub(&self, rhs: &Self) -> Self
Perform saturating subtraction, returning ZERO
on underflow.
sourcepub const fn wrapping_sub(&self, rhs: &Self) -> Self
pub const fn wrapping_sub(&self, rhs: &Self) -> Self
Perform wrapping subtraction, discarding underflow and wrapping around the boundary of the type.
source§impl<const LIMBS: usize> UInt<LIMBS>
impl<const LIMBS: usize> UInt<LIMBS>
sourcepub const fn sub_mod(&self, rhs: &UInt<LIMBS>, p: &UInt<LIMBS>) -> UInt<LIMBS>
pub const fn sub_mod(&self, rhs: &UInt<LIMBS>, p: &UInt<LIMBS>) -> UInt<LIMBS>
Computes self - rhs mod p
in constant time.
Assumes self - rhs
as unbounded signed integer is in [-p, p)
.
sourcepub const fn sub_mod_special(&self, rhs: &Self, c: Limb) -> Self
pub const fn sub_mod_special(&self, rhs: &Self, c: Limb) -> Self
Computes self - rhs mod p
in constant time for the special modulus
p = MAX+1-c
where c
is small enough to fit in a single Limb
.
Assumes self - rhs
as unbounded signed integer is in [-p, p)
.
source§impl<const LIMBS: usize> UInt<LIMBS>
impl<const LIMBS: usize> UInt<LIMBS>
sourcepub const fn from_words(arr: [Word; LIMBS]) -> Self
pub const fn from_words(arr: [Word; LIMBS]) -> Self
sourcepub fn as_words_mut(&mut self) -> &mut [Word; LIMBS]
pub fn as_words_mut(&mut self) -> &mut [Word; LIMBS]
Borrow the inner limbs as a mutable array of Word
s.
sourcepub const fn as_uint_array(&self) -> &[Word; LIMBS]
👎Deprecated since 0.4.8: please use as_words
instead
pub const fn as_uint_array(&self) -> &[Word; LIMBS]
as_words
insteadDeprecated: borrow the inner limbs as an array of Word
s.
sourcepub const fn from_uint_array(words: [Word; LIMBS]) -> Self
👎Deprecated since 0.4.8: please use from_words
instead
pub const fn from_uint_array(words: [Word; LIMBS]) -> Self
from_words
insteadsourcepub const fn to_uint_array(self) -> [Word; LIMBS]
👎Deprecated since 0.4.8: please use to_words
instead
pub const fn to_uint_array(self) -> [Word; LIMBS]
to_words
insteadsourcepub const fn into_limbs(self) -> [Limb; LIMBS]
pub const fn into_limbs(self) -> [Limb; LIMBS]
Convert this UInt
into its inner limbs.
Trait Implementations§
source§impl<const LIMBS: usize> BitAndAssign<&UInt<LIMBS>> for UInt<LIMBS>
impl<const LIMBS: usize> BitAndAssign<&UInt<LIMBS>> for UInt<LIMBS>
source§fn bitand_assign(&mut self, other: &Self)
fn bitand_assign(&mut self, other: &Self)
&=
operation. Read moresource§impl<const LIMBS: usize> BitAndAssign<UInt<LIMBS>> for UInt<LIMBS>
impl<const LIMBS: usize> BitAndAssign<UInt<LIMBS>> for UInt<LIMBS>
source§fn bitand_assign(&mut self, other: Self)
fn bitand_assign(&mut self, other: Self)
&=
operation. Read moresource§impl<const LIMBS: usize> BitOrAssign<&UInt<LIMBS>> for UInt<LIMBS>
impl<const LIMBS: usize> BitOrAssign<&UInt<LIMBS>> for UInt<LIMBS>
source§fn bitor_assign(&mut self, other: &Self)
fn bitor_assign(&mut self, other: &Self)
|=
operation. Read moresource§impl<const LIMBS: usize> BitOrAssign<UInt<LIMBS>> for UInt<LIMBS>
impl<const LIMBS: usize> BitOrAssign<UInt<LIMBS>> for UInt<LIMBS>
source§fn bitor_assign(&mut self, other: Self)
fn bitor_assign(&mut self, other: Self)
|=
operation. Read moresource§impl<const LIMBS: usize> BitXorAssign<&UInt<LIMBS>> for UInt<LIMBS>
impl<const LIMBS: usize> BitXorAssign<&UInt<LIMBS>> for UInt<LIMBS>
source§fn bitxor_assign(&mut self, other: &Self)
fn bitxor_assign(&mut self, other: &Self)
^=
operation. Read moresource§impl<const LIMBS: usize> BitXorAssign<UInt<LIMBS>> for UInt<LIMBS>
impl<const LIMBS: usize> BitXorAssign<UInt<LIMBS>> for UInt<LIMBS>
source§fn bitxor_assign(&mut self, other: Self)
fn bitxor_assign(&mut self, other: Self)
^=
operation. Read moresource§impl<const LIMBS: usize> ConditionallySelectable for UInt<LIMBS>
impl<const LIMBS: usize> ConditionallySelectable for UInt<LIMBS>
source§impl<const LIMBS: usize> ConstantTimeEq for UInt<LIMBS>
impl<const LIMBS: usize> ConstantTimeEq for UInt<LIMBS>
source§impl<const LIMBS: usize> ConstantTimeGreater for UInt<LIMBS>
impl<const LIMBS: usize> ConstantTimeGreater for UInt<LIMBS>
source§impl<const LIMBS: usize> ConstantTimeLess for UInt<LIMBS>
impl<const LIMBS: usize> ConstantTimeLess for UInt<LIMBS>
source§impl<const LIMBS: usize> Div<&NonZero<UInt<LIMBS>>> for &UInt<LIMBS>where
UInt<LIMBS>: Integer,
impl<const LIMBS: usize> Div<&NonZero<UInt<LIMBS>>> for &UInt<LIMBS>where UInt<LIMBS>: Integer,
source§impl<const LIMBS: usize> Div<&NonZero<UInt<LIMBS>>> for UInt<LIMBS>where
UInt<LIMBS>: Integer,
impl<const LIMBS: usize> Div<&NonZero<UInt<LIMBS>>> for UInt<LIMBS>where UInt<LIMBS>: Integer,
source§impl<const LIMBS: usize> Div<NonZero<UInt<LIMBS>>> for &UInt<LIMBS>where
UInt<LIMBS>: Integer,
impl<const LIMBS: usize> Div<NonZero<UInt<LIMBS>>> for &UInt<LIMBS>where UInt<LIMBS>: Integer,
source§impl<const LIMBS: usize> Div<NonZero<UInt<LIMBS>>> for UInt<LIMBS>where
UInt<LIMBS>: Integer,
impl<const LIMBS: usize> Div<NonZero<UInt<LIMBS>>> for UInt<LIMBS>where UInt<LIMBS>: Integer,
source§impl<const LIMBS: usize> DivAssign<&NonZero<UInt<LIMBS>>> for UInt<LIMBS>where
UInt<LIMBS>: Integer,
impl<const LIMBS: usize> DivAssign<&NonZero<UInt<LIMBS>>> for UInt<LIMBS>where UInt<LIMBS>: Integer,
source§impl<const LIMBS: usize> DivAssign<NonZero<UInt<LIMBS>>> for UInt<LIMBS>where
UInt<LIMBS>: Integer,
impl<const LIMBS: usize> DivAssign<NonZero<UInt<LIMBS>>> for UInt<LIMBS>where UInt<LIMBS>: Integer,
source§impl<const LIMBS: usize> Ord for UInt<LIMBS>
impl<const LIMBS: usize> Ord for UInt<LIMBS>
source§impl<const LIMBS: usize> PartialEq<UInt<LIMBS>> for UInt<LIMBS>
impl<const LIMBS: usize> PartialEq<UInt<LIMBS>> for UInt<LIMBS>
source§impl<const LIMBS: usize> PartialOrd<UInt<LIMBS>> for UInt<LIMBS>
impl<const LIMBS: usize> PartialOrd<UInt<LIMBS>> for UInt<LIMBS>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<const LIMBS: usize> RandomMod for UInt<LIMBS>
impl<const LIMBS: usize> RandomMod for UInt<LIMBS>
source§fn random_mod(rng: impl CryptoRng + RngCore, modulus: &NonZero<Self>) -> Self
fn random_mod(rng: impl CryptoRng + RngCore, modulus: &NonZero<Self>) -> Self
Generate a cryptographically secure random UInt
which is less than
a given modulus
.
This function uses rejection sampling, a method which produces an
unbiased distribution of in-range values provided the underlying
CryptoRng
is unbiased, but runs in variable-time.
The variable-time nature of the algorithm should not pose a security
issue so long as the underlying random number generator is truly a
CryptoRng
, where previous outputs are unrelated to subsequent
outputs and do not reveal information about the RNG’s internal state.
source§impl<const LIMBS: usize> Rem<&NonZero<UInt<LIMBS>>> for &UInt<LIMBS>where
UInt<LIMBS>: Integer,
impl<const LIMBS: usize> Rem<&NonZero<UInt<LIMBS>>> for &UInt<LIMBS>where UInt<LIMBS>: Integer,
source§impl<const LIMBS: usize> Rem<&NonZero<UInt<LIMBS>>> for UInt<LIMBS>where
UInt<LIMBS>: Integer,
impl<const LIMBS: usize> Rem<&NonZero<UInt<LIMBS>>> for UInt<LIMBS>where UInt<LIMBS>: Integer,
source§impl<const LIMBS: usize> Rem<NonZero<UInt<LIMBS>>> for &UInt<LIMBS>where
UInt<LIMBS>: Integer,
impl<const LIMBS: usize> Rem<NonZero<UInt<LIMBS>>> for &UInt<LIMBS>where UInt<LIMBS>: Integer,
source§impl<const LIMBS: usize> Rem<NonZero<UInt<LIMBS>>> for UInt<LIMBS>where
UInt<LIMBS>: Integer,
impl<const LIMBS: usize> Rem<NonZero<UInt<LIMBS>>> for UInt<LIMBS>where UInt<LIMBS>: Integer,
source§impl<const LIMBS: usize> RemAssign<&NonZero<UInt<LIMBS>>> for UInt<LIMBS>where
UInt<LIMBS>: Integer,
impl<const LIMBS: usize> RemAssign<&NonZero<UInt<LIMBS>>> for UInt<LIMBS>where UInt<LIMBS>: Integer,
source§impl<const LIMBS: usize> RemAssign<NonZero<UInt<LIMBS>>> for UInt<LIMBS>where
UInt<LIMBS>: Integer,
impl<const LIMBS: usize> RemAssign<NonZero<UInt<LIMBS>>> for UInt<LIMBS>where UInt<LIMBS>: Integer,
source§impl<const LIMBS: usize> ShlAssign<usize> for UInt<LIMBS>
impl<const LIMBS: usize> ShlAssign<usize> for UInt<LIMBS>
source§fn shl_assign(&mut self, rhs: usize)
fn shl_assign(&mut self, rhs: usize)
NOTE: this operation is variable time with respect to rhs
ONLY.
When used with a fixed rhs
, this function is constant-time with respect
to self
.
source§impl<const LIMBS: usize> ShrAssign<usize> for UInt<LIMBS>
impl<const LIMBS: usize> ShrAssign<usize> for UInt<LIMBS>
source§fn shr_assign(&mut self, rhs: usize)
fn shr_assign(&mut self, rhs: usize)
>>=
operation. Read more