sshbn.c 59 KB

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