Botan: Botan::BigInt Class Reference

Botan 1.10.17

Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
Botan::BigInt Class Reference

`#include <bigint.h>`

## Classes
struct | DivideByZero

## Public Types
enum | Base { Octal = 8, Decimal = 10, Hexadecimal = 16, Binary = 256 }
enum | NumberType { Power2 }
enum | Sign { Negative = 0, Positive = 1 }

## Public Member Functions
BigInt | abs () const
void | assign (const word x[], size_t length)
| BigInt ()
| BigInt (u64bit n)
| BigInt (const BigInt &other)
| BigInt (const std::string &str)
| BigInt (const byte buf[], size_t length, Base base=Binary)
| BigInt (RandomNumberGenerator &rng, size_t bits)
| BigInt (Sign sign, size_t n)
| BigInt (NumberType type, size_t n)
void | binary_decode (const byte buf[], size_t length)
void | binary_decode (const MemoryRegion< byte > &buf)
void | binary_encode (byte buf[]) const
size_t | bits () const
byte | byte_at (size_t n) const
size_t | bytes () const
void | clear ()
void | clear_bit (size_t n)
s32bit | 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
SecureVector< word > & | get_reg ()
const SecureVector< word > & | get_reg () const
u32bit | get_substring (size_t offset, size_t length) const
void | grow_reg (size_t n)
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)
bool | operator! () const
BigInt & | operator%= (const BigInt &y)
word | operator%= (word y)
BigInt & | operator*= (const BigInt &y)
BigInt & | operator++ ()
BigInt | operator++ (int)
BigInt & | operator+= (const BigInt &y)
BigInt | operator- () const
BigInt & | operator-- ()
BigInt | operator-- (int)
BigInt & | operator-= (const BigInt &y)
BigInt & | operator/= (const BigInt &y)
BigInt & | operator<<= (size_t shift)
BigInt & | operator>>= (size_t shift)
word & | operator[] (size_t i)
const word & | operator[] (size_t i) const
void | randomize (RandomNumberGenerator &rng, size_t bitsize=0)
Sign | reverse_sign () const
void | set_bit (size_t n)
void | set_sign (Sign sign)
void | shrink_to_fit ()
size_t | sig_words () const
Sign | sign () const
size_t | size () const
void | swap (BigInt &other)
u32bit | to_u32bit () const
word | word_at (size_t n) const

## Static Public Member Functions
static void | const_time_lookup (SecureVector< word > &output, const std::vector< BigInt > &vec, size_t idx)
static BigInt | decode (const byte buf[], size_t length, Base base=Binary)
static BigInt | decode (const MemoryRegion< byte > &buf, Base base=Binary)
static SecureVector< byte > | encode (const BigInt &n, Base base=Binary)
static void | encode (byte buf[], const BigInt &n, Base base=Binary)
static SecureVector< byte > | encode_1363 (const BigInt &n, size_t bytes)
static BigInt | random_integer (RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)

## Detailed Description

Arbitrary precision integer

Definition at line 22 of file bigint.h.

## Member Enumeration Documentation

## ◆ Base

enum Botan::BigInt::Base

Base enumerator for encoding and decoding

Enumerator
Octal |
Decimal |
Hexadecimal |
Binary |

Definition at line 28 of file bigint.h.

28 { Octal = 8, Decimal = 10, Hexadecimal = 16, Binary = 256 };
Botan::BigInt::Hexadecimal
Definition: bigint.h:28
Botan::BigInt::Decimal
Definition: bigint.h:28
Botan::BigInt::Octal
Definition: bigint.h:28
Botan::BigInt::Binary
Definition: bigint.h:28

## ◆ NumberType

enum Botan::BigInt::NumberType

Number types (currently only power-of-2 supported)

Enumerator
Power2 |

Definition at line 38 of file bigint.h.

38 { Power2 };
Botan::BigInt::Power2
Definition: bigint.h:38

## ◆ Sign

enum Botan::BigInt::Sign

Sign symbol definitions for positive and negative numbers

Enumerator
Negative |
Positive |

Definition at line 33 of file bigint.h.

33 { Negative = 0, Positive = 1 };
Botan::BigInt::Positive
Definition: bigint.h:33
Botan::BigInt::Negative
Definition: bigint.h:33

## Constructor & Destructor Documentation

## ◆ BigInt() [1/8]

Botan::BigInt::BigInt | ( |  | ) |
| inline

Create empty BigInt

Definition at line 446 of file bigint.h.

446 { signedness = Positive; }
Botan::BigInt::Positive
Definition: bigint.h:33

## ◆ BigInt() [2/8]

Botan::BigInt::BigInt | ( | u64bit | n | ) |

Create BigInt from 64 bit integer

Parameters

  n | initial value of this BigInt

Definition at line 20 of file bigint.cpp.

21 {
22 set_sign(Positive);
23
24 if(n == 0)
25 return;
26
27 const size_t limbs_needed = sizeof(u64bit) / sizeof(word);
28
29 reg.resize(4*limbs_needed);
30 for(size_t i = 0; i != limbs_needed; ++i)
31 reg[i] = ((n >> (i*MP_WORD_BITS)) & MP_WORD_MASK);
32 }
Botan::MemoryRegion::resize
void resize(size_t n)
Definition: secmem.h:217
Botan::BigInt::Positive
Definition: bigint.h:33
Botan::u64bit
unsigned long long u64bit
Definition: types.h:49
Botan::MP_WORD_MASK
const word MP_WORD_MASK
Definition: mp_types.h:27
Botan::BigInt::set_sign
void set_sign(Sign sign)
Definition: bigint.cpp:292
Botan::MP_WORD_BITS
const size_t MP_WORD_BITS
Definition: mp_core.h:18

## ◆ BigInt() [3/8]

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

Copy Constructor

Parameters

  other | the BigInt to copy

Definition at line 46 of file bigint.cpp.

References Botan::MemoryRegion< T >::copy(), data(), Positive, Botan::MemoryRegion< T >::resize(), set_sign(), sig_words(), and sign().

47 {
48 const size_t b_words = b.sig_words();
49
50 if(b_words)
51 {
52 reg.resize(round_up<size_t>(b_words, 8));
53 reg.copy(b.data(), b_words);
54 set_sign(b.sign());
55 }
56 else
57 {
58 reg.resize(2);
59 set_sign(Positive);
60 }
61 }
Botan::MemoryRegion::resize
void resize(size_t n)
Definition: secmem.h:217
Botan::BigInt::Positive
Definition: bigint.h:33
Botan::MemoryRegion::copy
void copy(const T in[], size_t n)
Definition: secmem.h:126
Botan::BigInt::set_sign
void set_sign(Sign sign)
Definition: bigint.cpp:292

## ◆ BigInt() [4/8]

Botan::BigInt::BigInt | ( | const std::string & | str | ) |

Create BigInt from a string. If the string starts with 0x the rest of the string will be interpreted as hexadecimal digits. If the string starts with 0 and the second character is NOT an 'x' the string will be interpreted as octal digits. If the string starts with non-zero digit, it will be interpreted as a decimal number.

Parameters

  str | the string to parse for an integer value

Definition at line 66 of file bigint.cpp.

