Struct sp_arithmetic::biguint::BigUint
source · pub struct BigUint { /* private fields */ }
Expand description
Simple wrapper around an infinitely large integer, represented as limbs of Single
.
Implementations§
source§impl BigUint
impl BigUint
sourcepub fn with_capacity(size: usize) -> Self
pub fn with_capacity(size: usize) -> Self
Create a new instance with size
limbs. This prevents any number with zero limbs to be
created.
The behavior of the type is undefined with zero limbs.
sourcepub fn from_limbs(limbs: &[Single]) -> Self
pub fn from_limbs(limbs: &[Single]) -> Self
Raw constructor from custom limbs. If limbs
is empty, Zero::zero()
implementation is
used.
sourcepub fn get(&self, index: usize) -> Single
pub fn get(&self, index: usize) -> Single
A naive getter for limb at index
. Note that the order is lsb -> msb.
Panics
This panics if index is out of range.
sourcepub fn checked_get(&self, index: usize) -> Option<Single>
pub fn checked_get(&self, index: usize) -> Option<Single>
A naive getter for limb at index
. Note that the order is lsb -> msb.
sourcepub fn set(&mut self, index: usize, value: Single)
pub fn set(&mut self, index: usize, value: Single)
A naive setter for limb at index
. Note that the order is lsb -> msb.
Panics
This panics if index is out of range.
sourcepub fn lsb(&self) -> Single
pub fn lsb(&self) -> Single
returns the least significant limb of the number.
Panics
While the constructor of the type prevents this, this can panic if self
has no digits.
sourcepub fn msb(&self) -> Single
pub fn msb(&self) -> Single
returns the most significant limb of the number.
Panics
While the constructor of the type prevents this, this can panic if self
has no digits.
sourcepub fn lstrip(&mut self)
pub fn lstrip(&mut self)
Strips zeros from the left side (the most significant limbs) of self
, if any.
sourcepub fn lpad(&mut self, size: usize)
pub fn lpad(&mut self, size: usize)
Zero-pad self
from left to reach size
limbs. Will not make any difference if self
is already bigger than size
limbs.
sourcepub fn add(self, other: &Self) -> Self
pub fn add(self, other: &Self) -> Self
Adds self
with other
. self and other do not have to have any particular size. Given
that the n = max{size(self), size(other)}
, it will produce a number with n + 1
limbs.
This function does not strip the output and returns the original allocated n + 1
limbs. The caller may strip the output if desired.
Taken from “The Art of Computer Programming” by D.E. Knuth, vol 2, chapter 4.
sourcepub fn sub(self, other: &Self) -> Result<Self, Self>
pub fn sub(self, other: &Self) -> Result<Self, Self>
Subtracts other
from self
. self and other do not have to have any particular size.
Given that the n = max{size(self), size(other)}
, it will produce a number of size n
.
If other
is bigger than self
, Err(B - borrow)
is returned.
Taken from “The Art of Computer Programming” by D.E. Knuth, vol 2, chapter 4.
sourcepub fn mul(self, other: &Self) -> Self
pub fn mul(self, other: &Self) -> Self
Multiplies n-limb number self
with m-limb number other
.
The resulting number will always have n + m
limbs.
This function does not strip the output and returns the original allocated n + m
limbs. The caller may strip the output if desired.
Taken from “The Art of Computer Programming” by D.E. Knuth, vol 2, chapter 4.
sourcepub fn div_unit(self, other: Single) -> Self
pub fn div_unit(self, other: Single) -> Self
Divides self
by a single limb other
. This can be used in cases where the original
division cannot work due to the divisor (other
) being just one limb.
Invariant: other
cannot be zero.
sourcepub fn div(self, other: &Self, rem: bool) -> Option<(Self, Self)>
pub fn div(self, other: &Self, rem: bool) -> Option<(Self, Self)>
Divides an n + m
limb self by a n
limb other
. The result is a m + 1
limb
quotient and a n
limb remainder, if enabled by passing true
in rem
argument, both
in the form of an option’s Ok
.
- requires
other
to be stripped and have no leading zeros. - requires
self
to be stripped and have no leading zeros. - requires
other
to have at least two limbs. - requires
self
to have a greater length compared toother
.
All arguments are examined without being stripped for the above conditions. If any of
the above fails, None
is returned.`
Taken from “The Art of Computer Programming” by D.E. Knuth, vol 2, chapter 4.
Trait Implementations§
source§impl Decode for BigUint
impl Decode for BigUint
source§fn decode<__CodecInputEdqy: Input>(
__codec_input_edqy: &mut __CodecInputEdqy
) -> Result<Self, Error>
fn decode<__CodecInputEdqy: Input>( __codec_input_edqy: &mut __CodecInputEdqy ) -> Result<Self, Error>
source§fn decode_into<I>(
input: &mut I,
dst: &mut MaybeUninit<Self>
) -> Result<DecodeFinished, Error>where
I: Input,
fn decode_into<I>( input: &mut I, dst: &mut MaybeUninit<Self> ) -> Result<DecodeFinished, Error>where I: Input,
source§impl Encode for BigUint
impl Encode for BigUint
source§fn size_hint(&self) -> usize
fn size_hint(&self) -> usize
source§fn encode_to<__CodecOutputEdqy: Output + ?Sized>(
&self,
__codec_dest_edqy: &mut __CodecOutputEdqy
)
fn encode_to<__CodecOutputEdqy: Output + ?Sized>( &self, __codec_dest_edqy: &mut __CodecOutputEdqy )
source§fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R
source§fn encoded_size(&self) -> usize
fn encoded_size(&self) -> usize
source§impl Ord for BigUint
impl Ord for BigUint
source§impl PartialEq for BigUint
impl PartialEq for BigUint
source§impl PartialOrd for BigUint
impl PartialOrd for BigUint
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 moreimpl EncodeLike for BigUint
impl Eq for BigUint
Auto Trait Implementations§
impl RefUnwindSafe for BigUint
impl Send for BigUint
impl Sync for BigUint
impl Unpin for BigUint
impl UnwindSafe for BigUint
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> DecodeLimit for Twhere
T: Decode,
impl<T> DecodeLimit for Twhere T: Decode,
source§impl<T, S> EnsureFrom<S> for Twhere
T: TryFrom<S> + PartialOrd + Zero,
S: PartialOrd + Zero,
impl<T, S> EnsureFrom<S> for Twhere T: TryFrom<S> + PartialOrd + Zero, S: PartialOrd + Zero,
source§fn ensure_from(other: T) -> Result<Self, ArithmeticError>
fn ensure_from(other: T) -> Result<Self, ArithmeticError>
ArithmeticError
if fails. Read moresource§impl<T, S> EnsureInto<S> for Twhere
T: TryInto<S> + PartialOrd + Zero,
S: PartialOrd + Zero,
impl<T, S> EnsureInto<S> for Twhere T: TryInto<S> + PartialOrd + Zero, S: PartialOrd + Zero,
source§fn ensure_into(self) -> Result<T, ArithmeticError>
fn ensure_into(self) -> Result<T, ArithmeticError>
ArithmeticError
if fails. Read moresource§impl<T> SaturatedConversion for T
impl<T> SaturatedConversion for T
source§fn saturated_from<T>(t: T) -> Selfwhere
Self: UniqueSaturatedFrom<T>,
fn saturated_from<T>(t: T) -> Selfwhere Self: UniqueSaturatedFrom<T>,
source§fn saturated_into<T>(self) -> Twhere
Self: UniqueSaturatedInto<T>,
fn saturated_into<T>(self) -> Twhere Self: UniqueSaturatedInto<T>,
T
. Read moresource§impl<T, S> UniqueSaturatedInto<T> for Swhere
T: Bounded,
S: TryInto<T>,
impl<T, S> UniqueSaturatedInto<T> for Swhere T: Bounded, S: TryInto<T>,
source§fn unique_saturated_into(self) -> T
fn unique_saturated_into(self) -> T
T
.