SecP192R1Field.cs 8.5 KB

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