Botan  2.7.0
Crypto and TLS for C++11
Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
Botan::BigInt Class Referencefinal

#include <bigint.h>

Classes

class  DivideByZero
 

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 (uint64_t n)
 
 BigInt (const BigInt &other)
 
 BigInt (const std::string &str)
 
 BigInt (const uint8_t buf[], size_t length)
 
 BigInt (const uint8_t buf[], size_t length, Base base)
 
 BigInt (const uint8_t buf[], size_t length, size_t max_bits)
 
 BigInt (const word words[], size_t length)
 
 BigInt (RandomNumberGenerator &rng, size_t bits, bool set_high_bit=true)
 Create a random BigInt of the specified size. More...
 
 BigInt (Sign sign, size_t n)
 
 BigInt (BigInt &&other)
 
void binary_decode (const uint8_t buf[], size_t length)
 
void binary_decode (const secure_vector< uint8_t > &buf)
 
void binary_encode (uint8_t buf[]) 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 const_time_poison () const
 
void const_time_unpoison () const
 
const word * data () const
 
void encode_words (word out[], size_t size) const
 
size_t encoded_size (Base base=Binary) 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)
 
bool is_even () 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_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)
 
void reduce_below (const BigInt &mod, secure_vector< word > &ws)
 
BigIntrev_sub (const word y[], size_t y_size, 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)
 
void shrink_to_fit (size_t min_size=0)
 
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)
 
uint32_t to_u32bit () const
 
word word_at (size_t n) const
 

Static Public Member Functions

static void const_time_lookup (secure_vector< word > &output, const std::vector< BigInt > &vec, size_t idx)
 
static BigInt decode (const uint8_t buf[], size_t length, Base base=Binary)
 
static BigInt decode (const secure_vector< uint8_t > &buf, Base base=Binary)
 
static BigInt decode (const std::vector< uint8_t > &buf, Base base=Binary)
 
static std::vector< uint8_t > encode (const BigInt &n, Base base=Binary)
 
static void encode (uint8_t buf[], const BigInt &n, Base base=Binary)
 
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, Base base=Binary)
 
static BigInt power_of_2 (size_t n)
 
static BigInt random_integer (RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)
 

Detailed Description

Arbitrary precision integer

Definition at line 25 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.

◆ Sign

Sign symbol definitions for positive and negative numbers

Enumerator
Negative 
Positive 

Definition at line 36 of file bigint.h.

Constructor & Destructor Documentation

◆ BigInt() [1/11]

Botan::BigInt::BigInt ( )
default

Create empty BigInt

◆ BigInt() [2/11]

Botan::BigInt::BigInt ( uint64_t  n)

Create BigInt from 64 bit integer

Parameters
ninitial value of this BigInt

Definition at line 24 of file bigint.cpp.

References Botan::MP_WORD_MASK.

25  {
26  if(n == 0)
27  return;
28 
29  const size_t limbs_needed = sizeof(uint64_t) / sizeof(word);
30 
31  m_reg.resize(limbs_needed);
32  for(size_t i = 0; i != limbs_needed; ++i)
33  m_reg[i] = ((n >> (i*BOTAN_MP_WORD_BITS)) & MP_WORD_MASK);
34  }
const word MP_WORD_MASK
Definition: mp_core.h:17

◆ BigInt() [3/11]

Botan::BigInt::BigInt ( const BigInt other)

Copy Constructor

Parameters
otherthe BigInt to copy

Definition at line 48 of file bigint.cpp.

49  {
50  m_reg = other.m_reg;
51  m_signedness = other.m_signedness;
52  }

◆ BigInt() [4/11]

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

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

58  {
59  Base base = Decimal;
60  size_t markers = 0;
61  bool negative = false;
62 
63  if(str.length() > 0 && str[0] == '-')
64  {
65  markers += 1;
66  negative = true;
67  }
68 
69  if(str.length() > markers + 2 && str[markers ] == '0' &&
70  str[markers + 1] == 'x')
71  {
72  markers += 2;
73  base = Hexadecimal;
74  }
75 
76  *this = decode(cast_char_ptr_to_uint8(str.data()) + markers,
77  str.length() - markers, base);
78 
79  if(negative) set_sign(Negative);
80  else set_sign(Positive);
81  }
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition: mem_ops.h:131
void set_sign(Sign sign)
Definition: bigint.h:496
static BigInt decode(const uint8_t buf[], size_t length, Base base=Binary)
Definition: big_code.cpp:114

◆ BigInt() [5/11]

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

References binary_decode().

84  {
85  binary_decode(input, length);
86  }
void binary_decode(const uint8_t buf[], size_t length)
Definition: bigint.cpp:326

◆ BigInt() [6/11]

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

References decode().

92  {
93  *this = decode(input, length, base);
94  }
static BigInt decode(const uint8_t buf[], size_t length, Base base=Binary)
Definition: big_code.cpp:114

◆ BigInt() [7/11]

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

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

References bits(), and decode().

97  {
98  const size_t max_bytes = std::min(length, (max_bits + 7) / 8);
99  *this = decode(buf, max_bytes);
100 
101  const size_t b = this->bits();
102  if(b > max_bits)
103  {
104  *this >>= (b - max_bits);
105  }
106  }
size_t bits() const
Definition: bigint.cpp:228
static BigInt decode(const uint8_t buf[], size_t length, Base base=Binary)
Definition: big_code.cpp:114

◆ BigInt() [8/11]

Botan::BigInt::BigInt ( const word  words[],
size_t  length 
)

Create a BigInt from an array of words

Parameters
wordsthe words
lengthnumber of words

Definition at line 16 of file bigint.cpp.

17  {
18  m_reg.assign(words, words + length);
19  }

◆ BigInt() [9/11]

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

References bits(), and randomize().

112  {
113  randomize(rng, bits, set_high_bit);
114  }
size_t bits() const
Definition: bigint.cpp:228
void randomize(RandomNumberGenerator &rng, size_t bitsize, bool set_high_bit=true)
Definition: big_rand.cpp:17

◆ BigInt() [10/11]

Botan::BigInt::BigInt ( Sign  sign,
size_t  n 
)

Create BigInt of specified size, all zeros

Parameters
signthe sign
nsize of the internal register in words

Definition at line 39 of file bigint.cpp.

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

40  {
41  m_reg.resize(round_up(size, 8));
42  m_signedness = s;
43  }
size_t size() const
Definition: bigint.h:513
size_t round_up(size_t n, size_t align_to)
Definition: rounding.h:21

◆ BigInt() [11/11]

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

Move constructor

Definition at line 125 of file bigint.h.

126  {
127  this->swap(other);
128  }
void swap(BigInt &other)
Definition: bigint.h:150

Member Function Documentation

◆ abs()

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

Definition at line 295 of file bigint.cpp.

References Positive, and set_sign().

Referenced by Botan::abs().

296  {
297  BigInt x = (*this);
298  x.set_sign(Positive);
299  return x;
300  }
BigInt()=default

