Nat160.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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 GetBit(uint[] x, int bit)
  164. {
  165. if (bit == 0)
  166. {
  167. return x[0] & 1;
  168. }
  169. int w = bit >> 5;
  170. if (w < 0 || w >= 5)
  171. {
  172. return 0;
  173. }
  174. int b = bit & 31;
  175. return (x[w] >> b) & 1;
  176. }
  177. public static bool Gte(uint[] x, uint[] y)
  178. {
  179. for (int i = 4; i >= 0; --i)
  180. {
  181. uint x_i = x[i], y_i = y[i];
  182. if (x_i < y_i)
  183. return false;
  184. if (x_i > y_i)
  185. return true;
  186. }
  187. return true;
  188. }
  189. public static bool Gte(uint[] x, int xOff, uint[] y, int yOff)
  190. {
  191. for (int i = 4; i >= 0; --i)
  192. {
  193. uint x_i = x[xOff + i], y_i = y[yOff + i];
  194. if (x_i < y_i)
  195. return false;
  196. if (x_i > y_i)
  197. return true;
  198. }
  199. return true;
  200. }
  201. public static bool IsOne(uint[] x)
  202. {
  203. if (x[0] != 1)
  204. {
  205. return false;
  206. }
  207. for (int i = 1; i < 5; ++i)
  208. {
  209. if (x[i] != 0)
  210. {
  211. return false;
  212. }
  213. }
  214. return true;
  215. }
  216. public static bool IsZero(uint[] x)
  217. {
  218. for (int i = 0; i < 5; ++i)
  219. {
  220. if (x[i] != 0)
  221. {
  222. return false;
  223. }
  224. }
  225. return true;
  226. }
  227. public static void Mul(uint[] x, uint[] y, uint[] zz)
  228. {
  229. ulong y_0 = y[0];
  230. ulong y_1 = y[1];
  231. ulong y_2 = y[2];
  232. ulong y_3 = y[3];
  233. ulong y_4 = y[4];
  234. {
  235. ulong c = 0, x_0 = x[0];
  236. c += x_0 * y_0;
  237. zz[0] = (uint)c;
  238. c >>= 32;
  239. c += x_0 * y_1;
  240. zz[1] = (uint)c;
  241. c >>= 32;
  242. c += x_0 * y_2;
  243. zz[2] = (uint)c;
  244. c >>= 32;
  245. c += x_0 * y_3;
  246. zz[3] = (uint)c;
  247. c >>= 32;
  248. c += x_0 * y_4;
  249. zz[4] = (uint)c;
  250. c >>= 32;
  251. zz[5] = (uint)c;
  252. }
  253. for (int i = 1; i < 5; ++i)
  254. {
  255. ulong c = 0, x_i = x[i];
  256. c += x_i * y_0 + zz[i + 0];
  257. zz[i + 0] = (uint)c;
  258. c >>= 32;
  259. c += x_i * y_1 + zz[i + 1];
  260. zz[i + 1] = (uint)c;
  261. c >>= 32;
  262. c += x_i * y_2 + zz[i + 2];
  263. zz[i + 2] = (uint)c;
  264. c >>= 32;
  265. c += x_i * y_3 + zz[i + 3];
  266. zz[i + 3] = (uint)c;
  267. c >>= 32;
  268. c += x_i * y_4 + zz[i + 4];
  269. zz[i + 4] = (uint)c;
  270. c >>= 32;
  271. zz[i + 5] = (uint)c;
  272. }
  273. }
  274. public static void Mul(uint[] x, int xOff, uint[] y, int yOff, uint[] zz, int zzOff)
  275. {
  276. ulong y_0 = y[yOff + 0];
  277. ulong y_1 = y[yOff + 1];
  278. ulong y_2 = y[yOff + 2];
  279. ulong y_3 = y[yOff + 3];
  280. ulong y_4 = y[yOff + 4];
  281. {
  282. ulong c = 0, x_0 = x[xOff + 0];
  283. c += x_0 * y_0;
  284. zz[zzOff + 0] = (uint)c;
  285. c >>= 32;
  286. c += x_0 * y_1;
  287. zz[zzOff + 1] = (uint)c;
  288. c >>= 32;
  289. c += x_0 * y_2;
  290. zz[zzOff + 2] = (uint)c;
  291. c >>= 32;
  292. c += x_0 * y_3;
  293. zz[zzOff + 3] = (uint)c;
  294. c >>= 32;
  295. c += x_0 * y_4;
  296. zz[zzOff + 4] = (uint)c;
  297. c >>= 32;
  298. zz[zzOff + 5] = (uint)c;
  299. }
  300. for (int i = 1; i < 5; ++i)
  301. {
  302. ++zzOff;
  303. ulong c = 0, x_i = x[xOff + i];
  304. c += x_i * y_0 + zz[zzOff + 0];
  305. zz[zzOff + 0] = (uint)c;
  306. c >>= 32;
  307. c += x_i * y_1 + zz[zzOff + 1];
  308. zz[zzOff + 1] = (uint)c;
  309. c >>= 32;
  310. c += x_i * y_2 + zz[zzOff + 2];
  311. zz[zzOff + 2] = (uint)c;
  312. c >>= 32;
  313. c += x_i * y_3 + zz[zzOff + 3];
  314. zz[zzOff + 3] = (uint)c;
  315. c >>= 32;
  316. c += x_i * y_4 + zz[zzOff + 4];
  317. zz[zzOff + 4] = (uint)c;
  318. c >>= 32;
  319. zz[zzOff + 5] = (uint)c;
  320. }
  321. }
  322. public static uint MulAddTo(uint[] x, uint[] y, uint[] zz)
  323. {
  324. ulong y_0 = y[0];
  325. ulong y_1 = y[1];
  326. ulong y_2 = y[2];
  327. ulong y_3 = y[3];
  328. ulong y_4 = y[4];
  329. ulong zc = 0;
  330. for (int i = 0; i < 5; ++i)
  331. {
  332. ulong c = 0, x_i = x[i];
  333. c += x_i * y_0 + zz[i + 0];
  334. zz[i + 0] = (uint)c;
  335. c >>= 32;
  336. c += x_i * y_1 + zz[i + 1];
  337. zz[i + 1] = (uint)c;
  338. c >>= 32;
  339. c += x_i * y_2 + zz[i + 2];
  340. zz[i + 2] = (uint)c;
  341. c >>= 32;
  342. c += x_i * y_3 + zz[i + 3];
  343. zz[i + 3] = (uint)c;
  344. c >>= 32;
  345. c += x_i * y_4 + zz[i + 4];
  346. zz[i + 4] = (uint)c;
  347. c >>= 32;
  348. zc += c + zz[i + 5];
  349. zz[i + 5] = (uint)zc;
  350. zc >>= 32;
  351. }
  352. return (uint)zc;
  353. }
  354. public static uint MulAddTo(uint[] x, int xOff, uint[] y, int yOff, uint[] zz, int zzOff)
  355. {
  356. ulong y_0 = y[yOff + 0];
  357. ulong y_1 = y[yOff + 1];
  358. ulong y_2 = y[yOff + 2];
  359. ulong y_3 = y[yOff + 3];
  360. ulong y_4 = y[yOff + 4];
  361. ulong zc = 0;
  362. for (int i = 0; i < 5; ++i)
  363. {
  364. ulong c = 0, x_i = x[xOff + i];
  365. c += x_i * y_0 + zz[zzOff + 0];
  366. zz[zzOff + 0] = (uint)c;
  367. c >>= 32;
  368. c += x_i * y_1 + zz[zzOff + 1];
  369. zz[zzOff + 1] = (uint)c;
  370. c >>= 32;
  371. c += x_i * y_2 + zz[zzOff + 2];
  372. zz[zzOff + 2] = (uint)c;
  373. c >>= 32;
  374. c += x_i * y_3 + zz[zzOff + 3];
  375. zz[zzOff + 3] = (uint)c;
  376. c >>= 32;
  377. c += x_i * y_4 + zz[zzOff + 4];
  378. zz[zzOff + 4] = (uint)c;
  379. c >>= 32;
  380. zc += c + zz[zzOff + 5];
  381. zz[zzOff + 5] = (uint)zc;
  382. zc >>= 32;
  383. ++zzOff;
  384. }
  385. return (uint)zc;
  386. }
  387. public static ulong Mul33Add(uint w, uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff)
  388. {
  389. Debug.Assert(w >> 31 == 0);
  390. ulong c = 0, wVal = w;
  391. ulong x0 = x[xOff + 0];
  392. c += wVal * x0 + y[yOff + 0];
  393. z[zOff + 0] = (uint)c;
  394. c >>= 32;
  395. ulong x1 = x[xOff + 1];
  396. c += wVal * x1 + x0 + y[yOff + 1];
  397. z[zOff + 1] = (uint)c;
  398. c >>= 32;
  399. ulong x2 = x[xOff + 2];
  400. c += wVal * x2 + x1 + y[yOff + 2];
  401. z[zOff + 2] = (uint)c;
  402. c >>= 32;
  403. ulong x3 = x[xOff + 3];
  404. c += wVal * x3 + x2 + y[yOff + 3];
  405. z[zOff + 3] = (uint)c;
  406. c >>= 32;
  407. ulong x4 = x[xOff + 4];
  408. c += wVal * x4 + x3 + y[yOff + 4];
  409. z[zOff + 4] = (uint)c;
  410. c >>= 32;
  411. c += x4;
  412. return c;
  413. }
  414. public static uint MulWordAddExt(uint x, uint[] yy, int yyOff, uint[] zz, int zzOff)
  415. {
  416. Debug.Assert(yyOff <= 5);
  417. Debug.Assert(zzOff <= 5);
  418. ulong c = 0, xVal = x;
  419. c += xVal * yy[yyOff + 0] + zz[zzOff + 0];
  420. zz[zzOff + 0] = (uint)c;
  421. c >>= 32;
  422. c += xVal * yy[yyOff + 1] + zz[zzOff + 1];
  423. zz[zzOff + 1] = (uint)c;
  424. c >>= 32;
  425. c += xVal * yy[yyOff + 2] + zz[zzOff + 2];
  426. zz[zzOff + 2] = (uint)c;
  427. c >>= 32;
  428. c += xVal * yy[yyOff + 3] + zz[zzOff + 3];
  429. zz[zzOff + 3] = (uint)c;
  430. c >>= 32;
  431. c += xVal * yy[yyOff + 4] + zz[zzOff + 4];
  432. zz[zzOff + 4] = (uint)c;
  433. c >>= 32;
  434. return (uint)c;
  435. }
  436. public static uint Mul33DWordAdd(uint x, ulong y, uint[] z, int zOff)
  437. {
  438. Debug.Assert(x >> 31 == 0);
  439. Debug.Assert(zOff <= 1);
  440. ulong c = 0, xVal = x;
  441. ulong y00 = y & M;
  442. c += xVal * y00 + z[zOff + 0];
  443. z[zOff + 0] = (uint)c;
  444. c >>= 32;
  445. ulong y01 = y >> 32;
  446. c += xVal * y01 + y00 + z[zOff + 1];
  447. z[zOff + 1] = (uint)c;
  448. c >>= 32;
  449. c += y01 + z[zOff + 2];
  450. z[zOff + 2] = (uint)c;
  451. c >>= 32;
  452. c += z[zOff + 3];
  453. z[zOff + 3] = (uint)c;
  454. c >>= 32;
  455. return c == 0 ? 0 : Nat.IncAt(5, z, zOff, 4);
  456. }
  457. public static uint Mul33WordAdd(uint x, uint y, uint[] z, int zOff)
  458. {
  459. Debug.Assert(x >> 31 == 0);
  460. Debug.Assert(zOff <= 2);
  461. ulong c = 0, yVal = y;
  462. c += yVal * x + z[zOff + 0];
  463. z[zOff + 0] = (uint)c;
  464. c >>= 32;
  465. c += yVal + z[zOff + 1];
  466. z[zOff + 1] = (uint)c;
  467. c >>= 32;
  468. c += z[zOff + 2];
  469. z[zOff + 2] = (uint)c;
  470. c >>= 32;
  471. return c == 0 ? 0 : Nat.IncAt(5, z, zOff, 3);
  472. }
  473. public static uint MulWordDwordAdd(uint x, ulong y, uint[] z, int zOff)
  474. {
  475. Debug.Assert(zOff <= 2);
  476. ulong c = 0, xVal = x;
  477. c += xVal * y + z[zOff + 0];
  478. z[zOff + 0] = (uint)c;
  479. c >>= 32;
  480. c += xVal * (y >> 32) + z[zOff + 1];
  481. z[zOff + 1] = (uint)c;
  482. c >>= 32;
  483. c += z[zOff + 2];
  484. z[zOff + 2] = (uint)c;
  485. c >>= 32;
  486. return c == 0 ? 0 : Nat.IncAt(5, z, zOff, 3);
  487. }
  488. public static uint MulWordsAdd(uint x, uint y, uint[] z, int zOff)
  489. {
  490. Debug.Assert(zOff <= 3);
  491. ulong c = 0, xVal = x, yVal = y;
  492. c += yVal * xVal + z[zOff + 0];
  493. z[zOff + 0] = (uint)c;
  494. c >>= 32;
  495. c += z[zOff + 1];
  496. z[zOff + 1] = (uint)c;
  497. c >>= 32;
  498. return c == 0 ? 0 : Nat.IncAt(5, z, zOff, 2);
  499. }
  500. public static uint MulWord(uint x, uint[] y, uint[] z, int zOff)
  501. {
  502. ulong c = 0, xVal = x;
  503. int i = 0;
  504. do
  505. {
  506. c += xVal * y[i];
  507. z[zOff + i] = (uint)c;
  508. c >>= 32;
  509. }
  510. while (++i < 5);
  511. return (uint)c;
  512. }
  513. public static void Square(uint[] x, uint[] zz)
  514. {
  515. ulong x_0 = x[0];
  516. ulong zz_1;
  517. uint c = 0, w;
  518. {
  519. int i = 4, j = 10;
  520. do
  521. {
  522. ulong xVal = x[i--];
  523. ulong p = xVal * xVal;
  524. zz[--j] = (c << 31) | (uint)(p >> 33);
  525. zz[--j] = (uint)(p >> 1);
  526. c = (uint)p;
  527. }
  528. while (i > 0);
  529. {
  530. ulong p = x_0 * x_0;
  531. zz_1 = (ulong)(c << 31) | (p >> 33);
  532. zz[0] = (uint)p;
  533. c = (uint)(p >> 32) & 1;
  534. }
  535. }
  536. ulong x_1 = x[1];
  537. ulong zz_2 = zz[2];
  538. {
  539. zz_1 += x_1 * x_0;
  540. w = (uint)zz_1;
  541. zz[1] = (w << 1) | c;
  542. c = w >> 31;
  543. zz_2 += zz_1 >> 32;
  544. }
  545. ulong x_2 = x[2];
  546. ulong zz_3 = zz[3];
  547. ulong zz_4 = zz[4];
  548. {
  549. zz_2 += x_2 * x_0;
  550. w = (uint)zz_2;
  551. zz[2] = (w << 1) | c;
  552. c = w >> 31;
  553. zz_3 += (zz_2 >> 32) + x_2 * x_1;
  554. zz_4 += zz_3 >> 32;
  555. zz_3 &= M;
  556. }
  557. ulong x_3 = x[3];
  558. ulong zz_5 = zz[5] + (zz_4 >> 32); zz_4 &= M;
  559. ulong zz_6 = zz[6] + (zz_5 >> 32); zz_5 &= M;
  560. {
  561. zz_3 += x_3 * x_0;
  562. w = (uint)zz_3;
  563. zz[3] = (w << 1) | c;
  564. c = w >> 31;
  565. zz_4 += (zz_3 >> 32) + x_3 * x_1;
  566. zz_5 += (zz_4 >> 32) + x_3 * x_2;
  567. zz_4 &= M;
  568. zz_6 += zz_5 >> 32;
  569. zz_5 &= M;
  570. }
  571. ulong x_4 = x[4];
  572. ulong zz_7 = zz[7] + (zz_6 >> 32); zz_6 &= M;
  573. ulong zz_8 = zz[8] + (zz_7 >> 32); zz_7 &= M;
  574. {
  575. zz_4 += x_4 * x_0;
  576. w = (uint)zz_4;
  577. zz[4] = (w << 1) | c;
  578. c = w >> 31;
  579. zz_5 += (zz_4 >> 32) + x_4 * x_1;
  580. zz_6 += (zz_5 >> 32) + x_4 * x_2;
  581. zz_7 += (zz_6 >> 32) + x_4 * x_3;
  582. zz_8 += zz_7 >> 32;
  583. }
  584. w = (uint)zz_5;
  585. zz[5] = (w << 1) | c;
  586. c = w >> 31;
  587. w = (uint)zz_6;
  588. zz[6] = (w << 1) | c;
  589. c = w >> 31;
  590. w = (uint)zz_7;
  591. zz[7] = (w << 1) | c;
  592. c = w >> 31;
  593. w = (uint)zz_8;
  594. zz[8] = (w << 1) | c;
  595. c = w >> 31;
  596. w = zz[9] + (uint)(zz_8 >> 32);
  597. zz[9] = (w << 1) | c;
  598. }
  599. public static void Square(uint[] x, int xOff, uint[] zz, int zzOff)
  600. {
  601. ulong x_0 = x[xOff + 0];
  602. ulong zz_1;
  603. uint c = 0, w;
  604. {
  605. int i = 4, j = 10;
  606. do
  607. {
  608. ulong xVal = x[xOff + i--];
  609. ulong p = xVal * xVal;
  610. zz[zzOff + --j] = (c << 31) | (uint)(p >> 33);
  611. zz[zzOff + --j] = (uint)(p >> 1);
  612. c = (uint)p;
  613. }
  614. while (i > 0);
  615. {
  616. ulong p = x_0 * x_0;
  617. zz_1 = (ulong)(c << 31) | (p >> 33);
  618. zz[zzOff + 0] = (uint)p;
  619. c = (uint)(p >> 32) & 1;
  620. }
  621. }
  622. ulong x_1 = x[xOff + 1];
  623. ulong zz_2 = zz[zzOff + 2];
  624. {
  625. zz_1 += x_1 * x_0;
  626. w = (uint)zz_1;
  627. zz[zzOff + 1] = (w << 1) | c;
  628. c = w >> 31;
  629. zz_2 += zz_1 >> 32;
  630. }
  631. ulong x_2 = x[xOff + 2];
  632. ulong zz_3 = zz[zzOff + 3];
  633. ulong zz_4 = zz[zzOff + 4];
  634. {
  635. zz_2 += x_2 * x_0;
  636. w = (uint)zz_2;
  637. zz[zzOff + 2] = (w << 1) | c;
  638. c = w >> 31;
  639. zz_3 += (zz_2 >> 32) + x_2 * x_1;
  640. zz_4 += zz_3 >> 32;
  641. zz_3 &= M;
  642. }
  643. ulong x_3 = x[xOff + 3];
  644. ulong zz_5 = zz[zzOff + 5] + (zz_4 >> 32); zz_4 &= M;
  645. ulong zz_6 = zz[zzOff + 6] + (zz_5 >> 32); zz_5 &= M;
  646. {
  647. zz_3 += x_3 * x_0;
  648. w = (uint)zz_3;
  649. zz[zzOff + 3] = (w << 1) | c;
  650. c = w >> 31;
  651. zz_4 += (zz_3 >> 32) + x_3 * x_1;
  652. zz_5 += (zz_4 >> 32) + x_3 * x_2;
  653. zz_4 &= M;
  654. zz_6 += zz_5 >> 32;
  655. zz_5 &= M;
  656. }
  657. ulong x_4 = x[xOff + 4];
  658. ulong zz_7 = zz[zzOff + 7] + (zz_6 >> 32); zz_6 &= M;
  659. ulong zz_8 = zz[zzOff + 8] + (zz_7 >> 32); zz_7 &= M;
  660. {
  661. zz_4 += x_4 * x_0;
  662. w = (uint)zz_4;
  663. zz[zzOff + 4] = (w << 1) | c;
  664. c = w >> 31;
  665. zz_5 += (zz_4 >> 32) + x_4 * x_1;
  666. zz_6 += (zz_5 >> 32) + x_4 * x_2;
  667. zz_7 += (zz_6 >> 32) + x_4 * x_3;
  668. zz_8 += zz_7 >> 32;
  669. }
  670. w = (uint)zz_5;
  671. zz[zzOff + 5] = (w << 1) | c;
  672. c = w >> 31;
  673. w = (uint)zz_6;
  674. zz[zzOff + 6] = (w << 1) | c;
  675. c = w >> 31;
  676. w = (uint)zz_7;
  677. zz[zzOff + 7] = (w << 1) | c;
  678. c = w >> 31;
  679. w = (uint)zz_8;
  680. zz[zzOff + 8] = (w << 1) | c;
  681. c = w >> 31;
  682. w = zz[zzOff + 9] + (uint)(zz_8 >> 32);
  683. zz[zzOff + 9] = (w << 1) | c;
  684. }
  685. public static int Sub(uint[] x, uint[] y, uint[] z)
  686. {
  687. long c = 0;
  688. c += (long)x[0] - y[0];
  689. z[0] = (uint)c;
  690. c >>= 32;
  691. c += (long)x[1] - y[1];
  692. z[1] = (uint)c;
  693. c >>= 32;
  694. c += (long)x[2] - y[2];
  695. z[2] = (uint)c;
  696. c >>= 32;
  697. c += (long)x[3] - y[3];
  698. z[3] = (uint)c;
  699. c >>= 32;
  700. c += (long)x[4] - y[4];
  701. z[4] = (uint)c;
  702. c >>= 32;
  703. return (int)c;
  704. }
  705. public static int Sub(uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff)
  706. {
  707. long c = 0;
  708. c += (long)x[xOff + 0] - y[yOff + 0];
  709. z[zOff + 0] = (uint)c;
  710. c >>= 32;
  711. c += (long)x[xOff + 1] - y[yOff + 1];
  712. z[zOff + 1] = (uint)c;
  713. c >>= 32;
  714. c += (long)x[xOff + 2] - y[yOff + 2];
  715. z[zOff + 2] = (uint)c;
  716. c >>= 32;
  717. c += (long)x[xOff + 3] - y[yOff + 3];
  718. z[zOff + 3] = (uint)c;
  719. c >>= 32;
  720. c += (long)x[xOff + 4] - y[yOff + 4];
  721. z[zOff + 4] = (uint)c;
  722. c >>= 32;
  723. return (int)c;
  724. }
  725. public static int SubBothFrom(uint[] x, uint[] y, uint[] z)
  726. {
  727. long c = 0;
  728. c += (long)z[0] - x[0] - y[0];
  729. z[0] = (uint)c;
  730. c >>= 32;
  731. c += (long)z[1] - x[1] - y[1];
  732. z[1] = (uint)c;
  733. c >>= 32;
  734. c += (long)z[2] - x[2] - y[2];
  735. z[2] = (uint)c;
  736. c >>= 32;
  737. c += (long)z[3] - x[3] - y[3];
  738. z[3] = (uint)c;
  739. c >>= 32;
  740. c += (long)z[4] - x[4] - y[4];
  741. z[4] = (uint)c;
  742. c >>= 32;
  743. return (int)c;
  744. }
  745. public static int SubFrom(uint[] x, uint[] z)
  746. {
  747. long c = 0;
  748. c += (long)z[0] - x[0];
  749. z[0] = (uint)c;
  750. c >>= 32;
  751. c += (long)z[1] - x[1];
  752. z[1] = (uint)c;
  753. c >>= 32;
  754. c += (long)z[2] - x[2];
  755. z[2] = (uint)c;
  756. c >>= 32;
  757. c += (long)z[3] - x[3];
  758. z[3] = (uint)c;
  759. c >>= 32;
  760. c += (long)z[4] - x[4];
  761. z[4] = (uint)c;
  762. c >>= 32;
  763. return (int)c;
  764. }
  765. public static int SubFrom(uint[] x, int xOff, uint[] z, int zOff)
  766. {
  767. long c = 0;
  768. c += (long)z[zOff + 0] - x[xOff + 0];
  769. z[zOff + 0] = (uint)c;
  770. c >>= 32;
  771. c += (long)z[zOff + 1] - x[xOff + 1];
  772. z[zOff + 1] = (uint)c;
  773. c >>= 32;
  774. c += (long)z[zOff + 2] - x[xOff + 2];
  775. z[zOff + 2] = (uint)c;
  776. c >>= 32;
  777. c += (long)z[zOff + 3] - x[xOff + 3];
  778. z[zOff + 3] = (uint)c;
  779. c >>= 32;
  780. c += (long)z[zOff + 4] - x[xOff + 4];
  781. z[zOff + 4] = (uint)c;
  782. c >>= 32;
  783. return (int)c;
  784. }
  785. public static BigInteger ToBigInteger(uint[] x)
  786. {
  787. byte[] bs = new byte[20];
  788. for (int i = 0; i < 5; ++i)
  789. {
  790. uint x_i = x[i];
  791. if (x_i != 0)
  792. {
  793. Pack.UInt32_To_BE(x_i, bs, (4 - i) << 2);
  794. }
  795. }
  796. return new BigInteger(1, bs);
  797. }
  798. public static void Zero(uint[] z)
  799. {
  800. z[0] = 0;
  801. z[1] = 0;
  802. z[2] = 0;
  803. z[3] = 0;
  804. z[4] = 0;
  805. }
  806. }
  807. }
  808. #pragma warning restore
  809. #endif