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

#include <bigint.h>

Classes

struct  DivideByZero
 

Public Types

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

Public Member Functions

BigInt abs () const
 
void assign (const word x[], size_t length)
 
 BigInt ()
 
 BigInt (u64bit n)
 
 BigInt (const BigInt &other)
 
 BigInt (const std::string &str)
 
 BigInt (const byte buf[], size_t length, Base base=Binary)
 
 BigInt (RandomNumberGenerator &rng, size_t bits)
 
 BigInt (Sign sign, size_t n)
 
 BigInt (NumberType type, size_t n)
 
void binary_decode (const byte buf[], size_t length)
 
void binary_decode (const MemoryRegion< byte > &buf)
 
void binary_encode (byte buf[]) const
 
size_t bits () const
 
byte byte_at (size_t n) const
 
size_t bytes () const
 
void clear ()
 
void clear_bit (size_t n)
 
s32bit cmp (const BigInt &n, bool check_signs=true) const
 
const word * data () const
 
size_t encoded_size (Base base=Binary) const
 
void flip_sign ()
 
bool get_bit (size_t n) const
 
SecureVector< word > & get_reg ()
 
const SecureVector< word > & get_reg () const
 
u32bit get_substring (size_t offset, size_t length) const
 
void grow_reg (size_t n)
 
void grow_to (size_t n)
 
bool is_even () const
 
bool is_negative () const
 
bool is_nonzero () const
 
bool is_odd () const
 
bool is_positive () const
 
bool is_zero () const
 
void mask_bits (size_t n)
 
bool operator! () const
 
BigIntoperator%= (const BigInt &y)
 
word operator%= (word y)
 
BigIntoperator*= (const BigInt &y)
 
BigIntoperator++ ()
 
BigInt operator++ (int)
 
BigIntoperator+= (const BigInt &y)
 
BigInt operator- () const
 
BigIntoperator-- ()
 
BigInt operator-- (int)
 
BigIntoperator-= (const BigInt &y)
 
BigIntoperator/= (const BigInt &y)
 
BigIntoperator<<= (size_t shift)
 
BigIntoperator>>= (size_t shift)
 
word & operator[] (size_t i)
 
const word & operator[] (size_t i) const
 
void randomize (RandomNumberGenerator &rng, size_t bitsize=0)
 
Sign reverse_sign () const
 
void set_bit (size_t n)
 
void set_sign (Sign sign)
 
void shrink_to_fit ()
 
size_t sig_words () const
 
Sign sign () const
 
size_t size () const
 
void swap (BigInt &other)
 
u32bit to_u32bit () const
 
word word_at (size_t n) const
 

Static Public Member Functions

static void const_time_lookup (SecureVector< word > &output, const std::vector< BigInt > &vec, size_t idx)
 
static BigInt decode (const byte buf[], size_t length, Base base=Binary)
 
static BigInt decode (const MemoryRegion< byte > &buf, Base base=Binary)
 
static SecureVector< byteencode (const BigInt &n, Base base=Binary)
 
static void encode (byte buf[], const BigInt &n, Base base=Binary)
 
static SecureVector< byteencode_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

Base enumerator for encoding and decoding

Enumerator
Octal 
Decimal 
Hexadecimal 
Binary 

Definition at line 28 of file bigint.h.

◆ NumberType

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

Enumerator
Power2 

Definition at line 38 of file bigint.h.

◆ Sign

Sign symbol definitions for positive and negative numbers

Enumerator
Negative 
Positive 

Definition at line 33 of file bigint.h.

Constructor & Destructor Documentation

◆ BigInt() [1/8]

Botan::BigInt::BigInt ( )
inline

Create empty BigInt

Definition at line 446 of file bigint.h.

446 { signedness = Positive; }

◆ BigInt() [2/8]

Botan::BigInt::BigInt ( u64bit  n)

Create BigInt from 64 bit integer

Parameters
ninitial value of this BigInt

Definition at line 20 of file bigint.cpp.

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

◆ BigInt() [3/8]

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

Copy Constructor

Parameters
otherthe BigInt to copy

Definition at line 46 of file bigint.cpp.

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

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

◆ BigInt() [4/8]

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

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

Parameters
strthe string to parse for an integer value

Definition at line 66 of file bigint.cpp.

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

◆ BigInt() [5/8]

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

Create a BigInt from an integer in a byte array

Parameters
bufthe byte array holding the value
lengthsize of buf
baseis the number base of the integer in buf

Definition at line 89 of file bigint.cpp.

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

◆ BigInt() [6/8]

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

Create a random BigInt of the specified size

Parameters
rngrandom number generator
bitssize in bits