◆ add()

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

Definition at line 15 of file big_ops2.cpp.

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 Botan::zeroise().

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

16  {
17  const size_t x_sw = sig_words();
18 
19  if(sign() == y_sign)
20  {
21  const size_t reg_size = std::max(x_sw, y_sw) + 1;
22 
23  if(m_reg.size() < reg_size)
24  grow_to(reg_size);
25 
26  bigint_add2(mutable_data(), reg_size - 1, y, y_sw);
27  }
28  else
29  {
30  const int32_t relative_size = bigint_cmp(data(), x_sw, y, y_sw);
31 
32  if(relative_size < 0)
33  {
34  const size_t reg_size = std::max(x_sw, y_sw);
35  grow_to(reg_size);
36  bigint_sub2_rev(mutable_data(), y, y_sw);
37  set_sign(y_sign);
38  }
39  else if(relative_size == 0)
40  {
41  zeroise(m_reg);
43  }
44  else if(relative_size > 0)
45  {
46  bigint_sub2(mutable_data(), x_sw, y, y_sw);
47  }
48  }
49 
50  return (*this);
51  }
void bigint_sub2_rev(word x[], const word y[], size_t y_size)
Definition: mp_core.cpp:227
int32_t bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:456
word bigint_sub2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:204
Sign sign() const
Definition: bigint.h:472
word * mutable_data()
Definition: bigint.h:545
const word * data() const
Definition: bigint.h:551
size_t sig_words() const
Definition: bigint.h:519
void grow_to(size_t n)
Definition: bigint.cpp:302
void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:186
void set_sign(Sign sign)
Definition: bigint.h:496
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:183

