Botan  2.6.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)
 
 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
 
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)
 
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)
 
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)
 
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 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)
 
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:449
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:314

◆ 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:216
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:216
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:466
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 283 of file bigint.cpp.

References Positive, and set_sign().

Referenced by Botan::abs().

284  {
285  BigInt x = (*this);
286  x.set_sign(Positive);
287  return x;
288  }
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 314 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().

315  {
316  const size_t WORD_BYTES = sizeof(word);
317 
318  clear();
319  m_reg.resize(round_up((length / WORD_BYTES) + 1, 8));
320 
321  for(size_t i = 0; i != length / WORD_BYTES; ++i)
322  {
323  const size_t top = length - WORD_BYTES*i;
324  for(size_t j = WORD_BYTES; j > 0; --j)
325  m_reg[i] = (m_reg[i] << 8) | buf[top - j];
326  }
327 
328  for(size_t i = 0; i != length % WORD_BYTES; ++i)
329  m_reg[length / WORD_BYTES] = (m_reg[length / WORD_BYTES] << 8) | buf[i];
330  }
void clear()
Definition: bigint.h:281
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 551 of file bigint.h.

552  {
553  binary_decode(buf.data(), buf.size());
554  }
void binary_decode(const uint8_t buf[], size_t length)
Definition: bigint.cpp:314

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

References byte_at(), and bytes().

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

305  {
306  const size_t sig_bytes = bytes();
307  for(size_t i = 0; i != sig_bytes; ++i)
308  output[sig_bytes-i-1] = byte_at(i);
309  }
uint8_t byte_at(size_t n) const
Definition: bigint.h:388
size_t bytes() const
Definition: bigint.cpp:208

◆ 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 216 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::generate_dsa_primes(), 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>>(), 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().

217  {
218  const size_t words = sig_words();
219 
220  if(words == 0)
221  return 0;
222 
223  const size_t full_words = words - 1;
224  return (full_words * BOTAN_MP_WORD_BITS + high_bit(word_at(full_words)));
225  }
word word_at(size_t n) const
Definition: bigint.h:399
size_t high_bit(T n)
Definition: bit_ops.h:37
size_t sig_words() const
Definition: bigint.h:472

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

389  {
390  return get_byte(sizeof(word) - (n % sizeof(word)) - 1,
391  word_at(n / sizeof(word)));
392  }
word word_at(size_t n) const
Definition: bigint.h:399
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 208 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().

209  {
210  return round_up(bits(), 8) / 8;
211  }
size_t bits() const
Definition: bigint.cpp:216
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 281 of file bigint.h.

References Botan::zeroise().

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