Definition at line 98 of file bigint.cpp.

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

◆ BigInt() [7/8]

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

Create BigInt of specified size, all zeros

Parameters
signthe sign
nsize of the internal register in words

Definition at line 37 of file bigint.cpp.

38  {
39  reg.resize(round_up<size_t>(size, 8));
40  signedness = s;
41  }
void resize(size_t n)
Definition: secmem.h:217
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
typethe type of number to create. For Power2, will create the integer 2^n
na size/length parameter, interpretation depends upon the value of type

Definition at line 16 of file big_rand.cpp.

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

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

Member Function Documentation

◆ abs()

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

Definition at line 331 of file bigint.cpp.

References Positive, and set_sign().

Referenced by Botan::operator*().

332  {
333  BigInt x = (*this);
334  x.set_sign(Positive);
335  return x;
336  }

◆ 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  }
void resize(size_t n)
Definition: secmem.h:217
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
bufbyte array buffer containing the integer
lengthsize of buf

Definition at line 351 of file bigint.cpp.

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

Referenced by binary_decode().

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

Definition at line 372 of file bigint.cpp.

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

373  {
374  binary_decode(buf, buf.size());
375  }
void binary_decode(const byte buf[], size_t length)
Definition: bigint.cpp:351
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
bufdestination byte array for the integer value

Definition at line 341 of file bigint.cpp.

References byte_at(), and bytes().

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

◆ bits()

size_t Botan::BigInt::bits ( ) const

Get the bit length of the integer

Returns
bit length of the represented integer value

Definition at line 254 of file bigint.cpp.

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

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

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

◆ byte_at()

byte Botan::BigInt::byte_at ( size_t  n) const
Parameters
nthe offset to get a byte from
Returns
byte at offset n

Definition at line 148 of file bigint.cpp.

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

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

◆ bytes()

size_t Botan::BigInt::bytes ( ) const

◆ 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); }
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:428

◆ clear_bit()

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

Clear bit at specified position

Parameters
nbit position to clear

Definition at line 217 of file bigint.cpp.

218  {
219  const size_t which = n / MP_WORD_BITS;
220  const word mask = static_cast<word>(1) << (n % MP_WORD_BITS);
221  if(which < size())
222  reg[which] &= ~mask;
223  }
size_t size() const
Definition: bigint.h:284
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
nthe BigInt value to compare with
check_signsinclude sign in comparison?
Returns
if (this<n) return -1, if (this>n) return 1, if both values are identical return 0 [like Perl's <=> operator]

Definition at line 133 of file bigint.cpp.

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

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

◆ const_time_lookup()

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

Definition at line 382 of file bigint.cpp.

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

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

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

◆ data()

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

Return a pointer to the big integer word register

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

Definition at line 317 of file bigint.h.

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

317 { return &reg[0]; }

◆ decode() [1/2]

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

Create a BigInt from an integer in a byte array

Parameters
bufthe binary value to load
lengthsize of buf
basenumber-base of the integer in buf
Returns
BigInt representing the integer in the byte array

Definition at line 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  }
size_t hex_decode(byte output[], const char input[], size_t input_length, size_t &input_consumed, bool ignore_ws)
Definition: hex.cpp:55
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
unsigned char byte
Definition: types.h:22
byte char2digit(char c)
Definition: charset.cpp:149
bool is_digit(char c)
Definition: charset.cpp:128
bool is_space(char c)
Definition: charset.cpp:139

◆ 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
bufthe binary value to load
basenumber-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  }
size_t size() const
Definition: secmem.h:29
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
nthe BigInt to use as integer source
basenumber-base of resulting byte array representation
Returns
SecureVector of bytes containing the integer with given base

Definition at line 64 of file big_code.cpp.

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

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

◆ 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
bufdestination byte array for the encoded integer value with given base
nthe BigInt to use as integer source
basenumber-base of resulting byte array representation

Definition at line 18 of file big_code.cpp.

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  }
void divide(const BigInt &x, const BigInt &y_arg, BigInt &q, BigInt &r)
Definition: divide.cpp:34
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
char digit2char(byte b)
Definition: charset.cpp:171
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
nthe BigInt to encode
bytesthe 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  }
static SecureVector< byte > encode(const BigInt &n, Base base=Binary)
Definition: big_code.cpp:64
size_t bytes() const
Definition: bigint.cpp:246

◆ encoded_size()

size_t Botan::BigInt::encoded_size ( Base  base = Binary) const
Parameters
basethe base to measure the size for
Returns
size of this integer in base base

Definition at line 273 of file bigint.cpp.

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

◆ flip_sign()

void Botan::BigInt::flip_sign ( )

