Botan  1.11.15
src/lib/math/numbertheory/reducer.h
Go to the documentation of this file.
00001 /*
00002 * Modular Reducer
00003 * (C) 1999-2010 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #ifndef BOTAN_MODULAR_REDUCER_H__
00009 #define BOTAN_MODULAR_REDUCER_H__
00010 
00011 #include <botan/numthry.h>
00012 
00013 namespace Botan {
00014 
00015 /**
00016 * Modular Reducer (using Barrett's technique)
00017 */
00018 class BOTAN_DLL Modular_Reducer
00019    {
00020    public:
00021       const BigInt& get_modulus() const { return modulus; }
00022 
00023       BigInt reduce(const BigInt& x) const;
00024 
00025       /**
00026       * Multiply mod p
00027       * @param x
00028       * @param y
00029       * @return (x * y) % p
00030       */
00031       BigInt multiply(const BigInt& x, const BigInt& y) const
00032          { return reduce(x * y); }
00033 
00034       /**
00035       * Square mod p
00036       * @param x
00037       * @return (x * x) % p
00038       */
00039       BigInt square(const BigInt& x) const
00040          { return reduce(Botan::square(x)); }
00041 
00042       /**
00043       * Cube mod p
00044       * @param x
00045       * @return (x * x * x) % p
00046       */
00047       BigInt cube(const BigInt& x) const
00048          { return multiply(x, this->square(x)); }
00049 
00050       bool initialized() const { return (mod_words != 0); }
00051 
00052       Modular_Reducer() { mod_words = 0; }
00053       Modular_Reducer(const BigInt& mod);
00054    private:
00055       BigInt modulus, modulus_2, mu;
00056       size_t mod_words;
00057    };
00058 
00059 }
00060 
00061 #endif