Botan 3.6.1
Crypto and TLS for C&
Botan::BigInt Class Referencefinal

#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
 
BigIntadd (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)
 
BigIntmod_add (const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
 
BigIntmod_mul (uint8_t y, const BigInt &mod, secure_vector< word > &ws)
 
BigIntmod_sub (const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
 
BigIntmul (const BigInt &y, secure_vector< word > &ws)
 
word * mutable_data ()
 
bool operator! () const
 
BigIntoperator%= (const BigInt &y)
 
word operator%= (word y)
 
BigIntoperator*= (const BigInt &y)
 
BigIntoperator*= (word y)
 
BigIntoperator++ ()
 
BigInt operator++ (int)
 
BigIntoperator+= (const BigInt &y)
 
BigIntoperator+= (word y)
 
BigInt operator- () const
 
BigIntoperator-- ()
 
BigInt operator-- (int)
 
BigIntoperator-= (const BigInt &y)
 
BigIntoperator-= (word y)
 
BigIntoperator/= (const BigInt &y)
 
BigIntoperator<<= (size_t shift)
 
BigIntoperator= (BigInt &&other)
 
BigIntoperator= (const BigInt &)=default
 
BigIntoperator>>= (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)
 
BigIntrev_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
 
BigIntsquare (secure_vector< word > &ws)
 
BigIntsub (const word y[], size_t y_words, Sign sign)
 
void swap (BigInt &other)
 
void swap_reg (secure_vector< word > &reg)
 
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)
 

Detailed Description

Arbitrary precision integer

Definition at line 26 of file bigint.h.

Member Enumeration Documentation

◆ Base

Base enumerator for encoding and decoding

Enumerator
Decimal 
Hexadecimal 
Binary 

Definition at line 31 of file bigint.h.

31 {
32 Decimal BOTAN_DEPRECATED("All functions using this enum are deprecated") = 10,
33 Hexadecimal BOTAN_DEPRECATED("All functions using this enum are deprecated") = 16,
34 Binary BOTAN_DEPRECATED("All functions using this enum are deprecated") = 256
35 };
#define BOTAN_DEPRECATED(msg)
Definition compiler.h:125

◆ Sign

Sign symbol definitions for positive and negative numbers

Enumerator
Negative 
Positive 

Definition at line 40 of file bigint.h.

40{ Negative = 0, Positive = 1 };

Constructor & Destructor Documentation

◆ BigInt() [1/9]

Botan::BigInt::BigInt ( )
default

Create empty (zero) BigInt

Referenced by from_string().

◆ BigInt() [2/9]

Botan::BigInt::BigInt ( uint64_t n)

Create BigInt from an unsigned 64 bit integer

Parameters
ninitial value of this BigInt

Prefer BigInt::from_u64

Definition at line 18 of file bigint.cpp.

18 {
19#if BOTAN_MP_WORD_BITS == 64
20 m_data.set_word_at(0, n);
21#else
22 m_data.set_word_at(1, static_cast<word>(n >> 32));
23 m_data.set_word_at(0, static_cast<word>(n));
24#endif
25}

◆ BigInt() [3/9]

Botan::BigInt::BigInt ( const BigInt & other)
default

Copy Constructor

Parameters
otherthe BigInt to copy

◆ BigInt() [4/9]

Botan::BigInt::BigInt ( std::string_view str)
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.

Parameters
strthe string to parse for an integer value

Definition at line 67 of file bigint.cpp.

67 {
68 Base base = Decimal;
69 size_t markers = 0;
70 bool negative = false;
71
72 if(!str.empty() && str[0] == '-') {
73 markers += 1;
74 negative = true;
75 }
76
77 if(str.length() > markers + 2 && str[markers] == '0' && str[markers + 1] == 'x') {
78 markers += 2;
79 base = Hexadecimal;
80 }
81
82 *this = decode(cast_char_ptr_to_uint8(str.data()) + markers, str.length() - markers, base);
83
84 if(negative) {
86 } else {
88 }
89}
static BigInt decode(const uint8_t buf[], size_t length)
Definition bigint.h:867
void set_sign(Sign sign)
Definition bigint.h:593
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition mem_ops.h:273

References Botan::cast_char_ptr_to_uint8(), Decimal, decode(), Hexadecimal, Negative, Positive, and set_sign().

◆ BigInt() [5/9]

Botan::BigInt::BigInt ( const uint8_t buf[],
size_t length )
inline

Create a BigInt from an integer in a byte array

Parameters
bufthe byte array holding the value
lengthsize of buf

Definition at line 118 of file bigint.h.

118{ assign_from_bytes(std::span{buf, length}); }

◆ BigInt() [6/9]

Botan::BigInt::BigInt ( std::span< const uint8_t > bytes)
inlineexplicit

Create a BigInt from an integer in a byte array

Parameters
bytesthe byte vector holding the value

Definition at line 124 of file bigint.h.

124{ assign_from_bytes(bytes); }
size_t bytes() const
Definition bigint.cpp:282

◆ BigInt() [7/9]

Botan::BigInt::BigInt ( const uint8_t buf[],
size_t length,
Base base )

Create a BigInt from an integer in a byte array

Parameters
bufthe byte array holding the value
lengthsize of buf
baseis the number base of the integer in buf

Definition at line 104 of file bigint.cpp.

104 {
105 *this = decode(input, length, base);
106}

References decode().

◆ BigInt() [8/9]

Botan::BigInt::BigInt ( RandomNumberGenerator & rng,
size_t bits,
bool set_high_bit = true )

Create a random BigInt of the specified size.

Parameters
rngrandom number generator
bitssize in bits
set_high_bitif true, the highest bit is always set
See also
randomize

Definition at line 126 of file bigint.cpp.

126 {
127 randomize(rng, bits, set_high_bit);
128}
void randomize(RandomNumberGenerator &rng, size_t bitsize, bool set_high_bit=true)
Definition big_rand.cpp:18
size_t bits() const
Definition bigint.cpp:295

References bits(), and randomize().

◆ BigInt() [9/9]

Botan::BigInt::BigInt ( BigInt && other)
inline

Move constructor

Definition at line 168 of file bigint.h.

168{ this->swap(other); }
friend void swap(BigInt &x, BigInt &y)
Definition bigint.h:197

◆ ~BigInt()

Botan::BigInt::~BigInt ( )
inline

Definition at line 170 of file bigint.h.

constexpr void _const_time_unpoison() const
Definition bigint.h:814

Member Function Documentation

◆ _assign_from_bytes()

void Botan::BigInt::_assign_from_bytes ( std::span< const uint8_t > bytes)
inline

Read integer value from a byte vector (big endian)

Warning
this is an implementation detail which is not for public use and not covered by SemVer. In applications use BigInt::from_bytes
Parameters
bytesthe span of bytes to load

Definition at line 947 of file bigint.h.

947{ assign_from_bytes(bytes); }

Referenced by Botan::BER_Decoder::decode(), Botan::generate_dsa_primes(), and Botan::RFC6979_Nonce_Generator::nonce_for().

◆ _const_time_poison()

void Botan::BigInt::_const_time_poison ( ) const
inlineconstexpr

Definition at line 812 of file bigint.h.

812{}

◆ _const_time_unpoison()

void Botan::BigInt::_const_time_unpoison ( ) const
inlineconstexpr

Definition at line 814 of file bigint.h.

814{}

◆ _data()

◆ abs()

BigInt Botan::BigInt::abs ( ) const
Returns
absolute (positive) value of this

Definition at line 374 of file bigint.cpp.

374 {
375 BigInt x = (*this);
376 x.set_sign(Positive);
377 return x;
378}
BigInt()=default

References Positive, and set_sign().

Referenced by Botan::abs().

◆ add()

BigInt & Botan::BigInt::add ( const word y[],
size_t y_words,
Sign sign )

Definition at line 16 of file big_ops2.cpp.

16 {
17 const size_t x_sw = sig_words();
18
19 grow_to(std::max(x_sw, y_words) + 1);
20
21 if(sign() == y_sign) {
22 bigint_add2(mutable_data(), size() - 1, y, y_words);
23 } else {
24 const int32_t relative_size = bigint_cmp(_data(), x_sw, y, y_words);
25
26 if(relative_size >= 0) {
27 // *this >= y
28 bigint_sub2(mutable_data(), x_sw, y, y_words);
29 } else {
30 // *this < y
31 bigint_sub2_rev(mutable_data(), y, y_words);
32 }
33
34 //this->sign_fixup(relative_size, y_sign);
35 if(relative_size < 0) {
36 set_sign(y_sign);
37 } else if(relative_size == 0) {
39 }
40 }
41
42 return (*this);
43}
size_t sig_words() const
Definition bigint.h:616
word * mutable_data()
Definition bigint.h:641
size_t size() const
Definition bigint.h:610
void grow_to(size_t n) const
Definition bigint.h:667
const word * _data() const
Definition bigint.h:936
Sign sign() const
Definition bigint.h:572
constexpr int32_t bigint_cmp(const W x[], size_t x_size, const W y[], size_t y_size)
Definition mp_core.h:592
constexpr void bigint_add2(W x[], size_t x_size, const W y[], size_t y_size)
Definition mp_core.h:275
constexpr auto bigint_sub2(W x[], size_t x_size, const W y[], size_t y_size) -> W
Definition mp_core.h:291
constexpr void bigint_sub2_rev(W x[], const W y[], size_t y_size)
Definition mp_core.h:317

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().

◆ add2()

BigInt Botan::BigInt::add2 ( const BigInt & x,
const word y[],
size_t y_words,
BigInt::Sign y_sign )
static

Definition at line 19 of file big_ops3.cpp.

19 {
20 const size_t x_sw = x.sig_words();
21
22 BigInt z = BigInt::with_capacity(std::max(x_sw, y_words) + 1);
23
24 if(x.sign() == y_sign) {
25 bigint_add3(z.mutable_data(), x._data(), x_sw, y, y_words);
26 z.set_sign(x.sign());
27 } else {
28 const int32_t relative_size = bigint_sub_abs(z.mutable_data(), x._data(), x_sw, y, y_words);
29
30 //z.sign_fixup(relative_size, y_sign);
31 if(relative_size < 0) {
32 z.set_sign(y_sign);
33 } else if(relative_size == 0) {
34 z.set_sign(BigInt::Positive);
35 } else {
36 z.set_sign(x.sign());
37 }
38 }
39
40 return z;
41}
static BigInt with_capacity(size_t n)
Definition bigint.cpp:58
constexpr auto bigint_sub_abs(W z[], const W x[], const W y[], size_t N, W ws[]) -> CT::Mask< W >
Definition mp_core.h:439
constexpr void bigint_add3(W z[], const W x[], size_t x_size, const W y[], size_t y_size)
Definition mp_core.h:283

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-().

◆ binary_decode() [1/2]

void Botan::BigInt::binary_decode ( const uint8_t buf[],
size_t length )
inline

Read integer value from a byte array with given size

Parameters
bufbyte array buffer containing the integer
lengthsize of buf

Definition at line 758 of file bigint.h.

758 {
759 this->assign_from_bytes(std::span{buf, length});
760 }

◆ binary_decode() [2/2]

void Botan::BigInt::binary_decode ( std::span< const uint8_t > buf)
inline

Read integer value from a byte vector

Parameters
bufthe vector to load from

Definition at line 766 of file bigint.h.

766 {
767 this->assign_from_bytes(buf);
768 }

◆ binary_encode() [1/2]

void Botan::BigInt::binary_encode ( uint8_t buf[]) const
inline

Store BigInt-value in a given byte array

Parameters
bufdestination byte array for the integer value

Definition at line 733 of file bigint.h.

733 {
734 this->serialize_to(std::span{buf, this->bytes()});
735 }
void serialize_to(std::span< uint8_t > out) const
Definition bigint.cpp:383

Referenced by serialize_to().

◆ binary_encode() [2/2]

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.

Parameters
bufdestination byte array for the integer value
lenhow many bytes to write

Definition at line 392 of file bigint.cpp.

392 {
393 const size_t full_words = len / sizeof(word);
394 const size_t extra_bytes = len % sizeof(word);
395
396 for(size_t i = 0; i != full_words; ++i) {
397 const word w = word_at(i);
398 store_be(w, output + (len - (i + 1) * sizeof(word)));
399 }
400
401 if(extra_bytes > 0) {
402 const word w = word_at(full_words);
403
404 for(size_t i = 0; i != extra_bytes; ++i) {
405 output[extra_bytes - i - 1] = get_byte_var(sizeof(word) - i - 1, w);
406 }
407 }
408}
word word_at(size_t n) const
Definition bigint.h:548
constexpr uint8_t get_byte_var(size_t byte_num, T input)
Definition loadstor.h:65
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:773

References Botan::get_byte_var(), Botan::store_be(), and word_at().

◆ bits()

size_t Botan::BigInt::bits ( ) const

Get the bit length of the integer

Returns
bit length of the represented integer value

Definition at line 295 of file bigint.cpp.

295 {
296 const size_t words = sig_words();
297
298 if(words == 0) {
299 return 0;
300 }
301
302 const size_t full_words = (words - 1) * BOTAN_MP_WORD_BITS;
303 const size_t top_bits = BOTAN_MP_WORD_BITS - top_bits_free();
304
305 return full_words + top_bits;
306}
size_t top_bits_free() const
Definition bigint.cpp:286
#define BOTAN_MP_WORD_BITS
Definition build.h:71

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().

◆ byte_at()

uint8_t Botan::BigInt::byte_at ( size_t n) const
Parameters
nthe offset to get a byte from
Returns
byte at offset n

Definition at line 130 of file bigint.cpp.

130 {
131 return get_byte_var(sizeof(word) - (n % sizeof(word)) - 1, word_at(n / sizeof(word)));
132}

References Botan::get_byte_var(), and word_at().

Referenced by Botan::BER_Decoder::decode(), Botan::BER_Decoder::decode_constrained_integer(), and to_u32bit().

◆ bytes()

size_t Botan::BigInt::bytes ( ) const

Give byte length of the integer

Returns
byte length of the represented integer value

Definition at line 282 of file bigint.cpp.

282 {
283 return round_up(bits(), 8) / 8;
284}
constexpr size_t round_up(size_t n, size_t align_to)
Definition rounding.h:25

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().

◆ clear()

void Botan::BigInt::clear ( )
inline

Zeroize the BigInt. The size of the underlying register is not modified.

Definition at line 400 of file bigint.h.

400 {
401 m_data.set_to_zero();
402 m_signedness = Positive;
403 }

Referenced by Botan::EC_Point::add(), Botan::EC_Point::add_affine(), Botan::BER_Decoder::decode(), mul(), operator*=(), and randomize().

◆ clear_bit()

void Botan::BigInt::clear_bit ( size_t n)

Clear bit at specified position

Parameters
nbit position to clear

Definition at line 273 of file bigint.cpp.

273 {
274 const size_t which = n / BOTAN_MP_WORD_BITS;
275
276 if(which < size()) {
277 const word mask = ~(static_cast<word>(1) << (n % BOTAN_MP_WORD_BITS));
278 m_data.set_word_at(which, word_at(which) & mask);
279 }
280}

References BOTAN_MP_WORD_BITS, size(), and word_at().

◆ cmp()

int32_t Botan::BigInt::cmp ( const BigInt & n,
bool check_signs = true ) const

Compare this to another BigInt

Parameters
nthe BigInt value to compare with
check_signsinclude sign in comparison?
Returns
if (this<n) return -1, if (this>n) return 1, if both values are identical return 0 [like Perl's <=> operator]

Definition at line 150 of file bigint.cpp.

150 {
151 if(check_signs) {
152 if(other.is_positive() && this->is_negative()) {
153 return -1;
154 }
155
156 if(other.is_negative() && this->is_positive()) {
157 return 1;
158 }
159
160 if(other.is_negative() && this->is_negative()) {
161 return (-bigint_cmp(this->_data(), this->size(), other._data(), other.size()));
162 }
163 }
164
165 return bigint_cmp(this->_data(), this->size(), other._data(), other.size());
166}

References _data(), Botan::bigint_cmp(), is_negative(), is_positive(), and size().

Referenced by Botan::operator<=(), and Botan::operator>=().

◆ cmp_word()

int32_t Botan::BigInt::cmp_word ( word n) const

Compare this to an integer

Parameters
nthe value to compare with
Returns
if (this<n) return -1, if (this>n) return 1, if both values are identical return 0 [like Perl's <=> operator]

Definition at line 134 of file bigint.cpp.

134 {
135 if(is_negative()) {
136 return -1; // other is positive ...
137 }
138
139 const size_t sw = this->sig_words();
140 if(sw > 1) {
141 return 1; // must be larger since other is just one word ...
142 }
143
144 return bigint_cmp(this->_data(), sw, &other, 1);
145}
bool is_negative() const
Definition bigint.h:560

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>=().

◆ cond_flip_sign()

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.

488 {
489 // This code is assuming Negative == 0, Positive == 1
490
491 const auto mask = CT::Mask<uint8_t>::expand(predicate);
492
493 const uint8_t current_sign = static_cast<uint8_t>(sign());
494
495 const uint8_t new_sign = mask.select(current_sign ^ 1, current_sign);
496
497 set_sign(static_cast<Sign>(new_sign));
498}
static constexpr Mask< T > expand(T v)
Definition ct_utils.h:389

References Botan::CT::Mask< T >::expand(), set_sign(), and sign().

Referenced by ct_cond_assign(), Botan::operator*(), and rev_sub().

◆ conditionally_set_bit()

void Botan::BigInt::conditionally_set_bit ( size_t n,
bool set_it )
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.

Parameters
nbit position to set
set_itif the bit should be set

Definition at line 474 of file bigint.h.

474 {
475 const size_t which = n / BOTAN_MP_WORD_BITS;
476 const word mask = static_cast<word>(set_it) << (n % BOTAN_MP_WORD_BITS);
477 m_data.set_word_at(which, word_at(which) | mask);
478 }

References BOTAN_MP_WORD_BITS.

Referenced by Botan::ct_divide(), Botan::ct_divide_word(), and Botan::ct_modulo().

◆ const_time_poison()

void Botan::BigInt::const_time_poison ( ) const
inline

Definition at line 804 of file bigint.h.

constexpr void _const_time_poison() const
Definition bigint.h:812

◆ const_time_unpoison()

void Botan::BigInt::const_time_unpoison ( ) const
inline

Definition at line 806 of file bigint.h.

◆ ct_cond_add()

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.

437 {
438 if(this->is_negative() || value.is_negative()) {
439 throw Invalid_Argument("BigInt::ct_cond_add requires both values to be positive");
440 }
441 this->grow_to(1 + value.sig_words());
442
443 bigint_cnd_add(static_cast<word>(predicate), this->mutable_data(), this->size(), value._data(), value.sig_words());
444}
constexpr W bigint_cnd_add(W cnd, W x[], size_t x_size, const W y[], size_t y_size)
Definition mp_core.h:42

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().

◆ ct_cond_assign()

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.

500 {
501 const size_t t_words = size();
502 const size_t o_words = other.size();
503
504 if(o_words < t_words) {
505 grow_to(o_words);
506 }
507
508 const size_t r_words = std::max(t_words, o_words);
509
510 const auto mask = CT::Mask<word>::expand(predicate);
511
512 for(size_t i = 0; i != r_words; ++i) {
513 const word o_word = other.word_at(i);
514 const word t_word = this->word_at(i);
515 this->set_word_at(i, mask.select(o_word, t_word));
516 }
517
518 const bool different_sign = sign() != other.sign();
519 cond_flip_sign(predicate && different_sign);
520}
void set_word_at(size_t i, word w)
Definition bigint.h:550
void cond_flip_sign(bool predicate)
Definition bigint.cpp:488

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().

◆ ct_cond_swap()

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.

480 {
481 const size_t max_words = std::max(size(), other.size());
482 grow_to(max_words);
483 other.grow_to(max_words);
484
485 bigint_cnd_swap(static_cast<word>(predicate), this->mutable_data(), other.mutable_data(), max_words);
486}
constexpr void bigint_cnd_swap(W cnd, W x[], W y[], size_t size)
Definition mp_core.h:30

References Botan::bigint_cnd_swap(), grow_to(), mutable_data(), and size().

Referenced by Botan::ct_divide(), and Botan::ct_modulo().

◆ ct_reduce_below()

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.

349 {
350 if(mod.is_negative() || this->is_negative()) {
351 throw Invalid_Argument("BigInt::ct_reduce_below both values must be positive");
352 }
353
354 const size_t mod_words = mod.sig_words();
355
356 grow_to(mod_words);
357
358 const size_t sz = size();
359
360 ws.resize(sz);
361
362 clear_mem(ws.data(), sz);
363
364 for(size_t i = 0; i != bound; ++i) {
365 word borrow = bigint_sub3(ws.data(), _data(), sz, mod._data(), mod_words);
366
367 CT::Mask<word>::is_zero(borrow).select_n(mutable_data(), ws.data(), _data(), sz);
368 }
369}
static constexpr Mask< T > is_zero(T x)
Definition ct_utils.h:429
constexpr auto bigint_sub3(W z[], const W x[], size_t x_size, const W y[], size_t y_size) -> W
Definition mp_core.h:341
constexpr void clear_mem(T *ptr, size_t n)
Definition mem_ops.h:120

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().

◆ ct_shift_left()

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.

446 {
447 auto shl_bit = [](const BigInt& a, BigInt& result) {
448 BOTAN_DEBUG_ASSERT(a.size() + 1 == result.size());
449 bigint_shl2(result.mutable_data(), a._data(), a.size(), 1);
450 // shl2 may have shifted a bit into the next word, which must be dropped
451 clear_mem(result.mutable_data() + result.size() - 1, 1);
452 };
453
454 auto shl_word = [](const BigInt& a, BigInt& result) {
455 // the most significant word is not copied, aka. shifted out
456 bigint_shl2(result.mutable_data(), a._data(), a.size() - 1 /* ignore msw */, BOTAN_MP_WORD_BITS);
457 // we left-shifted by a full word, the least significant word must be zero'ed
458 clear_mem(result.mutable_data(), 1);
459 };
460
462
463 constexpr size_t bits_in_word = sizeof(word) * 8;
464 const size_t word_shift = shift >> ceil_log2(bits_in_word); // shift / bits_in_word
465 const size_t bit_shift = shift & ((1 << ceil_log2(bits_in_word)) - 1); // shift % bits_in_word
466 const size_t iterations = std::max(size(), bits_in_word) - 1; // uint64_t i; i << 64 is undefined behaviour
467
468 // In every iteration, shift one bit and one word to the left and use the
469 // shift results only when they are within the shift range.
470 BigInt tmp;
471 tmp.resize(size() + 1 /* to hold the shifted-out word */);
472 for(size_t i = 0; i < iterations; ++i) {
473 shl_bit(*this, tmp);
474 ct_cond_assign(i < bit_shift, tmp);
475 shl_word(*this, tmp);
476 ct_cond_assign(i < word_shift, tmp);
477 }
478}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
#define BOTAN_DEBUG_ASSERT(expr)
Definition assert.h:98
void ct_cond_assign(bool predicate, const BigInt &other)
Definition bigint.cpp:500
constexpr void bigint_shl2(W y[], const W x[], size_t x_size, size_t shift)
Definition mp_core.h:510
constexpr uint8_t ceil_log2(T x)
Definition bit_ops.h:122

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().

◆ data()

const word * Botan::BigInt::data ( ) const
inline

Return a const pointer to the register

Returns
a pointer to the start of the internal register

Definition at line 647 of file bigint.h.

647{ return m_data.const_data(); }

◆ decode() [1/4]

static BigInt Botan::BigInt::decode ( const uint8_t buf[],
size_t length )
inlinestatic

Create a BigInt from an integer in a byte array

Parameters
bufthe binary value to load
lengthsize of buf
Returns
BigInt representing the integer in the byte array

Definition at line 867 of file bigint.h.

867 {
868 return BigInt::from_bytes(std::span{buf, length});
869 }
static BigInt from_bytes(std::span< const uint8_t > bytes)
Definition bigint.cpp:95

Referenced by BigInt(), BigInt(), decode(), and Botan::OS2ECP().

◆ decode() [2/4]

BigInt Botan::BigInt::decode ( const uint8_t buf[],
size_t length,
Base base )
static

Create a BigInt from an integer in a byte array

Parameters
bufthe binary value to load
lengthsize of buf
basenumber-base of the integer in buf
Returns
BigInt representing the integer in the byte array

Definition at line 130 of file big_code.cpp.

130 {
131 if(base == Binary) {
132 return BigInt::from_bytes(std::span{buf, length});
133 } else if(base == Hexadecimal) {
134 BigInt r;
136
137 if(length % 2) {
138 // Handle lack of leading 0
139 const char buf0_with_leading_0[2] = {'0', static_cast<char>(buf[0])};
140
141 binary = hex_decode_locked(buf0_with_leading_0, 2);
142
143 if(length > 1) {
144 binary += hex_decode_locked(cast_uint8_ptr_to_char(&buf[1]), length - 1, false);
145 }
146 } else {
147 binary = hex_decode_locked(cast_uint8_ptr_to_char(buf), length, false);
148 }
149
150 r.assign_from_bytes(binary);
151 return r;
152 } else if(base == Decimal) {
153 BigInt r;
154 // This could be made faster using the same trick as to_dec_string
155 for(size_t i = 0; i != length; ++i) {
156 const char c = buf[i];
157
158 if(c < '0' || c > '9') {
159 throw Invalid_Argument("BigInt::decode: invalid decimal char");
160 }
161
162 const uint8_t x = c - '0';
163 BOTAN_ASSERT_NOMSG(x < 10);
164
165 r *= 10;
166 r += x;
167 }
168 return r;
169 } else {
170 throw Invalid_Argument("Unknown BigInt decoding method");
171 }
172}
secure_vector< uint8_t > hex_decode_locked(const char input[], size_t input_length, bool ignore_ws)
Definition hex.cpp:136
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition mem_ops.h:277
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61

References Binary, BOTAN_ASSERT_NOMSG, Botan::cast_uint8_ptr_to_char(), Decimal, from_bytes(), Botan::hex_decode_locked(), and Hexadecimal.

◆ decode() [3/4]

static BigInt Botan::BigInt::decode ( std::span< const uint8_t > buf)
inlinestatic

Create a BigInt from an integer in a byte array

Parameters
bufthe binary value to load
Returns
BigInt representing the integer in the byte array

Definition at line 876 of file bigint.h.

876 {
877 return BigInt::from_bytes(buf);
878 }

◆ decode() [4/4]

BigInt Botan::BigInt::decode ( std::span< const uint8_t > buf,
Base base )
static

Create a BigInt from an integer in a byte array

Parameters
bufthe binary value to load
basenumber-base of the integer in buf
Returns
BigInt representing the integer in the byte array

Definition at line 120 of file big_code.cpp.

120 {
121 if(base == Binary) {
122 return BigInt::from_bytes(buf);
123 }
124 return BigInt::decode(buf.data(), buf.size(), base);
125}

References Binary, decode(), and from_bytes().

◆ encode()

static std::vector< uint8_t > Botan::BigInt::encode ( const BigInt & n)
inlinestatic

Encode the integer value from a BigInt to a std::vector of bytes

Parameters
nthe BigInt to use as integer source
Returns
secure_vector of bytes containing the bytes of the integer

Definition at line 841 of file bigint.h.

841 {
842 return n.serialize<std::vector<uint8_t>>(n.bytes());
843 }

◆ encode_1363() [1/3]

static secure_vector< uint8_t > Botan::BigInt::encode_1363 ( const BigInt & n,
size_t bytes )
inlinestatic

Encode a BigInt to a byte array according to IEEE 1363

Parameters
nthe BigInt to encode
bytesthe length of the resulting secure_vector<uint8_t>
Returns
a secure_vector<uint8_t> containing the encoded BigInt

Definition at line 905 of file bigint.h.

905 {
906 return n.serialize<secure_vector<uint8_t>>(bytes);
907 }

References serialize().

Referenced by Botan::EC_Point::x_bytes(), Botan::EC_Point::xy_bytes(), and Botan::EC_Point::y_bytes().

◆ encode_1363() [2/3]

static void Botan::BigInt::encode_1363 ( std::span< uint8_t > out,
const BigInt & n )
inlinestatic

Definition at line 909 of file bigint.h.

909 {
910 n.serialize_to(out);
911 }

◆ encode_1363() [3/3]

static void Botan::BigInt::encode_1363 ( uint8_t out[],
size_t bytes,
const BigInt & n )
inlinestatic

Definition at line 914 of file bigint.h.

914 {
915 n.serialize_to(std::span{out, bytes});
916 }

◆ encode_fixed_length_int_pair()

secure_vector< uint8_t > Botan::BigInt::encode_fixed_length_int_pair ( const BigInt & n1,
const BigInt & n2,
size_t bytes )
static

Encode two BigInt to a byte array according to IEEE 1363

Parameters
n1the first BigInt to encode
n2the second BigInt to encode
bytesthe length of the encoding of each single BigInt
Returns
a secure_vector<uint8_t> containing the concatenation of the two encoded BigInt

Definition at line 106 of file big_code.cpp.

106 {
107 if(n1.is_negative() || n2.is_negative()) {
108 throw Encoding_Error("encode_fixed_length_int_pair: values must be positive");
109 }
110 if(n1.bytes() > bytes || n2.bytes() > bytes) {
111 throw Encoding_Error("encode_fixed_length_int_pair: values too large to encode properly");
112 }
113 secure_vector<uint8_t> output(2 * bytes);
114 BufferStuffer stuffer(output);
115 n1.serialize_to(stuffer.next(bytes));
116 n2.serialize_to(stuffer.next(bytes));
117 return output;
118}

References bytes(), is_negative(), Botan::BufferStuffer::next(), and serialize_to().

◆ encode_locked()

static secure_vector< uint8_t > Botan::BigInt::encode_locked ( const BigInt & n)
inlinestatic

Encode the integer value from a BigInt to a secure_vector of bytes

Parameters
nthe BigInt to use as integer source
Returns
secure_vector of bytes containing the bytes of the integer

Definition at line 850 of file bigint.h.

850 {
851 return n.serialize<secure_vector<uint8_t>>(n.bytes());
852 }

◆ encode_words()

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.

192 {
193 const size_t words = sig_words();
194
195 if(words > size) {
196 throw Encoding_Error("BigInt::encode_words value too large to encode");
197 }
198
199 clear_mem(out, size);
200 copy_mem(out, _data(), words);
201}
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:146

References _data(), Botan::clear_mem(), Botan::copy_mem(), sig_words(), and size().

◆ flip_sign()

void Botan::BigInt::flip_sign ( )
inline

Flip the sign of this BigInt

Definition at line 587 of file bigint.h.

Sign reverse_sign() const
Definition bigint.h:577

Referenced by Botan::ct_divide_word(), Botan::BER_Decoder::decode(), Botan::is_lucas_probable_prime(), and operator-().

◆ from_bytes()

◆ from_bytes_with_max_bits()

BigInt Botan::BigInt::from_bytes_with_max_bits ( const uint8_t buf[],
size_t length,
size_t max_bits )
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.

Parameters
bufthe byte array holding the value
lengthsize of buf
max_bitsif 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.

109 {
110 const size_t input_bits = 8 * length;
111
112 auto bn = BigInt::from_bytes(std::span{input, length});
113
114 if(input_bits > max_bits) {
115 const size_t bits_to_shift = input_bits - max_bits;
116
117 bn >>= bits_to_shift;
118 }
119
120 return bn;
121}

References from_bytes().

◆ from_s32()

BigInt Botan::BigInt::from_s32 ( int32_t n)
static

Create BigInt from a signed 32 bit integer

Parameters
ninitial value of this BigInt

Definition at line 49 of file bigint.cpp.

49 {
50 if(n >= 0) {
51 return BigInt::from_u64(static_cast<uint64_t>(n));
52 } else {
53 return -BigInt::from_u64(static_cast<uint64_t>(-n));
54 }
55}
static BigInt from_u64(uint64_t n)
Definition bigint.cpp:28

References from_u64().

Referenced by botan_mp_set_from_int(), and Botan::sqrt_modulo_prime().

◆ from_string()

BigInt Botan::BigInt::from_string ( std::string_view str)
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

Parameters
strthe string to parse for an integer value

Definition at line 91 of file bigint.cpp.

91 {
92 return BigInt(str);
93}

References BigInt().

◆ from_u64()

BigInt Botan::BigInt::from_u64 ( uint64_t n)
static

Create BigInt from an unsigned 64 bit integer

Parameters
ninitial value of this BigInt

Definition at line 28 of file bigint.cpp.

28 {
29 BigInt bn;
30
31#if BOTAN_MP_WORD_BITS == 64
32 bn.set_word_at(0, n);
33#else
34 bn.set_word_at(1, static_cast<word>(n >> 32));
35 bn.set_word_at(0, static_cast<word>(n));
36#endif
37
38 return bn;
39}

References set_word_at().

Referenced by Botan::DER_Encoder::encode(), Botan::DER_Encoder::encode(), from_s32(), and Botan::RSA_PrivateKey::RSA_PrivateKey().

◆ from_word()

BigInt Botan::BigInt::from_word ( word n)
static

Create BigInt from a word (limb)

Parameters
ninitial value of this BigInt

Definition at line 42 of file bigint.cpp.

42 {
43 BigInt bn;
44 bn.set_word_at(0, n);
45 return bn;
46}

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().

◆ get_bit()

bool Botan::BigInt::get_bit ( size_t n) const
inline

Return bit value at specified position

Parameters
nthe bit offset to test
Returns
true, if the bit at position n is set, false otherwise

Definition at line 497 of file bigint.h.

497{ return ((word_at(n / BOTAN_MP_WORD_BITS) >> (n % BOTAN_MP_WORD_BITS)) & 1); }

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().

◆ get_substring()

uint32_t Botan::BigInt::get_substring ( size_t offset,
size_t length ) const

Return (a maximum of) 32 bits of the complete value

Parameters
offsetthe offset to start extracting
lengthamount of bits to extract (starting at offset)
Returns
the integer extracted from the register starting at offset with specified length

Definition at line 227 of file bigint.cpp.

227 {
228 if(length == 0 || length > 32) {
229 throw Invalid_Argument("BigInt::get_substring invalid substring length");
230 }
231
232 const uint32_t mask = 0xFFFFFFFF >> (32 - length);
233
234 const size_t word_offset = offset / BOTAN_MP_WORD_BITS;
235 const size_t wshift = (offset % BOTAN_MP_WORD_BITS);
236
237 /*
238 * The substring is contained within one or at most two words. The
239 * offset and length are not secret, so we can perform conditional
240 * operations on those values.
241 */
242 const word w0 = word_at(word_offset);
243
244 if(wshift == 0 || (offset + length) / BOTAN_MP_WORD_BITS == word_offset) {
245 return static_cast<uint32_t>(w0 >> wshift) & mask;
246 } else {
247 const word w1 = word_at(word_offset + 1);
248 return static_cast<uint32_t>((w0 >> wshift) | (w1 << (BOTAN_MP_WORD_BITS - wshift))) & mask;
249 }
250}

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().

◆ get_word_vector() [1/2]

secure_vector< word > & Botan::BigInt::get_word_vector ( )
inline

Don't use this function in application code

Definition at line 652 of file bigint.h.

652 {
653 return m_data.mutable_vector();
654 }

◆ get_word_vector() [2/2]

const secure_vector< word > & Botan::BigInt::get_word_vector ( ) const
inline

Don't use this function in application code

Definition at line 659 of file bigint.h.

659 {
660 return m_data.const_vector();
661 }

◆ grow_to()

◆ is_equal()

bool Botan::BigInt::is_equal ( const BigInt & n) const

Compare this to another BigInt

Parameters
nthe BigInt value to compare with
Returns
true if this == n or false otherwise

Definition at line 168 of file bigint.cpp.

168 {
169 if(this->sign() != other.sign()) {
170 return false;
171 }
172
173 return bigint_ct_is_eq(this->_data(), this->sig_words(), other._data(), other.sig_words()).as_bool();
174}
constexpr auto bigint_ct_is_eq(const W x[], size_t x_size, const W y[], size_t y_size) -> CT::Mask< W >
Definition mp_core.h:672

References _data(), Botan::bigint_ct_is_eq(), sig_words(), and sign().

Referenced by Botan::operator!=(), and Botan::operator==().

◆ is_even()

bool Botan::BigInt::is_even ( ) const
inline

◆ is_less_than()

bool Botan::BigInt::is_less_than ( const BigInt & n) const

Compare this to another BigInt

Parameters
nthe BigInt value to compare with
Returns
true if this < n or false otherwise

Definition at line 176 of file bigint.cpp.

176 {
177 if(this->is_negative() && other.is_positive()) {
178 return true;
179 }
180
181 if(this->is_positive() && other.is_negative()) {
182 return false;
183 }
184
185 if(other.is_negative() && this->is_negative()) {
186 return bigint_ct_is_lt(other._data(), other.sig_words(), this->_data(), this->sig_words()).as_bool();
187 }
188
189 return bigint_ct_is_lt(this->_data(), this->sig_words(), other._data(), other.sig_words()).as_bool();
190}
bool is_positive() const
Definition bigint.h:566
constexpr auto bigint_ct_is_lt(const W x[], size_t x_size, const W y[], size_t y_size, bool lt_or_equal=false) -> CT::Mask< W >
Definition mp_core.h:639

References _data(), Botan::bigint_ct_is_lt(), is_negative(), is_positive(), and sig_words().

Referenced by Botan::operator<().

◆ is_negative()

◆ is_nonzero()

bool Botan::BigInt::is_nonzero ( ) const
inline

Test if the integer is not zero

Returns
true if the integer is non-zero, false otherwise

Definition at line 452 of file bigint.h.

452{ return (!is_zero()); }
bool is_zero() const
Definition bigint.h:458

Referenced by Botan::ct_modulo(), Botan::inverse_mod(), and Botan::Modular_Reducer::reduce().

◆ is_odd()

bool Botan::BigInt::is_odd ( ) const
inline

Test if the integer has an odd value

Returns
true if the integer is odd, false otherwise

Definition at line 446 of file bigint.h.

446{ return (get_bit(0) == 1); }

Referenced by Botan::inverse_mod(), Botan::is_lucas_probable_prime(), Botan::power_mod(), and Botan::sqrt_modulo_prime().

◆ is_positive()

bool Botan::BigInt::is_positive ( ) const
inline

Tests if the sign of the integer is positive

Returns
true, iff the integer has a positive sign

Definition at line 566 of file bigint.h.

566{ return (sign() == Positive); }

Referenced by cmp(), is_less_than(), Botan::operator%(), Botan::redc_p192(), Botan::redc_p224(), Botan::redc_p256(), Botan::redc_p384(), and Botan::redc_p521().

◆ is_zero()

◆ mask_bits()

void Botan::BigInt::mask_bits ( size_t n)
inline

Clear all but the lowest n bits

Parameters
namount of bits to keep

Definition at line 490 of file bigint.h.

490{ m_data.mask_bits(n); }

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().

◆ mod_add()

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

Parameters
ythe BigInt to add - assumed y >= 0 and y < mod
modthe positive modulus
wsa temp workspace

Definition at line 45 of file big_ops2.cpp.

45 {
46 if(this->is_negative() || s.is_negative() || mod.is_negative()) {
47 throw Invalid_Argument("BigInt::mod_add expects all arguments are positive");
48 }
49
50 BOTAN_DEBUG_ASSERT(*this < mod);
51 BOTAN_DEBUG_ASSERT(s < mod);
52
53 /*
54 t + s or t + s - p == t - (p - s)
55
56 So first compute ws = p - s
57
58 Then compute t + s and t - ws
59
60 If t - ws does not borrow, then that is the correct valued
61 */
62
63 const size_t mod_sw = mod.sig_words();
64 BOTAN_ARG_CHECK(mod_sw > 0, "BigInt::mod_add modulus must be positive");
65
66 this->grow_to(mod_sw);
67 s.grow_to(mod_sw);
68
69 // First mod_sw for p - s, 2*mod_sw for bigint_addsub workspace
70 if(ws.size() < 3 * mod_sw) {
71 ws.resize(3 * mod_sw);
72 }
73
74 word borrow = bigint_sub3(&ws[0], mod._data(), mod_sw, s._data(), mod_sw);
75 BOTAN_DEBUG_ASSERT(borrow == 0);
76 BOTAN_UNUSED(borrow);
77
78 // Compute t - ws
79 borrow = bigint_sub3(&ws[mod_sw], this->_data(), mod_sw, &ws[0], mod_sw);
80
81 // Compute t + s
82 bigint_add3_nc(&ws[mod_sw * 2], this->_data(), mod_sw, s._data(), mod_sw);
83
84 CT::conditional_copy_mem(borrow, &ws[0], &ws[mod_sw * 2], &ws[mod_sw], mod_sw);
85 set_words(&ws[0], mod_sw);
86
87 return (*this);
88}
#define BOTAN_UNUSED
Definition assert.h:118
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29
void set_words(const word w[], size_t len)
Definition bigint.h:552
constexpr Mask< T > conditional_copy_mem(Mask< T > mask, T *to, const T *from0, const T *from1, size_t elems)
Definition ct_utils.h:699
constexpr auto bigint_add3_nc(W z[], const W x[], size_t x_size, const W y[], size_t y_size) -> W
Definition mp_core.h:232

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+().

◆ mod_mul()

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

Parameters
ythe small integer to multiply by
modthe positive modulus
wsa temp workspace

Definition at line 119 of file big_ops2.cpp.

119 {
120 BOTAN_ARG_CHECK(this->is_negative() == false, "*this must be positive");
121 BOTAN_ARG_CHECK(y < 16, "y too large");
122
123 BOTAN_DEBUG_ASSERT(*this < mod);
124
125 *this *= static_cast<word>(y);
126 this->reduce_below(mod, ws);
127 return (*this);
128}
size_t reduce_below(const BigInt &mod, secure_vector< word > &ws)
Definition bigint.cpp:317

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().

◆ mod_sub()

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

Parameters
ythe BigInt to subtract - assumed y >= 0 and y < mod
modthe positive modulus
wsa temp workspace

Definition at line 90 of file big_ops2.cpp.

90 {
91 if(this->is_negative() || s.is_negative() || mod.is_negative()) {
92 throw Invalid_Argument("BigInt::mod_sub expects all arguments are positive");
93 }
94
95 // We are assuming in this function that *this and s are no more than mod_sw words long
96 BOTAN_DEBUG_ASSERT(*this < mod);
97 BOTAN_DEBUG_ASSERT(s < mod);
98
99 const size_t mod_sw = mod.sig_words();
100
101 this->grow_to(mod_sw);
102 s.grow_to(mod_sw);
103
104 if(ws.size() < mod_sw) {
105 ws.resize(mod_sw);
106 }
107
108 if(mod_sw == 4) {
109 bigint_mod_sub_n<4>(mutable_data(), s._data(), mod._data(), ws.data());
110 } else if(mod_sw == 6) {
111 bigint_mod_sub_n<6>(mutable_data(), s._data(), mod._data(), ws.data());
112 } else {
113 bigint_mod_sub(mutable_data(), s._data(), mod._data(), mod_sw, ws.data());
114 }
115
116 return (*this);
117}
constexpr void bigint_mod_sub_n(W t[], const W s[], const W mod[], W ws[])
Definition mp_core.h:757
constexpr void bigint_mod_sub(W t[], const W s[], const W mod[], size_t mod_sw, W ws[])
Definition mp_core.h:739

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().

◆ mul()

BigInt & Botan::BigInt::mul ( const BigInt & y,
secure_vector< word > & ws )

Multiply this with y

Parameters
ythe BigInt to multiply with this
wsa temp workspace

Definition at line 156 of file big_ops2.cpp.

156 {
157 const size_t x_sw = sig_words();
158 const size_t y_sw = y.sig_words();
159 set_sign((sign() == y.sign()) ? Positive : Negative);
160
161 if(x_sw == 0 || y_sw == 0) {
162 clear();
164 } else if(x_sw == 1 && y_sw) {
165 grow_to(y_sw + 1);
166 bigint_linmul3(mutable_data(), y._data(), y_sw, word_at(0));
167 } else if(y_sw == 1 && x_sw) {
168 word carry = bigint_linmul2(mutable_data(), x_sw, y.word_at(0));
169 set_word_at(x_sw, carry);
170 } else {
171 const size_t new_size = x_sw + y_sw + 1;
172 ws.resize(new_size);
173 secure_vector<word> z_reg(new_size);
174
175 bigint_mul(z_reg.data(), z_reg.size(), _data(), size(), x_sw, y._data(), y.size(), y_sw, ws.data(), ws.size());
176
177 this->swap_reg(z_reg);
178 }
179
180 return (*this);
181}
void clear()
Definition bigint.h:400
void swap_reg(secure_vector< word > &reg)
Definition bigint.h:199
constexpr void bigint_linmul3(W z[], const W x[], size_t x_size, W y)
Definition mp_core.h:569
void bigint_mul(word z[], size_t z_size, const word x[], size_t x_size, size_t x_sw, const word y[], size_t y_size, size_t y_sw, word workspace[], size_t ws_size)
Definition mp_karat.cpp:282
void carry(int64_t &h0, int64_t &h1)
constexpr auto bigint_linmul2(W x[], size_t x_size, W y) -> W
Definition mp_core.h:552

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().

◆ mutable_data()

◆ one()

static BigInt Botan::BigInt::one ( )
inlinestatic

Create a 1-value BigInt

Definition at line 55 of file bigint.h.

55{ return BigInt::from_u64(1); }

Referenced by Botan::is_lucas_probable_prime(), Botan::is_perfect_square(), and Botan::power_mod().

◆ operator!()

bool Botan::BigInt::operator! ( ) const
inline

! operator

Returns
true iff this is zero, otherwise false

Definition at line 308 of file bigint.h.

308{ return (!is_nonzero()); }
bool is_nonzero() const
Definition bigint.h:452

◆ operator%=() [1/2]

BigInt & Botan::BigInt::operator%= ( const BigInt & y)

Modulo operator

Parameters
ythe modulus to reduce this by

Definition at line 224 of file big_ops2.cpp.

224 {
225 return (*this = (*this) % mod);
226}

◆ operator%=() [2/2]

word Botan::BigInt::operator%= ( word y)

Modulo operator

Parameters
ythe modulus (word) to reduce this by

Definition at line 231 of file big_ops2.cpp.

231 {
232 if(mod == 0) {
233 throw Invalid_Argument("BigInt::operator%= divide by zero");
234 }
235
236 word remainder = 0;
237
238 if(is_power_of_2(mod)) {
239 remainder = (word_at(0) & (mod - 1));
240 } else {
241 const size_t sw = sig_words();
242 for(size_t i = sw; i > 0; --i) {
243 remainder = bigint_modop_vartime(remainder, word_at(i - 1), mod);
244 }
245 }
246
247 if(remainder && sign() == BigInt::Negative) {
248 remainder = mod - remainder;
249 }
250
251 m_data.set_to_zero();
252 m_data.set_word_at(0, remainder);
254 return remainder;
255}
constexpr bool is_power_of_2(T arg)
Definition bit_ops.h:45
constexpr auto bigint_modop_vartime(W n1, W n0, W d) -> W
Definition mp_core.h:813

References Botan::bigint_modop_vartime(), Botan::is_power_of_2(), Negative, Positive, set_sign(), sig_words(), sign(), and word_at().

◆ operator*=() [1/2]

BigInt & Botan::BigInt::operator*= ( const BigInt & y)

*= operator

Parameters
ythe BigInt to multiply with this

Definition at line 151 of file big_ops2.cpp.

151 {
153 return this->mul(y, ws);
154}
BigInt & mul(const BigInt &y, secure_vector< word > &ws)
Definition big_ops2.cpp:156

References mul().

◆ operator*=() [2/2]

BigInt & Botan::BigInt::operator*= ( word y)

*= operator

Parameters
ythe word to multiply with this

Definition at line 197 of file big_ops2.cpp.

197 {
198 if(y == 0) {
199 clear();
201 }
202
203 const word carry = bigint_linmul2(mutable_data(), size(), y);
205
206 return (*this);
207}

References Botan::bigint_linmul2(), Botan::carry(), clear(), mutable_data(), Positive, set_sign(), set_word_at(), and size().

◆ operator++() [1/2]

BigInt & Botan::BigInt::operator++ ( )
inline

Increment operator

Definition at line 273 of file bigint.h.

273{ return (*this += 1); }

◆ operator++() [2/2]

BigInt Botan::BigInt::operator++ ( int )
inline

Postfix increment operator

Definition at line 283 of file bigint.h.

283 {
284 BigInt x = (*this);
285 ++(*this);
286 return x;
287 }

◆ operator+=() [1/2]

BigInt & Botan::BigInt::operator+= ( const BigInt & y)
inline

+= operator

Parameters
ythe BigInt to add to this

Definition at line 208 of file bigint.h.

208{ return add(y._data(), y.sig_words(), y.sign()); }
BigInt & add(const word y[], size_t y_words, Sign sign)
Definition big_ops2.cpp:16

References _data(), sig_words(), and sign().

◆ operator+=() [2/2]

BigInt & Botan::BigInt::operator+= ( word y)
inline

+= operator

Parameters
ythe word to add to this

Definition at line 214 of file bigint.h.

214{ return add(&y, 1, Positive); }

◆ operator-()

BigInt Botan::BigInt::operator- ( ) const

Unary negation operator

Returns
negative this

Definition at line 311 of file bigint.cpp.

311 {
312 BigInt x = (*this);
313 x.flip_sign();
314 return x;
315}

References flip_sign().

◆ operator--() [1/2]

BigInt & Botan::BigInt::operator-- ( )
inline

Decrement operator

Definition at line 278 of file bigint.h.

278{ return (*this -= 1); }

◆ operator--() [2/2]

BigInt Botan::BigInt::operator-- ( int )
inline

Postfix decrement operator

Definition at line 292 of file bigint.h.

292 {
293 BigInt x = (*this);
294 --(*this);
295 return x;
296 }

◆ operator-=() [1/2]

BigInt & Botan::BigInt::operator-= ( const BigInt & y)
inline

-= operator

Parameters
ythe BigInt to subtract from this

Definition at line 220 of file bigint.h.

220{ return sub(y._data(), y.sig_words(), y.sign()); }
BigInt & sub(const word y[], size_t y_words, Sign sign)
Definition bigint.h:317

References _data(), sig_words(), and sign().

◆ operator-=() [2/2]

BigInt & Botan::BigInt::operator-= ( word y)
inline

-= operator

Parameters
ythe word to subtract from this

Definition at line 226 of file bigint.h.

226{ return sub(&y, 1, Positive); }

◆ operator/=()

BigInt & Botan::BigInt::operator/= ( const BigInt & y)

/= operator

Parameters
ythe BigInt to divide this by

Definition at line 212 of file big_ops2.cpp.

212 {
213 if(y.sig_words() == 1 && is_power_of_2(y.word_at(0))) {
214 (*this) >>= (y.bits() - 1);
215 } else {
216 (*this) = (*this) / y;
217 }
218 return (*this);
219}

References bits(), Botan::is_power_of_2(), sig_words(), and word_at().

◆ operator<<=()

BigInt & Botan::BigInt::operator<<= ( size_t shift)

Left shift operator

Parameters
shiftthe number of bits to shift this left by

Definition at line 260 of file big_ops2.cpp.

260 {
261 const size_t sw = sig_words();
262 const size_t new_size = sw + (shift + BOTAN_MP_WORD_BITS - 1) / BOTAN_MP_WORD_BITS;
263
264 m_data.grow_to(new_size);
265
266 bigint_shl1(m_data.mutable_data(), new_size, sw, shift);
267
268 return (*this);
269}
constexpr void bigint_shl1(W x[], size_t x_size, size_t x_words, size_t shift)
Definition mp_core.h:467

References Botan::bigint_shl1(), BOTAN_MP_WORD_BITS, and sig_words().

◆ operator=() [1/2]

BigInt & Botan::BigInt::operator= ( BigInt && other)
inline

Move assignment

Definition at line 175 of file bigint.h.

175 {
176 if(this != &other) {
177 this->swap(other);
178 }
179
180 return (*this);
181 }

◆ operator=() [2/2]

BigInt & Botan::BigInt::operator= ( const BigInt & )
default

Copy assignment

◆ operator>>=()

BigInt & Botan::BigInt::operator>>= ( size_t shift)

Right shift operator

Parameters
shiftthe number of bits to shift this right by

Definition at line 274 of file big_ops2.cpp.

274 {
275 bigint_shr1(m_data.mutable_data(), m_data.size(), shift);
276
277 if(is_negative() && is_zero()) {
279 }
280
281 return (*this);
282}
constexpr void bigint_shr1(W x[], size_t x_size, size_t shift)
Definition mp_core.h:486

References Botan::bigint_shr1(), is_negative(), is_zero(), Positive, and set_sign().

◆ power_of_2()

static BigInt Botan::BigInt::power_of_2 ( size_t n)
inlinestatic

Create a power of two

Parameters
nthe power of two to create
Returns
bigint representing 2^n

Definition at line 830 of file bigint.h.

830 {
831 BigInt b;
832 b.set_bit(n);
833 return b;
834 }
const SIMD_8x32 & b

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().

◆ random_integer()

BigInt Botan::BigInt::random_integer ( RandomNumberGenerator & rng,
const BigInt & min,
const BigInt & max )
static
Parameters
rnga random number generator
minthe minimum value (must be non-negative)
maxthe maximum value (must be non-negative and > min)
Returns
random integer in [min,max)

Definition at line 43 of file big_rand.cpp.

43 {
44 if(min.is_negative() || max.is_negative() || max <= min) {
45 throw Invalid_Argument("BigInt::random_integer invalid range");
46 }
47
48 /*
49 If min is > 1 then we generate a random number `r` in [0,max-min)
50 and return min + r.
51
52 This same logic could also be reasonbly chosen for min == 1, but
53 that breaks certain tests which expect stability of this function
54 when generating within [1,n)
55 */
56 if(min > 1) {
57 const BigInt diff = max - min;
58 // This call is recursive, but will not recurse further
59 return min + BigInt::random_integer(rng, BigInt::zero(), diff);
60 }
61
62 BOTAN_DEBUG_ASSERT(min <= 1);
63
64 const size_t bits = max.bits();
65
66 BigInt r;
67
68 do {
69 r.randomize(rng, bits, false);
70 } while(r < min || r >= max);
71
72 return r;
73}
static BigInt zero()
Definition bigint.h:50
static BigInt random_integer(RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)
Definition big_rand.cpp:43

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().

◆ randomize()

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.

Parameters
rngthe random number generator to use
bitsizenumber of bits the created random value should have
set_high_bitif true, the highest bit is always set

Definition at line 18 of file big_rand.cpp.

18 {
20
21 if(bitsize == 0) {
22 clear();
23 } else {
24 secure_vector<uint8_t> array = rng.random_vec(round_up(bitsize, 8) / 8);
25
26 // Always cut unwanted bits
27 if(bitsize % 8) {
28 array[0] &= 0xFF >> (8 - (bitsize % 8));
29 }
30
31 // Set the highest bit if wanted
32 if(set_high_bit) {
33 array[0] |= 0x80 >> ((bitsize % 8) ? (8 - bitsize % 8) : 0);
34 }
35
36 assign_from_bytes(array);
37 }
38}

References clear(), Positive, Botan::RandomNumberGenerator::random_vec(), Botan::round_up(), and set_sign().

Referenced by BigInt(), and random_integer().

◆ reduce_below()

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.

317 {
318 if(p.is_negative() || this->is_negative()) {
319 throw Invalid_Argument("BigInt::reduce_below both values must be positive");
320 }
321
322 const size_t p_words = p.sig_words();
323
324 if(size() < p_words + 1) {
325 grow_to(p_words + 1);
326 }
327
328 if(ws.size() < p_words + 1) {
329 ws.resize(p_words + 1);
330 }
331
332 clear_mem(ws.data(), ws.size());
333
334 size_t reductions = 0;
335
336 for(;;) {
337 word borrow = bigint_sub3(ws.data(), _data(), p_words + 1, p._data(), p_words);
338 if(borrow) {
339 break;
340 }
341
342 ++reductions;
343 swap_reg(ws);
344 }
345
346 return reductions;
347}

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().

◆ resize()

void Botan::BigInt::resize ( size_t s)
inline

Definition at line 669 of file bigint.h.

669{ m_data.resize(s); }

Referenced by ct_shift_left().

◆ rev_sub()

BigInt & Botan::BigInt::rev_sub ( const word y[],
size_t y_words,
secure_vector< word > & ws )

Set *this to y - *this

Parameters
ythe BigInt to subtract from as a sequence of words
y_wordslength of y in words
wsa temp workspace

Definition at line 130 of file big_ops2.cpp.

130 {
131 if(this->sign() != BigInt::Positive) {
132 throw Invalid_State("BigInt::sub_rev requires this is positive");
133 }
134
135 const size_t x_sw = this->sig_words();
136
137 ws.resize(std::max(x_sw, y_sw));
138 clear_mem(ws.data(), ws.size());
139
140 const int32_t relative_size = bigint_sub_abs(ws.data(), _data(), x_sw, y, y_sw);
141
142 this->cond_flip_sign(relative_size > 0);
143 this->swap_reg(ws);
144
145 return (*this);
146}

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().

◆ reverse_sign()

Sign Botan::BigInt::reverse_sign ( ) const
inline
Returns
the opposite sign of the represented integer value

Definition at line 577 of file bigint.h.

577 {
578 if(sign() == Positive) {
579 return Negative;
580 }
581 return Positive;
582 }

Referenced by Botan::operator-().

◆ serialize() [1/2]

template<typename T = std::vector<uint8_t>>
T Botan::BigInt::serialize ( ) const
inline

Serialize the value of this BigInt as a big endian encoding.

Definition at line 725 of file bigint.h.

725 {
726 return serialize<T>(this->bytes());
727 }
T serialize() const
Definition bigint.h:725

◆ serialize() [2/2]

template<typename T = std::vector<uint8_t>>
T Botan::BigInt::serialize ( size_t len) const
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.

712 {
713 // TODO this supports std::vector and secure_vector
714 // it would be nice if this also could work with std::array as in
715 // bn.serialize_to<std::array<uint8_t, 32>>(32);
716 T out(len);
717 this->serialize_to(out);
718 return out;
719 }
FE_25519 T
Definition ge.cpp:34

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().

◆ serialize_to()

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.

Parameters
outdestination byte span for the integer value

Definition at line 383 of file bigint.cpp.

383 {
384 BOTAN_ARG_CHECK(this->bytes() <= output.size(), "Insufficient output space");
385
386 this->binary_encode(output.data(), output.size());
387}
void binary_encode(uint8_t buf[]) const
Definition bigint.h:733

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().

◆ set_bit()

void Botan::BigInt::set_bit ( size_t n)
inline

Set bit at specified position

Parameters
nbit position to set

Definition at line 464 of file bigint.h.

464{ conditionally_set_bit(n, true); }
void conditionally_set_bit(size_t n, bool set_it)
Definition bigint.h:474

Referenced by Botan::generate_dsa_primes(), Botan::generate_rsa_prime(), Botan::Modular_Reducer::Modular_Reducer(), and Botan::random_prime().

◆ set_sign()

void Botan::BigInt::set_sign ( Sign sign)
inline

Set sign of the integer

Parameters
signnew Sign to set

Definition at line 593 of file bigint.h.

593 {
594 if(sign == Negative && is_zero()) {
595 sign = Positive;
596 }
597
598 m_signedness = sign;
599 }

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().

◆ set_word_at()

void Botan::BigInt::set_word_at ( size_t i,
word w )
inline

Definition at line 550 of file bigint.h.

550{ m_data.set_word_at(i, w); }

Referenced by ct_cond_assign(), from_u64(), from_word(), mul(), and operator*=().

◆ set_words()

void Botan::BigInt::set_words ( const word w[],
size_t len )
inline

Definition at line 552 of file bigint.h.

552 {
553 m_data.set_words(w, len);
554 }

Referenced by Botan::EC_Point::add(), Botan::EC_Point::add_affine(), mod_add(), and Botan::Montgomery_Int::Montgomery_Int().

◆ sig_words()

◆ sign()

Sign Botan::BigInt::sign ( ) const
inline

Return the sign of the integer

Returns
the sign of the integer

Definition at line 572 of file bigint.h.

572{ return (m_signedness); }

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().

◆ size()

◆ square()

BigInt & Botan::BigInt::square ( secure_vector< word > & ws)

Square value of *this

Parameters
wsa temp workspace

Definition at line 183 of file big_ops2.cpp.

183 {
184 const size_t sw = sig_words();
185
186 secure_vector<word> z(2 * sw);
187 ws.resize(z.size());
188
189 bigint_sqr(z.data(), z.size(), _data(), size(), sw, ws.data(), ws.size());
190
191 swap_reg(z);
193
194 return (*this);
195}
void bigint_sqr(word z[], size_t z_size, const word x[], size_t x_size, size_t x_sw, word workspace[], size_t ws_size)
Definition mp_karat.cpp:326

References _data(), Botan::bigint_sqr(), Positive, set_sign(), sig_words(), size(), and swap_reg().

Referenced by Botan::square().

◆ sub()

BigInt & Botan::BigInt::sub ( const word y[],
size_t y_words,
Sign sign )
inline

Definition at line 317 of file bigint.h.

317 {
318 return add(y, y_words, sign == Positive ? Negative : Positive);
319 }

◆ swap()

void Botan::BigInt::swap ( BigInt & other)
inline

Swap this value with another

Parameters
otherBigInt to swap values with

Definition at line 192 of file bigint.h.

192 {
193 m_data.swap(other.m_data);
194 std::swap(m_signedness, other.m_signedness);
195 }

Referenced by Botan::EC_Point::add_affine(), Botan::EC_Point::mult2(), and Botan::EC_Point::swap_coords().

◆ swap_reg()

void Botan::BigInt::swap_reg ( secure_vector< word > & reg)
inline

Definition at line 199 of file bigint.h.

199 {
200 m_data.swap(reg);
201 // sign left unchanged
202 }

Referenced by mul(), reduce_below(), rev_sub(), and square().

◆ to_dec_string()

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.

16 {
17 // Use the largest power of 10 that fits in a word
18#if(BOTAN_MP_WORD_BITS == 64)
19 const word conversion_radix = 10000000000000000000U;
20 const word radix_digits = 19;
21#else
22 const word conversion_radix = 1000000000U;
23 const word radix_digits = 9;
24#endif
25
26 // (over-)estimate of the number of digits needed; log2(10) ~ 3.3219
27 const size_t digit_estimate = static_cast<size_t>(1 + (this->bits() / 3.32));
28
29 // (over-)estimate of db such that conversion_radix^db > *this
30 const size_t digit_blocks = (digit_estimate + radix_digits - 1) / radix_digits;
31
32 BigInt value = *this;
33 value.set_sign(Positive);
34
35 // Extract groups of digits into words
36 std::vector<word> digit_groups(digit_blocks);
37
38 for(size_t i = 0; i != digit_blocks; ++i) {
39 word remainder = 0;
40 ct_divide_word(value, conversion_radix, value, remainder);
41 digit_groups[i] = remainder;
42 }
43
44 BOTAN_ASSERT_NOMSG(value.is_zero());
45
46 // Extract digits from the groups
47 std::vector<uint8_t> digits(digit_blocks * radix_digits);
48
49 for(size_t i = 0; i != digit_blocks; ++i) {
50 word remainder = digit_groups[i];
51 for(size_t j = 0; j != radix_digits; ++j) {
52 // Compiler should convert div/mod by 10 into mul by magic constant
53 const word digit = remainder % 10;
54 remainder /= 10;
55 digits[radix_digits * i + j] = static_cast<uint8_t>(digit);
56 }
57 }
58
59 // remove leading zeros
60 while(!digits.empty() && digits.back() == 0) {
61 digits.pop_back();
62 }
63
64 BOTAN_ASSERT_NOMSG(digit_estimate >= digits.size());
65
66 // Reverse the digits to big-endian and format to text
67 std::string s;
68 s.reserve(1 + digits.size());
69
70 if(is_negative()) {
71 s += "-";
72 }
73
74 // Reverse and convert to textual digits
75 for(auto i = digits.rbegin(); i != digits.rend(); ++i) {
76 s.push_back(*i + '0'); // assumes ASCII
77 }
78
79 if(s.empty()) {
80 s += "0";
81 }
82
83 return s;
84}
void ct_divide_word(const BigInt &x, word y, BigInt &q_out, word &r_out)
Definition divide.cpp:80

References bits(), BOTAN_ASSERT_NOMSG, Botan::ct_divide_word(), is_negative(), is_zero(), Positive, and set_sign().

Referenced by Botan::operator<<().

◆ to_hex_string()

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.

86 {
87 const size_t this_bytes = this->bytes();
88 std::vector<uint8_t> bits(std::max<size_t>(1, this_bytes));
89
90 if(this_bytes > 0) {
91 this->serialize_to(bits);
92 }
93
94 std::string hrep;
95 if(is_negative()) {
96 hrep += "-";
97 }
98 hrep += "0x";
99 hrep += hex_encode(bits);
100 return hrep;
101}
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition hex.cpp:35

References bits(), bytes(), Botan::hex_encode(), is_negative(), and serialize_to().

Referenced by Botan::operator<<().

◆ to_u32bit()

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.

Returns
the value as a uint32_t if conversion is possible

Definition at line 255 of file bigint.cpp.

255 {
256 if(is_negative()) {
257 throw Encoding_Error("BigInt::to_u32bit: Number is negative");
258 }
259 if(bits() > 32) {
260 throw Encoding_Error("BigInt::to_u32bit: Number is too big to convert");
261 }
262
263 uint32_t out = 0;
264 for(size_t i = 0; i != 4; ++i) {
265 out = (out << 8) | byte_at(3 - i);
266 }
267 return out;
268}
uint8_t byte_at(size_t n) const
Definition bigint.cpp:130

References bits(), byte_at(), and is_negative().

◆ top_bits_free()

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.

286 {
287 const size_t words = sig_words();
288
289 const word top_word = word_at(words - 1);
290 const size_t bits_used = high_bit(CT::value_barrier(top_word));
291 CT::unpoison(bits_used);
292 return BOTAN_MP_WORD_BITS - bits_used;
293}
constexpr T value_barrier(T x)
Definition ct_utils.h:252
constexpr void unpoison(const T *p, size_t n)
Definition ct_utils.h:64
constexpr size_t high_bit(T n)
Definition bit_ops.h:58

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().

◆ with_capacity()

BigInt Botan::BigInt::with_capacity ( size_t n)
static

Create BigInt of specified size, all zeros

Parameters
nsize of the internal register in words

Definition at line 58 of file bigint.cpp.

58 {
59 BigInt bn;
60 bn.grow_to(size);
61 return bn;
62}

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().

◆ word_at()

word Botan::BigInt::word_at ( size_t n) const
inline

◆ zero()

static BigInt Botan::BigInt::zero ( )
inlinestatic

Friends And Related Symbol Documentation

◆ swap

void swap ( BigInt & x,
BigInt & y )
friend

Definition at line 197 of file bigint.h.

197{ x.swap(y); }

The documentation for this class was generated from the following files: