Struct cryptix_bigint::BigUInt

source ·
pub struct BigUInt<T, const LEN: usize>(pub [T; LEN]);
Expand description

Stack-based data structure for big unsigned integers

  • The inner structure is an fixed-length array, no memory overhead
  • It stores number in little-endian, which means lower digits first
  • Typically T is a primary integer type

Tuple Fields§

§0: [T; LEN]

Implementations§

source§

impl<T, const LEN: usize> BigUInt<T, LEN>where T: Copy + Default + Digit,

source

pub fn mul_dig(self, rhs: T) -> BigUInt<T, { _ }>

source§

impl<T: Digit, const LEN: usize> BigUInt<T, LEN>where Self: IsBigInt,

source

pub const fn from_bytes(value: &[u8]) -> Self

source§

impl<T: Copy + Default + Digit, const LEN: usize> BigUInt<T, LEN>

source

pub fn resize<const L: usize>(self) -> BigUInt<T, L>

resize a biguint, expand and set high bits to 0 if new length is larger, shrink and cut off higher bits if new length is smaller

Methods from Deref<Target = [T; LEN]>§

1.57.0 · source

pub fn as_slice(&self) -> &[T]

Returns a slice containing the entire array. Equivalent to &s[..].

1.57.0 · source

pub fn as_mut_slice(&mut self) -> &mut [T]

Returns a mutable slice containing the entire array. Equivalent to &mut s[..].

source

pub fn each_ref(&self) -> [&T; N]

🔬This is a nightly-only experimental API. (array_methods)

Borrows each element and returns an array of references with the same size as self.

Example
#![feature(array_methods)]

let floats = [3.1, 2.7, -1.0];
let float_refs: [&f64; 3] = floats.each_ref();
assert_eq!(float_refs, [&3.1, &2.7, &-1.0]);

This method is particularly useful if combined with other methods, like map. This way, you can avoid moving the original array if its elements are not Copy.

#![feature(array_methods)]

let strings = ["Ferris".to_string(), "♥".to_string(), "Rust".to_string()];
let is_ascii = strings.each_ref().map(|s| s.is_ascii());
assert_eq!(is_ascii, [true, false, true]);

// We can still access the original array: it has not been moved.
assert_eq!(strings.len(), 3);
source

pub fn each_mut(&mut self) -> [&mut T; N]

🔬This is a nightly-only experimental API. (array_methods)

Borrows each element mutably and returns an array of mutable references with the same size as self.

Example
#![feature(array_methods)]

let mut floats = [3.1, 2.7, -1.0];
let float_refs: [&mut f64; 3] = floats.each_mut();
*float_refs[0] = 0.0;
assert_eq!(float_refs, [&mut 0.0, &mut 2.7, &mut -1.0]);
assert_eq!(floats, [0.0, 2.7, -1.0]);
source

pub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T])

🔬This is a nightly-only experimental API. (split_array)

Divides one array reference into two at an index.

The first will contain all indices from [0, M) (excluding the index M itself) and the second will contain all indices from [M, N) (excluding the index N itself).

Panics

Panics if M > N.

Examples
#![feature(split_array)]

let v = [1, 2, 3, 4, 5, 6];

{
   let (left, right) = v.split_array_ref::<0>();
   assert_eq!(left, &[]);
   assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
}

{
    let (left, right) = v.split_array_ref::<2>();
    assert_eq!(left, &[1, 2]);
    assert_eq!(right, &[3, 4, 5, 6]);
}

{
    let (left, right) = v.split_array_ref::<6>();
    assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
    assert_eq!(right, &[]);
}
source

pub fn split_array_mut<const M: usize>(&mut self) -> (&mut [T; M], &mut [T])

🔬This is a nightly-only experimental API. (split_array)

Divides one mutable array reference into two at an index.

The first will contain all indices from [0, M) (excluding the index M itself) and the second will contain all indices from [M, N) (excluding the index N itself).

Panics

Panics if M > N.

Examples
#![feature(split_array)]

let mut v = [1, 0, 3, 0, 5, 6];
let (left, right) = v.split_array_mut::<2>();
assert_eq!(left, &mut [1, 0][..]);
assert_eq!(right, &mut [3, 0, 5, 6]);
left[1] = 2;
right[1] = 4;
assert_eq!(v, [1, 2, 3, 4, 5, 6]);
source

pub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M])

🔬This is a nightly-only experimental API. (split_array)

Divides one array reference into two at an index from the end.

The first will contain all indices from [0, N - M) (excluding the index N - M itself) and the second will contain all indices from [N - M, N) (excluding the index N itself).

Panics

Panics if M > N.

Examples
#![feature(split_array)]

