Botan 3.0.0-alpha0
Crypto and TLS for C&
Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
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

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 std::string &str)
 
template<typename Alloc >
 BigInt (const std::vector< uint8_t, Alloc > &vec)
 
 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. More...
 
 BigInt (uint64_t n)
 
template<typename Alloc >
void binary_decode (const std::vector< uint8_t, Alloc > &buf)
 
void binary_decode (const uint8_t buf[], size_t length)
 
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)
 
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
 
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)
 
template<typename Alloc >
static BigInt decode (const std::vector< uint8_t, Alloc > &buf)
 
template<typename Alloc >
static BigInt decode (const std::vector< uint8_t, Alloc > &buf, Base base)
 
static BigInt decode (const uint8_t buf[], size_t length)
 
static BigInt decode (const uint8_t buf[], size_t length, 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 (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_with_max_bits (const uint8_t buf[], size_t length, size_t max_bits)
 
static BigInt from_s32 (int32_t n)
 
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 ()
 

Detailed Description

Arbitrary precision integer

Definition at line 24 of file bigint.h.

Member Enumeration Documentation

◆ Base

Base enumerator for encoding and decoding

Enumerator
Decimal 
Hexadecimal 
Binary 

Definition at line 30 of file bigint.h.

30{ Decimal = 10, Hexadecimal = 16, Binary = 256 };
@ Hexadecimal
Definition: bigint.h:30

◆ Sign

Sign symbol definitions for positive and negative numbers

Enumerator
Negative 
Positive 

Definition at line 35 of file bigint.h.

35{ Negative = 0, Positive = 1 };

Constructor & Destructor Documentation

◆ BigInt() [1/9]

Botan::BigInt::BigInt ( )
default

Create empty (zero) BigInt

◆ BigInt() [2/9]

Botan::BigInt::BigInt ( uint64_t  n)

Create BigInt from an unsigned 64 bit integer

Parameters
ninitial value of this BigInt

Definition at line 17 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 ( const std::string &  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 70 of file bigint.cpp.

71 {
72 Base base = Decimal;
73 size_t markers = 0;
74 bool negative = false;
75
76 if(str.length() > 0 && str[0] == '-')
77 {
78 markers += 1;
79 negative = true;
80 }
81
82 if(str.length() > markers + 2 && str[markers ] == '0' &&
83 str[markers + 1] == 'x')
84 {
85 markers += 2;
86 base = Hexadecimal;
87 }
88
89 *this = decode(cast_char_ptr_to_uint8(str.data()) + markers,
90 str.length() - markers, base);
91
92 if(negative) set_sign(Negative);
93 else set_sign(Positive);
94 }
static BigInt decode(const uint8_t buf[], size_t length)
Definition: bigint.h:790
void set_sign(Sign sign)
Definition: bigint.h:577
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition: mem_ops.h:183

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 
)

Create a BigInt from an integer in a byte array

Parameters
bufthe byte array holding the value
lengthsize of buf

Definition at line 96 of file bigint.cpp.

97 {
98 binary_decode(input, length);
99 }
void binary_decode(const uint8_t buf[], size_t length)
Definition: bigint.cpp:428

References binary_decode().

◆ BigInt() [6/9]

template<typename Alloc >
Botan::BigInt::BigInt ( const std::vector< uint8_t, Alloc > &  vec)
inlineexplicit

Create a BigInt from an integer in a byte array

Parameters
vecthe byte vector holding the value

Definition at line 103 of file bigint.h.

103: BigInt(vec.data(), vec.size()) {}
BigInt()=default

◆ 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.

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

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 127 of file bigint.cpp.

128 {
129 randomize(rng, bits, set_high_bit);
130 }
void randomize(RandomNumberGenerator &rng, size_t bitsize, bool set_high_bit=true)
Definition: big_rand.cpp:17
size_t bits() const
Definition: bigint.cpp:309

References bits(), and randomize().

◆ BigInt() [9/9]

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

Move constructor

Definition at line 143 of file bigint.h.

144 {
145 this->swap(other);
146 }
void swap(BigInt &other)
Definition: bigint.h:170

◆ ~BigInt()

Botan::BigInt::~BigInt ( )
inline

Definition at line 148 of file bigint.h.

void const_time_unpoison() const
Definition: bigint.h:735

Member Function Documentation

◆ abs()

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

Definition at line 388 of file bigint.cpp.

389 {
390 BigInt x = (*this);
391 x.set_sign(Positive);
392 return x;
393 }

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 15 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 {
23 bigint_add2(mutable_data(), size() - 1, y, y_words);
24 }
25 else
26 {
27 const int32_t relative_size = bigint_cmp(data(), x_sw, y, y_words);
28
29 if(relative_size >= 0)
30 {
31 // *this >= y
32 bigint_sub2(mutable_data(), x_sw, y, y_words);
33 }
34 else
35 {
36 // *this < y
37 bigint_sub2_rev(mutable_data(), y, y_words);
38 }
39
40 //this->sign_fixup(relative_size, y_sign);
41 if(relative_size < 0)
42 set_sign(y_sign);
43 else if(relative_size == 0)
45 }
46
47 return (*this);
48 }
size_t sig_words() const
Definition: bigint.h:600
word * mutable_data()
Definition: bigint.h:628
size_t size() const
Definition: bigint.h:594
void grow_to(size_t n) const
Definition: bigint.h:650
const word * data() const
Definition: bigint.h:634
Sign sign() const
Definition: bigint.h:553
void bigint_sub2_rev(word x[], const word y[], size_t y_size)
Definition: mp_core.h:324
word bigint_sub2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.h:300
void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.h:280
int32_t bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.h:525

References Botan::bigint_add2(), Botan::bigint_cmp(), Botan::bigint_sub2(), Botan::bigint_sub2_rev(), data(), 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 18 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 {
26 bigint_add3(z.mutable_data(), x.data(), x_sw, y, y_words);
27 z.set_sign(x.sign());
28 }
29 else
30 {
31 const int32_t relative_size = bigint_sub_abs(z.mutable_data(), x.data(), x_sw, y, y_words);
32
33 //z.sign_fixup(relative_size, y_sign);
34 if(relative_size < 0)
35 z.set_sign(y_sign);
36 else if(relative_size == 0)
37 z.set_sign(BigInt::Positive);
38 else
39 z.set_sign(x.sign());
40 }
41
42 return z;
43 }
static BigInt with_capacity(size_t n)
Definition: bigint.cpp:60
void bigint_add3(word z[], const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.h:289
CT::Mask< word > bigint_sub_abs(word z[], const word x[], const word y[], size_t N, word ws[])
Definition: mp_core.h:377

References Botan::bigint_add3(), Botan::bigint_sub_abs(), data(), mutable_data(), Positive, set_sign(), sig_words(), sign(), and with_capacity().

Referenced by Botan::operator+(), and Botan::operator-().

◆ binary_decode() [1/2]

template<typename Alloc >
void Botan::BigInt::binary_decode ( const std::vector< uint8_t, Alloc > &  buf)
inline

Read integer value from a byte vector

Parameters
bufthe vector to load from

Definition at line 697 of file bigint.h.

698 {
699 binary_decode(buf.data(), buf.size());
700 }

◆ binary_decode() [2/2]

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

Read integer value from a byte array with given size

Parameters
bufbyte array buffer containing the integer
lengthsize of buf

Definition at line 428 of file bigint.cpp.

429 {
430 clear();
431
432 const size_t full_words = length / sizeof(word);
433 const size_t extra_bytes = length % sizeof(word);
434
435 secure_vector<word> reg((round_up(full_words + (extra_bytes > 0 ? 1 : 0), 8)));
436
437 for(size_t i = 0; i != full_words; ++i)
438 {
439 reg[i] = load_be<word>(buf + length - sizeof(word)*(i+1), 0);
440 }
441
442 if(extra_bytes > 0)
443 {
444 for(size_t i = 0; i != extra_bytes; ++i)
445 reg[full_words] = (reg[full_words] << 8) | buf[i];
446 }
447
448 m_data.swap(reg);
449 }
void clear()
Definition: bigint.h:375
size_t round_up(size_t n, size_t align_to)
Definition: rounding.h:21

References clear(), and Botan::round_up().

Referenced by BigInt(), botan_mp_from_bin(), decode(), from_bytes_with_max_bits(), Botan::generate_dsa_primes(), Botan::RFC6979_Nonce_Generator::nonce_for(), and randomize().

◆ binary_encode() [1/2]

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

Store BigInt-value in a given byte array

Parameters
bufdestination byte array for the integer value

Definition at line 395 of file bigint.cpp.

396 {
397 this->binary_encode(buf, bytes());
398 }
void binary_encode(uint8_t buf[]) const
Definition: bigint.cpp:395
size_t bytes() const
Definition: bigint.cpp:294

References binary_encode(), and bytes().

Referenced by binary_encode(), botan_mp_to_bin(), encode(), Botan::DER_Encoder::encode(), encode_1363(), encode_fixed_length_int_pair(), encode_locked(), and Botan::GOST_3410_PublicKey::public_key_bits().

◆ 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.

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

Definition at line 403 of file bigint.cpp.

404 {
405 const size_t full_words = len / sizeof(word);
406 const size_t extra_bytes = len % sizeof(word);
407
408 for(size_t i = 0; i != full_words; ++i)
409 {
410 const word w = word_at(i);
411 store_be(w, output + (len - (i+1)*sizeof(word)));
412 }
413
414 if(extra_bytes > 0)
415 {
416 const word w = word_at(full_words);
417
418 for(size_t i = 0; i != extra_bytes; ++i)
419 {
420 output[extra_bytes - i - 1] = get_byte_var(sizeof(word) - i - 1, w);
421 }
422 }
423 }
word word_at(size_t n) const
Definition: bigint.h:522
constexpr void store_be(uint16_t in, uint8_t out[2])
Definition: loadstor.h:449
constexpr uint8_t get_byte_var(size_t byte_num, T input)
Definition: loadstor.h:39

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

◆ bits()

size_t Botan::BigInt::bits ( ) const

◆ 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 132 of file bigint.cpp.

133 {
134 return get_byte_var(sizeof(word) - (n % sizeof(word)) - 1,
135 word_at(n / sizeof(word)));
136 }

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

◆ clear()

void Botan::BigInt::clear ( )
inline

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

Definition at line 375 of file bigint.h.

375{ m_data.set_to_zero(); m_signedness = Positive; }

Referenced by Botan::PointGFp::add(), Botan::PointGFp::add_affine(), binary_decode(), botan_mp_clear(), 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 283 of file bigint.cpp.

284 {
285 const size_t which = n / BOTAN_MP_WORD_BITS;
286
287 if(which < size())
288 {
289 const word mask = ~(static_cast<word>(1) << (n % BOTAN_MP_WORD_BITS));
290 m_data.set_word_at(which, word_at(which) & mask);
291 }
292 }

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

Referenced by botan_mp_clear_bit().

◆ 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 153 of file bigint.cpp.

154 {
155 if(check_signs)
156 {
157 if(other.is_positive() && this->is_negative())
158 return -1;
159
160 if(other.is_negative() && this->is_positive())
161 return 1;
162
163 if(other.is_negative() && this->is_negative())
164 return (-bigint_cmp(this->data(), this->size(),
165 other.data(), other.size()));
166 }
167
168 return bigint_cmp(this->data(), this->size(),
169 other.data(), other.size());
170 }

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

Referenced by botan_mp_cmp(), 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 138 of file bigint.cpp.

139 {
140 if(is_negative())
141 return -1; // other is positive ...
142
143 const size_t sw = this->sig_words();
144 if(sw > 1)
145 return 1; // must be larger since other is just one word ...
146
147 return bigint_cmp(this->data(), sw, &other, 1);
148 }
bool is_negative() const
Definition: bigint.h:541

References Botan::bigint_cmp(), data(), 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 471 of file bigint.cpp.

472 {
473 // This code is assuming Negative == 0, Positive == 1
474
475 const auto mask = CT::Mask<uint8_t>::expand(predicate);
476
477 const uint8_t current_sign = static_cast<uint8_t>(sign());
478
479 const uint8_t new_sign = mask.select(current_sign ^ 1, current_sign);
480
481 set_sign(static_cast<Sign>(new_sign));
482 }
static Mask< T > expand(T v)
Definition: ct_utils.h:121

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 452 of file bigint.h.

453 {
454 const size_t which = n / BOTAN_MP_WORD_BITS;
455 const word mask = static_cast<word>(set_it) << (n % BOTAN_MP_WORD_BITS);
456 m_data.set_word_at(which, word_at(which) | mask);
457 }

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 734 of file bigint.h.

734{}

Referenced by Botan::gcd().

◆ const_time_unpoison()

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

Definition at line 735 of file bigint.h.

735{}

Referenced by Botan::gcd().

◆ 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 451 of file bigint.cpp.

452 {
453 if(this->is_negative() || value.is_negative())
454 throw Invalid_Argument("BigInt::ct_cond_add requires both values to be positive");
455 this->grow_to(1 + value.sig_words());
456
457 bigint_cnd_add(static_cast<word>(predicate),
458 this->mutable_data(), this->size(),
459 value.data(), value.sig_words());
460 }
word bigint_cnd_add(word cnd, word x[], word x_size, const word y[], size_t y_size)
Definition: mp_core.h:42

References Botan::bigint_cnd_add(), data(), grow_to(), is_negative(), mutable_data(), sig_words(), and size().

Referenced by Botan::gcd(), 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 484 of file bigint.cpp.

485 {
486 const size_t t_words = size();
487 const size_t o_words = other.size();
488
489 if(o_words < t_words)
490 grow_to(o_words);
491
492 const size_t r_words = std::max(t_words, o_words);
493
494 const auto mask = CT::Mask<word>::expand(predicate);
495
496 for(size_t i = 0; i != r_words; ++i)
497 {
498 const word o_word = other.word_at(i);
499 const word t_word = this->word_at(i);
500 this->set_word_at(i, mask.select(o_word, t_word));
501 }
502
503 const bool different_sign = sign() != other.sign();
504 cond_flip_sign(predicate && different_sign);
505 }
void set_word_at(size_t i, word w)
Definition: bigint.h:527
void cond_flip_sign(bool predicate)
Definition: bigint.cpp:471

References cond_flip_sign(), Botan::CT::Mask< T >::expand(), grow_to(), set_word_at(), sign(), size(), and word_at().

Referenced by 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 462 of file bigint.cpp.

463 {
464 const size_t max_words = std::max(size(), other.size());
465 grow_to(max_words);
466 other.grow_to(max_words);
467
468 bigint_cnd_swap(predicate, this->mutable_data(), other.mutable_data(), max_words);
469 }
void bigint_cnd_swap(word cnd, word x[], word y[], size_t size)
Definition: mp_core.h:29

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

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

◆ 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 362 of file bigint.cpp.

363 {
364 if(mod.is_negative() || this->is_negative())
365 throw Invalid_Argument("BigInt::ct_reduce_below both values must be positive");
366
367 const size_t mod_words = mod.sig_words();
368
369 grow_to(mod_words);
370
371 const size_t sz = size();
372
373 ws.resize(sz);
374
375 clear_mem(ws.data(), sz);
376
377 for(size_t i = 0; i != bound; ++i)
378 {
379 word borrow = bigint_sub3(ws.data(), data(), sz, mod.data(), mod_words);
380
381 CT::Mask<word>::is_zero(borrow).select_n(mutable_data(), ws.data(), data(), sz);
382 }
383 }
static Mask< T > is_zero(T x)
Definition: ct_utils.h:139
word bigint_sub3(word z[], const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.h:342
constexpr void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:115

References Botan::bigint_sub3(), Botan::clear_mem(), data(), grow_to(), is_negative(), Botan::CT::Mask< T >::is_zero(), mutable_data(), sig_words(), and size().

Referenced by Botan::Modular_Reducer::reduce().

◆ data()

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

◆ decode() [1/4]

template<typename Alloc >
static BigInt Botan::BigInt::decode ( const std::vector< uint8_t, Alloc > &  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 801 of file bigint.h.

802 {
803 return BigInt(buf);
804 }

◆ decode() [2/4]

template<typename Alloc >
static BigInt Botan::BigInt::decode ( const std::vector< uint8_t, Alloc > &  buf,
Base  base 
)
inlinestatic

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 823 of file bigint.h.

824 {
825 if(base == Binary)
826 return BigInt(buf);
827 return BigInt::decode(buf.data(), buf.size(), base);
828 }

References decode().

◆ decode() [3/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 790 of file bigint.h.

791 {
792 return BigInt(buf, length);
793 }

Referenced by BigInt(), botan_srp6_client_agree(), botan_srp6_server_session_step1(), botan_srp6_server_session_step2(), decode(), Botan::BER_Decoder::decode_octet_string_bigint(), Botan::CRL_Entry::encode_into(), Botan::TPM_PrivateKey::get_e(), Botan::TPM_PrivateKey::get_n(), Botan::OCSP::CertID::is_id_for(), Botan::OS2ECP(), and Botan::TLS::Callbacks::tls_dh_agree().

◆ decode() [4/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 143 of file big_code.cpp.

144 {
145 BigInt r;
146 if(base == Binary)
147 {
148 r.binary_decode(buf, length);
149 }
150 else if(base == Hexadecimal)
151 {
152 secure_vector<uint8_t> binary;
153
154 if(length % 2)
155 {
156 // Handle lack of leading 0
157 const char buf0_with_leading_0[2] =
158 { '0', static_cast<char>(buf[0]) };
159
160 binary = hex_decode_locked(buf0_with_leading_0, 2);
161
162 binary += hex_decode_locked(cast_uint8_ptr_to_char(&buf[1]),
163 length - 1,
164 false);
165 }
166 else
168 length, false);
169
170 r.binary_decode(binary.data(), binary.size());
171 }
172 else if(base == Decimal)
173 {
174 // This could be made faster using the same trick as to_dec_string
175 for(size_t i = 0; i != length; ++i)
176 {
177 const char c = buf[i];
178
179 if(c < '0' || c > '9')
180 throw Invalid_Argument("BigInt::decode: invalid decimal char");
181
182 const uint8_t x = c - '0';
183 BOTAN_ASSERT_NOMSG(x < 10);
184
185 r *= 10;
186 r += x;
187 }
188 }
189 else
190 throw Invalid_Argument("Unknown BigInt decoding method");
191 return r;
192 }
#define BOTAN_ASSERT_NOMSG(expr)
Definition: assert.h:67
secure_vector< uint8_t > hex_decode_locked(const char input[], size_t input_length, bool ignore_ws)
Definition: hex.cpp:168
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition: mem_ops.h:188

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

◆ 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 765 of file bigint.h.

766 {
767 std::vector<uint8_t> output(n.bytes());
768 n.binary_encode(output.data());
769 return output;
770 }

References binary_encode(), and bytes().

Referenced by botan_generate_srp6_verifier(), botan_srp6_client_agree(), botan_srp6_server_session_step1(), Botan::CRL_Entry::decode_from(), Botan::FPE_FE1::FPE_FE1(), Botan::TLS::Server_Key_Exchange::Server_Key_Exchange(), and to_hex_string().

◆ encode_1363() [1/2]

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

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 106 of file big_code.cpp.

107 {
108 if(n.bytes() > bytes)
109 throw Encoding_Error("encode_1363: n is too large to encode properly");
110
111 secure_vector<uint8_t> output(bytes);
112 n.binary_encode(output.data(), output.size());
113 return output;
114 }

References binary_encode(), and bytes().

Referenced by Botan::PK_Verifier::check_signature(), Botan::EC_Group::DER_encode(), Botan::PointGFp::encode(), Botan::RFC6979_Nonce_Generator::nonce_for(), Botan::EC_PrivateKey::private_key_bits(), Botan::DH_PublicKey::public_value(), Botan::RFC6979_Nonce_Generator::RFC6979_Nonce_Generator(), Botan::Montgomery_Int::serialize(), Botan::sm2_compute_za(), Botan::srp6_client_agree(), and Botan::SRP6_Server_Session::step2().

◆ encode_1363() [2/2]

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

Definition at line 117 of file big_code.cpp.

118 {
119 if(n.bytes() > bytes)
120 throw Encoding_Error("encode_1363: n is too large to encode properly");
121
122 n.binary_encode(output, bytes);
123 }

References binary_encode(), and bytes().

◆ 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 128 of file big_code.cpp.

129 {
130 if(n1.is_negative() || n2.is_negative())
131 throw Encoding_Error("encode_fixed_length_int_pair: values must be positive");
132 if(n1.bytes() > bytes || n2.bytes() > bytes)
133 throw Encoding_Error("encode_fixed_length_int_pair: values too large to encode properly");
134 secure_vector<uint8_t> output(2 * bytes);
135 n1.binary_encode(output.data() , bytes);
136 n2.binary_encode(output.data() + bytes, bytes);
137 return output;
138 }

References binary_encode(), bytes(), and is_negative().

◆ 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 777 of file bigint.h.

778 {
779 secure_vector<uint8_t> output(n.bytes());
780 n.binary_encode(output.data());
781 return output;
782 }

References binary_encode(), and bytes().

◆ 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 199 of file bigint.cpp.

200 {
201 const size_t words = sig_words();
202
203 if(words > size)
204 throw Encoding_Error("BigInt::encode_words value too large to encode");
205
206 clear_mem(out, size);
207 copy_mem(out, data(), words);
208 }
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:126

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

◆ flip_sign()

void Botan::BigInt::flip_sign ( )
inline

Flip the sign of this BigInt

Definition at line 568 of file bigint.h.

569 {
571 }
Sign reverse_sign() const
Definition: bigint.h:558

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

◆ 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

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 110 of file bigint.cpp.

111 {
112 if(8 * length > max_bits)
113 length = (max_bits + 7) / 8;
114
115 BigInt bn;
116 bn.binary_decode(buf, length);
117
118 if(8 * length > max_bits)
119 bn >>= (8 - (max_bits % 8));
120
121 return bn;
122 }

References binary_decode().

◆ 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 51 of file bigint.cpp.

52 {
53 if(n >= 0)
54 return BigInt::from_u64(static_cast<uint64_t>(n));
55 else
56 return -BigInt::from_u64(static_cast<uint64_t>(-n));
57 }
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_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.

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

References set_word_at().

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

◆ 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 43 of file bigint.cpp.

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

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(), one(), 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 479 of file bigint.h.

480 {
481 return ((word_at(n / BOTAN_MP_WORD_BITS) >> (n % BOTAN_MP_WORD_BITS)) & 1);
482 }

References BOTAN_MP_WORD_BITS.

Referenced by Botan::ct_divide(), Botan::ct_divide_word(), Botan::ct_modulo(), Botan::PointGFp::encode(), Botan::inverse_mod(), Botan::is_lucas_probable_prime(), Botan::operator*(), 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 236 of file bigint.cpp.

237 {
238 if(length == 0 || length > 32)
239 throw Invalid_Argument("BigInt::get_substring invalid substring length");
240
241 const uint32_t mask = 0xFFFFFFFF >> (32 - length);
242
243 const size_t word_offset = offset / BOTAN_MP_WORD_BITS;
244 const size_t wshift = (offset % BOTAN_MP_WORD_BITS);
245
246 /*
247 * The substring is contained within one or at most two words. The
248 * offset and length are not secret, so we can perform conditional
249 * operations on those values.
250 */
251 const word w0 = word_at(word_offset);
252
253 if(wshift == 0 || (offset + length) / BOTAN_MP_WORD_BITS == word_offset)
254 {
255 return static_cast<uint32_t>(w0 >> wshift) & mask;
256 }
257 else
258 {
259 const word w1 = word_at(word_offset + 1);
260 return static_cast<uint32_t>((w0 >> wshift) | (w1 << (BOTAN_MP_WORD_BITS - wshift))) & mask;
261 }
262 }

References BOTAN_MP_WORD_BITS, and word_at().

Referenced by Botan::monty_multi_exp(), Botan::PointGFp_Base_Point_Precompute::mul(), Botan::PointGFp_Var_Point_Precompute::mul(), and Botan::PointGFp_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 639 of file bigint.h.

639{ return m_data.mutable_vector(); }

◆ 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 644 of file bigint.h.

644{ return m_data.const_vector(); }

◆ grow_to()

void Botan::BigInt::grow_to ( size_t  n) const
inline

◆ 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 172 of file bigint.cpp.

173 {
174 if(this->sign() != other.sign())
175 return false;
176
177 return bigint_ct_is_eq(this->data(), this->sig_words(),
178 other.data(), other.sig_words()).is_set();
179 }
CT::Mask< word > bigint_ct_is_eq(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.h:613

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

Referenced by 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 181 of file bigint.cpp.

182 {
183 if(this->is_negative() && other.is_positive())
184 return true;
185
186 if(this->is_positive() && other.is_negative())
187 return false;
188
189 if(other.is_negative() && this->is_negative())
190 {
191 return bigint_ct_is_lt(other.data(), other.sig_words(),
192 this->data(), this->sig_words()).is_set();
193 }
194
195 return bigint_ct_is_lt(this->data(), this->sig_words(),
196 other.data(), other.sig_words()).is_set();
197 }
bool is_positive() const
Definition: bigint.h:547
CT::Mask< word > bigint_ct_is_lt(const word x[], size_t x_size, const word y[], size_t y_size, bool lt_or_equal=false)
Definition: mp_core.h:576

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

Referenced by Botan::operator<().

◆ is_negative()

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

◆ 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 424 of file bigint.h.

424{ return (!is_zero()); }
bool is_zero() const
Definition: bigint.h:430

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 418 of file bigint.h.

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

Referenced by botan_mp_is_odd(), Botan::gcd(), 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 547 of file bigint.h.

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

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

◆ is_zero()

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

◆ 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 469 of file bigint.h.

470 {
471 m_data.mask_bits(n);
472 }

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 50 of file big_ops2.cpp.

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

References Botan::bigint_add3_nc(), Botan::bigint_sub3(), BOTAN_ARG_CHECK, BOTAN_DEBUG_ASSERT, BOTAN_UNUSED, Botan::CT::conditional_copy_mem(), data(), grow_to(), is_negative(), set_words(), and sig_words().

Referenced by Botan::Montgomery_Int::add(), Botan::PointGFp::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 121 of file big_ops2.cpp.

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

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::PointGFp::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 94 of file big_ops2.cpp.

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

References Botan::bigint_mod_sub(), BOTAN_DEBUG_ASSERT, data(), grow_to(), is_negative(), mutable_data(), and sig_words().

Referenced by Botan::PointGFp::add(), Botan::PointGFp::add_affine(), Botan::PointGFp::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 160 of file big_ops2.cpp.

161 {
162 const size_t x_sw = sig_words();
163 const size_t y_sw = y.sig_words();
164 set_sign((sign() == y.sign()) ? Positive : Negative);
165
166 if(x_sw == 0 || y_sw == 0)
167 {
168 clear();
170 }
171 else if(x_sw == 1 && y_sw)
172 {
173 grow_to(y_sw + 1);
174 bigint_linmul3(mutable_data(), y.data(), y_sw, word_at(0));
175 }
176 else if(y_sw == 1 && x_sw)
177 {
178 word carry = bigint_linmul2(mutable_data(), x_sw, y.word_at(0));
179 set_word_at(x_sw, carry);
180 }
181 else
182 {
183 const size_t new_size = x_sw + y_sw + 1;
184 ws.resize(new_size);
185 secure_vector<word> z_reg(new_size);
186
187 bigint_mul(z_reg.data(), z_reg.size(),
188 data(), size(), x_sw,
189 y.data(), y.size(), y_sw,
190 ws.data(), ws.size());
191
192 this->swap_reg(z_reg);
193 }
194
195 return (*this);
196 }
void swap_reg(secure_vector< word > &reg)
Definition: bigint.h:176
void bigint_linmul3(word z[], const word x[], size_t x_size, word y)
Definition: mp_core.h:504
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:297
void carry(int64_t &h0, int64_t &h1)
word bigint_linmul2(word x[], size_t x_size, word y)
Definition: mp_core.h:489

References Botan::bigint_linmul2(), Botan::bigint_linmul3(), Botan::bigint_mul(), Botan::carry(), clear(), data(), grow_to(), mutable_data(), Negative, Positive, set_sign(), set_word_at(), sig_words(), sign(), size(), swap_reg(), and word_at().

Referenced by operator*=(), and Botan::Modular_Reducer::reduce().

◆ mutable_data()

word * Botan::BigInt::mutable_data ( )
inline

◆ one()

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

Create a 1-value BigInt

Definition at line 50 of file bigint.h.

50{ return BigInt::from_word(1); }
static BigInt from_word(word n)
Definition: bigint.cpp:43

References from_word().

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

◆ operator!()

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

! operator

Returns
true iff this is zero, otherwise false

Definition at line 290 of file bigint.h.

290{ return (!is_nonzero()); }
bool is_nonzero() const
Definition: bigint.h:424

◆ operator%=() [1/2]

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

Modulo operator

Parameters
ythe modulus to reduce this by

Definition at line 244 of file big_ops2.cpp.

245 {
246 return (*this = (*this) % mod);
247 }

◆ operator%=() [2/2]

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

Modulo operator

Parameters
ythe modulus (word) to reduce this by

Definition at line 252 of file big_ops2.cpp.

253 {
254 if(mod == 0)
255 throw Invalid_Argument("BigInt::operator%= divide by zero");
256
257 word remainder = 0;
258
259 if(is_power_of_2(mod))
260 {
261 remainder = (word_at(0) & (mod - 1));
262 }
263 else
264 {
265 const size_t sw = sig_words();
266 for(size_t i = sw; i > 0; --i)
267 remainder = bigint_modop(remainder, word_at(i-1), mod);
268 }
269
270 if(remainder && sign() == BigInt::Negative)
271 remainder = mod - remainder;
272
273 m_data.set_to_zero();
274 m_data.set_word_at(0, remainder);
276 return remainder;
277 }
word bigint_modop(word n1, word n0, word d)
Definition: mp_core.h:755
constexpr bool is_power_of_2(T arg)
Definition: bit_ops.h:43

References Botan::bigint_modop(), 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 154 of file big_ops2.cpp.

155 {
156 secure_vector<word> ws;
157 return this->mul(y, ws);
158 }
BigInt & mul(const BigInt &y, secure_vector< word > &ws)
Definition: big_ops2.cpp:160

References mul().

◆ operator*=() [2/2]

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

*= operator

Parameters
ythe word to multiply with this

Definition at line 215 of file big_ops2.cpp.

216 {
217 if(y == 0)
218 {
219 clear();
221 }
222
223 const word carry = bigint_linmul2(mutable_data(), size(), y);
225
226 return (*this);
227 }

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 263 of file bigint.h.

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

◆ operator++() [2/2]

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

Postfix increment operator

Definition at line 273 of file bigint.h.

273{ BigInt x = (*this); ++(*this); return x; }

◆ operator+=() [1/2]

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

+= operator

Parameters
ythe BigInt to add to this

Definition at line 186 of file bigint.h.

187 {
188 return add(y.data(), y.sig_words(), y.sign());
189 }
BigInt & add(const word y[], size_t y_words, Sign sign)
Definition: big_ops2.cpp:15

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 195 of file bigint.h.

196 {
197 return add(&y, 1, Positive);
198 }

◆ operator-()

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

Unary negation operator

Returns
negative this

Definition at line 325 of file bigint.cpp.

326 {
327 BigInt x = (*this);
328 x.flip_sign();
329 return x;
330 }

References flip_sign().

◆ operator--() [1/2]

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

Decrement operator

Definition at line 268 of file bigint.h.

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

◆ operator--() [2/2]

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

Postfix decrement operator

Definition at line 278 of file bigint.h.

278{ BigInt x = (*this); --(*this); return x; }

◆ operator-=() [1/2]

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

-= operator

Parameters
ythe BigInt to subtract from this

Definition at line 204 of file bigint.h.

205 {
206 return sub(y.data(), y.sig_words(), y.sign());
207 }
BigInt & sub(const word y[], size_t y_words, Sign sign)
Definition: bigint.h:296

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 213 of file bigint.h.

214 {
215 return sub(&y, 1, Positive);
216 }

◆ operator/=()

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

/= operator

Parameters
ythe BigInt to divide this by

Definition at line 232 of file big_ops2.cpp.

233 {
234 if(y.sig_words() == 1 && is_power_of_2(y.word_at(0)))
235 (*this) >>= (y.bits() - 1);
236 else
237 (*this) = (*this) / y;
238 return (*this);
239 }

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 282 of file big_ops2.cpp.

283 {
284 const size_t shift_words = shift / BOTAN_MP_WORD_BITS;
285 const size_t shift_bits = shift % BOTAN_MP_WORD_BITS;
286 const size_t size = sig_words();
287
288 const size_t bits_free = top_bits_free();
289
290 const size_t new_size = size + shift_words + (bits_free < shift_bits);
291
292 m_data.grow_to(new_size);
293
294 bigint_shl1(m_data.mutable_data(), new_size, size, shift_words, shift_bits);
295
296 return (*this);
297 }
void bigint_shl1(word x[], size_t x_size, size_t x_words, size_t word_shift, size_t bit_shift)
Definition: mp_core.h:409

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

◆ operator=() [1/2]

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

Move assignment

Definition at line 153 of file bigint.h.

154 {
155 if(this != &other)
156 this->swap(other);
157
158 return (*this);
159 }

◆ 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 302 of file big_ops2.cpp.

303 {
304 const size_t shift_words = shift / BOTAN_MP_WORD_BITS;
305 const size_t shift_bits = shift % BOTAN_MP_WORD_BITS;
306
307 bigint_shr1(m_data.mutable_data(), m_data.size(), shift_words, shift_bits);
308
309 if(is_negative() && is_zero())
311
312 return (*this);
313 }
void bigint_shr1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition: mp_core.h:427

References Botan::bigint_shr1(), BOTAN_MP_WORD_BITS, 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 753 of file bigint.h.

754 {
755 BigInt b;
756 b.set_bit(n);
757 return b;
758 }
PolynomialVector b
Definition: kyber.cpp:821

References b.

Referenced by Botan::inverse_mod(), Botan::is_perfect_square(), 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 45 of file big_rand.cpp.

47 {
48 if(min.is_negative() || max.is_negative() || max <= min)
49 throw Invalid_Argument("BigInt::random_integer invalid range");
50
51 BigInt r;
52
53 const size_t bits = max.bits();
54
55 do
56 {
57 r.randomize(rng, bits, false);
58 }
59 while(r < min || r >= max);
60
61 return r;
62 }

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

Referenced by botan_mp_rand_range(), Botan::DSA_PrivateKey::DSA_PrivateKey(), Botan::is_miller_rabin_probable_prime(), Botan::EC_Group::random_scalar(), and Botan::PointGFp::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 17 of file big_rand.cpp.

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

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

Referenced by BigInt(), Botan::DH_PrivateKey::DH_PrivateKey(), Botan::ElGamal_PrivateKey::ElGamal_PrivateKey(), 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 332 of file bigint.cpp.

333 {
334 if(p.is_negative() || this->is_negative())
335 throw Invalid_Argument("BigInt::reduce_below both values must be positive");
336
337 const size_t p_words = p.sig_words();
338
339 if(size() < p_words + 1)
340 grow_to(p_words + 1);
341
342 if(ws.size() < p_words + 1)
343 ws.resize(p_words + 1);
344
345 clear_mem(ws.data(), ws.size());
346
347 size_t reductions = 0;
348
349 for(;;)
350 {
351 word borrow = bigint_sub3(ws.data(), data(), p_words + 1, p.data(), p_words);
352 if(borrow)
353 break;
354
355 ++reductions;
356 swap_reg(ws);
357 }
358
359 return reductions;
360 }

References Botan::bigint_sub3(), Botan::clear_mem(), data(), 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 652 of file bigint.h.

652{ m_data.resize(s); }

Referenced by Botan::PointGFp::force_all_affine().

◆ 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 133 of file big_ops2.cpp.

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

References Botan::bigint_sub_abs(), Botan::clear_mem(), cond_flip_sign(), data(), 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 558 of file bigint.h.

559 {
560 if(sign() == Positive)
561 return Negative;
562 return Positive;
563 }

Referenced by Botan::operator-().

◆ set_bit()

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

Set bit at specified position

Parameters
nbit position to set

Definition at line 439 of file bigint.h.

440 {
441 conditionally_set_bit(n, true);
442 }
void conditionally_set_bit(size_t n, bool set_it)
Definition: bigint.h:452

Referenced by botan_mp_set_bit(), 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 577 of file bigint.h.

578 {
579 if(sign == Negative && is_zero())
580 sign = Positive;
581
582 m_signedness = sign;
583 }

Referenced by abs(), add(), add2(), BigInt(), cond_flip_sign(), Botan::gcd(), Botan::inverse_mod(), mul(), operator%=(), Botan::operator*(), operator*=(), Botan::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 527 of file bigint.h.

528 {
529 m_data.set_word_at(i, w);
530 }

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 532 of file bigint.h.

533 {
534 m_data.set_words(w, len);
535 }

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

◆ sig_words()

size_t Botan::BigInt::sig_words ( ) const
inline

◆ sign()

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

Return the sign of the integer

Returns
the sign of the integer

Definition at line 553 of file bigint.h.

553{ return (m_signedness); }

Referenced by add(), add2(), cond_flip_sign(), ct_cond_assign(), is_equal(), mul(), Botan::operator%(), operator%=(), Botan::operator*(), Botan::operator+(), operator+=(), operator-=(), Botan::operator<<(), Botan::operator>>(), and rev_sub().

◆ size()

size_t Botan::BigInt::size ( ) const
inline

◆ square()

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

Square value of *this

Parameters
wsa temp workspace

Definition at line 198 of file big_ops2.cpp.

199 {
200 const size_t sw = sig_words();
201
202 secure_vector<word> z(2*sw);
203 ws.resize(z.size());
204
205 bigint_sqr(z.data(), z.size(),
206 data(), size(), sw,
207 ws.data(), ws.size());
208
209 swap_reg(z);
211
212 return (*this);
213 }
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:356

References Botan::bigint_sqr(), data(), 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 296 of file bigint.h.

297 {
298 return add(y, y_words, sign == Positive ? Negative : Positive);
299 }

◆ swap()

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

Swap this value with another

Parameters
otherBigInt to swap values with

Definition at line 170 of file bigint.h.

171 {
172 m_data.swap(other.m_data);
173 std::swap(m_signedness, other.m_signedness);
174 }

Referenced by Botan::PointGFp::add_affine(), botan_mp_swap(), Botan::PointGFp::mult2(), Botan::PointGFp::swap(), and std::swap< Botan::BigInt >().

◆ swap_reg()

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

Definition at line 176 of file bigint.h.

177 {
178 m_data.swap(reg);
179 // sign left unchanged
180 }

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

Definition at line 14 of file big_code.cpp.

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

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.

Definition at line 88 of file big_code.cpp.

89 {
90 std::vector<uint8_t> bits = BigInt::encode(*this);
91
92 if(bits.empty())
93 bits.push_back(0);
94
95 std::string hrep;
96 if(is_negative())
97 hrep += "-";
98 hrep += "0x";
99 hrep += hex_encode(bits);
100 return hrep;
101 }
static std::vector< uint8_t > encode(const BigInt &n)
Definition: bigint.h:765
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition: hex.cpp:31

References bits(), encode(), Botan::hex_encode(), and is_negative().

Referenced by botan_mp_to_hex(), and 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 267 of file bigint.cpp.

268 {
269 if(is_negative())
270 throw Encoding_Error("BigInt::to_u32bit: Number is negative");
271 if(bits() > 32)
272 throw Encoding_Error("BigInt::to_u32bit: Number is too big to convert");
273
274 uint32_t out = 0;
275 for(size_t i = 0; i != 4; ++i)
276 out = (out << 8) | byte_at(3-i);
277 return out;
278 }
uint8_t byte_at(size_t n) const
Definition: bigint.cpp:132

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

Referenced by botan_mp_to_uint32().

◆ 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 299 of file bigint.cpp.

300 {
301 const size_t words = sig_words();
302
303 const word top_word = word_at(words - 1);
304 const size_t bits_used = high_bit(top_word);
305 CT::unpoison(bits_used);
306 return BOTAN_MP_WORD_BITS - bits_used;
307 }
void unpoison(const T *p, size_t n)
Definition: ct_utils.h:58
constexpr size_t high_bit(T n)
Definition: bit_ops.h:55

References BOTAN_MP_WORD_BITS, Botan::high_bit(), sig_words(), Botan::CT::unpoison(), and word_at().

Referenced by bits(), operator<<=(), 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 60 of file bigint.cpp.

61 {
62 BigInt bn;
63 bn.grow_to(size);
64 return bn;
65 }

References grow_to(), and size().

Referenced by add2(), Botan::ct_divide(), Botan::ct_divide_word(), Botan::ct_modulo(), Botan::Montgomery_Params::mul(), Botan::operator*(), Botan::operator<<(), Botan::operator>>(), and Botan::Montgomery_Params::sqr().

◆ word_at()

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

Return the word at a specified position of the internal register

Parameters
nposition in the register
Returns
value at position n

Definition at line 522 of file bigint.h.

523 {
524 return m_data.get_word_at(n);
525 }

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(), mul(), Botan::operator%(), operator%=(), Botan::operator*(), Botan::operator/(), operator/=(), Botan::redc_p521(), top_bits_free(), and Botan::vartime_divide().

◆ zero()

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

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