purify
C++ Purify implementation with native circuit and BPP support
Loading...
Searching...
No Matches
purify::BigUInt< Words > Struct Template Reference

Little-endian fixed-width unsigned integer with simple arithmetic utilities. More...

#include <numeric.hpp>

Public Member Functions

bool is_zero () const
 Returns true when all limbs are zero.
 
int compare (const BigUInt &other) const
 Compares two fixed-width integers using unsigned ordering.
 
bool operator== (const BigUInt &other) const
 
bool operator!= (const BigUInt &other) const
 
bool operator< (const BigUInt &other) const
 
bool operator>= (const BigUInt &other) const
 
bool try_add_small (std::uint32_t value)
 Adds a small unsigned value in place when the result fits.
 
void add_small (std::uint32_t value)
 Adds a small unsigned value in place.
 
bool try_mul_small (std::uint32_t value)
 Multiplies by a small unsigned value in place when the result fits.
 
void mul_small (std::uint32_t value)
 Multiplies by a small unsigned value in place.
 
bool try_add_assign (const BigUInt &other)
 Adds another fixed-width integer when the result fits.
 
void add_assign (const BigUInt &other)
 Adds another fixed-width integer in place.
 
bool try_sub_assign (const BigUInt &other)
 Subtracts another fixed-width integer when the minuend is large enough.
 
void sub_assign (const BigUInt &other)
 Subtracts another fixed-width integer in place.
 
std::size_t bit_length () const
 Returns the index of the highest set bit plus one.
 
bool bit (std::size_t index) const
 Returns the bit at the given little-endian bit index.
 
bool try_set_bit (std::size_t index)
 Sets the bit at the given little-endian bit index when it is in range.
 
void set_bit (std::size_t index)
 Sets the bit at the given little-endian bit index.
 
BigUInt shifted_left (std::size_t bits) const
 Returns a copy shifted left by the requested bit count.
 
BigUInt shifted_right (std::size_t bits) const
 Returns a copy shifted right by the requested bit count.
 
void shift_right_one ()
 Shifts the value right by one bit in place.
 
void mask_bits (std::size_t bits)
 Clears all bits above the requested width.
 
std::uint32_t divmod_small (std::uint32_t divisor)
 Divides by a small unsigned value in place and returns the remainder.
 
std::array< unsigned char, Words *8 > to_bytes_be () const
 Serializes the value to a fixed-width big-endian byte array.
 
std::string to_hex () const
 Formats the value as lowercase hexadecimal without leading zero padding.
 
std::string to_decimal () const
 Formats the value as an unsigned decimal string.
 

Static Public Member Functions

static BigUInt zero ()
 Returns the additive identity.
 
static BigUInt one ()
 Returns the multiplicative identity.
 
static BigUInt from_u64 (std::uint64_t value)
 Constructs a value from a single 64-bit limb.
 
static BigUInt from_bytes_be (const unsigned char *data, std::size_t size)
 Parses a big-endian byte string into the fixed-width integer.
 
static Result< BigUInttry_from_hex (std::string_view hex)
 Parses a hexadecimal string, ignoring optional 0x and whitespace.
 
static BigUInt from_hex (std::string_view hex)
 Parses a hexadecimal string with the precondition that the value fits exactly.
 

Data Fields

std::array< std::uint64_t, Words > limbs {}
 

Detailed Description

template<std::size_t Words>
struct purify::BigUInt< Words >
Template Parameters
WordsNumber of 64-bit limbs stored in the integer.

Definition at line 200 of file numeric.hpp.

Member Function Documentation

◆ add_assign()

template<std::size_t Words>
void purify::BigUInt< Words >::add_assign ( const BigUInt< Words > &  other)
inline

Precondition: the sum fits in the fixed-width representation.

Definition at line 431 of file numeric.hpp.

References purify::BigUInt< Words >::try_add_assign().

◆ add_small()

template<std::size_t Words>
void purify::BigUInt< Words >::add_small ( std::uint32_t  value)
inline

Precondition: the sum fits in the fixed-width representation.

Definition at line 361 of file numeric.hpp.

References purify::BigUInt< Words >::try_add_small().

Referenced by purify::BigUInt< Words >::from_bytes_be().

◆ bit()

template<std::size_t Words>
bool purify::BigUInt< Words >::bit ( std::size_t  index) const
inline

Definition at line 489 of file numeric.hpp.

References purify::BigUInt< Words >::limbs.

◆ bit_length()

template<std::size_t Words>
std::size_t purify::BigUInt< Words >::bit_length ( ) const
inline

◆ compare()

template<std::size_t Words>
int purify::BigUInt< Words >::compare ( const BigUInt< Words > &  other) const
inline

◆ divmod_small()

template<std::size_t Words>
std::uint32_t purify::BigUInt< Words >::divmod_small ( std::uint32_t  divisor)
inline

◆ from_bytes_be()

template<std::size_t Words>
static BigUInt purify::BigUInt< Words >::from_bytes_be ( const unsigned char *  data,
std::size_t  size 
)
inlinestatic

◆ from_hex()

template<std::size_t Words>
static BigUInt purify::BigUInt< Words >::from_hex ( std::string_view  hex)
inlinestatic

Call try_from_hex() when the input may be user-controlled.

Definition at line 280 of file numeric.hpp.