67 {
68 Base base = Decimal;
69 size_t markers = 0;
70 bool negative = false;
71 if(str.length() > 0 && str[0] == '-') { markers += 1; negative = true; }
72
73 if(str.length() > markers + 2 && str[markers ] == '0' &&
74 str[markers + 1] == 'x')
75 { markers += 2; base = Hexadecimal; }
76 else if(str.length() > markers + 1 && str[markers] == '0')
77 { markers += 1; base = Octal; }
78
79 *this = decode(reinterpret_cast<const byte*>(str.data()) + markers,
80 str.length() - markers, base);
81
82 if(negative) set_sign(Negative);
83 else set_sign(Positive);
84 }
Botan::BigInt::Hexadecimal
Definition: bigint.h:28
Botan::BigInt::Positive
Definition: bigint.h:33
Botan::BigInt::Decimal
Definition: bigint.h:28
Botan::BigInt::Negative
Definition: bigint.h:33
Botan::BigInt::Octal
Definition: bigint.h:28
Botan::BigInt::Base
Base
Definition: bigint.h:28
Botan::BigInt::decode
static BigInt decode(const byte buf[], size_t length, Base base=Binary)
Definition: big_code.cpp:102
Botan::BigInt::set_sign
void set_sign(Sign sign)
Definition: bigint.cpp:292

## ◆ BigInt() [5/8]

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

Create a BigInt from an integer in a byte array

Parameters

  buf | the byte array holding the value
  length | size of buf
  base | is the number base of the integer in buf

Definition at line 89 of file bigint.cpp.

90 {
91 set_sign(Positive);
92 *this = decode(input, length, base);
93 }
Botan::BigInt::Positive
Definition: bigint.h:33
Botan::BigInt::decode
static BigInt decode(const byte buf[], size_t length, Base base=Binary)
Definition: big_code.cpp:102
Botan::BigInt::set_sign
void set_sign(Sign sign)
Definition: bigint.cpp:292

## ◆ BigInt() [6/8]

Botan::BigInt::BigInt | ( | RandomNumberGenerator & | rng,
|  | size_t | bits
| ) |  |

Create a random BigInt of the specified size

Parameters

  rng | random number generator
  bits | size in bits

Definition at line 98 of file bigint.cpp.

99 {
100 set_sign(Positive);
101 randomize(rng, bits);
102 }
Botan::BigInt::Positive
Definition: bigint.h:33
Botan::BigInt::bits
size_t bits() const
Definition: bigint.cpp:254
Botan::BigInt::randomize
void randomize(RandomNumberGenerator &rng, size_t bitsize=0)
Definition: big_rand.cpp:29
Botan::BigInt::set_sign
void set_sign(Sign sign)
Definition: bigint.cpp:292

## ◆ BigInt() [7/8]

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

Create BigInt of specified size, all zeros

Parameters

  sign | the sign
  n | size of the internal register in words

Definition at line 37 of file bigint.cpp.

38 {
39 reg.resize(round_up<size_t>(size, 8));
40 signedness = s;
41 }
Botan::MemoryRegion::resize
void resize(size_t n)
Definition: secmem.h:217
Botan::BigInt::size
size_t size() const
Definition: bigint.h:284

## ◆ BigInt() [8/8]

Botan::BigInt::BigInt | ( | NumberType | type,
|  | size_t | n
| ) |  |

Create a number of the specified type and size

Parameters

  type | the type of number to create. For Power2, will create the integer 2^n
  n | a size/length parameter, interpretation depends upon the value of type

Definition at line 16 of file big_rand.cpp.

References bits(), Positive, Power2, set_bit(), and set_sign().

17 {
18 set_sign(Positive);
19
20 if(type == Power2)
21 set_bit(bits);
22 else
23 throw Invalid_Argument("BigInt(NumberType): Unknown type");
24 }
Botan::BigInt::Positive
Definition: bigint.h:33
Botan::BigInt::bits
size_t bits() const
Definition: bigint.cpp:254
Botan::BigInt::set_bit
void set_bit(size_t n)
Definition: bigint.cpp:206
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::BigInt::Power2
Definition: bigint.h:38
Botan::BigInt::set_sign
void set_sign(Sign sign)
Definition: bigint.cpp:292

## Member Function Documentation

## ◆ abs()

BigInt Botan::BigInt::abs | ( |  | ) | const

Returns
  absolute (positive) value of this

Definition at line 331 of file bigint.cpp.

References Positive, and set_sign().

Referenced by Botan::operator*().

332 {
333 BigInt x = (*this);
334 x.set_sign(Positive);
335 return x;
336 }
Botan::BigInt::Positive
Definition: bigint.h:33
Botan::BigInt::BigInt
BigInt()
Definition: bigint.h:446

## ◆ assign()

void Botan::BigInt::assign | ( | const word | x[],
|  | size_t | length
| ) |  |
| inline

Assign using a plain word array

Definition at line 337 of file bigint.h.

References Botan::copy_mem().

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

338 {
339 reg.resize(length);
340 copy_mem(&reg[0], x, length);
341 }
Botan::MemoryRegion::resize
void resize(size_t n)
Definition: secmem.h:217
Botan::copy_mem
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:22

## ◆ binary_decode() [1/2]

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

Read integer value from a byte array with given size

Parameters

  buf | byte array buffer containing the integer
  length | size of buf

Definition at line 351 of file bigint.cpp.

References clear(), and Botan::MemoryRegion< T >::resize().

Referenced by binary_decode().

352 {
353 const size_t WORD_BYTES = sizeof(word);
354
355 clear();
356 reg.resize(round_up<size_t>((length / WORD_BYTES) + 1, 8));
357
358 for(size_t i = 0; i != length / WORD_BYTES; ++i)
359 {
360 const size_t top = length - WORD_BYTES*i;
361 for(size_t j = WORD_BYTES; j > 0; --j)
362 reg[i] = (reg[i] << 8) | buf[top - j];
363 }
364
365 for(size_t i = 0; i != length % WORD_BYTES; ++i)
366 reg[length / WORD_BYTES] = (reg[length / WORD_BYTES] << 8) | buf[i];
367 }
Botan::MemoryRegion::resize
void resize(size_t n)
Definition: secmem.h:217
Botan::BigInt::clear
void clear()
Definition: bigint.h:143

## ◆ binary_decode() [2/2]

void Botan::BigInt::binary_decode | ( | const MemoryRegion< byte > & | buf | ) |

Read integer value from a byte array (MemoryRegion<byte>)

Parameters

  buf | the array to load from

Definition at line 372 of file bigint.cpp.

References binary_decode(), and Botan::MemoryRegion< T >::size().

373 {
374 binary_decode(buf, buf.size());
375 }
Botan::BigInt::binary_decode
void binary_decode(const byte buf[], size_t length)
Definition: bigint.cpp:351
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29

## ◆ binary_encode()

void Botan::BigInt::binary_encode | ( | byte | buf[] | ) | const

Store BigInt-value in a given byte array

Parameters

  buf | destination byte array for the integer value

Definition at line 341 of file bigint.cpp.

References byte_at(), and bytes().

342 {
343 const size_t sig_bytes = bytes();
344 for(size_t i = 0; i != sig_bytes; ++i)
345 output[sig_bytes-i-1] = byte_at(i);
346 }
Botan::BigInt::bytes
size_t bytes() const
Definition: bigint.cpp:246
Botan::BigInt::byte_at
byte byte_at(size_t n) const
Definition: bigint.cpp:148

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

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

Referenced by BigInt(), bytes(), Botan::BER_Decoder::decode(), Botan::DH_KA_Operation::DH_KA_Operation(), Botan::ElGamal_Decryption_Operation::ElGamal_Decryption_Operation(), Botan::Fixed_Window_Exponentiator::execute(), Botan::multi_exponentiate(), Botan::operator*(), operator/=(), Botan::operator>>(), Botan::srp6_group_identifier(), and to_u32bit().