◆ binary_decode() [1/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 326 of file bigint.cpp.

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

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

327  {
328  const size_t WORD_BYTES = sizeof(word);
329 
330  clear();
331  m_reg.resize(round_up((length / WORD_BYTES) + 1, 8));
332 
333  for(size_t i = 0; i != length / WORD_BYTES; ++i)
334  {
335  const size_t top = length - WORD_BYTES*i;
336  for(size_t j = WORD_BYTES; j > 0; --j)
337  m_reg[i] = (m_reg[i] << 8) | buf[top - j];
338  }
339 
340  for(size_t i = 0; i != length % WORD_BYTES; ++i)
341  m_reg[length / WORD_BYTES] = (m_reg[length / WORD_BYTES] << 8) | buf[i];
342  }
void clear()
Definition: bigint.h:314
size_t round_up(size_t n, size_t align_to)
Definition: rounding.h:21

◆ binary_decode() [2/2]

void Botan::BigInt::binary_decode ( const secure_vector< uint8_t > &  buf)
inline

Read integer value from a byte array (secure_vector<uint8_t>)

Parameters
bufthe array to load from

Definition at line 602 of file bigint.h.

603  {
604  binary_decode(buf.data(), buf.size());
605  }
void binary_decode(const uint8_t buf[], size_t length)
Definition: bigint.cpp:326

◆ binary_encode()

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

References byte_at(), and bytes().

Referenced by botan_mp_to_bin(), encode(), and Botan::GOST_3410_PublicKey::public_key_bits().

317  {
318  const size_t sig_bytes = bytes();
319  for(size_t i = 0; i != sig_bytes; ++i)
320  output[sig_bytes-i-1] = byte_at(i);
321  }
uint8_t byte_at(size_t n) const
Definition: bigint.h:429
size_t bytes() const
Definition: bigint.cpp:220

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

References Botan::high_bit(), sig_words(), and word_at().

Referenced by BigInt(), botan_mp_num_bits(), bytes(), Botan::ct_inverse_mod_odd_modulus(), Botan::BER_Decoder::decode(), Botan::BER_Decoder::decode_constrained_integer(), Botan::DL_Group::DL_Group(), Botan::DER_Encoder::encode(), encoded_size(), Botan::Fixed_Window_Exponentiator::execute(), Botan::Montgomery_Exponentiator::execute(), Botan::generate_dsa_primes(), Botan::generate_rsa_prime(), Botan::generate_srp6_verifier(), Botan::is_prime(), Botan::RSA_PublicKey::key_length(), Botan::TPM_PrivateKey::key_length(), Botan::monty_multi_exp(), Botan::PointGFp_Base_Point_Precompute::mul(), Botan::PointGFp_Var_Point_Precompute::mul(), Botan::PointGFp_Multi_Point_Precompute::multi_exp(), Botan::operator*(), Botan::operator/(), operator/=(), Botan::operator>>(), Botan::DL_Group::power_g_p(), random_integer(), Botan::random_prime(), Botan::RSA_PrivateKey::RSA_PrivateKey(), Botan::Fixed_Window_Exponentiator::set_base(), Botan::Montgomery_Exponentiator::set_base(), Botan::srp6_group_identifier(), and to_u32bit().

229  {
230  const size_t words = sig_words();
231 
232  if(words == 0)
233  return 0;
234 
235  const size_t full_words = words - 1;
236  return (full_words * BOTAN_MP_WORD_BITS + high_bit(word_at(full_words)));
237  }
word word_at(size_t n) const
Definition: bigint.h:440
size_t high_bit(T n)
Definition: bit_ops.h:37
size_t sig_words() const
Definition: bigint.h:519

◆ byte_at()

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

Definition at line 429 of file bigint.h.

References Botan::get_byte().

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

430  {
431  return get_byte(sizeof(word) - (n % sizeof(word)) - 1,
432  word_at(n / sizeof(word)));
433  }
word word_at(size_t n) const
Definition: bigint.h:440
uint8_t get_byte(size_t byte_num, T input)
Definition: loadstor.h:39

◆ bytes()

size_t Botan::BigInt::bytes ( ) const

Give byte length of the integer

Returns
byte length of the represented integer value

Definition at line 220 of file bigint.cpp.

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

Referenced by binary_encode(), botan_mp_num_bytes(), Botan::ECIES_KA_Operation::derive_secret(), Botan::DER_Encoder::encode(), Botan::PointGFp::encode(), encode_1363(), encode_fixed_length_int_pair(), encoded_size(), Botan::EC_PrivateKey::private_key_bits(), Botan::GOST_3410_PublicKey::public_key_bits(), and Botan::SRP6_Server_Session::step1().

221  {
222  return round_up(bits(), 8) / 8;
223  }
size_t bits() const
Definition: bigint.cpp:228
size_t round_up(size_t n, size_t align_to)
Definition: rounding.h:21

◆ clear()

void Botan::BigInt::clear ( )
inline

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

Definition at line 314 of file bigint.h.

References Botan::zeroise().

Referenced by binary_decode(), botan_mp_clear(), mul(), operator%=(), operator*=(), randomize(), and sub().

314 { zeroise(m_reg); }
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:183

◆ clear_bit()

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

Clear bit at specified position

Parameters
nbit position to clear

Definition at line 212 of file bigint.cpp.

References size().

Referenced by botan_mp_clear_bit().

213  {
214  const size_t which = n / BOTAN_MP_WORD_BITS;
215  const word mask = static_cast<word>(1) << (n % BOTAN_MP_WORD_BITS);
216  if(which < size())
217  m_reg[which] &= ~mask;
218  }
size_t size() const
Definition: bigint.h:513

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

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

Referenced by botan_mp_cmp(), Botan::divide(), Botan::operator!=(), Botan::operator<(), Botan::operator<=(), Botan::operator>(), Botan::operator>=(), and Botan::Modular_Reducer::reduce().

132  {
133  if(check_signs)
134  {
135  if(other.is_positive() && this->is_negative())
136  return -1;
137 
138  if(other.is_negative() && this->is_positive())
139  return 1;
140 
141  if(other.is_negative() && this->is_negative())
142  return (-bigint_cmp(this->data(), this->sig_words(),
143  other.data(), other.sig_words()));
144  }
145 
146  return bigint_cmp(this->data(), this->sig_words(),
147  other.data(), other.sig_words());
148  }
bool is_negative() const
Definition: bigint.h:460
int32_t bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:456
const word * data() const
Definition: bigint.h:551
size_t sig_words() const
Definition: bigint.h:519
bool is_positive() const
Definition: bigint.h:466

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

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

117  {
118  if(is_negative())
119  return -1; // other is positive ...
120 
121  const size_t sw = this->sig_words();
122  if(sw > 1)
123  return 1; // must be larger since other is just one word ...
124 
125  return bigint_cmp(this->data(), sw, &other, 1);
126  }
bool is_negative() const
Definition: bigint.h:460
int32_t bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:456
const word * data() const
Definition: bigint.h:551
size_t sig_words() const
Definition: bigint.h:519

◆ const_time_lookup()

void Botan::BigInt::const_time_lookup ( secure_vector< word > &  output,
const std::vector< BigInt > &  vec,
size_t  idx 
)
static

Set output = vec[idx].m_reg in constant time All words of vec must have the same size

Definition at line 356 of file bigint.cpp.

References BOTAN_ASSERT, Botan::clear_mem(), Botan::CT::is_equal(), Botan::CT::poison(), size(), Botan::CT::unpoison(), and word_at().

359  {
360  const size_t words = output.size();
361 
362  clear_mem(output.data(), output.size());
363 
364  CT::poison(&idx, sizeof(idx));
365 
366  for(size_t i = 0; i != vec.size(); ++i)
367  {
368  BOTAN_ASSERT(vec[i].size() >= words,
369  "Word size as expected in const_time_lookup");
370 
371  const word mask = CT::is_equal(i, idx);
372 
373  for(size_t w = 0; w != words; ++w)
374  output[w] |= CT::select<word>(mask, vec[i].word_at(w), 0);
375  }
376 
377  CT::unpoison(idx);
378  CT::unpoison(output.data(), output.size());
379  }
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:97
void poison(const T *p, size_t n)
Definition: ct_utils.h:46
word word_at(size_t n) const
Definition: bigint.h:440
T is_equal(T x, T y)
Definition: ct_utils.h:136
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:43
size_t size() const
Definition: bigint.h:513
void unpoison(const T *p, size_t n)
Definition: ct_utils.h:57

◆ const_time_poison()

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

Definition at line 623 of file bigint.h.

623 {}

◆ const_time_unpoison()

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

Definition at line 624 of file bigint.h.

624 {}

◆ data()

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

◆ decode() [1/3]

BigInt Botan::BigInt::decode ( const uint8_t  buf[],
size_t  length,
Base  base = Binary 
)
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 114 of file big_code.cpp.

References Binary, binary_decode(), Botan::cast_uint8_ptr_to_char(), Botan::Charset::char2digit(), Decimal, Botan::hex_decode_locked(), Hexadecimal, Botan::Charset::is_digit(), and Botan::Charset::is_space().

Referenced by BigInt(), botan_mp_set_from_radix_str(), Botan::TLS::Client_Key_Exchange::Client_Key_Exchange(), decode(), Botan::BER_Decoder::decode_octet_string_bigint(), Botan::CRL_Entry::encode_into(), Botan::OCSP::CertID::is_id_for(), Botan::OS2ECP(), and Botan::TLS::Callbacks::tls_dh_agree().

115  {
116  BigInt r;
117  if(base == Binary)
118  r.binary_decode(buf, length);
119  else if(base == Hexadecimal)
120  {
121  secure_vector<uint8_t> binary;
122 
123  if(length % 2)
124  {
125  // Handle lack of leading 0
126  const char buf0_with_leading_0[2] =
127  { '0', static_cast<char>(buf[0]) };
128 
129  binary = hex_decode_locked(buf0_with_leading_0, 2);
130 
131  binary += hex_decode_locked(cast_uint8_ptr_to_char(&buf[1]),
132  length - 1,
133  false);
134  }
135  else
137  length, false);
138 
139  r.binary_decode(binary.data(), binary.size());
140  }
141  else if(base == Decimal)
142  {
143  for(size_t i = 0; i != length; ++i)
144  {
145  if(Charset::is_space(buf[i]))
146  continue;
147 
148  if(!Charset::is_digit(buf[i]))
149  throw Invalid_Argument("BigInt::decode: "
150  "Invalid character in decimal input");
151 
152  const uint8_t x = Charset::char2digit(buf[i]);
153 
154  if(x >= 10)
155  throw Invalid_Argument("BigInt: Invalid decimal string");
156 
157  r *= 10;
158  r += x;
159  }
160  }
161  else
162  throw Invalid_Argument("Unknown BigInt decoding method");
163  return r;
164  }
secure_vector< uint8_t > hex_decode_locked(const char input[], size_t input_length, bool ignore_ws)
Definition: hex.cpp:165
uint8_t char2digit(char c)
Definition: charset.cpp:231
bool is_space(char c)
Definition: charset.cpp:221
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition: mem_ops.h:136
bool is_digit(char c)
Definition: charset.cpp:210
BigInt()=default

◆ decode() [2/3]

static BigInt Botan::BigInt::decode ( const secure_vector< uint8_t > &  buf,
Base  base = Binary 
)
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 691 of file bigint.h.

References decode().

693  {
694  return BigInt::decode(buf.data(), buf.size(), base);
695  }
static BigInt decode(const uint8_t buf[], size_t length, Base base=Binary)
Definition: big_code.cpp:114

◆ decode() [3/3]

static BigInt Botan::BigInt::decode ( const std::vector< uint8_t > &  buf,
Base  base = Binary 
)
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 703 of file bigint.h.

References decode().

705  {
706  return BigInt::decode(buf.data(), buf.size(), base);
707  }
static BigInt decode(const uint8_t buf[], size_t length, Base base=Binary)
Definition: big_code.cpp:114

◆ encode() [1/2]

std::vector< uint8_t > Botan::BigInt::encode ( const BigInt n,
Base  base = Binary 
)
static

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

Parameters
nthe BigInt to use as integer source
basenumber-base of resulting byte array representation
Returns
secure_vector of bytes containing the integer with given base

Definition at line 54 of file big_code.cpp.

References Binary, and encoded_size().

Referenced by botan_mp_to_hex(), botan_mp_to_str(), Botan::TLS::Client_Key_Exchange::Client_Key_Exchange(), Botan::CRL_Entry::decode_from(), Botan::ECIES_KA_Operation::derive_secret(), Botan::DER_Encoder::encode(), encode_1363(), encode_locked(), Botan::FPE_FE1::FPE_FE1(), Botan::operator<<(), and Botan::TLS::Server_Key_Exchange::Server_Key_Exchange().

55  {
56  std::vector<uint8_t> output(n.encoded_size(base));
57  encode(output.data(), n, base);
58  if(base != Binary)
59  for(size_t j = 0; j != output.size(); ++j)
60  if(output[j] == 0)
61  output[j] = '0';
62  return output;
63  }
static std::vector< uint8_t > encode(const BigInt &n, Base base=Binary)
Definition: big_code.cpp:54

◆ encode() [2/2]

void Botan::BigInt::encode ( uint8_t  buf[],
const BigInt n,
Base  base = Binary 
)
static

Encode the integer value from a BigInt to a byte array

Parameters
bufdestination byte array for the encoded integer value with given base
nthe BigInt to use as integer source
basenumber-base of resulting byte array representation

Definition at line 18 of file big_code.cpp.

References Binary, binary_encode(), Botan::cast_uint8_ptr_to_char(), Decimal, Botan::Charset::digit2char(), Botan::divide(), encoded_size(), Botan::hex_encode(), Hexadecimal, is_zero(), Positive, set_sign(), and word_at().

19  {
20  if(base == Binary)
21  {
22  n.binary_encode(output);
23  }
24  else if(base == Hexadecimal)
25  {
26  secure_vector<uint8_t> binary(n.encoded_size(Binary));
27  n.binary_encode(binary.data());
28 
30  binary.data(), binary.size());
31  }
32  else if(base == Decimal)
33  {
34  BigInt copy = n;
35  BigInt remainder;
36  copy.set_sign(Positive);
37  const size_t output_size = n.encoded_size(Decimal);
38  for(size_t j = 0; j != output_size; ++j)
39  {
40  divide(copy, 10, copy, remainder);
41  output[output_size - 1 - j] =
42  Charset::digit2char(static_cast<uint8_t>(remainder.word_at(0)));
43  if(copy.is_zero())
44  break;
45  }
46  }
47  else
48  throw Invalid_Argument("Unknown BigInt encoding method");
49  }
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition: hex.cpp:14
void divide(const BigInt &x, const BigInt &y_arg, BigInt &q, BigInt &r)
Definition: divide.cpp:58
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition: mem_ops.h:136
char digit2char(uint8_t b)
Definition: charset.cpp:253
BigInt()=default

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

References bytes().

Referenced by Botan::PK_Verifier::check_signature(), Botan::EC_Group::DER_encode(), Botan::PointGFp::encode(), encode_fixed_length_int_pair(), 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().

83  {
84  secure_vector<uint8_t> output(bytes);
85  BigInt::encode_1363(output.data(), output.size(), n);
86  return output;
87  }
size_t bytes() const
Definition: bigint.cpp:220
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
Definition: big_code.cpp:82

◆ encode_1363() [2/2]

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

Definition at line 90 of file big_code.cpp.

References Binary, bytes(), and encode().

91  {
92  const size_t n_bytes = n.bytes();
93  if(n_bytes > bytes)
94  throw Encoding_Error("encode_1363: n is too large to encode properly");
95 
96  const size_t leading_0s = bytes - n_bytes;
97  encode(&output[leading_0s], n, Binary);
98  }
size_t bytes() const
Definition: bigint.cpp:220
static std::vector< uint8_t > encode(const BigInt &n, Base base=Binary)
Definition: big_code.cpp:54

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

References bytes(), and encode_1363().

104  {
105  secure_vector<uint8_t> output(2 * bytes);
106  BigInt::encode_1363(output.data(), bytes, n1);
107  BigInt::encode_1363(output.data() + bytes, bytes, n2);
108  return output;
109  }
size_t bytes() const
Definition: bigint.cpp:220
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
Definition: big_code.cpp:82

◆ encode_locked()

secure_vector< uint8_t > Botan::BigInt::encode_locked ( const BigInt n,
Base  base = Binary 
)
static

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

Parameters
nthe BigInt to use as integer source
basenumber-base of resulting byte array representation
Returns
secure_vector of bytes containing the integer with given base

Definition at line 68 of file big_code.cpp.

References Binary, encode(), and encoded_size().

69  {
70  secure_vector<uint8_t> output(n.encoded_size(base));
71  encode(output.data(), n, base);
72  if(base != Binary)
73  for(size_t j = 0; j != output.size(); ++j)
74  if(output[j] == 0)
75  output[j] = '0';
76  return output;
77  }
static std::vector< uint8_t > encode(const BigInt &n, Base base=Binary)
Definition: big_code.cpp:54

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

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

151  {
152  const size_t words = sig_words();
153 
154  if(words > size)
155  throw Encoding_Error("BigInt::encode_words value too large to encode");
156 
157  clear_mem(out, size);
158  copy_mem(out, data(), words);
159  }
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:97
const word * data() const
Definition: bigint.h:551
size_t size() const
Definition: bigint.h:513
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:108
size_t sig_words() const
Definition: bigint.h:519

◆ encoded_size()

size_t Botan::BigInt::encoded_size ( Base  base = Binary) const
Parameters
basethe base to measure the size for
Returns
size of this integer in base base

Definition at line 242 of file bigint.cpp.

References Binary, bits(), bytes(), Decimal, and Hexadecimal.

Referenced by encode(), and encode_locked().

243  {
244  static const double LOG_2_BASE_10 = 0.30102999566;
245 
246  if(base == Binary)
247  return bytes();
248  else if(base == Hexadecimal)
249  return 2*bytes();
250  else if(base == Decimal)
251  return static_cast<size_t>((bits() * LOG_2_BASE_10) + 1);
252  else
253  throw Invalid_Argument("Unknown base for BigInt encoding");
254  }
size_t bits() const
Definition: bigint.cpp:228
size_t bytes() const
Definition: bigint.cpp:220

◆ flip_sign()

void Botan::BigInt::flip_sign ( )
inline

Flip the sign of this BigInt

Definition at line 487 of file bigint.h.

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

488  {
490  }
Sign reverse_sign() const
Definition: bigint.h:477
void set_sign(Sign sign)
Definition: bigint.h:496

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

Referenced by Botan::PointGFp::encode(), and Botan::operator*().

405  {
406  return ((word_at(n / BOTAN_MP_WORD_BITS) >> (n % BOTAN_MP_WORD_BITS)) & 1);
407  }
word word_at(size_t n) const
Definition: bigint.h:440

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

