Fabcoin Core  0.16.2
P2P Digital Currency
modarith.h
Go to the documentation of this file.
1 // modarith.h - written and placed in the public domain by Wei Dai
2 
5 
6 #ifndef CRYPTOPP_MODARITH_H
7 #define CRYPTOPP_MODARITH_H
8 
9 // implementations are in integer.cpp
10 
11 #include "cryptlib.h"
12 #include "integer.h"
13 #include "algebra.h"
14 #include "secblock.h"
15 #include "misc.h"
16 
18 
22 
35 {
36 public:
37 
39  typedef Integer Element;
40 
41  virtual ~ModularArithmetic() {}
42 
45  ModularArithmetic(const Integer &modulus = Integer::One())
46  : AbstractRing<Integer>(), m_modulus(modulus), m_result((word)0, modulus.reg.size()) {}
47 
51  : AbstractRing<Integer>(), m_modulus(ma.m_modulus), m_result((word)0, ma.m_modulus.reg.size()) {}
52 
55  ModularArithmetic(BufferedTransformation &bt); // construct from BER encoded parameters
56 
61  virtual ModularArithmetic * Clone() const {return new ModularArithmetic(*this);}
62 
65  void DEREncode(BufferedTransformation &bt) const;
66 
70  void DEREncodeElement(BufferedTransformation &out, const Element &a) const;
71 
75  void BERDecodeElement(BufferedTransformation &in, Element &a) const;
76 
79  const Integer& GetModulus() const {return m_modulus;}
80 
83  void SetModulus(const Integer &newModulus)
84  {m_modulus = newModulus; m_result.reg.resize(m_modulus.reg.size());}
85 
88  virtual bool IsMontgomeryRepresentation() const {return false;}
89 
95  virtual Integer ConvertIn(const Integer &a) const
96  {return a%m_modulus;}
97 
103  virtual Integer ConvertOut(const Integer &a) const
104  {return a;}
105 
108  const Integer& Half(const Integer &a) const;
109 
115  bool Equal(const Integer &a, const Integer &b) const
116  {return a==b;}
117 
120  const Integer& Identity() const
121  {return Integer::Zero();}
122 
127  const Integer& Add(const Integer &a, const Integer &b) const;
128 
133  Integer& Accumulate(Integer &a, const Integer &b) const;
134 
138  const Integer& Inverse(const Integer &a) const;
139 
144  const Integer& Subtract(const Integer &a, const Integer &b) const;
145 
150  Integer& Reduce(Integer &a, const Integer &b) const;
151 
156  const Integer& Double(const Integer &a) const
157  {return Add(a, a);}
158 
162  const Integer& MultiplicativeIdentity() const
163  {return Integer::One();}
164 
170  const Integer& Multiply(const Integer &a, const Integer &b) const
171  {return m_result1 = a*b%m_modulus;}
172 
177  const Integer& Square(const Integer &a) const
178  {return m_result1 = a.Squared()%m_modulus;}
179 
183  bool IsUnit(const Integer &a) const
184  {return Integer::Gcd(a, m_modulus).IsUnit();}
185 
190  const Integer& MultiplicativeInverse(const Integer &a) const
191  {return m_result1 = a.InverseMod(m_modulus);}
192 
198  const Integer& Divide(const Integer &a, const Integer &b) const
199  {return Multiply(a, MultiplicativeInverse(b));}
200 
207  Integer CascadeExponentiate(const Integer &x, const Integer &e1, const Integer &y, const Integer &e2) const;
208 
219  void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const;
220 
223  unsigned int MaxElementBitLength() const
224  {return (m_modulus-1).BitCount();}
225 
228  unsigned int MaxElementByteLength() const
229  {return (m_modulus-1).ByteCount();}
230 
238  Element RandomElement(RandomNumberGenerator &rng , const RandomizationParameter &ignore_for_now = 0) const
239  // left RandomizationParameter arg as ref in case RandomizationParameter becomes a more complicated struct
240  {
241  CRYPTOPP_UNUSED(ignore_for_now);
242  return Element(rng, Integer::Zero(), m_modulus - Integer::One()) ;
243  }
244 
249  bool operator==(const ModularArithmetic &rhs) const
250  {return m_modulus == rhs.m_modulus;}
251 
252  static const RandomizationParameter DefaultRandomizationParameter ;
253 
254 protected:
255  Integer m_modulus;
256  mutable Integer m_result, m_result1;
257 };
258 
259 // const ModularArithmetic::RandomizationParameter ModularArithmetic::DefaultRandomizationParameter = 0 ;
260 
272 {
273 public:
275 
279  MontgomeryRepresentation(const Integer &modulus);
280 
285  virtual ModularArithmetic * Clone() const {return new MontgomeryRepresentation(*this);}
286 
287  bool IsMontgomeryRepresentation() const {return true;}
288 
289  Integer ConvertIn(const Integer &a) const
290  {return (a<<(WORD_BITS*m_modulus.reg.size()))%m_modulus;}
291 
292  Integer ConvertOut(const Integer &a) const;
293 
294  const Integer& MultiplicativeIdentity() const
295  {return m_result1 = Integer::Power2(WORD_BITS*m_modulus.reg.size())%m_modulus;}
296 
297  const Integer& Multiply(const Integer &a, const Integer &b) const;
298 
299  const Integer& Square(const Integer &a) const;
300 
301  const Integer& MultiplicativeInverse(const Integer &a) const;
302 
303  Integer CascadeExponentiate(const Integer &x, const Integer &e1, const Integer &y, const Integer &e2) const
304  {return AbstractRing<Integer>::CascadeExponentiate(x, e1, y, e2);}
305 
306  void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const
307  {AbstractRing<Integer>::SimultaneousExponentiate(results, base, exponents, exponentsCount);}
308 
309 private:
310  Integer m_u;
312 };
313 
315 
316 #endif
bool IsUnit(const Integer &a) const
Determines whether an element is a unit in the ring.
Definition: modarith.h:183
const Integer & GetModulus() const
Retrieves the modulus.
Definition: modarith.h:79
Utility functions for the Crypto++ library.
virtual ModularArithmetic * Clone() const
Clone a ModularArithmetic.
Definition: modarith.h:61
static Integer CRYPTOPP_API Gcd(const Integer &a, const Integer &n)
greatest common divisor
Definition: integer.cpp:4365
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
#define CRYPTOPP_DLL_TEMPLATE_CLASS
Definition: config.h:720
const Integer & MultiplicativeIdentity() const
Retrieves the multiplicative identity.
Definition: modarith.h:162
Abstract base classes that provide a uniform interface to this library.
const Integer & MultiplicativeInverse(const Integer &a) const
Calculate the multiplicative inverse of an element in the ring.
Definition: modarith.h:190
Abstract Euclidean domain.
Definition: algebra.h:276
const Integer & Square(const Integer &a) const
Square an element in the ring.
Definition: modarith.h:177
Ring of congruence classes modulo n.
Definition: modarith.h:34
Interface for random number generators.
Definition: cryptlib.h:1188
Element RandomElement(RandomNumberGenerator &rng, const RandomizationParameter &ignore_for_now=0) const
Provides a random element in the ring.
Definition: modarith.h:238
int Add(word *C, const word *A, const word *B, size_t N)
Definition: integer.cpp:2143
Integer Element
Definition: modarith.h:39
virtual void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const
Exponentiates a base to multiple exponents in the Ring.
Definition: algebra.cpp:334
Classes for performing mathematics over different fields.
Interface for buffered transformations.
Definition: cryptlib.h:1352
static const Integer &CRYPTOPP_API One()
Integer representing 1.
Definition: integer.cpp:3035
Abstract ring.
Definition: algebra.h:118
virtual ModularArithmetic * Clone() const
Clone a MontgomeryRepresentation.
Definition: modarith.h:285
Classes and functions for secure memory allocations.
bool IsUnit() const
is 1 or -1
Definition: integer.cpp:4344
virtual Integer ConvertIn(const Integer &a) const
Reduces an element in the congruence class.
Definition: modarith.h:95
#define a(i)
virtual Integer ConvertOut(const Integer &a) const
Reduces an element in the congruence class.
Definition: modarith.h:103
const Integer & Multiply(const Integer &a, const Integer &b) const
Multiplies elements in the ring.
Definition: modarith.h:170
#define x(i)
const Integer & MultiplicativeIdentity() const
Retrieves the multiplicative identity.
Definition: modarith.h:294
IntegerSecBlock m_workspace
Definition: modarith.h:311
const unsigned int WORD_BITS
Definition: config.h:317
Integer Squared() const
Multiply this integer by itself.
Definition: integer.h:570
Integer m_modulus
Definition: modarith.h:255
static Integer CRYPTOPP_API Power2(size_t e)
Exponentiates to a power of 2.
Definition: integer.cpp:3008
unsigned int MaxElementBitLength() const
Provides the maximum bit size of an element in the ring.
Definition: modarith.h:223
virtual ~MontgomeryRepresentation()
Definition: modarith.h:274
Multiple precision integer with arithmetic operations.
Definition: integer.h:43
const Integer & Double(const Integer &a) const
Doubles an element in the ring.
Definition: modarith.h:156
virtual Element CascadeExponentiate(const Element &x, const Integer &e1, const Element &y, const Integer &e2) const
TODO.
Definition: algebra.cpp:323
#define b(i, j)
int RandomizationParameter
Definition: modarith.h:38
ModularArithmetic(const Integer &modulus=Integer::One())
Construct a ModularArithmetic.
Definition: modarith.h:45
Abstract group.
Definition: algebra.h:26
const Integer & Divide(const Integer &a, const Integer &b) const
Divides elements in the ring.
Definition: modarith.h:198
static const RandomizationParameter DefaultRandomizationParameter
Definition: modarith.h:252
void SetModulus(const Integer &newModulus)
Sets the modulus.
Definition: modarith.h:83
Performs modular arithmetic in Montgomery representation for increased speed.
Definition: modarith.h:271
uint8_t const size_t const size
Definition: sha3.h:20
#define CRYPTOPP_UNUSED(x)
Definition: config.h:741
void Multiply(word *R, word *T, const word *A, const word *B, size_t N)
Definition: integer.cpp:2324
Integer CascadeExponentiate(const Integer &x, const Integer &e1, const Integer &y, const Integer &e2) const
TODO.
Definition: modarith.h:303
Integer InverseMod(const Integer &n) const
calculate multiplicative inverse of *this mod n
Definition: integer.cpp:4370
bool operator==(const ModularArithmetic &rhs) const
Compares two ModularArithmetic for equality.
Definition: modarith.h:249
Multiple precision integer with arithmetic operations.
static const Integer &CRYPTOPP_API Zero()
Integer representing 0.
Definition: integer.cpp:3027
#define NAMESPACE_END
Definition: config.h:201
int Subtract(word *C, const word *A, const word *B, size_t N)
Definition: integer.cpp:2152
bool IsMontgomeryRepresentation() const
Retrieves the representation.
Definition: modarith.h:287
ModularArithmetic(const ModularArithmetic &ma)
Copy construct a ModularArithmetic.
Definition: modarith.h:50
virtual bool IsMontgomeryRepresentation() const
Retrieves the representation.
Definition: modarith.h:88
#define CRYPTOPP_DLL
Definition: config.h:704
const Integer & Identity() const
Provides the Identity element.
Definition: modarith.h:120
bool Equal(const Integer &a, const Integer &b) const
Compare two elements for equality.
Definition: modarith.h:115
unsigned int MaxElementByteLength() const
Provides the maximum byte size of an element in the ring.
Definition: modarith.h:228
void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const
Exponentiates a base to multiple exponents in the ring.
Definition: modarith.h:306
virtual ~ModularArithmetic()
Definition: modarith.h:41
evm_result m_result
Definition: JitVM.cpp:271
word32 word
Definition: config.h:308
Integer m_result1
Definition: modarith.h:256
Integer ConvertIn(const Integer &a) const
Reduces an element in the congruence class.
Definition: modarith.h:289