255 {
256 const size_t words = sig_words();
257
258 if(words == 0)
259 return 0;
260
261 size_t full_words = words - 1, top_bits = MP_WORD_BITS;
262 word top_word = word_at(full_words), mask = MP_WORD_TOP_BIT;
263
264 while(top_bits && ((top_word & mask) == 0))
265 { mask >>= 1; top_bits--; }
266
267 return (full_words * MP_WORD_BITS + top_bits);
268 }
Botan::BigInt::word_at
word word_at(size_t n) const
Definition: bigint.h:238
Botan::BigInt::sig_words
size_t sig_words() const
Definition: bigint.h:290
Botan::MP_WORD_TOP_BIT
const word MP_WORD_TOP_BIT
Definition: mp_types.h:28
Botan::MP_WORD_BITS
const size_t MP_WORD_BITS
Definition: mp_core.h:18

## ◆ byte_at()

byte Botan::BigInt::byte_at | ( | size_t | n | ) | const

Parameters

  n | the offset to get a byte from

Returns
  byte at offset n

Definition at line 148 of file bigint.cpp.

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

149 {
150 const size_t WORD_BYTES = sizeof(word);
151 size_t word_num = n / WORD_BYTES, byte_num = n % WORD_BYTES;
152 if(word_num >= size())
153 return 0;
154 else
155 return get_byte(WORD_BYTES - byte_num - 1, reg[word_num]);
156 }
Botan::get_byte
byte get_byte(size_t byte_num, T input)
Definition: get_byte.h:21
Botan::BigInt::size
size_t size() const
Definition: bigint.h:284

## ◆ bytes()

size_t Botan::BigInt::bytes | ( |  | ) | const

Give byte length of the integer

Returns
  byte length of the represented integer value

Definition at line 246 of file bigint.cpp.

References bits().

Referenced by Botan::ECDH_KA_Operation::agree(), binary_encode(), Botan::ElGamal_Decryption_Operation::decrypt(), Botan::EC_Group::DER_encode(), Botan::EC2OSP(), Botan::ECDSA_Signature::get_concatenation(), Botan::EC_PrivateKey::pkcs8_private_key(), Botan::RSA_Private_Operation::sign(), Botan::DSA_Verification_Operation::verify(), Botan::ECDSA_Verification_Operation::verify(), and Botan::NR_Verification_Operation::verify_mr().

247 {
248 return (bits() + 7) / 8;
249 }
Botan::BigInt::bits
size_t bits() const
Definition: bigint.cpp:254

## ◆ clear()

void Botan::BigInt::clear | ( |  | ) |
| inline

Zeroize the BigInt

Definition at line 143 of file bigint.h.

References Botan::zeroise().

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

143 { zeroise(reg); }
Botan::zeroise
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:428

## ◆ clear_bit()

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

Clear bit at specified position

Parameters

  n | bit position to clear

Definition at line 217 of file bigint.cpp.

218 {
219 const size_t which = n / MP_WORD_BITS;
220 const word mask = static_cast<word>(1) << (n % MP_WORD_BITS);
221 if(which < size())
222 reg[which] &= ~mask;
223 }
Botan::BigInt::size
size_t size() const
Definition: bigint.h:284
Botan::MP_WORD_BITS
const size_t MP_WORD_BITS
Definition: mp_core.h:18

## ◆ cmp()

s32bit Botan::BigInt::cmp | ( | const BigInt & | n,
|  | bool | check_signs = `true`
| ) |  | const

Compare this to another BigInt

Parameters

  n | the BigInt value to compare with
  check_signs | include 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 133 of file bigint.cpp.

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

134 {
135 if(check_signs)
136 {
137 if(n.is_positive() && this->is_negative()) return -1;
138 if(n.is_negative() && this->is_positive()) return 1;
139 if(n.is_negative() && this->is_negative())
140 return (-bigint_cmp(data(), sig_words(), n.data(), n.sig_words()));
141 }
142 return bigint_cmp(data(), sig_words(), n.data(), n.sig_words());
143 }
Botan::BigInt::is_negative
bool is_negative() const
Definition: bigint.h:245
Botan::BigInt::data
const word * data() const
Definition: bigint.h:317
Botan::BigInt::sig_words
size_t sig_words() const
Definition: bigint.h:290
Botan::bigint_cmp
s32bit bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_misc.cpp:41
Botan::BigInt::is_positive
bool is_positive() const
Definition: bigint.h:251

## ◆ const_time_lookup()

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

Definition at line 382 of file bigint.cpp.

References Botan::clear_mem(), Botan::MemoryRegion< T >::data(), Botan::CT::is_equal(), and Botan::MemoryRegion< T >::size().

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

385 {
386 const size_t words = output.size();
387
388 clear_mem(output.data(), output.size());
389
390 for(size_t i = 0; i != vec.size(); ++i)
391 {
392 for(size_t w = 0; w != words; ++w)
393 output[w] |= CT::select<word>(CT::is_equal(i, idx), vec[i].word_at(w), 0);
394 }
395 }
Botan::clear_mem
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:32
Botan::MemoryRegion::data
T * data()
Definition: secmem.h:53
Botan::CT::is_equal
T is_equal(T x, T y)
Definition: ct_utils.h:63
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29

## ◆ data()

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

Return a pointer to the big integer word register

Returns
  a pointer to the start of the internal register of the integer value

Definition at line 317 of file bigint.h.

Referenced by BigInt(), Botan::Montgomery_Exponentiator::execute(), Botan::GMP_MPZ::GMP_MPZ(), Botan::operator*(), operator*=(), Botan::operator+(), operator+=(), Botan::operator-(), operator-=(), Botan::operator<<(), Botan::operator>>(), and Botan::square().

317 { return &reg[0]; }

## ◆ decode() [1/2]

BigInt Botan::BigInt::decode | ( | const byte | buf[],
|  | size_t | length,
|  | Base | base = `Binary`
| ) |  |
| static

Create a BigInt from an integer in a byte array

Parameters

  buf | the binary value to load
  length | size of buf
  base | number-base of the integer in buf

Returns
  BigInt representing the integer in the byte array

Definition at line 102 of file big_code.cpp.

Referenced by Botan::DH_KA_Operation::agree(), Botan::BER_Decoder::decode_octet_string_bigint(), Botan::CRL_Entry::encode_into(), Botan::OS2ECP(), and Botan::OSSL_BN::to_bigint().