References byte_at(), and Botan::ASN1::to_string().

Referenced by Botan::Fixed_Window_Exponentiator::execute(), Botan::monty_multi_exp(), Botan::PointGFp_Base_Point_Precompute::mul(), Botan::PointGFp_Var_Point_Precompute::mul(), and Botan::PointGFp_Multi_Point_Precompute::multi_exp().

165  {
166  if(length > 32)
167  throw Invalid_Argument("BigInt::get_substring: Substring size " + std::to_string(length) + " too big");
168 
169  uint64_t piece = 0;
170  for(size_t i = 0; i != 8; ++i)
171  {
172  const uint8_t part = byte_at((offset / 8) + (7-i));
173  piece = (piece << 8) | part;
174  }
175 
176  const uint64_t mask = (static_cast<uint64_t>(1) << length) - 1;
177  const size_t shift = (offset % 8);
178 
179  return static_cast<uint32_t>((piece >> shift) & mask);
180  }
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:210
uint8_t byte_at(size_t n) const
Definition: bigint.h:429

◆ get_word_vector() [1/2]

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

Definition at line 553 of file bigint.h.

Referenced by Botan::PointGFp::add_affine(), Botan::ct_inverse_mod_odd_modulus(), and Botan::Montgomery_Int::fix_size().

553 { return m_reg; }

◆ get_word_vector() [2/2]

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

Definition at line 554 of file bigint.h.

554 { return m_reg; }

◆ grow_to()

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

Increase internal register buffer to at least n words

Parameters
nnew size of register

Definition at line 302 of file bigint.cpp.

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

Referenced by add(), Botan::ct_inverse_mod_odd_modulus(), Botan::divide(), mod_sub(), mul(), Botan::Montgomery_Params::mul_by(), operator%=(), operator*=(), operator<<=(), Botan::Montgomery_Params::redc(), reduce_below(), set_bit(), Botan::Montgomery_Params::square_this(), and sub().

303  {
304  if(n > size())
305  {
306  if(n <= m_reg.capacity())
307  m_reg.resize(m_reg.capacity());
308  else
309  m_reg.resize(round_up(n, 8));
310  }
311  }
size_t size() const
Definition: bigint.h:513
size_t round_up(size_t n, size_t align_to)
Definition: rounding.h:21

◆ is_even()

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

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

References Botan::CT::is_zero().

Referenced by Botan::inverse_euclid(), Botan::low_zero_bits(), and Botan::Modular_Reducer::reduce().

349 { return (!is_zero()); }
bool is_zero() const
Definition: bigint.h:355

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

Referenced by botan_mp_is_odd(), Botan::inverse_euclid(), Botan::inverse_mod(), Botan::normalized_montgomery_inverse(), and Botan::Power_Mod::set_modulus().

343 { return (get_bit(0) == 1); }
bool get_bit(size_t n) const
Definition: bigint.h:404

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

Referenced by botan_mp_is_positive(), cmp(), Botan::low_zero_bits(), and Botan::operator%().

466 { return (sign() == Positive); }
Sign sign() const
Definition: bigint.h:472

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

References Botan::clear_mem().

Referenced by Botan::redc_p521(), and Botan::Modular_Reducer::reduce().

382  {
383  if(n == 0) { clear(); return; }
384 
385  const size_t top_word = n / BOTAN_MP_WORD_BITS;
386  const word mask = (static_cast<word>(1) << (n % BOTAN_MP_WORD_BITS)) - 1;
387 
388  if(top_word < size())
389  {
390  const size_t len = size() - (top_word + 1);
391  if (len > 0)
392  {
393  clear_mem(&m_reg[top_word+1], len);
394  }
395  m_reg[top_word] &= mask;
396  }
397  }
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:97
size_t size() const
Definition: bigint.h:513
void clear()
Definition: bigint.h:314

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

References is_negative(), and reduce_below().

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

113  {
114  if(this->is_negative() || s.is_negative() || mod.is_negative())
115  throw Invalid_Argument("BigInt::mod_add expects all arguments are positive");
116 
117  // TODO add optimized version of this
118  *this += s;
119  this->reduce_below(mod, ws);
120 
121  return (*this);
122  }
bool is_negative() const
Definition: bigint.h:460
void reduce_below(const BigInt &mod, secure_vector< word > &ws)
Definition: bigint.cpp:266

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

References Botan::bigint_add2_nc(), Botan::bigint_cmp(), Botan::bigint_sub2(), Botan::bigint_sub3(), BOTAN_ASSERT_NOMSG, BOTAN_DEBUG_ASSERT, Botan::carry(), data(), grow_to(), is_negative(), mutable_data(), and sig_words().

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

