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 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 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_uint_array(arr: [LimbUInt; LIMBS]) -> Self
pub const fn from_uint_array(arr: [LimbUInt; LIMBS]) -> Self
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 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.
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