Botan  1.10.9
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)
 
size_t sig_words () const
 
Sign sign () const
 
size_t size () const
 
void swap (BigInt &other)
 
u32bit to_u32bit () const
 
word word_at (size_t n) const
 

Static Public Member Functions

static BigInt decode (const byte buf[], size_t length, Base base=Binary)
 
static BigInt decode (const MemoryRegion< byte > &buf, Base base=Binary)
 
static SecureVector< 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 enumerator for encoding and decoding

Enumerator
Octal 
Decimal 
Hexadecimal 
Binary 

Definition at line 28 of file bigint.h.

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

Enumerator
Power2 

Definition at line 38 of file bigint.h.

Sign symbol definitions for positive and negative numbers

Enumerator
Negative 
Positive 

Definition at line 33 of file bigint.h.

Constructor & Destructor Documentation

Botan::BigInt::BigInt ( )
inline

Create empty BigInt

Definition at line 446 of file bigint.h.

Referenced by random_integer().

446 { signedness = Positive; }
Botan::BigInt::BigInt ( u64bit  n)

Create BigInt from 64 bit integer

Parameters
ninitial value of this BigInt

Definition at line 19 of file bigint.cpp.

References Botan::MP_WORD_BITS, Botan::MP_WORD_MASK, Positive, Botan::MemoryRegion< T >::resize(), and set_sign().

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

Copy Constructor

Parameters
otherthe BigInt to copy

Definition at line 45 of file bigint.cpp.

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

46  {
47  const size_t b_words = b.sig_words();
48 
49  if(b_words)
50  {
51  reg.resize(round_up<size_t>(b_words, 8));
52  reg.copy(b.data(), b_words);
53  set_sign(b.sign());
54  }
55  else
56  {
57  reg.resize(2);
59  }
60  }
void resize(size_t n)
Definition: secmem.h:211
void copy(const T in[], size_t n)
Definition: secmem.h:120
void set_sign(Sign sign)
Definition: bigint.cpp:291
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 65 of file bigint.cpp.

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

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

References decode(), Positive, and set_sign().

89  {
91  *this = decode(input, length, base);
92  }
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:291
GMP_MPZ base
Definition: gmp_powm.cpp:29
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 97 of file bigint.cpp.

References Positive, randomize(), and set_sign().

98  {
100  randomize(rng, bits);
101  }
size_t bits() const
Definition: bigint.cpp:253
RandomNumberGenerator * rng
Definition: global_rng.cpp:165
void randomize(RandomNumberGenerator &rng, size_t bitsize=0)
Definition: big_rand.cpp:29
void set_sign(Sign sign)
Definition: bigint.cpp:291
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 36 of file bigint.cpp.

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

37  {
38  reg.resize(round_up<size_t>(size, 8));
39  signedness = s;
40  }
void resize(size_t n)
Definition: secmem.h:211
size_t size() const
Definition: bigint.h:284
size_t s
Definition: numthry.cpp:27
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 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  }
void set_bit(size_t n)
Definition: bigint.cpp:205
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
size_t bits() const
Definition: bigint.cpp:253
void set_sign(Sign sign)
Definition: bigint.cpp:291

Member Function Documentation

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

Definition at line 330 of file bigint.cpp.

References Positive, and set_sign().

Referenced by Botan::abs(), and Botan::operator*().

331  {
332  BigInt x = (*this);
333  x.set_sign(Positive);
334  return x;
335  }
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:211
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:22
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 350 of file bigint.cpp.

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

Referenced by binary_decode(), decode(), Botan::generate_dsa_primes(), and randomize().

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

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

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

References byte_at(), and bytes().

Referenced by encode(), Botan::ElGamal_Encryption_Operation::encrypt(), Botan::NR_Signature_Operation::sign(), Botan::DSA_Signature_Operation::sign(), Botan::ECDSA_Signature_Operation::sign(), Botan::GOST_3410_Signature_Operation::sign(), and Botan::GOST_3410_PublicKey::x509_subject_public_key().

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

Get the bit length of the integer

Returns
bit length of the represented integer value

Definition at line 253 of file bigint.cpp.

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

