Botan  2.4.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
 
 BigInt ()=default
 
 BigInt (uint64_t n)
 
 BigInt (const BigInt &other)
 
 BigInt (const std::string &str)
 
 BigInt (const uint8_t buf[], size_t length, Base base=Binary)
 
 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
 
const word * data () 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)
 
word * mutable_data ()
 
bool operator! () const
 
BigIntoperator%= (const BigInt &y)
 
word operator%= (word y)
 
BigIntoperator*= (const BigInt &y)
 
BigIntoperator++ ()
 
BigInt operator++ (int)
 
BigIntoperator+= (const BigInt &y)
 
BigInt operator- () const
 
BigIntoperator-- ()
 
BigInt operator-- (int)
 
BigIntoperator-= (const BigInt &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)
 
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 shrink_to_fit ()
 
size_t sig_words () const
 
Sign sign () const
 
size_t size () const
 
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/8]

Botan::BigInt::BigInt ( )
default

Create empty BigInt

Referenced by operator*=().

◆ BigInt() [2/8]

Botan::BigInt::BigInt ( uint64_t  n)

Create BigInt from 64 bit integer

Parameters
ninitial value of this BigInt

Definition at line 19 of file bigint.cpp.

References Botan::MP_WORD_BITS, and Botan::MP_WORD_MASK.

20  {
21  if(n == 0)
22  return;
23 
24  const size_t limbs_needed = sizeof(uint64_t) / sizeof(word);
25 
26  m_reg.resize(4*limbs_needed);
27  for(size_t i = 0; i != limbs_needed; ++i)
28  m_reg[i] = ((n >> (i*MP_WORD_BITS)) & MP_WORD_MASK);
29  }
const word MP_WORD_MASK
Definition: mp_types.h:27
const size_t MP_WORD_BITS
Definition: mp_core.h:22

◆ BigInt() [3/8]

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

Copy Constructor

Parameters
otherthe BigInt to copy

Definition at line 43 of file bigint.cpp.

44  {
45  m_reg = other.m_reg;
46  m_signedness = other.m_signedness;
47  }

◆ BigInt() [4/8]

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

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

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

◆ BigInt() [5/8]

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

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

References decode().

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

◆ BigInt() [6/8]

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

References randomize().

90  {
91  randomize(rng, bits, set_high_bit);
92  }
size_t bits() const
Definition: bigint.cpp:183
void randomize(RandomNumberGenerator &rng, size_t bitsize, bool set_high_bit=true)
Definition: big_rand.cpp:17

◆ BigInt() [7/8]

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

References Botan::round_up().

35  {
36  m_reg.resize(round_up(size, 8));
37  m_signedness = s;
38  }
size_t size() const
Definition: bigint.h:392
size_t round_up(size_t n, size_t align_to)
Definition: rounding.h:21

◆ BigInt() [8/8]

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

Move constructor

Definition at line 102 of file bigint.h.

103  {
104  this->swap(other);
105  }
void swap(BigInt &other)
Definition: bigint.h:127

Member Function Documentation

◆ abs()

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

Definition at line 253 of file bigint.cpp.

References Positive, and set_sign().

Referenced by Botan::abs().

254  {
255  BigInt x = (*this);
256  x.set_sign(Positive);
257  return x;
258  }
BigInt()=default

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

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

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

280  {
281  const size_t WORD_BYTES = sizeof(word);
282 
283  clear();
284  m_reg.resize(round_up((length / WORD_BYTES) + 1, 8));
285 
286  for(size_t i = 0; i != length / WORD_BYTES; ++i)
287  {
288  const size_t top = length - WORD_BYTES*i;
289  for(size_t j = WORD_BYTES; j > 0; --j)
290  m_reg[i] = (m_reg[i] << 8) | buf[top - j];
291  }
292 
293  for(size_t i = 0; i != length % WORD_BYTES; ++i)
294  m_reg[length / WORD_BYTES] = (m_reg[length / WORD_BYTES] << 8) | buf[i];
295  }
void clear()
Definition: bigint.h:222
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 473 of file bigint.h.

