sshbn.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178
  1. /*
  2. * Bignum routines for RSA and DH and stuff.
  3. */
  4. #include <stdio.h>
  5. #include <assert.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <limits.h>
  9. #include <ctype.h>
  10. #include "misc.h"
  11. #include "sshbn.h"
  12. #define BIGNUM_INTERNAL
  13. typedef BignumInt *Bignum;
  14. #include "ssh.h"
  15. #include "marshal.h"
  16. BignumInt bnZero[1] = { 0 };
  17. BignumInt bnOne[2] = { 1, 1 };
  18. BignumInt bnTen[2] = { 1, 10 };
  19. /*
  20. * The Bignum format is an array of `BignumInt'. The first
  21. * element of the array counts the remaining elements. The
  22. * remaining elements express the actual number, base 2^BIGNUM_INT_BITS, _least_
  23. * significant digit first. (So it's trivial to extract the bit
  24. * with value 2^n for any n.)
  25. *
  26. * All Bignums in this module are positive. Negative numbers must
  27. * be dealt with outside it.
  28. *
  29. * INVARIANT: the most significant word of any Bignum must be
  30. * nonzero.
  31. */
  32. Bignum Zero = bnZero, One = bnOne, Ten = bnTen;
  33. static Bignum newbn(int length)
  34. {
  35. Bignum b;
  36. assert(length >= 0 && length < INT_MAX / BIGNUM_INT_BITS);
  37. b = snewn(length + 1, BignumInt);
  38. memset(b, 0, (length + 1) * sizeof(*b));
  39. b[0] = length;
  40. return b;
  41. }
  42. void bn_restore_invariant(Bignum b)
  43. {
  44. while (b[0] > 1 && b[b[0]] == 0)
  45. b[0]--;
  46. }
  47. Bignum copybn(Bignum orig)
  48. {
  49. Bignum b = snewn(orig[0] + 1, BignumInt);
  50. if (!b)
  51. abort(); /* FIXME */
  52. memcpy(b, orig, (orig[0] + 1) * sizeof(*b));
  53. return b;
  54. }
  55. void freebn(Bignum b)
  56. {
  57. /*
  58. * Burn the evidence, just in case.
  59. */
  60. smemclr(b, sizeof(b[0]) * (b[0] + 1));
  61. sfree(b);
  62. }
  63. Bignum bn_power_2(int n)
  64. {
  65. Bignum ret;
  66. assert(n >= 0);
  67. ret = newbn(n / BIGNUM_INT_BITS + 1);
  68. bignum_set_bit(ret, n, 1);
  69. return ret;
  70. }
  71. /*
  72. * Internal addition. Sets c = a - b, where 'a', 'b' and 'c' are all
  73. * big-endian arrays of 'len' BignumInts. Returns the carry off the
  74. * top.
  75. */
  76. static BignumCarry internal_add(const BignumInt *a, const BignumInt *b,
  77. BignumInt *c, int len)
  78. {
  79. int i;
  80. BignumCarry carry = 0;
  81. for (i = len-1; i >= 0; i--)
  82. BignumADC(c[i], carry, a[i], b[i], carry);
  83. return (BignumInt)carry;
  84. }
  85. /*
  86. * Internal subtraction. Sets c = a - b, where 'a', 'b' and 'c' are
  87. * all big-endian arrays of 'len' BignumInts. Any borrow from the top
  88. * is ignored.
  89. */
  90. static void internal_sub(const BignumInt *a, const BignumInt *b,
  91. BignumInt *c, int len)
  92. {
  93. int i;
  94. BignumCarry carry = 1;
  95. for (i = len-1; i >= 0; i--)
  96. BignumADC(c[i], carry, a[i], ~b[i], carry);
  97. }
  98. /*
  99. * Compute c = a * b.
  100. * Input is in the first len words of a and b.
  101. * Result is returned in the first 2*len words of c.
  102. *
  103. * 'scratch' must point to an array of BignumInt of size at least
  104. * mul_compute_scratch(len). (This covers the needs of internal_mul
  105. * and all its recursive calls to itself.)
  106. */
  107. #define KARATSUBA_THRESHOLD 50
  108. static int mul_compute_scratch(int len)
  109. {
  110. int ret = 0;
  111. while (len > KARATSUBA_THRESHOLD) {
  112. int toplen = len/2, botlen = len - toplen; /* botlen is the bigger */
  113. int midlen = botlen + 1;
  114. ret += 4*midlen;
  115. len = midlen;
  116. }
  117. return ret;
  118. }
  119. static void internal_mul(const BignumInt *a, const BignumInt *b,
  120. BignumInt *c, int len, BignumInt *scratch)
  121. {
  122. if (len > KARATSUBA_THRESHOLD) {
  123. int i;
  124. /*
  125. * Karatsuba divide-and-conquer algorithm. Cut each input in
  126. * half, so that it's expressed as two big 'digits' in a giant
  127. * base D:
  128. *
  129. * a = a_1 D + a_0
  130. * b = b_1 D + b_0
  131. *
  132. * Then the product is of course
  133. *
  134. * ab = a_1 b_1 D^2 + (a_1 b_0 + a_0 b_1) D + a_0 b_0
  135. *
  136. * and we compute the three coefficients by recursively
  137. * calling ourself to do half-length multiplications.
  138. *
  139. * The clever bit that makes this worth doing is that we only
  140. * need _one_ half-length multiplication for the central
  141. * coefficient rather than the two that it obviouly looks
  142. * like, because we can use a single multiplication to compute
  143. *
  144. * (a_1 + a_0) (b_1 + b_0) = a_1 b_1 + a_1 b_0 + a_0 b_1 + a_0 b_0
  145. *
  146. * and then we subtract the other two coefficients (a_1 b_1
  147. * and a_0 b_0) which we were computing anyway.
  148. *
  149. * Hence we get to multiply two numbers of length N in about
  150. * three times as much work as it takes to multiply numbers of
  151. * length N/2, which is obviously better than the four times
  152. * as much work it would take if we just did a long
  153. * conventional multiply.
  154. */
  155. int toplen = len/2, botlen = len - toplen; /* botlen is the bigger */
  156. int midlen = botlen + 1;
  157. BignumCarry carry;
  158. #ifdef KARA_DEBUG
  159. int i;
  160. #endif
  161. /*
  162. * The coefficients a_1 b_1 and a_0 b_0 just avoid overlapping
  163. * in the output array, so we can compute them immediately in
  164. * place.
  165. */
  166. #ifdef KARA_DEBUG
  167. printf("a1,a0 = 0x");
  168. for (i = 0; i < len; i++) {
  169. if (i == toplen) printf(", 0x");
  170. printf("%0*x", BIGNUM_INT_BITS/4, a[i]);
  171. }
  172. printf("\n");
  173. printf("b1,b0 = 0x");
  174. for (i = 0; i < len; i++) {
  175. if (i == toplen) printf(", 0x");
  176. printf("%0*x", BIGNUM_INT_BITS/4, b[i]);
  177. }
  178. printf("\n");
  179. #endif
  180. /* a_1 b_1 */
  181. internal_mul(a, b, c, toplen, scratch);
  182. #ifdef KARA_DEBUG
  183. printf("a1b1 = 0x");
  184. for (i = 0; i < 2*toplen; i++) {
  185. printf("%0*x", BIGNUM_INT_BITS/4, c[i]);
  186. }
  187. printf("\n");
  188. #endif
  189. /* a_0 b_0 */
  190. internal_mul(a + toplen, b + toplen, c + 2*toplen, botlen, scratch);
  191. #ifdef KARA_DEBUG
  192. printf("a0b0 = 0x");
  193. for (i = 0; i < 2*botlen; i++) {
  194. printf("%0*x", BIGNUM_INT_BITS/4, c[2*toplen+i]);
  195. }
  196. printf("\n");
  197. #endif
  198. /* Zero padding. midlen exceeds toplen by at most 2, so just
  199. * zero the first two words of each input and the rest will be
  200. * copied over. */
  201. scratch[0] = scratch[1] = scratch[midlen] = scratch[midlen+1] = 0;
  202. for (i = 0; i < toplen; i++) {
  203. scratch[midlen - toplen + i] = a[i]; /* a_1 */
  204. scratch[2*midlen - toplen + i] = b[i]; /* b_1 */
  205. }
  206. /* compute a_1 + a_0 */
  207. scratch[0] = internal_add(scratch+1, a+toplen, scratch+1, botlen);
  208. #ifdef KARA_DEBUG
  209. printf("a1plusa0 = 0x");
  210. for (i = 0; i < midlen; i++) {
  211. printf("%0*x", BIGNUM_INT_BITS/4, scratch[i]);
  212. }
  213. printf("\n");
  214. #endif
  215. /* compute b_1 + b_0 */
  216. scratch[midlen] = internal_add(scratch+midlen+1, b+toplen,
  217. scratch+midlen+1, botlen);
  218. #ifdef KARA_DEBUG
  219. printf("b1plusb0 = 0x");
  220. for (i = 0; i < midlen; i++) {
  221. printf("%0*x", BIGNUM_INT_BITS/4, scratch[midlen+i]);
  222. }
  223. printf("\n");
  224. #endif
  225. /*
  226. * Now we can do the third multiplication.
  227. */
  228. internal_mul(scratch, scratch + midlen, scratch + 2*midlen, midlen,
  229. scratch + 4*midlen);
  230. #ifdef KARA_DEBUG
  231. printf("a1plusa0timesb1plusb0 = 0x");
  232. for (i = 0; i < 2*midlen; i++) {
  233. printf("%0*x", BIGNUM_INT_BITS/4, scratch[2*midlen+i]);
  234. }
  235. printf("\n");
  236. #endif
  237. /*
  238. * Now we can reuse the first half of 'scratch' to compute the
  239. * sum of the outer two coefficients, to subtract from that
  240. * product to obtain the middle one.
  241. */
  242. scratch[0] = scratch[1] = scratch[2] = scratch[3] = 0;
  243. for (i = 0; i < 2*toplen; i++)
  244. scratch[2*midlen - 2*toplen + i] = c[i];
  245. scratch[1] = internal_add(scratch+2, c + 2*toplen,
  246. scratch+2, 2*botlen);
  247. #ifdef KARA_DEBUG
  248. printf("a1b1plusa0b0 = 0x");
  249. for (i = 0; i < 2*midlen; i++) {
  250. printf("%0*x", BIGNUM_INT_BITS/4, scratch[i]);
  251. }
  252. printf("\n");
  253. #endif
  254. internal_sub(scratch + 2*midlen, scratch,
  255. scratch + 2*midlen, 2*midlen);
  256. #ifdef KARA_DEBUG
  257. printf("a1b0plusa0b1 = 0x");
  258. for (i = 0; i < 2*midlen; i++) {
  259. printf("%0*x", BIGNUM_INT_BITS/4, scratch[2*midlen+i]);
  260. }
  261. printf("\n");
  262. #endif
  263. /*
  264. * And now all we need to do is to add that middle coefficient
  265. * back into the output. We may have to propagate a carry
  266. * further up the output, but we can be sure it won't
  267. * propagate right the way off the top.
  268. */
  269. carry = internal_add(c + 2*len - botlen - 2*midlen,
  270. scratch + 2*midlen,
  271. c + 2*len - botlen - 2*midlen, 2*midlen);
  272. i = 2*len - botlen - 2*midlen - 1;
  273. while (carry) {
  274. assert(i >= 0);
  275. BignumADC(c[i], carry, c[i], 0, carry);
  276. i--;
  277. }
  278. #ifdef KARA_DEBUG
  279. printf("ab = 0x");
  280. for (i = 0; i < 2*len; i++) {
  281. printf("%0*x", BIGNUM_INT_BITS/4, c[i]);
  282. }
  283. printf("\n");
  284. #endif
  285. } else {
  286. int i;
  287. BignumInt carry;
  288. const BignumInt *ap, *bp;
  289. BignumInt *cp, *cps;
  290. /*
  291. * Multiply in the ordinary O(N^2) way.
  292. */
  293. for (i = 0; i < 2 * len; i++)
  294. c[i] = 0;
  295. for (cps = c + 2*len, ap = a + len; ap-- > a; cps--) {
  296. carry = 0;
  297. for (cp = cps, bp = b + len; cp--, bp-- > b ;)
  298. BignumMULADD2(carry, *cp, *ap, *bp, *cp, carry);
  299. *cp = carry;
  300. }
  301. }
  302. }
  303. /*
  304. * Variant form of internal_mul used for the initial step of
  305. * Montgomery reduction. Only bothers outputting 'len' words
  306. * (everything above that is thrown away).
  307. */
  308. static void internal_mul_low(const BignumInt *a, const BignumInt *b,
  309. BignumInt *c, int len, BignumInt *scratch)
  310. {
  311. if (len > KARATSUBA_THRESHOLD) {
  312. int i;
  313. /*
  314. * Karatsuba-aware version of internal_mul_low. As before, we
  315. * express each input value as a shifted combination of two
  316. * halves:
  317. *
  318. * a = a_1 D + a_0
  319. * b = b_1 D + b_0
  320. *
  321. * Then the full product is, as before,
  322. *
  323. * ab = a_1 b_1 D^2 + (a_1 b_0 + a_0 b_1) D + a_0 b_0
  324. *
  325. * Provided we choose D on the large side (so that a_0 and b_0
  326. * are _at least_ as long as a_1 and b_1), we don't need the
  327. * topmost term at all, and we only need half of the middle
  328. * term. So there's no point in doing the proper Karatsuba
  329. * optimisation which computes the middle term using the top
  330. * one, because we'd take as long computing the top one as
  331. * just computing the middle one directly.
  332. *
  333. * So instead, we do a much more obvious thing: we call the
  334. * fully optimised internal_mul to compute a_0 b_0, and we
  335. * recursively call ourself to compute the _bottom halves_ of
  336. * a_1 b_0 and a_0 b_1, each of which we add into the result
  337. * in the obvious way.
  338. *
  339. * In other words, there's no actual Karatsuba _optimisation_
  340. * in this function; the only benefit in doing it this way is
  341. * that we call internal_mul proper for a large part of the
  342. * work, and _that_ can optimise its operation.
  343. */
  344. int toplen = len/2, botlen = len - toplen; /* botlen is the bigger */
  345. /*
  346. * Scratch space for the various bits and pieces we're going
  347. * to be adding together: we need botlen*2 words for a_0 b_0
  348. * (though we may end up throwing away its topmost word), and
  349. * toplen words for each of a_1 b_0 and a_0 b_1. That adds up
  350. * to exactly 2*len.
  351. */
  352. /* a_0 b_0 */
  353. internal_mul(a + toplen, b + toplen, scratch + 2*toplen, botlen,
  354. scratch + 2*len);
  355. /* a_1 b_0 */
  356. internal_mul_low(a, b + len - toplen, scratch + toplen, toplen,
  357. scratch + 2*len);
  358. /* a_0 b_1 */
  359. internal_mul_low(a + len - toplen, b, scratch, toplen,
  360. scratch + 2*len);
  361. /* Copy the bottom half of the big coefficient into place */
  362. for (i = 0; i < botlen; i++)
  363. c[toplen + i] = scratch[2*toplen + botlen + i];
  364. /* Add the two small coefficients, throwing away the returned carry */
  365. internal_add(scratch, scratch + toplen, scratch, toplen);
  366. /* And add that to the large coefficient, leaving the result in c. */
  367. internal_add(scratch, scratch + 2*toplen + botlen - toplen,
  368. c, toplen);
  369. } else {
  370. int i;
  371. BignumInt carry;
  372. const BignumInt *ap, *bp;
  373. BignumInt *cp, *cps;
  374. /*
  375. * Multiply in the ordinary O(N^2) way.
  376. */
  377. for (i = 0; i < len; i++)
  378. c[i] = 0;
  379. for (cps = c + len, ap = a + len; ap-- > a; cps--) {
  380. carry = 0;
  381. for (cp = cps, bp = b + len; bp--, cp-- > c ;)
  382. BignumMULADD2(carry, *cp, *ap, *bp, *cp, carry);
  383. }
  384. }
  385. }
  386. /*
  387. * Montgomery reduction. Expects x to be a big-endian array of 2*len
  388. * BignumInts whose value satisfies 0 <= x < rn (where r = 2^(len *
  389. * BIGNUM_INT_BITS) is the Montgomery base). Returns in the same array
  390. * a value x' which is congruent to xr^{-1} mod n, and satisfies 0 <=
  391. * x' < n.
  392. *
  393. * 'n' and 'mninv' should be big-endian arrays of 'len' BignumInts
  394. * each, containing respectively n and the multiplicative inverse of
  395. * -n mod r.
  396. *
  397. * 'tmp' is an array of BignumInt used as scratch space, of length at
  398. * least 3*len + mul_compute_scratch(len).
  399. */
  400. static void monty_reduce(BignumInt *x, const BignumInt *n,
  401. const BignumInt *mninv, BignumInt *tmp, int len)
  402. {
  403. int i;
  404. BignumInt carry;
  405. /*
  406. * Multiply x by (-n)^{-1} mod r. This gives us a value m such
  407. * that mn is congruent to -x mod r. Hence, mn+x is an exact
  408. * multiple of r, and is also (obviously) congruent to x mod n.
  409. */
  410. internal_mul_low(x + len, mninv, tmp, len, tmp + 3*len);
  411. /*
  412. * Compute t = (mn+x)/r in ordinary, non-modular, integer
  413. * arithmetic. By construction this is exact, and is congruent mod
  414. * n to x * r^{-1}, i.e. the answer we want.
  415. *
  416. * The following multiply leaves that answer in the _most_
  417. * significant half of the 'x' array, so then we must shift it
  418. * down.
  419. */
  420. internal_mul(tmp, n, tmp+len, len, tmp + 3*len);
  421. carry = internal_add(x, tmp+len, x, 2*len);
  422. for (i = 0; i < len; i++)
  423. x[len + i] = x[i], x[i] = 0;
  424. /*
  425. * Reduce t mod n. This doesn't require a full-on division by n,
  426. * but merely a test and single optional subtraction, since we can
  427. * show that 0 <= t < 2n.
  428. *
  429. * Proof:
  430. * + we computed m mod r, so 0 <= m < r.
  431. * + so 0 <= mn < rn, obviously
  432. * + hence we only need 0 <= x < rn to guarantee that 0 <= mn+x < 2rn
  433. * + yielding 0 <= (mn+x)/r < 2n as required.
  434. */
  435. if (!carry) {
  436. for (i = 0; i < len; i++)
  437. if (x[len + i] != n[i])
  438. break;
  439. }
  440. if (carry || i >= len || x[len + i] > n[i])
  441. internal_sub(x+len, n, x+len, len);
  442. }
  443. static void internal_add_shifted(BignumInt *number,
  444. BignumInt n, int shift)
  445. {
  446. int word = 1 + (shift / BIGNUM_INT_BITS);
  447. int bshift = shift % BIGNUM_INT_BITS;
  448. BignumInt addendh, addendl;
  449. BignumCarry carry;
  450. addendl = n << bshift;
  451. addendh = (bshift == 0 ? 0 : n >> (BIGNUM_INT_BITS - bshift));
  452. assert(word <= number[0]);
  453. BignumADC(number[word], carry, number[word], addendl, 0);
  454. word++;
  455. if (!addendh && !carry)
  456. return;
  457. assert(word <= number[0]);
  458. BignumADC(number[word], carry, number[word], addendh, carry);
  459. word++;
  460. while (carry) {
  461. assert(word <= number[0]);
  462. BignumADC(number[word], carry, number[word], 0, carry);
  463. word++;
  464. }
  465. }
  466. static int bn_clz(BignumInt x)
  467. {
  468. /*
  469. * Count the leading zero bits in x. Equivalently, how far left
  470. * would we need to shift x to make its top bit set?
  471. *
  472. * Precondition: x != 0.
  473. */
  474. /* FIXME: would be nice to put in some compiler intrinsics under
  475. * ifdef here */
  476. int i, ret = 0;
  477. for (i = BIGNUM_INT_BITS / 2; i != 0; i >>= 1) {
  478. if ((x >> (BIGNUM_INT_BITS-i)) == 0) {
  479. x <<= i;
  480. ret += i;
  481. }
  482. }
  483. return ret;
  484. }
  485. static BignumInt reciprocal_word(BignumInt d)
  486. {
  487. BignumInt dshort, recip, prodh, prodl;
  488. int corrections;
  489. /*
  490. * Input: a BignumInt value d, with its top bit set.
  491. */
  492. assert(d >> (BIGNUM_INT_BITS-1) == 1);
  493. /*
  494. * Output: a value, shifted to fill a BignumInt, which is strictly
  495. * less than 1/(d+1), i.e. is an *under*-estimate (but by as
  496. * little as possible within the constraints) of the reciprocal of
  497. * any number whose first BIGNUM_INT_BITS bits match d.
  498. *
  499. * Ideally we'd like to _totally_ fill BignumInt, i.e. always
  500. * return a value with the top bit set. Unfortunately we can't
  501. * quite guarantee that for all inputs and also return a fixed
  502. * exponent. So instead we take our reciprocal to be
  503. * 2^(BIGNUM_INT_BITS*2-1) / d, so that it has the top bit clear
  504. * only in the exceptional case where d takes exactly the maximum
  505. * value BIGNUM_INT_MASK; in that case, the top bit is clear and
  506. * the next bit down is set.
  507. */
  508. /*
  509. * Start by computing a half-length version of the answer, by
  510. * straightforward division within a BignumInt.
  511. */
  512. dshort = (d >> (BIGNUM_INT_BITS/2)) + 1;
  513. recip = (BIGNUM_TOP_BIT + dshort - 1) / dshort;
  514. recip <<= BIGNUM_INT_BITS - BIGNUM_INT_BITS/2;
  515. /*
  516. * Newton-Raphson iteration to improve that starting reciprocal
  517. * estimate: take f(x) = d - 1/x, and then the N-R formula gives
  518. * x_new = x - f(x)/f'(x) = x - (d-1/x)/(1/x^2) = x(2-d*x). Or,
  519. * taking our fixed-point representation into account, take f(x)
  520. * to be d - K/x (where K = 2^(BIGNUM_INT_BITS*2-1) as discussed
  521. * above) and then we get (2K - d*x) * x/K.
  522. *
  523. * Newton-Raphson doubles the number of correct bits at every
  524. * iteration, and the initial division above already gave us half
  525. * the output word, so it's only worth doing one iteration.
  526. */
  527. BignumMULADD(prodh, prodl, recip, d, recip);
  528. prodl = ~prodl;
  529. prodh = ~prodh;
  530. {
  531. BignumCarry c;
  532. BignumADC(prodl, c, prodl, 1, 0);
  533. prodh += c;
  534. }
  535. BignumMUL(prodh, prodl, prodh, recip);
  536. recip = (prodh << 1) | (prodl >> (BIGNUM_INT_BITS-1));
  537. /*
  538. * Now make sure we have the best possible reciprocal estimate,
  539. * before we return it. We might have been off by a handful either
  540. * way - not enough to bother with any better-thought-out kind of
  541. * correction loop.
  542. */
  543. BignumMULADD(prodh, prodl, recip, d, recip);
  544. corrections = 0;
  545. if (prodh >= BIGNUM_TOP_BIT) {
  546. do {
  547. BignumCarry c = 1;
  548. BignumADC(prodl, c, prodl, ~d, c); prodh += BIGNUM_INT_MASK + c;
  549. recip--;
  550. corrections++;
  551. } while (prodh >= ((BignumInt)1 << (BIGNUM_INT_BITS-1)));
  552. } else {
  553. while (1) {
  554. BignumInt newprodh, newprodl;
  555. BignumCarry c = 0;
  556. BignumADC(newprodl, c, prodl, d, c); newprodh = prodh + c;
  557. if (newprodh >= BIGNUM_TOP_BIT)
  558. break;
  559. prodh = newprodh;
  560. prodl = newprodl;
  561. recip++;
  562. corrections++;
  563. }
  564. }
  565. return recip;
  566. }
  567. /*
  568. * Compute a = a % m.
  569. * Input in first alen words of a and first mlen words of m.
  570. * Output in first alen words of a
  571. * (of which first alen-mlen words will be zero).
  572. * Quotient is accumulated in the `quotient' array, which is a Bignum
  573. * rather than the internal bigendian format.
  574. *
  575. * 'recip' must be the result of calling reciprocal_word() on the top
  576. * BIGNUM_INT_BITS of the modulus (denoted m0 in comments below), with
  577. * the topmost set bit normalised to the MSB of the input to
  578. * reciprocal_word. 'rshift' is how far left the top nonzero word of
  579. * the modulus had to be shifted to set that top bit.
  580. */
  581. static void internal_mod(BignumInt *a, int alen,
  582. BignumInt *m, int mlen,
  583. BignumInt *quot, BignumInt recip, int rshift)
  584. {
  585. int i, k;
  586. #ifdef DIVISION_DEBUG
  587. {
  588. int d;
  589. printf("start division, m=0x");
  590. for (d = 0; d < mlen; d++)
  591. printf("%0*llx", BIGNUM_INT_BITS/4, (unsigned long long)m[d]);
  592. printf(", recip=%#0*llx, rshift=%d\n",
  593. BIGNUM_INT_BITS/4, (unsigned long long)recip, rshift);
  594. }
  595. #endif
  596. /*
  597. * Repeatedly use that reciprocal estimate to get a decent number
  598. * of quotient bits, and subtract off the resulting multiple of m.
  599. *
  600. * Normally we expect to terminate this loop by means of finding
  601. * out q=0 part way through, but one way in which we might not get
  602. * that far in the first place is if the input a is actually zero,
  603. * in which case we'll discard zero words from the front of a
  604. * until we reach the termination condition in the for statement
  605. * here.
  606. */
  607. for (i = 0; i <= alen - mlen ;) {
  608. BignumInt product;
  609. BignumInt aword, q;
  610. int shift, full_bitoffset, bitoffset, wordoffset;
  611. #ifdef DIVISION_DEBUG
  612. {
  613. int d;
  614. printf("main loop, a=0x");
  615. for (d = 0; d < alen; d++)
  616. printf("%0*llx", BIGNUM_INT_BITS/4, (unsigned long long)a[d]);
  617. printf("\n");
  618. }
  619. #endif
  620. if (a[i] == 0) {
  621. #ifdef DIVISION_DEBUG
  622. printf("zero word at i=%d\n", i);
  623. #endif
  624. i++;
  625. continue;
  626. }
  627. aword = a[i];
  628. shift = bn_clz(aword);
  629. aword <<= shift;
  630. if (shift > 0 && i+1 < alen)
  631. aword |= a[i+1] >> (BIGNUM_INT_BITS - shift);
  632. {
  633. BignumInt unused;
  634. BignumMUL(q, unused, recip, aword);
  635. (void)unused;
  636. }
  637. #ifdef DIVISION_DEBUG
  638. printf("i=%d, aword=%#0*llx, shift=%d, q=%#0*llx\n",
  639. i, BIGNUM_INT_BITS/4, (unsigned long long)aword,
  640. shift, BIGNUM_INT_BITS/4, (unsigned long long)q);
  641. #endif
  642. /*
  643. * Work out the right bit and word offsets to use when
  644. * subtracting q*m from a.
  645. *
  646. * aword was taken from a[i], which means its LSB was at bit
  647. * position (alen-1-i) * BIGNUM_INT_BITS. But then we shifted
  648. * it left by 'shift', so now the low bit of aword corresponds
  649. * to bit position (alen-1-i) * BIGNUM_INT_BITS - shift, i.e.
  650. * aword is approximately equal to a / 2^(that).
  651. *
  652. * m0 comes from the top word of mod, so its LSB is at bit
  653. * position (mlen-1) * BIGNUM_INT_BITS - rshift, i.e. it can
  654. * be considered to be m / 2^(that power). 'recip' is the
  655. * reciprocal of m0, times 2^(BIGNUM_INT_BITS*2-1), i.e. it's
  656. * about 2^((mlen+1) * BIGNUM_INT_BITS - rshift - 1) / m.
  657. *
  658. * Hence, recip * aword is approximately equal to the product
  659. * of those, which simplifies to
  660. *
  661. * a/m * 2^((mlen+2+i-alen)*BIGNUM_INT_BITS + shift - rshift - 1)
  662. *
  663. * But we've also shifted recip*aword down by BIGNUM_INT_BITS
  664. * to form q, so we have
  665. *
  666. * q ~= a/m * 2^((mlen+1+i-alen)*BIGNUM_INT_BITS + shift - rshift - 1)
  667. *
  668. * and hence, when we now compute q*m, it will be about
  669. * a*2^(all that lot), i.e. the negation of that expression is
  670. * how far left we have to shift the product q*m to make it
  671. * approximately equal to a.
  672. */
  673. full_bitoffset = -((mlen+1+i-alen)*BIGNUM_INT_BITS + shift-rshift-1);
  674. #ifdef DIVISION_DEBUG
  675. printf("full_bitoffset=%d\n", full_bitoffset);
  676. #endif
  677. if (full_bitoffset < 0) {
  678. /*
  679. * If we find ourselves needing to shift q*m _right_, that
  680. * means we've reached the bottom of the quotient. Clip q
  681. * so that its right shift becomes zero, and if that means
  682. * q becomes _actually_ zero, this loop is done.
  683. */
  684. if (full_bitoffset <= -BIGNUM_INT_BITS)
  685. break;
  686. q >>= -full_bitoffset;
  687. full_bitoffset = 0;
  688. if (!q)
  689. break;
  690. #ifdef DIVISION_DEBUG
  691. printf("now full_bitoffset=%d, q=%#0*llx\n",
  692. full_bitoffset, BIGNUM_INT_BITS/4, (unsigned long long)q);
  693. #endif
  694. }
  695. wordoffset = full_bitoffset / BIGNUM_INT_BITS;
  696. bitoffset = full_bitoffset % BIGNUM_INT_BITS;
  697. #ifdef DIVISION_DEBUG
  698. printf("wordoffset=%d, bitoffset=%d\n", wordoffset, bitoffset);
  699. #endif
  700. /* wordoffset as computed above is the offset between the LSWs
  701. * of m and a. But in fact m and a are stored MSW-first, so we
  702. * need to adjust it to be the offset between the actual array
  703. * indices, and flip the sign too. */
  704. wordoffset = alen - mlen - wordoffset;
  705. if (bitoffset == 0) {
  706. BignumCarry c = 1;
  707. BignumInt prev_hi_word = 0;
  708. for (k = mlen - 1; wordoffset+k >= i; k--) {
  709. BignumInt mword = k<0 ? 0 : m[k];
  710. BignumMULADD(prev_hi_word, product, q, mword, prev_hi_word);
  711. #ifdef DIVISION_DEBUG
  712. printf(" aligned sub: product word for m[%d] = %#0*llx\n",
  713. k, BIGNUM_INT_BITS/4,
  714. (unsigned long long)product);
  715. #endif
  716. #ifdef DIVISION_DEBUG
  717. printf(" aligned sub: subtrahend for a[%d] = %#0*llx\n",
  718. wordoffset+k, BIGNUM_INT_BITS/4,
  719. (unsigned long long)product);
  720. #endif
  721. BignumADC(a[wordoffset+k], c, a[wordoffset+k], ~product, c);
  722. }
  723. } else {
  724. BignumInt add_word = 0;
  725. BignumInt c = 1;
  726. BignumInt prev_hi_word = 0;
  727. for (k = mlen - 1; wordoffset+k >= i; k--) {
  728. BignumInt mword = k<0 ? 0 : m[k];
  729. BignumMULADD(prev_hi_word, product, q, mword, prev_hi_word);
  730. #ifdef DIVISION_DEBUG
  731. printf(" unaligned sub: product word for m[%d] = %#0*llx\n",
  732. k, BIGNUM_INT_BITS/4,
  733. (unsigned long long)product);
  734. #endif
  735. add_word |= product << bitoffset;
  736. #ifdef DIVISION_DEBUG
  737. printf(" unaligned sub: subtrahend for a[%d] = %#0*llx\n",
  738. wordoffset+k,
  739. BIGNUM_INT_BITS/4, (unsigned long long)add_word);
  740. #endif
  741. BignumADC(a[wordoffset+k], c, a[wordoffset+k], ~add_word, c);
  742. add_word = product >> (BIGNUM_INT_BITS - bitoffset);
  743. }
  744. }
  745. if (quot) {
  746. #ifdef DIVISION_DEBUG
  747. printf("adding quotient word %#0*llx << %d\n",
  748. BIGNUM_INT_BITS/4, (unsigned long long)q, full_bitoffset);
  749. #endif
  750. internal_add_shifted(quot, q, full_bitoffset);
  751. #ifdef DIVISION_DEBUG
  752. {
  753. int d;
  754. printf("now quot=0x");
  755. for (d = quot[0]; d > 0; d--)
  756. printf("%0*llx", BIGNUM_INT_BITS/4,
  757. (unsigned long long)quot[d]);
  758. printf("\n");
  759. }
  760. #endif
  761. }
  762. }
  763. #ifdef DIVISION_DEBUG
  764. {
  765. int d;
  766. printf("end main loop, a=0x");
  767. for (d = 0; d < alen; d++)
  768. printf("%0*llx", BIGNUM_INT_BITS/4, (unsigned long long)a[d]);
  769. if (quot) {
  770. printf(", quot=0x");
  771. for (d = quot[0]; d > 0; d--)
  772. printf("%0*llx", BIGNUM_INT_BITS/4,
  773. (unsigned long long)quot[d]);
  774. }
  775. printf("\n");
  776. }
  777. #endif
  778. /*
  779. * The above loop should terminate with the remaining value in a
  780. * being strictly less than 2*m (if a >= 2*m then we should always
  781. * have managed to get a nonzero q word), but we can't guarantee
  782. * that it will be strictly less than m: consider a case where the
  783. * remainder is 1, and another where the remainder is m-1. By the
  784. * time a contains a value that's _about m_, you clearly can't
  785. * distinguish those cases by looking at only the top word of a -
  786. * you have to go all the way down to the bottom before you find
  787. * out whether it's just less or just more than m.
  788. *
  789. * Hence, we now do a final fixup in which we subtract one last
  790. * copy of m, or don't, accordingly. We should never have to
  791. * subtract more than one copy of m here.
  792. */
  793. for (i = 0; i < alen; i++) {
  794. /* Compare a with m, word by word, from the MSW down. As soon
  795. * as we encounter a difference, we know whether we need the
  796. * fixup. */
  797. int mindex = mlen-alen+i;
  798. BignumInt mword = mindex < 0 ? 0 : m[mindex];
  799. if (a[i] < mword) {
  800. #ifdef DIVISION_DEBUG
  801. printf("final fixup not needed, a < m\n");
  802. #endif
  803. return;
  804. } else if (a[i] > mword) {
  805. #ifdef DIVISION_DEBUG
  806. printf("final fixup is needed, a > m\n");
  807. #endif
  808. break;
  809. }
  810. /* If neither of those cases happened, the words are the same,
  811. * so keep going and look at the next one. */
  812. }
  813. #ifdef DIVISION_DEBUG
  814. if (i == mlen) /* if we printed neither of the above diagnostics */
  815. printf("final fixup is needed, a == m\n");
  816. #endif
  817. /*
  818. * If we got here without returning, then a >= m, so we must
  819. * subtract m, and increment the quotient.
  820. */
  821. {
  822. BignumCarry c = 1;
  823. for (i = alen - 1; i >= 0; i--) {
  824. int mindex = mlen-alen+i;
  825. BignumInt mword = mindex < 0 ? 0 : m[mindex];
  826. BignumADC(a[i], c, a[i], ~mword, c);
  827. }
  828. }
  829. if (quot)
  830. internal_add_shifted(quot, 1, 0);
  831. #ifdef DIVISION_DEBUG
  832. {
  833. int d;
  834. printf("after final fixup, a=0x");
  835. for (d = 0; d < alen; d++)
  836. printf("%0*llx", BIGNUM_INT_BITS/4, (unsigned long long)a[d]);
  837. if (quot) {
  838. printf(", quot=0x");
  839. for (d = quot[0]; d > 0; d--)
  840. printf("%0*llx", BIGNUM_INT_BITS/4,
  841. (unsigned long long)quot[d]);
  842. }
  843. printf("\n");
  844. }
  845. #endif
  846. }
  847. /*
  848. * Compute (base ^ exp) % mod, the pedestrian way.
  849. */
  850. Bignum modpow_simple(Bignum base_in, Bignum exp, Bignum mod)
  851. {
  852. BignumInt *a, *b, *n, *m, *scratch;
  853. BignumInt recip;
  854. int rshift;
  855. int mlen, scratchlen, i, j;
  856. Bignum base, result;
  857. /*
  858. * The most significant word of mod needs to be non-zero. It
  859. * should already be, but let's make sure.
  860. */
  861. assert(mod[mod[0]] != 0);
  862. /*
  863. * Make sure the base is smaller than the modulus, by reducing
  864. * it modulo the modulus if not.
  865. */
  866. base = bigmod(base_in, mod);
  867. /* Allocate m of size mlen, copy mod to m */
  868. /* We use big endian internally */
  869. mlen = mod[0];
  870. m = snewn(mlen, BignumInt);
  871. for (j = 0; j < mlen; j++)
  872. m[j] = mod[mod[0] - j];
  873. /* Allocate n of size mlen, copy base to n */
  874. n = snewn(mlen, BignumInt);
  875. i = mlen - base[0];
  876. for (j = 0; j < i; j++)
  877. n[j] = 0;
  878. for (j = 0; j < (int)base[0]; j++)
  879. n[i + j] = base[base[0] - j];
  880. /* Allocate a and b of size 2*mlen. Set a = 1 */
  881. a = snewn(2 * mlen, BignumInt);
  882. b = snewn(2 * mlen, BignumInt);
  883. for (i = 0; i < 2 * mlen; i++)
  884. a[i] = 0;
  885. a[2 * mlen - 1] = 1;
  886. /* Scratch space for multiplies */
  887. scratchlen = mul_compute_scratch(mlen);
  888. scratch = snewn(scratchlen, BignumInt);
  889. /* Skip leading zero bits of exp. */
  890. i = 0;
  891. j = BIGNUM_INT_BITS-1;
  892. while (i < (int)exp[0] && (exp[exp[0] - i] & ((BignumInt)1 << j)) == 0) {
  893. j--;
  894. if (j < 0) {
  895. i++;
  896. j = BIGNUM_INT_BITS-1;
  897. }
  898. }
  899. /* Compute reciprocal of the top full word of the modulus */
  900. {
  901. BignumInt m0 = m[0];
  902. rshift = bn_clz(m0);
  903. if (rshift) {
  904. m0 <<= rshift;
  905. if (mlen > 1)
  906. m0 |= m[1] >> (BIGNUM_INT_BITS - rshift);
  907. }
  908. recip = reciprocal_word(m0);
  909. }
  910. /* Main computation */
  911. while (i < (int)exp[0]) {
  912. while (j >= 0) {
  913. internal_mul(a + mlen, a + mlen, b, mlen, scratch);
  914. internal_mod(b, mlen * 2, m, mlen, NULL, recip, rshift);
  915. if ((exp[exp[0] - i] & ((BignumInt)1 << j)) != 0) {
  916. internal_mul(b + mlen, n, a, mlen, scratch);
  917. internal_mod(a, mlen * 2, m, mlen, NULL, recip, rshift);
  918. } else {
  919. BignumInt *t;
  920. t = a;
  921. a = b;
  922. b = t;
  923. }
  924. j--;
  925. }
  926. i++;
  927. j = BIGNUM_INT_BITS-1;
  928. }
  929. /* Copy result to buffer */
  930. result = newbn(mod[0]);
  931. for (i = 0; i < mlen; i++)
  932. result[result[0] - i] = a[i + mlen];
  933. while (result[0] > 1 && result[result[0]] == 0)
  934. result[0]--;
  935. /* Free temporary arrays */
  936. smemclr(a, 2 * mlen * sizeof(*a));
  937. sfree(a);
  938. smemclr(scratch, scratchlen * sizeof(*scratch));
  939. sfree(scratch);
  940. smemclr(b, 2 * mlen * sizeof(*b));
  941. sfree(b);
  942. smemclr(m, mlen * sizeof(*m));
  943. sfree(m);
  944. smemclr(n, mlen * sizeof(*n));
  945. sfree(n);
  946. freebn(base);
  947. return result;
  948. }
  949. /*
  950. * Compute (base ^ exp) % mod. Uses the Montgomery multiplication
  951. * technique where possible, falling back to modpow_simple otherwise.
  952. */
  953. Bignum modpow(Bignum base_in, Bignum exp, Bignum mod)
  954. {
  955. BignumInt *a, *b, *x, *n, *mninv, *scratch;
  956. int len, scratchlen, i, j;
  957. Bignum base, base2, r, rn, inv, result;
  958. /*
  959. * The most significant word of mod needs to be non-zero. It
  960. * should already be, but let's make sure.
  961. */
  962. assert(mod[mod[0]] != 0);
  963. /*
  964. * mod had better be odd, or we can't do Montgomery multiplication
  965. * using a power of two at all.
  966. */
  967. if (!(mod[1] & 1))
  968. return modpow_simple(base_in, exp, mod);
  969. /*
  970. * Make sure the base is smaller than the modulus, by reducing
  971. * it modulo the modulus if not.
  972. */
  973. base = bigmod(base_in, mod);
  974. /*
  975. * Compute the inverse of n mod r, for monty_reduce. (In fact we
  976. * want the inverse of _minus_ n mod r, but we'll sort that out
  977. * below.)
  978. */
  979. len = mod[0];
  980. r = bn_power_2(BIGNUM_INT_BITS * len);
  981. inv = modinv(mod, r);
  982. assert(inv); /* cannot fail, since mod is odd and r is a power of 2 */
  983. /*
  984. * Multiply the base by r mod n, to get it into Montgomery
  985. * representation.
  986. */
  987. base2 = modmul(base, r, mod);
  988. freebn(base);
  989. base = base2;
  990. rn = bigmod(r, mod); /* r mod n, i.e. Montgomerified 1 */
  991. freebn(r); /* won't need this any more */
  992. /*
  993. * Set up internal arrays of the right lengths, in big-endian
  994. * format, containing the base, the modulus, and the modulus's
  995. * inverse.
  996. */
  997. n = snewn(len, BignumInt);
  998. for (j = 0; j < len; j++)
  999. n[len - 1 - j] = mod[j + 1];
  1000. mninv = snewn(len, BignumInt);
  1001. for (j = 0; j < len; j++)
  1002. mninv[len - 1 - j] = (j < (int)inv[0] ? inv[j + 1] : 0);
  1003. freebn(inv); /* we don't need this copy of it any more */
  1004. /* Now negate mninv mod r, so it's the inverse of -n rather than +n. */
  1005. x = snewn(len, BignumInt);
  1006. for (j = 0; j < len; j++)
  1007. x[j] = 0;
  1008. internal_sub(x, mninv, mninv, len);
  1009. /* x = snewn(len, BignumInt); */ /* already done above */
  1010. for (j = 0; j < len; j++)
  1011. x[len - 1 - j] = (j < (int)base[0] ? base[j + 1] : 0);
  1012. freebn(base); /* we don't need this copy of it any more */
  1013. a = snewn(2*len, BignumInt);
  1014. b = snewn(2*len, BignumInt);
  1015. for (j = 0; j < len; j++)
  1016. a[2*len - 1 - j] = (j < (int)rn[0] ? rn[j + 1] : 0);
  1017. freebn(rn);
  1018. /* Scratch space for multiplies */
  1019. scratchlen = 3*len + mul_compute_scratch(len);
  1020. scratch = snewn(scratchlen, BignumInt);
  1021. /* Skip leading zero bits of exp. */
  1022. i = 0;
  1023. j = BIGNUM_INT_BITS-1;
  1024. while (i < (int)exp[0] && (exp[exp[0] - i] & ((BignumInt)1 << j)) == 0) {
  1025. j--;
  1026. if (j < 0) {
  1027. i++;
  1028. j = BIGNUM_INT_BITS-1;
  1029. }
  1030. }
  1031. /* Main computation */
  1032. while (i < (int)exp[0]) {
  1033. while (j >= 0) {
  1034. internal_mul(a + len, a + len, b, len, scratch);
  1035. monty_reduce(b, n, mninv, scratch, len);
  1036. if ((exp[exp[0] - i] & ((BignumInt)1 << j)) != 0) {
  1037. internal_mul(b + len, x, a, len, scratch);
  1038. monty_reduce(a, n, mninv, scratch, len);
  1039. } else {
  1040. BignumInt *t;
  1041. t = a;
  1042. a = b;
  1043. b = t;
  1044. }
  1045. j--;
  1046. }
  1047. i++;
  1048. j = BIGNUM_INT_BITS-1;
  1049. }
  1050. /*
  1051. * Final monty_reduce to get back from the adjusted Montgomery
  1052. * representation.
  1053. */
  1054. monty_reduce(a, n, mninv, scratch, len);
  1055. /* Copy result to buffer */
  1056. result = newbn(mod[0]);
  1057. for (i = 0; i < len; i++)
  1058. result[result[0] - i] = a[i + len];
  1059. while (result[0] > 1 && result[result[0]] == 0)
  1060. result[0]--;
  1061. /* Free temporary arrays */
  1062. smemclr(scratch, scratchlen * sizeof(*scratch));
  1063. sfree(scratch);
  1064. smemclr(a, 2 * len * sizeof(*a));
  1065. sfree(a);
  1066. smemclr(b, 2 * len * sizeof(*b));
  1067. sfree(b);
  1068. smemclr(mninv, len * sizeof(*mninv));
  1069. sfree(mninv);
  1070. smemclr(n, len * sizeof(*n));
  1071. sfree(n);
  1072. smemclr(x, len * sizeof(*x));
  1073. sfree(x);
  1074. return result;
  1075. }
  1076. /*
  1077. * Compute (p * q) % mod.
  1078. * The most significant word of mod MUST be non-zero.
  1079. * We assume that the result array is the same size as the mod array.
  1080. */
  1081. Bignum modmul(Bignum p, Bignum q, Bignum mod)
  1082. {
  1083. BignumInt *a, *n, *m, *o, *scratch;
  1084. BignumInt recip;
  1085. int rshift, scratchlen;
  1086. int pqlen, mlen, rlen, i, j;
  1087. Bignum result;
  1088. /*
  1089. * The most significant word of mod needs to be non-zero. It
  1090. * should already be, but let's make sure.
  1091. */
  1092. assert(mod[mod[0]] != 0);
  1093. /* Allocate m of size mlen, copy mod to m */
  1094. /* We use big endian internally */
  1095. mlen = mod[0];
  1096. m = snewn(mlen, BignumInt);
  1097. for (j = 0; j < mlen; j++)
  1098. m[j] = mod[mod[0] - j];
  1099. pqlen = (p[0] > q[0] ? p[0] : q[0]);
  1100. /*
  1101. * Make sure that we're allowing enough space. The shifting below
  1102. * will underflow the vectors we allocate if pqlen is too small.
  1103. */
  1104. if (2*pqlen <= mlen)
  1105. pqlen = mlen/2 + 1;
  1106. /* Allocate n of size pqlen, copy p to n */
  1107. n = snewn(pqlen, BignumInt);
  1108. i = pqlen - p[0];
  1109. for (j = 0; j < i; j++)
  1110. n[j] = 0;
  1111. for (j = 0; j < (int)p[0]; j++)
  1112. n[i + j] = p[p[0] - j];
  1113. /* Allocate o of size pqlen, copy q to o */
  1114. o = snewn(pqlen, BignumInt);
  1115. i = pqlen - q[0];
  1116. for (j = 0; j < i; j++)
  1117. o[j] = 0;
  1118. for (j = 0; j < (int)q[0]; j++)
  1119. o[i + j] = q[q[0] - j];
  1120. /* Allocate a of size 2*pqlen for result */
  1121. a = snewn(2 * pqlen, BignumInt);
  1122. /* Scratch space for multiplies */
  1123. scratchlen = mul_compute_scratch(pqlen);
  1124. scratch = snewn(scratchlen, BignumInt);
  1125. /* Compute reciprocal of the top full word of the modulus */
  1126. {
  1127. BignumInt m0 = m[0];
  1128. rshift = bn_clz(m0);
  1129. if (rshift) {
  1130. m0 <<= rshift;
  1131. if (mlen > 1)
  1132. m0 |= m[1] >> (BIGNUM_INT_BITS - rshift);
  1133. }
  1134. recip = reciprocal_word(m0);
  1135. }
  1136. /* Main computation */
  1137. internal_mul(n, o, a, pqlen, scratch);
  1138. internal_mod(a, pqlen * 2, m, mlen, NULL, recip, rshift);
  1139. /* Copy result to buffer */
  1140. rlen = (mlen < pqlen * 2 ? mlen : pqlen * 2);
  1141. result = newbn(rlen);
  1142. for (i = 0; i < rlen; i++)
  1143. result[result[0] - i] = a[i + 2 * pqlen - rlen];
  1144. while (result[0] > 1 && result[result[0]] == 0)
  1145. result[0]--;
  1146. /* Free temporary arrays */
  1147. smemclr(scratch, scratchlen * sizeof(*scratch));
  1148. sfree(scratch);
  1149. smemclr(a, 2 * pqlen * sizeof(*a));
  1150. sfree(a);
  1151. smemclr(m, mlen * sizeof(*m));
  1152. sfree(m);
  1153. smemclr(n, pqlen * sizeof(*n));
  1154. sfree(n);
  1155. smemclr(o, pqlen * sizeof(*o));
  1156. sfree(o);
  1157. return result;
  1158. }
  1159. Bignum modsub(const Bignum a, const Bignum b, const Bignum n)
  1160. {
  1161. Bignum a1, b1, ret;
  1162. if (bignum_cmp(a, n) >= 0) a1 = bigmod(a, n);
  1163. else a1 = a;
  1164. if (bignum_cmp(b, n) >= 0) b1 = bigmod(b, n);
  1165. else b1 = b;
  1166. if (bignum_cmp(a1, b1) >= 0) /* a >= b */
  1167. {
  1168. ret = bigsub(a1, b1);
  1169. }
  1170. else
  1171. {
  1172. /* Handle going round the corner of the modulus without having
  1173. * negative support in Bignum */
  1174. Bignum tmp = bigsub(n, b1);
  1175. assert(tmp);
  1176. ret = bigadd(tmp, a1);
  1177. freebn(tmp);
  1178. }
  1179. if (a != a1) freebn(a1);
  1180. if (b != b1) freebn(b1);
  1181. return ret;
  1182. }
  1183. /*
  1184. * Compute p % mod.
  1185. * The most significant word of mod MUST be non-zero.
  1186. * We assume that the result array is the same size as the mod array.
  1187. * We optionally write out a quotient if `quotient' is non-NULL.
  1188. * We can avoid writing out the result if `result' is NULL.
  1189. */
  1190. static void bigdivmod(Bignum p, Bignum mod, Bignum result, Bignum quotient)
  1191. {
  1192. BignumInt *n, *m;
  1193. BignumInt recip;
  1194. int rshift;
  1195. int plen, mlen, i, j;
  1196. /*
  1197. * The most significant word of mod needs to be non-zero. It
  1198. * should already be, but let's make sure.
  1199. */
  1200. assert(mod[mod[0]] != 0);
  1201. /* Allocate m of size mlen, copy mod to m */
  1202. /* We use big endian internally */
  1203. mlen = mod[0];
  1204. m = snewn(mlen, BignumInt);
  1205. for (j = 0; j < mlen; j++)
  1206. m[j] = mod[mod[0] - j];
  1207. plen = p[0];
  1208. /* Ensure plen > mlen */
  1209. if (plen <= mlen)
  1210. plen = mlen + 1;
  1211. /* Allocate n of size plen, copy p to n */
  1212. n = snewn(plen, BignumInt);
  1213. for (j = 0; j < plen; j++)
  1214. n[j] = 0;
  1215. for (j = 1; j <= (int)p[0]; j++)
  1216. n[plen - j] = p[j];
  1217. /* Compute reciprocal of the top full word of the modulus */
  1218. {
  1219. BignumInt m0 = m[0];
  1220. rshift = bn_clz(m0);
  1221. if (rshift) {
  1222. m0 <<= rshift;
  1223. if (mlen > 1)
  1224. m0 |= m[1] >> (BIGNUM_INT_BITS - rshift);
  1225. }
  1226. recip = reciprocal_word(m0);
  1227. }
  1228. /* Main computation */
  1229. internal_mod(n, plen, m, mlen, quotient, recip, rshift);
  1230. /* Copy result to buffer */
  1231. if (result) {
  1232. for (i = 1; i <= (int)result[0]; i++) {
  1233. int j = plen - i;
  1234. result[i] = j >= 0 ? n[j] : 0;
  1235. }
  1236. }
  1237. /* Free temporary arrays */
  1238. smemclr(m, mlen * sizeof(*m));
  1239. sfree(m);
  1240. smemclr(n, plen * sizeof(*n));
  1241. sfree(n);
  1242. }
  1243. /*
  1244. * Decrement a number.
  1245. */
  1246. void decbn(Bignum bn)
  1247. {
  1248. int i = 1;
  1249. while (i < (int)bn[0] && bn[i] == 0)
  1250. bn[i++] = BIGNUM_INT_MASK;
  1251. bn[i]--;
  1252. }
  1253. Bignum bignum_from_bytes(const void *vdata, int nbytes)
  1254. {
  1255. const unsigned char *data = (const unsigned char *)vdata;
  1256. Bignum result;
  1257. int w, i;
  1258. assert(nbytes >= 0 && nbytes < INT_MAX/8);
  1259. w = (nbytes + BIGNUM_INT_BYTES - 1) / BIGNUM_INT_BYTES; /* bytes->words */
  1260. result = newbn(w);
  1261. for (i = 1; i <= w; i++)
  1262. result[i] = 0;
  1263. for (i = nbytes; i--;) {
  1264. unsigned char byte = *data++;
  1265. result[1 + i / BIGNUM_INT_BYTES] |=
  1266. (BignumInt)byte << (8*i % BIGNUM_INT_BITS);
  1267. }
  1268. bn_restore_invariant(result);
  1269. return result;
  1270. }
  1271. Bignum bignum_from_bytes_le(const void *vdata, int nbytes)
  1272. {
  1273. const unsigned char *data = (const unsigned char *)vdata;
  1274. Bignum result;
  1275. int w, i;
  1276. assert(nbytes >= 0 && nbytes < INT_MAX/8);
  1277. w = (nbytes + BIGNUM_INT_BYTES - 1) / BIGNUM_INT_BYTES; /* bytes->words */
  1278. result = newbn(w);
  1279. for (i = 1; i <= w; i++)
  1280. result[i] = 0;
  1281. for (i = 0; i < nbytes; ++i) {
  1282. unsigned char byte = *data++;
  1283. result[1 + i / BIGNUM_INT_BYTES] |=
  1284. (BignumInt)byte << (8*i % BIGNUM_INT_BITS);
  1285. }
  1286. bn_restore_invariant(result);
  1287. return result;
  1288. }
  1289. Bignum bignum_from_decimal(const char *decimal)
  1290. {
  1291. Bignum result = copybn(Zero);
  1292. while (*decimal) {
  1293. Bignum tmp, tmp2;
  1294. if (!isdigit((unsigned char)*decimal)) {
  1295. freebn(result);
  1296. return 0;
  1297. }
  1298. tmp = bigmul(result, Ten);
  1299. tmp2 = bignum_from_long(*decimal - '0');
  1300. freebn(result);
  1301. result = bigadd(tmp, tmp2);
  1302. freebn(tmp);
  1303. freebn(tmp2);
  1304. decimal++;
  1305. }
  1306. return result;
  1307. }
  1308. Bignum bignum_random_in_range(const Bignum lower, const Bignum upper)
  1309. {
  1310. Bignum ret = NULL;
  1311. unsigned char *bytes;
  1312. int upper_len = bignum_bitcount(upper);
  1313. int upper_bytes = upper_len / 8;
  1314. int upper_bits = upper_len % 8;
  1315. if (upper_bits) ++upper_bytes;
  1316. bytes = snewn(upper_bytes, unsigned char);
  1317. do {
  1318. int i;
  1319. if (ret) freebn(ret);
  1320. for (i = 0; i < upper_bytes; ++i)
  1321. {
  1322. bytes[i] = (unsigned char)random_byte();
  1323. }
  1324. /* Mask the top to reduce failure rate to 50/50 */
  1325. if (upper_bits)
  1326. {
  1327. bytes[i - 1] &= 0xFF >> (8 - upper_bits);
  1328. }
  1329. ret = bignum_from_bytes(bytes, upper_bytes);
  1330. } while (bignum_cmp(ret, lower) < 0 || bignum_cmp(ret, upper) > 0);
  1331. smemclr(bytes, upper_bytes);
  1332. sfree(bytes);
  1333. return ret;
  1334. }
  1335. /*
  1336. * Return the bit count of a bignum.
  1337. */
  1338. int bignum_bitcount(Bignum bn)
  1339. {
  1340. int bitcount = bn[0] * BIGNUM_INT_BITS - 1;
  1341. while (bitcount >= 0
  1342. && (bn[bitcount / BIGNUM_INT_BITS + 1] >> (bitcount % BIGNUM_INT_BITS)) == 0) bitcount--;
  1343. return bitcount + 1;
  1344. }
  1345. /*
  1346. * Return a byte from a bignum; 0 is least significant, etc.
  1347. */
  1348. int bignum_byte(Bignum bn, int i)
  1349. {
  1350. if (i < 0 || i >= (int)(BIGNUM_INT_BYTES * bn[0]))
  1351. return 0; /* beyond the end */
  1352. else
  1353. return (bn[i / BIGNUM_INT_BYTES + 1] >>
  1354. ((i % BIGNUM_INT_BYTES)*8)) & 0xFF;
  1355. }
  1356. /*
  1357. * Return a bit from a bignum; 0 is least significant, etc.
  1358. */
  1359. int bignum_bit(Bignum bn, int i)
  1360. {
  1361. if (i < 0 || i >= (int)(BIGNUM_INT_BITS * bn[0]))
  1362. return 0; /* beyond the end */
  1363. else
  1364. return (bn[i / BIGNUM_INT_BITS + 1] >> (i % BIGNUM_INT_BITS)) & 1;
  1365. }
  1366. /*
  1367. * Set a bit in a bignum; 0 is least significant, etc.
  1368. */
  1369. void bignum_set_bit(Bignum bn, int bitnum, int value)
  1370. {
  1371. if (bitnum < 0 || bitnum >= (int)(BIGNUM_INT_BITS * bn[0])) {
  1372. if (value) abort(); /* beyond the end */
  1373. } else {
  1374. int v = bitnum / BIGNUM_INT_BITS + 1;
  1375. BignumInt mask = (BignumInt)1 << (bitnum % BIGNUM_INT_BITS);
  1376. if (value)
  1377. bn[v] |= mask;
  1378. else
  1379. bn[v] &= ~mask;
  1380. }
  1381. }
  1382. void BinarySink_put_mp_ssh1(BinarySink *bs, Bignum bn)
  1383. {
  1384. int bits = bignum_bitcount(bn);
  1385. int bytes = (bits + 7) / 8;
  1386. int i;
  1387. put_uint16(bs, bits);
  1388. for (i = bytes; i--;)
  1389. put_byte(bs, bignum_byte(bn, i));
  1390. }
  1391. void BinarySink_put_mp_ssh2(BinarySink *bs, Bignum bn)
  1392. {
  1393. int bytes = (bignum_bitcount(bn) + 8) / 8;
  1394. int i;
  1395. put_uint32(bs, bytes);
  1396. for (i = bytes; i--;)
  1397. put_byte(bs, bignum_byte(bn, i));
  1398. }
  1399. Bignum BinarySource_get_mp_ssh1(BinarySource *src)
  1400. {
  1401. unsigned bitc = get_uint16(src);
  1402. ptrlen bytes = get_data(src, (bitc + 7) / 8);
  1403. if (get_err(src)) {
  1404. return bignum_from_long(0);
  1405. } else {
  1406. Bignum toret = bignum_from_bytes(bytes.ptr, bytes.len);
  1407. if (bignum_bitcount(toret) != bitc) {
  1408. src->err = BSE_INVALID;
  1409. freebn(toret);
  1410. toret = bignum_from_long(0);
  1411. }
  1412. return toret;
  1413. }
  1414. }
  1415. Bignum BinarySource_get_mp_ssh2(BinarySource *src)
  1416. {
  1417. ptrlen bytes = get_string(src);
  1418. if (get_err(src)) {
  1419. return bignum_from_long(0);
  1420. } else {
  1421. const unsigned char *p = bytes.ptr;
  1422. if ((bytes.len > 0 &&
  1423. ((p[0] & 0x80) ||
  1424. (p[0] == 0 && (bytes.len <= 1 || !(p[1] & 0x80)))))) {
  1425. src->err = BSE_INVALID;
  1426. return bignum_from_long(0);
  1427. }
  1428. return bignum_from_bytes(bytes.ptr, bytes.len);
  1429. }
  1430. }
  1431. /*
  1432. * Compare two bignums. Returns like strcmp.
  1433. */
  1434. int bignum_cmp(Bignum a, Bignum b)
  1435. {
  1436. int amax = a[0], bmax = b[0];
  1437. int i;
  1438. /* Annoyingly we have two representations of zero */
  1439. if (amax == 1 && a[amax] == 0)
  1440. amax = 0;
  1441. if (bmax == 1 && b[bmax] == 0)
  1442. bmax = 0;
  1443. assert(amax == 0 || a[amax] != 0);
  1444. assert(bmax == 0 || b[bmax] != 0);
  1445. i = (amax > bmax ? amax : bmax);
  1446. while (i) {
  1447. BignumInt aval = (i > amax ? 0 : a[i]);
  1448. BignumInt bval = (i > bmax ? 0 : b[i]);
  1449. if (aval < bval)
  1450. return -1;
  1451. if (aval > bval)
  1452. return +1;
  1453. i--;
  1454. }
  1455. return 0;
  1456. }
  1457. /*
  1458. * Right-shift one bignum to form another.
  1459. */
  1460. Bignum bignum_rshift(Bignum a, int shift)
  1461. {
  1462. Bignum ret;
  1463. int i, shiftw, shiftb, shiftbb, bits;
  1464. BignumInt ai, ai1;
  1465. assert(shift >= 0);
  1466. bits = bignum_bitcount(a) - shift;
  1467. ret = newbn((bits + BIGNUM_INT_BITS - 1) / BIGNUM_INT_BITS);
  1468. if (ret) {
  1469. shiftw = shift / BIGNUM_INT_BITS;
  1470. shiftb = shift % BIGNUM_INT_BITS;
  1471. shiftbb = BIGNUM_INT_BITS - shiftb;
  1472. ai1 = a[shiftw + 1];
  1473. for (i = 1; i <= (int)ret[0]; i++) {
  1474. ai = ai1;
  1475. ai1 = (i + shiftw + 1 <= (int)a[0] ? a[i + shiftw + 1] : 0);
  1476. ret[i] = ((ai >> shiftb) | (ai1 << shiftbb)) & BIGNUM_INT_MASK;
  1477. }
  1478. }
  1479. return ret;
  1480. }
  1481. /*
  1482. * Left-shift one bignum to form another.
  1483. */
  1484. Bignum bignum_lshift(Bignum a, int shift)
  1485. {
  1486. Bignum ret;
  1487. int bits, shiftWords, shiftBits;
  1488. assert(shift >= 0);
  1489. bits = bignum_bitcount(a) + shift;
  1490. ret = newbn((bits + BIGNUM_INT_BITS - 1) / BIGNUM_INT_BITS);
  1491. shiftWords = shift / BIGNUM_INT_BITS;
  1492. shiftBits = shift % BIGNUM_INT_BITS;
  1493. if (shiftBits == 0)
  1494. {
  1495. memcpy(&ret[1 + shiftWords], &a[1], sizeof(BignumInt) * a[0]);
  1496. }
  1497. else
  1498. {
  1499. int i;
  1500. BignumInt carry = 0;
  1501. /* Remember that Bignum[0] is length, so add 1 */
  1502. for (i = shiftWords + 1; i < ((int)a[0]) + shiftWords + 1; ++i)
  1503. {
  1504. BignumInt from = a[i - shiftWords];
  1505. ret[i] = (from << shiftBits) | carry;
  1506. carry = from >> (BIGNUM_INT_BITS - shiftBits);
  1507. }
  1508. if (carry) ret[i] = carry;
  1509. }
  1510. return ret;
  1511. }
  1512. /*
  1513. * Non-modular multiplication and addition.
  1514. */
  1515. Bignum bigmuladd(Bignum a, Bignum b, Bignum addend)
  1516. {
  1517. int alen = a[0], blen = b[0];
  1518. int mlen = (alen > blen ? alen : blen);
  1519. int rlen, i, maxspot;
  1520. int wslen;
  1521. BignumInt *workspace;
  1522. Bignum ret;
  1523. /* mlen space for a, mlen space for b, 2*mlen for result,
  1524. * plus scratch space for multiplication */
  1525. wslen = mlen * 4 + mul_compute_scratch(mlen);
  1526. workspace = snewn(wslen, BignumInt);
  1527. for (i = 0; i < mlen; i++) {
  1528. workspace[0 * mlen + i] = (mlen - i <= (int)a[0] ? a[mlen - i] : 0);
  1529. workspace[1 * mlen + i] = (mlen - i <= (int)b[0] ? b[mlen - i] : 0);
  1530. }
  1531. internal_mul(workspace + 0 * mlen, workspace + 1 * mlen,
  1532. workspace + 2 * mlen, mlen, workspace + 4 * mlen);
  1533. /* now just copy the result back */
  1534. rlen = alen + blen + 1;
  1535. if (addend && rlen <= (int)addend[0])
  1536. rlen = addend[0] + 1;
  1537. ret = newbn(rlen);
  1538. maxspot = 0;
  1539. for (i = 1; i <= (int)ret[0]; i++) {
  1540. ret[i] = (i <= 2 * mlen ? workspace[4 * mlen - i] : 0);
  1541. if (ret[i] != 0)
  1542. maxspot = i;
  1543. }
  1544. ret[0] = maxspot;
  1545. /* now add in the addend, if any */
  1546. if (addend) {
  1547. BignumCarry carry = 0;
  1548. for (i = 1; i <= rlen; i++) {
  1549. BignumInt retword = (i <= (int)ret[0] ? ret[i] : 0);
  1550. BignumInt addword = (i <= (int)addend[0] ? addend[i] : 0);
  1551. BignumADC(ret[i], carry, retword, addword, carry);
  1552. if (ret[i] != 0 && i > maxspot)
  1553. maxspot = i;
  1554. }
  1555. }
  1556. ret[0] = maxspot;
  1557. smemclr(workspace, wslen * sizeof(*workspace));
  1558. sfree(workspace);
  1559. return ret;
  1560. }
  1561. /*
  1562. * Non-modular multiplication.
  1563. */
  1564. Bignum bigmul(Bignum a, Bignum b)
  1565. {
  1566. return bigmuladd(a, b, NULL);
  1567. }
  1568. /*
  1569. * Simple addition.
  1570. */
  1571. Bignum bigadd(Bignum a, Bignum b)
  1572. {
  1573. int alen = a[0], blen = b[0];
  1574. int rlen = (alen > blen ? alen : blen) + 1;
  1575. int i, maxspot;
  1576. Bignum ret;
  1577. BignumCarry carry;
  1578. ret = newbn(rlen);
  1579. carry = 0;
  1580. maxspot = 0;
  1581. for (i = 1; i <= rlen; i++) {
  1582. BignumInt aword = (i <= (int)a[0] ? a[i] : 0);
  1583. BignumInt bword = (i <= (int)b[0] ? b[i] : 0);
  1584. BignumADC(ret[i], carry, aword, bword, carry);
  1585. if (ret[i] != 0 && i > maxspot)
  1586. maxspot = i;
  1587. }
  1588. ret[0] = maxspot;
  1589. return ret;
  1590. }
  1591. /*
  1592. * Subtraction. Returns a-b, or NULL if the result would come out
  1593. * negative (recall that this entire bignum module only handles
  1594. * positive numbers).
  1595. */
  1596. Bignum bigsub(Bignum a, Bignum b)
  1597. {
  1598. int alen = a[0], blen = b[0];
  1599. int rlen = (alen > blen ? alen : blen);
  1600. int i, maxspot;
  1601. Bignum ret;
  1602. BignumCarry carry;
  1603. ret = newbn(rlen);
  1604. carry = 1;
  1605. maxspot = 0;
  1606. for (i = 1; i <= rlen; i++) {
  1607. BignumInt aword = (i <= (int)a[0] ? a[i] : 0);
  1608. BignumInt bword = (i <= (int)b[0] ? b[i] : 0);
  1609. BignumADC(ret[i], carry, aword, ~bword, carry);
  1610. if (ret[i] != 0 && i > maxspot)
  1611. maxspot = i;
  1612. }
  1613. ret[0] = maxspot;
  1614. if (!carry) {
  1615. freebn(ret);
  1616. return NULL;
  1617. }
  1618. return ret;
  1619. }
  1620. /*
  1621. * Create a bignum which is the bitmask covering another one. That
  1622. * is, the smallest integer which is >= N and is also one less than
  1623. * a power of two.
  1624. */
  1625. Bignum bignum_bitmask(Bignum n)
  1626. {
  1627. Bignum ret = copybn(n);
  1628. int i;
  1629. BignumInt j;
  1630. i = ret[0];
  1631. while (n[i] == 0 && i > 0)
  1632. i--;
  1633. if (i <= 0)
  1634. return ret; /* input was zero */
  1635. j = 1;
  1636. while (j < n[i])
  1637. j = 2 * j + 1;
  1638. ret[i] = j;
  1639. while (--i > 0)
  1640. ret[i] = BIGNUM_INT_MASK;
  1641. return ret;
  1642. }
  1643. /*
  1644. * Convert an unsigned long into a bignum.
  1645. */
  1646. Bignum bignum_from_long(unsigned long n)
  1647. {
  1648. const int maxwords =
  1649. (sizeof(unsigned long) + sizeof(BignumInt) - 1) / sizeof(BignumInt);
  1650. Bignum ret;
  1651. int i;
  1652. ret = newbn(maxwords);
  1653. ret[0] = 0;
  1654. for (i = 0; i < maxwords; i++) {
  1655. ret[i+1] = n >> (i * BIGNUM_INT_BITS);
  1656. if (ret[i+1] != 0)
  1657. ret[0] = i+1;
  1658. }
  1659. return ret;
  1660. }
  1661. /*
  1662. * Add a long to a bignum.
  1663. */
  1664. Bignum bignum_add_long(Bignum number, unsigned long n)
  1665. {
  1666. const int maxwords =
  1667. (sizeof(unsigned long) + sizeof(BignumInt) - 1) / sizeof(BignumInt);
  1668. Bignum ret;
  1669. int words, i;
  1670. BignumCarry carry;
  1671. words = number[0];
  1672. if (words < maxwords)
  1673. words = maxwords;
  1674. words++;
  1675. ret = newbn(words);
  1676. carry = 0;
  1677. ret[0] = 0;
  1678. for (i = 0; i < words; i++) {
  1679. BignumInt nword = (i < maxwords ? n >> (i * BIGNUM_INT_BITS) : 0);
  1680. BignumInt numword = (i < number[0] ? number[i+1] : 0);
  1681. BignumADC(ret[i+1], carry, numword, nword, carry);
  1682. if (ret[i+1] != 0)
  1683. ret[0] = i+1;
  1684. }
  1685. return ret;
  1686. }
  1687. /*
  1688. * Compute the residue of a bignum, modulo a (max 16-bit) short.
  1689. */
  1690. unsigned short bignum_mod_short(Bignum number, unsigned short modulus)
  1691. {
  1692. unsigned long mod = modulus, r = 0;
  1693. /* Precompute (BIGNUM_INT_MASK+1) % mod */
  1694. unsigned long base_r = (BIGNUM_INT_MASK - modulus + 1) % mod;
  1695. int i;
  1696. for (i = number[0]; i > 0; i--) {
  1697. /*
  1698. * Conceptually, ((r << BIGNUM_INT_BITS) + number[i]) % mod
  1699. */
  1700. r = ((r * base_r) + (number[i] % mod)) % mod;
  1701. }
  1702. return (unsigned short) r;
  1703. }
  1704. #ifdef DEBUG
  1705. void diagbn(char *prefix, Bignum md)
  1706. {
  1707. int i, nibbles, morenibbles;
  1708. static const char hex[] = "0123456789ABCDEF";
  1709. debug(("%s0x", prefix ? prefix : ""));
  1710. nibbles = (3 + bignum_bitcount(md)) / 4;
  1711. if (nibbles < 1)
  1712. nibbles = 1;
  1713. morenibbles = 4 * md[0] - nibbles;
  1714. for (i = 0; i < morenibbles; i++)
  1715. debug(("-"));
  1716. for (i = nibbles; i--;)
  1717. debug(("%c",
  1718. hex[(bignum_byte(md, i / 2) >> (4 * (i % 2))) & 0xF]));
  1719. if (prefix)
  1720. debug(("\n"));
  1721. }
  1722. #endif
  1723. /*
  1724. * Simple division.
  1725. */
  1726. Bignum bigdiv(Bignum a, Bignum b)
  1727. {
  1728. Bignum q = newbn(a[0]);
  1729. bigdivmod(a, b, NULL, q);
  1730. while (q[0] > 1 && q[q[0]] == 0)
  1731. q[0]--;
  1732. return q;
  1733. }
  1734. /*
  1735. * Simple remainder.
  1736. */
  1737. Bignum bigmod(Bignum a, Bignum b)
  1738. {
  1739. Bignum r = newbn(b[0]);
  1740. bigdivmod(a, b, r, NULL);
  1741. while (r[0] > 1 && r[r[0]] == 0)
  1742. r[0]--;
  1743. return r;
  1744. }
  1745. /*
  1746. * Greatest common divisor.
  1747. */
  1748. Bignum biggcd(Bignum av, Bignum bv)
  1749. {
  1750. Bignum a = copybn(av);
  1751. Bignum b = copybn(bv);
  1752. while (bignum_cmp(b, Zero) != 0) {
  1753. Bignum t = newbn(b[0]);
  1754. bigdivmod(a, b, t, NULL);
  1755. while (t[0] > 1 && t[t[0]] == 0)
  1756. t[0]--;
  1757. freebn(a);
  1758. a = b;
  1759. b = t;
  1760. }
  1761. freebn(b);
  1762. return a;
  1763. }
  1764. /*
  1765. * Modular inverse, using Euclid's extended algorithm.
  1766. */
  1767. Bignum modinv(Bignum number, Bignum modulus)
  1768. {
  1769. Bignum a = copybn(modulus);
  1770. Bignum b = copybn(number);
  1771. Bignum xp = copybn(Zero);
  1772. Bignum x = copybn(One);
  1773. int sign = +1;
  1774. assert(number[number[0]] != 0);
  1775. assert(modulus[modulus[0]] != 0);
  1776. while (bignum_cmp(b, One) != 0) {
  1777. Bignum t, q;
  1778. if (bignum_cmp(b, Zero) == 0) {
  1779. /*
  1780. * Found a common factor between the inputs, so we cannot
  1781. * return a modular inverse at all.
  1782. */
  1783. freebn(b);
  1784. freebn(a);
  1785. freebn(xp);
  1786. freebn(x);
  1787. return NULL;
  1788. }
  1789. t = newbn(b[0]);
  1790. q = newbn(a[0]);
  1791. bigdivmod(a, b, t, q);
  1792. while (t[0] > 1 && t[t[0]] == 0)
  1793. t[0]--;
  1794. while (q[0] > 1 && q[q[0]] == 0)
  1795. q[0]--;
  1796. freebn(a);
  1797. a = b;
  1798. b = t;
  1799. t = xp;
  1800. xp = x;
  1801. x = bigmuladd(q, xp, t);
  1802. sign = -sign;
  1803. freebn(t);
  1804. freebn(q);
  1805. }
  1806. freebn(b);
  1807. freebn(a);
  1808. freebn(xp);
  1809. /* now we know that sign * x == 1, and that x < modulus */
  1810. if (sign < 0) {
  1811. /* set a new x to be modulus - x */
  1812. Bignum newx = newbn(modulus[0]);
  1813. BignumInt carry = 0;
  1814. int maxspot = 1;
  1815. int i;
  1816. for (i = 1; i <= (int)newx[0]; i++) {
  1817. BignumInt aword = (i <= (int)modulus[0] ? modulus[i] : 0);
  1818. BignumInt bword = (i <= (int)x[0] ? x[i] : 0);
  1819. newx[i] = aword - bword - carry;
  1820. bword = ~bword;
  1821. carry = carry ? (newx[i] >= bword) : (newx[i] > bword);
  1822. if (newx[i] != 0)
  1823. maxspot = i;
  1824. }
  1825. newx[0] = maxspot;
  1826. freebn(x);
  1827. x = newx;
  1828. }
  1829. /* and return. */
  1830. return x;
  1831. }
  1832. /*
  1833. * Render a bignum into decimal. Return a malloced string holding
  1834. * the decimal representation.
  1835. */
  1836. char *bignum_decimal(Bignum x)
  1837. {
  1838. int ndigits, ndigit;
  1839. int i, iszero;
  1840. BignumInt carry;
  1841. char *ret;
  1842. BignumInt *workspace;
  1843. /*
  1844. * First, estimate the number of digits. Since log(10)/log(2)
  1845. * is just greater than 93/28 (the joys of continued fraction
  1846. * approximations...) we know that for every 93 bits, we need
  1847. * at most 28 digits. This will tell us how much to malloc.
  1848. *
  1849. * Formally: if x has i bits, that means x is strictly less
  1850. * than 2^i. Since 2 is less than 10^(28/93), this is less than
  1851. * 10^(28i/93). We need an integer power of ten, so we must
  1852. * round up (rounding down might make it less than x again).
  1853. * Therefore if we multiply the bit count by 28/93, rounding
  1854. * up, we will have enough digits.
  1855. *
  1856. * i=0 (i.e., x=0) is an irritating special case.
  1857. */
  1858. i = bignum_bitcount(x);
  1859. if (!i)
  1860. ndigits = 1; /* x = 0 */
  1861. else
  1862. ndigits = (28 * i + 92) / 93; /* multiply by 28/93 and round up */
  1863. ndigits++; /* allow for trailing \0 */
  1864. ret = snewn(ndigits, char);
  1865. /*
  1866. * Now allocate some workspace to hold the binary form as we
  1867. * repeatedly divide it by ten. Initialise this to the
  1868. * big-endian form of the number.
  1869. */
  1870. workspace = snewn(x[0], BignumInt);
  1871. for (i = 0; i < (int)x[0]; i++)
  1872. workspace[i] = x[x[0] - i];
  1873. /*
  1874. * Next, write the decimal number starting with the last digit.
  1875. * We use ordinary short division, dividing 10 into the
  1876. * workspace.
  1877. */
  1878. ndigit = ndigits - 1;
  1879. ret[ndigit] = '\0';
  1880. do {
  1881. iszero = 1;
  1882. carry = 0;
  1883. for (i = 0; i < (int)x[0]; i++) {
  1884. /*
  1885. * Conceptually, we want to compute
  1886. *
  1887. * (carry << BIGNUM_INT_BITS) + workspace[i]
  1888. * -----------------------------------------
  1889. * 10
  1890. *
  1891. * but we don't have an integer type longer than BignumInt
  1892. * to work with. So we have to do it in pieces.
  1893. */
  1894. BignumInt q, r;
  1895. q = workspace[i] / 10;
  1896. r = workspace[i] % 10;
  1897. /* I want (BIGNUM_INT_MASK+1)/10 but can't say so directly! */
  1898. q += carry * ((BIGNUM_INT_MASK-9) / 10 + 1);
  1899. r += carry * ((BIGNUM_INT_MASK-9) % 10);
  1900. q += r / 10;
  1901. r %= 10;
  1902. workspace[i] = q;
  1903. carry = r;
  1904. if (workspace[i])
  1905. iszero = 0;
  1906. }
  1907. ret[--ndigit] = (char) (carry + '0');
  1908. } while (!iszero);
  1909. /*
  1910. * There's a chance we've fallen short of the start of the
  1911. * string. Correct if so.
  1912. */
  1913. if (ndigit > 0)
  1914. memmove(ret, ret + ndigit, ndigits - ndigit);
  1915. /*
  1916. * Done.
  1917. */
  1918. smemclr(workspace, x[0] * sizeof(*workspace));
  1919. sfree(workspace);
  1920. return ret;
  1921. }