Nat160.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Math.Raw
  7. {
  8. internal abstract class Nat160
  9. {
  10. private const ulong M = 0xFFFFFFFFUL;
  11. public static uint Add(uint[] x, uint[] y, uint[] z)
  12. {
  13. ulong c = 0;
  14. c += (ulong)x[0] + y[0];
  15. z[0] = (uint)c;
  16. c >>= 32;
  17. c += (ulong)x[1] + y[1];
  18. z[1] = (uint)c;
  19. c >>= 32;
  20. c += (ulong)x[2] + y[2];
  21. z[2] = (uint)c;
  22. c >>= 32;
  23. c += (ulong)x[3] + y[3];
  24. z[3] = (uint)c;
  25. c >>= 32;
  26. c += (ulong)x[4] + y[4];
  27. z[4] = (uint)c;
  28. c >>= 32;
  29. return (uint)c;
  30. }
  31. public static uint AddBothTo(uint[] x, uint[] y, uint[] z)
  32. {
  33. ulong c = 0;
  34. c += (ulong)x[0] + y[0] + z[0];
  35. z[0] = (uint)c;
  36. c >>= 32;
  37. c += (ulong)x[1] + y[1] + z[1];
  38. z[1] = (uint)c;
  39. c >>= 32;
  40. c += (ulong)x[2] + y[2] + z[2];
  41. z[2] = (uint)c;
  42. c >>= 32;
  43. c += (ulong)x[3] + y[3] + z[3];
  44. z[3] = (uint)c;
  45. c >>= 32;
  46. c += (ulong)x[4] + y[4] + z[4];
  47. z[4] = (uint)c;
  48. c >>= 32;
  49. return (uint)c;
  50. }
  51. public static uint AddTo(uint[] x, uint[] z)
  52. {
  53. ulong c = 0;
  54. c += (ulong)x[0] + z[0];
  55. z[0] = (uint)c;
  56. c >>= 32;
  57. c += (ulong)x[1] + z[1];
  58. z[1] = (uint)c;
  59. c >>= 32;
  60. c += (ulong)x[2] + z[2];
  61. z[2] = (uint)c;
  62. c >>= 32;
  63. c += (ulong)x[3] + z[3];
  64. z[3] = (uint)c;
  65. c >>= 32;
  66. c += (ulong)x[4] + z[4];
  67. z[4] = (uint)c;
  68. c >>= 32;
  69. return (uint)c;
  70. }
  71. public static uint AddTo(uint[] x, int xOff, uint[] z, int zOff, uint cIn)
  72. {
  73. ulong c = cIn;
  74. c += (ulong)x[xOff + 0] + z[zOff + 0];
  75. z[zOff + 0] = (uint)c;
  76. c >>= 32;
  77. c += (ulong)x[xOff + 1] + z[zOff + 1];
  78. z[zOff + 1] = (uint)c;
  79. c >>= 32;
  80. c += (ulong)x[xOff + 2] + z[zOff + 2];
  81. z[zOff + 2] = (uint)c;
  82. c >>= 32;
  83. c += (ulong)x[xOff + 3] + z[zOff + 3];
  84. z[zOff + 3] = (uint)c;
  85. c >>= 32;
  86. c += (ulong)x[xOff + 4] + z[zOff + 4];
  87. z[zOff + 4] = (uint)c;
  88. c >>= 32;
  89. c += (ulong)x[xOff + 5] + z[zOff + 5];
  90. return (uint)c;
  91. }
  92. public static uint AddToEachOther(uint[] u, int uOff, uint[] v, int vOff)
  93. {
  94. ulong c = 0;
  95. c += (ulong)u[uOff + 0] + v[vOff + 0];
  96. u[uOff + 0] = (uint)c;
  97. v[vOff + 0] = (uint)c;
  98. c >>= 32;
  99. c += (ulong)u[uOff + 1] + v[vOff + 1];
  100. u[uOff + 1] = (uint)c;
  101. v[vOff + 1] = (uint)c;
  102. c >>= 32;
  103. c += (ulong)u[uOff + 2] + v[vOff + 2];
  104. u[uOff + 2] = (uint)c;
  105. v[vOff + 2] = (uint)c;
  106. c >>= 32;
  107. c += (ulong)u[uOff + 3] + v[vOff + 3];
  108. u[uOff + 3] = (uint)c;
  109. v[vOff + 3] = (uint)c;
  110. c >>= 32;
  111. c += (ulong)u[uOff + 4] + v[vOff + 4];
  112. u[uOff + 4] = (uint)c;
  113. v[vOff + 4] = (uint)c;
  114. c >>= 32;
  115. return (uint)c;
  116. }
  117. public static void Copy(uint[] x, uint[] z)
  118. {
  119. z[0] = x[0];
  120. z[1] = x[1];
  121. z[2] = x[2];
  122. z[3] = x[3];
  123. z[4] = x[4];
  124. }
  125. public static void Copy(uint[] x, int xOff, uint[] z, int zOff)
  126. {
  127. z[zOff + 0] = x[xOff + 0];
  128. z[zOff + 1] = x[xOff + 1];
  129. z[zOff + 2] = x[xOff + 2];
  130. z[zOff + 3] = x[xOff + 3];
  131. z[zOff + 4] = x[xOff + 4];
  132. }
  133. public static uint[] Create()
  134. {
  135. return new uint[5];
  136. }
  137. public static uint[] CreateExt()
  138. {
  139. return new uint[10];
  140. }
  141. public static bool Diff(uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff)
  142. {
  143. bool pos = Gte(x, xOff, y, yOff);
  144. if (pos)
  145. {
  146. Sub(x, xOff, y, yOff, z, zOff);
  147. }
  148. else
  149. {
  150. Sub(y, yOff, x, xOff, z, zOff);
  151. }
  152. return pos;
  153. }
  154. public static bool Eq(uint[] x, uint[] y)
  155. {
  156. for (int i = 4; i >= 0; --i)
  157. {
  158. if (x[i] != y[i])
  159. return false;
  160. }
  161. return true;
  162. }
  163. public static uint[] FromBigInteger(BigInteger x)
  164. {
  165. if (x.SignValue < 0 || x.BitLength > 160)
  166. throw new ArgumentException();
  167. uint[] z = Create();
  168. int i = 0;
  169. while (x.SignValue != 0)
  170. {
  171. z[i++] = (uint)x.IntValue;
  172. x = x.ShiftRight(32);
  173. }
  174. return z;
  175. }
  176. public static uint GetBit(uint[] x, int bit)
  177. {
  178. if (bit == 0)
  179. {
  180. return x[0] & 1;
  181. }
  182. int w = bit >> 5;
  183. if (w < 0 || w >= 5)
  184. {
  185. return 0;
  186. }
  187. int b = bit & 31;
  188. return (x[w] >> b) & 1;
  189. }
  190. public static bool Gte(uint[] x, uint[] y)
  191. {
  192. for (int i = 4; i >= 0; --i)
  193. {
  194. uint x_i = x[i], y_i = y[i];
  195. if (x_i < y_i)
  196. return false;
  197. if (x_i > y_i)
  198. return true;
  199. }
  200. return true;
  201. }
  202. public static bool Gte(uint[] x, int xOff, uint[] y, int yOff)
  203. {
  204. for (int i = 4; i >= 0; --i)
  205. {
  206. uint x_i = x[xOff + i], y_i = y[yOff + i];
  207. if (x_i < y_i)
  208. return false;
  209. if (x_i > y_i)
  210. return true;
  211. }
  212. return true;
  213. }
  214. public static bool IsOne(uint[] x)
  215. {
  216. if (x[0] != 1)
  217. {
  218. return false;
  219. }
  220. for (int i = 1; i < 5; ++i)
  221. {
  222. if (x[i] != 0)
  223. {
  224. return false;
  225. }
  226. }
  227. return true;
  228. }
  229. public static bool IsZero(uint[] x)
  230. {
  231. for (int i = 0; i < 5; ++i)
  232. {
  233. if (x[i] != 0)
  234. {
  235. return false;
  236. }
  237. }
  238. return true;
  239. }
  240. public static void Mul(uint[] x, uint[] y, uint[] zz)
  241. {
  242. ulong y_0 = y[0];
  243. ulong y_1 = y[1];
  244. ulong y_2 = y[2];
  245. ulong y_3 = y[3];
  246. ulong y_4 = y[4];
  247. {
  248. ulong c = 0, x_0 = x[0];
  249. c += x_0 * y_0;
  250. zz[0] = (uint)c;
  251. c >>= 32;
  252. c += x_0 * y_1;
  253. zz[1] = (uint)c;
  254. c >>= 32;
  255. c += x_0 * y_2;
  256. zz[2] = (uint)c;
  257. c >>= 32;
  258. c += x_0 * y_3;
  259. zz[3] = (uint)c;
  260. c >>= 32;
  261. c += x_0 * y_4;
  262. zz[4] = (uint)c;
  263. c >>= 32;
  264. zz[5] = (uint)c;
  265. }
  266. for (int i = 1; i < 5; ++i)
  267. {
  268. ulong c = 0, x_i = x[i];
  269. c += x_i * y_0 + zz[i + 0];
  270. zz[i + 0] = (uint)c;
  271. c >>= 32;
  272. c += x_i * y_1 + zz[i + 1];
  273. zz[i + 1] = (uint)c;
  274. c >>= 32;
  275. c += x_i * y_2 + zz[i + 2];
  276. zz[i + 2] = (uint)c;
  277. c >>= 32;
  278. c += x_i * y_3 + zz[i + 3];
  279. zz[i + 3] = (uint)c;
  280. c >>= 32;
  281. c += x_i * y_4 + zz[i + 4];
  282. zz[i + 4] = (uint)c;
  283. c >>= 32;
  284. zz[i + 5] = (uint)c;
  285. }
  286. }
  287. public static void Mul(uint[] x, int xOff, uint[] y, int yOff, uint[] zz, int zzOff)
  288. {
  289. ulong y_0 = y[yOff + 0];
  290. ulong y_1 = y[yOff + 1];
  291. ulong y_2 = y[yOff + 2];
  292. ulong y_3 = y[yOff + 3];
  293. ulong y_4 = y[yOff + 4];
  294. {
  295. ulong c = 0, x_0 = x[xOff + 0];
  296. c += x_0 * y_0;
  297. zz[zzOff + 0] = (uint)c;
  298. c >>= 32;
  299. c += x_0 * y_1;
  300. zz[zzOff + 1] = (uint)c;
  301. c >>= 32;
  302. c += x_0 * y_2;
  303. zz[zzOff + 2] = (uint)c;
  304. c >>= 32;
  305. c += x_0 * y_3;
  306. zz[zzOff + 3] = (uint)c;
  307. c >>= 32;
  308. c += x_0 * y_4;
  309. zz[zzOff + 4] = (uint)c;
  310. c >>= 32;
  311. zz[zzOff + 5] = (uint)c;
  312. }
  313. for (int i = 1; i < 5; ++i)
  314. {
  315. ++zzOff;
  316. ulong c = 0, x_i = x[xOff + i];
  317. c += x_i * y_0 + zz[zzOff + 0];
  318. zz[zzOff + 0] = (uint)c;
  319. c >>= 32;
  320. c += x_i * y_1 + zz[zzOff + 1];
  321. zz[zzOff + 1] = (uint)c;
  322. c >>= 32;
  323. c += x_i * y_2 + zz[zzOff + 2];
  324. zz[zzOff + 2] = (uint)c;
  325. c >>= 32;
  326. c += x_i * y_3 + zz[zzOff + 3];
  327. zz[zzOff + 3] = (uint)c;
  328. c >>= 32;
  329. c += x_i * y_4 + zz[zzOff + 4];
  330. zz[zzOff + 4] = (uint)c;
  331. c >>= 32;
  332. zz[zzOff + 5] = (uint)c;
  333. }
  334. }
  335. public static uint MulAddTo(uint[] x, uint[] y, uint[] zz)
  336. {
  337. ulong y_0 = y[0];
  338. ulong y_1 = y[1];
  339. ulong y_2 = y[2];
  340. ulong y_3 = y[3];
  341. ulong y_4 = y[4];
  342. ulong zc = 0;
  343. for (int i = 0; i < 5; ++i)
  344. {
  345. ulong c = 0, x_i = x[i];
  346. c += x_i * y_0 + zz[i + 0];
  347. zz[i + 0] = (uint)c;
  348. c >>= 32;
  349. c += x_i * y_1 + zz[i + 1];
  350. zz[i + 1] = (uint)c;
  351. c >>= 32;
  352. c += x_i * y_2 + zz[i + 2];
  353. zz[i + 2] = (uint)c;
  354. c >>= 32;
  355. c += x_i * y_3 + zz[i + 3];
  356. zz[i + 3] = (uint)c;
  357. c >>= 32;
  358. c += x_i * y_4 + zz[i + 4];
  359. zz[i + 4] = (uint)c;
  360. c >>= 32;
  361. c += zc + zz[i + 5];
  362. zz[i + 5] = (uint)c;
  363. zc = c >> 32;
  364. }
  365. return (uint)zc;
  366. }
  367. public static uint MulAddTo(uint[] x, int xOff, uint[] y, int yOff, uint[] zz, int zzOff)
  368. {
  369. ulong y_0 = y[yOff + 0];
  370. ulong y_1 = y[yOff + 1];
  371. ulong y_2 = y[yOff + 2];
  372. ulong y_3 = y[yOff + 3];
  373. ulong y_4 = y[yOff + 4];
  374. ulong zc = 0;
  375. for (int i = 0; i < 5; ++i)
  376. {
  377. ulong c = 0, x_i = x[xOff + i];
  378. c += x_i * y_0 + zz[zzOff + 0];
  379. zz[zzOff + 0] = (uint)c;
  380. c >>= 32;
  381. c += x_i * y_1 + zz[zzOff + 1];
  382. zz[zzOff + 1] = (uint)c;
  383. c >>= 32;
  384. c += x_i * y_2 + zz[zzOff + 2];
  385. zz[zzOff + 2] = (uint)c;
  386. c >>= 32;
  387. c += x_i * y_3 + zz[zzOff + 3];
  388. zz[zzOff + 3] = (uint)c;
  389. c >>= 32;
  390. c += x_i * y_4 + zz[zzOff + 4];
  391. zz[zzOff + 4] = (uint)c;
  392. c >>= 32;
  393. c += zc + zz[zzOff + 5];
  394. zz[zzOff + 5] = (uint)c;
  395. zc = c >> 32;
  396. ++zzOff;
  397. }
  398. return (uint)zc;
  399. }
  400. public static ulong Mul33Add(uint w, uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff)
  401. {
  402. Debug.Assert(w >> 31 == 0);
  403. ulong c = 0, wVal = w;
  404. ulong x0 = x[xOff + 0];
  405. c += wVal * x0 + y[yOff + 0];
  406. z[zOff + 0] = (uint)c;
  407. c >>= 32;
  408. ulong x1 = x[xOff + 1];
  409. c += wVal * x1 + x0 + y[yOff + 1];
  410. z[zOff + 1] = (uint)c;
  411. c >>= 32;
  412. ulong x2 = x[xOff + 2];
  413. c += wVal * x2 + x1 + y[yOff + 2];
  414. z[zOff + 2] = (uint)c;
  415. c >>= 32;
  416. ulong x3 = x[xOff + 3];
  417. c += wVal * x3 + x2 + y[yOff + 3];
  418. z[zOff + 3] = (uint)c;
  419. c >>= 32;
  420. ulong x4 = x[xOff + 4];
  421. c += wVal * x4 + x3 + y[yOff + 4];
  422. z[zOff + 4] = (uint)c;
  423. c >>= 32;
  424. c += x4;
  425. return c;
  426. }
  427. public static uint MulWordAddExt(uint x, uint[] yy, int yyOff, uint[] zz, int zzOff)
  428. {
  429. Debug.Assert(yyOff <= 5);
  430. Debug.Assert(zzOff <= 5);
  431. ulong c = 0, xVal = x;
  432. c += xVal * yy[yyOff + 0] + zz[zzOff + 0];
  433. zz[zzOff + 0] = (uint)c;
  434. c >>= 32;
  435. c += xVal * yy[yyOff + 1] + zz[zzOff + 1];
  436. zz[zzOff + 1] = (uint)c;
  437. c >>= 32;
  438. c += xVal * yy[yyOff + 2] + zz[zzOff + 2];
  439. zz[zzOff + 2] = (uint)c;
  440. c >>= 32;
  441. c += xVal * yy[yyOff + 3] + zz[zzOff + 3];
  442. zz[zzOff + 3] = (uint)c;
  443. c >>= 32;
  444. c += xVal * yy[yyOff + 4] + zz[zzOff + 4];
  445. zz[zzOff + 4] = (uint)c;
  446. c >>= 32;
  447. return (uint)c;
  448. }
  449. public static uint Mul33DWordAdd(uint x, ulong y, uint[] z, int zOff)
  450. {
  451. Debug.Assert(x >> 31 == 0);
  452. Debug.Assert(zOff <= 1);
  453. ulong c = 0, xVal = x;
  454. ulong y00 = y & M;
  455. c += xVal * y00 + z[zOff + 0];
  456. z[zOff + 0] = (uint)c;
  457. c >>= 32;
  458. ulong y01 = y >> 32;
  459. c += xVal * y01 + y00 + z[zOff + 1];
  460. z[zOff + 1] = (uint)c;
  461. c >>= 32;
  462. c += y01 + z[zOff + 2];
  463. z[zOff + 2] = (uint)c;
  464. c >>= 32;
  465. c += z[zOff + 3];
  466. z[zOff + 3] = (uint)c;
  467. c >>= 32;
  468. return c == 0 ? 0 : Nat.IncAt(5, z, zOff, 4);
  469. }
  470. public static uint Mul33WordAdd(uint x, uint y, uint[] z, int zOff)
  471. {
  472. Debug.Assert(x >> 31 == 0);
  473. Debug.Assert(zOff <= 2);
  474. ulong c = 0, yVal = y;
  475. c += yVal * x + z[zOff + 0];
  476. z[zOff + 0] = (uint)c;
  477. c >>= 32;
  478. c += yVal + z[zOff + 1];
  479. z[zOff + 1] = (uint)c;
  480. c >>= 32;
  481. c += z[zOff + 2];
  482. z[zOff + 2] = (uint)c;
  483. c >>= 32;
  484. return c == 0 ? 0 : Nat.IncAt(5, z, zOff, 3);
  485. }
  486. public static uint MulWordDwordAdd(uint x, ulong y, uint[] z, int zOff)
  487. {
  488. Debug.Assert(zOff <= 2);
  489. ulong c = 0, xVal = x;
  490. c += xVal * y + z[zOff + 0];
  491. z[zOff + 0] = (uint)c;
  492. c >>= 32;
  493. c += xVal * (y >> 32) + z[zOff + 1];
  494. z[zOff + 1] = (uint)c;
  495. c >>= 32;
  496. c += z[zOff + 2];
  497. z[zOff + 2] = (uint)c;
  498. c >>= 32;
  499. return c == 0 ? 0 : Nat.IncAt(5, z, zOff, 3);
  500. }
  501. public static uint MulWordsAdd(uint x, uint y, uint[] z, int zOff)
  502. {
  503. Debug.Assert(zOff <= 3);
  504. ulong c = 0, xVal = x, yVal = y;
  505. c += yVal * xVal + z[zOff + 0];
  506. z[zOff + 0] = (uint)c;
  507. c >>= 32;
  508. c += z[zOff + 1];
  509. z[zOff + 1] = (uint)c;
  510. c >>= 32;
  511. return c == 0 ? 0 : Nat.IncAt(5, z, zOff, 2);
  512. }
  513. public static uint MulWord(uint x, uint[] y, uint[] z, int zOff)
  514. {
  515. ulong c = 0, xVal = x;
  516. int i = 0;
  517. do
  518. {
  519. c += xVal * y[i];
  520. z[zOff + i] = (uint)c;
  521. c >>= 32;
  522. }
  523. while (++i < 5);
  524. return (uint)c;
  525. }
  526. public static void Square(uint[] x, uint[] zz)
  527. {
  528. ulong x_0 = x[0];
  529. ulong zz_1;
  530. uint c = 0, w;
  531. {
  532. int i = 4, j = 10;
  533. do
  534. {
  535. ulong xVal = x[i--];
  536. ulong p = xVal * xVal;
  537. zz[--j] = (c << 31) | (uint)(p >> 33);
  538. zz[--j] = (uint)(p >> 1);
  539. c = (uint)p;
  540. }
  541. while (i > 0);
  542. {
  543. ulong p = x_0 * x_0;
  544. zz_1 = (ulong)(c << 31) | (p >> 33);
  545. zz[0] = (uint)p;
  546. c = (uint)(p >> 32) & 1;
  547. }
  548. }
  549. ulong x_1 = x[1];
  550. ulong zz_2 = zz[2];
  551. {
  552. zz_1 += x_1 * x_0;
  553. w = (uint)zz_1;
  554. zz[1] = (w << 1) | c;
  555. c = w >> 31;
  556. zz_2 += zz_1 >> 32;
  557. }
  558. ulong x_2 = x[2];
  559. ulong zz_3 = zz[3];
  560. ulong zz_4 = zz[4];
  561. {
  562. zz_2 += x_2 * x_0;
  563. w = (uint)zz_2;
  564. zz[2] = (w << 1) | c;
  565. c = w >> 31;
  566. zz_3 += (zz_2 >> 32) + x_2 * x_1;
  567. zz_4 += zz_3 >> 32;
  568. zz_3 &= M;
  569. }
  570. ulong x_3 = x[3];
  571. ulong zz_5 = zz[5] + (zz_4 >> 32); zz_4 &= M;
  572. ulong zz_6 = zz[6] + (zz_5 >> 32); zz_5 &= M;
  573. {
  574. zz_3 += x_3 * x_0;
  575. w = (uint)zz_3;
  576. zz[3] = (w << 1) | c;
  577. c = w >> 31;
  578. zz_4 += (zz_3 >> 32) + x_3 * x_1;
  579. zz_5 += (zz_4 >> 32) + x_3 * x_2;
  580. zz_4 &= M;
  581. zz_6 += zz_5 >> 32;
  582. zz_5 &= M;
  583. }
  584. ulong x_4 = x[4];
  585. ulong zz_7 = zz[7] + (zz_6 >> 32); zz_6 &= M;
  586. ulong zz_8 = zz[8] + (zz_7 >> 32); zz_7 &= M;
  587. {
  588. zz_4 += x_4 * x_0;
  589. w = (uint)zz_4;
  590. zz[4] = (w << 1) | c;
  591. c = w >> 31;
  592. zz_5 += (zz_4 >> 32) + x_4 * x_1;
  593. zz_6 += (zz_5 >> 32) + x_4 * x_2;
  594. zz_7 += (zz_6 >> 32) + x_4 * x_3;
  595. zz_8 += zz_7 >> 32;
  596. }
  597. w = (uint)zz_5;
  598. zz[5] = (w << 1) | c;
  599. c = w >> 31;
  600. w = (uint)zz_6;
  601. zz[6] = (w << 1) | c;
  602. c = w >> 31;
  603. w = (uint)zz_7;
  604. zz[7] = (w << 1) | c;
  605. c = w >> 31;
  606. w = (uint)zz_8;
  607. zz[8] = (w << 1) | c;
  608. c = w >> 31;
  609. w = zz[9] + (uint)(zz_8 >> 32);
  610. zz[9] = (w << 1) | c;
  611. }
  612. public static void Square(uint[] x, int xOff, uint[] zz, int zzOff)
  613. {
  614. ulong x_0 = x[xOff + 0];
  615. ulong zz_1;
  616. uint c = 0, w;
  617. {
  618. int i = 4, j = 10;
  619. do
  620. {
  621. ulong xVal = x[xOff + i--];
  622. ulong p = xVal * xVal;
  623. zz[zzOff + --j] = (c << 31) | (uint)(p >> 33);
  624. zz[zzOff + --j] = (uint)(p >> 1);
  625. c = (uint)p;
  626. }
  627. while (i > 0);
  628. {
  629. ulong p = x_0 * x_0;
  630. zz_1 = (ulong)(c << 31) | (p >> 33);
  631. zz[zzOff + 0] = (uint)p;
  632. c = (uint)(p >> 32) & 1;
  633. }
  634. }
  635. ulong x_1 = x[xOff + 1];
  636. ulong zz_2 = zz[zzOff + 2];
  637. {
  638. zz_1 += x_1 * x_0;
  639. w = (uint)zz_1;
  640. zz[zzOff + 1] = (w << 1) | c;
  641. c = w >> 31;
  642. zz_2 += zz_1 >> 32;
  643. }
  644. ulong x_2 = x[xOff + 2];
  645. ulong zz_3 = zz[zzOff + 3];
  646. ulong zz_4 = zz[zzOff + 4];
  647. {
  648. zz_2 += x_2 * x_0;
  649. w = (uint)zz_2;
  650. zz[zzOff + 2] = (w << 1) | c;
  651. c = w >> 31;
  652. zz_3 += (zz_2 >> 32) + x_2 * x_1;
  653. zz_4 += zz_3 >> 32;
  654. zz_3 &= M;
  655. }
  656. ulong x_3 = x[xOff + 3];
  657. ulong zz_5 = zz[zzOff + 5] + (zz_4 >> 32); zz_4 &= M;
  658. ulong zz_6 = zz[zzOff + 6] + (zz_5 >> 32); zz_5 &= M;
  659. {
  660. zz_3 += x_3 * x_0;
  661. w = (uint)zz_3;
  662. zz[zzOff + 3] = (w << 1) | c;
  663. c = w >> 31;
  664. zz_4 += (zz_3 >> 32) + x_3 * x_1;
  665. zz_5 += (zz_4 >> 32) + x_3 * x_2;
  666. zz_4 &= M;
  667. zz_6 += zz_5 >> 32;
  668. zz_5 &= M;
  669. }
  670. ulong x_4 = x[xOff + 4];
  671. ulong zz_7 = zz[zzOff + 7] + (zz_6 >> 32); zz_6 &= M;
  672. ulong zz_8 = zz[zzOff + 8] + (zz_7 >> 32); zz_7 &= M;
  673. {
  674. zz_4 += x_4 * x_0;
  675. w = (uint)zz_4;
  676. zz[zzOff + 4] = (w << 1) | c;
  677. c = w >> 31;
  678. zz_5 += (zz_4 >> 32) + x_4 * x_1;
  679. zz_6 += (zz_5 >> 32) + x_4 * x_2;
  680. zz_7 += (zz_6 >> 32) + x_4 * x_3;
  681. zz_8 += zz_7 >> 32;
  682. }
  683. w = (uint)zz_5;
  684. zz[zzOff + 5] = (w << 1) | c;
  685. c = w >> 31;
  686. w = (uint)zz_6;
  687. zz[zzOff + 6] = (w << 1) | c;
  688. c = w >> 31;
  689. w = (uint)zz_7;
  690. zz[zzOff + 7] = (w << 1) | c;
  691. c = w >> 31;
  692. w = (uint)zz_8;
  693. zz[zzOff + 8] = (w << 1) | c;
  694. c = w >> 31;
  695. w = zz[zzOff + 9] + (uint)(zz_8 >> 32);
  696. zz[zzOff + 9] = (w << 1) | c;
  697. }
  698. public static int Sub(uint[] x, uint[] y, uint[] z)
  699. {
  700. long c = 0;
  701. c += (long)x[0] - y[0];
  702. z[0] = (uint)c;
  703. c >>= 32;
  704. c += (long)x[1] - y[1];
  705. z[1] = (uint)c;
  706. c >>= 32;
  707. c += (long)x[2] - y[2];
  708. z[2] = (uint)c;
  709. c >>= 32;
  710. c += (long)x[3] - y[3];
  711. z[3] = (uint)c;
  712. c >>= 32;
  713. c += (long)x[4] - y[4];
  714. z[4] = (uint)c;
  715. c >>= 32;
  716. return (int)c;
  717. }
  718. public static int Sub(uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff)
  719. {
  720. long c = 0;
  721. c += (long)x[xOff + 0] - y[yOff + 0];
  722. z[zOff + 0] = (uint)c;
  723. c >>= 32;
  724. c += (long)x[xOff + 1] - y[yOff + 1];
  725. z[zOff + 1] = (uint)c;
  726. c >>= 32;
  727. c += (long)x[xOff + 2] - y[yOff + 2];
  728. z[zOff + 2] = (uint)c;
  729. c >>= 32;
  730. c += (long)x[xOff + 3] - y[yOff + 3];
  731. z[zOff + 3] = (uint)c;
  732. c >>= 32;
  733. c += (long)x[xOff + 4] - y[yOff + 4];
  734. z[zOff + 4] = (uint)c;
  735. c >>= 32;
  736. return (int)c;
  737. }
  738. public static int SubBothFrom(uint[] x, uint[] y, uint[] z)
  739. {
  740. long c = 0;
  741. c += (long)z[0] - x[0] - y[0];
  742. z[0] = (uint)c;
  743. c >>= 32;
  744. c += (long)z[1] - x[1] - y[1];
  745. z[1] = (uint)c;
  746. c >>= 32;
  747. c += (long)z[2] - x[2] - y[2];
  748. z[2] = (uint)c;
  749. c >>= 32;
  750. c += (long)z[3] - x[3] - y[3];
  751. z[3] = (uint)c;
  752. c >>= 32;
  753. c += (long)z[4] - x[4] - y[4];
  754. z[4] = (uint)c;
  755. c >>= 32;
  756. return (int)c;
  757. }
  758. public static int SubFrom(uint[] x, uint[] z)
  759. {
  760. long c = 0;
  761. c += (long)z[0] - x[0];
  762. z[0] = (uint)c;
  763. c >>= 32;
  764. c += (long)z[1] - x[1];
  765. z[1] = (uint)c;
  766. c >>= 32;
  767. c += (long)z[2] - x[2];
  768. z[2] = (uint)c;
  769. c >>= 32;
  770. c += (long)z[3] - x[3];
  771. z[3] = (uint)c;
  772. c >>= 32;
  773. c += (long)z[4] - x[4];
  774. z[4] = (uint)c;
  775. c >>= 32;
  776. return (int)c;
  777. }
  778. public static int SubFrom(uint[] x, int xOff, uint[] z, int zOff)
  779. {
  780. long c = 0;
  781. c += (long)z[zOff + 0] - x[xOff + 0];
  782. z[zOff + 0] = (uint)c;
  783. c >>= 32;
  784. c += (long)z[zOff + 1] - x[xOff + 1];
  785. z[zOff + 1] = (uint)c;
  786. c >>= 32;
  787. c += (long)z[zOff + 2] - x[xOff + 2];
  788. z[zOff + 2] = (uint)c;
  789. c >>= 32;
  790. c += (long)z[zOff + 3] - x[xOff + 3];
  791. z[zOff + 3] = (uint)c;
  792. c >>= 32;
  793. c += (long)z[zOff + 4] - x[xOff + 4];
  794. z[zOff + 4] = (uint)c;
  795. c >>= 32;
  796. return (int)c;
  797. }
  798. public static BigInteger ToBigInteger(uint[] x)
  799. {
  800. byte[] bs = new byte[20];
  801. for (int i = 0; i < 5; ++i)
  802. {
  803. uint x_i = x[i];
  804. if (x_i != 0)
  805. {
  806. Pack.UInt32_To_BE(x_i, bs, (4 - i) << 2);
  807. }
  808. }
  809. return new BigInteger(1, bs);
  810. }
  811. public static void Zero(uint[] z)
  812. {
  813. z[0] = 0;
  814. z[1] = 0;
  815. z[2] = 0;
  816. z[3] = 0;
  817. z[4] = 0;
  818. }
  819. }
  820. }
  821. #pragma warning restore
  822. #endif