diff options
author | lloyd <[email protected]> | 2010-10-12 16:19:56 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2010-10-12 16:19:56 +0000 |
commit | 4a6fd8c70d40f88c8b51127bfa055b66b18e0f7a (patch) | |
tree | d8c5697f8de1fff74c5b813fd83c08d310fa8ac0 /src | |
parent | c46a5e8d3dd8f07a92fc90027e6f7f70b989ea47 (diff) |
Use size_t in all of math, remove to_u32bit
Diffstat (limited to 'src')
36 files changed, 357 insertions, 374 deletions
diff --git a/src/asn1/ber_dec.cpp b/src/asn1/ber_dec.cpp index 860003440..7f0459e22 100644 --- a/src/asn1/ber_dec.cpp +++ b/src/asn1/ber_dec.cpp @@ -388,7 +388,14 @@ BER_Decoder& BER_Decoder::decode(u32bit& out, { BigInt integer; decode(integer, type_tag, class_tag); - out = integer.to_u32bit(); + + if(integer.bits() > 32) + throw BER_Decoding_Error("Decoded integer value larger than expected"); + + out = 0; + for(size_t i = 0; i != 4; ++i) + out = (out << 8) | integer.byte_at(3-i); + return (*this); } diff --git a/src/math/bigint/big_code.cpp b/src/math/bigint/big_code.cpp index 25c27bd33..99cf25aa0 100644 --- a/src/math/bigint/big_code.cpp +++ b/src/math/bigint/big_code.cpp @@ -30,8 +30,8 @@ void BigInt::encode(byte output[], const BigInt& n, Base base) else if(base == Octal) { BigInt copy = n; - const u32bit output_size = n.encoded_size(Octal); - for(u32bit j = 0; j != output_size; ++j) + const size_t output_size = n.encoded_size(Octal); + for(size_t j = 0; j != output_size; ++j) { output[output_size - 1 - j] = Charset::digit2char(copy % 8); copy /= 8; @@ -42,8 +42,8 @@ void BigInt::encode(byte output[], const BigInt& n, Base base) BigInt copy = n; BigInt remainder; copy.set_sign(Positive); - const u32bit output_size = n.encoded_size(Decimal); - for(u32bit j = 0; j != output_size; ++j) + const size_t output_size = n.encoded_size(Decimal); + for(size_t j = 0; j != output_size; ++j) { divide(copy, 10, copy, remainder); output[output_size - 1 - j] = @@ -64,7 +64,7 @@ SecureVector<byte> BigInt::encode(const BigInt& n, Base base) SecureVector<byte> output(n.encoded_size(base)); encode(&output[0], n, base); if(base != Binary) - for(u32bit j = 0; j != output.size(); ++j) + for(size_t j = 0; j != output.size(); ++j) if(output[j] == 0) output[j] = '0'; return output; @@ -73,13 +73,13 @@ SecureVector<byte> BigInt::encode(const BigInt& n, Base base) /* * Encode a BigInt, with leading 0s if needed */ -SecureVector<byte> BigInt::encode_1363(const BigInt& n, u32bit bytes) +SecureVector<byte> BigInt::encode_1363(const BigInt& n, size_t bytes) { - const u32bit n_bytes = n.bytes(); + const size_t n_bytes = n.bytes(); if(n_bytes > bytes) throw Encoding_Error("encode_1363: n is too large to encode properly"); - const u32bit leading_0s = bytes - n_bytes; + const size_t leading_0s = bytes - n_bytes; SecureVector<byte> output(bytes); encode(&output[leading_0s], n, Binary); @@ -97,7 +97,7 @@ BigInt BigInt::decode(const MemoryRegion<byte>& buf, Base base) /* * Decode a BigInt */ -BigInt BigInt::decode(const byte buf[], u32bit length, Base base) +BigInt BigInt::decode(const byte buf[], size_t length, Base base) { BigInt r; if(base == Binary) @@ -124,8 +124,8 @@ BigInt BigInt::decode(const byte buf[], u32bit length, Base base) } else if(base == Decimal || base == Octal) { - const u32bit RADIX = ((base == Decimal) ? 10 : 8); - for(u32bit j = 0; j != length; ++j) + const size_t RADIX = ((base == Decimal) ? 10 : 8); + for(size_t j = 0; j != length; ++j) { if(Charset::is_space(buf[j])) continue; diff --git a/src/math/bigint/big_io.cpp b/src/math/bigint/big_io.cpp index f3e7fb861..70e4a464a 100644 --- a/src/math/bigint/big_io.cpp +++ b/src/math/bigint/big_io.cpp @@ -28,7 +28,7 @@ std::ostream& operator<<(std::ostream& stream, const BigInt& n) if(n < 0) stream.write("-", 1); SecureVector<byte> buffer = BigInt::encode(n, base); - u32bit skip = 0; + size_t skip = 0; while(buffer[skip] == '0' && skip < buffer.size()) ++skip; stream.write(reinterpret_cast<const char*>(&buffer[0]) + skip, diff --git a/src/math/bigint/big_ops2.cpp b/src/math/bigint/big_ops2.cpp index 554fb1793..ff5cc7922 100644 --- a/src/math/bigint/big_ops2.cpp +++ b/src/math/bigint/big_ops2.cpp @@ -17,9 +17,9 @@ namespace Botan { */ BigInt& BigInt::operator+=(const BigInt& y) { - const u32bit x_sw = sig_words(), y_sw = y.sig_words(); + const size_t x_sw = sig_words(), y_sw = y.sig_words(); - const u32bit reg_size = std::max(x_sw, y_sw) + 1; + const size_t reg_size = std::max(x_sw, y_sw) + 1; grow_to(reg_size); if(sign() == y.sign()) @@ -52,11 +52,11 @@ BigInt& BigInt::operator+=(const BigInt& y) */ BigInt& BigInt::operator-=(const BigInt& y) { - const u32bit x_sw = sig_words(), y_sw = y.sig_words(); + const size_t x_sw = sig_words(), y_sw = y.sig_words(); s32bit relative_size = bigint_cmp(data(), x_sw, y.data(), y_sw); - const u32bit reg_size = std::max(x_sw, y_sw) + 1; + const size_t reg_size = std::max(x_sw, y_sw) + 1; grow_to(reg_size); if(relative_size < 0) @@ -94,7 +94,7 @@ BigInt& BigInt::operator-=(const BigInt& y) */ BigInt& BigInt::operator*=(const BigInt& y) { - const u32bit x_sw = sig_words(), y_sw = y.sig_words(); + const size_t x_sw = sig_words(), y_sw = y.sig_words(); set_sign((sign() == y.sign()) ? Positive : Negative); if(x_sw == 0 || y_sw == 0) @@ -165,7 +165,7 @@ word BigInt::operator%=(word mod) word remainder = 0; - for(u32bit j = sig_words(); j > 0; --j) + for(size_t j = sig_words(); j > 0; --j) remainder = bigint_modop(remainder, word_at(j-1), mod); clear(); grow_to(2); @@ -183,11 +183,11 @@ word BigInt::operator%=(word mod) /* * Left Shift Operator */ -BigInt& BigInt::operator<<=(u32bit shift) +BigInt& BigInt::operator<<=(size_t shift) { if(shift) { - const u32bit shift_words = shift / MP_WORD_BITS, + const size_t shift_words = shift / MP_WORD_BITS, shift_bits = shift % MP_WORD_BITS, words = sig_words(); @@ -201,11 +201,11 @@ BigInt& BigInt::operator<<=(u32bit shift) /* * Right Shift Operator */ -BigInt& BigInt::operator>>=(u32bit shift) +BigInt& BigInt::operator>>=(size_t shift) { if(shift) { - const u32bit shift_words = shift / MP_WORD_BITS, + const size_t shift_words = shift / MP_WORD_BITS, shift_bits = shift % MP_WORD_BITS; bigint_shr1(get_reg(), sig_words(), shift_words, shift_bits); diff --git a/src/math/bigint/big_ops3.cpp b/src/math/bigint/big_ops3.cpp index b92b71543..52472bc52 100644 --- a/src/math/bigint/big_ops3.cpp +++ b/src/math/bigint/big_ops3.cpp @@ -18,7 +18,7 @@ namespace Botan { */ BigInt operator+(const BigInt& x, const BigInt& y) { - const u32bit x_sw = x.sig_words(), y_sw = y.sig_words(); + const size_t x_sw = x.sig_words(), y_sw = y.sig_words(); BigInt z(x.sign(), std::max(x_sw, y_sw) + 1); @@ -47,7 +47,7 @@ BigInt operator+(const BigInt& x, const BigInt& y) */ BigInt operator-(const BigInt& x, const BigInt& y) { - const u32bit x_sw = x.sig_words(), y_sw = y.sig_words(); + const size_t x_sw = x.sig_words(), y_sw = y.sig_words(); s32bit relative_size = bigint_cmp(x.data(), x_sw, y.data(), y_sw); @@ -82,7 +82,7 @@ BigInt operator-(const BigInt& x, const BigInt& y) */ BigInt operator*(const BigInt& x, const BigInt& y) { - const u32bit x_sw = x.sig_words(), y_sw = y.sig_words(); + const size_t x_sw = x.sig_words(), y_sw = y.sig_words(); BigInt z(BigInt::Positive, x.size() + y.size()); @@ -142,7 +142,7 @@ word operator%(const BigInt& n, word mod) word remainder = 0; - for(u32bit j = n.sig_words(); j > 0; --j) + for(size_t j = n.sig_words(); j > 0; --j) remainder = bigint_modop(remainder, n.word_at(j-1), mod); if(remainder && n.sign() == BigInt::Negative) @@ -153,15 +153,15 @@ word operator%(const BigInt& n, word mod) /* * Left Shift Operator */ -BigInt operator<<(const BigInt& x, u32bit shift) +BigInt operator<<(const BigInt& x, size_t shift) { if(shift == 0) return x; - const u32bit shift_words = shift / MP_WORD_BITS, + const size_t shift_words = shift / MP_WORD_BITS, shift_bits = shift % MP_WORD_BITS; - const u32bit x_sw = x.sig_words(); + const size_t x_sw = x.sig_words(); BigInt y(x.sign(), x_sw + shift_words + (shift_bits ? 1 : 0)); bigint_shl2(y.get_reg(), x.data(), x_sw, shift_words, shift_bits); @@ -171,14 +171,14 @@ BigInt operator<<(const BigInt& x, u32bit shift) /* * Right Shift Operator */ -BigInt operator>>(const BigInt& x, u32bit shift) +BigInt operator>>(const BigInt& x, size_t shift) { if(shift == 0) return x; if(x.bits() <= shift) return 0; - const u32bit shift_words = shift / MP_WORD_BITS, + const size_t shift_words = shift / MP_WORD_BITS, shift_bits = shift % MP_WORD_BITS, x_sw = x.sig_words(); diff --git a/src/math/bigint/big_rand.cpp b/src/math/bigint/big_rand.cpp index 29fdf1de6..7cddb2de0 100644 --- a/src/math/bigint/big_rand.cpp +++ b/src/math/bigint/big_rand.cpp @@ -13,7 +13,7 @@ namespace Botan { /* * Construct a BigInt of a specific form */ -BigInt::BigInt(NumberType type, u32bit bits) +BigInt::BigInt(NumberType type, size_t bits) { set_sign(Positive); @@ -27,7 +27,7 @@ BigInt::BigInt(NumberType type, u32bit bits) * Randomize this number */ void BigInt::randomize(RandomNumberGenerator& rng, - u32bit bitsize) + size_t bitsize) { set_sign(Positive); diff --git a/src/math/bigint/bigint.cpp b/src/math/bigint/bigint.cpp index 2ac387a97..6fa929b6d 100644 --- a/src/math/bigint/bigint.cpp +++ b/src/math/bigint/bigint.cpp @@ -23,19 +23,19 @@ BigInt::BigInt(u64bit n) if(n == 0) return; - const u32bit limbs_needed = sizeof(u64bit) / sizeof(word); + const size_t limbs_needed = sizeof(u64bit) / sizeof(word); reg.resize(4*limbs_needed); - for(u32bit j = 0; j != limbs_needed; ++j) + for(size_t j = 0; j != limbs_needed; ++j) reg[j] = ((n >> (j*MP_WORD_BITS)) & MP_WORD_MASK); } /* * Construct a BigInt of the specified size */ -BigInt::BigInt(Sign s, u32bit size) +BigInt::BigInt(Sign s, size_t size) { - reg.resize(round_up<u32bit>(size, 8)); + reg.resize(round_up<size_t>(size, 8)); signedness = s; } @@ -44,11 +44,11 @@ BigInt::BigInt(Sign s, u32bit size) */ BigInt::BigInt(const BigInt& b) { - const u32bit b_words = b.sig_words(); + const size_t b_words = b.sig_words(); if(b_words) { - reg.resize(round_up<u32bit>(b_words, 8)); + reg.resize(round_up<size_t>(b_words, 8)); reg.copy(b.data(), b_words); set_sign(b.sign()); } @@ -65,7 +65,7 @@ BigInt::BigInt(const BigInt& b) BigInt::BigInt(const std::string& str) { Base base = Decimal; - u32bit markers = 0; + size_t markers = 0; bool negative = false; if(str.length() > 0 && str[0] == '-') { markers += 1; negative = true; } @@ -85,7 +85,7 @@ BigInt::BigInt(const std::string& str) /* * Construct a BigInt from an encoded BigInt */ -BigInt::BigInt(const byte input[], u32bit length, Base base) +BigInt::BigInt(const byte input[], size_t length, Base base) { set_sign(Positive); *this = decode(input, length, base); @@ -94,7 +94,7 @@ BigInt::BigInt(const byte input[], u32bit length, Base base) /* * Construct a BigInt from an encoded BigInt */ -BigInt::BigInt(RandomNumberGenerator& rng, u32bit bits) +BigInt::BigInt(RandomNumberGenerator& rng, size_t bits) { set_sign(Positive); randomize(rng, bits); @@ -112,18 +112,18 @@ void BigInt::swap(BigInt& other) /* * Grow the internal storage */ -void BigInt::grow_reg(u32bit n) +void BigInt::grow_reg(size_t n) { - reg.resize(round_up<u32bit>(size() + n, 8)); + reg.resize(round_up<size_t>(size() + n, 8)); } /* * Grow the internal storage */ -void BigInt::grow_to(u32bit n) +void BigInt::grow_to(size_t n) { if(n > size()) - reg.resize(round_up<u32bit>(n, 8)); + reg.resize(round_up<size_t>(n, 8)); } /* @@ -142,28 +142,12 @@ s32bit BigInt::cmp(const BigInt& n, bool check_signs) const } /* -* Convert this number to a u32bit, if possible -*/ -u32bit BigInt::to_u32bit() const - { - if(is_negative()) - throw Encoding_Error("BigInt::to_u32bit: Number is negative"); - if(bits() >= 32) - throw Encoding_Error("BigInt::to_u32bit: Number is too big to convert"); - - u32bit out = 0; - for(u32bit j = 0; j != 4; ++j) - out = (out << 8) | byte_at(3-j); - return out; - } - -/* * Return byte n of this number */ -byte BigInt::byte_at(u32bit n) const +byte BigInt::byte_at(size_t n) const { - const u32bit WORD_BYTES = sizeof(word); - u32bit word_num = n / WORD_BYTES, byte_num = n % WORD_BYTES; + const size_t WORD_BYTES = sizeof(word); + size_t word_num = n / WORD_BYTES, byte_num = n % WORD_BYTES; if(word_num >= size()) return 0; else @@ -173,7 +157,7 @@ byte BigInt::byte_at(u32bit n) const /* * Return bit n of this number */ -bool BigInt::get_bit(u32bit n) const +bool BigInt::get_bit(size_t n) const { return ((word_at(n / MP_WORD_BITS) >> (n % MP_WORD_BITS)) & 1); } @@ -181,27 +165,27 @@ bool BigInt::get_bit(u32bit n) const /* * Return bits {offset...offset+length} */ -u32bit BigInt::get_substring(u32bit offset, u32bit length) const +size_t BigInt::get_substring(size_t offset, size_t length) const { if(length > 32) throw Invalid_Argument("BigInt::get_substring: Substring size too big"); u64bit piece = 0; - for(u32bit j = 0; j != 8; ++j) + for(size_t j = 0; j != 8; ++j) piece = (piece << 8) | byte_at((offset / 8) + (7-j)); u64bit mask = (1 << length) - 1; - u32bit shift = (offset % 8); + size_t shift = (offset % 8); - return static_cast<u32bit>((piece >> shift) & mask); + return static_cast<size_t>((piece >> shift) & mask); } /* * Set bit number n */ -void BigInt::set_bit(u32bit n) +void BigInt::set_bit(size_t n) { - const u32bit which = n / MP_WORD_BITS; + const size_t which = n / MP_WORD_BITS; const word mask = static_cast<word>(1) << (n % MP_WORD_BITS); if(which >= size()) grow_to(which + 1); reg[which] |= mask; @@ -210,9 +194,9 @@ void BigInt::set_bit(u32bit n) /* * Clear bit number n */ -void BigInt::clear_bit(u32bit n) +void BigInt::clear_bit(size_t n) { - const u32bit which = n / MP_WORD_BITS; + const size_t which = n / MP_WORD_BITS; const word mask = static_cast<word>(1) << (n % MP_WORD_BITS); if(which < size()) reg[which] &= ~mask; @@ -221,16 +205,16 @@ void BigInt::clear_bit(u32bit n) /* * Clear all but the lowest n bits */ -void BigInt::mask_bits(u32bit n) +void BigInt::mask_bits(size_t n) { if(n == 0) { clear(); return; } if(n >= bits()) return; - const u32bit top_word = n / MP_WORD_BITS; + const size_t top_word = n / MP_WORD_BITS; const word mask = (static_cast<word>(1) << (n % MP_WORD_BITS)) - 1; if(top_word < size()) - for(u32bit j = top_word + 1; j != size(); ++j) + for(size_t j = top_word + 1; j != size(); ++j) reg[j] = 0; reg[top_word] &= mask; @@ -239,7 +223,7 @@ void BigInt::mask_bits(u32bit n) /* * Count how many bytes are being used */ -u32bit BigInt::bytes() const +size_t BigInt::bytes() const { return (bits() + 7) / 8; } @@ -247,14 +231,14 @@ u32bit BigInt::bytes() const /* * Count how many bits are being used */ -u32bit BigInt::bits() const +size_t BigInt::bits() const { - const u32bit words = sig_words(); + const size_t words = sig_words(); if(words == 0) return 0; - u32bit full_words = words - 1, top_bits = MP_WORD_BITS; + size_t full_words = words - 1, top_bits = MP_WORD_BITS; word top_word = word_at(full_words), mask = MP_WORD_TOP_BIT; while(top_bits && ((top_word & mask) == 0)) @@ -266,7 +250,7 @@ u32bit BigInt::bits() const /* * Calcluate the size in a certain base */ -u32bit BigInt::encoded_size(Base base) const +size_t BigInt::encoded_size(Base base) const { static const double LOG_2_BASE_10 = 0.30102999566; @@ -277,7 +261,7 @@ u32bit BigInt::encoded_size(Base base) const else if(base == Octal) return ((bits() + 2) / 3); else if(base == Decimal) - return static_cast<u32bit>((bits() * LOG_2_BASE_10) + 1); + return static_cast<size_t>((bits() * LOG_2_BASE_10) + 1); else throw Invalid_Argument("Unknown base for BigInt encoding"); } @@ -336,28 +320,28 @@ BigInt BigInt::abs() const */ void BigInt::binary_encode(byte output[]) const { - const u32bit sig_bytes = bytes(); - for(u32bit j = 0; j != sig_bytes; ++j) + const size_t sig_bytes = bytes(); + for(size_t j = 0; j != sig_bytes; ++j) output[sig_bytes-j-1] = byte_at(j); } /* * Set this number to the value in buf */ -void BigInt::binary_decode(const byte buf[], u32bit length) +void BigInt::binary_decode(const byte buf[], size_t length) { - const u32bit WORD_BYTES = sizeof(word); + const size_t WORD_BYTES = sizeof(word); clear(); - reg.resize(round_up<u32bit>((length / WORD_BYTES) + 1, 8)); + reg.resize(round_up<size_t>((length / WORD_BYTES) + 1, 8)); - for(u32bit j = 0; j != length / WORD_BYTES; ++j) + for(size_t j = 0; j != length / WORD_BYTES; ++j) { - u32bit top = length - WORD_BYTES*j; - for(u32bit k = WORD_BYTES; k > 0; --k) + size_t top = length - WORD_BYTES*j; + for(size_t k = WORD_BYTES; k > 0; --k) reg[j] = (reg[j] << 8) | buf[top - k]; } - for(u32bit j = 0; j != length % WORD_BYTES; ++j) + for(size_t j = 0; j != length % WORD_BYTES; ++j) reg[length / WORD_BYTES] = (reg[length / WORD_BYTES] << 8) | buf[j]; } diff --git a/src/math/bigint/bigint.h b/src/math/bigint/bigint.h index cd6997698..3fee70899 100644 --- a/src/math/bigint/bigint.h +++ b/src/math/bigint/bigint.h @@ -83,13 +83,13 @@ class BOTAN_DLL BigInt * Left shift operator * @param shift the number of bits to shift this left by */ - BigInt& operator<<=(u32bit shift); + BigInt& operator<<=(size_t shift); /** * Right shift operator * @param shift the number of bits to shift this right by */ - BigInt& operator>>=(u32bit shift); + BigInt& operator>>=(size_t shift); /** * Increment operator @@ -128,14 +128,14 @@ class BOTAN_DLL BigInt * @param i a word index * @return the word at index i */ - word& operator[](u32bit i) { return reg[i]; } + word& operator[](size_t i) { return reg[i]; } /** * [] operator (array access) * @param i a word index * @return the word at index i */ - word operator[](u32bit i) const { return reg[i]; } + word operator[](size_t i) const { return reg[i]; } /** * Zeroize the BigInt @@ -175,9 +175,9 @@ class BOTAN_DLL BigInt */ bool is_zero() const { - const u32bit sw = sig_words(); + const size_t sw = sig_words(); - for(u32bit i = 0; i != sw; ++i) + for(size_t i = 0; i != sw; ++i) if(reg[i]) return false; return true; @@ -187,26 +187,26 @@ class BOTAN_DLL BigInt * Set bit at specified position * @param n bit position to set */ - void set_bit(u32bit n); + void set_bit(size_t n); /** * Clear bit at specified position * @param n bit position to clear */ - void clear_bit(u32bit n); + void clear_bit(size_t n); /** * Clear all but the lowest n bits * @param n amount of bits to keep */ - void mask_bits(u32bit n); + void mask_bits(size_t n); /** * Return bit value at specified position * @param n the bit offset to test * @result true, if the bit at position n is set, false otherwise */ - bool get_bit(u32bit n) const; + bool get_bit(size_t n) const; /** * Return (a maximum of) 32 bits of the complete value @@ -215,32 +215,23 @@ class BOTAN_DLL BigInt * @result the integer extracted from the register starting at * offset with specified length */ - u32bit get_substring(u32bit offset, u32bit length) const; + size_t get_substring(size_t offset, size_t length) const; /** * @param n the offset to get a byte from * @result byte at offset n */ - byte byte_at(u32bit n) const; + byte byte_at(size_t n) const; /** * Return the word at a specified position of the internal register * @param n position in the register * @return value at position n */ - word word_at(u32bit n) const + word word_at(size_t n) const { return ((n < size()) ? reg[n] : 0); } /** - * Return the integer as an unsigned 32bit-integer-value. If the - * value is negative OR too big to be stored in a u32bit, this - * function will throw an exception. - * - * @result unsigned 32 bit representation of this - */ - u32bit to_u32bit() const; - - /** * Tests if the sign of the integer is negative * @result true, iff the integer has a negative sign */ @@ -283,16 +274,16 @@ class BOTAN_DLL BigInt * Give size of internal register * @result size of internal register in words */ - u32bit size() const { return get_reg().size(); } + size_t size() const { return get_reg().size(); } /** * Return how many words we need to hold this value * @result significant words of the represented integer value */ - u32bit sig_words() const + size_t sig_words() const { const word* x = ®[0]; - u32bit sig = reg.size(); + size_t sig = reg.size(); while(sig && (x[sig-1] == 0)) sig--; @@ -303,13 +294,13 @@ class BOTAN_DLL BigInt * Give byte length of the integer * @result byte length of the represented integer value */ - u32bit bytes() const; + size_t bytes() const; /** * Get the bit length of the integer * @result bit length of the represented integer value */ - u32bit bits() const; + size_t bits() const; /** * Return a pointer to the big integer word register @@ -337,16 +328,16 @@ class BOTAN_DLL BigInt * Increase internal register buffer by n words * @param n increase by n words */ - void grow_reg(u32bit n); + void grow_reg(size_t n); - void grow_to(u32bit n); + void grow_to(size_t n); /** * Fill BigInt with a random number with size of bitsize * @param rng the random number generator to use * @param bitsize number of bits the created random value should have */ - void randomize(RandomNumberGenerator& rng, u32bit bitsize = 0); + void randomize(RandomNumberGenerator& rng, size_t bitsize = 0); /** * Store BigInt-value in a given byte array @@ -359,7 +350,7 @@ class BOTAN_DLL BigInt * @param buf byte array buffer containing the integer * @param length size of buf */ - void binary_decode(const byte buf[], u32bit length); + void binary_decode(const byte buf[], size_t length); /** * Read integer value from a byte array (MemoryRegion<byte>) @@ -371,7 +362,7 @@ class BOTAN_DLL BigInt * @param base the base to measure the size for * @return size of this integer in base base */ - u32bit encoded_size(Base base = Binary) const; + size_t encoded_size(Base base = Binary) const; /** * @param rng a random number generator @@ -407,7 +398,7 @@ class BOTAN_DLL BigInt * @param base number-base of the integer in buf * @result BigInt representing the integer in the byte array */ - static BigInt decode(const byte buf[], u32bit length, + static BigInt decode(const byte buf[], size_t length, Base base = Binary); /** @@ -425,7 +416,7 @@ class BOTAN_DLL BigInt * @param bytes the length of the resulting SecureVector<byte> * @result a SecureVector<byte> containing the encoded BigInt */ - static SecureVector<byte> encode_1363(const BigInt& n, u32bit bytes); + static SecureVector<byte> encode_1363(const BigInt& n, size_t bytes); /** * Swap this value with another @@ -468,21 +459,21 @@ class BOTAN_DLL BigInt * @param length size of buf * @param base is the number base of the integer in buf */ - BigInt(const byte buf[], u32bit length, Base base = Binary); + BigInt(const byte buf[], size_t length, Base base = Binary); /** * Create a random BigInt of the specified size * @param rng random number generator * @param bits size in bits */ - BigInt(RandomNumberGenerator& rng, u32bit bits); + BigInt(RandomNumberGenerator& rng, size_t bits); /** * Create BigInt of specified size, all zeros * @param sign the sign * @param n size of the internal register in words */ - BigInt(Sign sign, u32bit n); + BigInt(Sign sign, size_t n); /** * Create a number of the specified type and size @@ -491,7 +482,7 @@ class BOTAN_DLL BigInt * @param n a size/length parameter, interpretation depends upon * the value of type */ - BigInt(NumberType type, u32bit n); + BigInt(NumberType type, size_t n); private: SecureVector<word> reg; @@ -507,8 +498,8 @@ BigInt BOTAN_DLL operator*(const BigInt& x, const BigInt& y); BigInt BOTAN_DLL operator/(const BigInt& x, const BigInt& d); BigInt BOTAN_DLL operator%(const BigInt& x, const BigInt& m); word BOTAN_DLL operator%(const BigInt& x, word m); -BigInt BOTAN_DLL operator<<(const BigInt& x, u32bit n); -BigInt BOTAN_DLL operator>>(const BigInt& x, u32bit n); +BigInt BOTAN_DLL operator<<(const BigInt& x, size_t n); +BigInt BOTAN_DLL operator>>(const BigInt& x, size_t n); /* * Comparison Operators diff --git a/src/math/bigint/divide.cpp b/src/math/bigint/divide.cpp index 2eb7538db..ba84aa7d9 100644 --- a/src/math/bigint/divide.cpp +++ b/src/math/bigint/divide.cpp @@ -37,7 +37,7 @@ void divide(const BigInt& x, const BigInt& y_arg, BigInt& q, BigInt& r) throw BigInt::DivideByZero(); BigInt y = y_arg; - const u32bit y_words = y.sig_words(); + const size_t y_words = y.sig_words(); r = x; q = 0; @@ -54,13 +54,13 @@ void divide(const BigInt& x, const BigInt& y_arg, BigInt& q, BigInt& r) } else if(compare > 0) { - u32bit shifts = 0; + size_t shifts = 0; word y_top = y[y.sig_words()-1]; while(y_top < MP_WORD_TOP_BIT) { y_top <<= 1; ++shifts; } y <<= shifts; r <<= shifts; - const u32bit n = r.sig_words() - 1, t = y_words - 1; + const size_t n = r.sig_words() - 1, t = y_words - 1; if(n < t) throw Internal_Error("BigInt division word sizes"); @@ -78,7 +78,7 @@ void divide(const BigInt& x, const BigInt& y_arg, BigInt& q, BigInt& r) while(r >= temp) { r -= temp; ++q[n-t]; } - for(u32bit j = n; j != t; --j) + for(size_t j = n; j != t; --j) { const word x_j0 = r.word_at(j); const word x_j1 = r.word_at(j-1); diff --git a/src/math/mp/monty_generic/mp_monty.cpp b/src/math/mp/monty_generic/mp_monty.cpp index bce35259a..d7f7e0306 100644 --- a/src/math/mp/monty_generic/mp_monty.cpp +++ b/src/math/mp/monty_generic/mp_monty.cpp @@ -18,14 +18,14 @@ extern "C" { /* * Montgomery Reduction Algorithm */ -void bigint_monty_redc(word z[], u32bit z_size, +void bigint_monty_redc(word z[], size_t z_size, word ws[], - const word x[], u32bit x_size, + const word x[], size_t x_size, word u) { - const u32bit blocks_of_8 = x_size - (x_size % 8); + const size_t blocks_of_8 = x_size - (x_size % 8); - for(u32bit i = 0; i != x_size; ++i) + for(size_t i = 0; i != x_size; ++i) { word* z_i = z + i; @@ -37,10 +37,10 @@ void bigint_monty_redc(word z[], u32bit z_size, */ word carry = 0; - for(u32bit j = 0; j != blocks_of_8; j += 8) + for(size_t j = 0; j != blocks_of_8; j += 8) carry = word8_madd3(z_i + j, x + j, y, carry); - for(u32bit j = blocks_of_8; j != x_size; ++j) + for(size_t j = blocks_of_8; j != x_size; ++j) z_i[j] = word_madd3(x[j], y, z_i[j], &carry); word z_sum = z_i[x_size] + carry; @@ -48,7 +48,7 @@ void bigint_monty_redc(word z[], u32bit z_size, z_i[x_size] = z_sum; // Note: not constant time - for(u32bit j = x_size + 1; carry && j != z_size - i; ++j) + for(size_t j = x_size + 1; carry && j != z_size - i; ++j) { ++z_i[j]; carry = !z_i[j]; @@ -56,7 +56,7 @@ void bigint_monty_redc(word z[], u32bit z_size, } word borrow = 0; - for(u32bit i = 0; i != x_size; ++i) + for(size_t i = 0; i != x_size; ++i) ws[i] = word_sub(z[x_size + i], x[i], &borrow); ws[x_size] = word_sub(z[x_size+x_size], 0, &borrow); diff --git a/src/math/mp/mp_asm.cpp b/src/math/mp/mp_asm.cpp index 4fcdee7a4..d164c1d33 100644 --- a/src/math/mp/mp_asm.cpp +++ b/src/math/mp/mp_asm.cpp @@ -19,19 +19,19 @@ extern "C" { /* * Two Operand Addition, No Carry */ -word bigint_add2_nc(word x[], u32bit x_size, const word y[], u32bit y_size) +word bigint_add2_nc(word x[], size_t x_size, const word y[], size_t y_size) { word carry = 0; - const u32bit blocks = y_size - (y_size % 8); + const size_t blocks = y_size - (y_size % 8); - for(u32bit i = 0; i != blocks; i += 8) + for(size_t i = 0; i != blocks; i += 8) carry = word8_add2(x + i, y + i, carry); - for(u32bit i = blocks; i != y_size; ++i) + for(size_t i = blocks; i != y_size; ++i) x[i] = word_add(x[i], y[i], &carry); - for(u32bit i = y_size; i != x_size; ++i) + for(size_t i = y_size; i != x_size; ++i) x[i] = word_add(x[i], 0, &carry); return carry; @@ -40,23 +40,23 @@ word bigint_add2_nc(word x[], u32bit x_size, const word y[], u32bit y_size) /* * Three Operand Addition, No Carry */ -word bigint_add3_nc(word z[], const word x[], u32bit x_size, - const word y[], u32bit y_size) +word bigint_add3_nc(word z[], const word x[], size_t x_size, + const word y[], size_t y_size) { if(x_size < y_size) { return bigint_add3_nc(z, y, y_size, x, x_size); } word carry = 0; - const u32bit blocks = y_size - (y_size % 8); + const size_t blocks = y_size - (y_size % 8); - for(u32bit i = 0; i != blocks; i += 8) + for(size_t i = 0; i != blocks; i += 8) carry = word8_add3(z + i, x + i, y + i, carry); - for(u32bit i = blocks; i != y_size; ++i) + for(size_t i = blocks; i != y_size; ++i) z[i] = word_add(x[i], y[i], &carry); - for(u32bit i = y_size; i != x_size; ++i) + for(size_t i = y_size; i != x_size; ++i) z[i] = word_add(x[i], 0, &carry); return carry; @@ -65,7 +65,7 @@ word bigint_add3_nc(word z[], const word x[], u32bit x_size, /* * Two Operand Addition */ -void bigint_add2(word x[], u32bit x_size, const word y[], u32bit y_size) +void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size) { x[x_size] += bigint_add2_nc(x, x_size, y, y_size); } @@ -73,8 +73,8 @@ void bigint_add2(word x[], u32bit x_size, const word y[], u32bit y_size) /* * Three Operand Addition */ -void bigint_add3(word z[], const word x[], u32bit x_size, - const word y[], u32bit y_size) +void bigint_add3(word z[], const word x[], size_t x_size, + const word y[], size_t y_size) { z[(x_size > y_size ? x_size : y_size)] += bigint_add3_nc(z, x, x_size, y, y_size); @@ -83,19 +83,19 @@ void bigint_add3(word z[], const word x[], u32bit x_size, /* * Two Operand Subtraction */ -word bigint_sub2(word x[], u32bit x_size, const word y[], u32bit y_size) +word bigint_sub2(word x[], size_t x_size, const word y[], size_t y_size) { word borrow = 0; - const u32bit blocks = y_size - (y_size % 8); + const size_t blocks = y_size - (y_size % 8); - for(u32bit i = 0; i != blocks; i += 8) + for(size_t i = 0; i != blocks; i += 8) borrow = word8_sub2(x + i, y + i, borrow); - for(u32bit i = blocks; i != y_size; ++i) + for(size_t i = blocks; i != y_size; ++i) x[i] = word_sub(x[i], y[i], &borrow); - for(u32bit i = y_size; i != x_size; ++i) + for(size_t i = y_size; i != x_size; ++i) x[i] = word_sub(x[i], 0, &borrow); return borrow; @@ -104,16 +104,16 @@ word bigint_sub2(word x[], u32bit x_size, const word y[], u32bit y_size) /* * Two Operand Subtraction x = y - x */ -void bigint_sub2_rev(word x[], const word y[], u32bit y_size) +void bigint_sub2_rev(word x[], const word y[], size_t y_size) { word borrow = 0; - const u32bit blocks = y_size - (y_size % 8); + const size_t blocks = y_size - (y_size % 8); - for(u32bit i = 0; i != blocks; i += 8) + for(size_t i = 0; i != blocks; i += 8) borrow = word8_sub2_rev(x + i, y + i, borrow); - for(u32bit i = blocks; i != y_size; ++i) + for(size_t i = blocks; i != y_size; ++i) x[i] = word_sub(y[i], x[i], &borrow); if(borrow) @@ -123,20 +123,20 @@ void bigint_sub2_rev(word x[], const word y[], u32bit y_size) /* * Three Operand Subtraction */ -word bigint_sub3(word z[], const word x[], u32bit x_size, - const word y[], u32bit y_size) +word bigint_sub3(word z[], const word x[], size_t x_size, + const word y[], size_t y_size) { word borrow = 0; - const u32bit blocks = y_size - (y_size % 8); + const size_t blocks = y_size - (y_size % 8); - for(u32bit i = 0; i != blocks; i += 8) + for(size_t i = 0; i != blocks; i += 8) borrow = word8_sub3(z + i, x + i, y + i, borrow); - for(u32bit i = blocks; i != y_size; ++i) + for(size_t i = blocks; i != y_size; ++i) z[i] = word_sub(x[i], y[i], &borrow); - for(u32bit i = y_size; i != x_size; ++i) + for(size_t i = y_size; i != x_size; ++i) z[i] = word_sub(x[i], 0, &borrow); return borrow; @@ -145,16 +145,16 @@ word bigint_sub3(word z[], const word x[], u32bit x_size, /* * Two Operand Linear Multiply */ -void bigint_linmul2(word x[], u32bit x_size, word y) +void bigint_linmul2(word x[], size_t x_size, word y) { - const u32bit blocks = x_size - (x_size % 8); + const size_t blocks = x_size - (x_size % 8); word carry = 0; - for(u32bit i = 0; i != blocks; i += 8) + for(size_t i = 0; i != blocks; i += 8) carry = word8_linmul2(x + i, y, carry); - for(u32bit i = blocks; i != x_size; ++i) + for(size_t i = blocks; i != x_size; ++i) x[i] = word_madd2(x[i], y, &carry); x[x_size] = carry; @@ -163,16 +163,16 @@ void bigint_linmul2(word x[], u32bit x_size, word y) /* * Three Operand Linear Multiply */ -void bigint_linmul3(word z[], const word x[], u32bit x_size, word y) +void bigint_linmul3(word z[], const word x[], size_t x_size, word y) { - const u32bit blocks = x_size - (x_size % 8); + const size_t blocks = x_size - (x_size % 8); word carry = 0; - for(u32bit i = 0; i != blocks; i += 8) + for(size_t i = 0; i != blocks; i += 8) carry = word8_linmul3(z + i, x + i, y, carry); - for(u32bit i = blocks; i != x_size; ++i) + for(size_t i = blocks; i != x_size; ++i) z[i] = word_madd2(x[i], y, &carry); z[x_size] = carry; diff --git a/src/math/mp/mp_asm64/mp_asm.h b/src/math/mp/mp_asm64/mp_asm.h index d9135ace2..625ea1c4f 100644 --- a/src/math/mp/mp_asm64/mp_asm.h +++ b/src/math/mp/mp_asm64/mp_asm.h @@ -53,7 +53,7 @@ namespace Botan { // with 64-bit registers/ALU, but no 64x64->128 multiply. inline void bigint_2word_mul(word a, word b, word* z1, word* z0) { - const u32bit MP_HWORD_BITS = BOTAN_MP_WORD_BITS / 2; + const size_t MP_HWORD_BITS = BOTAN_MP_WORD_BITS / 2; const word MP_HWORD_MASK = ((word)1 << MP_HWORD_BITS) - 1; const word a_hi = (a >> MP_HWORD_BITS); diff --git a/src/math/mp/mp_core.h b/src/math/mp/mp_core.h index 63082795f..e1692006e 100644 --- a/src/math/mp/mp_core.h +++ b/src/math/mp/mp_core.h @@ -15,67 +15,67 @@ namespace Botan { /* * The size of the word type, in bits */ -const u32bit MP_WORD_BITS = BOTAN_MP_WORD_BITS; +const size_t MP_WORD_BITS = BOTAN_MP_WORD_BITS; extern "C" { /* * Addition/Subtraction Operations */ -void bigint_add2(word x[], u32bit x_size, - const word y[], u32bit y_size); +void bigint_add2(word x[], size_t x_size, + const word y[], size_t y_size); void bigint_add3(word z[], - const word x[], u32bit x_size, - const word y[], u32bit y_size); + const word x[], size_t x_size, + const word y[], size_t y_size); -word bigint_add2_nc(word x[], u32bit x_size, const word y[], u32bit y_size); +word bigint_add2_nc(word x[], size_t x_size, const word y[], size_t y_size); word bigint_add3_nc(word z[], - const word x[], u32bit x_size, - const word y[], u32bit y_size); + const word x[], size_t x_size, + const word y[], size_t y_size); -word bigint_sub2(word x[], u32bit x_size, - const word y[], u32bit y_size); +word bigint_sub2(word x[], size_t x_size, + const word y[], size_t y_size); /** * x = y - x; assumes y >= x */ -void bigint_sub2_rev(word x[], const word y[], u32bit y_size); +void bigint_sub2_rev(word x[], const word y[], size_t y_size); word bigint_sub3(word z[], - const word x[], u32bit x_size, - const word y[], u32bit y_size); + const word x[], size_t x_size, + const word y[], size_t y_size); /* * Shift Operations */ -void bigint_shl1(word x[], u32bit x_size, - u32bit word_shift, u32bit bit_shift); +void bigint_shl1(word x[], size_t x_size, + size_t word_shift, size_t bit_shift); -void bigint_shr1(word x[], u32bit x_size, - u32bit word_shift, u32bit bit_shift); +void bigint_shr1(word x[], size_t x_size, + size_t word_shift, size_t bit_shift); -void bigint_shl2(word y[], const word x[], u32bit x_size, - u32bit word_shift, u32bit bit_shift); +void bigint_shl2(word y[], const word x[], size_t x_size, + size_t word_shift, size_t bit_shift); -void bigint_shr2(word y[], const word x[], u32bit x_size, - u32bit word_shift, u32bit bit_shift); +void bigint_shr2(word y[], const word x[], size_t x_size, + size_t word_shift, size_t bit_shift); /* * Simple O(N^2) Multiplication and Squaring */ void bigint_simple_mul(word z[], - const word x[], u32bit x_size, - const word y[], u32bit y_size); + const word x[], size_t x_size, + const word y[], size_t y_size); -void bigint_simple_sqr(word z[], const word x[], u32bit x_size); +void bigint_simple_sqr(word z[], const word x[], size_t x_size); /* * Linear Multiply */ -void bigint_linmul2(word x[], u32bit x_size, word y); -void bigint_linmul3(word z[], const word x[], u32bit x_size, word y); +void bigint_linmul2(word x[], size_t x_size, word y); +void bigint_linmul3(word z[], const word x[], size_t x_size, word y); /* * Montgomery Reduction @@ -86,22 +86,22 @@ void bigint_linmul3(word z[], const word x[], u32bit x_size, word y); * @param x_size size of x * @param u Montgomery value */ -void bigint_monty_redc(word z[], u32bit z_size, +void bigint_monty_redc(word z[], size_t z_size, word workspace[], - const word x[], u32bit x_size, + const word x[], size_t x_size, word u); /* * Division operation */ -u32bit bigint_divcore(word q, word y2, word y1, +size_t bigint_divcore(word q, word y2, word y1, word x3, word x2, word x1); /** * Compare x and y */ -s32bit bigint_cmp(const word x[], u32bit x_size, - const word y[], u32bit y_size); +s32bit bigint_cmp(const word x[], size_t x_size, + const word y[], size_t y_size); /** * Compute ((n1<<bits) + n0) / d @@ -132,12 +132,12 @@ void bigint_comba_sqr16(word out[64], const word in[32]); /* * High Level Multiplication/Squaring Interfaces */ -void bigint_mul(word z[], u32bit z_size, word workspace[], - const word x[], u32bit x_size, u32bit x_sw, - const word y[], u32bit y_size, u32bit y_sw); +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); -void bigint_sqr(word z[], u32bit z_size, word workspace[], - const word x[], u32bit x_size, u32bit x_sw); +void bigint_sqr(word z[], size_t z_size, word workspace[], + const word x[], size_t x_size, size_t x_sw); } diff --git a/src/math/mp/mp_generic/mp_asm.h b/src/math/mp/mp_generic/mp_asm.h index 7c18343ef..ee46e1aa9 100644 --- a/src/math/mp/mp_generic/mp_asm.h +++ b/src/math/mp/mp_generic/mp_asm.h @@ -14,7 +14,7 @@ #if (BOTAN_MP_WORD_BITS == 8) typedef Botan::u16bit dword; #elif (BOTAN_MP_WORD_BITS == 16) - typedef Botan::u32bit dword; + typedef Botan::size_t dword; #elif (BOTAN_MP_WORD_BITS == 32) typedef Botan::u64bit dword; #elif (BOTAN_MP_WORD_BITS == 64) diff --git a/src/math/mp/mp_karat.cpp b/src/math/mp/mp_karat.cpp index 1cb278367..ea0693bf1 100644 --- a/src/math/mp/mp_karat.cpp +++ b/src/math/mp/mp_karat.cpp @@ -16,7 +16,7 @@ namespace { /* * Karatsuba Multiplication Operation */ -void karatsuba_mul(word z[], const word x[], const word y[], u32bit N, +void karatsuba_mul(word z[], const word x[], const word y[], size_t N, word workspace[]) { if(N < BOTAN_KARAT_MUL_THRESHOLD || N % 2) @@ -31,7 +31,7 @@ void karatsuba_mul(word z[], const word x[], const word y[], u32bit N, return bigint_simple_mul(z, x, N, y, N); } - const u32bit N2 = N / 2; + const size_t N2 = N / 2; const word* x0 = x; const word* x1 = x + N2; @@ -63,28 +63,28 @@ void karatsuba_mul(word z[], const word x[], const word y[], u32bit N, karatsuba_mul(z0, x0, y0, N2, workspace+N); karatsuba_mul(z1, x1, y1, N2, workspace+N); - const u32bit blocks_of_8 = N - (N % 8); + const size_t blocks_of_8 = N - (N % 8); word ws_carry = 0; - for(u32bit j = 0; j != blocks_of_8; j += 8) + for(size_t j = 0; j != blocks_of_8; j += 8) ws_carry = word8_add3(workspace + N + j, z0 + j, z1 + j, ws_carry); - for(u32bit j = blocks_of_8; j != N; ++j) + for(size_t j = blocks_of_8; j != N; ++j) workspace[N + j] = word_add(z0[j], z1[j], &ws_carry); word z_carry = 0; - for(u32bit j = 0; j != blocks_of_8; j += 8) + for(size_t j = 0; j != blocks_of_8; j += 8) z_carry = word8_add2(z + N2 + j, workspace + N + j, z_carry); - for(u32bit j = blocks_of_8; j != N; ++j) + for(size_t j = blocks_of_8; j != N; ++j) z[N2 + j] = word_add(z[N2 + j], workspace[N + j], &z_carry); z[N + N2] = word_add(z[N + N2], ws_carry, &z_carry); if(z_carry) - for(u32bit j = 1; j != N2; ++j) + for(size_t j = 1; j != N2; ++j) if(++z[N + N2 + j]) break; @@ -97,7 +97,7 @@ void karatsuba_mul(word z[], const word x[], const word y[], u32bit N, /* * Karatsuba Squaring Operation */ -void karatsuba_sqr(word z[], const word x[], u32bit N, word workspace[]) +void karatsuba_sqr(word z[], const word x[], size_t N, word workspace[]) { if(N < BOTAN_KARAT_SQR_THRESHOLD || N % 2) { @@ -111,7 +111,7 @@ void karatsuba_sqr(word z[], const word x[], u32bit N, word workspace[]) return bigint_simple_sqr(z, x, N); } - const u32bit N2 = N / 2; + const size_t N2 = N / 2; const word* x0 = x; const word* x1 = x + N2; @@ -135,28 +135,28 @@ void karatsuba_sqr(word z[], const word x[], u32bit N, word workspace[]) karatsuba_sqr(z0, x0, N2, workspace+N); karatsuba_sqr(z1, x1, N2, workspace+N); - const u32bit blocks_of_8 = N - (N % 8); + const size_t blocks_of_8 = N - (N % 8); word ws_carry = 0; - for(u32bit j = 0; j != blocks_of_8; j += 8) + for(size_t j = 0; j != blocks_of_8; j += 8) ws_carry = word8_add3(workspace + N + j, z0 + j, z1 + j, ws_carry); - for(u32bit j = blocks_of_8; j != N; ++j) + for(size_t j = blocks_of_8; j != N; ++j) workspace[N + j] = word_add(z0[j], z1[j], &ws_carry); word z_carry = 0; - for(u32bit j = 0; j != blocks_of_8; j += 8) + for(size_t j = 0; j != blocks_of_8; j += 8) z_carry = word8_add2(z + N2 + j, workspace + N + j, z_carry); - for(u32bit j = blocks_of_8; j != N; ++j) + for(size_t j = blocks_of_8; j != N; ++j) z[N2 + j] = word_add(z[N2 + j], workspace[N + j], &z_carry); z[N + N2] = word_add(z[N + N2], ws_carry, &z_carry); if(z_carry) - for(u32bit j = 1; j != N2; ++j) + for(size_t j = 1; j != N2; ++j) if(++z[N + N2 + j]) break; @@ -171,9 +171,9 @@ void karatsuba_sqr(word z[], const word x[], u32bit N, word workspace[]) /* * Pick a good size for the Karatsuba multiply */ -u32bit karatsuba_size(u32bit z_size, - u32bit x_size, u32bit x_sw, - u32bit y_size, u32bit y_sw) +size_t karatsuba_size(size_t z_size, + size_t x_size, size_t x_sw, + size_t y_size, size_t y_sw) { if(x_sw > x_size || x_sw > y_size || y_sw > x_size || y_sw > y_size) return 0; @@ -182,8 +182,8 @@ u32bit karatsuba_size(u32bit z_size, ((y_size == y_sw) && (y_size % 2))) return 0; - const u32bit start = (x_sw > y_sw) ? x_sw : y_sw; - const u32bit end = (x_size < y_size) ? x_size : y_size; + const size_t start = (x_sw > y_sw) ? x_sw : y_sw; + const size_t end = (x_size < y_size) ? x_size : y_size; if(start == end) { @@ -192,7 +192,7 @@ u32bit karatsuba_size(u32bit z_size, return start; } - for(u32bit j = start; j <= end; ++j) + for(size_t j = start; j <= end; ++j) { if(j % 2) continue; @@ -215,7 +215,7 @@ u32bit karatsuba_size(u32bit z_size, /* * Pick a good size for the Karatsuba squaring */ -u32bit karatsuba_size(u32bit z_size, u32bit x_size, u32bit x_sw) +size_t karatsuba_size(size_t z_size, size_t x_size, size_t x_sw) { if(x_sw == x_size) { @@ -224,7 +224,7 @@ u32bit karatsuba_size(u32bit z_size, u32bit x_size, u32bit x_sw) return x_sw; } - for(u32bit j = x_sw; j <= x_size; ++j) + for(size_t j = x_sw; j <= x_size; ++j) { if(j % 2) continue; @@ -245,9 +245,9 @@ u32bit karatsuba_size(u32bit z_size, u32bit x_size, u32bit x_sw) /* * Multiplication Algorithm Dispatcher */ -void bigint_mul(word z[], u32bit z_size, word workspace[], - const word x[], u32bit x_size, u32bit x_sw, - const word y[], u32bit y_size, u32bit y_sw) +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) { if(x_sw == 1) { @@ -285,7 +285,7 @@ void bigint_mul(word z[], u32bit z_size, word workspace[], } else { - const u32bit N = karatsuba_size(z_size, x_size, x_sw, y_size, y_sw); + const size_t N = karatsuba_size(z_size, x_size, x_sw, y_size, y_sw); if(N) { @@ -300,8 +300,8 @@ void bigint_mul(word z[], u32bit z_size, word workspace[], /* * Squaring Algorithm Dispatcher */ -void bigint_sqr(word z[], u32bit z_size, word workspace[], - const word x[], u32bit x_size, u32bit x_sw) +void bigint_sqr(word z[], size_t z_size, word workspace[], + const word x[], size_t x_size, size_t x_sw) { if(x_sw == 1) { @@ -329,7 +329,7 @@ void bigint_sqr(word z[], u32bit z_size, word workspace[], } else { - const u32bit N = karatsuba_size(z_size, x_size, x_sw); + const size_t N = karatsuba_size(z_size, x_size, x_sw); if(N) { diff --git a/src/math/mp/mp_misc.cpp b/src/math/mp/mp_misc.cpp index 77b8e6f51..0232f01d6 100644 --- a/src/math/mp/mp_misc.cpp +++ b/src/math/mp/mp_misc.cpp @@ -15,7 +15,7 @@ extern "C" { /* * Core Division Operation */ -u32bit bigint_divcore(word q, word y2, word y1, +size_t bigint_divcore(word q, word y2, word y1, word x3, word x2, word x1) { // Compute (y2,y1) * q @@ -38,8 +38,8 @@ u32bit bigint_divcore(word q, word y2, word y1, /* * Compare two MP integers */ -s32bit bigint_cmp(const word x[], u32bit x_size, - const word y[], u32bit y_size) +s32bit bigint_cmp(const word x[], size_t x_size, + const word y[], size_t y_size) { if(x_size < y_size) { return (-bigint_cmp(y, y_size, x, x_size)); } @@ -50,7 +50,7 @@ s32bit bigint_cmp(const word x[], u32bit x_size, x_size--; } - for(u32bit j = x_size; j > 0; --j) + for(size_t j = x_size; j > 0; --j) { if(x[j-1] > y[j-1]) return 1; @@ -68,7 +68,7 @@ word bigint_divop(word n1, word n0, word d) { word high = n1 % d, quotient = 0; - for(u32bit j = 0; j != MP_WORD_BITS; ++j) + for(size_t j = 0; j != MP_WORD_BITS; ++j) { word high_top_bit = (high & MP_WORD_TOP_BIT); diff --git a/src/math/mp/mp_shift.cpp b/src/math/mp/mp_shift.cpp index f1d609bfb..0531658ec 100644 --- a/src/math/mp/mp_shift.cpp +++ b/src/math/mp/mp_shift.cpp @@ -15,11 +15,11 @@ extern "C" { /* * Single Operand Left Shift */ -void bigint_shl1(word x[], u32bit x_size, u32bit word_shift, u32bit bit_shift) +void bigint_shl1(word x[], size_t x_size, size_t word_shift, size_t bit_shift) { if(word_shift) { - for(u32bit j = 1; j != x_size + 1; ++j) + for(size_t j = 1; j != x_size + 1; ++j) x[(x_size - j) + word_shift] = x[x_size - j]; clear_mem(x, word_shift); } @@ -27,7 +27,7 @@ void bigint_shl1(word x[], u32bit x_size, u32bit word_shift, u32bit bit_shift) if(bit_shift) { word carry = 0; - for(u32bit j = word_shift; j != x_size + word_shift + 1; ++j) + for(size_t j = word_shift; j != x_size + word_shift + 1; ++j) { word temp = x[j]; x[j] = (temp << bit_shift) | carry; @@ -39,7 +39,7 @@ void bigint_shl1(word x[], u32bit x_size, u32bit word_shift, u32bit bit_shift) /* * Single Operand Right Shift */ -void bigint_shr1(word x[], u32bit x_size, u32bit word_shift, u32bit bit_shift) +void bigint_shr1(word x[], size_t x_size, size_t word_shift, size_t bit_shift) { if(x_size < word_shift) { @@ -57,7 +57,7 @@ void bigint_shr1(word x[], u32bit x_size, u32bit word_shift, u32bit bit_shift) { word carry = 0; - u32bit top = x_size - word_shift; + size_t top = x_size - word_shift; while(top >= 4) { @@ -94,15 +94,15 @@ void bigint_shr1(word x[], u32bit x_size, u32bit word_shift, u32bit bit_shift) /* * Two Operand Left Shift */ -void bigint_shl2(word y[], const word x[], u32bit x_size, - u32bit word_shift, u32bit bit_shift) +void bigint_shl2(word y[], const word x[], size_t x_size, + size_t word_shift, size_t bit_shift) { - for(u32bit j = 0; j != x_size; ++j) + for(size_t j = 0; j != x_size; ++j) y[j + word_shift] = x[j]; if(bit_shift) { word carry = 0; - for(u32bit j = word_shift; j != x_size + word_shift + 1; ++j) + for(size_t j = word_shift; j != x_size + word_shift + 1; ++j) { word w = y[j]; y[j] = (w << bit_shift) | carry; @@ -114,17 +114,17 @@ void bigint_shl2(word y[], const word x[], u32bit x_size, /* * Two Operand Right Shift */ -void bigint_shr2(word y[], const word x[], u32bit x_size, - u32bit word_shift, u32bit bit_shift) +void bigint_shr2(word y[], const word x[], size_t x_size, + size_t word_shift, size_t bit_shift) { if(x_size < word_shift) return; - for(u32bit j = 0; j != x_size - word_shift; ++j) + for(size_t j = 0; j != x_size - word_shift; ++j) y[j] = x[j + word_shift]; if(bit_shift) { word carry = 0; - for(u32bit j = x_size - word_shift; j > 0; --j) + for(size_t j = x_size - word_shift; j > 0; --j) { word w = y[j-1]; y[j-1] = (w >> bit_shift) | carry; diff --git a/src/math/mp/mp_types.h b/src/math/mp/mp_types.h index 1648713ed..e8723b5bf 100644 --- a/src/math/mp/mp_types.h +++ b/src/math/mp/mp_types.h @@ -17,7 +17,7 @@ namespace Botan { #elif (BOTAN_MP_WORD_BITS == 16) typedef u16bit word; #elif (BOTAN_MP_WORD_BITS == 32) - typedef u32bit word; + typedef size_t word; #elif (BOTAN_MP_WORD_BITS == 64) typedef u64bit word; #else diff --git a/src/math/mp/mulop_generic/mp_mulop.cpp b/src/math/mp/mulop_generic/mp_mulop.cpp index b6966ada7..e6a8ba891 100644 --- a/src/math/mp/mulop_generic/mp_mulop.cpp +++ b/src/math/mp/mulop_generic/mp_mulop.cpp @@ -17,23 +17,23 @@ extern "C" { /* * Simple O(N^2) Multiplication */ -void bigint_simple_mul(word z[], const word x[], u32bit x_size, - const word y[], u32bit y_size) +void bigint_simple_mul(word z[], const word x[], size_t x_size, + const word y[], size_t y_size) { - const u32bit x_size_8 = x_size - (x_size % 8); + const size_t x_size_8 = x_size - (x_size % 8); clear_mem(z, x_size + y_size); - for(u32bit i = 0; i != y_size; ++i) + for(size_t i = 0; i != y_size; ++i) { const word y_i = y[i]; word carry = 0; - for(u32bit j = 0; j != x_size_8; j += 8) + for(size_t j = 0; j != x_size_8; j += 8) carry = word8_madd3(z + i + j, x + j, y_i, carry); - for(u32bit j = x_size_8; j != x_size; ++j) + for(size_t j = x_size_8; j != x_size; ++j) z[i+j] = word_madd3(x[j], y_i, z[i+j], &carry); z[x_size+i] = carry; @@ -51,21 +51,21 @@ void bigint_simple_mul(word z[], const word x[], u32bit x_size, * Applied Cryptography, chapter 14 * */ -void bigint_simple_sqr(word z[], const word x[], u32bit x_size) +void bigint_simple_sqr(word z[], const word x[], size_t x_size) { - const u32bit x_size_8 = x_size - (x_size % 8); + const size_t x_size_8 = x_size - (x_size % 8); clear_mem(z, 2*x_size); - for(u32bit i = 0; i != x_size; ++i) + for(size_t i = 0; i != x_size; ++i) { const word x_i = x[i]; word carry = 0; - for(u32bit j = 0; j != x_size_8; j += 8) + for(size_t j = 0; j != x_size_8; j += 8) carry = word8_madd3(z + i + j, x + j, x_i, carry); - for(u32bit j = x_size_8; j != x_size; ++j) + for(size_t j = x_size_8; j != x_size; ++j) z[i+j] = word_madd3(x[j], x_i, z[i+j], &carry); z[x_size+i] = carry; diff --git a/src/math/numbertheory/curve_gfp.h b/src/math/numbertheory/curve_gfp.h index 3609ffd9d..f3c4dc1a1 100644 --- a/src/math/numbertheory/curve_gfp.h +++ b/src/math/numbertheory/curve_gfp.h @@ -91,7 +91,7 @@ class BOTAN_DLL CurveGFp /** * @return p.sig_words() */ - u32bit get_p_words() const { return p_words; } + size_t get_p_words() const { return p_words; } /** * @return modular reducer for p @@ -128,7 +128,7 @@ class BOTAN_DLL CurveGFp // Curve parameters BigInt p, a, b; - u32bit p_words; // cache of p.sig_words() + size_t p_words; // cache of p.sig_words() // Montgomery parameters BigInt r, r_inv, a_r; diff --git a/src/math/numbertheory/def_powm.h b/src/math/numbertheory/def_powm.h index ce128b965..a93db9b82 100644 --- a/src/math/numbertheory/def_powm.h +++ b/src/math/numbertheory/def_powm.h @@ -31,7 +31,7 @@ class Fixed_Window_Exponentiator : public Modular_Exponentiator private: Modular_Reducer reducer; BigInt exp; - u32bit window_bits; + size_t window_bits; std::vector<BigInt> g; Power_Mod::Usage_Hints hints; }; @@ -55,7 +55,7 @@ class Montgomery_Exponentiator : public Modular_Exponentiator BigInt R2, R_mod; std::vector<BigInt> g; word mod_prime; - u32bit mod_words, exp_bits, window_bits; + size_t mod_words, exp_bits, window_bits; Power_Mod::Usage_Hints hints; }; diff --git a/src/math/numbertheory/dsa_gen.cpp b/src/math/numbertheory/dsa_gen.cpp index e09de4b04..fcae7c619 100644 --- a/src/math/numbertheory/dsa_gen.cpp +++ b/src/math/numbertheory/dsa_gen.cpp @@ -19,7 +19,7 @@ namespace { /* * Check if this size is allowed by FIPS 186-3 */ -bool fips186_3_valid_size(u32bit pbits, u32bit qbits) +bool fips186_3_valid_size(size_t pbits, size_t qbits) { if(qbits == 160) return (pbits == 512 || pbits == 768 || pbits == 1024); @@ -41,7 +41,7 @@ bool fips186_3_valid_size(u32bit pbits, u32bit qbits) bool generate_dsa_primes(RandomNumberGenerator& rng, Algorithm_Factory& af, BigInt& p, BigInt& q, - u32bit pbits, u32bit qbits, + size_t pbits, size_t qbits, const MemoryRegion<byte>& seed_c) { if(!fips186_3_valid_size(pbits, qbits)) @@ -57,7 +57,7 @@ bool generate_dsa_primes(RandomNumberGenerator& rng, std::auto_ptr<HashFunction> hash( af.make_hash_function("SHA-" + to_string(qbits))); - const u32bit HASH_SIZE = hash->OUTPUT_LENGTH; + const size_t HASH_SIZE = hash->OUTPUT_LENGTH; class Seed { @@ -68,7 +68,7 @@ bool generate_dsa_primes(RandomNumberGenerator& rng, Seed& operator++() { - for(u32bit j = seed.size(); j > 0; --j) + for(size_t j = seed.size(); j > 0; --j) if(++seed[j-1]) break; return (*this); @@ -86,15 +86,15 @@ bool generate_dsa_primes(RandomNumberGenerator& rng, if(!check_prime(q, rng)) return false; - const u32bit n = (pbits-1) / (HASH_SIZE * 8), + const size_t n = (pbits-1) / (HASH_SIZE * 8), b = (pbits-1) % (HASH_SIZE * 8); BigInt X; SecureVector<byte> V(HASH_SIZE * (n+1)); - for(u32bit j = 0; j != 4096; ++j) + for(size_t j = 0; j != 4096; ++j) { - for(u32bit k = 0; k <= n; ++k) + for(size_t k = 0; k <= n; ++k) { ++seed; hash->update(seed); @@ -119,7 +119,7 @@ bool generate_dsa_primes(RandomNumberGenerator& rng, SecureVector<byte> generate_dsa_primes(RandomNumberGenerator& rng, Algorithm_Factory& af, BigInt& p, BigInt& q, - u32bit pbits, u32bit qbits) + size_t pbits, size_t qbits) { while(true) { diff --git a/src/math/numbertheory/jacobi.cpp b/src/math/numbertheory/jacobi.cpp index 2ad05ff71..fcccc80e5 100644 --- a/src/math/numbertheory/jacobi.cpp +++ b/src/math/numbertheory/jacobi.cpp @@ -34,7 +34,7 @@ s32bit jacobi(const BigInt& a, const BigInt& n) if(x.is_zero()) return 0; - u32bit shifts = low_zero_bits(x); + size_t shifts = low_zero_bits(x); x >>= shifts; if(shifts % 2) { diff --git a/src/math/numbertheory/make_prm.cpp b/src/math/numbertheory/make_prm.cpp index 59a5c2635..4fb3f908c 100644 --- a/src/math/numbertheory/make_prm.cpp +++ b/src/math/numbertheory/make_prm.cpp @@ -15,8 +15,8 @@ namespace Botan { * Generate a random prime */ BigInt random_prime(RandomNumberGenerator& rng, - u32bit bits, const BigInt& coprime, - u32bit equiv, u32bit modulo) + size_t bits, const BigInt& coprime, + size_t equiv, size_t modulo) { if(bits <= 1) throw Invalid_Argument("random_prime: Can't make a prime of " + @@ -47,13 +47,13 @@ BigInt random_prime(RandomNumberGenerator& rng, if(p % modulo != equiv) p += (modulo - p % modulo) + equiv; - const u32bit sieve_size = std::min(bits / 2, PRIME_TABLE_SIZE); - SecureVector<u32bit> sieve(sieve_size); + const size_t sieve_size = std::min(bits / 2, PRIME_TABLE_SIZE); + SecureVector<size_t> sieve(sieve_size); - for(u32bit j = 0; j != sieve.size(); ++j) + for(size_t j = 0; j != sieve.size(); ++j) sieve[j] = p % PRIMES[j]; - u32bit counter = 0; + size_t counter = 0; while(true) { if(counter == 4096 || p.bits() > bits) @@ -66,7 +66,7 @@ BigInt random_prime(RandomNumberGenerator& rng, if(p.bits() > bits) break; - for(u32bit j = 0; j != sieve.size(); ++j) + for(size_t j = 0; j != sieve.size(); ++j) { sieve[j] = (sieve[j] + modulo) % PRIMES[j]; if(sieve[j] == 0) @@ -84,7 +84,7 @@ BigInt random_prime(RandomNumberGenerator& rng, /* * Generate a random safe prime */ -BigInt random_safe_prime(RandomNumberGenerator& rng, u32bit bits) +BigInt random_safe_prime(RandomNumberGenerator& rng, size_t bits) { if(bits <= 64) throw Invalid_Argument("random_safe_prime: Can't make a prime of " + diff --git a/src/math/numbertheory/mp_numth.cpp b/src/math/numbertheory/mp_numth.cpp index 4edc694e5..23623b5f0 100644 --- a/src/math/numbertheory/mp_numth.cpp +++ b/src/math/numbertheory/mp_numth.cpp @@ -17,9 +17,9 @@ namespace Botan { */ BigInt square(const BigInt& x) { - const u32bit x_sw = x.sig_words(); + const size_t x_sw = x.sig_words(); - BigInt z(BigInt::Positive, round_up<u32bit>(2*x_sw, 16)); + BigInt z(BigInt::Positive, round_up<size_t>(2*x_sw, 16)); SecureVector<word> workspace(z.size()); bigint_sqr(z.get_reg(), z.size(), workspace, @@ -39,9 +39,9 @@ BigInt mul_add(const BigInt& a, const BigInt& b, const BigInt& c) if(a.sign() != b.sign()) sign = BigInt::Negative; - const u32bit a_sw = a.sig_words(); - const u32bit b_sw = b.sig_words(); - const u32bit c_sw = c.sig_words(); + const size_t a_sw = a.sig_words(); + const size_t b_sw = b.sig_words(); + const size_t c_sw = c.sig_words(); BigInt r(sign, std::max(a.size() + b.size(), c_sw) + 1); SecureVector<word> workspace(r.size()); @@ -49,7 +49,7 @@ BigInt mul_add(const BigInt& a, const BigInt& b, const BigInt& c) bigint_mul(r.get_reg(), r.size(), workspace, a.data(), a.size(), a_sw, b.data(), b.size(), b_sw); - const u32bit r_size = std::max(r.sig_words(), c_sw); + const size_t r_size = std::max(r.sig_words(), c_sw); bigint_add2(r.get_reg(), r_size, c.data(), c_sw); return r; } diff --git a/src/math/numbertheory/numthry.cpp b/src/math/numbertheory/numthry.cpp index 010a523ff..8018c1d2d 100644 --- a/src/math/numbertheory/numthry.cpp +++ b/src/math/numbertheory/numthry.cpp @@ -24,7 +24,7 @@ class MillerRabin_Test MillerRabin_Test(const BigInt& num); private: BigInt n, r, n_minus_1; - u32bit s; + size_t s; Fixed_Exponent_Power_Mod pow_mod; Modular_Reducer reducer; }; @@ -41,7 +41,7 @@ bool MillerRabin_Test::passes_test(const BigInt& a) if(y == 1 || y == n_minus_1) return true; - for(u32bit i = 1; i != s; ++i) + for(size_t i = 1; i != s; ++i) { y = reducer.square(y); @@ -73,9 +73,9 @@ MillerRabin_Test::MillerRabin_Test(const BigInt& num) /* * Miller-Rabin Iterations */ -u32bit miller_rabin_test_iterations(u32bit bits, u32bit level) +size_t miller_rabin_test_iterations(size_t bits, size_t level) { - struct mapping { u32bit bits; u32bit verify_iter; u32bit check_iter; }; + struct mapping { size_t bits; size_t verify_iter; size_t check_iter; }; static const mapping tests[] = { { 50, 55, 25 }, @@ -113,7 +113,7 @@ u32bit miller_rabin_test_iterations(u32bit bits, u32bit level) { 0, 0, 0 } }; - for(u32bit i = 0; tests[i].bits; ++i) + for(size_t i = 0; tests[i].bits; ++i) { if(bits <= tests[i].bits) { @@ -122,7 +122,7 @@ u32bit miller_rabin_test_iterations(u32bit bits, u32bit level) else if(level == 1) return tests[i].check_iter; else if(level == 0) - return std::max<u32bit>(tests[i].check_iter / 4, 1); + return std::max<size_t>(tests[i].check_iter / 4, 1); } } @@ -134,13 +134,13 @@ u32bit miller_rabin_test_iterations(u32bit bits, u32bit level) /* * Return the number of 0 bits at the end of n */ -u32bit low_zero_bits(const BigInt& n) +size_t low_zero_bits(const BigInt& n) { - u32bit low_zero = 0; + size_t low_zero = 0; if(n.is_positive() && n.is_nonzero()) { - for(u32bit i = 0; i != n.size(); ++i) + for(size_t i = 0; i != n.size(); ++i) { word x = n[i]; @@ -168,7 +168,7 @@ BigInt gcd(const BigInt& a, const BigInt& b) BigInt x = a, y = b; x.set_sign(BigInt::Positive); y.set_sign(BigInt::Positive); - u32bit shift = std::min(low_zero_bits(x), low_zero_bits(y)); + size_t shift = std::min(low_zero_bits(x), low_zero_bits(y)); x >>= shift; y >>= shift; @@ -210,9 +210,9 @@ BigInt inverse_mod(const BigInt& n, const BigInt& mod) while(u.is_nonzero()) { - u32bit zero_bits = low_zero_bits(u); + size_t zero_bits = low_zero_bits(u); u >>= zero_bits; - for(u32bit i = 0; i != zero_bits; ++i) + for(size_t i = 0; i != zero_bits; ++i) { if(A.is_odd() || B.is_odd()) { A += y; B -= x; } @@ -221,7 +221,7 @@ BigInt inverse_mod(const BigInt& n, const BigInt& mod) zero_bits = low_zero_bits(v); v >>= zero_bits; - for(u32bit i = 0; i != zero_bits; ++i) + for(size_t i = 0; i != zero_bits; ++i) { if(C.is_odd() || D.is_odd()) { C += y; D -= x; } @@ -257,9 +257,9 @@ BigInt power_mod(const BigInt& base, const BigInt& exp, const BigInt& mod) */ bool primality_test(const BigInt& n, RandomNumberGenerator& rng, - u32bit level) + size_t level) { - const u32bit PREF_NONCE_BITS = 64; + const size_t PREF_NONCE_BITS = 64; if(n == 2) return true; @@ -271,7 +271,7 @@ bool primality_test(const BigInt& n, { const word num = n.word_at(0); - for(u32bit i = 0; PRIMES[i]; ++i) + for(size_t i = 0; PRIMES[i]; ++i) { if(num == PRIMES[i]) return true; @@ -285,14 +285,14 @@ bool primality_test(const BigInt& n, if(level > 2) level = 2; - const u32bit NONCE_BITS = std::min(n.bits() - 2, PREF_NONCE_BITS); + const size_t NONCE_BITS = std::min(n.bits() - 2, PREF_NONCE_BITS); MillerRabin_Test mr(n); - const u32bit tests = miller_rabin_test_iterations(n.bits(), level); + const size_t tests = miller_rabin_test_iterations(n.bits(), level); BigInt nonce; - for(u32bit i = 0; i != tests; ++i) + for(size_t i = 0; i != tests; ++i) { while(nonce < 2 || nonce >= (n-1)) nonce.randomize(rng, NONCE_BITS); diff --git a/src/math/numbertheory/numthry.h b/src/math/numbertheory/numthry.h index 1ab64b038..750fbc78e 100644 --- a/src/math/numbertheory/numthry.h +++ b/src/math/numbertheory/numthry.h @@ -112,7 +112,7 @@ BigInt BOTAN_DLL ressol(const BigInt& x, const BigInt& p); * @return count of the zero bits in x, or, equivalently, the largest * value of n such that 2^n divides x evently */ -u32bit BOTAN_DLL low_zero_bits(const BigInt& x); +size_t BOTAN_DLL low_zero_bits(const BigInt& x); /** * Primality Testing @@ -123,7 +123,7 @@ u32bit BOTAN_DLL low_zero_bits(const BigInt& x); */ bool BOTAN_DLL primality_test(const BigInt& n, RandomNumberGenerator& rng, - u32bit level = 1); + size_t level = 1); /** * Quickly check for primality @@ -165,8 +165,8 @@ inline bool verify_prime(const BigInt& n, RandomNumberGenerator& rng) * @return random prime with the specified criteria */ BigInt BOTAN_DLL random_prime(RandomNumberGenerator& rng, - u32bit bits, const BigInt& coprime = 1, - u32bit equiv = 1, u32bit equiv_mod = 2); + size_t bits, const BigInt& coprime = 1, + size_t equiv = 1, size_t equiv_mod = 2); /** * Return a 'safe' prime, of the form p=2*q+1 with q prime @@ -175,7 +175,7 @@ BigInt BOTAN_DLL random_prime(RandomNumberGenerator& rng, * @return prime randomly chosen from safe primes of length bits */ BigInt BOTAN_DLL random_safe_prime(RandomNumberGenerator& rng, - u32bit bits); + size_t bits); class Algorithm_Factory; @@ -193,7 +193,7 @@ SecureVector<byte> BOTAN_DLL generate_dsa_primes(RandomNumberGenerator& rng, Algorithm_Factory& af, BigInt& p_out, BigInt& q_out, - u32bit pbits, u32bit qbits); + size_t pbits, size_t qbits); /** * Generate DSA parameters using the FIPS 186 kosherizer @@ -211,13 +211,13 @@ bool BOTAN_DLL generate_dsa_primes(RandomNumberGenerator& rng, Algorithm_Factory& af, BigInt& p_out, BigInt& q_out, - u32bit pbits, u32bit qbits, + size_t pbits, size_t qbits, const MemoryRegion<byte>& seed); /** * The size of the PRIMES[] array */ -const u32bit PRIME_TABLE_SIZE = 6541; +const size_t PRIME_TABLE_SIZE = 6541; /** * A const array of all primes less than 65535 diff --git a/src/math/numbertheory/point_gfp.cpp b/src/math/numbertheory/point_gfp.cpp index 56d4a145a..5da1959bc 100644 --- a/src/math/numbertheory/point_gfp.cpp +++ b/src/math/numbertheory/point_gfp.cpp @@ -45,7 +45,7 @@ void PointGFp::monty_mult(BigInt& z, } const BigInt& p = curve.get_p(); - const u32bit p_size = curve.get_p_words(); + const size_t p_size = curve.get_p_words(); const word p_dash = curve.get_p_dash(); SecureVector<word>& z_reg = z.get_reg(); @@ -75,7 +75,7 @@ void PointGFp::monty_sqr(BigInt& z, const BigInt& x, } const BigInt& p = curve.get_p(); - const u32bit p_size = curve.get_p_words(); + const size_t p_size = curve.get_p_words(); const word p_dash = curve.get_p_dash(); SecureVector<word>& z_reg = z.get_reg(); @@ -289,7 +289,7 @@ PointGFp operator*(const BigInt& scalar, const PointGFp& point) if(scalar.abs() <= 2) // special cases for small values { - u32bit value = scalar.abs().to_u32bit(); + byte value = scalar.abs().byte_at(0); PointGFp result = point; @@ -302,14 +302,14 @@ PointGFp operator*(const BigInt& scalar, const PointGFp& point) return result; } - const u32bit scalar_bits = scalar.bits(); + const size_t scalar_bits = scalar.bits(); - const u32bit window_size = 4; + const size_t window_size = 4; std::vector<PointGFp> Ps((1 << window_size) - 1); Ps[0] = point; - for(u32bit i = 1; i != Ps.size(); ++i) + for(size_t i = 1; i != Ps.size(); ++i) { Ps[i] = Ps[i-1]; @@ -320,14 +320,14 @@ PointGFp operator*(const BigInt& scalar, const PointGFp& point) } PointGFp H(curve); // create as zero - u32bit bits_left = scalar_bits; + size_t bits_left = scalar_bits; while(bits_left >= window_size) { - u32bit nibble = scalar.get_substring(bits_left - window_size, + size_t nibble = scalar.get_substring(bits_left - window_size, window_size); - for(u32bit i = 0; i != window_size; ++i) + for(size_t i = 0; i != window_size; ++i) H.mult2(ws); if(nibble) @@ -469,7 +469,7 @@ SecureVector<byte> EC2OSP(const PointGFp& point, byte format) if(point.is_zero()) return SecureVector<byte>(1); // single 0 byte - const u32bit p_bytes = point.get_curve().get_p().bytes(); + const size_t p_bytes = point.get_curve().get_p().bytes(); BigInt x = point.get_affine_x(); BigInt y = point.get_affine_y(); @@ -541,7 +541,7 @@ BigInt decompress_point(bool yMod2, } -PointGFp OS2ECP(const byte data[], u32bit data_len, +PointGFp OS2ECP(const byte data[], size_t data_len, const CurveGFp& curve) { if(data_len <= 1) @@ -561,7 +561,7 @@ PointGFp OS2ECP(const byte data[], u32bit data_len, } else if(pc == 4) { - const u32bit l = (data_len - 1) / 2; + const size_t l = (data_len - 1) / 2; // uncompressed form x = BigInt::decode(&data[1], l); @@ -569,7 +569,7 @@ PointGFp OS2ECP(const byte data[], u32bit data_len, } else if(pc == 6 || pc == 7) { - const u32bit l = (data_len - 1) / 2; + const size_t l = (data_len - 1) / 2; // hybrid form x = BigInt::decode(&data[1], l); diff --git a/src/math/numbertheory/point_gfp.h b/src/math/numbertheory/point_gfp.h index 42baa7d2c..35ec6d503 100644 --- a/src/math/numbertheory/point_gfp.h +++ b/src/math/numbertheory/point_gfp.h @@ -156,7 +156,7 @@ class BOTAN_DLL PointGFp class Workspace { public: - Workspace(u32bit p_words) : + Workspace(size_t p_words) : ws_monty(2*(p_words+2)), ws_bn(12) {} SecureVector<word> ws_monty; @@ -259,7 +259,7 @@ inline PointGFp operator*(const PointGFp& point, const BigInt& scalar) // encoding and decoding SecureVector<byte> BOTAN_DLL EC2OSP(const PointGFp& point, byte format); -PointGFp BOTAN_DLL OS2ECP(const byte data[], u32bit data_len, +PointGFp BOTAN_DLL OS2ECP(const byte data[], size_t data_len, const CurveGFp& curve); inline PointGFp OS2ECP(const MemoryRegion<byte>& data, const CurveGFp& curve) diff --git a/src/math/numbertheory/pow_mod.cpp b/src/math/numbertheory/pow_mod.cpp index 5ab5638ea..a66a1f7df 100644 --- a/src/math/numbertheory/pow_mod.cpp +++ b/src/math/numbertheory/pow_mod.cpp @@ -114,18 +114,18 @@ BigInt Power_Mod::execute() const /* * Try to choose a good window size */ -u32bit Power_Mod::window_bits(u32bit exp_bits, u32bit, +size_t Power_Mod::window_bits(size_t exp_bits, size_t, Power_Mod::Usage_Hints hints) { - static const u32bit wsize[][2] = { + static const size_t wsize[][2] = { { 2048, 7 }, { 1024, 6 }, { 256, 5 }, { 128, 4 }, { 64, 3 }, { 0, 0 } }; - u32bit window_bits = 1; + size_t window_bits = 1; if(exp_bits) { - for(u32bit j = 0; wsize[j][0]; ++j) + for(size_t j = 0; wsize[j][0]; ++j) { if(exp_bits >= wsize[j][0]) { @@ -154,8 +154,8 @@ Power_Mod::Usage_Hints choose_base_hints(const BigInt& b, const BigInt& n) return Power_Mod::Usage_Hints(Power_Mod::BASE_IS_2 | Power_Mod::BASE_IS_SMALL); - const u32bit b_bits = b.bits(); - const u32bit n_bits = n.bits(); + const size_t b_bits = b.bits(); + const size_t n_bits = n.bits(); if(b_bits < n_bits / 32) return Power_Mod::BASE_IS_SMALL; @@ -170,8 +170,8 @@ Power_Mod::Usage_Hints choose_base_hints(const BigInt& b, const BigInt& n) */ Power_Mod::Usage_Hints choose_exp_hints(const BigInt& e, const BigInt& n) { - const u32bit e_bits = e.bits(); - const u32bit n_bits = n.bits(); + const size_t e_bits = e.bits(); + const size_t n_bits = n.bits(); if(e_bits < n_bits / 32) return Power_Mod::BASE_IS_SMALL; diff --git a/src/math/numbertheory/pow_mod.h b/src/math/numbertheory/pow_mod.h index 1a60ca05f..7ec237d72 100644 --- a/src/math/numbertheory/pow_mod.h +++ b/src/math/numbertheory/pow_mod.h @@ -48,7 +48,7 @@ class BOTAN_DLL Power_Mod /* * Try to choose a good window size */ - static u32bit window_bits(u32bit exp_bits, u32bit base_bits, + static size_t window_bits(size_t exp_bits, size_t base_bits, Power_Mod::Usage_Hints hints); void set_modulus(const BigInt&, Usage_Hints = NO_HINTS) const; diff --git a/src/math/numbertheory/powm_fw.cpp b/src/math/numbertheory/powm_fw.cpp index 68dabc332..afc53f233 100644 --- a/src/math/numbertheory/powm_fw.cpp +++ b/src/math/numbertheory/powm_fw.cpp @@ -28,7 +28,7 @@ void Fixed_Window_Exponentiator::set_base(const BigInt& base) g.resize((1 << window_bits) - 1); g[0] = base; - for(u32bit j = 1; j != g.size(); ++j) + for(size_t j = 1; j != g.size(); ++j) g[j] = reducer.multiply(g[j-1], g[0]); } @@ -37,15 +37,15 @@ void Fixed_Window_Exponentiator::set_base(const BigInt& base) */ BigInt Fixed_Window_Exponentiator::execute() const { - const u32bit exp_nibbles = (exp.bits() + window_bits - 1) / window_bits; + const size_t exp_nibbles = (exp.bits() + window_bits - 1) / window_bits; BigInt x = 1; - for(u32bit j = exp_nibbles; j > 0; --j) + for(size_t j = exp_nibbles; j > 0; --j) { - for(u32bit k = 0; k != window_bits; ++k) + for(size_t k = 0; k != window_bits; ++k) x = reducer.square(x); - u32bit nibble = exp.get_substring(window_bits*(j-1), window_bits); + size_t nibble = exp.get_substring(window_bits*(j-1), window_bits); if(nibble) x = reducer.multiply(x, g[nibble-1]); } diff --git a/src/math/numbertheory/powm_mnt.cpp b/src/math/numbertheory/powm_mnt.cpp index 8b915390c..038ce14da 100644 --- a/src/math/numbertheory/powm_mnt.cpp +++ b/src/math/numbertheory/powm_mnt.cpp @@ -44,12 +44,12 @@ void Montgomery_Exponentiator::set_base(const BigInt& base) g[0].get_reg().set(&z[0], mod_words + 1); const BigInt& x = g[0]; - const u32bit x_sig = x.sig_words(); + const size_t x_sig = x.sig_words(); - for(u32bit i = 1; i != g.size(); ++i) + for(size_t i = 1; i != g.size(); ++i) { const BigInt& y = g[i-1]; - const u32bit y_sig = y.sig_words(); + const size_t y_sig = y.sig_words(); zeroise(z); bigint_mul(&z[0], z.size(), &workspace[0], @@ -69,15 +69,15 @@ void Montgomery_Exponentiator::set_base(const BigInt& base) */ BigInt Montgomery_Exponentiator::execute() const { - const u32bit exp_nibbles = (exp_bits + window_bits - 1) / window_bits; + const size_t exp_nibbles = (exp_bits + window_bits - 1) / window_bits; BigInt x = R_mod; SecureVector<word> z(2 * (mod_words + 1)); SecureVector<word> workspace(2 * (mod_words + 1)); - for(u32bit i = exp_nibbles; i > 0; --i) + for(size_t i = exp_nibbles; i > 0; --i) { - for(u32bit k = 0; k != window_bits; ++k) + for(size_t k = 0; k != window_bits; ++k) { zeroise(z); bigint_sqr(&z[0], z.size(), &workspace[0], @@ -90,7 +90,7 @@ BigInt Montgomery_Exponentiator::execute() const x.get_reg().set(&z[0], mod_words + 1); } - u32bit nibble = exp.get_substring(window_bits*(i-1), window_bits); + size_t nibble = exp.get_substring(window_bits*(i-1), window_bits); if(nibble) { const BigInt& y = g[nibble-1]; diff --git a/src/math/numbertheory/reducer.h b/src/math/numbertheory/reducer.h index 861983ef0..05c12a440 100644 --- a/src/math/numbertheory/reducer.h +++ b/src/math/numbertheory/reducer.h @@ -53,7 +53,7 @@ class BOTAN_DLL Modular_Reducer Modular_Reducer(const BigInt& mod); private: BigInt modulus, modulus_2, mu; - u32bit mod_words, mod2_words, mu_words; + size_t mod_words, mod2_words, mu_words; }; } diff --git a/src/math/numbertheory/ressol.cpp b/src/math/numbertheory/ressol.cpp index 4696168b8..2e01406f8 100644 --- a/src/math/numbertheory/ressol.cpp +++ b/src/math/numbertheory/ressol.cpp @@ -32,7 +32,7 @@ BigInt ressol(const BigInt& a, const BigInt& p) if(p % 4 == 3) return power_mod(a, ((p+1) >> 2), p); - u32bit s = low_zero_bits(p - 1); + size_t s = low_zero_bits(p - 1); BigInt q = p >> s; q -= 1; @@ -58,7 +58,7 @@ BigInt ressol(const BigInt& a, const BigInt& p) { q = n; - u32bit i = 0; + size_t i = 0; while(q != 1) { q = mod_p.square(q); diff --git a/src/utils/types.h b/src/utils/types.h index 58fdb5ff8..c14149779 100644 --- a/src/utils/types.h +++ b/src/utils/types.h @@ -9,6 +9,7 @@ #define BOTAN_TYPES_H__ #include <botan/build.h> +#include <stddef.h> /** * The primary namespace for the botan library |