Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

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 { Decimal = 10, Hexadecimal = 16, Binary = 256 }
enum | Sign { Negative = 0, Positive = 1 }

## Public Member Functions
BigInt | abs () const
| BigInt ()=default
| BigInt (uint64_t n)
| BigInt (const BigInt &other)
| BigInt (const std::string &str)
| BigInt (const uint8_t buf[], size_t length, Base base=Binary)
| BigInt (RandomNumberGenerator &rng, size_t bits, bool set_high_bit=true)
| Create a random BigInt of the specified size. More...
| BigInt (Sign sign, size_t n)
| BigInt (BigInt &&other)
void | binary_decode (const uint8_t buf[], size_t length)
void | binary_decode (const secure_vector< uint8_t > &buf)
void | binary_encode (uint8_t buf[]) const
size_t | bits () const
uint8_t | byte_at (size_t n) const
size_t | bytes () const
void | clear ()
void | clear_bit (size_t n)
int32_t | cmp (const BigInt &n, bool check_signs=true) const
const word * | data () const
size_t | encoded_size (Base base=Binary) const
void | flip_sign ()
bool | get_bit (size_t n) const
uint32_t | get_substring (size_t offset, size_t length) const
secure_vector< word > & | get_word_vector ()
const secure_vector< word > & | get_word_vector () const
void | grow_to (size_t n)
bool | is_even () const
bool | is_negative () const
bool | is_nonzero () const
bool | is_odd () const
bool | is_positive () const
bool | is_zero () const
void | mask_bits (size_t n)
word * | mutable_data ()
bool | operator! () const
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= (BigInt &&other)
BigInt & | operator= (const BigInt &)=default
BigInt & | operator>>= (size_t shift)
void | randomize (RandomNumberGenerator &rng, size_t bitsize, bool set_high_bit=true)
Sign | reverse_sign () const
void | set_bit (size_t n)
void | set_sign (Sign sign)
void | set_word_at (size_t i, word w)
size_t | sig_words () const
Sign | sign () const
size_t | size () const
void | swap (BigInt &other)
void | swap_reg (secure_vector< word > &reg)
uint32_t | to_u32bit () const
word | word_at (size_t n) const

## Static Public Member Functions
static BigInt | decode (const uint8_t buf[], size_t length, Base base=Binary)
static BigInt | decode (const secure_vector< uint8_t > &buf, Base base=Binary)
static BigInt | decode (const std::vector< uint8_t > &buf, Base base=Binary)
static std::vector< uint8_t > | encode (const BigInt &n, Base base=Binary)
static void | encode (uint8_t buf[], const BigInt &n, Base base=Binary)
static secure_vector< uint8_t > | encode_1363 (const BigInt &n, size_t bytes)
static void | encode_1363 (uint8_t out[], size_t bytes, const BigInt &n)
static secure_vector< uint8_t > | encode_fixed_length_int_pair (const BigInt &n1, const BigInt &n2, size_t bytes)
static secure_vector< uint8_t > | encode_locked (const BigInt &n, Base base=Binary)
static BigInt | power_of_2 (size_t n)
static BigInt | random_integer (RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)

## Detailed Description

Arbitrary precision integer

Definition at line 23 of file bigint.h.

## Member Enumeration Documentation

enum Botan::BigInt::Base

Base enumerator for encoding and decoding

Enumerator
Decimal |
Hexadecimal |
Binary |

Definition at line 29 of file bigint.h.

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

enum Botan::BigInt::Sign

Sign symbol definitions for positive and negative numbers

Enumerator
Negative |
Positive |

Definition at line 34 of file bigint.h.

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

## Constructor & Destructor Documentation

Botan::BigInt::BigInt | ( |  | ) |
| default

Create empty BigInt

Referenced by operator*=().

Botan::BigInt::BigInt | ( | uint64_t | n | ) |

Create BigInt from 64 bit integer