Flip the sign of this BigInt

Definition at line 303 of file bigint.cpp.

References reverse_sign(), and set_sign().

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

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

◆ get_bit()

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

Return bit value at specified position

Parameters
nthe bit offset to test
Returns
true, if the bit at position n is set, false otherwise

Definition at line 161 of file bigint.cpp.

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

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

◆ get_reg() [1/2]

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

return a reference to the internal register containing the value

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

Definition at line 325 of file bigint.h.

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

325 { return reg; }

◆ get_reg() [2/2]

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

return a const reference to the internal register containing the value

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

Definition at line 332 of file bigint.h.

332 { return reg; }

◆ get_substring()

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

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

Parameters
offsetthe offset to start extracting
lengthamount of bits to extract (starting at offset)
Returns
the integer extracted from the register starting at offset with specified length

Definition at line 169 of file bigint.cpp.

References byte_at().

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

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

Definition at line 116 of file bigint.cpp.

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

◆ grow_to()

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

Definition at line 124 of file bigint.cpp.

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

125  {
126  if(n > size())
127  reg.resize(round_up<size_t>(n, 8));
128  }
void resize(size_t n)
Definition: secmem.h:217
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); }
bool get_bit(size_t n) const
Definition: bigint.cpp:161

◆ is_negative()

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

Tests if the sign of the integer is negative

Returns
true, iff the integer has a negative sign

Definition at line 245 of file bigint.h.

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

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

◆ 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()); }
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); }
bool get_bit(size_t n) const
Definition: bigint.cpp:161

◆ is_positive()

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

Tests if the sign of the integer is positive

Returns
true, iff the integer has a positive sign

Definition at line 251 of file bigint.h.

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

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

◆ is_zero()

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

Test if the integer is zero

Returns
true if the integer is zero, false otherwise

Definition at line 176 of file bigint.h.

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

177  {
178  const size_t sw = sig_words();
179 
180  for(size_t i = 0; i != sw; ++i)
181  if(reg[i])
182  return false;
183  return true;
184  }
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
namount of bits to keep

Definition at line 228 of file bigint.cpp.

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

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

◆ operator%=() [1/2]

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

Modulo operator

Parameters
ythe 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
ythe 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 
179 
180  return word_at(0);
181  }
Sign sign() const
Definition: bigint.h:257
SecureVector< word > & get_reg()
Definition: bigint.h:325
word word_at(size_t n) const
Definition: bigint.h:238
size_t sig_words() const
Definition: bigint.h:290
void clear()
Definition: bigint.h:143
bool power_of_2(T arg)
Definition: bit_ops.h:21
void grow_to(size_t n)
Definition: bigint.cpp:124
void set_sign(Sign sign)
Definition: bigint.cpp:292
word bigint_modop(word n1, word n0, word d)
Definition: mp_misc.cpp:92

◆ operator*=()

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

*= operator

Parameters
ythe 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();
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  }
void bigint_linmul2(word x[], size_t x_size, word y)
Definition: mp_asm.cpp:220
Sign sign() const
Definition: bigint.h:257
SecureVector< word > & get_reg()
Definition: bigint.h:325
word word_at(size_t n) const
Definition: bigint.h:238
const word * data() const
Definition: bigint.h:317
void bigint_linmul3(word z[], const word x[], size_t x_size, word y)
Definition: mp_asm.cpp:238
size_t size() const
Definition: bigint.h:284
size_t sig_words() const
Definition: bigint.h:290
void clear()
Definition: bigint.h:143
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
void grow_to(size_t n)
Definition: bigint.cpp:124
void set_sign(Sign sign)
Definition: bigint.cpp:292

◆ operator++() [1/2]

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

Increment operator

Definition at line 97 of file bigint.h.

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

◆ operator++() [2/2]

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

Postfix increment operator

Definition at line 107 of file bigint.h.

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

◆ operator+=()

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

+= operator

Parameters
ythe BigInt to add to this

Definition at line 18 of file big_ops2.cpp.

