Botan: Botan::BigInt Class Reference

Botan 1.10.16

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

20 {
21 set_sign(Positive);
22
23 if(n == 0)
24 return;
25
26 const size_t limbs_needed = sizeof(u64bit) / sizeof(word);
27
28 reg.resize(4*limbs_needed);
29 for(size_t i = 0; i != limbs_needed; ++i)
30 reg[i] = ((n >> (i*MP_WORD_BITS)) & MP_WORD_MASK);
31 }
Botan::MemoryRegion::resize
void resize(size_t n)
Definition: secmem.h:211
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:291
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 45 of file bigint.cpp.

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

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

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

66 {
67 Base base = Decimal;
68 size_t markers = 0;
69 bool negative = false;
70 if(str.length() > 0 && str[0] == '-') { markers += 1; negative = true; }
71
72 if(str.length() > markers + 2 && str[markers ] == '0' &&
73 str[markers + 1] == 'x')
74 { markers += 2; base = Hexadecimal; }
75 else if(str.length() > markers + 1 && str[markers] == '0')
76 { markers += 1; base = Octal; }
77
78 *this = decode(reinterpret_cast<const byte*>(str.data()) + markers,
79 str.length() - markers, base);
80
81 if(negative) set_sign(Negative);
82 else set_sign(Positive);
83 }
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:291

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

89 {
90 set_sign(Positive);
91 *this = decode(input, length, base);
92 }
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:291

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

98 {
99 set_sign(Positive);
100 randomize(rng, bits);
101 }
Botan::BigInt::Positive
Definition: bigint.h:33
Botan::BigInt::bits
size_t bits() const
Definition: bigint.cpp:253
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:291

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

37 {
38 reg.resize(round_up<size_t>(size, 8));
39 signedness = s;
40 }
Botan::MemoryRegion::resize
void resize(size_t n)
Definition: secmem.h:211
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 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:253
Botan::BigInt::set_bit
void set_bit(size_t n)
Definition: bigint.cpp:205
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:291

## Member Function Documentation

## ◆ abs()

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

Returns
  absolute (positive) value of this

Definition at line 330 of file bigint.cpp.

References Positive, and set_sign().

Referenced by Botan::operator*().

331 {
332 BigInt x = (*this);
333 x.set_sign(Positive);
334 return x;
335 }
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:211
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 350 of file bigint.cpp.

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

Referenced by binary_decode().

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

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

372 {
373 binary_decode(buf, buf.size());
374 }
Botan::BigInt::binary_decode
void binary_decode(const byte buf[], size_t length)
Definition: bigint.cpp:350
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 340 of file bigint.cpp.

References byte_at(), and bytes().

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

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

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

Referenced by 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::RSA_Private_Operation::RSA_Private_Operation(), Botan::srp6_group_identifier(), and to_u32bit().

254 {
255 const size_t words = sig_words();
256
257 if(words == 0)
258 return 0;
259
260 size_t full_words = words - 1, top_bits = MP_WORD_BITS;
261 word top_word = word_at(full_words), mask = MP_WORD_TOP_BIT;
262
263 while(top_bits && ((top_word & mask) == 0))
264 { mask >>= 1; top_bits--; }
265
266 return (full_words * MP_WORD_BITS + top_bits);
267 }
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 147 of file bigint.cpp.

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

148 {
149 const size_t WORD_BYTES = sizeof(word);
150 size_t word_num = n / WORD_BYTES, byte_num = n % WORD_BYTES;
151 if(word_num >= size())
152 return 0;
153 else
154 return get_byte(WORD_BYTES - byte_num - 1, reg[word_num]);
155 }
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 245 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::RSA_Private_Operation::sign(), Botan::DSA_Verification_Operation::verify(), Botan::ECDSA_Verification_Operation::verify(), and Botan::NR_Verification_Operation::verify_mr().

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

## ◆ 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:415

## ◆ clear_bit()

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

Clear bit at specified position

Parameters

  n | bit position to clear

Definition at line 216 of file bigint.cpp.

217 {
218 const size_t which = n / MP_WORD_BITS;
219 const word mask = static_cast<word>(1) << (n % MP_WORD_BITS);
220 if(which < size())
221 reg[which] &= ~mask;
222 }
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 132 of file bigint.cpp.

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

133 {
134 if(check_signs)
135 {
136 if(n.is_positive() && this->is_negative()) return -1;
137 if(n.is_negative() && this->is_positive()) return 1;
138 if(n.is_negative() && this->is_negative())
139 return (-bigint_cmp(data(), sig_words(), n.data(), n.sig_words()));
140 }
141 return bigint_cmp(data(), sig_words(), n.data(), n.sig_words());
142 }
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

## ◆ 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>>(), Botan::PointGFp::PointGFp(), 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::OSSL_BN::to_bytes(), Botan::GMP_MPZ::to_bytes(), Botan::NR_Verification_Operation::verify_mr(), Botan::RSA_Public_Operation::verify_mr(), and Botan::X509_Certificate::X509_Certificate().

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:245
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 272 of file bigint.cpp.