281 { 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 200 of file bigint.cpp.

References size().

Referenced by botan_mp_clear_bit().

201  {
202  const size_t which = n / BOTAN_MP_WORD_BITS;
203  const word mask = static_cast<word>(1) << (n % BOTAN_MP_WORD_BITS);
204  if(which < size())
205  m_reg[which] &= ~mask;
206  }
size_t size() const
Definition: bigint.h:466

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

120  {
121  if(check_signs)
122  {
123  if(other.is_positive() && this->is_negative())
124  return -1;
125 
126  if(other.is_negative() && this->is_positive())
127  return 1;
128 
129  if(other.is_negative() && this->is_negative())
130  return (-bigint_cmp(this->data(), this->sig_words(),
131  other.data(), other.sig_words()));
132  }
133 
134  return bigint_cmp(this->data(), this->sig_words(),
135  other.data(), other.sig_words());
136  }
bool is_negative() const
Definition: bigint.h:413
int32_t bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:398
const word * data() const
Definition: bigint.h:504
size_t sig_words() const
Definition: bigint.h:472
bool is_positive() const
Definition: bigint.h:419

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

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

341  {
342  const size_t words = output.size();
343 
344  clear_mem(output.data(), output.size());
345 
346  CT::poison(&idx, sizeof(idx));
347 
348  for(size_t i = 0; i != vec.size(); ++i)
349  {
350  BOTAN_ASSERT(vec[i].size() >= words,
351  "Word size as expected in const_time_lookup");
352 
353  const word mask = CT::is_equal(i, idx);
354 
355  for(size_t w = 0; w != words; ++w)
356  output[w] |= CT::select<word>(mask, vec[i].word_at(w), 0);
357  }
358 
359  CT::unpoison(idx);
360  CT::unpoison(output.data(), output.size());
361  }
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:399
T is_equal(T x, T y)
Definition: ct_utils.h:124
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:30
size_t size() const
Definition: bigint.h:466
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::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 632 of file bigint.h.

References decode().

634  {
635  return BigInt::decode(buf.data(), buf.size(), base);
636  }
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 644 of file bigint.h.

References decode().

646  {
647  return BigInt::decode(buf.data(), buf.size(), base);
648  }
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:208
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:208
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:208
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 138 of file bigint.cpp.

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

139  {
140  const size_t words = sig_words();
141 
142  if(words > size)
143  throw Encoding_Error("BigInt::encode_words value too large to encode");
144 
145  clear_mem(out, size);
146  copy_mem(out, data(), words);
147  }
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:97
const word * data() const
Definition: bigint.h:504
size_t size() const
Definition: bigint.h:466
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:108
size_t sig_words() const
Definition: bigint.h:472

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

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

Referenced by encode(), and encode_locked().

231  {
232  static const double LOG_2_BASE_10 = 0.30102999566;
233 
234  if(base == Binary)
235  return bytes();
236  else if(base == Hexadecimal)
237  return 2*bytes();
238  else if(base == Decimal)
239  return static_cast<size_t>((bits() * LOG_2_BASE_10) + 1);
240  else
241  throw Invalid_Argument("Unknown base for BigInt encoding");
242  }
size_t bits() const
Definition: bigint.cpp:216
size_t bytes() const
Definition: bigint.cpp:208

◆ flip_sign()

void Botan::BigInt::flip_sign ( )
inline

Flip the sign of this BigInt

Definition at line 440 of file bigint.h.

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

441  {
443  }
Sign reverse_sign() const
Definition: bigint.h:430
void set_sign(Sign sign)
Definition: bigint.h:449

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

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

364  {
365  return ((word_at(n / BOTAN_MP_WORD_BITS) >> (n % BOTAN_MP_WORD_BITS)) & 1);
366  }
word word_at(size_t n) const
Definition: bigint.h:399

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

153  {
154  if(length > 32)
155  throw Invalid_Argument("BigInt::get_substring: Substring size " + std::to_string(length) + " too big");
156 
157  uint64_t piece = 0;
158  for(size_t i = 0; i != 8; ++i)
159  {
160  const uint8_t part = byte_at((offset / 8) + (7-i));
161  piece = (piece << 8) | part;
162  }
163 
164  const uint64_t mask = (static_cast<uint64_t>(1) << length) - 1;
165  const size_t shift = (offset % 8);
166 
167  return static_cast<uint32_t>((piece >> shift) & mask);
168  }
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:145
uint8_t byte_at(size_t n) const
Definition: bigint.h:388

◆ get_word_vector() [1/2]

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

◆ get_word_vector() [2/2]

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

Definition at line 507 of file bigint.h.

507 { 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 290 of file bigint.cpp.

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

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

291  {
292  if(n > size())
293  {
294  if(n <= m_reg.capacity())
295  m_reg.resize(m_reg.capacity());
296  else
297  m_reg.resize(round_up(n, 8));
298  }
299  }
size_t size() const
Definition: bigint.h:466
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 308 of file bigint.h.

References Botan::CT::is_zero().

Referenced by Botan::gcd(), Botan::inverse_euclid(), and Botan::low_zero_bits().

308 { return (!is_zero()); }
bool is_zero() const
Definition: bigint.h:314

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

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

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

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

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

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

References Botan::clear_mem().

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

341  {
342  if(n == 0) { clear(); return; }
343 
344  const size_t top_word = n / BOTAN_MP_WORD_BITS;
345  const word mask = (static_cast<word>(1) << (n % BOTAN_MP_WORD_BITS)) - 1;
346 
347  if(top_word < size())
348  {
349  const size_t len = size() - (top_word + 1);
350  if (len > 0)
351  {
352  clear_mem(&m_reg[top_word+1], len);
353  }
354  m_reg[top_word] &= mask;
355  }
356  }
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:97
size_t size() const
Definition: bigint.h:466
void clear()
Definition: bigint.h:281

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

143  {
144  const size_t x_sw = sig_words();
145  const size_t y_sw = y.sig_words();
146  set_sign((sign() == y.sign()) ? Positive : Negative);
147 
148  if(x_sw == 0 || y_sw == 0)
149  {
150  clear();
152  }
153  else if(x_sw == 1 && y_sw)
154  {
155  grow_to(y_sw + 1);
156  bigint_linmul3(mutable_data(), y.data(), y_sw, word_at(0));
157  }
158  else if(y_sw == 1 && x_sw)
159  {
160  grow_to(x_sw + 1);
161  bigint_linmul2(mutable_data(), x_sw, y.word_at(0));
162  }
163  else
164  {
165  const size_t new_size = x_sw + y_sw + 1;
166  ws.resize(new_size);
167  secure_vector<word> z_reg(new_size);
168 
169  bigint_mul(z_reg.data(), z_reg.size(),
170  data(), size(), x_sw,
171  y.data(), y.size(), y_sw,
172  ws.data(), ws.size());
173 
174  z_reg.swap(m_reg);
175  }
176 
177  return (*this);
178  }
void bigint_linmul2(word x[], size_t x_size, word y)
Definition: mp_core.cpp:242
Sign sign() const
Definition: bigint.h:425
word * mutable_data()
Definition: bigint.h:498
word word_at(size_t n) const
Definition: bigint.h:399
const word * data() const
Definition: bigint.h:504
void bigint_linmul3(word z[], const word x[], size_t x_size, word y)
Definition: mp_core.cpp:260
size_t size() const
Definition: bigint.h:466
size_t sig_words() const
Definition: bigint.h:472
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:296
void clear()
Definition: bigint.h:281
void grow_to(size_t n)
Definition: bigint.cpp:290
void set_sign(Sign sign)
Definition: bigint.h:449

◆ mutable_data()

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

◆ operator!()

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

! operator

Returns
true iff this is zero, otherwise false

Definition at line 245 of file bigint.h.

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

◆ operator%=() [1/2]

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

Modulo operator

Parameters
ythe modulus to reduce this by

Definition at line 229 of file big_ops2.cpp.

230  {
231  return (*this = (*this) % mod);
232  }

◆ operator%=() [2/2]

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

Modulo operator

Parameters
ythe modulus (word) to reduce this by

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

238  {
239  if(mod == 0)
240  throw BigInt::DivideByZero();
241 
242  if(is_power_of_2(mod))
243  {
244  word result = (word_at(0) & (mod - 1));
245  clear();
246  grow_to(2);
247  m_reg[0] = result;
248  return result;
249  }
250 
251  word remainder = 0;
252 
253  for(size_t j = sig_words(); j > 0; --j)
254  remainder = bigint_modop(remainder, word_at(j-1), mod);
255  clear();
256  grow_to(2);
257 
258  if(remainder && sign() == BigInt::Negative)
259  m_reg[0] = mod - remainder;
260  else
261  m_reg[0] = remainder;
262 
264 
265  return word_at(0);
266  }
Sign sign() const
Definition: bigint.h:425
word word_at(size_t n) const
Definition: bigint.h:399
size_t sig_words() const
Definition: bigint.h:472
void clear()
Definition: bigint.h:281
void grow_to(size_t n)
Definition: bigint.cpp:290
void set_sign(Sign sign)
Definition: bigint.h:449
word bigint_modop(word n1, word n0, word d)
Definition: mp_core.cpp:457
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 136 of file big_ops2.cpp.

References mul().

137  {
138  secure_vector<word> ws;
139  return this->mul(y, ws);
140  }
BigInt & mul(const BigInt &y, secure_vector< word > &ws)
Definition: big_ops2.cpp:142

◆ operator*=() [2/2]

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

*= operator

Parameters
ythe word to multiply with this

Definition at line 197 of file big_ops2.cpp.

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

198  {
199  if(y == 0)
200  {
201  clear();
203  }
204 
205  const size_t x_sw = sig_words();
206 
207  if(size() < x_sw + 1)
208  grow_to(x_sw + 1);
209  bigint_linmul2(mutable_data(), x_sw, y);
210 
211  return (*this);
212  }
void bigint_linmul2(word x[], size_t x_size, word y)
Definition: mp_core.cpp:242
word * mutable_data()
Definition: bigint.h:498
size_t size() const
Definition: bigint.h:466
size_t sig_words() const
Definition: bigint.h:472
void clear()
Definition: bigint.h:281
void grow_to(size_t n)
Definition: bigint.cpp:290
void set_sign(Sign sign)
Definition: bigint.h:449

◆ operator++() [1/2]

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

Increment operator

Definition at line 218 of file bigint.h.

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

◆ operator++() [2/2]

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

Postfix increment operator

Definition at line 228 of file bigint.h.

228 { 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 18 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().

19  {
20  const size_t x_sw = sig_words(), y_sw = y.sig_words();
21 
22  if(sign() == y.sign())
23  {
24  const size_t reg_size = std::max(x_sw, y_sw) + 1;
25 
26  if(m_reg.size() < reg_size)
27  grow_to(reg_size);
28 
29  bigint_add2(mutable_data(), reg_size - 1, y.data(), y_sw);
30  }
31  else
32  {
33  const int32_t relative_size = bigint_cmp(data(), x_sw, y.data(), y_sw);
34 
35  if(relative_size < 0)
36  {
37  const size_t reg_size = std::max(x_sw, y_sw);
38  grow_to(reg_size);
39  bigint_sub2_rev(mutable_data(), y.data(), y_sw);
40  set_sign(y.sign());
41  }
42  else if(relative_size == 0)
43  {
44  zeroise(m_reg);
46  }
47  else if(relative_size > 0)
48  {
49  bigint_sub2(mutable_data(), x_sw, y.data(), y_sw);
50  }
51  }
52 
53  return (*this);
54  }
void bigint_sub2_rev(word x[], const word y[], size_t y_size)
Definition: mp_core.cpp:200
int32_t bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:398
word bigint_sub2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:177
Sign sign() const
Definition: bigint.h:425
word * mutable_data()
Definition: bigint.h:498
const word * data() const
Definition: bigint.h:504
size_t sig_words() const
Definition: bigint.h:472
void grow_to(size_t n)
Definition: bigint.cpp:290
void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:158
void set_sign(Sign sign)
Definition: bigint.h:449
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:183

◆ operator-()

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

Unary negation operator

Returns
negative this

Definition at line 247 of file bigint.cpp.

References flip_sign().

248  {
249  BigInt x = (*this);
250  x.flip_sign();
251  return x;
252  }
BigInt()=default

◆ operator--() [1/2]

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

Decrement operator

Definition at line 223 of file bigint.h.

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

◆ operator--() [2/2]

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

Postfix decrement operator

Definition at line 233 of file bigint.h.

233 { 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 59 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().

60  {
61  const size_t x_sw = sig_words(), y_sw = y.sig_words();
62 
63  int32_t relative_size = bigint_cmp(data(), x_sw, y.data(), y_sw);
64 
65  const size_t reg_size = std::max(x_sw, y_sw) + 1;
66  grow_to(reg_size);
67 
68  if(relative_size < 0)
69  {
70  if(sign() == y.sign())
71  bigint_sub2_rev(mutable_data(), y.data(), y_sw);
72  else
73  bigint_add2(mutable_data(), reg_size - 1, y.data(), y_sw);
74 
75  set_sign(y.reverse_sign());
76  }
77  else if(relative_size == 0)
78  {
79  if(sign() == y.sign())
80  {
81  clear();
83  }
84  else
85  bigint_shl1(mutable_data(), x_sw, 0, 1);
86  }
87  else if(relative_size > 0)
88  {
89  if(sign() == y.sign())
90  bigint_sub2(mutable_data(), x_sw, y.data(), y_sw);
91  else
92  bigint_add2(mutable_data(), reg_size - 1, y.data(), y_sw);
93  }
94 
95  return (*this);
96  }
void bigint_sub2_rev(word x[], const word y[], size_t y_size)
Definition: mp_core.cpp:200
int32_t bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:398
word bigint_sub2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:177
Sign sign() const
Definition: bigint.h:425
word * mutable_data()
Definition: bigint.h:498
const word * data() const
Definition: bigint.h:504
size_t sig_words() const
Definition: bigint.h:472
void clear()
Definition: bigint.h:281
void bigint_shl1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition: mp_core.cpp:278
void grow_to(size_t n)
Definition: bigint.cpp:290
void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:158
void set_sign(Sign sign)
Definition: bigint.h:449

◆ operator/=()

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

/= operator

Parameters
ythe BigInt to divide this by

Definition at line 217 of file big_ops2.cpp.

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

218  {
219  if(y.sig_words() == 1 && is_power_of_2(y.word_at(0)))
220  (*this) >>= (y.bits() - 1);
221  else
222  (*this) = (*this) / y;
223  return (*this);
224  }
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 271 of file big_ops2.cpp.

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

272  {
273  if(shift)
274  {
275  const size_t shift_words = shift / BOTAN_MP_WORD_BITS,
276  shift_bits = shift % BOTAN_MP_WORD_BITS,
277  words = sig_words();
278 
279  /*
280  * FIXME - if shift_words == 0 && the top shift_bits of the top word
281  * are zero then we know that no additional word is needed and can
282  * skip the allocation.
283  */
284  const size_t needed_size = words + shift_words + (shift_bits ? 1 : 0);
285 
286  if(m_reg.size() < needed_size)
287  grow_to(needed_size);
288 
289  bigint_shl1(mutable_data(), words, shift_words, shift_bits);
290  }
291 
292  return (*this);
293  }
word * mutable_data()
Definition: bigint.h:498
size_t sig_words() const
Definition: bigint.h:472
void bigint_shl1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition: mp_core.cpp:278
void grow_to(size_t n)
Definition: bigint.cpp:290

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

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

299  {
300  if(shift)
301  {
302  const size_t shift_words = shift / BOTAN_MP_WORD_BITS,
303  shift_bits = shift % BOTAN_MP_WORD_BITS;
304 
305  bigint_shr1(mutable_data(), sig_words(), shift_words, shift_bits);
306 
307  if(is_zero())
309  }
310 
311  return (*this);
312  }
void bigint_shr1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition: mp_core.cpp:301
bool is_zero() const
Definition: bigint.h:314
word * mutable_data()
Definition: bigint.h:498
size_t sig_words() const
Definition: bigint.h:472
void set_sign(Sign sign)
Definition: bigint.h:449

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

References set_bit().

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

584  {
585  BigInt b;
586  b.set_bit(n);
587  return b;
588  }
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::DSA_PrivateKey::DSA_PrivateKey(), Botan::is_prime(), and Botan::EC_Group::random_scalar().

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

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

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

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

255  {
256  if(p.is_negative())
257  throw Invalid_Argument("BigInt::reduce_below mod must be positive");
258 
259  const size_t p_words = p.sig_words();
260 
261  if(size() < p_words + 1)
262  grow_to(p_words + 1);
263 
264  if(ws.size() < p_words + 1)
265  ws.resize(p_words + 1);
266 
267  clear_mem(ws.data(), ws.size());
268 
269  for(;;)
270  {
271  word borrow = bigint_sub3(ws.data(), data(), p_words + 1, p.data(), p_words);
272 
273  if(borrow)
274  break;
275 
276  m_reg.swap(ws);
277  }
278  }
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:218
const word * data() const
Definition: bigint.h:504
size_t size() const
Definition: bigint.h:466
void grow_to(size_t n)
Definition: bigint.cpp:290

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

99  {
100  /*
101  *this = BigInt(y, y_sw) - *this;
102  return *this;
103  */
104  if(this->sign() != BigInt::Positive)
105  throw Invalid_State("BigInt::sub_rev requires this is positive");
106 
107  const size_t x_sw = this->sig_words();
108 
109  const int32_t relative_size = bigint_cmp(y, y_sw, this->data(), x_sw);
110 
111  ws.resize(std::max(y_sw, x_sw) + 1);
112  clear_mem(ws.data(), ws.size());
113 
114  if(relative_size < 0)
115  {
116  bigint_sub3(ws.data(), this->data(), x_sw, y, y_sw);
117  this->flip_sign();
118  }
119  else if(relative_size == 0)
120  {
121  ws.clear();
122  }
123  else if(relative_size > 0)
124  {
125  bigint_sub3(ws.data(), y, y_sw, this->data(), x_sw);
126  }
127 
128  m_reg.swap(ws);
129 
130  return (*this);
131  }
int32_t bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:398
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:97
Sign sign() const
Definition: bigint.h:425
word bigint_sub3(word z[], const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:218
const word * data() const
Definition: bigint.h:504
size_t sig_words() const
Definition: bigint.h:472
void flip_sign()
Definition: bigint.h:440

◆ reverse_sign()

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

Definition at line 430 of file bigint.h.

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

431  {
432  if(sign() == Positive)
433  return Negative;
434  return Positive;
435  }
Sign sign() const
Definition: bigint.h:425

◆ set_bit()

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

Set bit at specified position

Parameters
nbit position to set

Definition at line 189 of file bigint.cpp.

References grow_to(), and size().

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

190  {
191  const size_t which = n / BOTAN_MP_WORD_BITS;
192  const word mask = static_cast<word>(1) << (n % BOTAN_MP_WORD_BITS);
193  if(which >= size()) grow_to(which + 1);
194  m_reg[which] |= mask;
195  }
size_t size() const
Definition: bigint.h:466
void grow_to(size_t n)
Definition: bigint.cpp:290

◆ set_sign()

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

Set sign of the integer

Parameters
signnew Sign to set

Definition at line 449 of file bigint.h.

References Botan::CT::is_zero().

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

450  {
451  if(is_zero())
452  m_signedness = Positive;
453  else
454  m_signedness = sign;
455  }
Sign sign() const
Definition: bigint.h:425
bool is_zero() const
Definition: bigint.h:314

◆ set_word_at()

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

Definition at line 402 of file bigint.h.

403  {
404  if(i >= m_reg.size())
405  grow_to(i + 1);
406  m_reg[i] = w;
407  }
void grow_to(size_t n)
Definition: bigint.cpp:290

◆ shrink_to_fit()

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

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

Definition at line 332 of file bigint.cpp.

References sig_words().

333  {
334  const size_t words = std::max(min_size, sig_words());
335  m_reg.resize(words);
336  }
size_t sig_words() const
Definition: bigint.h:472

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

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

425 { 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 180 of file big_ops2.cpp.

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

Referenced by Botan::square().

181  {
182  const size_t sw = sig_words();
183 
184  secure_vector<word> z(2*sw);
185  ws.resize(z.size());
186 
187  bigint_sqr(z.data(), z.size(),
188  data(), size(), sw,
189  ws.data(), ws.size());
190 
191  swap_reg(z);
193 
194  return (*this);
195  }
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:351
const word * data() const
Definition: bigint.h:504
size_t size() const
Definition: bigint.h:466
size_t sig_words() const
Definition: bigint.h:472
void set_sign(Sign sign)
Definition: bigint.h:449

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

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

Referenced by botan_mp_to_uint32().

174  {
175  if(is_negative())
176  throw Encoding_Error("BigInt::to_u32bit: Number is negative");
177  if(bits() > 32)
178  throw Encoding_Error("BigInt::to_u32bit: Number is too big to convert");
179 
180  uint32_t out = 0;
181  for(size_t i = 0; i != 4; ++i)
182  out = (out << 8) | byte_at(3-i);
183  return out;
184  }
bool is_negative() const
Definition: bigint.h:413
size_t bits() const
Definition: bigint.cpp:216
uint8_t byte_at(size_t n) const
Definition: bigint.h:388

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

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

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