Referenced by bytes(), Botan::BER_Decoder::decode(), Botan::DH_KA_Operation::DH_KA_Operation(), Botan::DH_PrivateKey::DH_PrivateKey(), Botan::DL_Group::DL_Group(), Botan::ElGamal_Decryption_Operation::ElGamal_Decryption_Operation(), Botan::DER_Encoder::encode(), encoded_size(), Botan::ElGamal_Encryption_Operation::encrypt(), Botan::Fixed_Window_Exponentiator::execute(), Botan::generate_dsa_primes(), mask_bits(), Botan::multi_exponentiate(), Botan::operator*(), operator/=(), Botan::operator>>(), Botan::primality_test(), random_integer(), Botan::random_prime(), Botan::RSA_Private_Operation::RSA_Private_Operation(), Botan::RSA_PrivateKey::RSA_PrivateKey(), Botan::RW_PrivateKey::RW_PrivateKey(), Botan::Fixed_Window_Exponentiator::set_base(), Botan::Montgomery_Exponentiator::set_base(), Botan::Montgomery_Exponentiator::set_exponent(), Botan::NR_Signature_Operation::sign(), Botan::DSA_Signature_Operation::sign(), Botan::RW_Signature_Operation::sign(), Botan::ECDSA_Signature_Operation::sign(), Botan::GOST_3410_Signature_Operation::sign(), Botan::srp6_group_identifier(), and to_u32bit().

254  {
255  const size_t words = sig_words();
256 
257  if(words == 0)
258  return 0;
259 
260  size_t full_words = words - 1, top_bits = MP_WORD_BITS;
261  word top_word = word_at(full_words), mask = MP_WORD_TOP_BIT;
262 
263  while(top_bits && ((top_word & mask) == 0))
264  { mask >>= 1; top_bits--; }
265 
266  return (full_words * MP_WORD_BITS + top_bits);
267  }
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 Botan::BigInt::byte_at ( size_t  n) const
Parameters
nthe offset to get a byte from
Returns
byte at offset n

Definition at line 147 of file bigint.cpp.

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

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

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

Zeroize the BigInt

Definition at line 143 of file bigint.h.

References Botan::zeroise().

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

143 { zeroise(reg); }
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:415
void Botan::BigInt::clear_bit ( size_t  n)

Clear bit at specified position

Parameters
nbit position to clear

Definition at line 216 of file bigint.cpp.

References Botan::MP_WORD_BITS, and size().

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

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

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

133  {
134  if(check_signs)
135  {
136  if(n.is_positive() && this->is_negative()) return -1;
137  if(n.is_negative() && this->is_positive()) return 1;
138  if(n.is_negative() && this->is_negative())
139  return (-bigint_cmp(data(), sig_words(), n.data(), n.sig_words()));
140  }
141  return bigint_cmp(data(), sig_words(), n.data(), n.sig_words());
142  }
size_t sig_words() const
Definition: bigint.h:290
BigInt n
Definition: numthry.cpp:26
bool is_negative() const
Definition: bigint.h:245
const word * data() const
Definition: bigint.h:317
bool is_positive() const
Definition: bigint.h:251
s32bit bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_misc.cpp:41
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(), cmp(), Botan::Montgomery_Exponentiator::execute(), Botan::GMP_MPZ::GMP_MPZ(), Botan::mul_add(), Botan::operator*(), operator*=(), Botan::operator+(), operator+=(), Botan::operator-(), operator-=(), Botan::operator<<(), Botan::operator>>(), Botan::Montgomery_Exponentiator::set_base(), and Botan::square().

317 { return &reg[0]; }
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.

References Binary, binary_decode(), Botan::Charset::char2digit(), Decimal, Botan::hex_decode(), Hexadecimal, Botan::Charset::is_digit(), Botan::Charset::is_space(), Octal, r, and Botan::MemoryRegion< T >::size().

Referenced by Botan::DH_KA_Operation::agree(), BigInt(), decode(), Botan::decode_concatenation(), Botan::BER_Decoder::decode_octet_string_bigint(), Botan::CRL_Entry::encode_into(), Botan::OS2ECP(), Botan::Server_Key_Exchange::Server_Key_Exchange(), 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
BigInt r
Definition: numthry.cpp:26
GMP_MPZ base
Definition: gmp_powm.cpp:29
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.

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

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
GMP_MPZ base
Definition: gmp_powm.cpp:29
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.