103 {
104 BigInt r;
105 if(base == Binary)
106 r.binary_decode(buf, length);
107 else if(base == Hexadecimal)
108 {
109 SecureVector<byte> binary;
110
111 if(length % 2)
112 {
113 // Handle lack of leading 0
114 const char buf0_with_leading_0[2] = { '0', static_cast<char>(buf[0]) };
115 binary = hex_decode(buf0_with_leading_0, 2);
116
117 binary += hex_decode(reinterpret_cast<const char*>(&buf[1]),
118 length - 1,
119 false);
120 }
121 else
122 binary = hex_decode(reinterpret_cast<const char*>(buf),
123 length, false);
124
125 r.binary_decode(&binary[0], binary.size());
126 }
127 else if(base == Decimal || base == Octal)
128 {
129 const size_t RADIX = ((base == Decimal) ? 10 : 8);
130 for(size_t j = 0; j != length; ++j)
131 {
132 if(Charset::is_space(buf[j]))
133 continue;
134
135 if(!Charset::is_digit(buf[j]))
136 throw Invalid_Argument("BigInt::decode: "
137 "Invalid character in decimal input");
138
139 byte x = Charset::char2digit(buf[j]);
140 if(x >= RADIX)
141 {
142 if(RADIX == 10)
143 throw Invalid_Argument("BigInt: Invalid decimal string");
144 else
145 throw Invalid_Argument("BigInt: Invalid octal string");
146 }
147
148 r *= RADIX;
149 r += x;
150 }
151 }
152 else
153 throw Invalid_Argument("Unknown BigInt decoding method");
154 return r;
155 }
Botan::BigInt::Hexadecimal
Definition: bigint.h:28
Botan::BigInt::Decimal
Definition: bigint.h:28
Botan::hex_decode
size_t hex_decode(byte output[], const char input[], size_t input_length, size_t &input_consumed, bool ignore_ws)
Definition: hex.cpp:55
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::Charset::char2digit
byte char2digit(char c)
Definition: charset.cpp:149
Botan::BigInt::Octal
Definition: bigint.h:28
Botan::BigInt::Binary
Definition: bigint.h:28
Botan::Charset::is_digit
bool is_digit(char c)
Definition: charset.cpp:128
Botan::Charset::is_space
bool is_space(char c)
Definition: charset.cpp:139
Botan::BigInt::BigInt
BigInt()
Definition: bigint.h:446

## ◆ decode() [2/2]

BigInt Botan::BigInt::decode | ( | const MemoryRegion< byte > & | buf,
|  | Base | base = `Binary`
| ) |  |
| static

Create a BigInt from an integer in a byte array

Parameters

  buf | the binary value to load
  base | number-base of the integer in buf

Returns
  BigInt representing the integer in the byte array

Definition at line 94 of file big_code.cpp.

95 {
96 return BigInt::decode(&buf[0], buf.size(), base);
97 }
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::BigInt::decode
static BigInt decode(const byte buf[], size_t length, Base base=Binary)
Definition: big_code.cpp:102

## ◆ encode() [1/2]

SecureVector< byte > Botan::BigInt::encode | ( | const BigInt & | n,
|  | Base | base = `Binary`
| ) |  |
| static

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

Parameters

  n | the BigInt to use as integer source
  base | number-base of resulting byte array representation

Returns
  SecureVector of bytes containing the integer with given base

Definition at line 64 of file big_code.cpp.

Referenced by Botan::CRL_Entry::decode_from(), Botan::RSA_Private_Operation::decrypt(), Botan::OSSL_BN::OSSL_BN(), Botan::GMP_MPZ::to_bytes(), Botan::OSSL_BN::to_bytes(), Botan::NR_Verification_Operation::verify_mr(), and Botan::RSA_Public_Operation::verify_mr().

65 {
66 SecureVector<byte> output(n.encoded_size(base));
67 encode(&output[0], n, base);
68 if(base != Binary)
69 for(size_t j = 0; j != output.size(); ++j)
70 if(output[j] == 0)
71 output[j] = '0';
72 return output;
73 }
Botan::BigInt::encode
static SecureVector< byte > encode(const BigInt &n, Base base=Binary)
Definition: big_code.cpp:64
Botan::BigInt::Binary
Definition: bigint.h:28

## ◆ encode() [2/2]

void Botan::BigInt::encode | ( | byte | buf[],
|  | const BigInt & | n,
|  | Base | base = `Binary`
| ) |  |
| static

Encode the integer value from a BigInt to a byte array

Parameters

  buf | destination byte array for the encoded integer value with given base
  n | the BigInt to use as integer source
  base | number-base of resulting byte array representation

Definition at line 18 of file big_code.cpp.

19 {
20 if(base == Binary)
21 n.binary_encode(output);
22 else if(base == Hexadecimal)
23 {
24 SecureVector<byte> binary(n.encoded_size(Binary));
25 n.binary_encode(&binary[0]);
26
27 hex_encode(reinterpret_cast<char*>(output),
28 &binary[0], binary.size());
29 }
30 else if(base == Octal)
31 {
32 BigInt copy = n;
33 const size_t output_size = n.encoded_size(Octal);
34 for(size_t j = 0; j != output_size; ++j)
35 {
36 output[output_size - 1 - j] =
37 Charset::digit2char(static_cast<byte>(copy % 8));
38
39 copy /= 8;
40 }
41 }
42 else if(base == Decimal)
43 {
44 BigInt copy = n;
45 BigInt remainder;
46 copy.set_sign(Positive);
47 const size_t output_size = n.encoded_size(Decimal);
48 for(size_t j = 0; j != output_size; ++j)
49 {
50 divide(copy, 10, copy, remainder);
51 output[output_size - 1 - j] =
52 Charset::digit2char(static_cast<byte>(remainder.word_at(0)));
53 if(copy.is_zero())
54 break;
55 }
56 }
57 else
58 throw Invalid_Argument("Unknown BigInt encoding method");
59 }
Botan::BigInt::Hexadecimal
Definition: bigint.h:28
Botan::divide
void divide(const BigInt &x, const BigInt &y_arg, BigInt &q, BigInt &r)
Definition: divide.cpp:34
Botan::BigInt::Positive
Definition: bigint.h:33
Botan::BigInt::Decimal
Definition: bigint.h:28
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::BigInt::Octal
Definition: bigint.h:28
Botan::BigInt::Binary
Definition: bigint.h:28
Botan::BigInt::BigInt
BigInt()
Definition: bigint.h:446
Botan::Charset::digit2char
char digit2char(byte b)
Definition: charset.cpp:171
Botan::hex_encode
void hex_encode(char output[], const byte input[], size_t input_length, bool uppercase)
Definition: hex.cpp:14

## ◆ encode_1363()

SecureVector< byte > Botan::BigInt::encode_1363 | ( | const BigInt & | n,
|  | size_t | bytes
| ) |  |
| static

Encode a BigInt to a byte array according to IEEE 1363

Parameters

  n | the BigInt to encode
  bytes | the length of the resulting SecureVector<byte>

Returns
  a SecureVector<byte> containing the encoded BigInt

Definition at line 78 of file big_code.cpp.

Referenced by Botan::ECDH_KA_Operation::agree(), Botan::PK_Verifier::check_signature(), Botan::EC_Group::DER_encode(), Botan::EC2OSP(), Botan::RSA_Public_Operation::encrypt(), Botan::ECDSA_Signature::get_concatenation(), Botan::EC_PrivateKey::pkcs8_private_key(), Botan::DH_PublicKey::public_value(), Botan::RSA_Private_Operation::sign(), and Botan::SRP6_Server_Session::step2().

79 {
80 const size_t n_bytes = n.bytes();
81 if(n_bytes > bytes)
82 throw Encoding_Error("encode_1363: n is too large to encode properly");
83
84 const size_t leading_0s = bytes - n_bytes;
85
86 SecureVector<byte> output(bytes);
87 encode(&output[leading_0s], n, Binary);
88 return output;
89 }
Botan::BigInt::encode
static SecureVector< byte > encode(const BigInt &n, Base base=Binary)
Definition: big_code.cpp:64
Botan::BigInt::bytes
size_t bytes() const
Definition: bigint.cpp:246
Botan::BigInt::Binary
Definition: bigint.h:28

## ◆ encoded_size()

size_t Botan::BigInt::encoded_size | ( | Base | base = `Binary` | ) | const

Parameters

  base | the base to measure the size for

Returns
  size of this integer in base base

Definition at line 273 of file bigint.cpp.