let v = [1, 2, 3, 4, 5, 6];

{
   let (left, right) = v.rsplit_array_ref::<0>();
   assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
   assert_eq!(right, &[]);
}

{
    let (left, right) = v.rsplit_array_ref::<2>();
    assert_eq!(left, &[1, 2, 3, 4]);
    assert_eq!(right, &[5, 6]);
}

{
    let (left, right) = v.rsplit_array_ref::<6>();
    assert_eq!(left, &[]);
    assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
}
source

pub fn rsplit_array_mut<const M: usize>(&mut self) -> (&mut [T], &mut [T; M])

🔬This is a nightly-only experimental API. (split_array)

Divides one mutable array reference into two at an index from the end.

The first will contain all indices from [0, N - M) (excluding the index N - M itself) and the second will contain all indices from [N - M, N) (excluding the index N itself).

Panics

Panics if M > N.

Examples
#![feature(split_array)]

let mut v = [1, 0, 3, 0, 5, 6];
let (left, right) = v.rsplit_array_mut::<4>();
assert_eq!(left, &mut [1, 0]);
assert_eq!(right, &mut [3, 0, 5, 6][..]);
left[1] = 2;
right[1] = 4;
assert_eq!(v, [1, 2, 3, 4, 5, 6]);
source

pub fn as_ascii(&self) -> Option<&[AsciiChar; N]>

🔬This is a nightly-only experimental API. (ascii_char)

Converts this array of bytes into a array of ASCII characters, or returns None if any of the characters is non-ASCII.

Examples
#![feature(ascii_char)]
#![feature(const_option)]

const HEX_DIGITS: [std::ascii::Char; 16] =
    *b"0123456789abcdef".as_ascii().unwrap();

assert_eq!(HEX_DIGITS[1].as_str(), "1");
assert_eq!(HEX_DIGITS[10].as_str(), "a");
source

pub unsafe fn as_ascii_unchecked(&self) -> &[AsciiChar; N]

🔬This is a nightly-only experimental API. (ascii_char)

Converts this array of bytes into a array of ASCII characters, without checking whether they’re valid.

Safety

Every byte in the array must be in 0..=127, or else this is UB.

Trait Implementations§

source§

impl<T, const LEN: usize> Add<&BigUInt<T, LEN>> for &BigUInt<T, LEN>where T: Copy + Default + Digit,

§

type Output = (BigUInt<T, LEN>, T)

The resulting type after applying the + operator.
source§

fn add(self, rhs: Self) -> (BigUInt<T, LEN>, T)

Performs the + operation. Read more
source§

impl<T, const LEN: usize> Add<BigUInt<T, LEN>> for &(BigUInt<T, LEN>, T)where T: Copy + Default + Digit,

§

type Output = (BigUInt<T, LEN>, T)

The resulting type after applying the + operator.
source§

fn add(self, rhs: BigUInt<T, LEN>) -> (BigUInt<T, LEN>, T)

Performs the + operation. Read more
source§

impl<T, const LEN: usize> Add<BigUInt<T, LEN>> for (BigUInt<T, LEN>, T)where T: Copy + Default + Digit,

§

type Output = (BigUInt<T, LEN>, T)

The resulting type after applying the + operator.
source§

fn add(self, rhs: BigUInt<T, LEN>) -> (BigUInt<T, LEN>, T)

Performs the + operation. Read more
source§

impl<T, const LEN: usize> Add<BigUInt<T, LEN>> for BigUInt<T, LEN>where T: Copy + Default + Digit,

§

type Output = (BigUInt<T, LEN>, T)

The resulting type after applying the + operator.
source§

fn add(self, rhs: Self) -> (Self, T)

Performs the + operation. Read more
source§

impl<T, const LEN: usize> Add<T> for BigUInt<T, LEN>where T: Copy + Default + Digit,

§

type Output = (BigUInt<T, LEN>, T)

The resulting type after applying the + operator.
source§

fn add(self, rhs: T) -> (Self, T)

Performs the + operation. Read more
source§

impl<T: Clone + Copy, const LEN: usize> Clone for BigUInt<T, LEN>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Digit, const LEN: usize> Debug for BigUInt<T, LEN>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Copy, const LEN: usize> Deref for BigUInt<T, LEN>

§

type Target = [T; LEN]

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<T: Copy, const LEN: usize> DerefMut for BigUInt<T, LEN>

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
source§

impl<T: Digit, const LEN: usize> Display for BigUInt<T, LEN>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Copy + Default, const LEN: usize> From<&[T]> for BigUInt<T, LEN>

source§

fn from(value: &[T]) -> Self

Converts to this type from the input type.
source§

