Nat224.cs 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Diagnostics;
  4. using Org.BouncyCastle.Crypto.Utilities;
  5. namespace Org.BouncyCastle.Math.Raw
  6. {
  7. internal abstract class Nat224
  8. {
  9. private const ulong M = 0xFFFFFFFFUL;
  10. public static uint Add(uint[] x, uint[] y, uint[] z)
  11. {
  12. ulong c = 0;
  13. c += (ulong)x[0] + y[0];
  14. z[0] = (uint)c;
  15. c >>= 32;
  16. c += (ulong)x[1] + y[1];
  17. z[1] = (uint)c;
  18. c >>= 32;
  19. c += (ulong)x[2] + y[2];
  20. z[2] = (uint)c;
  21. c >>= 32;
  22. c += (ulong)x[3] + y[3];
  23. z[3] = (uint)c;
  24. c >>= 32;
  25. c += (ulong)x[4] + y[4];
  26. z[4] = (uint)c;
  27. c >>= 32;
  28. c += (ulong)x[5] + y[5];
  29. z[5] = (uint)c;
  30. c >>= 32;
  31. c += (ulong)x[6] + y[6];
  32. z[6] = (uint)c;
  33. c >>= 32;
  34. return (uint)c;
  35. }
  36. public static uint Add(uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff)
  37. {
  38. ulong c = 0;
  39. c += (ulong)x[xOff + 0] + y[yOff + 0];
  40. z[zOff + 0] = (uint)c;
  41. c >>= 32;
  42. c += (ulong)x[xOff + 1] + y[yOff + 1];
  43. z[zOff + 1] = (uint)c;
  44. c >>= 32;
  45. c += (ulong)x[xOff + 2] + y[yOff + 2];
  46. z[zOff + 2] = (uint)c;
  47. c >>= 32;
  48. c += (ulong)x[xOff + 3] + y[yOff + 3];
  49. z[zOff + 3] = (uint)c;
  50. c >>= 32;
  51. c += (ulong)x[xOff + 4] + y[yOff + 4];
  52. z[zOff + 4] = (uint)c;
  53. c >>= 32;
  54. c += (ulong)x[xOff + 5] + y[yOff + 5];
  55. z[zOff + 5] = (uint)c;
  56. c >>= 32;
  57. c += (ulong)x[xOff + 6] + y[yOff + 6];
  58. z[zOff + 6] = (uint)c;
  59. c >>= 32;
  60. return (uint)c;
  61. }
  62. public static uint AddBothTo(uint[] x, uint[] y, uint[] z)
  63. {
  64. ulong c = 0;
  65. c += (ulong)x[0] + y[0] + z[0];
  66. z[0] = (uint)c;
  67. c >>= 32;
  68. c += (ulong)x[1] + y[1] + z[1];
  69. z[1] = (uint)c;
  70. c >>= 32;
  71. c += (ulong)x[2] + y[2] + z[2];
  72. z[2] = (uint)c;
  73. c >>= 32;
  74. c += (ulong)x[3] + y[3] + z[3];
  75. z[3] = (uint)c;
  76. c >>= 32;
  77. c += (ulong)x[4] + y[4] + z[4];
  78. z[4] = (uint)c;
  79. c >>= 32;
  80. c += (ulong)x[5] + y[5] + z[5];
  81. z[5] = (uint)c;
  82. c >>= 32;
  83. c += (ulong)x[6] + y[6] + z[6];
  84. z[6] = (uint)c;
  85. c >>= 32;
  86. return (uint)c;
  87. }
  88. public static uint AddBothTo(uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff)
  89. {
  90. ulong c = 0;
  91. c += (ulong)x[xOff + 0] + y[yOff + 0] + z[zOff + 0];
  92. z[zOff + 0] = (uint)c;
  93. c >>= 32;
  94. c += (ulong)x[xOff + 1] + y[yOff + 1] + z[zOff + 1];
  95. z[zOff + 1] = (uint)c;
  96. c >>= 32;
  97. c += (ulong)x[xOff + 2] + y[yOff + 2] + z[zOff + 2];
  98. z[zOff + 2] = (uint)c;
  99. c >>= 32;
  100. c += (ulong)x[xOff + 3] + y[yOff + 3] + z[zOff + 3];
  101. z[zOff + 3] = (uint)c;
  102. c >>= 32;
  103. c += (ulong)x[xOff + 4] + y[yOff + 4] + z[zOff + 4];
  104. z[zOff + 4] = (uint)c;
  105. c >>= 32;
  106. c += (ulong)x[xOff + 5] + y[yOff + 5] + z[zOff + 5];
  107. z[zOff + 5] = (uint)c;
  108. c >>= 32;
  109. c += (ulong)x[xOff + 6] + y[yOff + 6] + z[zOff + 6];
  110. z[zOff + 6] = (uint)c;
  111. c >>= 32;
  112. return (uint)c;
  113. }
  114. public static uint AddTo(uint[] x, uint[] z)
  115. {
  116. ulong c = 0;
  117. c += (ulong)x[0] + z[0];
  118. z[0] = (uint)c;
  119. c >>= 32;
  120. c += (ulong)x[1] + z[1];
  121. z[1] = (uint)c;
  122. c >>= 32;
  123. c += (ulong)x[2] + z[2];
  124. z[2] = (uint)c;
  125. c >>= 32;
  126. c += (ulong)x[3] + z[3];
  127. z[3] = (uint)c;
  128. c >>= 32;
  129. c += (ulong)x[4] + z[4];
  130. z[4] = (uint)c;
  131. c >>= 32;
  132. c += (ulong)x[5] + z[5];
  133. z[5] = (uint)c;
  134. c >>= 32;
  135. c += (ulong)x[6] + z[6];
  136. z[6] = (uint)c;
  137. c >>= 32;
  138. return (uint)c;
  139. }
  140. public static uint AddTo(uint[] x, int xOff, uint[] z, int zOff, uint cIn)
  141. {
  142. ulong c = cIn;
  143. c += (ulong)x[xOff + 0] + z[zOff + 0];
  144. z[zOff + 0] = (uint)c;
  145. c >>= 32;
  146. c += (ulong)x[xOff + 1] + z[zOff + 1];
  147. z[zOff + 1] = (uint)c;
  148. c >>= 32;
  149. c += (ulong)x[xOff + 2] + z[zOff + 2];
  150. z[zOff + 2] = (uint)c;
  151. c >>= 32;
  152. c += (ulong)x[xOff + 3] + z[zOff + 3];
  153. z[zOff + 3] = (uint)c;
  154. c >>= 32;
  155. c += (ulong)x[xOff + 4] + z[zOff + 4];
  156. z[zOff + 4] = (uint)c;
  157. c >>= 32;
  158. c += (ulong)x[xOff + 5] + z[zOff + 5];
  159. z[zOff + 5] = (uint)c;
  160. c >>= 32;
  161. c += (ulong)x[xOff + 6] + z[zOff + 6];
  162. z[zOff + 6] = (uint)c;
  163. c >>= 32;
  164. return (uint)c;
  165. }
  166. public static uint AddToEachOther(uint[] u, int uOff, uint[] v, int vOff)
  167. {
  168. ulong c = 0;
  169. c += (ulong)u[uOff + 0] + v[vOff + 0];
  170. u[uOff + 0] = (uint)c;
  171. v[vOff + 0] = (uint)c;
  172. c >>= 32;
  173. c += (ulong)u[uOff + 1] + v[vOff + 1];
  174. u[uOff + 1] = (uint)c;
  175. v[vOff + 1] = (uint)c;
  176. c >>= 32;
  177. c += (ulong)u[uOff + 2] + v[vOff + 2];
  178. u[uOff + 2] = (uint)c;
  179. v[vOff + 2] = (uint)c;
  180. c >>= 32;
  181. c += (ulong)u[uOff + 3] + v[vOff + 3];
  182. u[uOff + 3] = (uint)c;
  183. v[vOff + 3] = (uint)c;
  184. c >>= 32;
  185. c += (ulong)u[uOff + 4] + v[vOff + 4];
  186. u[uOff + 4] = (uint)c;
  187. v[vOff + 4] = (uint)c;
  188. c >>= 32;
  189. c += (ulong)u[uOff + 5] + v[vOff + 5];
  190. u[uOff + 5] = (uint)c;
  191. v[vOff + 5] = (uint)c;
  192. c >>= 32;
  193. c += (ulong)u[uOff + 6] + v[vOff + 6];
  194. u[uOff + 6] = (uint)c;
  195. v[vOff + 6] = (uint)c;
  196. c >>= 32;
  197. return (uint)c;
  198. }
  199. public static void Copy(uint[] x, uint[] z)
  200. {
  201. z[0] = x[0];
  202. z[1] = x[1];
  203. z[2] = x[2];
  204. z[3] = x[3];
  205. z[4] = x[4];
  206. z[5] = x[5];
  207. z[6] = x[6];
  208. }
  209. public static uint[] Create()
  210. {
  211. return new uint[7];
  212. }
  213. public static uint[] CreateExt()
  214. {
  215. return new uint[14];
  216. }
  217. public static bool Diff(uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff)
  218. {
  219. bool pos = Gte(x, xOff, y, yOff);
  220. if (pos)
  221. {
  222. Sub(x, xOff, y, yOff, z, zOff);
  223. }
  224. else
  225. {
  226. Sub(y, yOff, x, xOff, z, zOff);
  227. }
  228. return pos;
  229. }
  230. public static bool Eq(uint[] x, uint[] y)
  231. {
  232. for (int i = 6; i >= 0; --i)
  233. {
  234. if (x[i] != y[i])
  235. return false;
  236. }
  237. return true;
  238. }
  239. public static uint[] FromBigInteger(BigInteger x)
  240. {
  241. if (x.SignValue < 0 || x.BitLength > 224)
  242. throw new ArgumentException();
  243. uint[] z = Create();
  244. int i = 0;
  245. while (x.SignValue != 0)
  246. {
  247. z[i++] = (uint)x.IntValue;
  248. x = x.ShiftRight(32);
  249. }
  250. return z;
  251. }
  252. public static uint GetBit(uint[] x, int bit)
  253. {
  254. if (bit == 0)
  255. {
  256. return x[0] & 1;
  257. }
  258. int w = bit >> 5;
  259. if (w < 0 || w >= 7)
  260. {
  261. return 0;
  262. }
  263. int b = bit & 31;
  264. return (x[w] >> b) & 1;
  265. }
  266. public static bool Gte(uint[] x, uint[] y)
  267. {
  268. for (int i = 6; i >= 0; --i)
  269. {
  270. uint x_i = x[i], y_i = y[i];
  271. if (x_i < y_i)
  272. return false;
  273. if (x_i > y_i)
  274. return true;
  275. }
  276. return true;
  277. }
  278. public static bool Gte(uint[] x, int xOff, uint[] y, int yOff)
  279. {
  280. for (int i = 6; i >= 0; --i)
  281. {
  282. uint x_i = x[xOff + i], y_i = y[yOff + i];
  283. if (x_i < y_i)
  284. return false;
  285. if (x_i > y_i)
  286. return true;
  287. }
  288. return true;
  289. }
  290. public static bool IsOne(uint[] x)
  291. {
  292. if (x[0] != 1)
  293. {
  294. return false;
  295. }
  296. for (int i = 1; i < 7; ++i)
  297. {
  298. if (x[i] != 0)
  299. {
  300. return false;
  301. }
  302. }
  303. return true;
  304. }
  305. public static bool IsZero(uint[] x)
  306. {
  307. for (int i = 0; i < 7; ++i)
  308. {
  309. if (x[i] != 0)
  310. {
  311. return false;
  312. }
  313. }
  314. return true;
  315. }
  316. public static void Mul(uint[] x, uint[] y, uint[] zz)
  317. {
  318. ulong y_0 = y[0];
  319. ulong y_1 = y[1];
  320. ulong y_2 = y[2];
  321. ulong y_3 = y[3];
  322. ulong y_4 = y[4];
  323. ulong y_5 = y[5];
  324. ulong y_6 = y[6];
  325. {
  326. ulong c = 0, x_0 = x[0];
  327. c += x_0 * y_0;
  328. zz[0] = (uint)c;
  329. c >>= 32;
  330. c += x_0 * y_1;
  331. zz[1] = (uint)c;
  332. c >>= 32;
  333. c += x_0 * y_2;
  334. zz[2] = (uint)c;
  335. c >>= 32;
  336. c += x_0 * y_3;
  337. zz[3] = (uint)c;
  338. c >>= 32;
  339. c += x_0 * y_4;
  340. zz[4] = (uint)c;
  341. c >>= 32;
  342. c += x_0 * y_5;
  343. zz[5] = (uint)c;
  344. c >>= 32;
  345. c += x_0 * y_6;
  346. zz[6] = (uint)c;
  347. c >>= 32;
  348. zz[7] = (uint)c;
  349. }
  350. for (int i = 1; i < 7; ++i)
  351. {
  352. ulong c = 0, x_i = x[i];
  353. c += x_i * y_0 + zz[i + 0];
  354. zz[i + 0] = (uint)c;
  355. c >>= 32;
  356. c += x_i * y_1 + zz[i + 1];
  357. zz[i + 1] = (uint)c;
  358. c >>= 32;
  359. c += x_i * y_2 + zz[i + 2];
  360. zz[i + 2] = (uint)c;
  361. c >>= 32;
  362. c += x_i * y_3 + zz[i + 3];
  363. zz[i + 3] = (uint)c;
  364. c >>= 32;
  365. c += x_i * y_4 + zz[i + 4];
  366. zz[i + 4] = (uint)c;
  367. c >>= 32;
  368. c += x_i * y_5 + zz[i + 5];
  369. zz[i + 5] = (uint)c;
  370. c >>= 32;
  371. c += x_i * y_6 + zz[i + 6];
  372. zz[i + 6] = (uint)c;
  373. c >>= 32;
  374. zz[i + 7] = (uint)c;
  375. }
  376. }
  377. public static void Mul(uint[] x, int xOff, uint[] y, int yOff, uint[] zz, int zzOff)
  378. {
  379. ulong y_0 = y[yOff + 0];
  380. ulong y_1 = y[yOff + 1];
  381. ulong y_2 = y[yOff + 2];
  382. ulong y_3 = y[yOff + 3];
  383. ulong y_4 = y[yOff + 4];
  384. ulong y_5 = y[yOff + 5];
  385. ulong y_6 = y[yOff + 6];
  386. {
  387. ulong c = 0, x_0 = x[xOff + 0];
  388. c += x_0 * y_0;
  389. zz[zzOff + 0] = (uint)c;
  390. c >>= 32;
  391. c += x_0 * y_1;
  392. zz[zzOff + 1] = (uint)c;
  393. c >>= 32;
  394. c += x_0 * y_2;
  395. zz[zzOff + 2] = (uint)c;
  396. c >>= 32;
  397. c += x_0 * y_3;
  398. zz[zzOff + 3] = (uint)c;
  399. c >>= 32;
  400. c += x_0 * y_4;
  401. zz[zzOff + 4] = (uint)c;
  402. c >>= 32;
  403. c += x_0 * y_5;
  404. zz[zzOff + 5] = (uint)c;
  405. c >>= 32;
  406. c += x_0 * y_6;
  407. zz[zzOff + 6] = (uint)c;
  408. c >>= 32;
  409. zz[zzOff + 7] = (uint)c;
  410. }
  411. for (int i = 1; i < 7; ++i)
  412. {
  413. ++zzOff;
  414. ulong c = 0, x_i = x[xOff + i];
  415. c += x_i * y_0 + zz[zzOff + 0];
  416. zz[zzOff + 0] = (uint)c;
  417. c >>= 32;
  418. c += x_i * y_1 + zz[zzOff + 1];
  419. zz[zzOff + 1] = (uint)c;
  420. c >>= 32;
  421. c += x_i * y_2 + zz[zzOff + 2];
  422. zz[zzOff + 2] = (uint)c;
  423. c >>= 32;
  424. c += x_i * y_3 + zz[zzOff + 3];
  425. zz[zzOff + 3] = (uint)c;
  426. c >>= 32;
  427. c += x_i * y_4 + zz[zzOff + 4];
  428. zz[zzOff + 4] = (uint)c;
  429. c >>= 32;
  430. c += x_i * y_5 + zz[zzOff + 5];
  431. zz[zzOff + 5] = (uint)c;
  432. c >>= 32;
  433. c += x_i * y_6 + zz[zzOff + 6];
  434. zz[zzOff + 6] = (uint)c;
  435. c >>= 32;
  436. zz[zzOff + 7] = (uint)c;
  437. }
  438. }
  439. public static uint MulAddTo(uint[] x, uint[] y, uint[] zz)
  440. {
  441. ulong y_0 = y[0];
  442. ulong y_1 = y[1];
  443. ulong y_2 = y[2];
  444. ulong y_3 = y[3];
  445. ulong y_4 = y[4];
  446. ulong y_5 = y[5];
  447. ulong y_6 = y[6];
  448. ulong zc = 0;
  449. for (int i = 0; i < 7; ++i)
  450. {
  451. ulong c = 0, x_i = x[i];
  452. c += x_i * y_0 + zz[i + 0];
  453. zz[i + 0] = (uint)c;
  454. c >>= 32;
  455. c += x_i * y_1 + zz[i + 1];
  456. zz[i + 1] = (uint)c;
  457. c >>= 32;
  458. c += x_i * y_2 + zz[i + 2];
  459. zz[i + 2] = (uint)c;
  460. c >>= 32;
  461. c += x_i * y_3 + zz[i + 3];
  462. zz[i + 3] = (uint)c;
  463. c >>= 32;
  464. c += x_i * y_4 + zz[i + 4];
  465. zz[i + 4] = (uint)c;
  466. c >>= 32;
  467. c += x_i * y_5 + zz[i + 5];
  468. zz[i + 5] = (uint)c;
  469. c >>= 32;
  470. c += x_i * y_6 + zz[i + 6];
  471. zz[i + 6] = (uint)c;
  472. c >>= 32;
  473. c += zc + zz[i + 7];
  474. zz[i + 7] = (uint)c;
  475. zc = c >> 32;
  476. }
  477. return (uint)zc;
  478. }
  479. public static uint MulAddTo(uint[] x, int xOff, uint[] y, int yOff, uint[] zz, int zzOff)
  480. {
  481. ulong y_0 = y[yOff + 0];
  482. ulong y_1 = y[yOff + 1];
  483. ulong y_2 = y[yOff + 2];
  484. ulong y_3 = y[yOff + 3];
  485. ulong y_4 = y[yOff + 4];
  486. ulong y_5 = y[yOff + 5];
  487. ulong y_6 = y[yOff + 6];
  488. ulong zc = 0;
  489. for (int i = 0; i < 7; ++i)
  490. {
  491. ulong c = 0, x_i = x[xOff + i];
  492. c += x_i * y_0 + zz[zzOff + 0];
  493. zz[zzOff + 0] = (uint)c;
  494. c >>= 32;
  495. c += x_i * y_1 + zz[zzOff + 1];
  496. zz[zzOff + 1] = (uint)c;
  497. c >>= 32;
  498. c += x_i * y_2 + zz[zzOff + 2];
  499. zz[zzOff + 2] = (uint)c;
  500. c >>= 32;
  501. c += x_i * y_3 + zz[zzOff + 3];
  502. zz[zzOff + 3] = (uint)c;
  503. c >>= 32;
  504. c += x_i * y_4 + zz[zzOff + 4];
  505. zz[zzOff + 4] = (uint)c;
  506. c >>= 32;
  507. c += x_i * y_5 + zz[zzOff + 5];
  508. zz[zzOff + 5] = (uint)c;
  509. c >>= 32;
  510. c += x_i * y_6 + zz[zzOff + 6];
  511. zz[zzOff + 6] = (uint)c;
  512. c >>= 32;
  513. c += zc + zz[zzOff + 7];
  514. zz[zzOff + 7] = (uint)c;
  515. zc = c >> 32;
  516. ++zzOff;
  517. }
  518. return (uint)zc;
  519. }
  520. public static ulong Mul33Add(uint w, uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff)
  521. {
  522. Debug.Assert(w >> 31 == 0);
  523. ulong c = 0, wVal = w;
  524. ulong x0 = x[xOff + 0];
  525. c += wVal * x0 + y[yOff + 0];
  526. z[zOff + 0] = (uint)c;
  527. c >>= 32;
  528. ulong x1 = x[xOff + 1];
  529. c += wVal * x1 + x0 + y[yOff + 1];
  530. z[zOff + 1] = (uint)c;
  531. c >>= 32;
  532. ulong x2 = x[xOff + 2];
  533. c += wVal * x2 + x1 + y[yOff + 2];
  534. z[zOff + 2] = (uint)c;
  535. c >>= 32;
  536. ulong x3 = x[xOff + 3];
  537. c += wVal * x3 + x2 + y[yOff + 3];
  538. z[zOff + 3] = (uint)c;
  539. c >>= 32;
  540. ulong x4 = x[xOff + 4];
  541. c += wVal * x4 + x3 + y[yOff + 4];
  542. z[zOff + 4] = (uint)c;
  543. c >>= 32;
  544. ulong x5 = x[xOff + 5];
  545. c += wVal * x5 + x4 + y[yOff + 5];
  546. z[zOff + 5] = (uint)c;
  547. c >>= 32;
  548. ulong x6 = x[xOff + 6];
  549. c += wVal * x6 + x5 + y[yOff + 6];
  550. z[zOff + 6] = (uint)c;
  551. c >>= 32;
  552. c += x6;
  553. return c;
  554. }
  555. public static uint MulByWord(uint x, uint[] z)
  556. {
  557. ulong c = 0, xVal = x;
  558. c += xVal * (ulong)z[0];
  559. z[0] = (uint)c;
  560. c >>= 32;
  561. c += xVal * (ulong)z[1];
  562. z[1] = (uint)c;
  563. c >>= 32;
  564. c += xVal * (ulong)z[2];
  565. z[2] = (uint)c;
  566. c >>= 32;
  567. c += xVal * (ulong)z[3];
  568. z[3] = (uint)c;
  569. c >>= 32;
  570. c += xVal * (ulong)z[4];
  571. z[4] = (uint)c;
  572. c >>= 32;
  573. c += xVal * (ulong)z[5];
  574. z[5] = (uint)c;
  575. c >>= 32;
  576. c += xVal * (ulong)z[6];
  577. z[6] = (uint)c;
  578. c >>= 32;
  579. return (uint)c;
  580. }
  581. public static uint MulByWordAddTo(uint x, uint[] y, uint[] z)
  582. {
  583. ulong c = 0, xVal = x;
  584. c += xVal * (ulong)z[0] + y[0];
  585. z[0] = (uint)c;
  586. c >>= 32;
  587. c += xVal * (ulong)z[1] + y[1];
  588. z[1] = (uint)c;
  589. c >>= 32;
  590. c += xVal * (ulong)z[2] + y[2];
  591. z[2] = (uint)c;
  592. c >>= 32;
  593. c += xVal * (ulong)z[3] + y[3];
  594. z[3] = (uint)c;
  595. c >>= 32;
  596. c += xVal * (ulong)z[4] + y[4];
  597. z[4] = (uint)c;
  598. c >>= 32;
  599. c += xVal * (ulong)z[5] + y[5];
  600. z[5] = (uint)c;
  601. c >>= 32;
  602. c += xVal * (ulong)z[6] + y[6];
  603. z[6] = (uint)c;
  604. c >>= 32;
  605. return (uint)c;
  606. }
  607. public static uint MulWordAddTo(uint x, uint[] y, int yOff, uint[] z, int zOff)
  608. {
  609. ulong c = 0, xVal = x;
  610. c += xVal * y[yOff + 0] + z[zOff + 0];
  611. z[zOff + 0] = (uint)c;
  612. c >>= 32;
  613. c += xVal * y[yOff + 1] + z[zOff + 1];
  614. z[zOff + 1] = (uint)c;
  615. c >>= 32;
  616. c += xVal * y[yOff + 2] + z[zOff + 2];
  617. z[zOff + 2] = (uint)c;
  618. c >>= 32;
  619. c += xVal * y[yOff + 3] + z[zOff + 3];
  620. z[zOff + 3] = (uint)c;
  621. c >>= 32;
  622. c += xVal * y[yOff + 4] + z[zOff + 4];
  623. z[zOff + 4] = (uint)c;
  624. c >>= 32;
  625. c += xVal * y[yOff + 5] + z[zOff + 5];
  626. z[zOff + 5] = (uint)c;
  627. c >>= 32;
  628. c += xVal * y[yOff + 6] + z[zOff + 6];
  629. z[zOff + 6] = (uint)c;
  630. c >>= 32;
  631. return (uint)c;
  632. }
  633. public static uint Mul33DWordAdd(uint x, ulong y, uint[] z, int zOff)
  634. {
  635. Debug.Assert(x >> 31 == 0);
  636. Debug.Assert(zOff <= 3);
  637. ulong c = 0, xVal = x;
  638. ulong y00 = y & M;
  639. c += xVal * y00 + z[zOff + 0];
  640. z[zOff + 0] = (uint)c;
  641. c >>= 32;
  642. ulong y01 = y >> 32;
  643. c += xVal * y01 + y00 + z[zOff + 1];
  644. z[zOff + 1] = (uint)c;
  645. c >>= 32;
  646. c += y01 + z[zOff + 2];
  647. z[zOff + 2] = (uint)c;
  648. c >>= 32;
  649. c += z[zOff + 3];
  650. z[zOff + 3] = (uint)c;
  651. c >>= 32;
  652. return c == 0 ? 0 : Nat.IncAt(7, z, zOff, 4);
  653. }
  654. public static uint Mul33WordAdd(uint x, uint y, uint[] z, int zOff)
  655. {
  656. Debug.Assert(x >> 31 == 0);
  657. Debug.Assert(zOff <= 4);
  658. ulong c = 0, yVal = y;
  659. c += yVal * x + z[zOff + 0];
  660. z[zOff + 0] = (uint)c;
  661. c >>= 32;
  662. c += yVal + z[zOff + 1];
  663. z[zOff + 1] = (uint)c;
  664. c >>= 32;
  665. c += z[zOff + 2];
  666. z[zOff + 2] = (uint)c;
  667. c >>= 32;
  668. return c == 0 ? 0 : Nat.IncAt(7, z, zOff, 3);
  669. }
  670. public static uint MulWordDwordAdd(uint x, ulong y, uint[] z, int zOff)
  671. {
  672. Debug.Assert(zOff <= 4);
  673. ulong c = 0, xVal = x;
  674. c += xVal * y + z[zOff + 0];
  675. z[zOff + 0] = (uint)c;
  676. c >>= 32;
  677. c += xVal * (y >> 32) + z[zOff + 1];
  678. z[zOff + 1] = (uint)c;
  679. c >>= 32;
  680. c += z[zOff + 2];
  681. z[zOff + 2] = (uint)c;
  682. c >>= 32;
  683. return c == 0 ? 0 : Nat.IncAt(7, z, zOff, 3);
  684. }
  685. public static uint MulWord(uint x, uint[] y, uint[] z, int zOff)
  686. {
  687. ulong c = 0, xVal = x;
  688. int i = 0;
  689. do
  690. {
  691. c += xVal * y[i];
  692. z[zOff + i] = (uint)c;
  693. c >>= 32;
  694. }
  695. while (++i < 7);
  696. return (uint)c;
  697. }
  698. public static void Square(uint[] x, uint[] zz)
  699. {
  700. ulong x_0 = x[0];
  701. ulong zz_1;
  702. uint c = 0, w;
  703. {
  704. int i = 6, j = 14;
  705. do
  706. {
  707. ulong xVal = x[i--];
  708. ulong p = xVal * xVal;
  709. zz[--j] = (c << 31) | (uint)(p >> 33);
  710. zz[--j] = (uint)(p >> 1);
  711. c = (uint)p;
  712. }
  713. while (i > 0);
  714. {
  715. ulong p = x_0 * x_0;
  716. zz_1 = (ulong)(c << 31) | (p >> 33);
  717. zz[0] = (uint)p;
  718. c = (uint)(p >> 32) & 1;
  719. }
  720. }
  721. ulong x_1 = x[1];
  722. ulong zz_2 = zz[2];
  723. {
  724. zz_1 += x_1 * x_0;
  725. w = (uint)zz_1;
  726. zz[1] = (w << 1) | c;
  727. c = w >> 31;
  728. zz_2 += zz_1 >> 32;
  729. }
  730. ulong x_2 = x[2];
  731. ulong zz_3 = zz[3];
  732. ulong zz_4 = zz[4];
  733. {
  734. zz_2 += x_2 * x_0;
  735. w = (uint)zz_2;
  736. zz[2] = (w << 1) | c;
  737. c = w >> 31;
  738. zz_3 += (zz_2 >> 32) + x_2 * x_1;
  739. zz_4 += zz_3 >> 32;
  740. zz_3 &= M;
  741. }
  742. ulong x_3 = x[3];
  743. ulong zz_5 = zz[5];
  744. ulong zz_6 = zz[6];
  745. {
  746. zz_3 += x_3 * x_0;
  747. w = (uint)zz_3;
  748. zz[3] = (w << 1) | c;
  749. c = w >> 31;
  750. zz_4 += (zz_3 >> 32) + x_3 * x_1;
  751. zz_5 += (zz_4 >> 32) + x_3 * x_2;
  752. zz_4 &= M;
  753. zz_6 += zz_5 >> 32;
  754. zz_5 &= M;
  755. }
  756. ulong x_4 = x[4];
  757. ulong zz_7 = zz[7];
  758. ulong zz_8 = zz[8];
  759. {
  760. zz_4 += x_4 * x_0;
  761. w = (uint)zz_4;
  762. zz[4] = (w << 1) | c;
  763. c = w >> 31;
  764. zz_5 += (zz_4 >> 32) + x_4 * x_1;
  765. zz_6 += (zz_5 >> 32) + x_4 * x_2;
  766. zz_5 &= M;
  767. zz_7 += (zz_6 >> 32) + x_4 * x_3;
  768. zz_6 &= M;
  769. zz_8 += zz_7 >> 32;
  770. zz_7 &= M;
  771. }
  772. ulong x_5 = x[5];
  773. ulong zz_9 = zz[9];
  774. ulong zz_10 = zz[10];
  775. {
  776. zz_5 += x_5 * x_0;
  777. w = (uint)zz_5;
  778. zz[5] = (w << 1) | c;
  779. c = w >> 31;
  780. zz_6 += (zz_5 >> 32) + x_5 * x_1;
  781. zz_7 += (zz_6 >> 32) + x_5 * x_2;
  782. zz_6 &= M;
  783. zz_8 += (zz_7 >> 32) + x_5 * x_3;
  784. zz_7 &= M;
  785. zz_9 += (zz_8 >> 32) + x_5 * x_4;
  786. zz_8 &= M;
  787. zz_10 += zz_9 >> 32;
  788. zz_9 &= M;
  789. }
  790. ulong x_6 = x[6];
  791. ulong zz_11 = zz[11];
  792. ulong zz_12 = zz[12];
  793. {
  794. zz_6 += x_6 * x_0;
  795. w = (uint)zz_6;
  796. zz[6] = (w << 1) | c;
  797. c = w >> 31;
  798. zz_7 += (zz_6 >> 32) + x_6 * x_1;
  799. zz_8 += (zz_7 >> 32) + x_6 * x_2;
  800. zz_9 += (zz_8 >> 32) + x_6 * x_3;
  801. zz_10 += (zz_9 >> 32) + x_6 * x_4;
  802. zz_11 += (zz_10 >> 32) + x_6 * x_5;
  803. zz_12 += zz_11 >> 32;
  804. }
  805. w = (uint)zz_7;
  806. zz[7] = (w << 1) | c;
  807. c = w >> 31;
  808. w = (uint)zz_8;
  809. zz[8] = (w << 1) | c;
  810. c = w >> 31;
  811. w = (uint)zz_9;
  812. zz[9] = (w << 1) | c;
  813. c = w >> 31;
  814. w = (uint)zz_10;
  815. zz[10] = (w << 1) | c;
  816. c = w >> 31;
  817. w = (uint)zz_11;
  818. zz[11] = (w << 1) | c;
  819. c = w >> 31;
  820. w = (uint)zz_12;
  821. zz[12] = (w << 1) | c;
  822. c = w >> 31;
  823. w = zz[13] + (uint)(zz_12 >> 32);
  824. zz[13] = (w << 1) | c;
  825. }
  826. public static void Square(uint[] x, int xOff, uint[] zz, int zzOff)
  827. {
  828. ulong x_0 = x[xOff + 0];
  829. ulong zz_1;
  830. uint c = 0, w;
  831. {
  832. int i = 6, j = 14;
  833. do
  834. {
  835. ulong xVal = x[xOff + i--];
  836. ulong p = xVal * xVal;
  837. zz[zzOff + --j] = (c << 31) | (uint)(p >> 33);
  838. zz[zzOff + --j] = (uint)(p >> 1);
  839. c = (uint)p;
  840. }
  841. while (i > 0);
  842. {
  843. ulong p = x_0 * x_0;
  844. zz_1 = (ulong)(c << 31) | (p >> 33);
  845. zz[zzOff + 0] = (uint)p;
  846. c = (uint)(p >> 32) & 1;
  847. }
  848. }
  849. ulong x_1 = x[xOff + 1];
  850. ulong zz_2 = zz[zzOff + 2];
  851. {
  852. zz_1 += x_1 * x_0;
  853. w = (uint)zz_1;
  854. zz[zzOff + 1] = (w << 1) | c;
  855. c = w >> 31;
  856. zz_2 += zz_1 >> 32;
  857. }
  858. ulong x_2 = x[xOff + 2];
  859. ulong zz_3 = zz[zzOff + 3];
  860. ulong zz_4 = zz[zzOff + 4];
  861. {
  862. zz_2 += x_2 * x_0;
  863. w = (uint)zz_2;
  864. zz[zzOff + 2] = (w << 1) | c;
  865. c = w >> 31;
  866. zz_3 += (zz_2 >> 32) + x_2 * x_1;
  867. zz_4 += zz_3 >> 32;
  868. zz_3 &= M;
  869. }
  870. ulong x_3 = x[xOff + 3];
  871. ulong zz_5 = zz[zzOff + 5];
  872. ulong zz_6 = zz[zzOff + 6];
  873. {
  874. zz_3 += x_3 * x_0;
  875. w = (uint)zz_3;
  876. zz[zzOff + 3] = (w << 1) | c;
  877. c = w >> 31;
  878. zz_4 += (zz_3 >> 32) + x_3 * x_1;
  879. zz_5 += (zz_4 >> 32) + x_3 * x_2;
  880. zz_4 &= M;
  881. zz_6 += zz_5 >> 32;
  882. zz_5 &= M;
  883. }
  884. ulong x_4 = x[xOff + 4];
  885. ulong zz_7 = zz[zzOff + 7];
  886. ulong zz_8 = zz[zzOff + 8];
  887. {
  888. zz_4 += x_4 * x_0;
  889. w = (uint)zz_4;
  890. zz[zzOff + 4] = (w << 1) | c;
  891. c = w >> 31;
  892. zz_5 += (zz_4 >> 32) + x_4 * x_1;
  893. zz_6 += (zz_5 >> 32) + x_4 * x_2;
  894. zz_5 &= M;
  895. zz_7 += (zz_6 >> 32) + x_4 * x_3;
  896. zz_6 &= M;
  897. zz_8 += zz_7 >> 32;
  898. zz_7 &= M;
  899. }
  900. ulong x_5 = x[xOff + 5];
  901. ulong zz_9 = zz[zzOff + 9];
  902. ulong zz_10 = zz[zzOff + 10];
  903. {
  904. zz_5 += x_5 * x_0;
  905. w = (uint)zz_5;
  906. zz[zzOff + 5] = (w << 1) | c;
  907. c = w >> 31;
  908. zz_6 += (zz_5 >> 32) + x_5 * x_1;
  909. zz_7 += (zz_6 >> 32) + x_5 * x_2;
  910. zz_6 &= M;
  911. zz_8 += (zz_7 >> 32) + x_5 * x_3;
  912. zz_7 &= M;
  913. zz_9 += (zz_8 >> 32) + x_5 * x_4;
  914. zz_8 &= M;
  915. zz_10 += zz_9 >> 32;
  916. zz_9 &= M;
  917. }
  918. ulong x_6 = x[xOff + 6];
  919. ulong zz_11 = zz[zzOff + 11];
  920. ulong zz_12 = zz[zzOff + 12];
  921. {
  922. zz_6 += x_6 * x_0;
  923. w = (uint)zz_6;
  924. zz[zzOff + 6] = (w << 1) | c;
  925. c = w >> 31;
  926. zz_7 += (zz_6 >> 32) + x_6 * x_1;
  927. zz_8 += (zz_7 >> 32) + x_6 * x_2;
  928. zz_9 += (zz_8 >> 32) + x_6 * x_3;
  929. zz_10 += (zz_9 >> 32) + x_6 * x_4;
  930. zz_11 += (zz_10 >> 32) + x_6 * x_5;
  931. zz_12 += zz_11 >> 32;
  932. }
  933. w = (uint)zz_7;
  934. zz[zzOff + 7] = (w << 1) | c;
  935. c = w >> 31;
  936. w = (uint)zz_8;
  937. zz[zzOff + 8] = (w << 1) | c;
  938. c = w >> 31;
  939. w = (uint)zz_9;
  940. zz[zzOff + 9] = (w << 1) | c;
  941. c = w >> 31;
  942. w = (uint)zz_10;
  943. zz[zzOff + 10] = (w << 1) | c;
  944. c = w >> 31;
  945. w = (uint)zz_11;
  946. zz[zzOff + 11] = (w << 1) | c;
  947. c = w >> 31;
  948. w = (uint)zz_12;
  949. zz[zzOff + 12] = (w << 1) | c;
  950. c = w >> 31;
  951. w = zz[zzOff + 13] + (uint)(zz_12 >> 32);
  952. zz[zzOff + 13] = (w << 1) | c;
  953. }
  954. public static int Sub(uint[] x, uint[] y, uint[] z)
  955. {
  956. long c = 0;
  957. c += (long)x[0] - y[0];
  958. z[0] = (uint)c;
  959. c >>= 32;
  960. c += (long)x[1] - y[1];
  961. z[1] = (uint)c;
  962. c >>= 32;
  963. c += (long)x[2] - y[2];
  964. z[2] = (uint)c;
  965. c >>= 32;
  966. c += (long)x[3] - y[3];
  967. z[3] = (uint)c;
  968. c >>= 32;
  969. c += (long)x[4] - y[4];
  970. z[4] = (uint)c;
  971. c >>= 32;
  972. c += (long)x[5] - y[5];
  973. z[5] = (uint)c;
  974. c >>= 32;
  975. c += (long)x[6] - y[6];
  976. z[6] = (uint)c;
  977. c >>= 32;
  978. return (int)c;
  979. }
  980. public static int Sub(uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff)
  981. {
  982. long c = 0;
  983. c += (long)x[xOff + 0] - y[yOff + 0];
  984. z[zOff + 0] = (uint)c;
  985. c >>= 32;
  986. c += (long)x[xOff + 1] - y[yOff + 1];
  987. z[zOff + 1] = (uint)c;
  988. c >>= 32;
  989. c += (long)x[xOff + 2] - y[yOff + 2];
  990. z[zOff + 2] = (uint)c;
  991. c >>= 32;
  992. c += (long)x[xOff + 3] - y[yOff + 3];
  993. z[zOff + 3] = (uint)c;
  994. c >>= 32;
  995. c += (long)x[xOff + 4] - y[yOff + 4];
  996. z[zOff + 4] = (uint)c;
  997. c >>= 32;
  998. c += (long)x[xOff + 5] - y[yOff + 5];
  999. z[zOff + 5] = (uint)c;
  1000. c >>= 32;
  1001. c += (long)x[xOff + 6] - y[yOff + 6];
  1002. z[zOff + 6] = (uint)c;
  1003. c >>= 32;
  1004. return (int)c;
  1005. }
  1006. public static int SubBothFrom(uint[] x, uint[] y, uint[] z)
  1007. {
  1008. long c = 0;
  1009. c += (long)z[0] - x[0] - y[0];
  1010. z[0] = (uint)c;
  1011. c >>= 32;
  1012. c += (long)z[1] - x[1] - y[1];
  1013. z[1] = (uint)c;
  1014. c >>= 32;
  1015. c += (long)z[2] - x[2] - y[2];
  1016. z[2] = (uint)c;
  1017. c >>= 32;
  1018. c += (long)z[3] - x[3] - y[3];
  1019. z[3] = (uint)c;
  1020. c >>= 32;
  1021. c += (long)z[4] - x[4] - y[4];
  1022. z[4] = (uint)c;
  1023. c >>= 32;
  1024. c += (long)z[5] - x[5] - y[5];
  1025. z[5] = (uint)c;
  1026. c >>= 32;
  1027. c += (long)z[6] - x[6] - y[6];
  1028. z[6] = (uint)c;
  1029. c >>= 32;
  1030. return (int)c;
  1031. }
  1032. public static int SubFrom(uint[] x, uint[] z)
  1033. {
  1034. long c = 0;
  1035. c += (long)z[0] - x[0];
  1036. z[0] = (uint)c;
  1037. c >>= 32;
  1038. c += (long)z[1] - x[1];
  1039. z[1] = (uint)c;
  1040. c >>= 32;
  1041. c += (long)z[2] - x[2];
  1042. z[2] = (uint)c;
  1043. c >>= 32;
  1044. c += (long)z[3] - x[3];
  1045. z[3] = (uint)c;
  1046. c >>= 32;
  1047. c += (long)z[4] - x[4];
  1048. z[4] = (uint)c;
  1049. c >>= 32;
  1050. c += (long)z[5] - x[5];
  1051. z[5] = (uint)c;
  1052. c >>= 32;
  1053. c += (long)z[6] - x[6];
  1054. z[6] = (uint)c;
  1055. c >>= 32;
  1056. return (int)c;
  1057. }
  1058. public static int SubFrom(uint[] x, int xOff, uint[] z, int zOff)
  1059. {
  1060. long c = 0;
  1061. c += (long)z[zOff + 0] - x[xOff + 0];
  1062. z[zOff + 0] = (uint)c;
  1063. c >>= 32;
  1064. c += (long)z[zOff + 1] - x[xOff + 1];
  1065. z[zOff + 1] = (uint)c;
  1066. c >>= 32;
  1067. c += (long)z[zOff + 2] - x[xOff + 2];
  1068. z[zOff + 2] = (uint)c;
  1069. c >>= 32;
  1070. c += (long)z[zOff + 3] - x[xOff + 3];
  1071. z[zOff + 3] = (uint)c;
  1072. c >>= 32;
  1073. c += (long)z[zOff + 4] - x[xOff + 4];
  1074. z[zOff + 4] = (uint)c;
  1075. c >>= 32;
  1076. c += (long)z[zOff + 5] - x[xOff + 5];
  1077. z[zOff + 5] = (uint)c;
  1078. c >>= 32;
  1079. c += (long)z[zOff + 6] - x[xOff + 6];
  1080. z[zOff + 6] = (uint)c;
  1081. c >>= 32;
  1082. return (int)c;
  1083. }
  1084. public static BigInteger ToBigInteger(uint[] x)
  1085. {
  1086. byte[] bs = new byte[28];
  1087. for (int i = 0; i < 7; ++i)
  1088. {
  1089. uint x_i = x[i];
  1090. if (x_i != 0)
  1091. {
  1092. Pack.UInt32_To_BE(x_i, bs, (6 - i) << 2);
  1093. }
  1094. }
  1095. return new BigInteger(1, bs);
  1096. }
  1097. public static void Zero(uint[] z)
  1098. {
  1099. z[0] = 0;
  1100. z[1] = 0;
  1101. z[2] = 0;
  1102. z[3] = 0;
  1103. z[4] = 0;
  1104. z[5] = 0;
  1105. z[6] = 0;
  1106. }
  1107. }
  1108. }
  1109. #endif