474  {
475  binary_decode(buf.data(), buf.size());
476  }
void binary_decode(const uint8_t buf[], size_t length)
Definition: bigint.cpp:279

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

References byte_at(), and bytes().

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

270  {
271  const size_t sig_bytes = bytes();
272  for(size_t i = 0; i != sig_bytes; ++i)
273  output[sig_bytes-i-1] = byte_at(i);
274  }
uint8_t byte_at(size_t n) const
Definition: bigint.h:329
size_t bytes() const
Definition: bigint.cpp:175

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

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

Referenced by Botan::Blinded_Point_Multiply::blinded_multiply(), botan_mp_num_bits(), bytes(), Botan::RSA_PrivateKey::check_key(), 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::generate_dsa_primes(), Botan::is_prime(), Botan::RSA_PublicKey::key_length(), Botan::DL_Scheme_PublicKey::key_length(), Botan::EC_PublicKey::key_length(), Botan::TPM_PrivateKey::key_length(), Botan::multi_exponentiate(), Botan::operator*(), operator/=(), Botan::operator>>(), random_integer(), Botan::random_prime(), Botan::RSA_PrivateKey::RSA_PrivateKey(), Botan::Fixed_Window_Exponentiator::set_base(), Botan::Montgomery_Exponentiator::set_base(), Botan::Montgomery_Exponentiator::set_exponent(), Botan::srp6_group_identifier(), to_u32bit(), and Botan::Power_Mod::window_bits().

184  {
185  const size_t words = sig_words();
186 
187  if(words == 0)
188  return 0;
189 
190  const size_t full_words = words - 1;
191  return (full_words * MP_WORD_BITS + high_bit(word_at(full_words)));
192  }
word word_at(size_t n) const
Definition: bigint.h:340
size_t high_bit(T n)
Definition: bit_ops.h:37
size_t sig_words() const
Definition: bigint.h:398
const size_t MP_WORD_BITS
Definition: mp_core.h:22

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

330  {
331  return get_byte(sizeof(word) - (n % sizeof(word)) - 1,
332  word_at(n / sizeof(word)));
333  }
word word_at(size_t n) const
Definition: bigint.h:340
uint8_t get_byte(size_t byte_num, T input)
Definition: loadstor.h:39

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

References Botan::zeroise().

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

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

◆ clear_bit()

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

Clear bit at specified position

Parameters
nbit position to clear

Definition at line 167 of file bigint.cpp.

References Botan::MP_WORD_BITS, and size().

Referenced by botan_mp_clear_bit().

168  {
169  const size_t which = n / MP_WORD_BITS;
170  const word mask = static_cast<word>(1) << (n % MP_WORD_BITS);
171  if(which < size())
172  m_reg[which] &= ~mask;
173  }
size_t size() const
Definition: bigint.h:392
const size_t MP_WORD_BITS
Definition: mp_core.h:22

◆ 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 97 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>(), Botan::operator>=(), and Botan::Modular_Reducer::reduce().

98  {
99  if(check_signs)
100  {
101  if(other.is_positive() && this->is_negative())
102  return -1;
103 
104  if(other.is_negative() && this->is_positive())
105  return 1;
106 
107  if(other.is_negative() && this->is_negative())
108  return (-bigint_cmp(this->data(), this->sig_words(),
109  other.data(), other.sig_words()));
110  }
111 
112  return bigint_cmp(this->data(), this->sig_words(),
113  other.data(), other.sig_words());
114  }
bool is_negative() const
Definition: bigint.h:353
int32_t bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:378
const word * data() const
Definition: bigint.h:430
size_t sig_words() const
Definition: bigint.h:398
bool is_positive() const
Definition: bigint.h:359

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

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

