Team Fortress 2 Source Code as on 22/4/2020
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.

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