References Binary, and encoded_size().

Referenced by Botan::CRL_Entry::decode_from(), Botan::ElGamal_Decryption_Operation::decrypt(), Botan::RSA_Private_Operation::decrypt(), Botan::DER_Encoder::encode(), encode_1363(), Botan::operator<<(), Botan::OSSL_BN::OSSL_BN(), Botan::OSSL_BN::to_bytes(), Botan::GMP_MPZ::to_bytes(), Botan::NR_Verification_Operation::verify_mr(), Botan::RW_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  }
BigInt n
Definition: numthry.cpp:26
static SecureVector< byte > encode(const BigInt &n, Base base=Binary)
Definition: big_code.cpp:64
GMP_MPZ base
Definition: gmp_powm.cpp:29
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.

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

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
BigInt n
Definition: numthry.cpp:26
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
char digit2char(byte b)
Definition: charset.cpp:171
GMP_MPZ base
Definition: gmp_powm.cpp:29
void hex_encode(char output[], const byte input[], size_t input_length, bool uppercase)
Definition: hex.cpp:14
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.

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

Referenced by Botan::DH_KA_Operation::agree(), 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::RW_Signature_Operation::sign(), Botan::RSA_Private_Operation::sign(), Botan::srp6_client_agree(), 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  }
BigInt n
Definition: numthry.cpp:26
static SecureVector< byte > encode(const BigInt &n, Base base=Binary)
Definition: big_code.cpp:64
size_t bytes() const
Definition: bigint.cpp:245
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 272 of file bigint.cpp.

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

Referenced by encode().

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

Flip the sign of this BigInt

Definition at line 302 of file bigint.cpp.

References reverse_sign(), and set_sign().

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

303  {
305  }
Sign reverse_sign() const
Definition: bigint.cpp:310
void set_sign(Sign sign)
Definition: bigint.cpp:291
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 160 of file bigint.cpp.

References Botan::MP_WORD_BITS, and word_at().

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

161  {
162  return ((word_at(n / MP_WORD_BITS) >> (n % MP_WORD_BITS)) & 1);
163  }
word word_at(size_t n) const
Definition: bigint.h:238
BigInt n
Definition: numthry.cpp:26
const size_t MP_WORD_BITS
Definition: mp_core.h:18
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::divide(), Botan::Montgomery_Exponentiator::execute(), Botan::mul_add(), operator%=(), operator*=(), operator+=(), operator-=(), Botan::operator<<(), operator<<=(), operator>>=(), Botan::square(), and Botan::GMP_MPZ::to_bigint().

325 { return reg; }
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; }
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 168 of file bigint.cpp.

References byte_at().

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

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

Increase internal register buffer by n words

Parameters
nincrease by n words

Definition at line 115 of file bigint.cpp.

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

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

Definition at line 123 of file bigint.cpp.

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

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

124  {
125  if(n > size())
126  reg.resize(round_up<size_t>(n, 8));
127  }
void resize(size_t n)
Definition: secmem.h:211
BigInt n
Definition: numthry.cpp:26
size_t size() const
Definition: bigint.h:284
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(), Botan::IF_Scheme_PrivateKey::check_key(), Botan::IF_Scheme_PrivateKey::IF_Scheme_PrivateKey(), Botan::inverse_mod(), Botan::jacobi(), Botan::Montgomery_Exponentiator::Montgomery_Exponentiator(), and Botan::primality_test().

158 { return (get_bit(0) == 0); }
bool get_bit(size_t n) const
Definition: bigint.cpp:160
bool Botan::BigInt::is_negative ( ) const
inline
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.

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

170 { return (!is_zero()); }
bool is_zero() const
Definition: bigint.h:176
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.

Referenced by Botan::inverse_mod(), and Botan::Core_Engine::mod_exp().

164 { return (get_bit(0) == 1); }
bool get_bit(size_t n) const
Definition: bigint.cpp:160
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 cmp(), Botan::low_zero_bits(), Botan::Montgomery_Exponentiator::Montgomery_Exponentiator(), Botan::operator%(), and Botan::Modular_Reducer::reduce().