Referenced by Botan::Montgomery_Exponentiator::execute().

305  {
306  const size_t words = output.size();
307 
308  clear_mem(output.data(), output.size());
309 
310  CT::poison(&idx, sizeof(idx));
311 
312  for(size_t i = 0; i != vec.size(); ++i)
313  {
314  BOTAN_ASSERT(vec[i].size() >= words,
315  "Word size as expected in const_time_lookup");
316 
317  for(size_t w = 0; w != words; ++w)
318  output[w] |= CT::select<word>(CT::is_equal(i, idx), vec[i].word_at(w), 0);
319  }
320 
321  CT::unpoison(idx);
322  CT::unpoison(output.data(), output.size());
323  }
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:86
void poison(const T *p, size_t n)
Definition: ct_utils.h:46
word word_at(size_t n) const
Definition: bigint.h:340
T is_equal(T x, T y)
Definition: ct_utils.h:124
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:29
size_t size() const
Definition: bigint.h:392
void unpoison(const T *p, size_t n)
Definition: ct_utils.h:57

◆ 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::PKIX::check_crl(), 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(), Botan::TPM_PrivateKey::registered_keys(), Botan::OCSP::Response::status_for(), Botan::TLS::Callbacks::tls_dh_agree(), and Botan::X509_Certificate::X509_Certificate().

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:125
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 548 of file bigint.h.

References decode().

550  {
551  return BigInt::decode(buf.data(), buf.size(), base);
552  }
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 560 of file bigint.h.

References BOTAN_PUBLIC_API, decode(), Botan::operator%(), Botan::operator*(), Botan::operator+(), Botan::operator-(), Botan::operator/(), Botan::operator<<(), and Botan::operator>>().

562  {
563  return BigInt::decode(buf.data(), buf.size(), base);
564  }
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::DER_Encoder::encode(), encode_1363(), encode_locked(), Botan::operator<<(), Botan::ASN1_Formatter::print_to_stream(), Botan::TLS::Server_Key_Exchange::Server_Key_Exchange(), and Botan::X509_Certificate::X509_Certificate().

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:125
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.

Referenced by Botan::PK_Verifier::check_signature(), Botan::EC_Group::DER_encode(), Botan::EC2OSP(), encode_fixed_length_int_pair(), Botan::RFC6979_Nonce_Generator::nonce_for(), Botan::EC_PrivateKey::private_key_bits(), Botan::DH_PublicKey::public_value(), 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:175
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:175
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:175
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

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

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

Referenced by encode(), and encode_locked().

198  {
199  static const double LOG_2_BASE_10 = 0.30102999566;
200 
201  if(base == Binary)
202  return bytes();
203  else if(base == Hexadecimal)
204  return 2*bytes();
205  else if(base == Decimal)
206  return static_cast<size_t>((bits() * LOG_2_BASE_10) + 1);
207  else
208  throw Invalid_Argument("Unknown base for BigInt encoding");
209  }
size_t bits() const
Definition: bigint.cpp:183
size_t bytes() const
Definition: bigint.cpp:175

◆ flip_sign()

void Botan::BigInt::flip_sign ( )

Flip the sign of this BigInt

Definition at line 225 of file bigint.cpp.

References reverse_sign(), and set_sign().

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

226  {
228  }
Sign reverse_sign() const
Definition: bigint.cpp:233
void set_sign(Sign sign)
Definition: bigint.cpp:214

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

References Botan::to_u32bit().

Referenced by Botan::Blinded_Point_Multiply::blinded_multiply(), Botan::EC2OSP(), Botan::multi_exponentiate(), and Botan::operator*().

305  {
306  return ((word_at(n / BOTAN_MP_WORD_BITS) >> (n % BOTAN_MP_WORD_BITS)) & 1);
307  }
word word_at(size_t n) const
Definition: bigint.h:340

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

References byte_at().

