Botan
1.11.15
|
#include <reducer.h>
Public Member Functions | |
BigInt | cube (const BigInt &x) const |
const BigInt & | get_modulus () const |
bool | initialized () const |
Modular_Reducer () | |
Modular_Reducer (const BigInt &mod) | |
BigInt | multiply (const BigInt &x, const BigInt &y) const |
BigInt | reduce (const BigInt &x) const |
BigInt | square (const BigInt &x) const |
Botan::Modular_Reducer::Modular_Reducer | ( | ) | [inline] |
Botan::Modular_Reducer::Modular_Reducer | ( | const BigInt & | mod | ) |
Definition at line 16 of file reducer.cpp.
References Botan::MP_WORD_BITS, Botan::BigInt::power_of_2(), Botan::BigInt::sig_words(), and square().
{ if(mod <= 0) throw Invalid_Argument("Modular_Reducer: modulus must be positive"); modulus = mod; mod_words = modulus.sig_words(); modulus_2 = Botan::square(modulus); mu = BigInt::power_of_2(2 * MP_WORD_BITS * mod_words) / modulus; }
BigInt Botan::Modular_Reducer::cube | ( | const BigInt & | x | ) | const [inline] |
const BigInt& Botan::Modular_Reducer::get_modulus | ( | ) | const [inline] |
bool Botan::Modular_Reducer::initialized | ( | ) | const [inline] |
Definition at line 50 of file reducer.h.
Referenced by Botan::Blinder::blind(), and Botan::Blinder::unblind().
{ return (mod_words != 0); }
BigInt Botan::Modular_Reducer::multiply | ( | const BigInt & | x, |
const BigInt & | y | ||
) | const [inline] |
Multiply mod p
x | |
y |
Definition at line 31 of file reducer.h.
Referenced by Botan::Blinder::blind(), Botan::Fixed_Window_Exponentiator::execute(), Botan::ressol(), Botan::Fixed_Window_Exponentiator::set_base(), and Botan::Blinder::unblind().
BigInt Botan::Modular_Reducer::reduce | ( | const BigInt & | x | ) | const |
Definition at line 32 of file reducer.cpp.
References Botan::BigInt::cmp(), Botan::BigInt::is_negative(), Botan::BigInt::is_positive(), Botan::BigInt::mask_bits(), Botan::MP_WORD_BITS, Botan::BigInt::Positive, Botan::BigInt::power_of_2(), Botan::BigInt::set_sign(), and x.
{ if(mod_words == 0) throw Invalid_State("Modular_Reducer: Never initalized"); if(x.cmp(modulus, false) < 0) { if(x.is_negative()) return x + modulus; // make positive return x; } else if(x.cmp(modulus_2, false) < 0) { BigInt t1 = x; t1.set_sign(BigInt::Positive); t1 >>= (MP_WORD_BITS * (mod_words - 1)); t1 *= mu; t1 >>= (MP_WORD_BITS * (mod_words + 1)); t1 *= modulus; t1.mask_bits(MP_WORD_BITS * (mod_words + 1)); BigInt t2 = x; t2.set_sign(BigInt::Positive); t2.mask_bits(MP_WORD_BITS * (mod_words + 1)); t2 -= t1; if(t2.is_negative()) { t2 += BigInt::power_of_2(MP_WORD_BITS * (mod_words + 1)); } while(t2 >= modulus) t2 -= modulus; if(x.is_positive()) return t2; else return (modulus - t2); } else { // too big, fall back to normal division return (x % modulus); } }
BigInt Botan::Modular_Reducer::square | ( | const BigInt & | x | ) | const [inline] |
Square mod p
x |
Definition at line 39 of file reducer.h.
References Botan::square().
Referenced by Botan::Blinder::blind(), Botan::Fixed_Window_Exponentiator::execute(), Modular_Reducer(), and Botan::ressol().
{ return reduce(Botan::square(x)); }