Curve25519Point.cs 10 KB

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