274 {
275 static const double LOG_2_BASE_10 = 0.30102999566;
276
277 if(base == Binary)
278 return bytes();
279 else if(base == Hexadecimal)
280 return 2*bytes();
281 else if(base == Octal)
282 return ((bits() + 2) / 3);
283 else if(base == Decimal)
284 return static_cast<size_t>((bits() * LOG_2_BASE_10) + 1);
285 else
286 throw Invalid_Argument("Unknown base for BigInt encoding");
287 }
Botan::BigInt::Hexadecimal
Definition: bigint.h:28
Botan::BigInt::Decimal
Definition: bigint.h:28
Botan::BigInt::bits
size_t bits() const
Definition: bigint.cpp:254
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::BigInt::Octal
Definition: bigint.h:28
Botan::BigInt::bytes
size_t bytes() const
Definition: bigint.cpp:246
Botan::BigInt::Binary
Definition: bigint.h:28

## ◆ flip_sign()

void Botan::BigInt::flip_sign | ( |  | ) |

Flip the sign of this BigInt

Definition at line 303 of file bigint.cpp.

References reverse_sign(), and set_sign().

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

304 {
305 set_sign(reverse_sign());
306 }
Botan::BigInt::reverse_sign
Sign reverse_sign() const
Definition: bigint.cpp:311
Botan::BigInt::set_sign
void set_sign(Sign sign)
Definition: bigint.cpp:292

## ◆ get_bit()

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

Return bit value at specified position

Parameters

  n | the bit offset to test

Returns
  true, if the bit at position n is set, false otherwise

Definition at line 161 of file bigint.cpp.

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

162 {
163 return ((word_at(n / MP_WORD_BITS) >> (n % MP_WORD_BITS)) & 1);
164 }
Botan::BigInt::word_at
word word_at(size_t n) const
Definition: bigint.h:238
Botan::MP_WORD_BITS
const size_t MP_WORD_BITS
Definition: mp_core.h:18

## ◆ get_reg() [1/2]

SecureVector<word>& Botan::BigInt::get_reg | ( |  | ) |
| inline

return a reference to the internal register containing the value

Returns
  a reference to the word-array (SecureVector<word>) with the internal register value (containing the integer value)

Definition at line 325 of file bigint.h.

Referenced by Botan::Montgomery_Exponentiator::execute(), operator*=(), operator+=(), operator-=(), Botan::operator<<(), operator<<=(), operator>>=(), Botan::square(), and Botan::GMP_MPZ::to_bigint().

325 { return reg; }

## ◆ get_reg() [2/2]

const SecureVector<word>& Botan::BigInt::get_reg | ( |  | ) | const
| inline

return a const reference to the internal register containing the value

Returns
  a const reference to the word-array (SecureVector<word>) with the internal register value (containing the integer value)

Definition at line 332 of file bigint.h.

332 { return reg; }

## ◆ get_substring()

u32bit Botan::BigInt::get_substring | ( | size_t | offset,
|  | size_t | length
| ) |  | const

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

Parameters

  offset | the offset to start extracting
  length | amount of bits to extract (starting at offset)

Returns
  the integer extracted from the register starting at offset with specified length

Definition at line 169 of file bigint.cpp.

References byte_at().

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

170 {
171 if(length > 32)
172 throw Invalid_Argument("BigInt::get_substring: Substring size too big");
173
174 u64bit piece = 0;
175 for(size_t i = 0; i != 8; ++i)
176 {
177 const byte part = byte_at((offset / 8) + (7-i));
178 piece = (piece << 8) | part;
179 }
180
181 const u64bit mask = (static_cast<u64bit>(1) << length) - 1;
182 const size_t shift = (offset % 8);
183
184 return static_cast<u32bit>((piece >> shift) & mask);
185 }
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::u64bit
unsigned long long u64bit
Definition: types.h:49
Botan::BigInt::byte_at
byte byte_at(size_t n) const
Definition: bigint.cpp:148
Botan::u32bit
unsigned int u32bit
Definition: types.h:32

## ◆ grow_reg()

void Botan::BigInt::grow_reg | ( | size_t | n | ) |

Increase internal register buffer by n words

Parameters

  n | increase by n words

Definition at line 116 of file bigint.cpp.

117 {
118 reg.resize(round_up<size_t>(size() + n, 8));
119 }
Botan::MemoryRegion::resize
void resize(size_t n)
Definition: secmem.h:217
Botan::BigInt::size
size_t size() const
Definition: bigint.h:284

## ◆ grow_to()

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

Definition at line 124 of file bigint.cpp.

Referenced by operator*=(), operator+=(), operator-=(), and operator<<=().

125 {
126 if(n > size())
127 reg.resize(round_up<size_t>(n, 8));
128 }
Botan::MemoryRegion::resize
void resize(size_t n)
Definition: secmem.h:217
Botan::BigInt::size
size_t size() const
Definition: bigint.h:284

## ◆ is_even()

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

Test if the integer has an even value

Returns
  true if the integer is even, false otherwise

Definition at line 158 of file bigint.h.

Referenced by Botan::IF_Scheme_PublicKey::check_key().

158 { return (get_bit(0) == 0); }
Botan::BigInt::get_bit
bool get_bit(size_t n) const
Definition: bigint.cpp:161

## ◆ is_negative()

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

Tests if the sign of the integer is negative

Returns
  true, iff the integer has a negative sign

Definition at line 245 of file bigint.h.

Referenced by Botan::mul_add(), Botan::multi_exponentiate(), Botan::operator*(), Botan::Modular_Reducer::reduce(), Botan::Power_Mod::set_base(), Botan::Power_Mod::set_exponent(), Botan::sub_mul(), to_u32bit(), and Botan::RW_Verification_Operation::verify_mr().

245 { return (sign() == Negative); }
Botan::BigInt::sign
Sign sign() const
Definition: bigint.h:257
Botan::BigInt::Negative
Definition: bigint.h:33

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

References Botan::CT::is_zero().

Referenced by Botan::gcd().

170 { return (!is_zero()); }
Botan::BigInt::is_zero
bool is_zero() const
Definition: bigint.h:176

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

164 { return (get_bit(0) == 1); }
Botan::BigInt::get_bit
bool get_bit(size_t n) const
Definition: bigint.cpp:161

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

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

251 { return (sign() == Positive); }
Botan::BigInt::Positive
Definition: bigint.h:33
Botan::BigInt::sign
Sign sign() const
Definition: bigint.h:257

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

Referenced by Botan::gcd(), Botan::mul_add(), Botan::operator*(), operator>>=(), and Botan::Power_Mod::set_base().

177 {
178 const size_t sw = sig_words();
179
180 for(size_t i = 0; i != sw; ++i)
181 if(reg[i])
182 return false;
183 return true;
184 }
Botan::BigInt::sig_words
size_t sig_words() const
Definition: bigint.h:290

## ◆ mask_bits()

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

Clear all but the lowest n bits

Parameters

  n | amount of bits to keep

Definition at line 228 of file bigint.cpp.

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

229 {
230 if(n == 0) { clear(); return; }
231 if(n >= bits()) return;
232
233 const size_t top_word = n / MP_WORD_BITS;
234 const word mask = (static_cast<word>(1) << (n % MP_WORD_BITS)) - 1;
235
236 if(top_word < size())
237 for(size_t i = top_word + 1; i != size(); ++i)
238 reg[i] = 0;
239
240 reg[top_word] &= mask;
241 }
Botan::BigInt::bits
size_t bits() const
Definition: bigint.cpp:254
Botan::BigInt::size
size_t size() const
Definition: bigint.h:284
Botan::BigInt::clear
void clear()
Definition: bigint.h:143
Botan::MP_WORD_BITS
const size_t MP_WORD_BITS
Definition: mp_core.h:18