273 {
274 static const double LOG_2_BASE_10 = 0.30102999566;
275
276 if(base == Binary)
277 return bytes();
278 else if(base == Hexadecimal)
279 return 2*bytes();
280 else if(base == Octal)
281 return ((bits() + 2) / 3);
282 else if(base == Decimal)
283 return static_cast<size_t>((bits() * LOG_2_BASE_10) + 1);
284 else
285 throw Invalid_Argument("Unknown base for BigInt encoding");
286 }
Botan::BigInt::Hexadecimal
Definition: bigint.h:28
Botan::BigInt::Decimal
Definition: bigint.h:28
Botan::BigInt::bits
size_t bits() const
Definition: bigint.cpp:253
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:245
Botan::BigInt::Binary
Definition: bigint.h:28

## ◆ flip_sign()

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

Flip the sign of this BigInt

Definition at line 302 of file bigint.cpp.

References reverse_sign(), and set_sign().

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

303 {
304 set_sign(reverse_sign());
305 }
Botan::BigInt::reverse_sign
Sign reverse_sign() const
Definition: bigint.cpp:310
Botan::BigInt::set_sign
void set_sign(Sign sign)
Definition: bigint.cpp:291

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

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

161 {
162 return ((word_at(n / MP_WORD_BITS) >> (n % MP_WORD_BITS)) & 1);
163 }
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::PointGFp::PointGFp(), 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 168 of file bigint.cpp.

References byte_at().

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

169 {
170 if(length > 32)
171 throw Invalid_Argument("BigInt::get_substring: Substring size too big");
172
173 u64bit piece = 0;
174 for(size_t i = 0; i != 8; ++i)
175 {
176 const byte part = byte_at((offset / 8) + (7-i));
177 piece = (piece << 8) | part;
178 }
179
180 const u64bit mask = (static_cast<u64bit>(1) << length) - 1;
181 const size_t shift = (offset % 8);
182
183 return static_cast<u32bit>((piece >> shift) & mask);
184 }
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:147
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 115 of file bigint.cpp.

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

## ◆ grow_to()

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

Definition at line 123 of file bigint.cpp.

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

124 {
125 if(n > size())
126 reg.resize(round_up<size_t>(n, 8));
127 }
Botan::MemoryRegion::resize
void resize(size_t n)
Definition: secmem.h:211
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:160

## ◆ 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:160

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

References Botan::to_u32bit().

Referenced by Botan::divide(), Botan::gcd(), Botan::mul_add(), Botan::operator*(), operator>>=(), Botan::PointGFp::PointGFp(), 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 227 of file bigint.cpp.

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

228 {
229 if(n == 0) { clear(); return; }
230 if(n >= bits()) return;
231
232 const size_t top_word = n / MP_WORD_BITS;
233 const word mask = (static_cast<word>(1) << (n % MP_WORD_BITS)) - 1;
234
235 if(top_word < size())
236 for(size_t i = top_word + 1; i != size(); ++i)
237 reg[i] = 0;
238
239 reg[top_word] &= mask;
240 }
Botan::BigInt::bits
size_t bits() const
Definition: bigint.cpp:253
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:123
Botan::BigInt::set_sign
void set_sign(Sign sign)
Definition: bigint.cpp:291
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::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:123
Botan::BigInt::set_sign
void set_sign(Sign sign)
Definition: bigint.cpp:291

## ◆ 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::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:123
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:291
Botan::zeroise
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:415
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 320 of file bigint.cpp.

References flip_sign().

321 {
322 BigInt x = (*this);
323 x.flip_sign();
324 return x;
325 }
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.

References Botan::operator-().

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::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:123
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:291
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:123
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:291
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:350
Botan::BigInt::clear
void clear()
Definition: bigint.h:143
Botan::BigInt::set_sign
void set_sign(Sign sign)
Definition: bigint.cpp:291

## ◆ reverse_sign()

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

Returns
  the opposite sign of the represented integer value

Definition at line 310 of file bigint.cpp.

References Negative, Positive, and sign().

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

311 {
312 if(sign() == Positive)
313 return Negative;
314 return Positive;
315 }
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 205 of file bigint.cpp.

Referenced by BigInt().

206 {
207 const size_t which = n / MP_WORD_BITS;
208 const word mask = static_cast<word>(1) << (n % MP_WORD_BITS);
209 if(which >= size()) grow_to(which + 1);
210 reg[which] |= mask;
211 }
Botan::BigInt::size
size_t size() const
Definition: bigint.h:284
Botan::BigInt::grow_to
void grow_to(size_t n)
Definition: bigint.cpp:123
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 291 of file bigint.cpp.

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

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

## ◆ 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::divide(), 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>>=(), Botan::PointGFp::PointGFp(), 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.

References Botan::abs().

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*=(), Botan::PointGFp::PointGFp(), 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 106 of file bigint.cpp.

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

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

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

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

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

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