251 { return (sign() == Positive); }
Sign sign() const
Definition: bigint.h:257
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::divide(), encode(), Botan::gcd(), Botan::inverse_mod(), Botan::jacobi(), Botan::mul_add(), Botan::operator%(), Botan::operator*(), operator>>=(), Botan::Power_Mod::set_base(), and set_sign().

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
void Botan::BigInt::mask_bits ( size_t  n)

Clear all but the lowest n bits

Parameters
namount of bits to keep

Definition at line 227 of file bigint.cpp.

References bits(), clear(), Botan::MP_WORD_BITS, and size().

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

228  {
229  if(n == 0) { clear(); return; }
230  if(n >= bits()) return;
231 
232  const size_t top_word = n / MP_WORD_BITS;
233  const word mask = (static_cast<word>(1) << (n % MP_WORD_BITS)) - 1;
234 
235  if(top_word < size())
236  for(size_t i = top_word + 1; i != size(); ++i)
237  reg[i] = 0;
238 
239  reg[top_word] &= mask;
240  }
BigInt n
Definition: numthry.cpp:26
size_t size() const
Definition: bigint.h:284
size_t bits() const
Definition: bigint.cpp:253
void clear()
Definition: bigint.h:143
const size_t MP_WORD_BITS
Definition: mp_core.h:18
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
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  }
GMP_MPZ mod
Definition: gmp_powm.cpp:29
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.

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

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  }
word word_at(size_t n) const
Definition: bigint.h:238
size_t sig_words() const
Definition: bigint.h:290
SecureVector< word > & get_reg()
Definition: bigint.h:325
GMP_MPZ mod
Definition: gmp_powm.cpp:29
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:123
void set_sign(Sign sign)
Definition: bigint.cpp:291
word bigint_modop(word n1, word n0, word d)
Definition: mp_misc.cpp:92
Sign sign() const
Definition: bigint.h:257
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  }
word word_at(size_t n) const
Definition: bigint.h:238
size_t sig_words() const
Definition: bigint.h:290
void bigint_linmul2(word x[], size_t x_size, word y)
Definition: mp_asm.cpp:149
SecureVector< word > & get_reg()
Definition: bigint.h:325
size_t size() const
Definition: bigint.h:284
void bigint_linmul3(word z[], const word x[], size_t x_size, word y)
Definition: mp_asm.cpp:167
const word * data() const
Definition: bigint.h:317
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:248
void grow_to(size_t n)
Definition: bigint.cpp:123
void set_sign(Sign sign)
Definition: bigint.cpp:291
Sign sign() const
Definition: bigint.h:257
BigInt& Botan::BigInt::operator++ ( )
inline

Increment operator

Definition at line 97 of file bigint.h.

97 { return (*this += 1); }
BigInt Botan::BigInt::operator++ ( int  )
inline

Postfix increment operator

Definition at line 107 of file bigint.h.

107 { BigInt x = (*this); ++(*this); return x; }
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(), 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  }
size_t sig_words() const
Definition: bigint.h:290
word bigint_sub2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_asm.cpp:87
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:127
signed int s32bit
Definition: types.h:37
const word * data() const
Definition: bigint.h:317
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:22
void grow_to(size_t n)
Definition: bigint.cpp:123
void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_asm.cpp:68
void set_sign(Sign sign)
Definition: bigint.cpp:291
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:415
s32bit bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_misc.cpp:41
Sign sign() const
Definition: bigint.h:257
BigInt Botan::BigInt::operator- ( ) const

Unary negation operator

Returns
negative this

Definition at line 320 of file bigint.cpp.

References flip_sign().

321  {
322  BigInt x = (*this);
323  x.flip_sign();
324  return x;
325  }
BigInt& Botan::BigInt::operator-- ( )
inline

Decrement operator

Definition at line 102 of file bigint.h.

102 { return (*this -= 1); }
BigInt Botan::BigInt::operator-- ( int  )
inline

Postfix decrement operator

Definition at line 112 of file bigint.h.