Referenced by Botan::Fixed_Window_Exponentiator::execute(), and Botan::Montgomery_Exponentiator::execute().

120  {
121  if(length > 32)
122  throw Invalid_Argument("BigInt::get_substring: Substring size too big");
123 
124  uint64_t piece = 0;
125  for(size_t i = 0; i != 8; ++i)
126  {
127  const uint8_t part = byte_at((offset / 8) + (7-i));
128  piece = (piece << 8) | part;
129  }
130 
131  const uint64_t mask = (static_cast<uint64_t>(1) << length) - 1;
132  const size_t shift = (offset % 8);
133 
134  return static_cast<uint32_t>((piece >> shift) & mask);
135  }
uint8_t byte_at(size_t n) const
Definition: bigint.h:329

◆ get_word_vector() [1/2]

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

Definition at line 432 of file bigint.h.

Referenced by Botan::ct_inverse_mod_odd_modulus().

432 { return m_reg; }

◆ get_word_vector() [2/2]

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

Definition at line 433 of file bigint.h.

433 { 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 260 of file bigint.cpp.

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

Referenced by Botan::ct_inverse_mod_odd_modulus(), Botan::divide(), Botan::Montgomery_Exponentiator::execute(), operator%=(), operator*=(), operator+=(), operator-=(), operator<<=(), and set_bit().

261  {
262  if(n > size())
263  m_reg.resize(round_up(n, 8));
264  }
size_t size() const
Definition: bigint.h:392
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 249 of file bigint.h.

References Botan::CT::is_zero().

Referenced by Botan::gcd(), Botan::inverse_mod(), Botan::low_zero_bits(), and Botan::RSA_PrivateKey::RSA_PrivateKey().

249 { return (!is_zero()); }
bool is_zero() const
Definition: bigint.h:255

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

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

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

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

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

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

◆ is_zero()

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

Test if the integer is zero

Returns
true if the integer is zero, false otherwise

Definition at line 255 of file bigint.h.

Referenced by botan_mp_is_zero(), Botan::ct_inverse_mod_odd_modulus(), Botan::divide(), encode(), Botan::gcd(), Botan::inverse_mod(), Botan::jacobi(), Botan::mul_sub(), Botan::operator%(), operator>>=(), Botan::PointGFp::randomize_repr(), Botan::Power_Mod::set_base(), and set_sign().

256  {
257  const size_t sw = sig_words();
258 
259  for(size_t i = 0; i != sw; ++i)
260  if(m_reg[i])
261  return false;
262  return true;
263  }
size_t sig_words() const
Definition: bigint.h:398

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

References Botan::clear_mem().

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

282  {
283  if(n == 0) { clear(); return; }
284 
285  const size_t top_word = n / BOTAN_MP_WORD_BITS;
286  const word mask = (static_cast<word>(1) << (n % BOTAN_MP_WORD_BITS)) - 1;
287 
288  if(top_word < size())
289  {
290  const size_t len = size() - (top_word + 1);
291  if (len > 0)
292  {
293  clear_mem(&m_reg[top_word+1], len);
294  }
295  m_reg[top_word] &= mask;
296  }
297  }
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:86
size_t size() const
Definition: bigint.h:392
void clear()
Definition: bigint.h:222

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

Referenced by Botan::bigint_monty_mul(), Botan::bigint_monty_sqr(), Botan::bigint_mul(), Botan::divide(), Botan::Montgomery_Exponentiator::execute(), Botan::mul_add(), operator*=(), operator+=(), operator-=(), Botan::operator<<(), operator<<=(), operator>>=(), Botan::redc_p521(), and Botan::square().

424 { return m_reg.data(); }

◆ operator!()

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

! operator

Returns
true iff this is zero, otherwise false

Definition at line 216 of file bigint.h.

216 { return (!is_nonzero()); }
bool is_nonzero() const
Definition: bigint.h:249

◆ operator%=() [1/2]

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

Modulo operator

Parameters
ythe modulus to reduce this by

Definition at line 141 of file big_ops2.cpp.

142  {
143  return (*this = (*this) % mod);
144  }

◆ operator%=() [2/2]

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

Modulo operator

Parameters
ythe modulus (word) to reduce this by

Definition at line 149 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().

150  {
151  if(mod == 0)
152  throw BigInt::DivideByZero();
153 
154  if(is_power_of_2(mod))
155  {
156  word result = (word_at(0) & (mod - 1));
157  clear();
158  grow_to(2);
159  m_reg[0] = result;
160  return result;
161  }
162 
163  word remainder = 0;
164 
165  for(size_t j = sig_words(); j > 0; --j)
166  remainder = bigint_modop(remainder, word_at(j-1), mod);
167  clear();
168  grow_to(2);
169 
170  if(remainder && sign() == BigInt::Negative)
171  m_reg[0] = mod - remainder;
172  else
173  m_reg[0] = remainder;
174 
176 
177  return word_at(0);
178  }
Sign sign() const
Definition: bigint.h:365
word word_at(size_t n) const
Definition: bigint.h:340
size_t sig_words() const
Definition: bigint.h:398
void clear()
Definition: bigint.h:222
void grow_to(size_t n)
Definition: bigint.cpp:260
void set_sign(Sign sign)
Definition: bigint.cpp:214
word bigint_modop(word n1, word n0, word d)
Definition: mp_core.cpp:437
bool is_power_of_2(T arg)
Definition: bit_ops.h:25

◆ operator*=()

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

*= operator

Parameters
ythe BigInt to multiply with this

Definition at line 96 of file big_ops2.cpp.

References BigInt(), 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().

97  {
98  const size_t x_sw = sig_words(), y_sw = y.sig_words();
99  set_sign((sign() == y.sign()) ? Positive : Negative);
100 
101  if(x_sw == 0 || y_sw == 0)
102  {
103  clear();
105  }
106  else if(x_sw == 1 && y_sw)
107  {
108  grow_to(y_sw + 2);
109  bigint_linmul3(mutable_data(), y.data(), y_sw, word_at(0));
110  }
111  else if(y_sw == 1 && x_sw)
112  {
113  grow_to(x_sw + 2);
114  bigint_linmul2(mutable_data(), x_sw, y.word_at(0));
115  }
116  else
117  {
118  grow_to(size() + y.size());
119  secure_vector<word> workspace(size());
120  bigint_mul(*this, BigInt(*this), y, workspace.data());
121  }
122 
123  return (*this);
124  }
void bigint_linmul2(word x[], size_t x_size, word y)
Definition: mp_core.cpp:222
Sign sign() const
Definition: bigint.h:365
word * mutable_data()
Definition: bigint.h:424
word word_at(size_t n) const
Definition: bigint.h:340
void bigint_linmul3(word z[], const word x[], size_t x_size, word y)
Definition: mp_core.cpp:240
size_t size() const
Definition: bigint.h:392
size_t sig_words() const
Definition: bigint.h:398
void clear()
Definition: bigint.h:222
void grow_to(size_t n)
Definition: bigint.cpp:260
void bigint_mul(BigInt &z, const BigInt &x, const BigInt &y, word workspace[])
Definition: mp_karat.cpp:253
BigInt()=default
void set_sign(Sign sign)
Definition: bigint.cpp:214

◆ operator++() [1/2]

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

Increment operator

Definition at line 189 of file bigint.h.

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

◆ operator++() [2/2]

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

Postfix increment operator

Definition at line 199 of file bigint.h.

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

◆ operator+=()

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

+= operator

Parameters
ythe BigInt to add to this

Definition at line 19 of file big_ops2.cpp.

References Botan::bigint_add2(), Botan::bigint_cmp(), Botan::bigint_sub2(), Botan::bigint_sub3(), data(), grow_to(), mutable_data(), Positive, set_sign(), sig_words(), sign(), and Botan::zeroise().

20  {
21  const size_t x_sw = sig_words(), y_sw = y.sig_words();
22 
23  const size_t reg_size = std::max(x_sw, y_sw) + 1;
24  grow_to(reg_size);
25 
26  if(sign() == y.sign())
27  bigint_add2(mutable_data(), reg_size - 1, y.data(), y_sw);
28  else
29  {
30  int32_t relative_size = bigint_cmp(data(), x_sw, y.data(), y_sw);
31 
32  if(relative_size < 0)
33  {
34  secure_vector<word> z(reg_size - 1);
35  bigint_sub3(z.data(), y.data(), reg_size - 1, data(), x_sw);
36  std::swap(m_reg, z);
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  bigint_sub2(mutable_data(), x_sw, y.data(), y_sw);
46  }
47 
48  return (*this);
49  }
int32_t bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:378
word bigint_sub2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:157
Sign sign() const
Definition: bigint.h:365
word * mutable_data()
Definition: bigint.h:424
word bigint_sub3(word z[], const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:198
const word * data() const
Definition: bigint.h:430
size_t sig_words() const
Definition: bigint.h:398
void grow_to(size_t n)
Definition: bigint.cpp:260
void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:138
void set_sign(Sign sign)
Definition: bigint.cpp:214
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:181

◆ operator-()

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

Unary negation operator

Returns
negative this

Definition at line 243 of file bigint.cpp.

References flip_sign().

244  {
245  BigInt x = (*this);
246  x.flip_sign();
247  return x;
248  }
BigInt()=default

◆ operator--() [1/2]

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

Decrement operator

Definition at line 194 of file bigint.h.

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

◆ operator--() [2/2]

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

Postfix decrement operator

Definition at line 204 of file bigint.h.

References Botan::operator-().

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

◆ operator-=()

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

-= operator

Parameters
ythe BigInt to subtract from this

Definition at line 54 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(), Positive, reverse_sign(), set_sign(), sig_words(), and sign().

55  {
56  const size_t x_sw = sig_words(), y_sw = y.sig_words();
57 
58  int32_t relative_size = bigint_cmp(data(), x_sw, y.data(), y_sw);
59 
60  const size_t reg_size = std::max(x_sw, y_sw) + 1;
61  grow_to(reg_size);
62 
63  if(relative_size < 0)
64  {
65  if(sign() == y.sign())
66  bigint_sub2_rev(mutable_data(), y.data(), y_sw);
67  else
68  bigint_add2(mutable_data(), reg_size - 1, y.data(), y_sw);
69 
70  set_sign(y.reverse_sign());
71  }
72  else if(relative_size == 0)
73  {
74  if(sign() == y.sign())
75  {
76  clear();
78  }
79  else
80  bigint_shl1(mutable_data(), x_sw, 0, 1);
81  }
82  else if(relative_size > 0)
83  {
84  if(sign() == y.sign())
85  bigint_sub2(mutable_data(), x_sw, y.data(), y_sw);
86  else
87  bigint_add2(mutable_data(), reg_size - 1, y.data(), y_sw);
88  }
89 
90  return (*this);
91  }
void bigint_sub2_rev(word x[], const word y[], size_t y_size)
Definition: mp_core.cpp:180
int32_t bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:378
word bigint_sub2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:157
Sign sign() const
Definition: bigint.h:365
word * mutable_data()
Definition: bigint.h:424
const word * data() const
Definition: bigint.h:430
size_t sig_words() const
Definition: bigint.h:398
void clear()
Definition: bigint.h:222
void bigint_shl1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition: mp_core.cpp:258
void grow_to(size_t n)
Definition: bigint.cpp:260
void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:138
void set_sign(Sign sign)
Definition: bigint.cpp:214

◆ operator/=()

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

/= operator

Parameters
ythe BigInt to divide this by

Definition at line 129 of file big_ops2.cpp.

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

130  {
131  if(y.sig_words() == 1 && is_power_of_2(y.word_at(0)))
132  (*this) >>= (y.bits() - 1);
133  else
134  (*this) = (*this) / y;
135  return (*this);
136  }
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 183 of file big_ops2.cpp.

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

184  {
185  if(shift)
186  {
187  const size_t shift_words = shift / MP_WORD_BITS,
188  shift_bits = shift % MP_WORD_BITS,
189  words = sig_words();
190 
191  grow_to(words + shift_words + (shift_bits ? 1 : 0));
192  bigint_shl1(mutable_data(), words, shift_words, shift_bits);
193  }
194 
195  return (*this);
196  }
word * mutable_data()
Definition: bigint.h:424
size_t sig_words() const
Definition: bigint.h:398
void bigint_shl1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition: mp_core.cpp:258
void grow_to(size_t n)
Definition: bigint.cpp:260
const size_t MP_WORD_BITS
Definition: mp_core.h:22

◆ operator=() [1/2]

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

Move assignment

Definition at line 110 of file bigint.h.

111  {
112  if(this != &other)
113  this->swap(other);
114 
115  return (*this);
116  }
void swap(BigInt &other)
Definition: bigint.h:127

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

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

202  {
203  if(shift)
204  {
205  const size_t shift_words = shift / MP_WORD_BITS,
206  shift_bits = shift % MP_WORD_BITS;
207 
208  bigint_shr1(mutable_data(), sig_words(), shift_words, shift_bits);
209 
210  if(is_zero())
212  }
213 
214  return (*this);
215  }
void bigint_shr1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition: mp_core.cpp:281
bool is_zero() const
Definition: bigint.h:255
word * mutable_data()
Definition: bigint.h:424
size_t sig_words() const
Definition: bigint.h:398
void set_sign(Sign sign)
Definition: bigint.cpp:214
const size_t MP_WORD_BITS
Definition: mp_core.h:22

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

References Botan::PEM_Code::decode(), Botan::PEM_Code::encode(), and set_bit().

Referenced by Botan::Modular_Reducer::Modular_Reducer(), Botan::Montgomery_Exponentiator::Montgomery_Exponentiator(), Botan::Modular_Reducer::reduce(), and Botan::ressol().

500  {
501  BigInt b;
502  b.set_bit(n);
503  return b;
504  }
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
maxthe maximum value
Returns
random integer in [min,max)

Definition at line 45 of file big_rand.cpp.

References bits(), and randomize().

Referenced by botan_mp_rand_range(), Botan::EC_PrivateKey::EC_PrivateKey(), and Botan::is_prime().

47  {
48  BigInt r;
49 
50  const size_t bits = max.bits();
51 
52  do
53  {
54  r.randomize(rng, bits, false);
55  }
56  while(r < min || r >= max);
57 
58  return r;
59  }
size_t bits() const
Definition: bigint.cpp:183
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::DL_Group::DL_Group(), random_integer(), and Botan::PointGFp::randomize_repr().

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

◆ reverse_sign()

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

Definition at line 233 of file bigint.cpp.

References Negative, Positive, and sign().

Referenced by flip_sign(), Botan::operator-(), and operator-=().

234  {
235  if(sign() == Positive)
236  return Negative;
237  return Positive;
238  }
Sign sign() const
Definition: bigint.h:365

◆ set_bit()

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

Set bit at specified position

Parameters
nbit position to set

Definition at line 156 of file bigint.cpp.

References grow_to(), Botan::MP_WORD_BITS, and size().

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

157  {
158  const size_t which = n / MP_WORD_BITS;
159  const word mask = static_cast<word>(1) << (n % MP_WORD_BITS);
160  if(which >= size()) grow_to(which + 1);
161  m_reg[which] |= mask;
162  }
size_t size() const
Definition: bigint.h:392
void grow_to(size_t n)
Definition: bigint.cpp:260
const size_t MP_WORD_BITS
Definition: mp_core.h:22

◆ set_sign()

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

Set sign of the integer

Parameters
signnew Sign to set

Definition at line 214 of file bigint.cpp.

References is_zero(), and Positive.

Referenced by abs(), BigInt(), Botan::divide(), encode(), flip_sign(), Botan::gcd(), operator%=(), operator*=(), operator+=(), operator-=(), operator>>=(), randomize(), and Botan::Modular_Reducer::reduce().

215  {
216  if(is_zero())
217  m_signedness = Positive;
218  else
219  m_signedness = s;
220  }
bool is_zero() const
Definition: bigint.h:255

◆ set_word_at()

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

Definition at line 343 of file bigint.h.

Referenced by Botan::redc_p521().

344  {
345  grow_to(i + 1);
346  m_reg[i] = w;
347  }
void grow_to(size_t n)
Definition: bigint.cpp:260

◆ shrink_to_fit()

void Botan::BigInt::shrink_to_fit ( )

Definition at line 297 of file bigint.cpp.

References sig_words().

Referenced by Botan::Montgomery_Exponentiator::set_base().

298  {
299  m_reg.resize(sig_words());
300  }
size_t sig_words() const
Definition: bigint.h:398

◆ sig_words()

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

Return how many words we need to hold this value

Returns
significant words of the represented integer value

Definition at line 398 of file bigint.h.

Referenced by Botan::bigint_monty_sqr(), Botan::bigint_mul(), bits(), cmp(), Botan::ct_inverse_mod_odd_modulus(), Botan::divide(), Botan::Montgomery_Exponentiator::execute(), Botan::Modular_Reducer::Modular_Reducer(), Botan::mul_add(), Botan::operator%(), operator%=(), Botan::operator*(), operator*=(), Botan::operator+(), operator+=(), Botan::operator-(), operator-=(), operator/=(), Botan::operator<<(), operator<<=(), Botan::operator>>(), operator>>=(), Botan::redc_p521(), shrink_to_fit(), and Botan::square().

399  {
400  const word* x = m_reg.data();
401  size_t sig = m_reg.size();
402 
403  while(sig && (x[sig-1] == 0))
404  sig--;
405  return sig;
406  }

◆ sign()

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

Return the sign of the integer

Returns
the sign of the integer

Definition at line 365 of file bigint.h.

References Botan::abs().

Referenced by Botan::mul_add(), Botan::operator%(), operator%=(), Botan::operator*(), operator*=(), Botan::operator+(), operator+=(), Botan::operator-(), operator-=(), Botan::operator<<(), Botan::operator>>(), and reverse_sign().

365 { return (m_signedness); }

◆ size()

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

◆ swap()

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

Swap this value with another

Parameters
otherBigInt to swap values with

Definition at line 127 of file bigint.h.

Referenced by botan_mp_swap().

128  {
129  m_reg.swap(other.m_reg);
130  std::swap(m_signedness, other.m_signedness);
131  }

◆ swap_reg()

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

Definition at line 133 of file bigint.h.

References Botan::operator+=().

134  {
135  m_reg.swap(reg);
136  }

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

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

Referenced by botan_mp_to_uint32().

141  {
142  if(is_negative())
143  throw Encoding_Error("BigInt::to_u32bit: Number is negative");
144  if(bits() > 32)
145  throw Encoding_Error("BigInt::to_u32bit: Number is too big to convert");
146 
147  uint32_t out = 0;
148  for(size_t i = 0; i != 4; ++i)
149  out = (out << 8) | byte_at(3-i);
150  return out;
151  }
bool is_negative() const
Definition: bigint.h:353
size_t bits() const
Definition: bigint.cpp:183
uint8_t byte_at(size_t n) const
Definition: bigint.h:329

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

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

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

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