125  {
126  if(this->is_negative() || s.is_negative() || mod.is_negative())
127  throw Invalid_Argument("BigInt::mod_sub expects all arguments are positive");
128 
129  const size_t t_sw = sig_words();
130  const size_t s_sw = s.sig_words();
131  const size_t mod_sw = mod.sig_words();
132 
133  if(t_sw > mod_sw || s_sw > mod_sw)
134  throw Invalid_Argument("BigInt::mod_sub args larger than modulus");
135 
136  BOTAN_DEBUG_ASSERT(*this < mod);
137  BOTAN_DEBUG_ASSERT(s < mod);
138 
139  int32_t relative_size = bigint_cmp(data(), t_sw, s.data(), s_sw);
140 
141  if(relative_size >= 0)
142  {
143  // this >= s in which case just subtract
144  bigint_sub2(mutable_data(), t_sw, s.data(), s_sw);
145  }
146  else
147  {
148  // Otherwise we must sub s and then add p (or add (p - s) as here)
149 
150  if(ws.size() < mod_sw)
151  ws.resize(mod_sw);
152 
153  word borrow = bigint_sub3(ws.data(), mod.data(), mod_sw, s.data(), s_sw);
154  BOTAN_ASSERT_NOMSG(borrow == 0);
155 
156  if(m_reg.size() < mod_sw)
157  grow_to(mod_sw);
158 
159  word carry = bigint_add2_nc(mutable_data(), m_reg.size(), ws.data(), mod_sw);
161  }
162 
163  return (*this);
164  }
bool is_negative() const
Definition: bigint.h:460
void carry(int64_t &h0, int64_t &h1)
int32_t bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:456
word bigint_sub2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:204
word * mutable_data()
Definition: bigint.h:545
#define BOTAN_ASSERT_NOMSG(expr)
Definition: assert.h:56
word bigint_sub3(word z[], const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:276
const word * data() const
Definition: bigint.h:551
#define BOTAN_DEBUG_ASSERT(expr)
Definition: assert.h:111
word bigint_add2_nc(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:138
size_t sig_words() const
Definition: bigint.h:519
void grow_to(size_t n)
Definition: bigint.cpp:302

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

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

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

211  {
212  const size_t x_sw = sig_words();
213  const size_t y_sw = y.sig_words();
214  set_sign((sign() == y.sign()) ? Positive : Negative);
215 
216  if(x_sw == 0 || y_sw == 0)
217  {
218  clear();
220  }
221  else if(x_sw == 1 && y_sw)
222  {
223  grow_to(y_sw + 1);
224  bigint_linmul3(mutable_data(), y.data(), y_sw, word_at(0));
225  }
226  else if(y_sw == 1 && x_sw)
227  {
228  grow_to(x_sw + 1);
229  bigint_linmul2(mutable_data(), x_sw, y.word_at(0));
230  }
231  else
232  {
233  const size_t new_size = x_sw + y_sw + 1;
234  ws.resize(new_size);
235  secure_vector<word> z_reg(new_size);
236 
237  bigint_mul(z_reg.data(), z_reg.size(),
238  data(), size(), x_sw,
239  y.data(), y.size(), y_sw,
240  ws.data(), ws.size());
241 
242  z_reg.swap(m_reg);
243  }
244 
245  return (*this);
246  }
void bigint_linmul2(word x[], size_t x_size, word y)
Definition: mp_core.cpp:300
Sign sign() const
Definition: bigint.h:472
word * mutable_data()
Definition: bigint.h:545
word word_at(size_t n) const
Definition: bigint.h:440
const word * data() const
Definition: bigint.h:551
void bigint_linmul3(word z[], const word x[], size_t x_size, word y)
Definition: mp_core.cpp:318
size_t size() const
Definition: bigint.h:513
size_t sig_words() const
Definition: bigint.h:519
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:293
void clear()
Definition: bigint.h:314
void grow_to(size_t n)
Definition: bigint.cpp:302
void set_sign(Sign sign)
Definition: bigint.h:496

◆ mutable_data()

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

Return a mutable pointer to the register

Returns
a pointer to the start of the internal register

Definition at line 545 of file bigint.h.

Referenced by add(), Botan::divide(), mod_sub(), Botan::Montgomery_Params::mul(), mul(), Botan::mul_add(), Botan::Montgomery_Params::mul_by(), operator*=(), Botan::operator<<(), operator<<=(), operator>>=(), Botan::Montgomery_Params::redc(), Botan::redc_p521(), Botan::Montgomery_Params::sqr(), Botan::Montgomery_Params::square_this(), and sub().

545 { return m_reg.data(); }

◆ operator!()

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

! operator

Returns
true iff this is zero, otherwise false

Definition at line 257 of file bigint.h.

257 { return (!is_nonzero()); }
bool is_nonzero() const
Definition: bigint.h:349

◆ operator%=() [1/2]

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

Modulo operator

Parameters
ythe modulus to reduce this by

Definition at line 297 of file big_ops2.cpp.

298  {
299  return (*this = (*this) % mod);
300  }

◆ operator%=() [2/2]

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

Modulo operator

Parameters
ythe modulus (word) to reduce this by

Definition at line 305 of file big_ops2.cpp.

References Botan::bigint_modop(), clear(), grow_to(), Botan::is_power_of_2(), Negative, Positive, set_sign(), sig_words(), sign(), and word_at().

306  {
307  if(mod == 0)
308  throw BigInt::DivideByZero();
309 
310  if(is_power_of_2(mod))
311  {
312  word result = (word_at(0) & (mod - 1));
313  clear();
314  grow_to(2);
315  m_reg[0] = result;
316  return result;
317  }
318 
319  word remainder = 0;
320 
321  for(size_t j = sig_words(); j > 0; --j)
322  remainder = bigint_modop(remainder, word_at(j-1), mod);
323  clear();
324  grow_to(2);
325 
326  if(remainder && sign() == BigInt::Negative)
327  m_reg[0] = mod - remainder;
328  else
329  m_reg[0] = remainder;
330 
332 
333  return word_at(0);
334  }
Sign sign() const
Definition: bigint.h:472
word word_at(size_t n) const
Definition: bigint.h:440
size_t sig_words() const
Definition: bigint.h:519
void clear()
Definition: bigint.h:314
void grow_to(size_t n)
Definition: bigint.cpp:302
void set_sign(Sign sign)
Definition: bigint.h:496
word bigint_modop(word n1, word n0, word d)
Definition: mp_core.cpp:515
bool is_power_of_2(T arg)
Definition: bit_ops.h:25

◆ operator*=() [1/2]

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

*= operator

Parameters
ythe BigInt to multiply with this

Definition at line 204 of file big_ops2.cpp.

References mul().

205  {
206  secure_vector<word> ws;
207  return this->mul(y, ws);
208  }
BigInt & mul(const BigInt &y, secure_vector< word > &ws)
Definition: big_ops2.cpp:210

◆ operator*=() [2/2]

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

*= operator

Parameters
ythe word to multiply with this

Definition at line 265 of file big_ops2.cpp.

References Botan::bigint_linmul2(), clear(), grow_to(), mutable_data(), Positive, set_sign(), sig_words(), and size().

266  {
267  if(y == 0)
268  {
269  clear();
271  }
272 
273  const size_t x_sw = sig_words();
274 
275  if(size() < x_sw + 1)
276  grow_to(x_sw + 1);
277  bigint_linmul2(mutable_data(), x_sw, y);
278 
279  return (*this);
280  }
void bigint_linmul2(word x[], size_t x_size, word y)
Definition: mp_core.cpp:300
word * mutable_data()
Definition: bigint.h:545
size_t size() const
Definition: bigint.h:513
size_t sig_words() const
Definition: bigint.h:519
void clear()
Definition: bigint.h:314
void grow_to(size_t n)
Definition: bigint.cpp:302
void set_sign(Sign sign)
Definition: bigint.h:496

◆ operator++() [1/2]

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

Increment operator

Definition at line 230 of file bigint.h.

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

◆ operator++() [2/2]

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

Postfix increment operator

Definition at line 240 of file bigint.h.

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

◆ operator+=() [1/2]

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

+= operator

Parameters
ythe BigInt to add to this

Definition at line 53 of file big_ops2.cpp.

References add(), data(), sig_words(), and sign().

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

◆ operator+=() [2/2]

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

+= operator

Parameters
ythe word to add to this

Definition at line 58 of file big_ops2.cpp.

References add(), and Positive.

59  {
60  return add(&y, 1, Positive);
61  }
BigInt & add(const word y[], size_t y_words, Sign sign)
Definition: big_ops2.cpp:15

◆ operator-()

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

Unary negation operator

Returns
negative this

Definition at line 259 of file bigint.cpp.

References flip_sign().

260  {
261  BigInt x = (*this);
262  x.flip_sign();
263  return x;
264  }
BigInt()=default

◆ operator--() [1/2]

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

Decrement operator

Definition at line 235 of file bigint.h.

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

◆ operator--() [2/2]

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

Postfix decrement operator

Definition at line 245 of file bigint.h.

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

◆ operator-=() [1/2]

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

-= operator

Parameters
ythe BigInt to subtract from this

Definition at line 102 of file big_ops2.cpp.

References data(), sig_words(), sign(), and sub().

103  {
104  return sub(y.data(), y.sig_words(), y.sign());
105  }
BigInt & sub(const word y[], size_t y_words, Sign sign)
Definition: big_ops2.cpp:63

◆ operator-=() [2/2]

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

-= operator

Parameters
ythe word to subtract from this

Definition at line 107 of file big_ops2.cpp.

References Positive, and sub().

108  {
109  return sub(&y, 1, Positive);
110  }
BigInt & sub(const word y[], size_t y_words, Sign sign)
Definition: big_ops2.cpp:63

◆ operator/=()

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

/= operator

Parameters
ythe BigInt to divide this by

Definition at line 285 of file big_ops2.cpp.

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

286  {
287  if(y.sig_words() == 1 && is_power_of_2(y.word_at(0)))
288  (*this) >>= (y.bits() - 1);
289  else
290  (*this) = (*this) / y;
291  return (*this);
292  }
bool is_power_of_2(T arg)
Definition: bit_ops.h:25

◆ operator<<=()

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

Left shift operator

Parameters
shiftthe number of bits to shift this left by

Definition at line 339 of file big_ops2.cpp.

References Botan::bigint_shl1(), grow_to(), mutable_data(), and sig_words().

340  {
341  if(shift)
342  {
343  const size_t shift_words = shift / BOTAN_MP_WORD_BITS,
344  shift_bits = shift % BOTAN_MP_WORD_BITS,
345  words = sig_words();
346 
347  /*
348  * FIXME - if shift_words == 0 && the top shift_bits of the top word
349  * are zero then we know that no additional word is needed and can
350  * skip the allocation.
351  */
352  const size_t needed_size = words + shift_words + (shift_bits ? 1 : 0);
353 
354  if(m_reg.size() < needed_size)
355  grow_to(needed_size);
356 
357  bigint_shl1(mutable_data(), words, shift_words, shift_bits);
358  }
359 
360  return (*this);
361  }
word * mutable_data()
Definition: bigint.h:545
size_t sig_words() const
Definition: bigint.h:519
void bigint_shl1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition: mp_core.cpp:336
void grow_to(size_t n)
Definition: bigint.cpp:302

◆ operator=() [1/2]

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

Move assignment

Definition at line 133 of file bigint.h.

134  {
135  if(this != &other)
136  this->swap(other);
137 
138  return (*this);
139  }
void swap(BigInt &other)
Definition: bigint.h:150

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

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

367  {
368  if(shift)
369  {
370  const size_t shift_words = shift / BOTAN_MP_WORD_BITS,
371  shift_bits = shift % BOTAN_MP_WORD_BITS;
372 
373  bigint_shr1(mutable_data(), sig_words(), shift_words, shift_bits);
374 
375  if(is_zero())
377  }
378 
379  return (*this);
380  }
void bigint_shr1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition: mp_core.cpp:359
bool is_zero() const
Definition: bigint.h:355
word * mutable_data()
Definition: bigint.h:545
size_t sig_words() const
Definition: bigint.h:519
void set_sign(Sign sign)
Definition: bigint.h:496

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

References set_bit().

Referenced by Botan::Modular_Reducer::Modular_Reducer(), Botan::Montgomery_Params::Montgomery_Params(), and Botan::ressol().

643  {
644  BigInt b;
645  b.set_bit(n);
646  return b;
647  }
BigInt()=default

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

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

Referenced by botan_mp_rand_range(), Botan::DSA_PrivateKey::DSA_PrivateKey(), Botan::is_prime(), Botan::EC_Group::random_scalar(), and Botan::PointGFp::randomize_repr().

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  }
size_t bits() const
Definition: bigint.cpp:228
BigInt()=default

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

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

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  }
void clear()
Definition: bigint.h:314
void binary_decode(const uint8_t buf[], size_t length)
Definition: bigint.cpp:326
size_t round_up(size_t n, size_t align_to)
Definition: rounding.h:21
void set_sign(Sign sign)
Definition: bigint.h:496