Parameters

  n | initial value of this BigInt

Definition at line 20 of file bigint.cpp.

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

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

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

Copy Constructor

Parameters

  other | the BigInt to copy

Definition at line 44 of file bigint.cpp.

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

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. Otherwise, it will be interpreted as a decimal number.

Parameters

  str | the string to parse for an integer value

Definition at line 53 of file bigint.cpp.

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

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

Botan::BigInt::BigInt | ( | const uint8_t | 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 82 of file bigint.cpp.

References decode().

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

Botan::BigInt::BigInt | ( | RandomNumberGenerator & | rng,
|  | size_t | bits,
|  | bool | set_high_bit = `true`
| ) |  |

Create a random BigInt of the specified size.

Parameters

  rng | random number generator
  bits | size in bits
  set_high_bit | if true, the highest bit is always set

See also
  randomize

Definition at line 90 of file bigint.cpp.

References randomize().

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

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

References Botan::round_up().

36 {
37 m_reg.resize(round_up(size, 8));
38 m_signedness = s;
39 }
Botan::BigInt::size
size_t size() const
Definition: bigint.h:387
Botan::round_up
size_t round_up(size_t n, size_t align_to)
Definition: rounding.h:22

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

Move constructor

Definition at line 97 of file bigint.h.

98 {
99 this->swap(other);
100 }
Botan::BigInt::swap
void swap(BigInt &other)
Definition: bigint.h:122

## Member Function Documentation

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

Returns
  absolute (positive) value of this

Definition at line 254 of file bigint.cpp.

References Positive, and set_sign().

Referenced by Botan::abs().

255 {
256 BigInt x = (*this);
257 x.set_sign(Positive);
258 return x;
259 }
Botan::BigInt::Positive
Definition: bigint.h:34
Botan::BigInt::BigInt
BigInt()=default

void Botan::BigInt::binary_decode | ( | const uint8_t | 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 280 of file bigint.cpp.

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

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

281 {
282 const size_t WORD_BYTES = sizeof(word);
283
284 clear();
285 m_reg.resize(round_up((length / WORD_BYTES) + 1, 8));
286
287 for(size_t i = 0; i != length / WORD_BYTES; ++i)
288 {
289 const size_t top = length - WORD_BYTES*i;
290 for(size_t j = WORD_BYTES; j > 0; --j)
291 m_reg[i] = (m_reg[i] << 8) | buf[top - j];
292 }
293
294 for(size_t i = 0; i != length % WORD_BYTES; ++i)
295 m_reg[length / WORD_BYTES] = (m_reg[length / WORD_BYTES] << 8) | buf[i];
296 }
Botan::BigInt::clear
void clear()
Definition: bigint.h:217
Botan::round_up
size_t round_up(size_t n, size_t align_to)
Definition: rounding.h:22

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

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

Parameters

  buf | the array to load from

Definition at line 466 of file bigint.h.

467 {
468 binary_decode(buf.data(), buf.size());
469 }
Botan::BigInt::binary_decode
void binary_decode(const uint8_t buf[], size_t length)
Definition: bigint.cpp:280

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

Store BigInt-value in a given byte array

Parameters

  buf | destination byte array for the integer value

Definition at line 270 of file bigint.cpp.

References byte_at(), and bytes().

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

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

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

Get the bit length of the integer

Returns
  bit length of the represented integer value

Definition at line 184 of file bigint.cpp.

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

Referenced by Botan::Blinded_Point_Multiply::blinded_multiply(), botan_mp_num_bits(), bytes(), Botan::ct_inverse_mod_odd_modulus(), Botan::BER_Decoder::decode(), Botan::BER_Decoder::decode_constrained_integer(), Botan::DH_PrivateKey::DH_PrivateKey(), Botan::DL_Group::DL_Group(), Botan::DER_Encoder::encode(), encoded_size(), Botan::Fixed_Window_Exponentiator::execute(), Botan::generate_dsa_primes(), Botan::is_prime(), Botan::RSA_PublicKey::key_length(), Botan::DL_Scheme_PublicKey::key_length(), Botan::EC_PublicKey::key_length(), Botan::TPM_PrivateKey::key_length(), Botan::multi_exponentiate(), Botan::operator*(), operator/=(), Botan::operator>>(), random_integer(), Botan::random_prime(), Botan::RSA_PrivateKey::RSA_PrivateKey(), Botan::Fixed_Window_Exponentiator::set_base(), Botan::Montgomery_Exponentiator::set_base(), Botan::Montgomery_Exponentiator::set_exponent(), Botan::srp6_group_identifier(), and to_u32bit().

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

uint8_t Botan::BigInt::byte_at | ( | size_t | n | ) | const
| inline

Parameters

  n | the offset to get a byte from

Returns
  byte at offset n

Definition at line 324 of file bigint.h.

References Botan::get_byte().

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

325 {
326 return get_byte(sizeof(word) - (n % sizeof(word)) - 1,
327 word_at(n / sizeof(word)));
328 }
Botan::BigInt::word_at
word word_at(size_t n) const
Definition: bigint.h:335
Botan::get_byte
uint8_t get_byte(size_t byte_num, T input)
Definition: loadstor.h:47

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

Give byte length of the integer

Returns
  byte length of the represented integer value

Definition at line 176 of file bigint.cpp.

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

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

177 {
178 return round_up(bits(), 8) / 8;
179 }
Botan::BigInt::bits
size_t bits() const
Definition: bigint.cpp:184
Botan::round_up
size_t round_up(size_t n, size_t align_to)
Definition: rounding.h:22

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

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

Definition at line 217 of file bigint.h.

References Botan::zeroise().

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

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

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

Clear bit at specified position

Parameters

  n | bit position to clear

Definition at line 168 of file bigint.cpp.

References Botan::MP_WORD_BITS, and size().

Referenced by botan_mp_clear_bit().

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

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

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

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

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

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

Return a const pointer to the register

Returns
  a pointer to the start of the internal register

Definition at line 425 of file bigint.h.

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

425 { return m_reg.data(); }

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

Create a BigInt from an integer in a byte array

Parameters

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

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

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

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

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

References decode().

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

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

References decode().

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

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

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

Parameters

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

Returns
  secure_vector of bytes containing the integer with given base

Definition at line 54 of file big_code.cpp.

References Binary, and encoded_size().

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

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

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

Encode the integer value from a BigInt to a byte array

Parameters

  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.

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

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

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

Encode a BigInt to a byte array according to IEEE 1363

Parameters

  n | the BigInt to encode
  bytes | the length of the resulting secure_vector<uint8_t>

Returns
  a secure_vector<uint8_t> containing the encoded BigInt

Definition at line 82 of file big_code.cpp.

Referenced by Botan::PK_Verifier::check_signature(), Botan::EC_Group::DER_encode(), Botan::EC2OSP(), encode_fixed_length_int_pair(), Botan::RFC6979_Nonce_Generator::nonce_for(), Botan::EC_PrivateKey::private_key_bits(), Botan::DH_PublicKey::public_value(), Botan::RFC6979_Nonce_Generator::RFC6979_Nonce_Generator(), Botan::srp6_client_agree(), and Botan::SRP6_Server_Session::step2().

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

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

Definition at line 90 of file big_code.cpp.

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

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

secure_vector< uint8_t > Botan::BigInt::encode_fixed_length_int_pair | ( | const BigInt & | n1,
|  | const BigInt & | n2,
|  | size_t | bytes
| ) |  |
| static

Encode two BigInt to a byte array according to IEEE 1363

Parameters

  n1 | the first BigInt to encode
  n2 | the second BigInt to encode
  bytes | the length of the encoding of each single BigInt

Returns
  a secure_vector<uint8_t> containing the concatenation of the two encoded BigInt

Definition at line 103 of file big_code.cpp.

References bytes(), and encode_1363().

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

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

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

Parameters

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

Returns
  secure_vector of bytes containing the integer with given base

Definition at line 68 of file big_code.cpp.

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

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

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

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

Referenced by encode(), and encode_locked().

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

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

Flip the sign of this BigInt

Definition at line 226 of file bigint.cpp.

References reverse_sign(), and set_sign().

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

227 {
228 set_sign(reverse_sign());
229 }
Botan::BigInt::reverse_sign
Sign reverse_sign() const
Definition: bigint.cpp:234
Botan::BigInt::set_sign
void set_sign(Sign sign)
Definition: bigint.cpp:215

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

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

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

300 {
301 return ((word_at(n / BOTAN_MP_WORD_BITS) >> (n % BOTAN_MP_WORD_BITS)) & 1);
302 }
Botan::BigInt::word_at
word word_at(size_t n) const
Definition: bigint.h:335

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

References byte_at().

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

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

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

Definition at line 427 of file bigint.h.

Referenced by Botan::ct_inverse_mod_odd_modulus().

427 { return m_reg; }

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

Definition at line 428 of file bigint.h.

428 { return m_reg; }

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

Increase internal register buffer to at least n words

Parameters

  n | new size of register

Definition at line 261 of file bigint.cpp.

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

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

262 {
263 if(n > size())
264 m_reg.resize(round_up(n, 8));
265 }
Botan::BigInt::size
size_t size() const
Definition: bigint.h:387
Botan::round_up
size_t round_up(size_t n, size_t align_to)
Definition: rounding.h:22

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

Referenced by Botan::almost_montgomery_inverse(), botan_mp_is_even(), Botan::RSA_PublicKey::check_key(), Botan::RSA_PrivateKey::check_key(), Botan::ct_inverse_mod_odd_modulus(), Botan::inverse_mod(), Botan::is_prime(), Botan::jacobi(), Botan::Montgomery_Exponentiator::Montgomery_Exponentiator(), and Botan::ressol().

232 { return (get_bit(0) == 0); }
Botan::BigInt::get_bit
bool get_bit(size_t n) const
Definition: bigint.h:299

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

Referenced by Botan::Blinded_Point_Multiply::blinded_multiply(), botan_mp_is_negative(), cmp(), Botan::ct_inverse_mod_odd_modulus(), Botan::divide(), Botan::inverse_mod(), Botan::jacobi(), Botan::mul_add(), Botan::mul_sub(), Botan::multi_exponentiate(), Botan::operator%(), Botan::operator*(), Botan::Modular_Reducer::reduce(), Botan::Power_Mod::set_base(), Botan::Power_Mod::set_exponent(), Botan::sub_mul(), and to_u32bit().

348 { return (sign() == Negative); }
Botan::BigInt::Negative
Definition: bigint.h:34
Botan::BigInt::sign
Sign sign() const
Definition: bigint.h:360

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

References Botan::CT::is_zero().

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

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

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

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

238 { return (get_bit(0) == 1); }
Botan::BigInt::get_bit
bool get_bit(size_t n) const
Definition: bigint.h:299

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

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

354 { return (sign() == Positive); }
Botan::BigInt::Positive
Definition: bigint.h:34
Botan::BigInt::sign
Sign sign() const
Definition: bigint.h:360

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

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

251 {
252 const size_t sw = sig_words();
253
254 for(size_t i = 0; i != sw; ++i)
255 if(m_reg[i])
256 return false;
257 return true;
258 }
Botan::BigInt::sig_words
size_t sig_words() const
Definition: bigint.h:393

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

Clear all but the lowest n bits

Parameters

  n | amount of bits to keep

Definition at line 276 of file bigint.h.

References Botan::clear_mem().

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

277 {
278 if(n == 0) { clear(); return; }
279
280 const size_t top_word = n / BOTAN_MP_WORD_BITS;
281 const word mask = (static_cast<word>(1) << (n % BOTAN_MP_WORD_BITS)) - 1;
282
283 if(top_word < size())
284 {
285 const size_t len = size() - (top_word + 1);
286 if (len > 0)
287 {
288 clear_mem(&m_reg[top_word+1], len);
289 }
290 m_reg[top_word] &= mask;
291 }
292 }
Botan::clear_mem
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:57
Botan::BigInt::size
size_t size() const
Definition: bigint.h:387
Botan::BigInt::clear
void clear()
Definition: bigint.h:217

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

Return a mutable pointer to the register

Returns
  a pointer to the start of the internal register

Definition at line 419 of file bigint.h.

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

419 { return m_reg.data(); }

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

! operator

Returns
  true iff this is zero, otherwise false

Definition at line 211 of file bigint.h.

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

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

Modulo operator

Parameters

  y | the modulus to reduce this by

Definition at line 143 of file big_ops2.cpp.

144 {
145 return (*this = (*this) % mod);
146 }

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

Modulo operator

Parameters

  y | the modulus (word) to reduce this by

Definition at line 151 of file big_ops2.cpp.

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

152 {
153 if(mod == 0)
154 throw BigInt::DivideByZero();
155
156 if(is_power_of_2(mod))
157 {
158 word result = (word_at(0) & (mod - 1));
159 clear();
160 grow_to(2);
161 m_reg[0] = result;
162 return result;
163 }
164
165 word remainder = 0;
166
167 for(size_t j = sig_words(); j > 0; --j)
168 remainder = bigint_modop(remainder, word_at(j-1), mod);
169 clear();
170 grow_to(2);
171
172 if(remainder && sign() == BigInt::Negative)
173 m_reg[0] = mod - remainder;
174 else
175 m_reg[0] = remainder;
176
177 set_sign(BigInt::Positive);
178
179 return word_at(0);
180 }
Botan::BigInt::word_at
word word_at(size_t n) const
Definition: bigint.h:335
Botan::BigInt::sig_words
size_t sig_words() const
Definition: bigint.h:393
Botan::BigInt::Positive
Definition: bigint.h:34
Botan::BigInt::Negative
Definition: bigint.h:34
Botan::BigInt::clear
void clear()
Definition: bigint.h:217
Botan::BigInt::grow_to
void grow_to(size_t n)
Definition: bigint.cpp:261
Botan::BigInt::set_sign
void set_sign(Sign sign)
Definition: bigint.cpp:215
Botan::bigint_modop
word bigint_modop(word n1, word n0, word d)
Definition: mp_core.cpp:432
Botan::is_power_of_2
bool is_power_of_2(T arg)
Definition: bit_ops.h:25
Botan::BigInt::sign
Sign sign() const
Definition: bigint.h:360

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

*= operator

Parameters

  y | the BigInt to multiply with this

Definition at line 96 of file big_ops2.cpp.

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

97 {
98 const size_t x_sw = sig_words(), y_sw = y.sig_words();
99 set_sign((sign() == y.sign()) ? Positive : Negative);
100
101 if(x_sw == 0 || y_sw == 0)
102 {
103 clear();
104 set_sign(Positive);
105 }
106 else if(x_sw == 1 && y_sw)
107 {
108 grow_to(y_sw + 2);
109 bigint_linmul3(mutable_data(), y.data(), y_sw, word_at(0));
110 }
111 else if(y_sw == 1 && x_sw)
112 {
113 grow_to(x_sw + 2);
114 bigint_linmul2(mutable_data(), x_sw, y.word_at(0));
115 }
116 else
117 {
118 grow_to(size() + y.size());
119
120 secure_vector<word> z(data(), data() + x_sw);
121 secure_vector<word> workspace(size());
122 bigint_mul(*this, BigInt(*this), y, workspace.data());
123 }
124
125 return (*this);
126 }
Botan::BigInt::word_at
word word_at(size_t n) const
Definition: bigint.h:335
Botan::BigInt::sig_words
size_t sig_words() const
Definition: bigint.h:393
Botan::BigInt::Positive
Definition: bigint.h:34
Botan::bigint_linmul2
void bigint_linmul2(word x[], size_t x_size, word y)
Definition: mp_core.cpp:222
Botan::BigInt::mutable_data
word * mutable_data()
Definition: bigint.h:419
Botan::BigInt::size
size_t size() const
Definition: bigint.h:387
Botan::bigint_linmul3
void bigint_linmul3(word z[], const word x[], size_t x_size, word y)
Definition: mp_core.cpp:240
Botan::BigInt::Negative
Definition: bigint.h:34
Botan::BigInt::data
const word * data() const
Definition: bigint.h:425
Botan::BigInt::clear
void clear()
Definition: bigint.h:217
Botan::BigInt::grow_to
void grow_to(size_t n)
Definition: bigint.cpp:261
Botan::bigint_mul
void bigint_mul(BigInt &z, const BigInt &x, const BigInt &y, word workspace[])
Definition: mp_karat.cpp:252
Botan::BigInt::BigInt
BigInt()=default
Botan::BigInt::set_sign
void set_sign(Sign sign)
Definition: bigint.cpp:215
Botan::BigInt::sign
Sign sign() const
Definition: bigint.h:360

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

Increment operator

Definition at line 184 of file bigint.h.

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

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

Postfix increment operator

Definition at line 194 of file bigint.h.

194 { BigInt x = (*this); ++(*this); return x; }
Botan::BigInt::BigInt
BigInt()=default

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

+= operator

Parameters

  y | the BigInt to add to this

Definition at line 19 of file big_ops2.cpp.

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

20 {
21 const size_t x_sw = sig_words(), y_sw = y.sig_words();
22
23 const size_t reg_size = std::max(x_sw, y_sw) + 1;
24 grow_to(reg_size);
25
26 if(sign() == y.sign())
27 bigint_add2(mutable_data(), reg_size - 1, y.data(), y_sw);
28 else
29 {
30 int32_t relative_size = bigint_cmp(data(), x_sw, y.data(), y_sw);
31
32 if(relative_size < 0)
33 {
34 secure_vector<word> z(reg_size - 1);
35 bigint_sub3(z.data(), y.data(), reg_size - 1, data(), x_sw);
36 std::swap(m_reg, z);
37 set_sign(y.sign());
38 }
39 else if(relative_size == 0)
40 {
41 zeroise(m_reg);
42 set_sign(Positive);
43 }
44 else if(relative_size > 0)
45 bigint_sub2(mutable_data(), x_sw, y.data(), y_sw);
46 }
47
48 return (*this);
49 }
Botan::BigInt::sig_words
size_t sig_words() const
Definition: bigint.h:393
Botan::BigInt::Positive
Definition: bigint.h:34
Botan::bigint_cmp
int32_t bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:378
Botan::bigint_sub2
word bigint_sub2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:157
Botan::BigInt::mutable_data
word * mutable_data()
Definition: bigint.h:419
Botan::bigint_sub3
word bigint_sub3(word z[], const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:198
Botan::BigInt::data
const word * data() const
Definition: bigint.h:425
Botan::CT::max
T max(T a, T b)
Definition: ct_utils.h:173
Botan::BigInt::grow_to
void grow_to(size_t n)
Definition: bigint.cpp:261
Botan::bigint_add2
void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:138
Botan::BigInt::set_sign
void set_sign(Sign sign)
Definition: bigint.cpp:215
Botan::zeroise
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:211
Botan::BigInt::sign
Sign sign() const
Definition: bigint.h:360

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

Unary negation operator

Returns
  negative this

Definition at line 244 of file bigint.cpp.

References flip_sign().

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

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

Decrement operator

Definition at line 189 of file bigint.h.

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

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

Postfix decrement operator

Definition at line 199 of file bigint.h.

199 { BigInt x = (*this); --(*this); return x; }
Botan::BigInt::BigInt
BigInt()=default

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

-= operator

Parameters

  y | the BigInt to subtract from this

Definition at line 54 of file big_ops2.cpp.

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

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

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

/= operator

Parameters

  y | the BigInt to divide this by

Definition at line 131 of file big_ops2.cpp.

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

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

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

Left shift operator

Parameters

  shift | the number of bits to shift this left by

Definition at line 185 of file big_ops2.cpp.

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

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

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

Move assignment

Definition at line 105 of file bigint.h.

106 {
107 if(this != &other)
108 this->swap(other);
109
110 return (*this);
111 }
Botan::BigInt::swap
void swap(BigInt &other)
Definition: bigint.h:122

BigInt& Botan::BigInt::operator= | ( | const BigInt & |  | ) |
| default

Copy assignment

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

Right shift operator

Parameters

  shift | the number of bits to shift this right by

Definition at line 203 of file big_ops2.cpp.

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

204 {
205 if(shift)
206 {
207 const size_t shift_words = shift / MP_WORD_BITS,
208 shift_bits = shift % MP_WORD_BITS;
209
210 bigint_shr1(mutable_data(), sig_words(), shift_words, shift_bits);
211
212 if(is_zero())
213 set_sign(Positive);
214 }
215
216 return (*this);
217 }
Botan::bigint_shr1
void bigint_shr1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition: mp_core.cpp:281
Botan::BigInt::sig_words
size_t sig_words() const
Definition: bigint.h:393
Botan::BigInt::Positive
Definition: bigint.h:34
Botan::BigInt::mutable_data
word * mutable_data()
Definition: bigint.h:419
Botan::BigInt::is_zero
bool is_zero() const
Definition: bigint.h:250
Botan::BigInt::set_sign
void set_sign(Sign sign)
Definition: bigint.cpp:215
Botan::MP_WORD_BITS
const size_t MP_WORD_BITS
Definition: mp_core.h:21

static BigInt Botan::BigInt::power_of_2 | ( | size_t | n | ) |
| inline static

Create a power of two

Parameters

  n | the power of two to create

Returns
  bigint representing 2^n

Definition at line 492 of file bigint.h.

References set_bit().

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

493 {
494 BigInt b;
495 b.set_bit(n);
496 return b;
497 }
Botan::BigInt::BigInt
BigInt()=default

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 in [min,max)

Definition at line 45 of file big_rand.cpp.

References bits(), and randomize().

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

47 {
48 BigInt r;
49
50 const size_t bits = max.bits();
51
52 do
53 {
54 r.randomize(rng, bits, false);
55 }
56 while(r < min || r >= max);
57
58 return r;
59 }
Botan::BigInt::bits
size_t bits() const
Definition: bigint.cpp:184
Botan::CT::max
T max(T a, T b)
Definition: ct_utils.h:173
Botan::BigInt::BigInt
BigInt()=default

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

Fill BigInt with a random number with size of bitsize

If `set_high_bit` is true, the highest bit will be set, which causes the entropy to be bits-1. Otherwise the highest bit is randomly chosen by the rng, causing the entropy to be bits.

Parameters

  rng | the random number generator to use
  bitsize | number of bits the created random value should have
  set_high_bit | if true, the highest bit is always set

Definition at line 17 of file big_rand.cpp.

References binary_decode(), clear(), Positive, Botan::RandomNumberGenerator::random_vec(), Botan::round_up(), and set_sign().

Referenced by BigInt(), Botan::DH_PrivateKey::DH_PrivateKey(), Botan::DL_Group::DL_Group(), Botan::ElGamal_PrivateKey::ElGamal_PrivateKey(), random_integer(), and Botan::PointGFp::randomize_repr().

19 {
20 set_sign(Positive);
21
22 if(bitsize == 0)
23 {
24 clear();
25 }
26 else
27 {
28 secure_vector<uint8_t> array = rng.random_vec(round_up(bitsize, 8) / 8);
29
30 // Always cut unwanted bits
31 if(bitsize % 8)
32 array[0] &= 0xFF >> (8 - (bitsize % 8));
33
34 // Set the highest bit if wanted
35 if (set_high_bit)
36 array[0] |= 0x80 >> ((bitsize % 8) ? (8 - bitsize % 8) : 0);
37
38 binary_decode(array);
39 }
40 }
Botan::BigInt::Positive
Definition: bigint.h:34
Botan::BigInt::clear
void clear()
Definition: bigint.h:217
Botan::BigInt::binary_decode
void binary_decode(const uint8_t buf[], size_t length)
Definition: bigint.cpp:280
Botan::round_up
size_t round_up(size_t n, size_t align_to)
Definition: rounding.h:22
Botan::BigInt::set_sign
void set_sign(Sign sign)
Definition: bigint.cpp:215

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

Returns
  the opposite sign of the represented integer value

Definition at line 234 of file bigint.cpp.

References Negative, Positive, and sign().

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

235 {
236 if(sign() == Positive)
237 return Negative;
238 return Positive;
239 }
Botan::BigInt::Positive
Definition: bigint.h:34
Botan::BigInt::Negative
Definition: bigint.h:34
Botan::BigInt::sign
Sign sign() const
Definition: bigint.h:360

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

Set bit at specified position

Parameters

  n | bit position to set

Definition at line 157 of file bigint.cpp.

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

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

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

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

Set sign of the integer

Parameters

  sign | new Sign to set

Definition at line 215 of file bigint.cpp.

References is_zero(), and Positive.

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

216 {
217 if(is_zero())
218 m_signedness = Positive;
219 else
220 m_signedness = s;
221 }
Botan::BigInt::Positive
Definition: bigint.h:34
Botan::BigInt::is_zero
bool is_zero() const
Definition: bigint.h:250

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

Definition at line 338 of file bigint.h.

339 {
340 grow_to(i + 1);
341 m_reg[i] = w;
342 }
Botan::BigInt::grow_to
void grow_to(size_t n)
Definition: bigint.cpp:261

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

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

394 {
395 const word* x = m_reg.data();
396 size_t sig = m_reg.size();
397
398 while(sig && (x[sig-1] == 0))
399 sig--;
400 return sig;
401 }

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

Return the sign of the integer

Returns
  the sign of the integer

Definition at line 360 of file bigint.h.

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

360 { return (m_signedness); }

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

Give size of internal register

Returns
  size of internal register in words

Definition at line 387 of file bigint.h.

Referenced by Botan::bigint_monty_sqr(), Botan::bigint_mul(), clear_bit(), Botan::Montgomery_Exponentiator::execute(), grow_to(), Botan::low_zero_bits(), Botan::mul_add(), Botan::operator*(), operator*=(), Botan::Montgomery_Exponentiator::set_base(), set_bit(), and Botan::square().

387 { return m_reg.size(); }

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

Swap this value with another

Parameters

  other | BigInt to swap values with

Definition at line 122 of file bigint.h.

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

123 {
124 m_reg.swap(other.m_reg);
125 std::swap(m_signedness, other.m_signedness);
126 }

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

Definition at line 128 of file bigint.h.

129 {
130 m_reg.swap(reg);
131 }

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

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

Returns
  the value as a uint32_t if conversion is possible

Definition at line 141 of file bigint.cpp.

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

Referenced by botan_mp_to_uint32().

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

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

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

336 { return ((n < size()) ? m_reg[n] : 0); }
Botan::BigInt::size
size_t size() const
Definition: bigint.h:387

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

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