Curve25519FieldElement.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Djb
  7. {
  8. internal class Curve25519FieldElement
  9. : AbstractFpFieldElement
  10. {
  11. public static readonly BigInteger Q = Nat256.ToBigInteger(Curve25519Field.P);
  12. // Calculated as ECConstants.TWO.modPow(Q.shiftRight(2), Q)
  13. private static readonly uint[] PRECOMP_POW2 = new uint[]{ 0x4a0ea0b0, 0xc4ee1b27, 0xad2fe478, 0x2f431806,
  14. 0x3dfbd7a7, 0x2b4d0099, 0x4fc1df0b, 0x2b832480 };
  15. protected internal readonly uint[] x;
  16. public Curve25519FieldElement(BigInteger x)
  17. {
  18. if (x == null || x.SignValue < 0 || x.CompareTo(Q) >= 0)
  19. throw new ArgumentException("value invalid for Curve25519FieldElement", "x");
  20. this.x = Curve25519Field.FromBigInteger(x);
  21. }
  22. public Curve25519FieldElement()
  23. {
  24. this.x = Nat256.Create();
  25. }
  26. protected internal Curve25519FieldElement(uint[] x)
  27. {
  28. this.x = x;
  29. }
  30. public override bool IsZero
  31. {
  32. get { return Nat256.IsZero(x); }
  33. }
  34. public override bool IsOne
  35. {
  36. get { return Nat256.IsOne(x); }
  37. }
  38. public override bool TestBitZero()
  39. {
  40. return Nat256.GetBit(x, 0) == 1;
  41. }
  42. public override BigInteger ToBigInteger()
  43. {
  44. return Nat256.ToBigInteger(x);
  45. }
  46. public override string FieldName
  47. {
  48. get { return "Curve25519Field"; }
  49. }
  50. public override int FieldSize
  51. {
  52. get { return Q.BitLength; }
  53. }
  54. public override ECFieldElement Add(ECFieldElement b)
  55. {
  56. uint[] z = Nat256.Create();
  57. Curve25519Field.Add(x, ((Curve25519FieldElement)b).x, z);
  58. return new Curve25519FieldElement(z);
  59. }
  60. public override ECFieldElement AddOne()
  61. {
  62. uint[] z = Nat256.Create();
  63. Curve25519Field.AddOne(x, z);
  64. return new Curve25519FieldElement(z);
  65. }
  66. public override ECFieldElement Subtract(ECFieldElement b)
  67. {
  68. uint[] z = Nat256.Create();
  69. Curve25519Field.Subtract(x, ((Curve25519FieldElement)b).x, z);
  70. return new Curve25519FieldElement(z);
  71. }
  72. public override ECFieldElement Multiply(ECFieldElement b)
  73. {
  74. uint[] z = Nat256.Create();
  75. Curve25519Field.Multiply(x, ((Curve25519FieldElement)b).x, z);
  76. return new Curve25519FieldElement(z);
  77. }
  78. public override ECFieldElement Divide(ECFieldElement b)
  79. {
  80. //return Multiply(b.Invert());
  81. uint[] z = Nat256.Create();
  82. Curve25519Field.Inv(((Curve25519FieldElement)b).x, z);
  83. Curve25519Field.Multiply(z, x, z);
  84. return new Curve25519FieldElement(z);
  85. }
  86. public override ECFieldElement Negate()
  87. {
  88. uint[] z = Nat256.Create();
  89. Curve25519Field.Negate(x, z);
  90. return new Curve25519FieldElement(z);
  91. }
  92. public override ECFieldElement Square()
  93. {
  94. uint[] z = Nat256.Create();
  95. Curve25519Field.Square(x, z);
  96. return new Curve25519FieldElement(z);
  97. }
  98. public override ECFieldElement Invert()
  99. {
  100. //return new Curve25519FieldElement(ToBigInteger().ModInverse(Q));
  101. uint[] z = Nat256.Create();
  102. Curve25519Field.Inv(x, z);
  103. return new Curve25519FieldElement(z);
  104. }
  105. /**
  106. * return a sqrt root - the routine verifies that the calculation returns the right value - if
  107. * none exists it returns null.
  108. */
  109. public override ECFieldElement Sqrt()
  110. {
  111. /*
  112. * Q == 8m + 5, so we use Pocklington's method for this case.
  113. *
  114. * First, raise this element to the exponent 2^252 - 2^1 (i.e. m + 1)
  115. *
  116. * Breaking up the exponent's binary representation into "repunits", we get:
  117. * { 251 1s } { 1 0s }
  118. *
  119. * Therefore we need an addition chain containing 251 (the lengths of the repunits)
  120. * We use: 1, 2, 3, 4, 7, 11, 15, 30, 60, 120, 131, [251]
  121. */
  122. uint[] x1 = this.x;
  123. if (Nat256.IsZero(x1) || Nat256.IsOne(x1))
  124. return this;
  125. uint[] x2 = Nat256.Create();
  126. Curve25519Field.Square(x1, x2);
  127. Curve25519Field.Multiply(x2, x1, x2);
  128. uint[] x3 = x2;
  129. Curve25519Field.Square(x2, x3);
  130. Curve25519Field.Multiply(x3, x1, x3);
  131. uint[] x4 = Nat256.Create();
  132. Curve25519Field.Square(x3, x4);
  133. Curve25519Field.Multiply(x4, x1, x4);
  134. uint[] x7 = Nat256.Create();
  135. Curve25519Field.SquareN(x4, 3, x7);
  136. Curve25519Field.Multiply(x7, x3, x7);
  137. uint[] x11 = x3;
  138. Curve25519Field.SquareN(x7, 4, x11);
  139. Curve25519Field.Multiply(x11, x4, x11);
  140. uint[] x15 = x7;
  141. Curve25519Field.SquareN(x11, 4, x15);
  142. Curve25519Field.Multiply(x15, x4, x15);
  143. uint[] x30 = x4;
  144. Curve25519Field.SquareN(x15, 15, x30);
  145. Curve25519Field.Multiply(x30, x15, x30);
  146. uint[] x60 = x15;
  147. Curve25519Field.SquareN(x30, 30, x60);
  148. Curve25519Field.Multiply(x60, x30, x60);
  149. uint[] x120 = x30;
  150. Curve25519Field.SquareN(x60, 60, x120);
  151. Curve25519Field.Multiply(x120, x60, x120);
  152. uint[] x131 = x60;
  153. Curve25519Field.SquareN(x120, 11, x131);
  154. Curve25519Field.Multiply(x131, x11, x131);
  155. uint[] x251 = x11;
  156. Curve25519Field.SquareN(x131, 120, x251);
  157. Curve25519Field.Multiply(x251, x120, x251);
  158. uint[] t1 = x251;
  159. Curve25519Field.Square(t1, t1);
  160. uint[] t2 = x120;
  161. Curve25519Field.Square(t1, t2);
  162. if (Nat256.Eq(x1, t2))
  163. {
  164. return new Curve25519FieldElement(t1);
  165. }
  166. /*
  167. * If the first guess is incorrect, we multiply by a precomputed power of 2 to get the second guess,
  168. * which is ((4x)^(m + 1))/2 mod Q
  169. */
  170. Curve25519Field.Multiply(t1, PRECOMP_POW2, t1);
  171. Curve25519Field.Square(t1, t2);
  172. if (Nat256.Eq(x1, t2))
  173. {
  174. return new Curve25519FieldElement(t1);
  175. }
  176. return null;
  177. }
  178. public override bool Equals(object obj)
  179. {
  180. return Equals(obj as Curve25519FieldElement);
  181. }
  182. public override bool Equals(ECFieldElement other)
  183. {
  184. return Equals(other as Curve25519FieldElement);
  185. }
  186. public virtual bool Equals(Curve25519FieldElement other)
  187. {
  188. if (this == other)
  189. return true;
  190. if (null == other)
  191. return false;
  192. return Nat256.Eq(x, other.x);
  193. }
  194. public override int GetHashCode()
  195. {
  196. return Q.GetHashCode() ^ Arrays.GetHashCode(x, 0, 8);
  197. }
  198. }
  199. }
  200. #pragma warning restore
  201. #endif