monotone

monotone Mtn Source Tree

Root/cryptopp/modarith.h

1#ifndef CRYPTOPP_MODARITH_H
2#define CRYPTOPP_MODARITH_H
3
4// implementations are in integer.cpp
5
6#include "cryptlib.h"
7#include "misc.h"
8#include "integer.h"
9#include "algebra.h"
10
11NAMESPACE_BEGIN(CryptoPP)
12
13//! .
14class ModularArithmetic : public AbstractRing<Integer>
15{
16public:
17
18typedef int RandomizationParameter;
19typedef Integer Element;
20
21ModularArithmetic(const Integer &modulus = Integer::One())
22: modulus(modulus), result((word)0, modulus.reg.size()) {}
23
24ModularArithmetic(const ModularArithmetic &ma)
25: modulus(ma.modulus), result((word)0, modulus.reg.size()) {}
26
27ModularArithmetic(BufferedTransformation &bt);// construct from BER encoded parameters
28
29virtual ModularArithmetic * Clone() const {return new ModularArithmetic(*this);}
30
31void DEREncode(BufferedTransformation &bt) const;
32
33void DEREncodeElement(BufferedTransformation &out, const Element &a) const;
34void BERDecodeElement(BufferedTransformation &in, Element &a) const;
35
36const Integer& GetModulus() const {return modulus;}
37void SetModulus(const Integer &newModulus) {modulus = newModulus; result.reg.resize(modulus.reg.size());}
38
39virtual bool IsMontgomeryRepresentation() const {return false;}
40
41virtual Integer ConvertIn(const Integer &a) const
42{return a%modulus;}
43
44virtual Integer ConvertOut(const Integer &a) const
45{return a;}
46
47const Integer& Half(const Integer &a) const;
48
49bool Equal(const Integer &a, const Integer &b) const
50{return a==b;}
51
52const Integer& Identity() const
53{return Integer::Zero();}
54
55const Integer& Add(const Integer &a, const Integer &b) const;
56
57Integer& Accumulate(Integer &a, const Integer &b) const;
58
59const Integer& Inverse(const Integer &a) const;
60
61const Integer& Subtract(const Integer &a, const Integer &b) const;
62
63Integer& Reduce(Integer &a, const Integer &b) const;
64
65const Integer& Double(const Integer &a) const
66{return Add(a, a);}
67
68const Integer& MultiplicativeIdentity() const
69{return Integer::One();}
70
71const Integer& Multiply(const Integer &a, const Integer &b) const
72{return result1 = a*b%modulus;}
73
74const Integer& Square(const Integer &a) const
75{return result1 = a.Squared()%modulus;}
76
77bool IsUnit(const Integer &a) const
78{return Integer::Gcd(a, modulus).IsUnit();}
79
80const Integer& MultiplicativeInverse(const Integer &a) const
81{return result1 = a.InverseMod(modulus);}
82
83const Integer& Divide(const Integer &a, const Integer &b) const
84{return Multiply(a, MultiplicativeInverse(b));}
85
86Integer CascadeExponentiate(const Integer &x, const Integer &e1, const Integer &y, const Integer &e2) const;
87
88void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const;
89
90unsigned int MaxElementBitLength() const
91{return (modulus-1).BitCount();}
92
93unsigned int MaxElementByteLength() const
94{return (modulus-1).ByteCount();}
95
96Element RandomElement( RandomNumberGenerator &rng , const RandomizationParameter &ignore_for_now = 0 ) const
97// left RandomizationParameter arg as ref in case RandomizationParameter becomes a more complicated struct
98{
99return Element( rng , Integer( (long) 0) , modulus - Integer( (long) 1 ) ) ;
100}
101
102static const RandomizationParameter DefaultRandomizationParameter ;
103
104protected:
105Integer modulus;
106mutable Integer result, result1;
107
108};
109
110// const ModularArithmetic::RandomizationParameter ModularArithmetic::DefaultRandomizationParameter = 0 ;
111
112//! do modular arithmetics in Montgomery representation for increased speed
113class MontgomeryRepresentation : public ModularArithmetic
114{
115public:
116MontgomeryRepresentation(const Integer &modulus);// modulus must be odd
117
118virtual ModularArithmetic * Clone() const {return new MontgomeryRepresentation(*this);}
119
120bool IsMontgomeryRepresentation() const {return true;}
121
122Integer ConvertIn(const Integer &a) const
123{return (a<<(WORD_BITS*modulus.reg.size()))%modulus;}
124
125Integer ConvertOut(const Integer &a) const;
126
127const Integer& MultiplicativeIdentity() const
128{return result1 = Integer::Power2(WORD_BITS*modulus.reg.size())%modulus;}
129
130const Integer& Multiply(const Integer &a, const Integer &b) const;
131
132const Integer& Square(const Integer &a) const;
133
134const Integer& MultiplicativeInverse(const Integer &a) const;
135
136Integer CascadeExponentiate(const Integer &x, const Integer &e1, const Integer &y, const Integer &e2) const
137{return AbstractRing<Integer>::CascadeExponentiate(x, e1, y, e2);}
138
139void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const
140{AbstractRing<Integer>::SimultaneousExponentiate(results, base, exponents, exponentsCount);}
141
142private:
143Integer u;
144mutable SecAlignedWordBlock workspace;
145};
146
147NAMESPACE_END
148
149#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status