impl<T: Copy, const LEN: usize, const N: usize> From<[BigUInt<T, LEN>; N]> for BigUInt<T, { _ }>

source§

fn from(value: [BigUInt<T, LEN>; N]) -> Self

Converts to this type from the input type.
source§

impl<T: Copy, const LEN: usize, const N: usize> From<BigUInt<T, {N * LEN}>> for [BigUInt<T, LEN>; N]

source§

fn from(value: BigUInt<T, { _ }>) -> Self

Converts to this type from the input type.
source§

impl<T: Copy + Default, const LEN: usize> From<T> for BigUInt<T, LEN>

source§

fn from(value: T) -> Self

Converts to this type from the input type.
source§

impl<T: Digit, const LEN: usize> IsBigInt for BigUInt<T, LEN>

source§

fn bit_nlz(&self) -> usize

number of leading zeros

§

type Dig = T

segment type, typically primitive integer type like u64, u32
source§

const DIG_LEN: usize = LEN

number of segments
source§

const ONE: Self = _

source§

const ZERO: Self = _

source§

fn bit_len(&self) -> usize

big uint length in bit, excluding leading zeros
source§

fn seg_len(&self) -> usize

big uint length in segment, excluding leading zeros
source§

fn bit(&self, idx: usize) -> bool

get the idx-th bit of big integer
source§

fn is_negative(&self) -> bool

check if this biguint is negative, i.e. the MSB is 0
source§

fn is_odd(&self) -> bool

check is self is odd
source§

fn is_zero(&self) -> bool

check if self is zero
source§

fn as_slice(&self) -> &[Self::Dig; Self::DIG_LEN]

get a reference of slice of digs for this bigint
source§

fn from_slice(arr: &[Self::Dig; Self::DIG_LEN]) -> Self

source§

fn to_array(self) -> [Self::Dig; Self::DIG_LEN]

source§

fn from_array(arr: [Self::Dig; Self::DIG_LEN]) -> Self

source§

fn as_bytes(&self) -> &[u8]

source§

fn set_bit(self, idx: usize, bit: bool) -> Self

source§

const DIG_BYTE_LEN: usize = <Self::Dig>::BYTE_LEN

segment length in byte
source§

const DIG_BIT_LEN: usize = _

segment length in bit
source§

const DIG_BIT_LEN_MSK: usize = _

segment bit length mask, for example, if segment is u8 type, its bit length is 8, mask is 0b111 = (1 << 3) - 1
source§

const DIG_BIT_LEN_SHT: usize = _

segment bit length shift amount. uf segment is u8, this value is 3 since 1 << 3 = 8
source§

const DIG_BYTE_LEN_MSK: usize = _

segment byte length mask, u8 segment has 1 byte length, so mask is 0
source§

const DIG_BYTE_LEN_SHT: usize = _

segment byte length mask, u8 segment has 1 byte length, so mask is 0
source§

const BYTE_LEN: usize = _

big integer length in byte
source§

const BIT_LEN: usize = _

big integer length in bit
source§

impl<T, const LEN: usize> Mul<BigUInt<T, LEN>> for BigUInt<T, LEN>where T: Default + Copy + Digit, Self: Mul<T, Output = (Self, T)>, BigUInt<T, { _ }>: Add<Output = (BigUInt<T, { _ }>, T)>,

§

type Output = [BigUInt<T, LEN>; 2]

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
source§

impl<T, const LEN: usize> Mul<T> for BigUInt<T, LEN>where T: Copy + Default + Digit,

§

type Output = (BigUInt<T, LEN>, T)

The resulting type after applying the * operator.
source§

fn mul(self, rhs: T) -> (Self, T)

Performs the * operation. Read more
source§

impl<T, const LEN: usize> Neg for BigUInt<T, LEN>where T: Digit,

§

type Output = BigUInt<T, LEN>

The resulting type after applying the - operator.
source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
source§

impl<T: Copy + Ord, const LEN: usize> Ord for BigUInt<T, LEN>

source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl<T: Copy + PartialEq, const LEN: usize> PartialEq<[BigUInt<T, LEN>; 2]> for BigUInt<T, LEN>where BigUInt<T, LEN>: IsBigInt<Dig = T>,

source§

fn eq(&self, other: &[BigUInt<T, LEN>; 2]) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: Copy + PartialEq, const LEN: usize> PartialEq<BigUInt<T, LEN>> for BigUInt<T, LEN>

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: Copy + PartialOrd, const LEN: usize> PartialOrd<[BigUInt<T, LEN>; 2]> for BigUInt<T, LEN>where BigUInt<T, LEN>: IsBigInt<Dig = T>,

source§

