Crypto++  5.6.3
Free C++ class library of cryptographic schemes
xtr.cpp
1 // cryptlib.cpp - written and placed in the public domain by Wei Dai
2 
3 #include "pch.h"
4 
5 #include "xtr.h"
6 #include "nbtheory.h"
7 #include "integer.h"
8 #include "modarith.h"
9 #include "algebra.cpp"
10 
11 NAMESPACE_BEGIN(CryptoPP)
12 
13 const GFP2Element & GFP2Element::Zero()
14 {
15  return Singleton<GFP2Element>().Ref();
16 }
17 
18 void XTR_FindPrimesAndGenerator(RandomNumberGenerator &rng, Integer &p, Integer &q, GFP2Element &g, unsigned int pbits, unsigned int qbits)
19 {
20  assert(qbits > 9); // no primes exist for pbits = 10, qbits = 9
21  assert(pbits > qbits);
22 
23  const Integer minQ = Integer::Power2(qbits - 1);
24  const Integer maxQ = Integer::Power2(qbits) - 1;
25  const Integer minP = Integer::Power2(pbits - 1);
26  const Integer maxP = Integer::Power2(pbits) - 1;
27 
28  Integer r1, r2;
29  do
30  {
31  bool qFound = q.Randomize(rng, minQ, maxQ, Integer::PRIME, 7, 12);
32  CRYPTOPP_UNUSED(qFound); assert(qFound);
33  bool solutionsExist = SolveModularQuadraticEquation(r1, r2, 1, -1, 1, q);
34  CRYPTOPP_UNUSED(solutionsExist); assert(solutionsExist);
35  } while (!p.Randomize(rng, minP, maxP, Integer::PRIME, CRT(rng.GenerateBit()?r1:r2, q, 2, 3, EuclideanMultiplicativeInverse(p, 3)), 3*q));
36  assert(((p.Squared() - p + 1) % q).IsZero());
37 
39  GFP2Element three = gfp2.ConvertIn(3), t;
40 
41  while (true)
42  {
43  g.c1.Randomize(rng, Integer::Zero(), p-1);
44  g.c2.Randomize(rng, Integer::Zero(), p-1);
45  t = XTR_Exponentiate(g, p+1, p);
46  if (t.c1 == t.c2)
47  continue;
48  g = XTR_Exponentiate(g, (p.Squared()-p+1)/q, p);
49  if (g != three)
50  break;
51  }
52  assert(XTR_Exponentiate(g, q, p) == three);
53 }
54 
55 GFP2Element XTR_Exponentiate(const GFP2Element &b, const Integer &e, const Integer &p)
56 {
57  unsigned int bitCount = e.BitCount();
58  if (bitCount == 0)
59  return GFP2Element(-3, -3);
60 
61  // find the lowest bit of e that is 1
62  unsigned int lowest1bit;
63  for (lowest1bit=0; e.GetBit(lowest1bit) == 0; lowest1bit++) {}
64 
66  GFP2Element c = gfp2.ConvertIn(b);
67  GFP2Element cp = gfp2.PthPower(c);
68  GFP2Element S[5] = {gfp2.ConvertIn(3), c, gfp2.SpecialOperation1(c)};
69 
70  // do all exponents bits except the lowest zeros starting from the top
71  unsigned int i;
72  for (i = e.BitCount() - 1; i>lowest1bit; i--)
73  {
74  if (e.GetBit(i))
75  {
76  gfp2.RaiseToPthPower(S[0]);
77  gfp2.Accumulate(S[0], gfp2.SpecialOperation2(S[2], c, S[1]));
78  S[1] = gfp2.SpecialOperation1(S[1]);
79  S[2] = gfp2.SpecialOperation1(S[2]);
80  S[0].swap(S[1]);
81  }
82  else
83  {
84  gfp2.RaiseToPthPower(S[2]);
85  gfp2.Accumulate(S[2], gfp2.SpecialOperation2(S[0], cp, S[1]));
86  S[1] = gfp2.SpecialOperation1(S[1]);
87  S[0] = gfp2.SpecialOperation1(S[0]);
88  S[2].swap(S[1]);
89  }
90  }
91 
92  // now do the lowest zeros
93  while (i--)
94  S[1] = gfp2.SpecialOperation1(S[1]);
95 
96  return gfp2.ConvertOut(S[1]);
97 }
98 
99 template class AbstractRing<GFP2Element>;
100 template class AbstractGroup<GFP2Element>;
101 
102 NAMESPACE_END
a number which is probabilistically prime
Definition: integer.h:77
Restricts the instantiation of a class to one static object without locks.
Definition: misc.h:233
bool GetBit(size_t i) const
return the i-th bit, i=0 being the least significant bit
Definition: integer.cpp:2958
Interface for random number generators.
Definition: cryptlib.h:1085
void Randomize(RandomNumberGenerator &rng, size_t bitCount)
Set this Integer to random integer.
Definition: integer.cpp:3268
unsigned int BitCount() const
number of significant bits = floor(log2(abs(*this))) + 1
Definition: integer.cpp:3118
an element of GF(p^2)
Definition: xtr.h:15
Integer Squared() const
Definition: integer.h:452
"The XTR public key system" by Arjen K.
static Integer Power2(size_t e)
Exponentiates to a power of 2.
Definition: integer.cpp:2910
Multiple precision integer with arithmetic operations.
Definition: integer.h:31
Classes and functions for number theoretic operations.
virtual unsigned int GenerateBit()
Generate new random bit and return it.
Definition: cryptlib.cpp:289
static const Integer & Zero()
Integer representing 0.
Definition: integer.cpp:2926
Class file for performing modular arithmetic.
Crypto++ library namespace.
GF(p^2), optimal normal basis.
Definition: xtr.h:45