◆ reduce_below()

void Botan::BigInt::reduce_below ( const BigInt mod,
secure_vector< word > &  ws 
)

Return *this below 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 of modulo operator.

Definition at line 266 of file bigint.cpp.

References Botan::bigint_sub3(), Botan::clear_mem(), data(), grow_to(), is_negative(), sig_words(), and size().

Referenced by mod_add(), Botan::Montgomery_Int::mul_by_2(), Botan::Montgomery_Int::mul_by_3(), Botan::Montgomery_Int::mul_by_4(), Botan::Montgomery_Int::mul_by_8(), Botan::Montgomery_Int::operator+(), Botan::redc_p521(), and Botan::Modular_Reducer::reduce().

267  {
268  if(p.is_negative())
269  throw Invalid_Argument("BigInt::reduce_below mod must be positive");
270 
271  const size_t p_words = p.sig_words();
272 
273  if(size() < p_words + 1)
274  grow_to(p_words + 1);
275 
276  if(ws.size() < p_words + 1)
277  ws.resize(p_words + 1);
278 
279  clear_mem(ws.data(), ws.size());
280 
281  for(;;)
282  {
283  word borrow = bigint_sub3(ws.data(), data(), p_words + 1, p.data(), p_words);
284 
285  if(borrow)
286  break;
287 
288  m_reg.swap(ws);
289  }
290  }
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:97
word bigint_sub3(word z[], const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:276
const word * data() const
Definition: bigint.h:551
size_t size() const
Definition: bigint.h:513
void grow_to(size_t n)
Definition: bigint.cpp:302

◆ rev_sub()

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

Set *this to y - *this

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

Definition at line 166 of file big_ops2.cpp.

References Botan::bigint_cmp(), Botan::bigint_sub3(), Botan::clear_mem(), data(), flip_sign(), Positive, sig_words(), and sign().

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

167  {
168  /*
169  *this = BigInt(y, y_sw) - *this;
170  return *this;
171  */
172  if(this->sign() != BigInt::Positive)
173  throw Invalid_State("BigInt::sub_rev requires this is positive");
174 
175  const size_t x_sw = this->sig_words();
176 
177  const int32_t relative_size = bigint_cmp(y, y_sw, this->data(), x_sw);
178 
179  ws.resize(std::max(y_sw, x_sw) + 1);
180  clear_mem(ws.data(), ws.size());
181 
182  if(relative_size < 0)
183  {
184  bigint_sub3(ws.data(), this->data(), x_sw, y, y_sw);
185  this->flip_sign();
186  }
187  else if(relative_size == 0)
188  {
189  ws.clear();
190  }
191  else if(relative_size > 0)
192  {
193  bigint_sub3(ws.data(), y, y_sw, this->data(), x_sw);
194  }
195 
196  m_reg.swap(ws);
197 
198  return (*this);
199  }
int32_t bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:456
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:97
Sign sign() const
Definition: bigint.h:472
word bigint_sub3(word z[], const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:276
const word * data() const
Definition: bigint.h:551
size_t sig_words() const
Definition: bigint.h:519
void flip_sign()
Definition: bigint.h:487

◆ reverse_sign()

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

Definition at line 477 of file bigint.h.

478  {
479  if(sign() == Positive)
480  return Negative;
481  return Positive;
482  }
Sign sign() const
Definition: bigint.h:472

◆ set_bit()

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

Set bit at specified position

Parameters
nbit position to set

Definition at line 201 of file bigint.cpp.

References grow_to(), and size().

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

202  {
203  const size_t which = n / BOTAN_MP_WORD_BITS;
204  const word mask = static_cast<word>(1) << (n % BOTAN_MP_WORD_BITS);
205  if(which >= size()) grow_to(which + 1);
206  m_reg[which] |= mask;
207  }
size_t size() const
Definition: bigint.h:513
void grow_to(size_t n)
Definition: bigint.cpp:302

◆ set_sign()

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

Set sign of the integer

Parameters
signnew Sign to set

Definition at line 496 of file bigint.h.

References Botan::CT::is_zero().

Referenced by abs(), add(), BigInt(), Botan::divide(), encode(), mul(), operator%=(), operator*=(), operator>>=(), randomize(), Botan::Modular_Reducer::reduce(), square(), and sub().

497  {
498  if(is_zero())
499  m_signedness = Positive;
500  else
501  m_signedness = sign;
502  }
Sign sign() const
Definition: bigint.h:472
bool is_zero() const
Definition: bigint.h:355

◆ set_word_at()

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

Definition at line 443 of file bigint.h.

444  {
445  if(i >= m_reg.size())
446  grow_to(i + 1);
447  m_reg[i] = w;
448  }
void grow_to(size_t n)
Definition: bigint.cpp:302

◆ set_words()

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

Definition at line 450 of file bigint.h.

References Botan::copy_mem().

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

451  {
452  m_reg.resize(len);
453  copy_mem(mutable_data(), w, len);
454  }
word * mutable_data()
Definition: bigint.h:545
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:108

◆ shrink_to_fit()

void Botan::BigInt::shrink_to_fit ( size_t  min_size = 0)
inline

Resize the vector to the minimum word size to hold the integer, or min_size words, whichever is larger

Definition at line 566 of file bigint.h.

567  {
568  const size_t words = std::max(min_size, sig_words());
569  m_reg.resize(words);
570  }
size_t sig_words() const
Definition: bigint.h:519

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

Referenced by add(), mul(), Botan::mul_add(), Botan::operator%(), operator%=(), Botan::operator*(), Botan::operator+(), operator+=(), Botan::operator-(), operator-=(), Botan::operator<<(), Botan::operator>>(), rev_sub(), and sub().

472 { return (m_signedness); }

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

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

Referenced by Botan::square().

249  {
250  const size_t sw = sig_words();
251 
252  secure_vector<word> z(2*sw);
253  ws.resize(z.size());
254 
255  bigint_sqr(z.data(), z.size(),
256  data(), size(), sw,
257  ws.data(), ws.size());
258 
259  swap_reg(z);
261 
262  return (*this);
263  }
void swap_reg(secure_vector< word > &reg)
Definition: bigint.h:156
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:352
const word * data() const
Definition: bigint.h:551
size_t size() const
Definition: bigint.h:513
size_t sig_words() const
Definition: bigint.h:519
void set_sign(Sign sign)
Definition: bigint.h:496

◆ sub()

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

Definition at line 63 of file big_ops2.cpp.

References Botan::bigint_add2(), Botan::bigint_cmp(), Botan::bigint_shl1(), Botan::bigint_sub2(), Botan::bigint_sub2_rev(), clear(), data(), grow_to(), mutable_data(), Negative, Positive, set_sign(), sig_words(), and sign().

Referenced by operator-=().

64  {
65  const size_t x_sw = sig_words();
66 
67  int32_t relative_size = bigint_cmp(data(), x_sw, y, y_sw);
68 
69  const size_t reg_size = std::max(x_sw, y_sw) + 1;
70  grow_to(reg_size);
71 
72  if(relative_size < 0)
73  {
74  if(sign() == y_sign)
75  bigint_sub2_rev(mutable_data(), y, y_sw);
76  else
77  bigint_add2(mutable_data(), reg_size - 1, y, y_sw);
78 
79  set_sign(y_sign == Positive ? Negative : Positive);
80  }
81  else if(relative_size == 0)
82  {
83  if(sign() == y_sign)
84  {
85  clear();
87  }
88  else
89  bigint_shl1(mutable_data(), x_sw, 0, 1);
90  }
91  else if(relative_size > 0)
92  {
93  if(sign() == y_sign)
94  bigint_sub2(mutable_data(), x_sw, y, y_sw);
95  else
96  bigint_add2(mutable_data(), reg_size - 1, y, y_sw);
97  }
98 
99  return (*this);
100  }
void bigint_sub2_rev(word x[], const word y[], size_t y_size)
Definition: mp_core.cpp:227
int32_t bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:456
word bigint_sub2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:204
Sign sign() const
Definition: bigint.h:472
word * mutable_data()
Definition: bigint.h:545
const word * data() const
Definition: bigint.h:551
size_t sig_words() const
Definition: bigint.h:519
void clear()
Definition: bigint.h:314
void bigint_shl1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition: mp_core.cpp:336
void grow_to(size_t n)
Definition: bigint.cpp:302
void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:186
void set_sign(Sign sign)
Definition: bigint.h:496

◆ swap()

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

Swap this value with another

Parameters
otherBigInt to swap values with

Definition at line 150 of file bigint.h.

Referenced by botan_mp_swap(), and Botan::PointGFp::swap().

151  {
152  m_reg.swap(other.m_reg);
153  std::swap(m_signedness, other.m_signedness);
154  }

◆ swap_reg()

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

Definition at line 156 of file bigint.h.

Referenced by square().

157  {
158  m_reg.swap(reg);
159  }

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

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

Referenced by botan_mp_to_uint32().

186  {
187  if(is_negative())
188  throw Encoding_Error("BigInt::to_u32bit: Number is negative");
189  if(bits() > 32)
190  throw Encoding_Error("BigInt::to_u32bit: Number is too big to convert");
191 
192  uint32_t out = 0;
193  for(size_t i = 0; i != 4; ++i)
194  out = (out << 8) | byte_at(3-i);
195  return out;
196  }
bool is_negative() const
Definition: bigint.h:460
size_t bits() const
Definition: bigint.cpp:228
uint8_t byte_at(size_t n) const
Definition: bigint.h:429

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

Referenced by bits(), const_time_lookup(), Botan::divide(), encode(), Botan::is_prime(), Botan::low_zero_bits(), Botan::Montgomery_Params::Montgomery_Params(), mul(), Botan::operator%(), operator%=(), Botan::operator*(), Botan::operator/(), operator/=(), and Botan::redc_p521().

441  { return ((n < size()) ? m_reg[n] : 0); }
size_t size() const
Definition: bigint.h:513

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