Fabcoin Core  0.16.2
P2P Digital Currency
xtr.cpp
Go to the documentation of this file.
1 // xtr.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 "algebra.h"
9 #include "modarith.h"
10 #include "algebra.cpp"
11 
13 
14 const GFP2Element & GFP2Element::Zero()
15 {
16  return Singleton<GFP2Element>().Ref();
17 }
18 
19 void XTR_FindPrimesAndGenerator(RandomNumberGenerator &rng, Integer &p, Integer &q, GFP2Element &g, unsigned int pbits, unsigned int qbits)
20 {
21  CRYPTOPP_ASSERT(qbits > 9); // no primes exist for pbits = 10, qbits = 9
22  CRYPTOPP_ASSERT(pbits > qbits);
23 
24  const Integer minQ = Integer::Power2(qbits - 1);
25  const Integer maxQ = Integer::Power2(qbits) - 1;
26  const Integer minP = Integer::Power2(pbits - 1);
27  const Integer maxP = Integer::Power2(pbits) - 1;
28 
29  Integer r1, r2;
30  do
31  {
32  bool qFound = q.Randomize(rng, minQ, maxQ, Integer::PRIME, 7, 12);
33  CRYPTOPP_UNUSED(qFound); CRYPTOPP_ASSERT(qFound);
34  bool solutionsExist = SolveModularQuadraticEquation(r1, r2, 1, -1, 1, q);
35  CRYPTOPP_UNUSED(solutionsExist); CRYPTOPP_ASSERT(solutionsExist);
36  } while (!p.Randomize(rng, minP, maxP, Integer::PRIME, CRT(rng.GenerateBit()?r1:r2, q, 2, 3, EuclideanMultiplicativeInverse(p, 3)), 3*q));
37  CRYPTOPP_ASSERT(((p.Squared() - p + 1) % q).IsZero());
38 
40  GFP2Element three = gfp2.ConvertIn(3), t;
41 
42  while (true)
43  {
44  g.c1.Randomize(rng, Integer::Zero(), p-1);
45  g.c2.Randomize(rng, Integer::Zero(), p-1);
46  t = XTR_Exponentiate(g, p+1, p);
47  if (t.c1 == t.c2)
48  continue;
49  g = XTR_Exponentiate(g, (p.Squared()-p+1)/q, p);
50  if (g != three)
51  break;
52  }
53  CRYPTOPP_ASSERT(XTR_Exponentiate(g, q, p) == three);
54 }
55 
57 {
58  unsigned int bitCount = e.BitCount();
59  if (bitCount == 0)
60  return GFP2Element(-3, -3);
61 
62  // find the lowest bit of e that is 1
63  unsigned int lowest1bit;
64  for (lowest1bit=0; e.GetBit(lowest1bit) == 0; lowest1bit++) {}
65 
67  GFP2Element c = gfp2.ConvertIn(b);
68  GFP2Element cp = gfp2.PthPower(c);
69  GFP2Element S[5] = {gfp2.ConvertIn(3), c, gfp2.SpecialOperation1(c)};
70 
71  // do all exponents bits except the lowest zeros starting from the top
72  unsigned int i;
73  for (i = e.BitCount() - 1; i>lowest1bit; i--)
74  {
75  if (e.GetBit(i))
76  {
77  gfp2.RaiseToPthPower(S[0]);
78  gfp2.Accumulate(S[0], gfp2.SpecialOperation2(S[2], c, S[1]));
79  S[1] = gfp2.SpecialOperation1(S[1]);
80  S[2] = gfp2.SpecialOperation1(S[2]);
81  S[0].swap(S[1]);
82  }
83  else
84  {
85  gfp2.RaiseToPthPower(S[2]);
86  gfp2.Accumulate(S[2], gfp2.SpecialOperation2(S[0], cp, S[1]));
87  S[1] = gfp2.SpecialOperation1(S[1]);
88  S[0] = gfp2.SpecialOperation1(S[0]);
89  S[2].swap(S[1]);
90  }
91  }
92 
93  // now do the lowest zeros
94  while (i--)
95  S[1] = gfp2.SpecialOperation1(S[1]);
96 
97  return gfp2.ConvertOut(S[1]);
98 }
99 
100 template class AbstractRing<GFP2Element>;
101 template class AbstractGroup<GFP2Element>;
102 
const Element & SpecialOperation1(const Element &a) const
Definition: xtr.h:184
a number which is probabilistically prime
Definition: integer.h:89
Restricts the instantiation of a class to one static object without locks.
Definition: misc.h:274
bool GetBit(size_t i) const
Provides the i-th bit of the Integer.
Definition: integer.cpp:3065
void XTR_FindPrimesAndGenerator(RandomNumberGenerator &rng, Integer &p, Integer &q, GFP2Element &g, unsigned int pbits, unsigned int qbits)
Creates primes p,q and generator g for XTR.
Definition: xtr.cpp:19
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
#define g(i)
Definition: sha.cpp:735
#define c(i)
GFP2Element ConvertOut(const GFP2Element &a) const
Definition: xtr.h:70
Interface for random number generators.
Definition: cryptlib.h:1188
void Randomize(RandomNumberGenerator &rng, size_t bitCount)
Set this Integer to random integer.
Definition: integer.cpp:3458
Integer CRT(const Integer &xp, const Integer &p, const Integer &xq, const Integer &q, const Integer &u)
Definition: nbtheory.cpp:555
Classes for performing mathematics over different fields.
void swap(GFP2Element &a)
Definition: xtr.h:34
Element & Accumulate(Element &a, const Element &b) const
TODO.
Definition: xtr.h:111
void RaiseToPthPower(Element &a) const
Definition: xtr.h:178
const Element & SpecialOperation2(const Element &x, const Element &y, const Element &z) const
Definition: xtr.h:196
unsigned int BitCount() const
Determines the number of bits required to represent the Integer.
Definition: integer.cpp:3305
an element of GF(p^2)
Definition: xtr.h:17
#define r1(i)
bool SolveModularQuadraticEquation(Integer &r1, Integer &r2, const Integer &a, const Integer &b, const Integer &c, const Integer &p)
Definition: nbtheory.cpp:623
Integer Squared() const
Multiply this integer by itself.
Definition: integer.h:570
#define r2(i)
The XTR public key system.
static Integer CRYPTOPP_API Power2(size_t e)
Exponentiates to a power of 2.
Definition: integer.cpp:3008
Multiple precision integer with arithmetic operations.
Definition: integer.h:43
#define b(i, j)
#define CRYPTOPP_ASSERT(exp)
Definition: trap.h:92
Classes and functions for number theoretic operations.
Integer EuclideanMultiplicativeInverse(const Integer &a, const Integer &b)
Definition: nbtheory.h:111
virtual unsigned int GenerateBit()
Generate new random bit and return it.
Definition: cryptlib.cpp:286
#define CRYPTOPP_UNUSED(x)
Definition: config.h:741
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
Integer c2
Definition: xtr.h:42
#define e(i)
Definition: sha.cpp:733
GFP2Element ConvertIn(const Integer &a) const
Definition: xtr.h:61
Class file for performing modular arithmetic.
const Element & PthPower(const Element &a) const
Definition: xtr.h:171
#define S(a)
Definition: mars.cpp:50
GF(p^2), optimal normal basis.
Definition: xtr.h:48
GFP2Element XTR_Exponentiate(const GFP2Element &b, const Integer &e, const Integer &p)
Definition: xtr.cpp:56
Integer c1
Definition: xtr.h:42