Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.7 KiB

  1. // cryptlib.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #include "xtr.h"
  4. #include "nbtheory.h"
  5. #include "algebra.cpp"
  6. NAMESPACE_BEGIN(CryptoPP)
  7. const GFP2Element & GFP2Element::Zero()
  8. {
  9. return Singleton<GFP2Element>().Ref();
  10. }
  11. void XTR_FindPrimesAndGenerator(RandomNumberGenerator &rng, Integer &p, Integer &q, GFP2Element &g, unsigned int pbits, unsigned int qbits)
  12. {
  13. assert(qbits > 9); // no primes exist for pbits = 10, qbits = 9
  14. assert(pbits > qbits);
  15. const Integer minQ = Integer::Power2(qbits - 1);
  16. const Integer maxQ = Integer::Power2(qbits) - 1;
  17. const Integer minP = Integer::Power2(pbits - 1);
  18. const Integer maxP = Integer::Power2(pbits) - 1;
  19. Integer r1, r2;
  20. do
  21. {
  22. bool qFound = q.Randomize(rng, minQ, maxQ, Integer::PRIME, 7, 12);
  23. assert(qFound);
  24. bool solutionsExist = SolveModularQuadraticEquation(r1, r2, 1, -1, 1, q);
  25. assert(solutionsExist);
  26. } while (!p.Randomize(rng, minP, maxP, Integer::PRIME, CRT(rng.GenerateBit()?r1:r2, q, 2, 3, EuclideanMultiplicativeInverse(p, 3)), 3*q));
  27. assert(((p.Squared() - p + 1) % q).IsZero());
  28. GFP2_ONB<ModularArithmetic> gfp2(p);
  29. GFP2Element three = gfp2.ConvertIn(3), t;
  30. while (true)
  31. {
  32. g.c1.Randomize(rng, Integer::Zero(), p-1);
  33. g.c2.Randomize(rng, Integer::Zero(), p-1);
  34. t = XTR_Exponentiate(g, p+1, p);
  35. if (t.c1 == t.c2)
  36. continue;
  37. g = XTR_Exponentiate(g, (p.Squared()-p+1)/q, p);
  38. if (g != three)
  39. break;
  40. }
  41. assert(XTR_Exponentiate(g, q, p) == three);
  42. }
  43. GFP2Element XTR_Exponentiate(const GFP2Element &b, const Integer &e, const Integer &p)
  44. {
  45. unsigned int bitCount = e.BitCount();
  46. if (bitCount == 0)
  47. return GFP2Element(-3, -3);
  48. // find the lowest bit of e that is 1
  49. unsigned int lowest1bit;
  50. for (lowest1bit=0; e.GetBit(lowest1bit) == 0; lowest1bit++) {}
  51. GFP2_ONB<MontgomeryRepresentation> gfp2(p);
  52. GFP2Element c = gfp2.ConvertIn(b);
  53. GFP2Element cp = gfp2.PthPower(c);
  54. GFP2Element S[5] = {gfp2.ConvertIn(3), c, gfp2.SpecialOperation1(c)};
  55. // do all exponents bits except the lowest zeros starting from the top
  56. unsigned int i;
  57. for (i = e.BitCount() - 1; i>lowest1bit; i--)
  58. {
  59. if (e.GetBit(i))
  60. {
  61. gfp2.RaiseToPthPower(S[0]);
  62. gfp2.Accumulate(S[0], gfp2.SpecialOperation2(S[2], c, S[1]));
  63. S[1] = gfp2.SpecialOperation1(S[1]);
  64. S[2] = gfp2.SpecialOperation1(S[2]);
  65. S[0].swap(S[1]);
  66. }
  67. else
  68. {
  69. gfp2.RaiseToPthPower(S[2]);
  70. gfp2.Accumulate(S[2], gfp2.SpecialOperation2(S[0], cp, S[1]));
  71. S[1] = gfp2.SpecialOperation1(S[1]);
  72. S[0] = gfp2.SpecialOperation1(S[0]);
  73. S[2].swap(S[1]);
  74. }
  75. }
  76. // now do the lowest zeros
  77. while (i--)
  78. S[1] = gfp2.SpecialOperation1(S[1]);
  79. return gfp2.ConvertOut(S[1]);
  80. }
  81. template class AbstractRing<GFP2Element>;
  82. template class AbstractGroup<GFP2Element>;
  83. NAMESPACE_END