## ◆ operator!()

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

! operator

Returns
  true iff this is zero, otherwise false

Definition at line 124 of file bigint.h.

124 { return (!is_nonzero()); }
Botan::BigInt::is_nonzero
bool is_nonzero() const
Definition: bigint.h:170

## ◆ operator%=() [1/2]

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

Modulo operator

Parameters

  y | the modulus to reduce this by

Definition at line 145 of file big_ops2.cpp.

146 {
147 return (*this = (*this) % mod);
148 }

## ◆ operator%=() [2/2]

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

Modulo operator

Parameters

  y | the modulus (word) to reduce this by

Definition at line 153 of file big_ops2.cpp.

154 {
155 if(mod == 0)
156 throw BigInt::DivideByZero();
157 if(power_of_2(mod))
158 {
159 word result = (word_at(0) & (mod - 1));
160 clear();
161 grow_to(2);
162 get_reg()[0] = result;
163 return result;
164 }
165
166 word remainder = 0;
167
168 for(size_t j = sig_words(); j > 0; --j)
169 remainder = bigint_modop(remainder, word_at(j-1), mod);
170 clear();
171 grow_to(2);
172
173 if(remainder && sign() == BigInt::Negative)
174 get_reg()[0] = mod - remainder;
175 else
176 get_reg()[0] = remainder;
177
178 set_sign(BigInt::Positive);
179
180 return word_at(0);
181 }
Botan::BigInt::Positive
Definition: bigint.h:33
Botan::BigInt::sign
Sign sign() const
Definition: bigint.h:257
Botan::BigInt::get_reg
SecureVector< word > & get_reg()
Definition: bigint.h:325
Botan::BigInt::word_at
word word_at(size_t n) const
Definition: bigint.h:238
Botan::BigInt::Negative
Definition: bigint.h:33
Botan::BigInt::sig_words
size_t sig_words() const
Definition: bigint.h:290
Botan::BigInt::clear
void clear()
Definition: bigint.h:143
Botan::power_of_2
bool power_of_2(T arg)
Definition: bit_ops.h:21
Botan::BigInt::grow_to
void grow_to(size_t n)
Definition: bigint.cpp:124
Botan::BigInt::set_sign
void set_sign(Sign sign)
Definition: bigint.cpp:292
Botan::bigint_modop
word bigint_modop(word n1, word n0, word d)
Definition: mp_misc.cpp:92

## ◆ operator*=()

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

*= operator

Parameters

  y | the BigInt to multiply with this

Definition at line 95 of file big_ops2.cpp.

References Botan::bigint_linmul2(), Botan::bigint_linmul3(), Botan::bigint_mul(), clear(), data(), get_reg(), grow_to(), Negative, Positive, set_sign(), sig_words(), sign(), Botan::MemoryRegion< T >::size(), size(), and word_at().

96 {
97 const size_t x_sw = sig_words(), y_sw = y.sig_words();
98 set_sign((sign() == y.sign()) ? Positive : Negative);
99
100 if(x_sw == 0 || y_sw == 0)
101 {
102 clear();
103 set_sign(Positive);
104 }
105 else if(x_sw == 1 && y_sw)
106 {
107 grow_to(y_sw + 2);
108 bigint_linmul3(get_reg(), y.data(), y_sw, word_at(0));
109 }
110 else if(y_sw == 1 && x_sw)
111 {
112 grow_to(x_sw + 2);
113 bigint_linmul2(get_reg(), x_sw, y.word_at(0));
114 }
115 else
116 {
117 grow_to(size() + y.size());
118
119 SecureVector<word> z(data(), x_sw);
120 SecureVector<word> workspace(size());
121
122 bigint_mul(get_reg(), size(), workspace,
123 z, z.size(), x_sw,
124 y.data(), y.size(), y_sw);
125 }
126
127 return (*this);
128 }
Botan::BigInt::Positive
Definition: bigint.h:33
Botan::MemoryRegion::data
T * data()
Definition: secmem.h:53
Botan::bigint_linmul2
void bigint_linmul2(word x[], size_t x_size, word y)
Definition: mp_asm.cpp:220
Botan::BigInt::sign
Sign sign() const
Definition: bigint.h:257
Botan::BigInt::get_reg
SecureVector< word > & get_reg()
Definition: bigint.h:325
Botan::BigInt::word_at
word word_at(size_t n) const
Definition: bigint.h:238
Botan::BigInt::data
const word * data() const
Definition: bigint.h:317
Botan::bigint_linmul3
void bigint_linmul3(word z[], const word x[], size_t x_size, word y)
Definition: mp_asm.cpp:238
Botan::BigInt::Negative
Definition: bigint.h:33
Botan::BigInt::size
size_t size() const
Definition: bigint.h:284
Botan::BigInt::sig_words
size_t sig_words() const
Definition: bigint.h:290
Botan::BigInt::clear
void clear()
Definition: bigint.h:143
Botan::bigint_mul
void bigint_mul(word z[], size_t z_size, word workspace[], const word x[], size_t x_size, size_t x_sw, const word y[], size_t y_size, size_t y_sw)
Definition: mp_karat.cpp:249
Botan::BigInt::grow_to
void grow_to(size_t n)
Definition: bigint.cpp:124
Botan::BigInt::set_sign
void set_sign(Sign sign)
Definition: bigint.cpp:292

## ◆ operator++() [1/2]

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

Increment operator

Definition at line 97 of file bigint.h.

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

## ◆ operator++() [2/2]

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

Postfix increment operator

Definition at line 107 of file bigint.h.

107 { BigInt x = (*this); ++(*this); return x; }
Botan::BigInt::BigInt
BigInt()
Definition: bigint.h:446

## ◆ operator+=()

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

+= operator

Parameters

  y | the 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_sub3(), Botan::copy_mem(), data(), get_reg(), grow_to(), Botan::CT::max(), Positive, set_sign(), sig_words(), sign(), Botan::MemoryRegion< T >::size(), and Botan::zeroise().

