Botan  1.11.15
Public Member Functions
Botan::Modular_Reducer Class Reference

#include <reducer.h>

List of all members.

Public Member Functions

BigInt cube (const BigInt &x) const
const BigIntget_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

Detailed Description

Modular Reducer (using Barrett's technique)

Definition at line 18 of file reducer.h.


Constructor & Destructor Documentation

Definition at line 52 of file reducer.h.

{ mod_words = 0; }

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;
   }

Member Function Documentation

BigInt Botan::Modular_Reducer::cube ( const BigInt x) const [inline]

Cube mod p

Parameters:
x
Returns:
(x * x * x) % p

Definition at line 47 of file reducer.h.

References Botan::square().

         { return multiply(x, this->square(x)); }
const BigInt& Botan::Modular_Reducer::get_modulus ( ) const [inline]

Definition at line 21 of file reducer.h.

{ return modulus; }
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

Parameters:
x
y
Returns:
(x * y) % p

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().

         { return reduce(x * y); }

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

Parameters:
x
Returns:
(x * x) % p

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)); }

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