fn partial_cmp(&self, other: &[BigUInt<T, LEN>; 2]) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T: Copy + PartialOrd, const LEN: usize> PartialOrd<BigUInt<T, LEN>> for BigUInt<T, LEN>

source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T, const LEN: usize> Rem<BigUInt<T, LEN>> for BigUInt<T, LEN>where T: Default + Copy + Digit, Self: IsBigInt<Dig = T> + Ord + Sub<Output = (Self, T)>,

§

type Output = BigUInt<T, LEN>

The resulting type after applying the % operator.
source§

fn rem(self, rhs: Self) -> Self::Output

Performs the % operation. Read more
source§

impl<T, const LEN: usize> Shl<usize> for BigUInt<T, LEN>where T: Default + Copy + Digit, Self: IsBigInt<Dig = T>,

source§

fn shl(self, rhs: usize) -> Self::Output

this is overflowing shift for big integers

§

type Output = BigUInt<T, LEN>

The resulting type after applying the << operator.
source§

impl<T, const LEN: usize> Shr<usize> for BigUInt<T, LEN>where T: Default + Copy + Digit, Self: IsBigInt<Dig = T>,

source§

fn shr(self, rhs: usize) -> Self::Output

this is overflowing shift for big integers

§

type Output = BigUInt<T, LEN>

The resulting type after applying the >> operator.
source§

impl<T, const LEN: usize> Sub<BigUInt<T, LEN>> for (BigUInt<T, LEN>, T)where T: Copy + Default + Digit,

§

type Output = (BigUInt<T, LEN>, T)

The resulting type after applying the - operator.
source§

fn sub(self, rhs: BigUInt<T, LEN>) -> (BigUInt<T, LEN>, T)

Performs the - operation. Read more
source§

impl<T, const LEN: usize> Sub<BigUInt<T, LEN>> for BigUInt<T, LEN>where T: Copy + Default + Digit,

source§

fn sub(self, rhs: Self) -> (Self, T)

Calculate self - rhs, and an extra digit indicating if there is a borrow. If a borrows occurs, the extra digit will become -1

§

type Output = (BigUInt<T, LEN>, T)

The resulting type after applying the - operator.
source§

impl<T, const LEN: usize> Sub<T> for BigUInt<T, LEN>where T: Copy + Default + Digit,

§

type Output = (BigUInt<T, LEN>, T)

The resulting type after applying the - operator.
source§

fn sub(self, rhs: <Self as IsBigInt>::Dig) -> (Self, T)

Performs the - operation. Read more
source§

impl<T, const LEN: usize> WrapAround<[BigUInt<T, LEN>; 2]> for BigUInt<T, LEN>where BigUInt<T, { _ }>: Rem<Output = BigUInt<T, { _ }>>, T: Default + Copy + Digit,

source§

fn wrap(self, rhs: [BigUInt<T, LEN>; 2]) -> Self

source§

impl<T, const LEN: usize> WrapAround<BigUInt<T, LEN>> for BigUInt<T, LEN>where T: Default + Copy + Digit,

source§

fn wrap(self, rhs: BigUInt<T, LEN>) -> Self

source§

impl<T: Copy, const LEN: usize> BigIntOps for BigUInt<T, LEN>where Self: IsBigInt + Add<Output = (Self, Self::Dig)> + Add<Self::Dig, Output = (Self, Self::Dig)> + Sub<Output = (Self, Self::Dig)> + Sub<Self::Dig, Output = (Self, Self::Dig)> + Shr<usize, Output = Self> + Shl<usize, Output = Self> + Rem<Output = Self> + Ord + Eq,

source§

impl<T: Copy, const LEN: usize> BigIntOpsExt for BigUInt<T, LEN>where Self: BigIntOps + Mul<Output = [Self; 2]> + Mul<Self::Dig, Output = (Self, Self::Dig)> + WrapAround<[Self; 2]> + PartialEq<[Self; 2]> + PartialOrd<[Self; 2]>,

source§

impl<T: Copy, const LEN: usize> Copy for BigUInt<T, LEN>

source§

impl<T, const LEN: usize> Eq for BigUInt<T, LEN>where BigUInt<T, LEN>: PartialEq,

Auto Trait Implementations§

§

impl<T, const LEN: usize> RefUnwindSafe for BigUInt<T, LEN>where T: RefUnwindSafe,

§

impl<T, const LEN: usize> Send for BigUInt<T, LEN>where T: Send,

§

impl<T, const LEN: usize> Sync for BigUInt<T, LEN>where T: Sync,

§

impl<T, const LEN: usize> Unpin for BigUInt<T, LEN>where T: Unpin,

§

impl<T, const LEN: usize> UnwindSafe for BigUInt<T, LEN>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.