19 {
20 const size_t x_sw = sig_words(), y_sw = y.sig_words();
21
22 const size_t reg_size = std::max(x_sw, y_sw) + 1;
23 grow_to(reg_size);
24
25 if(sign() == y.sign())
26 bigint_add2(get_reg(), reg_size - 1, y.data(), y_sw);
27 else
28 {
29 s32bit relative_size = bigint_cmp(data(), x_sw, y.data(), y_sw);
30
31 if(relative_size < 0)
32 {
33 SecureVector<word> z(reg_size - 1);
34 bigint_sub3(z, y.data(), reg_size - 1, data(), x_sw);
35 copy_mem(&reg[0], &z[0], z.size());
36 set_sign(y.sign());
37 }
38 else if(relative_size == 0)
39 {
40 zeroise(reg);
41 set_sign(Positive);
42 }
43 else if(relative_size > 0)
44 bigint_sub2(get_reg(), x_sw, y.data(), y_sw);
45 }
46
47 return (*this);
48 }
Botan::BigInt::Positive
Definition: bigint.h:33
Botan::MemoryRegion::data
T * data()
Definition: secmem.h:53
Botan::bigint_sub2
word bigint_sub2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_asm.cpp:158
Botan::BigInt::sign
Sign sign() const
Definition: bigint.h:257
Botan::BigInt::get_reg
SecureVector< word > & get_reg()
Definition: bigint.h:325
Botan::bigint_sub3
word bigint_sub3(word z[], const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_asm.cpp:198
Botan::BigInt::data
const word * data() const
Definition: bigint.h:317
Botan::s32bit
signed int s32bit
Definition: types.h:37
Botan::copy_mem
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:22
Botan::BigInt::sig_words
size_t sig_words() const
Definition: bigint.h:290
Botan::CT::max
T max(T a, T b)
Definition: ct_utils.h:120
Botan::BigInt::grow_to
void grow_to(size_t n)
Definition: bigint.cpp:124
Botan::bigint_add2
void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_asm.cpp:139
Botan::BigInt::set_sign
void set_sign(Sign sign)
Definition: bigint.cpp:292
Botan::zeroise
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:428
Botan::bigint_cmp
s32bit bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_misc.cpp:41

## ◆ operator-()

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

Unary negation operator

Returns
  negative this

Definition at line 321 of file bigint.cpp.

References flip_sign().

322 {
323 BigInt x = (*this);
324 x.flip_sign();
325 return x;
326 }
Botan::BigInt::BigInt
BigInt()
Definition: bigint.h:446

## ◆ operator--() [1/2]

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

Decrement operator

Definition at line 102 of file bigint.h.

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

## ◆ operator--() [2/2]

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

Postfix decrement operator

Definition at line 112 of file bigint.h.

112 { BigInt x = (*this); --(*this); return x; }
Botan::BigInt::BigInt
BigInt()
Definition: bigint.h:446

## ◆ operator-=()

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

-= operator

Parameters

  y | the BigInt to subtract from this

Definition at line 53 of file big_ops2.cpp.

References Botan::bigint_add2(), Botan::bigint_cmp(), Botan::bigint_shl1(), Botan::bigint_sub2(), Botan::bigint_sub2_rev(), clear(), data(), get_reg(), grow_to(), Botan::CT::max(), Positive, reverse_sign(), set_sign(), sig_words(), and sign().

54 {
55 const size_t x_sw = sig_words(), y_sw = y.sig_words();
56
57 s32bit relative_size = bigint_cmp(data(), x_sw, y.data(), y_sw);
58
59 const size_t reg_size = std::max(x_sw, y_sw) + 1;
60 grow_to(reg_size);
61
62 if(relative_size < 0)
63 {
64 if(sign() == y.sign())
65 bigint_sub2_rev(get_reg(), y.data(), y_sw);
66 else
67 bigint_add2(get_reg(), reg_size - 1, y.data(), y_sw);
68
69 set_sign(y.reverse_sign());
70 }
71 else if(relative_size == 0)
72 {
73 if(sign() == y.sign())
74 {
75 clear();
76 set_sign(Positive);
77 }
78 else
79 bigint_shl1(get_reg(), x_sw, 0, 1);
80 }
81 else if(relative_size > 0)
82 {
83 if(sign() == y.sign())
84 bigint_sub2(get_reg(), x_sw, y.data(), y_sw);
85 else
86 bigint_add2(get_reg(), reg_size - 1, y.data(), y_sw);
87 }
88
89 return (*this);
90 }
Botan::bigint_sub2_rev
void bigint_sub2_rev(word x[], const word y[], size_t y_size)
Definition: mp_asm.cpp:179
Botan::BigInt::Positive
Definition: bigint.h:33
Botan::MemoryRegion::data
T * data()
Definition: secmem.h:53
Botan::bigint_sub2
word bigint_sub2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_asm.cpp:158
Botan::BigInt::sign
Sign sign() const
Definition: bigint.h:257
Botan::BigInt::get_reg
SecureVector< word > & get_reg()
Definition: bigint.h:325
Botan::BigInt::data
const word * data() const
Definition: bigint.h:317
Botan::s32bit
signed int s32bit
Definition: types.h:37
Botan::BigInt::sig_words
size_t sig_words() const
Definition: bigint.h:290
Botan::CT::max
T max(T a, T b)
Definition: ct_utils.h:120
Botan::BigInt::clear
void clear()
Definition: bigint.h:143
Botan::bigint_shl1
void bigint_shl1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition: mp_shift.cpp:18
Botan::BigInt::grow_to
void grow_to(size_t n)
Definition: bigint.cpp:124
Botan::bigint_add2
void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_asm.cpp:139
Botan::BigInt::set_sign
void set_sign(Sign sign)
Definition: bigint.cpp:292
Botan::bigint_cmp
s32bit bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_misc.cpp:41

## ◆ operator/=()

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

/= operator

Parameters

  y | the BigInt to divide this by

Definition at line 133 of file big_ops2.cpp.

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

134 {
135 if(y.sig_words() == 1 && power_of_2(y.word_at(0)))
136 (*this) >>= (y.bits() - 1);
137 else
138 (*this) = (*this) / y;
139 return (*this);
140 }
Botan::power_of_2
bool power_of_2(T arg)
Definition: bit_ops.h:21

## ◆ operator<<=()

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

Left shift operator

Parameters

  shift | the number of bits to shift this left by

Definition at line 186 of file big_ops2.cpp.

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

187 {
188 if(shift)
189 {
190 const size_t shift_words = shift / MP_WORD_BITS,
191 shift_bits = shift % MP_WORD_BITS,
192 words = sig_words();
193
194 grow_to(words + shift_words + (shift_bits ? 1 : 0));
195 bigint_shl1(get_reg(), words, shift_words, shift_bits);
196 }
197
198 return (*this);
199 }
Botan::BigInt::get_reg
SecureVector< word > & get_reg()
Definition: bigint.h:325
Botan::BigInt::sig_words
size_t sig_words() const
Definition: bigint.h:290
Botan::bigint_shl1
void bigint_shl1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition: mp_shift.cpp:18
Botan::BigInt::grow_to
void grow_to(size_t n)
Definition: bigint.cpp:124
Botan::MP_WORD_BITS
const size_t MP_WORD_BITS
Definition: mp_core.h:18

## ◆ operator>>=()

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

Right shift operator

Parameters

  shift | the number of bits to shift this right by

Definition at line 204 of file big_ops2.cpp.

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

205 {
206 if(shift)
207 {
208 const size_t shift_words = shift / MP_WORD_BITS,
209 shift_bits = shift % MP_WORD_BITS;
210
211 bigint_shr1(get_reg(), sig_words(), shift_words, shift_bits);
212
213 if(is_zero())
214 set_sign(Positive);
215 }
216
217 return (*this);
218 }
Botan::bigint_shr1
void bigint_shr1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition: mp_shift.cpp:42
Botan::BigInt::Positive
Definition: bigint.h:33
Botan::BigInt::get_reg
SecureVector< word > & get_reg()
Definition: bigint.h:325
Botan::BigInt::is_zero
bool is_zero() const
Definition: bigint.h:176
Botan::BigInt::sig_words
size_t sig_words() const
Definition: bigint.h:290
Botan::BigInt::set_sign
void set_sign(Sign sign)
Definition: bigint.cpp:292
Botan::MP_WORD_BITS
const size_t MP_WORD_BITS
Definition: mp_core.h:18

## ◆ operator[]() [1/2]

word& Botan::BigInt::operator[] | ( | size_t | i | ) |
| inline

[] operator (array access)

Parameters

  i | a word index

Returns
  the word at index i

Definition at line 131 of file bigint.h.

131 { return reg[i]; }

## ◆ operator[]() [2/2]

const word& Botan::BigInt::operator[] | ( | size_t | i | ) | const
| inline

[] operator (array access)

Parameters

  i | a word index

Returns
  the word at index i

Definition at line 138 of file bigint.h.

138 { return reg[i]; }

## ◆ random_integer()

BigInt Botan::BigInt::random_integer | ( | RandomNumberGenerator & | rng,
|  | const BigInt & | min,
|  | const BigInt & | max
| ) |  |
| static

Parameters

  rng | a random number generator
  min | the minimum value
  max | the maximum value

Returns
  random integer between min and max

Definition at line 50 of file big_rand.cpp.

52 {
53 BigInt range = max - min;
54
55 if(range <= 0)
56 throw Invalid_Argument("random_integer: invalid min/max values");
57
58 return (min + (BigInt(rng, range.bits() + 2) % range));
59 }
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::CT::max
T max(T a, T b)
Definition: ct_utils.h:120
Botan::BigInt::BigInt
BigInt()
Definition: bigint.h:446
Botan::CT::min
T min(T a, T b)
Definition: ct_utils.h:127

## ◆ randomize()

void Botan::BigInt::randomize | ( | RandomNumberGenerator & | rng,
|  | size_t | bitsize = `0`
| ) |  |

Fill BigInt with a random number with size of bitsize

Parameters

  rng | the random number generator to use
  bitsize | number of bits the created random value should have

Definition at line 29 of file big_rand.cpp.

31 {
32 set_sign(Positive);
33
34 if(bitsize == 0)
35 clear();
36 else
37 {
38 SecureVector<byte> array = rng.random_vec((bitsize + 7) / 8);
39
40 if(bitsize % 8)
41 array[0] &= 0xFF >> (8 - (bitsize % 8));
42 array[0] |= 0x80 >> ((bitsize % 8) ? (8 - bitsize % 8) : 0);
43 binary_decode(&array[0], array.size());
44 }
45 }
Botan::BigInt::Positive
Definition: bigint.h:33
Botan::BigInt::binary_decode
void binary_decode(const byte buf[], size_t length)
Definition: bigint.cpp:351
Botan::BigInt::clear
void clear()
Definition: bigint.h:143
Botan::BigInt::set_sign
void set_sign(Sign sign)
Definition: bigint.cpp:292

## ◆ reverse_sign()

BigInt::Sign Botan::BigInt::reverse_sign | ( |  | ) | const

Returns
  the opposite sign of the represented integer value

Definition at line 311 of file bigint.cpp.

References Negative, Positive, and sign().

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

312 {
313 if(sign() == Positive)
314 return Negative;
315 return Positive;
316 }
Botan::BigInt::Positive
Definition: bigint.h:33
Botan::BigInt::sign
Sign sign() const
Definition: bigint.h:257
Botan::BigInt::Negative
Definition: bigint.h:33

## ◆ set_bit()

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

Set bit at specified position

Parameters

  n | bit position to set

Definition at line 206 of file bigint.cpp.

Referenced by BigInt().

207 {
208 const size_t which = n / MP_WORD_BITS;
209 const word mask = static_cast<word>(1) << (n % MP_WORD_BITS);
210 if(which >= size()) grow_to(which + 1);
211 reg[which] |= mask;
212 }
Botan::BigInt::size
size_t size() const
Definition: bigint.h:284
Botan::BigInt::grow_to
void grow_to(size_t n)
Definition: bigint.cpp:124
Botan::MP_WORD_BITS
const size_t MP_WORD_BITS
Definition: mp_core.h:18

## ◆ set_sign()

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

Set sign of the integer

Parameters

  sign | new Sign to set

Definition at line 292 of file bigint.cpp.

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

293 {
294 if(is_zero())
295 signedness = Positive;
296 else
297 signedness = s;
298 }
Botan::BigInt::Positive
Definition: bigint.h:33
Botan::BigInt::is_zero
bool is_zero() const
Definition: bigint.h:176

## ◆ shrink_to_fit()

void Botan::BigInt::shrink_to_fit | ( |  | ) |

Definition at line 377 of file bigint.cpp.

References Botan::MemoryRegion< T >::resize(), and sig_words().

378 {
379 reg.resize(sig_words());
380 }
Botan::MemoryRegion::resize
void resize(size_t n)
Definition: secmem.h:217
Botan::BigInt::sig_words
size_t sig_words() const
Definition: bigint.h:290

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

Referenced by BigInt(), bits(), Botan::Montgomery_Exponentiator::execute(), Botan::GMP_MPZ::GMP_MPZ(), Botan::mul_add(), Botan::operator*(), operator*=(), Botan::operator+(), operator+=(), Botan::operator-(), operator-=(), operator/=(), Botan::operator<<(), operator<<=(), Botan::operator>>(), operator>>=(), shrink_to_fit(), and Botan::square().

291 {
292 const word* x = &reg[0];
293 size_t sig = reg.size();
294
295 while(sig && (x[sig-1] == 0))
296 sig--;
297 return sig;
298 }
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29

## ◆ sign()

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

Return the sign of the integer

Returns
  the sign of the integer

Definition at line 257 of file bigint.h.

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

257 { return (signedness); }

## ◆ size()

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

Give size of internal register

Returns
  size of internal register in words

Definition at line 284 of file bigint.h.

Referenced by Botan::Montgomery_Exponentiator::execute(), Botan::operator*(), operator*=(), and Botan::square().

284 { return get_reg().size(); }
Botan::BigInt::get_reg
SecureVector< word > & get_reg()
Definition: bigint.h:325
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29

## ◆ swap()

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

Swap this value with another

Parameters

  other | BigInt to swap values with

Definition at line 107 of file bigint.cpp.

References Botan::MemoryRegion< T >::swap(), and std::swap().

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

108 {
109 reg.swap(other.reg);
110 std::swap(signedness, other.signedness);
111 }
Botan::MemoryRegion::swap
void swap(MemoryRegion< T > &other)
Definition: secmem.h:260
std::swap
void swap(Botan::MemoryRegion< T > &x, Botan::MemoryRegion< T > &y)
Definition: secmem.h:438

## ◆ to_u32bit()

u32bit Botan::BigInt::to_u32bit | ( |  | ) | const

Convert this value into a u32bit, if it is in the range [0 ... 2**32-1], or otherwise throw an exception.

Returns
  the value as a u32bit if conversion is possible

Definition at line 190 of file bigint.cpp.

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

191 {
192 if(is_negative())
193 throw Encoding_Error("BigInt::to_u32bit: Number is negative");
194 if(bits() > 32)
195 throw Encoding_Error("BigInt::to_u32bit: Number is too big to convert");
196
197 u32bit out = 0;
198 for(u32bit j = 0; j != 4; ++j)
199 out = (out << 8) | byte_at(3-j);
200 return out;
201 }
Botan::BigInt::is_negative
bool is_negative() const
Definition: bigint.h:245
Botan::BigInt::bits
size_t bits() const
Definition: bigint.cpp:254
Botan::BigInt::byte_at
byte byte_at(size_t n) const
Definition: bigint.cpp:148
Botan::u32bit
unsigned int u32bit
Definition: types.h:32

## ◆ word_at()

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

Return the word at a specified position of the internal register

Parameters

  n | position in the register

Returns
  value at position n

Definition at line 238 of file bigint.h.

Referenced by bits(), Botan::operator*(), operator*=(), and operator/=().

239 { return ((n < size()) ? reg[n] : 0); }
Botan::BigInt::size
size_t size() const
Definition: bigint.h:284

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

- src/math/bigint/bigint.h
- src/math/bigint/big_code.cpp
- src/math/bigint/big_ops2.cpp
- src/math/bigint/big_rand.cpp
- src/math/bigint/bigint.cpp