Botan  1.11.15
src/lib/math/numbertheory/pow_mod.h
Go to the documentation of this file.
00001 /*
00002 * Modular Exponentiator
00003 * (C) 1999-2007 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #ifndef BOTAN_POWER_MOD_H__
00009 #define BOTAN_POWER_MOD_H__
00010 
00011 #include <botan/bigint.h>
00012 
00013 namespace Botan {
00014 
00015 /**
00016 * Modular Exponentiator Interface
00017 */
00018 class BOTAN_DLL Modular_Exponentiator
00019    {
00020    public:
00021       virtual void set_base(const BigInt&) = 0;
00022       virtual void set_exponent(const BigInt&) = 0;
00023       virtual BigInt execute() const = 0;
00024       virtual Modular_Exponentiator* copy() const = 0;
00025       virtual ~Modular_Exponentiator() {}
00026    };
00027 
00028 /**
00029 * Modular Exponentiator Proxy
00030 */
00031 class BOTAN_DLL Power_Mod
00032    {
00033    public:
00034 
00035       enum Usage_Hints {
00036          NO_HINTS        = 0x0000,
00037 
00038          BASE_IS_FIXED   = 0x0001,
00039          BASE_IS_SMALL   = 0x0002,
00040          BASE_IS_LARGE   = 0x0004,
00041          BASE_IS_2       = 0x0008,
00042 
00043          EXP_IS_FIXED    = 0x0100,
00044          EXP_IS_SMALL    = 0x0200,
00045          EXP_IS_LARGE    = 0x0400
00046       };
00047 
00048       /*
00049       * Try to choose a good window size
00050       */
00051       static size_t window_bits(size_t exp_bits, size_t base_bits,
00052                                 Power_Mod::Usage_Hints hints);
00053 
00054       void set_modulus(const BigInt&, Usage_Hints = NO_HINTS) const;
00055       void set_base(const BigInt&) const;
00056       void set_exponent(const BigInt&) const;
00057 
00058       BigInt execute() const;
00059 
00060       Power_Mod& operator=(const Power_Mod&);
00061 
00062       Power_Mod(const BigInt& = 0, Usage_Hints = NO_HINTS);
00063       Power_Mod(const Power_Mod&);
00064       virtual ~Power_Mod();
00065    private:
00066       mutable Modular_Exponentiator* core;
00067    };
00068 
00069 /**
00070 * Fixed Exponent Modular Exponentiator Proxy
00071 */
00072 class BOTAN_DLL Fixed_Exponent_Power_Mod : public Power_Mod
00073    {
00074    public:
00075       BigInt operator()(const BigInt& b) const
00076          { set_base(b); return execute(); }
00077 
00078       Fixed_Exponent_Power_Mod() {}
00079 
00080       Fixed_Exponent_Power_Mod(const BigInt& exponent,
00081                                const BigInt& modulus,
00082                                Usage_Hints hints = NO_HINTS);
00083    };
00084 
00085 /**
00086 * Fixed Base Modular Exponentiator Proxy
00087 */
00088 class BOTAN_DLL Fixed_Base_Power_Mod : public Power_Mod
00089    {
00090    public:
00091       BigInt operator()(const BigInt& e) const
00092          { set_exponent(e); return execute(); }
00093 
00094       Fixed_Base_Power_Mod() {}
00095 
00096       Fixed_Base_Power_Mod(const BigInt& base,
00097                            const BigInt& modulus,
00098                            Usage_Hints hints = NO_HINTS);
00099    };
00100 
00101 }
00102 
00103 #endif