pub type U256 = UInt<{ _ }>;
Expand description
256-bit unsigned big integer.
Aliased Type§
struct U256 { /* private fields */ }
Implementations§
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.
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)
.
Trait Implementations§
Source§impl ArrayEncoding for U256
impl ArrayEncoding for U256
Source§type ByteSize = UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>
type ByteSize = UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>
Source§fn from_be_byte_array(bytes: ByteArray<Self>) -> Self
fn from_be_byte_array(bytes: ByteArray<Self>) -> Self
Source§fn from_le_byte_array(bytes: ByteArray<Self>) -> Self
fn from_le_byte_array(bytes: ByteArray<Self>) -> Self
Source§fn to_be_byte_array(&self) -> ByteArray<Self>
fn to_be_byte_array(&self) -> ByteArray<Self>
Source§fn to_le_byte_array(&self) -> ByteArray<Self>
fn to_le_byte_array(&self) -> ByteArray<Self>
Source§impl Encoding for U256
impl Encoding for U256
Source§fn from_be_bytes(bytes: Self::Repr) -> Self
fn from_be_bytes(bytes: Self::Repr) -> Self
Source§fn from_le_bytes(bytes: Self::Repr) -> Self
fn from_le_bytes(bytes: Self::Repr) -> Self
Source§fn to_be_bytes(&self) -> Self::Repr
fn to_be_bytes(&self) -> Self::Repr
Source§fn to_le_bytes(&self) -> Self::Repr
fn to_le_bytes(&self) -> Self::Repr
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 for UInt<LIMBS>
impl<const LIMBS: usize> BitAndAssign 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 for UInt<LIMBS>
impl<const LIMBS: usize> BitOrAssign 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 for UInt<LIMBS>
impl<const LIMBS: usize> BitXorAssign 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§fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self
Source§fn conditional_assign(&mut self, other: &Self, choice: Choice)
fn conditional_assign(&mut self, other: &Self, choice: Choice)
Source§fn conditional_swap(a: &mut Self, b: &mut Self, choice: Choice)
fn conditional_swap(a: &mut Self, b: &mut Self, choice: Choice)
self
and other
if choice == 1
; otherwise,
reassign both unto themselves. Read moreSource§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> Ord for UInt<LIMBS>
impl<const LIMBS: usize> Ord for UInt<LIMBS>
Source§impl<const LIMBS: usize> PartialOrd for UInt<LIMBS>
impl<const LIMBS: usize> PartialOrd for UInt<LIMBS>
Source§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> 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