Botan 3.6.1
Crypto and TLS for C&
|
#include <bigint.h>
Public Types | |
enum | Base { Decimal = 10 , Hexadecimal = 16 , Binary = 256 } |
enum | Sign { Negative = 0 , Positive = 1 } |
Public Member Functions | |
void | _assign_from_bytes (std::span< const uint8_t > bytes) |
constexpr void | _const_time_poison () const |
constexpr void | _const_time_unpoison () const |
const word * | _data () const |
BigInt | abs () const |
BigInt & | add (const word y[], size_t y_words, Sign sign) |
BigInt ()=default | |
BigInt (BigInt &&other) | |
BigInt (const BigInt &other)=default | |
BigInt (const uint8_t buf[], size_t length) | |
BigInt (const uint8_t buf[], size_t length, Base base) | |
BigInt (RandomNumberGenerator &rng, size_t bits, bool set_high_bit=true) | |
Create a random BigInt of the specified size. | |
BigInt (std::span< const uint8_t > bytes) | |
BigInt (std::string_view str) | |
BigInt (uint64_t n) | |
void | binary_decode (const uint8_t buf[], size_t length) |
void | binary_decode (std::span< const uint8_t > buf) |
void | binary_encode (uint8_t buf[]) const |
void | binary_encode (uint8_t buf[], size_t len) const |
size_t | bits () const |
uint8_t | byte_at (size_t n) const |
size_t | bytes () const |
void | clear () |
void | clear_bit (size_t n) |
int32_t | cmp (const BigInt &n, bool check_signs=true) const |
int32_t | cmp_word (word n) const |
void | cond_flip_sign (bool predicate) |
void | conditionally_set_bit (size_t n, bool set_it) |
void | const_time_poison () const |
void | const_time_unpoison () const |
void | ct_cond_add (bool predicate, const BigInt &value) |
void | ct_cond_assign (bool predicate, const BigInt &other) |
void | ct_cond_swap (bool predicate, BigInt &other) |
void | ct_reduce_below (const BigInt &mod, secure_vector< word > &ws, size_t bound) |
void | ct_shift_left (size_t shift) |
const word * | data () const |
void | encode_words (word out[], size_t size) const |
void | flip_sign () |
bool | get_bit (size_t n) const |
uint32_t | get_substring (size_t offset, size_t length) const |
secure_vector< word > & | get_word_vector () |
const secure_vector< word > & | get_word_vector () const |
void | grow_to (size_t n) const |
bool | is_equal (const BigInt &n) const |
bool | is_even () const |
bool | is_less_than (const BigInt &n) const |
bool | is_negative () const |
bool | is_nonzero () const |
bool | is_odd () const |
bool | is_positive () const |
bool | is_zero () const |
void | mask_bits (size_t n) |
BigInt & | mod_add (const BigInt &y, const BigInt &mod, secure_vector< word > &ws) |
BigInt & | mod_mul (uint8_t y, const BigInt &mod, secure_vector< word > &ws) |
BigInt & | mod_sub (const BigInt &y, const BigInt &mod, secure_vector< word > &ws) |
BigInt & | mul (const BigInt &y, secure_vector< word > &ws) |
word * | mutable_data () |
bool | operator! () const |
BigInt & | operator%= (const BigInt &y) |
word | operator%= (word y) |
BigInt & | operator*= (const BigInt &y) |
BigInt & | operator*= (word y) |
BigInt & | operator++ () |
BigInt | operator++ (int) |
BigInt & | operator+= (const BigInt &y) |
BigInt & | operator+= (word y) |
BigInt | operator- () const |
BigInt & | operator-- () |
BigInt | operator-- (int) |
BigInt & | operator-= (const BigInt &y) |
BigInt & | operator-= (word y) |
BigInt & | operator/= (const BigInt &y) |
BigInt & | operator<<= (size_t shift) |
BigInt & | operator= (BigInt &&other) |
BigInt & | operator= (const BigInt &)=default |
BigInt & | operator>>= (size_t shift) |
void | randomize (RandomNumberGenerator &rng, size_t bitsize, bool set_high_bit=true) |
size_t | reduce_below (const BigInt &mod, secure_vector< word > &ws) |
void | resize (size_t s) |
BigInt & | rev_sub (const word y[], size_t y_words, secure_vector< word > &ws) |
Sign | reverse_sign () const |
template<typename T = std::vector<uint8_t>> | |
T | serialize () const |
template<typename T = std::vector<uint8_t>> | |
T | serialize (size_t len) const |
void | serialize_to (std::span< uint8_t > out) const |
void | set_bit (size_t n) |
void | set_sign (Sign sign) |
void | set_word_at (size_t i, word w) |
void | set_words (const word w[], size_t len) |
size_t | sig_words () const |
Sign | sign () const |
size_t | size () const |
BigInt & | square (secure_vector< word > &ws) |
BigInt & | sub (const word y[], size_t y_words, Sign sign) |
void | swap (BigInt &other) |
void | swap_reg (secure_vector< word > ®) |
std::string | to_dec_string () const |
std::string | to_hex_string () const |
uint32_t | to_u32bit () const |
size_t | top_bits_free () const |
word | word_at (size_t n) const |
~BigInt () | |
Static Public Member Functions | |
static BigInt | add2 (const BigInt &x, const word y[], size_t y_words, Sign y_sign) |
static BigInt | decode (const uint8_t buf[], size_t length) |
static BigInt | decode (const uint8_t buf[], size_t length, Base base) |
static BigInt | decode (std::span< const uint8_t > buf) |
static BigInt | decode (std::span< const uint8_t > buf, Base base) |
static std::vector< uint8_t > | encode (const BigInt &n) |
static secure_vector< uint8_t > | encode_1363 (const BigInt &n, size_t bytes) |
static void | encode_1363 (std::span< uint8_t > out, const BigInt &n) |
static void | encode_1363 (uint8_t out[], size_t bytes, const BigInt &n) |
static secure_vector< uint8_t > | encode_fixed_length_int_pair (const BigInt &n1, const BigInt &n2, size_t bytes) |
static secure_vector< uint8_t > | encode_locked (const BigInt &n) |
static BigInt | from_bytes (std::span< const uint8_t > bytes) |
static BigInt | from_bytes_with_max_bits (const uint8_t buf[], size_t length, size_t max_bits) |
static BigInt | from_s32 (int32_t n) |
static BigInt | from_string (std::string_view str) |
static BigInt | from_u64 (uint64_t n) |
static BigInt | from_word (word n) |
static BigInt | one () |
static BigInt | power_of_2 (size_t n) |
static BigInt | random_integer (RandomNumberGenerator &rng, const BigInt &min, const BigInt &max) |
static BigInt | with_capacity (size_t n) |
static BigInt | zero () |
Friends | |
void | swap (BigInt &x, BigInt &y) |
enum Botan::BigInt::Base |
enum Botan::BigInt::Sign |
|
default |
Create empty (zero) BigInt
Referenced by from_string().
Botan::BigInt::BigInt | ( | uint64_t | n | ) |
Create BigInt from an unsigned 64 bit integer
n | initial value of this BigInt |
Prefer BigInt::from_u64
Definition at line 18 of file bigint.cpp.
|
default |
Copy Constructor
other | the BigInt to copy |
|
explicit |
Create BigInt from a string. If the string starts with 0x the rest of the string will be interpreted as hexadecimal digits. Otherwise, it will be interpreted as a decimal number.
str | the string to parse for an integer value |
Definition at line 67 of file bigint.cpp.
References Botan::cast_char_ptr_to_uint8(), Decimal, decode(), Hexadecimal, Negative, Positive, and set_sign().
|
inline |
|
inlineexplicit |
Botan::BigInt::BigInt | ( | const uint8_t | buf[], |
size_t | length, | ||
Base | base ) |
Create a BigInt from an integer in a byte array
buf | the byte array holding the value |
length | size of buf |
base | is the number base of the integer in buf |
Definition at line 104 of file bigint.cpp.
References decode().
Botan::BigInt::BigInt | ( | RandomNumberGenerator & | rng, |
size_t | bits, | ||
bool | set_high_bit = true ) |
Create a random BigInt of the specified size.
rng | random number generator |
bits | size in bits |
set_high_bit | if true, the highest bit is always set |
Definition at line 126 of file bigint.cpp.
References bits(), and randomize().
|
inline |
|
inline |
Definition at line 170 of file bigint.h.
|
inline |
Read integer value from a byte vector (big endian)
bytes | the span of bytes to load |
Definition at line 947 of file bigint.h.
Referenced by Botan::BER_Decoder::decode(), Botan::generate_dsa_primes(), and Botan::RFC6979_Nonce_Generator::nonce_for().
|
inlineconstexpr |
|
inlineconstexpr |
|
inline |
Return a const pointer to the register
Definition at line 936 of file bigint.h.
Referenced by add(), Botan::EC_Point::add(), add2(), Botan::EC_Point::add_affine(), cmp(), cmp_word(), ct_cond_add(), Botan::ct_divide(), Botan::ct_modulo(), ct_reduce_below(), ct_shift_left(), Botan::CurveGFp_Repr::curve_mul(), Botan::CurveGFp_Repr::curve_sqr(), encode_words(), is_equal(), is_less_than(), mod_add(), mod_sub(), mul(), Botan::Montgomery_Params::mul(), Botan::Montgomery_Params::mul(), Botan::Montgomery_Params::mul_by(), Botan::Montgomery_Params::mul_by(), Botan::operator*(), Botan::operator*(), Botan::operator+(), operator+=(), Botan::operator-(), operator-=(), Botan::operator<<(), Botan::operator>>(), Botan::Montgomery_Params::redc(), Botan::redc_p521(), Botan::Modular_Reducer::reduce(), reduce_below(), rev_sub(), Botan::Montgomery_Params::sqr(), square(), and Botan::Montgomery_Params::square_this().
BigInt Botan::BigInt::abs | ( | ) | const |
Definition at line 374 of file bigint.cpp.
References Positive, and set_sign().
Referenced by Botan::abs().
Definition at line 16 of file big_ops2.cpp.
References _data(), Botan::bigint_add2(), Botan::bigint_cmp(), Botan::bigint_sub2(), Botan::bigint_sub2_rev(), grow_to(), mutable_data(), Positive, set_sign(), sig_words(), sign(), and size().
Referenced by Botan::Modular_Reducer::reduce().
|
static |
Definition at line 19 of file big_ops3.cpp.
References _data(), Botan::bigint_add3(), Botan::bigint_sub_abs(), mutable_data(), Positive, set_sign(), sig_words(), sign(), and with_capacity().
Referenced by Botan::operator+(), Botan::operator+(), Botan::operator-(), and Botan::operator-().
|
inline |
|
inline |
|
inline |
Store BigInt-value in a given byte array
buf | destination byte array for the integer value |
Definition at line 733 of file bigint.h.
Referenced by serialize_to().
void Botan::BigInt::binary_encode | ( | uint8_t | buf[], |
size_t | len ) const |
Store BigInt-value in a given byte array. If len is less than the size of the value, then it will be truncated. If len is greater than the size of the value, it will be zero-padded. If len exactly equals this->bytes(), this function behaves identically to binary_encode.
Zero-padding the binary encoding is useful to ensure that other applications correctly parse the encoded value as "positive integer", as a leading 1-bit may be interpreted as a sign bit.
buf | destination byte array for the integer value |
len | how many bytes to write |
Definition at line 392 of file bigint.cpp.
References Botan::get_byte_var(), Botan::store_be(), and word_at().
size_t Botan::BigInt::bits | ( | ) | const |
Get the bit length of the integer
Definition at line 295 of file bigint.cpp.
References BOTAN_MP_WORD_BITS, sig_words(), and top_bits_free().
Referenced by BigInt(), bytes(), Botan::ct_divide(), Botan::ct_divide_word(), Botan::ct_modulo(), Botan::BER_Decoder::decode(), Botan::BER_Decoder::decode_constrained_integer(), Botan::DL_Group::DL_Group(), Botan::EC_Group::EC_Group(), Botan::EC_Point_Base_Point_Precompute::EC_Point_Base_Point_Precompute(), Botan::DER_Encoder::encode(), Botan::generate_dsa_primes(), Botan::generate_rfc6979_nonce(), Botan::generate_rsa_prime(), Botan::inverse_mod(), Botan::is_perfect_square(), Botan::is_prime(), Botan::TPM_PrivateKey::key_length(), Botan::monty_multi_exp(), Botan::EC_Point::mul(), Botan::EC_Point_Base_Point_Precompute::mul(), Botan::EC_Point_Var_Point_Precompute::mul(), Botan::EC_Point_Multi_Point_Precompute::multi_exp(), Botan::RFC6979_Nonce_Generator::nonce_for(), operator/=(), Botan::passes_miller_rabin_test(), Botan::DL_Group::power_g_p(), Botan::power_mod(), random_integer(), Botan::random_prime(), Botan::RSA_PrivateKey::RSA_PrivateKey(), Botan::srp6_client_agree(), Botan::srp6_group_identifier(), to_dec_string(), to_hex_string(), to_u32bit(), and Botan::EC_Group::verify_group().
uint8_t Botan::BigInt::byte_at | ( | size_t | n | ) | const |
n | the offset to get a byte from |
Definition at line 130 of file bigint.cpp.
References Botan::get_byte_var(), and word_at().
Referenced by Botan::BER_Decoder::decode(), Botan::BER_Decoder::decode_constrained_integer(), and to_u32bit().
size_t Botan::BigInt::bytes | ( | ) | const |
Give byte length of the integer
Definition at line 282 of file bigint.cpp.
References bits(), and Botan::round_up().
Referenced by Botan::base58_decode(), Botan::DER_Encoder::encode(), Botan::EC_Point::encode(), encode_fixed_length_int_pair(), serialize_to(), and to_hex_string().
|
inline |
Zeroize the BigInt. The size of the underlying register is not modified.
Definition at line 400 of file bigint.h.
Referenced by Botan::EC_Point::add(), Botan::EC_Point::add_affine(), Botan::BER_Decoder::decode(), mul(), operator*=(), and randomize().
void Botan::BigInt::clear_bit | ( | size_t | n | ) |
Clear bit at specified position
n | bit position to clear |
Definition at line 273 of file bigint.cpp.
References BOTAN_MP_WORD_BITS, size(), and word_at().
int32_t Botan::BigInt::cmp | ( | const BigInt & | n, |
bool | check_signs = true ) const |
Compare this to another BigInt
n | the BigInt value to compare with |
check_signs | include sign in comparison? |
Definition at line 150 of file bigint.cpp.
References _data(), Botan::bigint_cmp(), is_negative(), is_positive(), and size().
Referenced by Botan::operator<=(), and Botan::operator>=().
int32_t Botan::BigInt::cmp_word | ( | word | n | ) | const |
Compare this to an integer
n | the value to compare with |
Definition at line 134 of file bigint.cpp.
References _data(), Botan::bigint_cmp(), is_negative(), and sig_words().
Referenced by Botan::operator!=(), Botan::operator<(), Botan::operator<=(), Botan::operator==(), Botan::operator>(), and Botan::operator>=().
void Botan::BigInt::cond_flip_sign | ( | bool | predicate | ) |
If predicate is true flip the sign of *this
Definition at line 488 of file bigint.cpp.
References Botan::CT::Mask< T >::expand(), set_sign(), and sign().
Referenced by ct_cond_assign(), Botan::operator*(), and rev_sub().
|
inline |
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 474 of file bigint.h.
References BOTAN_MP_WORD_BITS.
Referenced by Botan::ct_divide(), Botan::ct_divide_word(), and Botan::ct_modulo().
|
inline |
Definition at line 804 of file bigint.h.
|
inline |
Definition at line 806 of file bigint.h.
void Botan::BigInt::ct_cond_add | ( | bool | predicate, |
const BigInt & | value ) |
If predicate is true add value to *this
Definition at line 437 of file bigint.cpp.
References _data(), Botan::bigint_cnd_add(), grow_to(), is_negative(), mutable_data(), sig_words(), and size().
Referenced by Botan::inverse_mod(), and Botan::is_lucas_probable_prime().
void Botan::BigInt::ct_cond_assign | ( | bool | predicate, |
const BigInt & | other ) |
If predicate is true assign other to *this Uses a masked operation to avoid side channels
Definition at line 500 of file bigint.cpp.
References cond_flip_sign(), Botan::CT::Mask< T >::expand(), grow_to(), set_word_at(), sign(), size(), and word_at().
Referenced by ct_shift_left(), Botan::inverse_mod(), Botan::is_lucas_probable_prime(), and Botan::power_mod().
void Botan::BigInt::ct_cond_swap | ( | bool | predicate, |
BigInt & | other ) |
If predicate is true swap *this and other Uses a masked operation to avoid side channels
Definition at line 480 of file bigint.cpp.
References Botan::bigint_cnd_swap(), grow_to(), mutable_data(), and size().
Referenced by Botan::ct_divide(), and Botan::ct_modulo().
void Botan::BigInt::ct_reduce_below | ( | const BigInt & | mod, |
secure_vector< word > & | ws, | ||
size_t | bound ) |
Return *this % mod
Assumes that *this is (if anything) only slightly larger than mod and performs repeated subtractions. It should not be used if *this is much larger than mod, instead use modulo operator.
Performs exactly bound subtractions, so if *this is >= bound*mod then the result will not be fully reduced. If bound is zero, nothing happens.
Definition at line 349 of file bigint.cpp.
References _data(), Botan::bigint_sub3(), Botan::clear_mem(), grow_to(), is_negative(), Botan::CT::Mask< T >::is_zero(), mutable_data(), sig_words(), and size().
Referenced by Botan::Modular_Reducer::reduce().
void Botan::BigInt::ct_shift_left | ( | size_t | shift | ) |
Shift shift
bits to the left, runtime is independent of the value of shift
.
Definition at line 446 of file bigint.cpp.
References _data(), Botan::bigint_shl2(), BOTAN_ASSERT_NOMSG, BOTAN_DEBUG_ASSERT, BOTAN_MP_WORD_BITS, Botan::ceil_log2(), Botan::clear_mem(), ct_cond_assign(), resize(), and size().
|
inline |
|
inlinestatic |
Create a BigInt from an integer in a byte array
buf | the binary value to load |
length | size of buf |
Definition at line 867 of file bigint.h.
Referenced by BigInt(), BigInt(), decode(), and Botan::OS2ECP().
Create a BigInt from an integer in a byte array
buf | the binary value to load |
length | size of buf |
base | number-base of the integer in buf |
Definition at line 130 of file big_code.cpp.
References Binary, BOTAN_ASSERT_NOMSG, Botan::cast_uint8_ptr_to_char(), Decimal, from_bytes(), Botan::hex_decode_locked(), and Hexadecimal.
|
inlinestatic |
Create a BigInt from an integer in a byte array
buf | the binary value to load |
base | number-base of the integer in buf |
Definition at line 120 of file big_code.cpp.
References Binary, decode(), and from_bytes().
|
inlinestatic |
|
inlinestatic |
Encode a BigInt to a byte array according to IEEE 1363
n | the BigInt to encode |
bytes | the length of the resulting secure_vector<uint8_t> |
Definition at line 905 of file bigint.h.
References serialize().
Referenced by Botan::EC_Point::x_bytes(), Botan::EC_Point::xy_bytes(), and Botan::EC_Point::y_bytes().
|
inlinestatic |
|
inlinestatic |
|
static |
Encode two BigInt to a byte array according to IEEE 1363
n1 | the first BigInt to encode |
n2 | the second BigInt to encode |
bytes | the length of the encoding of each single BigInt |
Definition at line 106 of file big_code.cpp.
References bytes(), is_negative(), Botan::BufferStuffer::next(), and serialize_to().
|
inlinestatic |
void Botan::BigInt::encode_words | ( | word | out[], |
size_t | size ) const |
Place the value into out, zero-padding up to size words Throw if *this cannot be represented in size words
Definition at line 192 of file bigint.cpp.
References _data(), Botan::clear_mem(), Botan::copy_mem(), sig_words(), and size().
|
inline |
Flip the sign of this BigInt
Definition at line 587 of file bigint.h.
Referenced by Botan::ct_divide_word(), Botan::BER_Decoder::decode(), Botan::is_lucas_probable_prime(), and operator-().
|
static |
Create a BigInt from an integer in a byte array
bytes | the binary value to load |
Definition at line 95 of file bigint.cpp.
Referenced by botan_srp6_client_agree(), botan_srp6_server_session_step1(), botan_srp6_server_session_step2(), Botan::TLS::Client_Key_Exchange::Client_Key_Exchange(), decode(), decode(), Botan::BER_Decoder::decode_octet_string_bigint(), Botan::CRL_Entry::encode_into(), from_bytes_with_max_bits(), Botan::TPM_PrivateKey::get_e(), Botan::TPM_PrivateKey::get_n(), Botan::OCSP::CertID::is_id_for(), Botan::TLS::Callbacks::tls_deserialize_peer_public_key(), Botan::EC_Scalar::to_bigint(), and Botan::EC_AffinePoint_Data_PC::to_legacy_point().
|
static |
Create a BigInt from an integer in a byte array
Note this function is primarily used for implementing signature schemes and is not useful in typical applications.
buf | the byte array holding the value |
length | size of buf |
max_bits | if the resulting integer is more than max_bits, it will be shifted so it is at most max_bits in length. |
Definition at line 109 of file bigint.cpp.
References from_bytes().
|
static |
Create BigInt from a signed 32 bit integer
n | initial value of this BigInt |
Definition at line 49 of file bigint.cpp.
References from_u64().
Referenced by botan_mp_set_from_int(), and Botan::sqrt_modulo_prime().
|
static |
Create BigInt from a string.
If the string starts with 0x the rest of the string will be interpreted as hexadecimal digits. Otherwise, it will be interpreted as a decimal number.
A prefix of "-" will result in a negative integer
str | the string to parse for an integer value |
Definition at line 91 of file bigint.cpp.
References BigInt().
|
static |
Create BigInt from an unsigned 64 bit integer
n | initial value of this BigInt |
Definition at line 28 of file bigint.cpp.
References set_word_at().
Referenced by Botan::DER_Encoder::encode(), Botan::DER_Encoder::encode(), from_s32(), and Botan::RSA_PrivateKey::RSA_PrivateKey().
|
static |
Create BigInt from a word (limb)
n | initial value of this BigInt |
Definition at line 42 of file bigint.cpp.
References set_word_at().
Referenced by Botan::DL_Group::DL_Group(), Botan::is_bailie_psw_probable_prime(), Botan::is_lucas_probable_prime(), Botan::is_miller_rabin_probable_prime(), Botan::operator%(), Botan::random_prime(), Botan::sqrt_modulo_prime(), and Botan::DL_Group::verify_group().
|
inline |
Return bit value at specified position
n | the bit offset to test |
Definition at line 497 of file bigint.h.
References BOTAN_MP_WORD_BITS.
Referenced by Botan::ct_divide(), Botan::ct_divide_word(), Botan::ct_modulo(), Botan::EC_Point::encode(), Botan::inverse_mod(), Botan::EC_Point::mul(), and Botan::power_mod().
uint32_t Botan::BigInt::get_substring | ( | size_t | offset, |
size_t | length ) const |
Return (a maximum of) 32 bits of the complete value
offset | the offset to start extracting |
length | amount of bits to extract (starting at offset) |
Definition at line 227 of file bigint.cpp.
References BOTAN_MP_WORD_BITS, and word_at().
Referenced by Botan::monty_multi_exp(), Botan::EC_Point_Base_Point_Precompute::mul(), Botan::EC_Point_Var_Point_Precompute::mul(), and Botan::EC_Point_Multi_Point_Precompute::multi_exp().
|
inline |
|
inline |
|
inline |
Increase internal register buffer to at least n words
n | new size of register |
Definition at line 667 of file bigint.h.
Referenced by add(), ct_cond_add(), ct_cond_assign(), ct_cond_swap(), ct_reduce_below(), Botan::Montgomery_Int::fix_size(), mod_add(), mod_sub(), mul(), Botan::Montgomery_Params::mul_by(), Botan::Montgomery_Params::mul_by(), Botan::Montgomery_Params::redc(), Botan::redc_p192(), Botan::redc_p224(), Botan::redc_p256(), Botan::redc_p384(), Botan::redc_p521(), reduce_below(), Botan::Montgomery_Params::square_this(), Botan::vartime_divide(), and with_capacity().
bool Botan::BigInt::is_equal | ( | const BigInt & | n | ) | const |
Compare this to another BigInt
n | the BigInt value to compare with |
Definition at line 168 of file bigint.cpp.
References _data(), Botan::bigint_ct_is_eq(), sig_words(), and sign().
Referenced by Botan::operator!=(), and Botan::operator==().
|
inline |
Test if the integer has an even value
Definition at line 440 of file bigint.h.
Referenced by Botan::generate_rsa_prime(), Botan::inverse_mod(), Botan::is_bailie_psw_probable_prime(), Botan::is_lucas_probable_prime(), Botan::is_miller_rabin_probable_prime(), Botan::is_prime(), Botan::jacobi(), Botan::Montgomery_Params::Montgomery_Params(), Botan::Montgomery_Params::Montgomery_Params(), Botan::passes_miller_rabin_test(), and Botan::random_prime().
bool Botan::BigInt::is_less_than | ( | const BigInt & | n | ) | const |
Compare this to another BigInt
n | the BigInt value to compare with |
Definition at line 176 of file bigint.cpp.
References _data(), Botan::bigint_ct_is_lt(), is_negative(), is_positive(), and sig_words().
Referenced by Botan::operator<().
|
inline |
Tests if the sign of the integer is negative
Definition at line 560 of file bigint.h.
Referenced by cmp(), cmp_word(), ct_cond_add(), Botan::ct_divide_word(), Botan::ct_modulo(), ct_reduce_below(), Botan::BER_Decoder::decode(), encode_fixed_length_int_pair(), Botan::EC_AffinePoint::from_bigint_xy(), Botan::inverse_mod(), is_less_than(), Botan::is_lucas_probable_prime(), mod_add(), mod_mul(), mod_sub(), Botan::monty_multi_exp(), Botan::EC_Point::mul(), Botan::EC_Point_Base_Point_Precompute::mul(), Botan::EC_Point_Var_Point_Precompute::mul(), Botan::EC_Point_Multi_Point_Precompute::multi_exp(), Botan::operator%(), Botan::operator>>(), operator>>=(), Botan::power_mod(), random_integer(), Botan::random_prime(), Botan::Modular_Reducer::reduce(), reduce_below(), to_dec_string(), to_hex_string(), to_u32bit(), and Botan::vartime_divide().
|
inline |
Test if the integer is not zero
Definition at line 452 of file bigint.h.
Referenced by Botan::ct_modulo(), Botan::inverse_mod(), and Botan::Modular_Reducer::reduce().
|
inline |
Test if the integer has an odd value
Definition at line 446 of file bigint.h.
Referenced by Botan::inverse_mod(), Botan::is_lucas_probable_prime(), Botan::power_mod(), and Botan::sqrt_modulo_prime().
|
inline |
Tests if the sign of the integer is positive
Definition at line 566 of file bigint.h.
Referenced by cmp(), is_less_than(), Botan::operator%(), Botan::redc_p192(), Botan::redc_p224(), Botan::redc_p256(), Botan::redc_p384(), and Botan::redc_p521().
|
inline |
Test if the integer is zero
Definition at line 458 of file bigint.h.
Referenced by Botan::EC_Point::add(), Botan::EC_Point::add_affine(), Botan::ct_divide(), Botan::ct_modulo(), Botan::gcd(), Botan::inverse_mod(), Botan::EC_Scalar_Data_BN::is_zero(), Botan::Montgomery_Int::is_zero(), Botan::jacobi(), Botan::EC_Point::mult2(), Botan::EC_Point::mult2i(), Botan::operator%(), Botan::operator>>(), operator>>=(), Botan::power_mod(), Botan::random_prime(), Botan::RSA_PrivateKey::RSA_PrivateKey(), Botan::EC_Group_Data::scalar_deserialize(), to_dec_string(), Botan::vartime_divide(), and Botan::DL_Group::verify_public_element().
|
inline |
Clear all but the lowest n bits
n | amount of bits to keep |
Definition at line 490 of file bigint.h.
Referenced by Botan::inverse_mod(), Botan::redc_p192(), Botan::redc_p224(), Botan::redc_p256(), Botan::redc_p384(), Botan::redc_p521(), and Botan::Modular_Reducer::reduce().
BigInt & Botan::BigInt::mod_add | ( | const BigInt & | y, |
const BigInt & | mod, | ||
secure_vector< word > & | ws ) |
Set *this to (*this + y) % mod This function assumes *this is >= 0 && < mod
y | the BigInt to add - assumed y >= 0 and y < mod |
mod | the positive modulus |
ws | a temp workspace |
Definition at line 45 of file big_ops2.cpp.
References _data(), Botan::bigint_add3_nc(), Botan::bigint_sub3(), BOTAN_ARG_CHECK, BOTAN_DEBUG_ASSERT, BOTAN_UNUSED, Botan::CT::conditional_copy_mem(), grow_to(), is_negative(), set_words(), and sig_words().
Referenced by Botan::Montgomery_Int::add(), Botan::EC_Point::mult2(), and Botan::Montgomery_Int::operator+().
BigInt & Botan::BigInt::mod_mul | ( | uint8_t | y, |
const BigInt & | mod, | ||
secure_vector< word > & | ws ) |
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 |
Definition at line 119 of file big_ops2.cpp.
References BOTAN_ARG_CHECK, BOTAN_DEBUG_ASSERT, is_negative(), and reduce_below().
Referenced by Botan::Montgomery_Int::mul_by_2(), Botan::Montgomery_Int::mul_by_3(), Botan::Montgomery_Int::mul_by_4(), Botan::Montgomery_Int::mul_by_8(), and Botan::EC_Point::mult2().
BigInt & Botan::BigInt::mod_sub | ( | const BigInt & | y, |
const BigInt & | mod, | ||
secure_vector< word > & | ws ) |
Set *this to (*this - y) % mod This function assumes *this is >= 0 && < mod
y | the BigInt to subtract - assumed y >= 0 and y < mod |
mod | the positive modulus |
ws | a temp workspace |
Definition at line 90 of file big_ops2.cpp.
References _data(), Botan::bigint_mod_sub(), Botan::bigint_mod_sub_n(), BOTAN_DEBUG_ASSERT, grow_to(), is_negative(), mutable_data(), and sig_words().
Referenced by Botan::EC_Point::add(), Botan::EC_Point::add_affine(), Botan::EC_Point::mult2(), Botan::Montgomery_Int::operator-(), and Botan::Montgomery_Int::sub().
BigInt & Botan::BigInt::mul | ( | const BigInt & | y, |
secure_vector< word > & | ws ) |
Multiply this with y
y | the BigInt to multiply with this |
ws | a temp workspace |
Definition at line 156 of file big_ops2.cpp.
References _data(), Botan::bigint_linmul2(), Botan::bigint_linmul3(), Botan::bigint_mul(), Botan::carry(), clear(), grow_to(), mutable_data(), Negative, Positive, set_sign(), set_word_at(), sig_words(), sign(), size(), swap_reg(), and word_at().
Referenced by Botan::EC_Mul2Table_Data_BN::mul2_vartime_x_mod_order_eq(), operator*=(), and Botan::Modular_Reducer::reduce().
|
inline |
Return a mutable pointer to the register
Definition at line 641 of file bigint.h.
Referenced by add(), add2(), ct_cond_add(), ct_cond_swap(), Botan::ct_divide(), Botan::ct_modulo(), ct_reduce_below(), mod_sub(), mul(), Botan::Montgomery_Params::mul(), Botan::Montgomery_Params::mul(), Botan::Montgomery_Params::mul_by(), Botan::Montgomery_Params::mul_by(), Botan::operator*(), Botan::operator*(), operator*=(), Botan::operator>>(), Botan::Montgomery_Params::redc(), Botan::redc_p192(), Botan::redc_p224(), Botan::redc_p256(), Botan::redc_p384(), Botan::redc_p521(), Botan::Montgomery_Params::sqr(), Botan::Montgomery_Params::square_this(), and Botan::vartime_divide().
|
inlinestatic |
Create a 1-value BigInt
Definition at line 55 of file bigint.h.
Referenced by Botan::is_lucas_probable_prime(), Botan::is_perfect_square(), and Botan::power_mod().
|
inline |
Modulo operator
y | the modulus to reduce this by |
Definition at line 224 of file big_ops2.cpp.
word Botan::BigInt::operator%= | ( | word | y | ) |
Modulo operator
y | the modulus (word) to reduce this by |
Definition at line 231 of file big_ops2.cpp.
References Botan::bigint_modop_vartime(), Botan::is_power_of_2(), Negative, Positive, set_sign(), sig_words(), sign(), and word_at().
*= operator
y | the BigInt to multiply with this |
Definition at line 151 of file big_ops2.cpp.
References mul().
BigInt & Botan::BigInt::operator*= | ( | word | y | ) |
*= operator
y | the word to multiply with this |
Definition at line 197 of file big_ops2.cpp.
References Botan::bigint_linmul2(), Botan::carry(), clear(), mutable_data(), Positive, set_sign(), set_word_at(), and size().
|
inline |
|
inline |
|
inline |
BigInt Botan::BigInt::operator- | ( | ) | const |
Unary negation operator
Definition at line 311 of file bigint.cpp.
References flip_sign().
|
inline |
|
inline |
|
inline |
/= operator
y | the BigInt to divide this by |
Definition at line 212 of file big_ops2.cpp.
References bits(), Botan::is_power_of_2(), sig_words(), and word_at().
BigInt & Botan::BigInt::operator<<= | ( | size_t | shift | ) |
Left shift operator
shift | the number of bits to shift this left by |
Definition at line 260 of file big_ops2.cpp.
References Botan::bigint_shl1(), BOTAN_MP_WORD_BITS, and sig_words().
BigInt & Botan::BigInt::operator>>= | ( | size_t | shift | ) |
Right shift operator
shift | the number of bits to shift this right by |
Definition at line 274 of file big_ops2.cpp.
References Botan::bigint_shr1(), is_negative(), is_zero(), Positive, and set_sign().
|
inlinestatic |
Create a power of two
n | the power of two to create |
Definition at line 830 of file bigint.h.
References Botan::b.
Referenced by Botan::EC_Group::EC_Group(), Botan::inverse_mod(), Botan::is_perfect_square(), Botan::Montgomery_Params::Montgomery_Params(), Botan::Montgomery_Params::Montgomery_Params(), and Botan::sqrt_modulo_prime().
|
static |
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 43 of file big_rand.cpp.
References bits(), BOTAN_DEBUG_ASSERT, is_negative(), random_integer(), randomize(), and zero().
Referenced by botan_mp_rand_range(), Botan::is_miller_rabin_probable_prime(), random_integer(), and Botan::EC_Point::randomize_repr().
void Botan::BigInt::randomize | ( | RandomNumberGenerator & | rng, |
size_t | bitsize, | ||
bool | set_high_bit = true ) |
Fill BigInt with a random number with size of bitsize
If set_high_bit
is true, the highest bit will be set, which causes the entropy to be bits-1. Otherwise the highest bit is randomly chosen by the rng, causing the entropy to be bits.
rng | the random number generator to use |
bitsize | number of bits the created random value should have |
set_high_bit | if true, the highest bit is always set |
Definition at line 18 of file big_rand.cpp.
References clear(), Positive, Botan::RandomNumberGenerator::random_vec(), Botan::round_up(), and set_sign().
Referenced by BigInt(), and random_integer().
size_t Botan::BigInt::reduce_below | ( | const BigInt & | mod, |
secure_vector< word > & | ws ) |
Return *this % mod
Assumes that *this is (if anything) only slightly larger than mod and performs repeated subtractions. It should not be used if *this is much larger than mod, instead use modulo operator.
Definition at line 317 of file bigint.cpp.
References _data(), Botan::bigint_sub3(), Botan::clear_mem(), grow_to(), is_negative(), sig_words(), size(), and swap_reg().
Referenced by mod_mul(), and Botan::vartime_divide().
|
inline |
BigInt & Botan::BigInt::rev_sub | ( | const word | y[], |
size_t | y_words, | ||
secure_vector< word > & | ws ) |
Set *this to y - *this
y | the BigInt to subtract from as a sequence of words |
y_words | length of y in words |
ws | a temp workspace |
Definition at line 130 of file big_ops2.cpp.
References _data(), Botan::bigint_sub_abs(), Botan::clear_mem(), cond_flip_sign(), Positive, sig_words(), sign(), and swap_reg().
Referenced by Botan::Modular_Reducer::reduce().
|
inline |
Serialize the value of this BigInt as a big endian encoding, always returning the specified number of bytes.
Throws if the BigInt is too large to encode in the length specified.
Definition at line 712 of file bigint.h.
References T.
Referenced by Botan::base58_decode(), Botan::CRL_Entry::decode_from(), Botan::DER_Encoder::encode(), encode_1363(), Botan::FPE_FE1::FPE_FE1(), Botan::DL_PublicKey::public_key_as_bytes(), Botan::DL_PrivateKey::raw_private_key_bits(), Botan::EC_Group_Data::scalar_from_bigint(), Botan::Montgomery_Int::serialize(), Botan::srp6_client_agree(), and Botan::SRP6_Server_Session::step2().
void Botan::BigInt::serialize_to | ( | std::span< uint8_t > | out | ) | const |
Serialize the absolute value of this BigInt as a big endian encoding.
If out is smaller than the total bytes of the BigInt then an exception is thrown.
If out is larger than the total bytes of the BigInt then the necessary number of zeros are prefixed to produce the desired output length
Zero-padding the binary encoding is useful to ensure that other applications correctly parse the encoded value as "positive integer", as a leading 1-bit may be interpreted as a sign bit. It also is necessary when using a fixed size encoding for the integers.
out | destination byte span for the integer value |
Definition at line 383 of file bigint.cpp.
References binary_encode(), BOTAN_ARG_CHECK, and bytes().
Referenced by Botan::EC_Point::encode(), encode_fixed_length_int_pair(), Botan::EC_AffinePoint::from_bigint_xy(), Botan::RFC6979_Nonce_Generator::nonce_for(), Botan::RFC6979_Nonce_Generator::RFC6979_Nonce_Generator(), Botan::EC_Scalar_Data_BN::serialize_to(), and to_hex_string().
|
inline |
Set bit at specified position
n | bit position to set |
Definition at line 464 of file bigint.h.
Referenced by Botan::generate_dsa_primes(), Botan::generate_rsa_prime(), Botan::Modular_Reducer::Modular_Reducer(), and Botan::random_prime().
|
inline |
Set sign of the integer
sign | new Sign to set |
Definition at line 593 of file bigint.h.
Referenced by abs(), add(), add2(), BigInt(), cond_flip_sign(), Botan::inverse_mod(), mul(), operator%=(), Botan::operator*(), operator*=(), Botan::operator>>(), operator>>=(), randomize(), Botan::Modular_Reducer::reduce(), square(), to_dec_string(), and Botan::vartime_divide().
|
inline |
Definition at line 550 of file bigint.h.
Referenced by ct_cond_assign(), from_u64(), from_word(), mul(), and operator*=().
|
inline |
Definition at line 552 of file bigint.h.
Referenced by Botan::EC_Point::add(), Botan::EC_Point::add_affine(), mod_add(), and Botan::Montgomery_Int::Montgomery_Int().
|
inline |
Return how many words we need to hold this value
Definition at line 616 of file bigint.h.
Referenced by add(), add2(), bits(), cmp_word(), ct_cond_add(), Botan::ct_divide(), Botan::ct_divide_word(), Botan::ct_modulo(), ct_reduce_below(), Botan::CurveGFp_Repr::curve_mul(), Botan::CurveGFp_Repr::curve_sqr(), encode_words(), Botan::Montgomery_Int::fix_size(), Botan::gcd(), is_equal(), is_less_than(), mod_add(), mod_sub(), Botan::Modular_Reducer::Modular_Reducer(), Botan::Montgomery_Params::Montgomery_Params(), Botan::Montgomery_Params::Montgomery_Params(), mul(), Botan::Montgomery_Params::mul(), Botan::Montgomery_Params::mul(), Botan::Montgomery_Params::mul_by(), Botan::Montgomery_Params::mul_by(), Botan::operator%(), Botan::operator%(), operator%=(), Botan::operator*(), Botan::operator*(), Botan::operator+(), operator+=(), Botan::operator-(), operator-=(), Botan::operator/(), operator/=(), Botan::operator<<(), operator<<=(), Botan::operator>>(), Botan::Modular_Reducer::reduce(), reduce_below(), rev_sub(), Botan::Montgomery_Params::sqr(), square(), Botan::Montgomery_Params::square_this(), top_bits_free(), and Botan::vartime_divide().
|
inline |
Return the sign of the integer
Definition at line 572 of file bigint.h.
Referenced by add(), add2(), cond_flip_sign(), ct_cond_assign(), is_equal(), mul(), Botan::operator%(), operator%=(), Botan::operator*(), Botan::operator*(), Botan::operator+(), operator+=(), operator-=(), Botan::operator<<(), Botan::operator>>(), and rev_sub().
|
inline |
Give size of internal register
Definition at line 610 of file bigint.h.
Referenced by add(), Botan::EC_Point::add(), Botan::EC_Point::add_affine(), clear_bit(), cmp(), ct_cond_add(), ct_cond_assign(), ct_cond_swap(), Botan::ct_divide(), Botan::ct_modulo(), ct_reduce_below(), ct_shift_left(), Botan::CurveGFp_Repr::curve_mul(), Botan::CurveGFp_Repr::curve_sqr(), encode_words(), Botan::low_zero_bits(), mul(), Botan::Montgomery_Params::mul(), Botan::Montgomery_Params::mul(), Botan::Montgomery_Params::mul_by(), Botan::Montgomery_Params::mul_by(), Botan::operator*(), operator*=(), Botan::redc_p192(), Botan::redc_p224(), Botan::redc_p256(), Botan::redc_p384(), Botan::redc_p521(), Botan::Modular_Reducer::reduce(), reduce_below(), Botan::Montgomery_Params::sqr(), square(), Botan::Montgomery_Params::square_this(), and with_capacity().
BigInt & Botan::BigInt::square | ( | secure_vector< word > & | ws | ) |
Square value of *this
ws | a temp workspace |
Definition at line 183 of file big_ops2.cpp.
References _data(), Botan::bigint_sqr(), Positive, set_sign(), sig_words(), size(), and swap_reg().
Referenced by Botan::square().
|
inline |
Swap this value with another
other | BigInt to swap values with |
Definition at line 192 of file bigint.h.
Referenced by Botan::EC_Point::add_affine(), Botan::EC_Point::mult2(), and Botan::EC_Point::swap_coords().
|
inline |
std::string Botan::BigInt::to_dec_string | ( | ) | const |
Convert this value to a decimal string. Warning: decimal conversions are relatively slow
If the integer is zero then "0" is returned. If the integer is negative then "-" is prefixed.
Definition at line 16 of file big_code.cpp.
References bits(), BOTAN_ASSERT_NOMSG, Botan::ct_divide_word(), is_negative(), is_zero(), Positive, and set_sign().
Referenced by Botan::operator<<().
std::string Botan::BigInt::to_hex_string | ( | ) | const |
Convert this value to a hexadecimal string.
If the integer is negative then "-" is prefixed. Then a prefix of "0x" is added. Follows is a sequence of hexadecimal characters in uppercase.
The number of hexadecimal characters is always an even number, with a zero prefix being included if necessary. For example encoding the integer "5" results in "0x05"
Definition at line 86 of file big_code.cpp.
References bits(), bytes(), Botan::hex_encode(), is_negative(), and serialize_to().
Referenced by Botan::operator<<().
uint32_t Botan::BigInt::to_u32bit | ( | ) | const |
Convert this value into a uint32_t, if it is in the range [0 ... 2**32-1], or otherwise throw an exception.
Definition at line 255 of file bigint.cpp.
References bits(), byte_at(), and is_negative().
size_t Botan::BigInt::top_bits_free | ( | ) | const |
Get the number of high bits unset in the top (allocated) word of this integer. Returns BOTAN_MP_WORD_BITS only iff *this is zero. Ignores sign.
Definition at line 286 of file bigint.cpp.
References BOTAN_MP_WORD_BITS, Botan::high_bit(), sig_words(), Botan::CT::unpoison(), Botan::CT::value_barrier(), and word_at().
Referenced by bits(), and Botan::vartime_divide().
|
static |
Create BigInt of specified size, all zeros
n | size of the internal register in words |
Definition at line 58 of file bigint.cpp.
References grow_to(), and size().
Referenced by add2(), Botan::ct_divide(), Botan::ct_divide_word(), Botan::ct_modulo(), Botan::gcd(), Botan::Montgomery_Params::mul(), Botan::Montgomery_Params::mul(), Botan::operator*(), Botan::operator*(), Botan::operator<<(), Botan::operator>>(), and Botan::Montgomery_Params::sqr().
|
inline |
Return the word at a specified position of the internal register
n | position in the register |
Definition at line 548 of file bigint.h.
Referenced by binary_encode(), byte_at(), clear_bit(), ct_cond_assign(), Botan::EC_Group::EC_group_identity_from_order(), get_substring(), Botan::is_prime(), Botan::low_zero_bits(), Botan::Montgomery_Params::Montgomery_Params(), Botan::Montgomery_Params::Montgomery_Params(), mul(), Botan::operator%(), Botan::operator%(), operator%=(), Botan::operator*(), Botan::operator/(), operator/=(), Botan::redc_p521(), top_bits_free(), and Botan::vartime_divide().
|
inlinestatic |
Create a 0-value BigInt
Definition at line 50 of file bigint.h.
Referenced by Botan::DL_Group::DL_Group(), Botan::inverse_mod(), Botan::is_perfect_square(), Botan::operator>>(), Botan::power_mod(), random_integer(), Botan::random_safe_prime(), and Botan::vartime_divide().