References purify::Expected< T, E >::has_value(), and purify::BigUInt< Words >::try_from_hex().

◆ from_u64()

template<std::size_t Words>
static BigUInt purify::BigUInt< Words >::from_u64 ( std::uint64_t  value)
inlinestatic

Definition at line 224 of file numeric.hpp.

References purify::BigUInt< Words >::limbs.

◆ is_zero()

template<std::size_t Words>
bool purify::BigUInt< Words >::is_zero ( ) const
inline

◆ mask_bits()

template<std::size_t Words>
void purify::BigUInt< Words >::mask_bits ( std::size_t  bits)
inline

Definition at line 586 of file numeric.hpp.

References purify::BigUInt< Words >::limbs.

Referenced by purify::random_below().

◆ mul_small()

template<std::size_t Words>
void purify::BigUInt< Words >::mul_small ( std::uint32_t  value)
inline

Precondition: the product fits in the fixed-width representation.

Definition at line 395 of file numeric.hpp.

References purify::BigUInt< Words >::try_mul_small().

Referenced by purify::BigUInt< Words >::from_bytes_be().

◆ one()

template<std::size_t Words>
static BigUInt purify::BigUInt< Words >::one ( )
inlinestatic

Definition at line 213 of file numeric.hpp.

References purify::BigUInt< Words >::limbs.

◆ operator!=()

template<std::size_t Words>
bool purify::BigUInt< Words >::operator!= ( const BigUInt< Words > &  other) const
inline

Definition at line 322 of file numeric.hpp.

◆ operator<()

template<std::size_t Words>
bool purify::BigUInt< Words >::operator< ( const BigUInt< Words > &  other) const
inline

Definition at line 326 of file numeric.hpp.

References purify::BigUInt< Words >::compare().

◆ operator==()

template<std::size_t Words>
bool purify::BigUInt< Words >::operator== ( const BigUInt< Words > &  other) const
inline

Definition at line 318 of file numeric.hpp.

References purify::BigUInt< Words >::limbs.

◆ operator>=()

template<std::size_t Words>
bool purify::BigUInt< Words >::operator>= ( const BigUInt< Words > &  other) const
inline

Definition at line 330 of file numeric.hpp.

References purify::BigUInt< Words >::compare().

◆ set_bit()

template<std::size_t Words>
void purify::BigUInt< Words >::set_bit ( std::size_t  index)
inline

Precondition: index < Words * 64.

Definition at line 522 of file numeric.hpp.

References purify::BigUInt< Words >::try_set_bit().

◆ shift_right_one()

template<std::size_t Words>
void purify::BigUInt< Words >::shift_right_one ( )
inline

Definition at line 574 of file numeric.hpp.

References purify::BigUInt< Words >::limbs.

Referenced by purify::try_divmod_same().

◆ shifted_left()

template<std::size_t Words>
BigUInt purify::BigUInt< Words >::shifted_left ( std::size_t  bits) const
inline

Definition at line 529 of file numeric.hpp.

References purify::BigUInt< Words >::limbs.

Referenced by purify::try_divmod_same().

◆ shifted_right()

template<std::size_t Words>
BigUInt purify::BigUInt< Words >::shifted_right ( std::size_t  bits) const
inline

Definition at line 552 of file numeric.hpp.

References purify::BigUInt< Words >::limbs.

◆ sub_assign()

template<std::size_t Words>
void purify::BigUInt< Words >::sub_assign ( const BigUInt< Words > &  other)
inline

Precondition: *this >= other.

Definition at line 467 of file numeric.hpp.

References purify::BigUInt< Words >::try_sub_assign().

Referenced by purify::capi_detail::secp256k1_order_minus_one().

◆ to_bytes_be()

◆ to_decimal()

template<std::size_t Words>
std::string purify::BigUInt< Words >::to_decimal ( ) const
inline

◆ to_hex()

template<std::size_t Words>
std::string purify::BigUInt< Words >::to_hex ( ) const
inline

Definition at line 638 of file numeric.hpp.

References purify::BigUInt< Words >::to_bytes_be().

Referenced by purify::FieldElement::to_hex().

◆ try_add_assign()

template<std::size_t Words>
bool purify::BigUInt< Words >::try_add_assign ( const BigUInt< Words > &  other)
inline

Definition at line 402 of file numeric.hpp.

References purify::BigUInt< Words >::limbs.

Referenced by purify::BigUInt< Words >::add_assign().

◆ try_add_small()

template<std::size_t Words>
bool purify::BigUInt< Words >::try_add_small ( std::uint32_t  value)
inline

◆ try_from_hex()

template<std::size_t Words>
static Result< BigUInt > purify::BigUInt< Words >::try_from_hex ( std::string_view  hex)
inlinestatic

◆ try_mul_small()

◆ try_set_bit()

template<std::size_t Words>
bool purify::BigUInt< Words >::try_set_bit ( std::size_t  index)
inline

◆ try_sub_assign()

template<std::size_t Words>
bool purify::BigUInt< Words >::try_sub_assign ( const BigUInt< Words > &  other)
inline

◆ zero()

template<std::size_t Words>
static BigUInt purify::BigUInt< Words >::zero ( )
inlinestatic

Definition at line 204 of file numeric.hpp.

References purify::BigUInt< Words >::limbs.

Field Documentation

◆ limbs


The documentation for this struct was generated from the following file: