SecP256R1Field.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Diagnostics;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Raw;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
  8. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Custom.Sec
  9. {
  10. internal class SecP256R1Field
  11. {
  12. // 2^256 - 2^224 + 2^192 + 2^96 - 1
  13. internal static readonly uint[] P = new uint[]{ 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
  14. 0x00000000, 0x00000001, 0xFFFFFFFF };
  15. private static readonly uint[] PExt = new uint[]{ 0x00000001, 0x00000000, 0x00000000, 0xFFFFFFFE, 0xFFFFFFFF,
  16. 0xFFFFFFFF, 0xFFFFFFFE, 0x00000001, 0xFFFFFFFE, 0x00000001, 0xFFFFFFFE, 0x00000001, 0x00000001, 0xFFFFFFFE,
  17. 0x00000002, 0xFFFFFFFE };
  18. private const uint P7 = 0xFFFFFFFF;
  19. private const uint PExt15 = 0xFFFFFFFE;
  20. public static void Add(uint[] x, uint[] y, uint[] z)
  21. {
  22. uint c = Nat256.Add(x, y, z);
  23. if (c != 0 || (z[7] == P7 && Nat256.Gte(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(16, xx, yy, zz);
  31. if (c != 0 || (zz[15] >= PExt15 && Nat.Gte(16, zz, PExt)))
  32. {
  33. Nat.SubFrom(16, PExt, zz);
  34. }
  35. }
  36. public static void AddOne(uint[] x, uint[] z)
  37. {
  38. uint c = Nat.Inc(8, x, z);
  39. if (c != 0 || (z[7] == P7 && Nat256.Gte(z, P)))
  40. {
  41. AddPInvTo(z);
  42. }
  43. }
  44. public static uint[] FromBigInteger(BigInteger x)
  45. {
  46. uint[] z = Nat.FromBigInteger(256, x);
  47. if (z[7] == P7 && Nat256.Gte(z, P))
  48. {
  49. Nat256.SubFrom(P, z);
  50. }
  51. return z;
  52. }
  53. public static void Half(uint[] x, uint[] z)
  54. {
  55. if ((x[0] & 1) == 0)
  56. {
  57. Nat.ShiftDownBit(8, x, 0, z);
  58. }
  59. else
  60. {
  61. uint c = Nat256.Add(x, P, z);
  62. Nat.ShiftDownBit(8, z, c);
  63. }
  64. }
  65. public static void Inv(uint[] x, uint[] z)
  66. {
  67. Mod.CheckedModOddInverse(P, x, z);
  68. }
  69. public static int IsZero(uint[] x)
  70. {
  71. uint d = 0;
  72. for (int i = 0; i < 8; ++i)
  73. {
  74. d |= x[i];
  75. }
  76. d = (d >> 1) | (d & 1);
  77. return ((int)d - 1) >> 31;
  78. }
  79. public static void Multiply(uint[] x, uint[] y, uint[] z)
  80. {
  81. uint[] tt = Nat256.CreateExt();
  82. Nat256.Mul(x, y, tt);
  83. Reduce(tt, z);
  84. }
  85. public static void Multiply(uint[] x, uint[] y, uint[] z, uint[] tt)
  86. {
  87. Nat256.Mul(x, y, tt);
  88. Reduce(tt, z);
  89. }
  90. public static void MultiplyAddToExt(uint[] x, uint[] y, uint[] zz)
  91. {
  92. uint c = Nat256.MulAddTo(x, y, zz);
  93. if (c != 0 || (zz[15] >= PExt15 && Nat.Gte(16, zz, PExt)))
  94. {
  95. Nat.SubFrom(16, PExt, zz);
  96. }
  97. }
  98. public static void Negate(uint[] x, uint[] z)
  99. {
  100. if (0 != IsZero(x))
  101. {
  102. Nat256.Sub(P, P, z);
  103. }
  104. else
  105. {
  106. Nat256.Sub(P, x, z);
  107. }
  108. }
  109. public static void Random(SecureRandom r, uint[] z)
  110. {
  111. byte[] bb = new byte[8 * 4];
  112. do
  113. {
  114. r.NextBytes(bb);
  115. Pack.LE_To_UInt32(bb, 0, z, 0, 8);
  116. }
  117. while (0 == Nat.LessThan(8, z, P));
  118. }
  119. public static void RandomMult(SecureRandom r, uint[] z)
  120. {
  121. do
  122. {
  123. Random(r, z);
  124. }
  125. while (0 != IsZero(z));
  126. }
  127. public static void Reduce(uint[] xx, uint[] z)
  128. {
  129. long xx08 = xx[8], xx09 = xx[9], xx10 = xx[10], xx11 = xx[11];
  130. long xx12 = xx[12], xx13 = xx[13], xx14 = xx[14], xx15 = xx[15];
  131. const long n = 6;
  132. xx08 -= n;
  133. long t0 = xx08 + xx09;
  134. long t1 = xx09 + xx10;
  135. long t2 = xx10 + xx11 - xx15;
  136. long t3 = xx11 + xx12;
  137. long t4 = xx12 + xx13;
  138. long t5 = xx13 + xx14;
  139. long t6 = xx14 + xx15;
  140. long t7 = t5 - t0;
  141. long cc = 0;
  142. cc += (long)xx[0] - t3 - t7;
  143. z[0] = (uint)cc;
  144. cc >>= 32;
  145. cc += (long)xx[1] + t1 - t4 - t6;
  146. z[1] = (uint)cc;
  147. cc >>= 32;
  148. cc += (long)xx[2] + t2 - t5;
  149. z[2] = (uint)cc;
  150. cc >>= 32;
  151. cc += (long)xx[3] + (t3 << 1) + t7 - t6;
  152. z[3] = (uint)cc;
  153. cc >>= 32;
  154. cc += (long)xx[4] + (t4 << 1) + xx14 - t1;
  155. z[4] = (uint)cc;
  156. cc >>= 32;
  157. cc += (long)xx[5] + (t5 << 1) - t2;
  158. z[5] = (uint)cc;
  159. cc >>= 32;
  160. cc += (long)xx[6] + (t6 << 1) + t7;
  161. z[6] = (uint)cc;
  162. cc >>= 32;
  163. cc += (long)xx[7] + (xx15 << 1) + xx08 - t2 - t4;
  164. z[7] = (uint)cc;
  165. cc >>= 32;
  166. cc += n;
  167. Debug.Assert(cc >= 0);
  168. Reduce32((uint)cc, z);
  169. }
  170. public static void Reduce32(uint x, uint[] z)
  171. {
  172. long cc = 0;
  173. if (x != 0)
  174. {
  175. long xx08 = x;
  176. cc += (long)z[0] + xx08;
  177. z[0] = (uint)cc;
  178. cc >>= 32;
  179. if (cc != 0)
  180. {
  181. cc += (long)z[1];
  182. z[1] = (uint)cc;
  183. cc >>= 32;
  184. cc += (long)z[2];
  185. z[2] = (uint)cc;
  186. cc >>= 32;
  187. }
  188. cc += (long)z[3] - xx08;
  189. z[3] = (uint)cc;
  190. cc >>= 32;
  191. if (cc != 0)
  192. {
  193. cc += (long)z[4];
  194. z[4] = (uint)cc;
  195. cc >>= 32;
  196. cc += (long)z[5];
  197. z[5] = (uint)cc;
  198. cc >>= 32;
  199. }
  200. cc += (long)z[6] - xx08;
  201. z[6] = (uint)cc;
  202. cc >>= 32;
  203. cc += (long)z[7] + xx08;
  204. z[7] = (uint)cc;
  205. cc >>= 32;
  206. Debug.Assert(cc == 0 || cc == 1);
  207. }
  208. if (cc != 0 || (z[7] == P7 && Nat256.Gte(z, P)))
  209. {
  210. AddPInvTo(z);
  211. }
  212. }
  213. public static void Square(uint[] x, uint[] z)
  214. {
  215. uint[] tt = Nat256.CreateExt();
  216. Nat256.Square(x, tt);
  217. Reduce(tt, z);
  218. }
  219. public static void Square(uint[] x, uint[] z, uint[] tt)
  220. {
  221. Nat256.Square(x, tt);
  222. Reduce(tt, z);
  223. }
  224. public static void SquareN(uint[] x, int n, uint[] z)
  225. {
  226. Debug.Assert(n > 0);
  227. uint[] tt = Nat256.CreateExt();
  228. Nat256.Square(x, tt);
  229. Reduce(tt, z);
  230. while (--n > 0)
  231. {
  232. Nat256.Square(z, tt);
  233. Reduce(tt, z);
  234. }
  235. }
  236. public static void SquareN(uint[] x, int n, uint[] z, uint[] tt)
  237. {
  238. Debug.Assert(n > 0);
  239. Nat256.Square(x, tt);
  240. Reduce(tt, z);
  241. while (--n > 0)
  242. {
  243. Nat256.Square(z, tt);
  244. Reduce(tt, z);
  245. }
  246. }
  247. public static void Subtract(uint[] x, uint[] y, uint[] z)
  248. {
  249. int c = Nat256.Sub(x, y, z);
  250. if (c != 0)
  251. {
  252. SubPInvFrom(z);
  253. }
  254. }
  255. public static void SubtractExt(uint[] xx, uint[] yy, uint[] zz)
  256. {
  257. int c = Nat.Sub(16, xx, yy, zz);
  258. if (c != 0)
  259. {
  260. Nat.AddTo(16, PExt, zz);
  261. }
  262. }
  263. public static void Twice(uint[] x, uint[] z)
  264. {
  265. uint c = Nat.ShiftUpBit(8, x, 0, z);
  266. if (c != 0 || (z[7] == P7 && Nat256.Gte(z, P)))
  267. {
  268. AddPInvTo(z);
  269. }
  270. }
  271. private static void AddPInvTo(uint[] z)
  272. {
  273. long c = (long)z[0] + 1;
  274. z[0] = (uint)c;
  275. c >>= 32;
  276. if (c != 0)
  277. {
  278. c += (long)z[1];
  279. z[1] = (uint)c;
  280. c >>= 32;
  281. c += (long)z[2];
  282. z[2] = (uint)c;
  283. c >>= 32;
  284. }
  285. c += (long)z[3] - 1;
  286. z[3] = (uint)c;
  287. c >>= 32;
  288. if (c != 0)
  289. {
  290. c += (long)z[4];
  291. z[4] = (uint)c;
  292. c >>= 32;
  293. c += (long)z[5];
  294. z[5] = (uint)c;
  295. c >>= 32;
  296. }
  297. c += (long)z[6] - 1;
  298. z[6] = (uint)c;
  299. c >>= 32;
  300. c += (long)z[7] + 1;
  301. z[7] = (uint)c;
  302. //c >>= 32;
  303. }
  304. private static void SubPInvFrom(uint[] z)
  305. {
  306. long c = (long)z[0] - 1;
  307. z[0] = (uint)c;
  308. c >>= 32;
  309. if (c != 0)
  310. {
  311. c += (long)z[1];
  312. z[1] = (uint)c;
  313. c >>= 32;
  314. c += (long)z[2];
  315. z[2] = (uint)c;
  316. c >>= 32;
  317. }
  318. c += (long)z[3] + 1;
  319. z[3] = (uint)c;
  320. c >>= 32;
  321. if (c != 0)
  322. {
  323. c += (long)z[4];
  324. z[4] = (uint)c;
  325. c >>= 32;
  326. c += (long)z[5];
  327. z[5] = (uint)c;
  328. c >>= 32;
  329. }
  330. c += (long)z[6] + 1;
  331. z[6] = (uint)c;
  332. c >>= 32;
  333. c += (long)z[7] - 1;
  334. z[7] = (uint)c;
  335. //c >>= 32;
  336. }
  337. }
  338. }
  339. #pragma warning restore
  340. #endif