SecP224R1Field.cs 8.0 KB

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