112 { BigInt x = (*this); --(*this); return x; }
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(), 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:108
size_t sig_words() const
Definition: bigint.h:290
word bigint_sub2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_asm.cpp:87
SecureVector< word > & get_reg()
Definition: bigint.h:325
signed int s32bit
Definition: types.h:37
const word * data() const
Definition: bigint.h:317
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:123
void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_asm.cpp:68
void set_sign(Sign sign)
Definition: bigint.cpp:291
s32bit bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_misc.cpp:41
Sign sign() const
Definition: bigint.h:257
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
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  }
size_t sig_words() const
Definition: bigint.h:290
SecureVector< word > & get_reg()
Definition: bigint.h:325
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:123
const size_t MP_WORD_BITS
Definition: mp_core.h:18
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
size_t sig_words() const
Definition: bigint.h:290
SecureVector< word > & get_reg()
Definition: bigint.h:325
bool is_zero() const
Definition: bigint.h:176
void set_sign(Sign sign)
Definition: bigint.cpp:291
const size_t MP_WORD_BITS
Definition: mp_core.h:18
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]; }
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]; }
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.

References BigInt(), and bits().

Referenced by Botan::DSA_PrivateKey::DSA_PrivateKey(), Botan::EC_PrivateKey::EC_PrivateKey(), and Botan::NR_PrivateKey::NR_PrivateKey().

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
RandomNumberGenerator * rng
Definition: global_rng.cpp:165
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.

References binary_decode(), clear(), Positive, Botan::RandomNumberGenerator::random_vec(), set_sign(), and Botan::MemoryRegion< T >::size().

Referenced by BigInt(), Botan::DH_PrivateKey::DH_PrivateKey(), Botan::DL_Group::DL_Group(), Botan::ElGamal_PrivateKey::ElGamal_PrivateKey(), Botan::primality_test(), Botan::NR_Signature_Operation::sign(), Botan::DSA_Signature_Operation::sign(), Botan::ECDSA_Signature_Operation::sign(), and Botan::GOST_3410_Signature_Operation::sign().

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:350
RandomNumberGenerator * rng
Definition: global_rng.cpp:165
void clear()
Definition: bigint.h:143
void set_sign(Sign sign)
Definition: bigint.cpp:291
BigInt::Sign Botan::BigInt::reverse_sign ( ) const
Returns
the opposite sign of the represented integer value

Definition at line 310 of file bigint.cpp.

References Negative, Positive, and sign().

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

311  {
312  if(sign() == Positive)
313  return Negative;
314  return Positive;
315  }
Sign sign() const
Definition: bigint.h:257
void Botan::BigInt::set_bit ( size_t  n)

Set bit at specified position

Parameters
nbit position to set

Definition at line 205 of file bigint.cpp.

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

Referenced by BigInt(), Botan::generate_dsa_primes(), and Botan::random_prime().

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

Set sign of the integer

Parameters
signnew Sign to set

Definition at line 291 of file bigint.cpp.

References is_zero(), Positive, and s.

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

292  {
293  if(is_zero())
294  signedness = Positive;
295  else
296  signedness = s;
297  }
bool is_zero() const
Definition: bigint.h:176
size_t s
Definition: numthry.cpp:27
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(), cmp(), Botan::divide(), Botan::Montgomery_Exponentiator::execute(), Botan::GMP_MPZ::GMP_MPZ(), Botan::Modular_Reducer::Modular_Reducer(), Botan::Montgomery_Exponentiator::Montgomery_Exponentiator(), Botan::mul_add(), Botan::operator%(), operator%=(), Botan::operator*(), operator*=(), Botan::operator+(), operator+=(), Botan::operator-(), operator-=(), operator/=(), Botan::operator<<(), operator<<=(), Botan::operator>>(), operator>>=(), Botan::Montgomery_Exponentiator::set_base(), 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 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-(), operator-=(), Botan::operator<<(), Botan::operator>>(), and reverse_sign().

257 { return (signedness); }
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 byte_at(), clear_bit(), Botan::Montgomery_Exponentiator::execute(), grow_reg(), grow_to(), Botan::low_zero_bits(), mask_bits(), Botan::mul_add(), Botan::operator*(), operator*=(), Botan::Montgomery_Exponentiator::set_base(), set_bit(), and Botan::square().

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

Swap this value with another

Parameters
otherBigInt to swap values with

Definition at line 106 of file bigint.cpp.

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

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

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

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

Returns
the value as a u32bit if conversion is possible

Definition at line 189 of file bigint.cpp.

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

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

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

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