Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
|
Arbitrary precision integer type. More...
#include <big_int.hpp>
Public Types | |
enum | sign_t { negative = 0 , positive = 1 } |
Sign symbol definitions for positive and negative numbers. More... | |
Public Member Functions | |
BigInt () noexcept=default | |
BigInt (BigInt &&other) noexcept | |
BigInt (const BigInt &o) noexcept=default | |
BigInt (const std::string &str) | |
Construct a big_int_t from a string encoded as hexadecimal or decimal. More... | |
BigInt (const uint8_t buf[], size_t byte_len, const lb_endian_t byte_order) | |
Create a big_int_t from an integer in a byte array with given byte_len, considering the given byte_order. More... | |
BigInt (std::vector< mp_word_t > &&other_reg) noexcept | |
BigInt (uint64_t n) | |
Create big_int_t from an unsigned 64 bit integer. More... | |
~BigInt () noexcept=default | |
BigInt | abs () const noexcept |
Returns absolute (positive) value of this instance. More... | |
size_t | binary_encode (uint8_t output[], size_t byte_len, const lb_endian_t byte_order) const noexcept |
Stores this number to the value in buf with given byte_len, considering the given byte_order. More... | |
size_t | bits () const noexcept |
Returns bit length of this integer. More... | |
uint8_t | byte_at (size_t n) const noexcept |
size_t | bytes () const |
Returns byte length of this integer. More... | |
void | clear () |
Zeroize the big_int. More... | |
int | compare (const BigInt &b) const noexcept |
Compares this instance against other considering both sign() value Returns. More... | |
void | conditionally_set_bit (size_t n, bool set_it) noexcept |
Conditionally set bit at specified position. More... | |
const mp_word_t * | data () const |
Return a const pointer to the register. More... | |
BigInt & | flip_sign () noexcept |
Flip the sign of this big_int. More... | |
bool | get_bit (size_t n) const noexcept |
Return bit value at specified position. More... | |
bool | is_even () const noexcept |
Test if the integer has an even value. More... | |
bool | is_negative () const noexcept |
Tests if the sign of the integer is negative. More... | |
bool | is_nonzero () const noexcept |
Test if the integer is not zero. More... | |
bool | is_odd () const noexcept |
Test if the integer has an odd value. More... | |
bool | is_positive () const noexcept |
Tests if the sign of the integer is positive. More... | |
bool | is_zero () const noexcept |
Test if the integer is zero. More... | |
BigInt & | mod_add (const BigInt &y, const BigInt &mod, std::vector< mp_word_t > &ws) |
Set *this to (*this + y) % mod This function assumes *this is >= 0 && < mod. More... | |
BigInt & | mod_mul (uint8_t y, const BigInt &mod, std::vector< mp_word_t > &ws) |
Set *this to (*this * y) % mod This function assumes *this is >= 0 && < mod y should be small, less than 16. More... | |
BigInt | mod_pow (BigInt e, BigInt m) |
Returns (*this)^e % m, or pow(*this, e) % m. More... | |
BigInt & | mod_sub (const BigInt &y, const BigInt &mod, std::vector< mp_word_t > &ws) |
Set *this to (*this - y) % mod This function assumes *this is >= 0 && < mod. More... | |
bool | operator! () const noexcept |
! operator, returns true iff this is zero, otherwise false. More... | |
bool | operator!= (const BigInt &b) const noexcept |
BigInt | operator% (const BigInt &mod) |
BigInt & | operator%= (const BigInt &mod) |
Modulo operator. More... | |
BigInt | operator* (const BigInt &y) noexcept |
BigInt & | operator*= (const BigInt &y) noexcept |
BigInt | operator+ (const BigInt &y) const noexcept |
BigInt & | operator++ () noexcept |
BigInt | operator++ (int) noexcept |
BigInt & | operator+= (const BigInt &y) noexcept |
BigInt | operator- () const noexcept |
Unary negation operator, returns new negative instance of this. More... | |
BigInt | operator- (const BigInt &y) const noexcept |
BigInt & | operator-- () noexcept |
BigInt | operator-- (int) noexcept |
BigInt & | operator-= (const BigInt &y) noexcept |
BigInt | operator/ (const BigInt &y) const |
BigInt & | operator/= (const BigInt &y) |
bool | operator< (const BigInt &b) const noexcept |
BigInt | operator<< (size_t shift) const |
BigInt & | operator<<= (size_t shift) noexcept |
bool | operator<= (const BigInt &b) const noexcept |
BigInt & | operator= (BigInt &&other) noexcept |
BigInt & | operator= (const BigInt &r)=default |
bool | operator== (const BigInt &b) const noexcept |
bool | operator> (const BigInt &b) const noexcept |
bool | operator>= (const BigInt &b) const noexcept |
BigInt | operator>> (size_t shift) const |
BigInt & | operator>>= (size_t shift) noexcept |
BigInt | pow (BigInt e) |
Returns (*this)^e, or pow(*this, e) More... | |
BigInt & | rev_sub (const BigInt &y, std::vector< mp_word_t > &ws) |
Set *this to y - *this. More... | |
sign_t | reverse_sign () const noexcept |
void | set_bit (size_t n) noexcept |
Set bit at specified position. More... | |
BigInt & | set_sign (sign_t sign) noexcept |
Set sign of the integer. More... | |
size_t | sig_words () const noexcept |
Return how many words we need to hold this value. More... | |
sign_t | sign () const noexcept |
Return the sign of the integer. More... | |
size_t | size () const noexcept |
Give size of internal register. More... | |
BigInt & | square (std::vector< mp_word_t > &ws) |
Square value of *this. More... | |
void | swap (BigInt &other) noexcept |
Swap this value with another. More... | |
std::string | to_dec_string (bool add_details=false) const noexcept |
std::string | to_hex_string (bool add_details=false) const noexcept |
mp_word_t | word_at (size_t n) const noexcept |
Return the mp_word_t at a specified position of the internal register. More... | |
Static Public Member Functions | |
static BigInt | from_s32 (int32_t n) |
Create big_int from a signed 32 bit integer. More... | |
static BigInt | from_u64 (uint64_t n) |
Create big_int from an unsigned 64 bit integer. More... | |
static BigInt | from_word (mp_word_t n) |
Create big_int from a mp_word_t (limb) More... | |
static BigInt | one () |
Create a 1-value big_int. More... | |
static BigInt | power_of_2 (size_t n) |
Create a power of two. More... | |
static BigInt | with_capacity (size_t n) |
Create big_int of specified size, all zeros. More... | |
static BigInt | zero () |
Create a 0-value big_int. More... | |
Arbitrary precision integer type.
Internally the big integer is stored in an array of mp_word_t ordered little-endian alike, with the least significant word at the array-bottom and most significant word at the array-top.
The mp_word_t itself is stored in jau::endian::native!
Definition at line 43 of file big_int.hpp.
Sign symbol definitions for positive and negative numbers.
Enumerator | |
---|---|
negative | |
positive |
Definition at line 48 of file big_int.hpp.
|
defaultnoexcept |
|
defaultnoexcept |
|
defaultnoexcept |
|
inline |
Create big_int_t from an unsigned 64 bit integer.
n | initial value of this big_int_t |
Definition at line 129 of file big_int.hpp.
|
inline |
Construct a big_int_t from a string encoded as hexadecimal or decimal.
Both number bases may lead a -
, denoting a negative number.
Hexadecimal is detected by a leading 0x
.
Definition at line 145 of file big_int.hpp.
|
inline |
Create a big_int_t from an integer in a byte array with given byte_len, considering the given byte_order.
The value is stored in the local storage format, see bigint_storage_format
buf | the byte array holding the value |
byte_len | size of buf in bytes |
littleEndian |
Definition at line 178 of file big_int.hpp.
|
inlinenoexcept |
Definition at line 182 of file big_int.hpp.
|
inlinenoexcept |
Definition at line 186 of file big_int.hpp.
|
inlinestatic |
Create a 0-value big_int.
Definition at line 60 of file big_int.hpp.
|
inlinestatic |
Create a 1-value big_int.
Definition at line 65 of file big_int.hpp.
|
inlinestatic |
Create big_int from an unsigned 64 bit integer.
n | initial value of this big_int |
Definition at line 71 of file big_int.hpp.
Create big_int from a mp_word_t (limb)
n | initial value of this big_int |
Definition at line 86 of file big_int.hpp.
|
inlinestatic |
Create big_int from a signed 32 bit integer.
n | initial value of this big_int |
Definition at line 96 of file big_int.hpp.
|
inlinestatic |
Create big_int of specified size, all zeros.
n | size of the internal register in words |
Definition at line 108 of file big_int.hpp.
|
inlinestatic |
Create a power of two.
n | the power of two to create |
Definition at line 119 of file big_int.hpp.
Definition at line 192 of file big_int.hpp.
|
inlinenoexcept |
Swap this value with another.
other | big_int to swap values with |
Definition at line 203 of file big_int.hpp.
|
inlinenoexcept |
Unary negation operator, returns new negative instance of this.
Definition at line 216 of file big_int.hpp.
|
inlinenoexcept |
n | the offset to get a byte from |
Definition at line 222 of file big_int.hpp.
|
inlinenoexcept |
Return the mp_word_t at a specified position of the internal register.
n | position in the register |
Definition at line 232 of file big_int.hpp.
|
inline |
Return a const pointer to the register.
Definition at line 240 of file big_int.hpp.
|
inlinenoexcept |
Tests if the sign of the integer is negative.
Definition at line 246 of file big_int.hpp.
|
inlinenoexcept |
Tests if the sign of the integer is positive.
Definition at line 252 of file big_int.hpp.
|
inlinenoexcept |
Return the sign of the integer.
Definition at line 258 of file big_int.hpp.
|
inlinenoexcept |
Definition at line 263 of file big_int.hpp.
|
inlinenoexcept |
Flip the sign of this big_int.
Definition at line 273 of file big_int.hpp.
Set sign of the integer.
sign | new Sign to set |
Definition at line 281 of file big_int.hpp.
|
inlinenoexcept |
Returns absolute (positive) value of this instance.
Definition at line 290 of file big_int.hpp.
|
inlinenoexcept |
Give size of internal register.
Definition at line 298 of file big_int.hpp.
|
inlinenoexcept |
Return how many words we need to hold this value.
Definition at line 304 of file big_int.hpp.
|
inline |
Returns byte length of this integer.
Definition at line 307 of file big_int.hpp.
|
inlinenoexcept |
Returns bit length of this integer.
Definition at line 310 of file big_int.hpp.
|
inline |
Zeroize the big_int.
The size of the underlying register is not modified.
Definition at line 324 of file big_int.hpp.
|
inlinenoexcept |
Compares this instance against other considering both sign() value Returns.
Definition at line 333 of file big_int.hpp.
|
inlinenoexcept |
Test if the integer has an even value.
Definition at line 341 of file big_int.hpp.
|
inlinenoexcept |
Test if the integer has an odd value.
Definition at line 347 of file big_int.hpp.
|
inlinenoexcept |
Test if the integer is not zero.
Definition at line 353 of file big_int.hpp.
|
inlinenoexcept |
Test if the integer is zero.
Definition at line 359 of file big_int.hpp.
|
inlinenoexcept |
Return bit value at specified position.
n | the bit offset to test |
Definition at line 368 of file big_int.hpp.
|
inlinenoexcept |
Set bit at specified position.
n | bit position to set |
Definition at line 376 of file big_int.hpp.
|
inlinenoexcept |
Conditionally set bit at specified position.
Note if set_it is false, nothing happens, and if the bit is already set, it remains set.
n | bit position to set |
set_it | if the bit should be set |
Definition at line 388 of file big_int.hpp.
|
inlinenoexcept |
! operator, returns true
iff this is zero, otherwise false.
Definition at line 395 of file big_int.hpp.
|
inlinenoexcept |
Definition at line 397 of file big_int.hpp.
|
inlinenoexcept |
Definition at line 398 of file big_int.hpp.
|
inlinenoexcept |
Definition at line 399 of file big_int.hpp.
|
inlinenoexcept |
Definition at line 400 of file big_int.hpp.
|
inlinenoexcept |
Definition at line 401 of file big_int.hpp.
|
inlinenoexcept |
Definition at line 402 of file big_int.hpp.
|
inlinenoexcept |
Definition at line 410 of file big_int.hpp.
|
inlinenoexcept |
Definition at line 412 of file big_int.hpp.
|
inlinenoexcept |
Definition at line 414 of file big_int.hpp.
|
inlinenoexcept |
Definition at line 416 of file big_int.hpp.
Definition at line 418 of file big_int.hpp.
Definition at line 422 of file big_int.hpp.
Definition at line 426 of file big_int.hpp.
Definition at line 430 of file big_int.hpp.
|
inlinenoexcept |
Definition at line 434 of file big_int.hpp.
|
inlinenoexcept |
Definition at line 450 of file big_int.hpp.
|
inline |
Definition at line 462 of file big_int.hpp.
|
inline |
Definition at line 473 of file big_int.hpp.
Definition at line 494 of file big_int.hpp.
Definition at line 499 of file big_int.hpp.
Definition at line 518 of file big_int.hpp.
Definition at line 527 of file big_int.hpp.
Modulo operator.
y | the modulus to reduce this by |
Definition at line 539 of file big_int.hpp.
Definition at line 543 of file big_int.hpp.
Returns (*this)^e, or pow(*this, e)
Implementation is not optimized and naive, i.e. O(n)
e | the exponent |
Definition at line 568 of file big_int.hpp.
Returns (*this)^e % m, or pow(*this, e) % m.
Implementation is not optimized and naive, i.e. O(n)
e | the exponent |
Definition at line 602 of file big_int.hpp.
Square value of *this.
ws | a temp workspace |
Set *this to y - *this.
y | the big_int_t to subtract from |
ws | a temp workspace |
BigInt & jau::mp::BigInt::mod_add | ( | const BigInt & | y, |
const BigInt & | mod, | ||
std::vector< mp_word_t > & | ws | ||
) |
Set *this to (*this + y) % mod This function assumes *this is >= 0 && < mod.
y | the big_int_t to add - assumed y >= 0 and y < mod |
mod | the positive modulus |
ws | a temp workspace |
BigInt & jau::mp::BigInt::mod_sub | ( | const BigInt & | y, |
const BigInt & | mod, | ||
std::vector< mp_word_t > & | ws | ||
) |
Set *this to (*this - y) % mod This function assumes *this is >= 0 && < mod.
y | the big_int_t to subtract - assumed y >= 0 and y < mod |
mod | the positive modulus |
ws | a temp workspace |
Set *this to (*this * y) % mod This function assumes *this is >= 0 && < mod y should be small, less than 16.
y | the small integer to multiply by |
mod | the positive modulus |
ws | a temp workspace |
|
inlinenoexcept |
rng | a random number generator |
min | the minimum value (must be non-negative) |
max | the maximum value (must be non-negative and > min) |
Definition at line 681 of file big_int.hpp.
|
inlinenoexcept |
|
inlinenoexcept |
Stores this number to the value in buf with given byte_len, considering the given byte_order.
The value is read from the local storage in its format, see bigint_storage_format
If byte_len is less than the byt-esize of this integer, i.e. bytes(), then it will be truncated.
If byte_len is greater than the byte-size of this integer, i.e. bytes(), it will be zero-padded.
Definition at line 1119 of file big_int.hpp.