Curve25519Point.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Djb
  6. {
  7. internal class Curve25519Point
  8. : AbstractFpPoint
  9. {
  10. /**
  11. * Create a point which encodes with point compression.
  12. *
  13. * @param curve the curve to use
  14. * @param x affine x co-ordinate
  15. * @param y affine y co-ordinate
  16. *
  17. * @deprecated Use ECCurve.CreatePoint to construct points
  18. */
  19. public Curve25519Point(ECCurve curve, ECFieldElement x, ECFieldElement y)
  20. : this(curve, x, y, false)
  21. {
  22. }
  23. /**
  24. * Create a point that encodes with or without point compresion.
  25. *
  26. * @param curve the curve to use
  27. * @param x affine x co-ordinate
  28. * @param y affine y co-ordinate
  29. * @param withCompression if true encode with point compression
  30. *
  31. * @deprecated per-point compression property will be removed, refer {@link #getEncoded(bool)}
  32. */
  33. public Curve25519Point(ECCurve curve, ECFieldElement x, ECFieldElement y, bool withCompression)
  34. : base(curve, x, y, withCompression)
  35. {
  36. if ((x == null) != (y == null))
  37. throw new ArgumentException("Exactly one of the field elements is null");
  38. }
  39. internal Curve25519Point(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, bool withCompression)
  40. : base(curve, x, y, zs, withCompression)
  41. {
  42. }
  43. protected override ECPoint Detach()
  44. {
  45. return new Curve25519Point(null, AffineXCoord, AffineYCoord);
  46. }
  47. public override ECFieldElement GetZCoord(int index)
  48. {
  49. if (index == 1)
  50. {
  51. return GetJacobianModifiedW();
  52. }
  53. return base.GetZCoord(index);
  54. }
  55. public override ECPoint Add(ECPoint b)
  56. {
  57. if (this.IsInfinity)
  58. return b;
  59. if (b.IsInfinity)
  60. return this;
  61. if (this == b)
  62. return Twice();
  63. ECCurve curve = this.Curve;
  64. Curve25519FieldElement X1 = (Curve25519FieldElement)this.RawXCoord, Y1 = (Curve25519FieldElement)this.RawYCoord,
  65. Z1 = (Curve25519FieldElement)this.RawZCoords[0];
  66. Curve25519FieldElement X2 = (Curve25519FieldElement)b.RawXCoord, Y2 = (Curve25519FieldElement)b.RawYCoord,
  67. Z2 = (Curve25519FieldElement)b.RawZCoords[0];
  68. uint c;
  69. uint[] tt1 = Nat256.CreateExt();
  70. uint[] t2 = Nat256.Create();
  71. uint[] t3 = Nat256.Create();
  72. uint[] t4 = Nat256.Create();
  73. bool Z1IsOne = Z1.IsOne;
  74. uint[] U2, S2;
  75. if (Z1IsOne)
  76. {
  77. U2 = X2.x;
  78. S2 = Y2.x;
  79. }
  80. else
  81. {
  82. S2 = t3;
  83. Curve25519Field.Square(Z1.x, S2);
  84. U2 = t2;
  85. Curve25519Field.Multiply(S2, X2.x, U2);
  86. Curve25519Field.Multiply(S2, Z1.x, S2);
  87. Curve25519Field.Multiply(S2, Y2.x, S2);
  88. }
  89. bool Z2IsOne = Z2.IsOne;
  90. uint[] U1, S1;
  91. if (Z2IsOne)
  92. {
  93. U1 = X1.x;
  94. S1 = Y1.x;
  95. }
  96. else
  97. {
  98. S1 = t4;
  99. Curve25519Field.Square(Z2.x, S1);
  100. U1 = tt1;
  101. Curve25519Field.Multiply(S1, X1.x, U1);
  102. Curve25519Field.Multiply(S1, Z2.x, S1);
  103. Curve25519Field.Multiply(S1, Y1.x, S1);
  104. }
  105. uint[] H = Nat256.Create();
  106. Curve25519Field.Subtract(U1, U2, H);
  107. uint[] R = t2;
  108. Curve25519Field.Subtract(S1, S2, R);
  109. // Check if b == this or b == -this
  110. if (Nat256.IsZero(H))
  111. {
  112. if (Nat256.IsZero(R))
  113. {
  114. // this == b, i.e. this must be doubled
  115. return this.Twice();
  116. }
  117. // this == -b, i.e. the result is the point at infinity
  118. return curve.Infinity;
  119. }
  120. uint[] HSquared = Nat256.Create();
  121. Curve25519Field.Square(H, HSquared);
  122. uint[] G = Nat256.Create();
  123. Curve25519Field.Multiply(HSquared, H, G);
  124. uint[] V = t3;
  125. Curve25519Field.Multiply(HSquared, U1, V);
  126. Curve25519Field.Negate(G, G);
  127. Nat256.Mul(S1, G, tt1);
  128. c = Nat256.AddBothTo(V, V, G);
  129. Curve25519Field.Reduce27(c, G);
  130. Curve25519FieldElement X3 = new Curve25519FieldElement(t4);
  131. Curve25519Field.Square(R, X3.x);
  132. Curve25519Field.Subtract(X3.x, G, X3.x);
  133. Curve25519FieldElement Y3 = new Curve25519FieldElement(G);
  134. Curve25519Field.Subtract(V, X3.x, Y3.x);
  135. Curve25519Field.MultiplyAddToExt(Y3.x, R, tt1);
  136. Curve25519Field.Reduce(tt1, Y3.x);
  137. Curve25519FieldElement Z3 = new Curve25519FieldElement(H);
  138. if (!Z1IsOne)
  139. {
  140. Curve25519Field.Multiply(Z3.x, Z1.x, Z3.x);
  141. }
  142. if (!Z2IsOne)
  143. {
  144. Curve25519Field.Multiply(Z3.x, Z2.x, Z3.x);
  145. }
  146. uint[] Z3Squared = (Z1IsOne && Z2IsOne) ? HSquared : null;
  147. // TODO If the result will only be used in a subsequent addition, we don't need W3
  148. Curve25519FieldElement W3 = CalculateJacobianModifiedW((Curve25519FieldElement)Z3, Z3Squared);
  149. ECFieldElement[] zs = new ECFieldElement[] { Z3, W3 };
  150. return new Curve25519Point(curve, X3, Y3, zs, IsCompressed);
  151. }
  152. public override ECPoint Twice()
  153. {
  154. if (this.IsInfinity)
  155. return this;
  156. ECCurve curve = this.Curve;
  157. ECFieldElement Y1 = this.RawYCoord;
  158. if (Y1.IsZero)
  159. return curve.Infinity;
  160. return TwiceJacobianModified(true);
  161. }
  162. public override ECPoint TwicePlus(ECPoint b)
  163. {
  164. if (this == b)
  165. return ThreeTimes();
  166. if (this.IsInfinity)
  167. return b;
  168. if (b.IsInfinity)
  169. return Twice();
  170. ECFieldElement Y1 = this.RawYCoord;
  171. if (Y1.IsZero)
  172. return b;
  173. return TwiceJacobianModified(false).Add(b);
  174. }
  175. public override ECPoint ThreeTimes()
  176. {
  177. if (this.IsInfinity || this.RawYCoord.IsZero)
  178. return this;
  179. return TwiceJacobianModified(false).Add(this);
  180. }
  181. public override ECPoint Negate()
  182. {
  183. if (IsInfinity)
  184. return this;
  185. return new Curve25519Point(Curve, RawXCoord, RawYCoord.Negate(), RawZCoords, IsCompressed);
  186. }
  187. protected virtual Curve25519FieldElement CalculateJacobianModifiedW(Curve25519FieldElement Z, uint[] ZSquared)
  188. {
  189. Curve25519FieldElement a4 = (Curve25519FieldElement)this.Curve.A;
  190. if (Z.IsOne)
  191. return a4;
  192. Curve25519FieldElement W = new Curve25519FieldElement();
  193. if (ZSquared == null)
  194. {
  195. ZSquared = W.x;
  196. Curve25519Field.Square(Z.x, ZSquared);
  197. }
  198. Curve25519Field.Square(ZSquared, W.x);
  199. Curve25519Field.Multiply(W.x, a4.x, W.x);
  200. return W;
  201. }
  202. protected virtual Curve25519FieldElement GetJacobianModifiedW()
  203. {
  204. ECFieldElement[] ZZ = this.RawZCoords;
  205. Curve25519FieldElement W = (Curve25519FieldElement)ZZ[1];
  206. if (W == null)
  207. {
  208. // NOTE: Rarely, TwicePlus will result in the need for a lazy W1 calculation here
  209. ZZ[1] = W = CalculateJacobianModifiedW((Curve25519FieldElement)ZZ[0], null);
  210. }
  211. return W;
  212. }
  213. protected virtual Curve25519Point TwiceJacobianModified(bool calculateW)
  214. {
  215. Curve25519FieldElement X1 = (Curve25519FieldElement)this.RawXCoord, Y1 = (Curve25519FieldElement)this.RawYCoord,
  216. Z1 = (Curve25519FieldElement)this.RawZCoords[0], W1 = GetJacobianModifiedW();
  217. uint c;
  218. uint[] M = Nat256.Create();
  219. Curve25519Field.Square(X1.x, M);
  220. c = Nat256.AddBothTo(M, M, M);
  221. c += Nat256.AddTo(W1.x, M);
  222. Curve25519Field.Reduce27(c, M);
  223. uint[] _2Y1 = Nat256.Create();
  224. Curve25519Field.Twice(Y1.x, _2Y1);
  225. uint[] _2Y1Squared = Nat256.Create();
  226. Curve25519Field.Multiply(_2Y1, Y1.x, _2Y1Squared);
  227. uint[] S = Nat256.Create();
  228. Curve25519Field.Multiply(_2Y1Squared, X1.x, S);
  229. Curve25519Field.Twice(S, S);
  230. uint[] _8T = Nat256.Create();
  231. Curve25519Field.Square(_2Y1Squared, _8T);
  232. Curve25519Field.Twice(_8T, _8T);
  233. Curve25519FieldElement X3 = new Curve25519FieldElement(_2Y1Squared);
  234. Curve25519Field.Square(M, X3.x);
  235. Curve25519Field.Subtract(X3.x, S, X3.x);
  236. Curve25519Field.Subtract(X3.x, S, X3.x);
  237. Curve25519FieldElement Y3 = new Curve25519FieldElement(S);
  238. Curve25519Field.Subtract(S, X3.x, Y3.x);
  239. Curve25519Field.Multiply(Y3.x, M, Y3.x);
  240. Curve25519Field.Subtract(Y3.x, _8T, Y3.x);
  241. Curve25519FieldElement Z3 = new Curve25519FieldElement(_2Y1);
  242. if (!Nat256.IsOne(Z1.x))
  243. {
  244. Curve25519Field.Multiply(Z3.x, Z1.x, Z3.x);
  245. }
  246. Curve25519FieldElement W3 = null;
  247. if (calculateW)
  248. {
  249. W3 = new Curve25519FieldElement(_8T);
  250. Curve25519Field.Multiply(W3.x, W1.x, W3.x);
  251. Curve25519Field.Twice(W3.x, W3.x);
  252. }
  253. return new Curve25519Point(this.Curve, X3, Y3, new ECFieldElement[] { Z3, W3 }, IsCompressed);
  254. }
  255. }
  256. }
  257. #pragma warning restore
  258. #endif