SecP384R1Field.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Diagnostics;
  4. using Org.BouncyCastle.Math.Raw;
  5. namespace Org.BouncyCastle.Math.EC.Custom.Sec
  6. {
  7. internal class SecP384R1Field
  8. {
  9. // 2^384 - 2^128 - 2^96 + 2^32 - 1
  10. internal static readonly uint[] P = new uint[]{ 0xFFFFFFFF, 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFF,
  11. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
  12. internal static readonly uint[] PExt = new uint[]{ 0x00000001, 0xFFFFFFFE, 0x00000000, 0x00000002, 0x00000000, 0xFFFFFFFE,
  13. 0x00000000, 0x00000002, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0xFFFFFFFE, 0x00000001, 0x00000000,
  14. 0xFFFFFFFE, 0xFFFFFFFD, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
  15. private static readonly uint[] PExtInv = new uint[]{ 0xFFFFFFFF, 0x00000001, 0xFFFFFFFF, 0xFFFFFFFD, 0xFFFFFFFF, 0x00000001,
  16. 0xFFFFFFFF, 0xFFFFFFFD, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000001, 0xFFFFFFFE, 0xFFFFFFFF,
  17. 0x00000001, 0x00000002 };
  18. private const uint P11 = 0xFFFFFFFF;
  19. private const uint PExt23 = 0xFFFFFFFF;
  20. public static void Add(uint[] x, uint[] y, uint[] z)
  21. {
  22. uint c = Nat.Add(12, x, y, z);
  23. if (c != 0 || (z[11] == P11 && Nat.Gte(12, z, P)))
  24. {
  25. AddPInvTo(z);
  26. }
  27. }
  28. public static void AddExt(uint[] xx, uint[] yy, uint[] zz)
  29. {
  30. uint c = Nat.Add(24, xx, yy, zz);
  31. if (c != 0 || (zz[23] == PExt23 && Nat.Gte(24, zz, PExt)))
  32. {
  33. if (Nat.AddTo(PExtInv.Length, PExtInv, zz) != 0)
  34. {
  35. Nat.IncAt(24, zz, PExtInv.Length);
  36. }
  37. }
  38. }
  39. public static void AddOne(uint[] x, uint[] z)
  40. {
  41. uint c = Nat.Inc(12, x, z);
  42. if (c != 0 || (z[11] == P11 && Nat.Gte(12, z, P)))
  43. {
  44. AddPInvTo(z);
  45. }
  46. }
  47. public static uint[] FromBigInteger(BigInteger x)
  48. {
  49. uint[] z = Nat.FromBigInteger(384, x);
  50. if (z[11] == P11 && Nat.Gte(12, z, P))
  51. {
  52. Nat.SubFrom(12, P, z);
  53. }
  54. return z;
  55. }
  56. public static void Half(uint[] x, uint[] z)
  57. {
  58. if ((x[0] & 1) == 0)
  59. {
  60. Nat.ShiftDownBit(12, x, 0, z);
  61. }
  62. else
  63. {
  64. uint c = Nat.Add(12, x, P, z);
  65. Nat.ShiftDownBit(12, z, c);
  66. }
  67. }
  68. public static void Multiply(uint[] x, uint[] y, uint[] z)
  69. {
  70. uint[] tt = Nat.Create(24);
  71. Nat384.Mul(x, y, tt);
  72. Reduce(tt, z);
  73. }
  74. public static void Negate(uint[] x, uint[] z)
  75. {
  76. if (Nat.IsZero(12, x))
  77. {
  78. Nat.Zero(12, z);
  79. }
  80. else
  81. {
  82. Nat.Sub(12, P, x, z);
  83. }
  84. }
  85. public static void Reduce(uint[] xx, uint[] z)
  86. {
  87. long xx16 = xx[16], xx17 = xx[17], xx18 = xx[18], xx19 = xx[19];
  88. long xx20 = xx[20], xx21 = xx[21], xx22 = xx[22], xx23 = xx[23];
  89. const long n = 1;
  90. long t0 = (long)xx[12] + xx20 - n;
  91. long t1 = (long)xx[13] + xx22;
  92. long t2 = (long)xx[14] + xx22 + xx23;
  93. long t3 = (long)xx[15] + xx23;
  94. long t4 = xx17 + xx21;
  95. long t5 = xx21 - xx23;
  96. long t6 = xx22 - xx23;
  97. long t7 = t0 + t5;
  98. long cc = 0;
  99. cc += (long)xx[0] + t7;
  100. z[0] = (uint)cc;
  101. cc >>= 32;
  102. cc += (long)xx[1] + xx23 - t0 + t1;
  103. z[1] = (uint)cc;
  104. cc >>= 32;
  105. cc += (long)xx[2] - xx21 - t1 + t2;
  106. z[2] = (uint)cc;
  107. cc >>= 32;
  108. cc += (long)xx[3] - t2 + t3 + t7;
  109. z[3] = (uint)cc;
  110. cc >>= 32;
  111. cc += (long)xx[4] + xx16 + xx21 + t1 - t3 + t7;
  112. z[4] = (uint)cc;
  113. cc >>= 32;
  114. cc += (long)xx[5] - xx16 + t1 + t2 + t4;
  115. z[5] = (uint)cc;
  116. cc >>= 32;
  117. cc += (long)xx[6] + xx18 - xx17 + t2 + t3;
  118. z[6] = (uint)cc;
  119. cc >>= 32;
  120. cc += (long)xx[7] + xx16 + xx19 - xx18 + t3;
  121. z[7] = (uint)cc;
  122. cc >>= 32;
  123. cc += (long)xx[8] + xx16 + xx17 + xx20 - xx19;
  124. z[8] = (uint)cc;
  125. cc >>= 32;
  126. cc += (long)xx[9] + xx18 - xx20 + t4;
  127. z[9] = (uint)cc;
  128. cc >>= 32;
  129. cc += (long)xx[10] + xx18 + xx19 - t5 + t6;
  130. z[10] = (uint)cc;
  131. cc >>= 32;
  132. cc += (long)xx[11] + xx19 + xx20 - t6;
  133. z[11] = (uint)cc;
  134. cc >>= 32;
  135. cc += n;
  136. Debug.Assert(cc >= 0);
  137. Reduce32((uint)cc, z);
  138. }
  139. public static void Reduce32(uint x, uint[] z)
  140. {
  141. long cc = 0;
  142. if (x != 0)
  143. {
  144. long xx12 = x;
  145. cc += (long)z[0] + xx12;
  146. z[0] = (uint)cc;
  147. cc >>= 32;
  148. cc += (long)z[1] - xx12;
  149. z[1] = (uint)cc;
  150. cc >>= 32;
  151. if (cc != 0)
  152. {
  153. cc += (long)z[2];
  154. z[2] = (uint)cc;
  155. cc >>= 32;
  156. }
  157. cc += (long)z[3] + xx12;
  158. z[3] = (uint)cc;
  159. cc >>= 32;
  160. cc += (long)z[4] + xx12;
  161. z[4] = (uint)cc;
  162. cc >>= 32;
  163. Debug.Assert(cc == 0 || cc == 1);
  164. }
  165. if ((cc != 0 && Nat.IncAt(12, z, 5) != 0)
  166. || (z[11] == P11 && Nat.Gte(12, z, P)))
  167. {
  168. AddPInvTo(z);
  169. }
  170. }
  171. public static void Square(uint[] x, uint[] z)
  172. {
  173. uint[] tt = Nat.Create(24);
  174. Nat384.Square(x, tt);
  175. Reduce(tt, z);
  176. }
  177. public static void SquareN(uint[] x, int n, uint[] z)
  178. {
  179. Debug.Assert(n > 0);
  180. uint[] tt = Nat.Create(24);
  181. Nat384.Square(x, tt);
  182. Reduce(tt, z);
  183. while (--n > 0)
  184. {
  185. Nat384.Square(z, tt);
  186. Reduce(tt, z);
  187. }
  188. }
  189. public static void Subtract(uint[] x, uint[] y, uint[] z)
  190. {
  191. int c = Nat.Sub(12, x, y, z);
  192. if (c != 0)
  193. {
  194. SubPInvFrom(z);
  195. }
  196. }
  197. public static void SubtractExt(uint[] xx, uint[] yy, uint[] zz)
  198. {
  199. int c = Nat.Sub(24, xx, yy, zz);
  200. if (c != 0)
  201. {
  202. if (Nat.SubFrom(PExtInv.Length, PExtInv, zz) != 0)
  203. {
  204. Nat.DecAt(24, zz, PExtInv.Length);
  205. }
  206. }
  207. }
  208. public static void Twice(uint[] x, uint[] z)
  209. {
  210. uint c = Nat.ShiftUpBit(12, x, 0, z);
  211. if (c != 0 || (z[11] == P11 && Nat.Gte(12, z, P)))
  212. {
  213. AddPInvTo(z);
  214. }
  215. }
  216. private static void AddPInvTo(uint[] z)
  217. {
  218. long c = (long)z[0] + 1;
  219. z[0] = (uint)c;
  220. c >>= 32;
  221. c += (long)z[1] - 1;
  222. z[1] = (uint)c;
  223. c >>= 32;
  224. if (c != 0)
  225. {
  226. c += (long)z[2];
  227. z[2] = (uint)c;
  228. c >>= 32;
  229. }
  230. c += (long)z[3] + 1;
  231. z[3] = (uint)c;
  232. c >>= 32;
  233. c += (long)z[4] + 1;
  234. z[4] = (uint)c;
  235. c >>= 32;
  236. if (c != 0)
  237. {
  238. Nat.IncAt(12, z, 5);
  239. }
  240. }
  241. private static void SubPInvFrom(uint[] z)
  242. {
  243. long c = (long)z[0] - 1;
  244. z[0] = (uint)c;
  245. c >>= 32;
  246. c += (long)z[1] + 1;
  247. z[1] = (uint)c;
  248. c >>= 32;
  249. if (c != 0)
  250. {
  251. c += (long)z[2];
  252. z[2] = (uint)c;
  253. c >>= 32;
  254. }
  255. c += (long)z[3] - 1;
  256. z[3] = (uint)c;
  257. c >>= 32;
  258. c += (long)z[4] - 1;
  259. z[4] = (uint)c;
  260. c >>= 32;
  261. if (c != 0)
  262. {
  263. Nat.DecAt(12, z, 5);
  264. }
  265. }
  266. }
  267. }
  268. #endif