/*************************************************
* Modular Reducer Source File                    *
* (C) 1999-2003 The Botan Project                *
*************************************************/

#include <botan/reducer.h>
#include <botan/barrett.h>

#if defined(BOTAN_EXT_GNU_MP_REDUCER)
  #include <botan/gmp_redu.h>
#endif

namespace Botan {

/*************************************************
* Construct a ModularReducer                     *
*************************************************/
ModularReducer::ModularReducer(const BigInt& n) : modulus(n)
   {
   if(modulus <= 0)
      throw Invalid_Argument("ModularReducer: modulus must be positive");
   if(modulus.size() > 8 && !power_of_2(modulus.size()))
      modulus.grow_reg((1 << high_bit(modulus.size())) - modulus.size());
   }

/*************************************************
* Modular Multiplication                         *
*************************************************/
BigInt ModularReducer::multiply(const BigInt& a, const BigInt& b) const
   {
   return reduce(a * b);
   }

/*************************************************
* Modular Squaring                               *
*************************************************/
BigInt ModularReducer::square(const BigInt& n) const
   {
   return multiply(n, n);
   }

/*************************************************
* Get a modular reducer for n                    *
*************************************************/
ModularReducer* get_reducer(const BigInt& n)
   {
#if defined(BOTAN_EXT_GNU_MP_REDUCER)
   return new GMP_Reducer(n);
#else
   return new BarrettReducer(n);
#endif
   }

}