References Botan::bigint_add2(), Botan::bigint_cmp(), Botan::bigint_sub2(), Botan::bigint_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);
42  }
43  else if(relative_size > 0)
44  bigint_sub2(get_reg(), x_sw, y.data(), y_sw);
45  }
46 
47  return (*this);
48  }
word bigint_sub2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_asm.cpp:158
Sign sign() const
Definition: bigint.h:257
SecureVector< word > & get_reg()
Definition: bigint.h:325
word bigint_sub3(word z[], const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_asm.cpp:198
const word * data() const
Definition: bigint.h:317
signed int s32bit
Definition: types.h:37
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:22
size_t sig_words() const
Definition: bigint.h:290
T max(T a, T b)
Definition: ct_utils.h:120
void grow_to(size_t n)
Definition: bigint.cpp:124
void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_asm.cpp:139
void set_sign(Sign sign)
Definition: bigint.cpp:292
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:428
s32bit bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_misc.cpp:41

◆ operator-()

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

Unary negation operator

Returns
negative this

Definition at line 321 of file bigint.cpp.

References flip_sign().

322  {
323  BigInt x = (*this);
324  x.flip_sign();
325  return x;
326  }

◆ operator--() [1/2]

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

Decrement operator

Definition at line 102 of file bigint.h.

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

◆ operator--() [2/2]

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

Postfix decrement operator

Definition at line 112 of file bigint.h.

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

◆ operator-=()

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

-= operator

Parameters
ythe 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();
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  }
void bigint_sub2_rev(word x[], const word y[], size_t y_size)
Definition: mp_asm.cpp:179
word bigint_sub2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_asm.cpp:158
Sign sign() const
Definition: bigint.h:257
SecureVector< word > & get_reg()
Definition: bigint.h:325
const word * data() const
Definition: bigint.h:317
signed int s32bit
Definition: types.h:37
size_t sig_words() const
Definition: bigint.h:290
T max(T a, T b)
Definition: ct_utils.h:120
void clear()
Definition: bigint.h:143
void bigint_shl1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition: mp_shift.cpp:18
void grow_to(size_t n)
Definition: bigint.cpp:124
void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_asm.cpp:139
void set_sign(Sign sign)
Definition: bigint.cpp:292
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
ythe 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  }
bool power_of_2(T arg)
Definition: bit_ops.h:21

◆ operator<<=()

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

Left shift operator

Parameters
shiftthe 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  }
SecureVector< word > & get_reg()
Definition: bigint.h:325
size_t sig_words() const
Definition: bigint.h:290
void bigint_shl1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition: mp_shift.cpp:18
void grow_to(size_t n)
Definition: bigint.cpp:124
const size_t MP_WORD_BITS
Definition: mp_core.h:18

◆ operator>>=()

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

Right shift operator

Parameters
shiftthe 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())
215  }
216 
217  return (*this);
218  }
void bigint_shr1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition: mp_shift.cpp:42
SecureVector< word > & get_reg()
Definition: bigint.h:325
bool is_zero() const
Definition: bigint.h:176
size_t sig_words() const
Definition: bigint.h:290
void set_sign(Sign sign)
Definition: bigint.cpp:292
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
ia 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
ia 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
rnga random number generator
minthe minimum value
maxthe 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  }
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
T max(T a, T b)
Definition: ct_utils.h:120
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
rngthe random number generator to use
bitsizenumber of bits the created random value should have

Definition at line 29 of file big_rand.cpp.

31  {
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  }
void binary_decode(const byte buf[], size_t length)
Definition: bigint.cpp:351
void clear()
Definition: bigint.h:143
void set_sign(Sign sign)
Definition: bigint.cpp:292

◆ reverse_sign()

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

Definition at line 311 of file bigint.cpp.

References Negative, Positive, and sign().

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

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

◆ set_bit()

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

Set bit at specified position

Parameters
nbit position to set

Definition at line 206 of file bigint.cpp.

Referenced by BigInt().

207  {
208  const size_t which = n / MP_WORD_BITS;
209  const word mask = static_cast<word>(1) << (n % MP_WORD_BITS);
210  if(which >= size()) grow_to(which + 1);
211  reg[which] |= mask;
212  }
size_t size() const
Definition: bigint.h:284
void grow_to(size_t n)
Definition: bigint.cpp:124
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
signnew Sign to set

Definition at line 292 of file bigint.cpp.

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

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

◆ shrink_to_fit()

void Botan::BigInt::shrink_to_fit ( )

Definition at line 377 of file bigint.cpp.

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

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

◆ sig_words()

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

Return how many words we need to hold this value

Returns
significant words of the represented integer value

Definition at line 290 of file bigint.h.

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

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

◆ sign()

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

Return the sign of the integer

Returns
the sign of the integer

Definition at line 257 of file bigint.h.

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

257 { return (signedness); }

◆ size()

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

Give size of internal register

Returns
size of internal register in words

Definition at line 284 of file bigint.h.

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

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

◆ swap()

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

Swap this value with another

Parameters
otherBigInt to swap values with

Definition at line 107 of file bigint.cpp.

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

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

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

◆ to_u32bit()

u32bit Botan::BigInt::to_u32bit ( ) const

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

Returns
the value as a u32bit if conversion is possible

Definition at line 190 of file bigint.cpp.

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

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

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