Nat192.cs 30 KB

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