sshbn.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180
  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. /* SSH-1.5 spec says that it's OK for the prefix uint16 to be
  1408. * _greater_ than the actual number of bits */
  1409. if (bignum_bitcount(toret) > bitc) {
  1410. src->err = BSE_INVALID;
  1411. freebn(toret);
  1412. toret = bignum_from_long(0);
  1413. }
  1414. return toret;
  1415. }
  1416. }
  1417. Bignum BinarySource_get_mp_ssh2(BinarySource *src)
  1418. {
  1419. ptrlen bytes = get_string(src);
  1420. if (get_err(src)) {
  1421. return bignum_from_long(0);
  1422. } else {
  1423. const unsigned char *p = bytes.ptr;
  1424. if ((bytes.len > 0 &&
  1425. ((p[0] & 0x80) ||
  1426. (p[0] == 0 && (bytes.len <= 1 || !(p[1] & 0x80)))))) {
  1427. src->err = BSE_INVALID;
  1428. return bignum_from_long(0);
  1429. }
  1430. return bignum_from_bytes(bytes.ptr, bytes.len);
  1431. }
  1432. }
  1433. /*
  1434. * Compare two bignums. Returns like strcmp.
  1435. */
  1436. int bignum_cmp(Bignum a, Bignum b)
  1437. {
  1438. int amax = a[0], bmax = b[0];
  1439. int i;
  1440. /* Annoyingly we have two representations of zero */
  1441. if (amax == 1 && a[amax] == 0)
  1442. amax = 0;
  1443. if (bmax == 1 && b[bmax] == 0)
  1444. bmax = 0;
  1445. assert(amax == 0 || a[amax] != 0);
  1446. assert(bmax == 0 || b[bmax] != 0);
  1447. i = (amax > bmax ? amax : bmax);
  1448. while (i) {
  1449. BignumInt aval = (i > amax ? 0 : a[i]);
  1450. BignumInt bval = (i > bmax ? 0 : b[i]);
  1451. if (aval < bval)
  1452. return -1;
  1453. if (aval > bval)
  1454. return +1;
  1455. i--;
  1456. }
  1457. return 0;
  1458. }
  1459. /*
  1460. * Right-shift one bignum to form another.
  1461. */
  1462. Bignum bignum_rshift(Bignum a, int shift)
  1463. {
  1464. Bignum ret;
  1465. int i, shiftw, shiftb, shiftbb, bits;
  1466. BignumInt ai, ai1;
  1467. assert(shift >= 0);
  1468. bits = bignum_bitcount(a) - shift;
  1469. ret = newbn((bits + BIGNUM_INT_BITS - 1) / BIGNUM_INT_BITS);
  1470. if (ret) {
  1471. shiftw = shift / BIGNUM_INT_BITS;
  1472. shiftb = shift % BIGNUM_INT_BITS;
  1473. shiftbb = BIGNUM_INT_BITS - shiftb;
  1474. ai1 = a[shiftw + 1];
  1475. for (i = 1; i <= (int)ret[0]; i++) {
  1476. ai = ai1;
  1477. ai1 = (i + shiftw + 1 <= (int)a[0] ? a[i + shiftw + 1] : 0);
  1478. ret[i] = ((ai >> shiftb) | (ai1 << shiftbb)) & BIGNUM_INT_MASK;
  1479. }
  1480. }
  1481. return ret;
  1482. }
  1483. /*
  1484. * Left-shift one bignum to form another.
  1485. */
  1486. Bignum bignum_lshift(Bignum a, int shift)
  1487. {
  1488. Bignum ret;
  1489. int bits, shiftWords, shiftBits;
  1490. assert(shift >= 0);
  1491. bits = bignum_bitcount(a) + shift;
  1492. ret = newbn((bits + BIGNUM_INT_BITS - 1) / BIGNUM_INT_BITS);
  1493. shiftWords = shift / BIGNUM_INT_BITS;
  1494. shiftBits = shift % BIGNUM_INT_BITS;
  1495. if (shiftBits == 0)
  1496. {
  1497. memcpy(&ret[1 + shiftWords], &a[1], sizeof(BignumInt) * a[0]);
  1498. }
  1499. else
  1500. {
  1501. int i;
  1502. BignumInt carry = 0;
  1503. /* Remember that Bignum[0] is length, so add 1 */
  1504. for (i = shiftWords + 1; i < ((int)a[0]) + shiftWords + 1; ++i)
  1505. {
  1506. BignumInt from = a[i - shiftWords];
  1507. ret[i] = (from << shiftBits) | carry;
  1508. carry = from >> (BIGNUM_INT_BITS - shiftBits);
  1509. }
  1510. if (carry) ret[i] = carry;
  1511. }
  1512. return ret;
  1513. }
  1514. /*
  1515. * Non-modular multiplication and addition.
  1516. */
  1517. Bignum bigmuladd(Bignum a, Bignum b, Bignum addend)
  1518. {
  1519. int alen = a[0], blen = b[0];
  1520. int mlen = (alen > blen ? alen : blen);
  1521. int rlen, i, maxspot;
  1522. int wslen;
  1523. BignumInt *workspace;
  1524. Bignum ret;
  1525. /* mlen space for a, mlen space for b, 2*mlen for result,
  1526. * plus scratch space for multiplication */
  1527. wslen = mlen * 4 + mul_compute_scratch(mlen);
  1528. workspace = snewn(wslen, BignumInt);
  1529. for (i = 0; i < mlen; i++) {
  1530. workspace[0 * mlen + i] = (mlen - i <= (int)a[0] ? a[mlen - i] : 0);
  1531. workspace[1 * mlen + i] = (mlen - i <= (int)b[0] ? b[mlen - i] : 0);
  1532. }
  1533. internal_mul(workspace + 0 * mlen, workspace + 1 * mlen,
  1534. workspace + 2 * mlen, mlen, workspace + 4 * mlen);
  1535. /* now just copy the result back */
  1536. rlen = alen + blen + 1;
  1537. if (addend && rlen <= (int)addend[0])
  1538. rlen = addend[0] + 1;
  1539. ret = newbn(rlen);
  1540. maxspot = 0;
  1541. for (i = 1; i <= (int)ret[0]; i++) {
  1542. ret[i] = (i <= 2 * mlen ? workspace[4 * mlen - i] : 0);
  1543. if (ret[i] != 0)
  1544. maxspot = i;
  1545. }
  1546. ret[0] = maxspot;
  1547. /* now add in the addend, if any */
  1548. if (addend) {
  1549. BignumCarry carry = 0;
  1550. for (i = 1; i <= rlen; i++) {
  1551. BignumInt retword = (i <= (int)ret[0] ? ret[i] : 0);
  1552. BignumInt addword = (i <= (int)addend[0] ? addend[i] : 0);
  1553. BignumADC(ret[i], carry, retword, addword, carry);
  1554. if (ret[i] != 0 && i > maxspot)
  1555. maxspot = i;
  1556. }
  1557. }
  1558. ret[0] = maxspot;
  1559. smemclr(workspace, wslen * sizeof(*workspace));
  1560. sfree(workspace);
  1561. return ret;
  1562. }
  1563. /*
  1564. * Non-modular multiplication.
  1565. */
  1566. Bignum bigmul(Bignum a, Bignum b)
  1567. {
  1568. return bigmuladd(a, b, NULL);
  1569. }
  1570. /*
  1571. * Simple addition.
  1572. */
  1573. Bignum bigadd(Bignum a, Bignum b)
  1574. {
  1575. int alen = a[0], blen = b[0];
  1576. int rlen = (alen > blen ? alen : blen) + 1;
  1577. int i, maxspot;
  1578. Bignum ret;
  1579. BignumCarry carry;
  1580. ret = newbn(rlen);
  1581. carry = 0;
  1582. maxspot = 0;
  1583. for (i = 1; i <= rlen; i++) {
  1584. BignumInt aword = (i <= (int)a[0] ? a[i] : 0);
  1585. BignumInt bword = (i <= (int)b[0] ? b[i] : 0);
  1586. BignumADC(ret[i], carry, aword, bword, carry);
  1587. if (ret[i] != 0 && i > maxspot)
  1588. maxspot = i;
  1589. }
  1590. ret[0] = maxspot;
  1591. return ret;
  1592. }
  1593. /*
  1594. * Subtraction. Returns a-b, or NULL if the result would come out
  1595. * negative (recall that this entire bignum module only handles
  1596. * positive numbers).
  1597. */
  1598. Bignum bigsub(Bignum a, Bignum b)
  1599. {
  1600. int alen = a[0], blen = b[0];
  1601. int rlen = (alen > blen ? alen : blen);
  1602. int i, maxspot;
  1603. Bignum ret;
  1604. BignumCarry carry;
  1605. ret = newbn(rlen);
  1606. carry = 1;
  1607. maxspot = 0;
  1608. for (i = 1; i <= rlen; i++) {
  1609. BignumInt aword = (i <= (int)a[0] ? a[i] : 0);
  1610. BignumInt bword = (i <= (int)b[0] ? b[i] : 0);
  1611. BignumADC(ret[i], carry, aword, ~bword, carry);
  1612. if (ret[i] != 0 && i > maxspot)
  1613. maxspot = i;
  1614. }
  1615. ret[0] = maxspot;
  1616. if (!carry) {
  1617. freebn(ret);
  1618. return NULL;
  1619. }
  1620. return ret;
  1621. }
  1622. /*
  1623. * Create a bignum which is the bitmask covering another one. That
  1624. * is, the smallest integer which is >= N and is also one less than
  1625. * a power of two.
  1626. */
  1627. Bignum bignum_bitmask(Bignum n)
  1628. {
  1629. Bignum ret = copybn(n);
  1630. int i;
  1631. BignumInt j;
  1632. i = ret[0];
  1633. while (n[i] == 0 && i > 0)
  1634. i--;
  1635. if (i <= 0)
  1636. return ret; /* input was zero */
  1637. j = 1;
  1638. while (j < n[i])
  1639. j = 2 * j + 1;
  1640. ret[i] = j;
  1641. while (--i > 0)
  1642. ret[i] = BIGNUM_INT_MASK;
  1643. return ret;
  1644. }
  1645. /*
  1646. * Convert an unsigned long into a bignum.
  1647. */
  1648. Bignum bignum_from_long(unsigned long n)
  1649. {
  1650. const int maxwords =
  1651. (sizeof(unsigned long) + sizeof(BignumInt) - 1) / sizeof(BignumInt);
  1652. Bignum ret;
  1653. int i;
  1654. ret = newbn(maxwords);
  1655. ret[0] = 0;
  1656. for (i = 0; i < maxwords; i++) {
  1657. ret[i+1] = n >> (i * BIGNUM_INT_BITS);
  1658. if (ret[i+1] != 0)
  1659. ret[0] = i+1;
  1660. }
  1661. return ret;
  1662. }
  1663. /*
  1664. * Add a long to a bignum.
  1665. */
  1666. Bignum bignum_add_long(Bignum number, unsigned long n)
  1667. {
  1668. const int maxwords =
  1669. (sizeof(unsigned long) + sizeof(BignumInt) - 1) / sizeof(BignumInt);
  1670. Bignum ret;
  1671. int words, i;
  1672. BignumCarry carry;
  1673. words = number[0];
  1674. if (words < maxwords)
  1675. words = maxwords;
  1676. words++;
  1677. ret = newbn(words);
  1678. carry = 0;
  1679. ret[0] = 0;
  1680. for (i = 0; i < words; i++) {
  1681. BignumInt nword = (i < maxwords ? n >> (i * BIGNUM_INT_BITS) : 0);
  1682. BignumInt numword = (i < number[0] ? number[i+1] : 0);
  1683. BignumADC(ret[i+1], carry, numword, nword, carry);
  1684. if (ret[i+1] != 0)
  1685. ret[0] = i+1;
  1686. }
  1687. return ret;
  1688. }
  1689. /*
  1690. * Compute the residue of a bignum, modulo a (max 16-bit) short.
  1691. */
  1692. unsigned short bignum_mod_short(Bignum number, unsigned short modulus)
  1693. {
  1694. unsigned long mod = modulus, r = 0;
  1695. /* Precompute (BIGNUM_INT_MASK+1) % mod */
  1696. unsigned long base_r = (BIGNUM_INT_MASK - modulus + 1) % mod;
  1697. int i;
  1698. for (i = number[0]; i > 0; i--) {
  1699. /*
  1700. * Conceptually, ((r << BIGNUM_INT_BITS) + number[i]) % mod
  1701. */
  1702. r = ((r * base_r) + (number[i] % mod)) % mod;
  1703. }
  1704. return (unsigned short) r;
  1705. }
  1706. #ifdef DEBUG
  1707. void diagbn(char *prefix, Bignum md)
  1708. {
  1709. int i, nibbles, morenibbles;
  1710. static const char hex[] = "0123456789ABCDEF";
  1711. debug("%s0x", prefix ? prefix : "");
  1712. nibbles = (3 + bignum_bitcount(md)) / 4;
  1713. if (nibbles < 1)
  1714. nibbles = 1;
  1715. morenibbles = 4 * md[0] - nibbles;
  1716. for (i = 0; i < morenibbles; i++)
  1717. debug("-");
  1718. for (i = nibbles; i--;)
  1719. debug("%c", hex[(bignum_byte(md, i / 2) >> (4 * (i % 2))) & 0xF]);
  1720. if (prefix)
  1721. debug("\n");
  1722. }
  1723. #endif
  1724. /*
  1725. * Simple division.
  1726. */
  1727. Bignum bigdiv(Bignum a, Bignum b)
  1728. {
  1729. Bignum q = newbn(a[0]);
  1730. bigdivmod(a, b, NULL, q);
  1731. while (q[0] > 1 && q[q[0]] == 0)
  1732. q[0]--;
  1733. return q;
  1734. }
  1735. /*
  1736. * Simple remainder.
  1737. */
  1738. Bignum bigmod(Bignum a, Bignum b)
  1739. {
  1740. Bignum r = newbn(b[0]);
  1741. bigdivmod(a, b, r, NULL);
  1742. while (r[0] > 1 && r[r[0]] == 0)
  1743. r[0]--;
  1744. return r;
  1745. }
  1746. /*
  1747. * Greatest common divisor.
  1748. */
  1749. Bignum biggcd(Bignum av, Bignum bv)
  1750. {
  1751. Bignum a = copybn(av);
  1752. Bignum b = copybn(bv);
  1753. while (bignum_cmp(b, Zero) != 0) {
  1754. Bignum t = newbn(b[0]);
  1755. bigdivmod(a, b, t, NULL);
  1756. while (t[0] > 1 && t[t[0]] == 0)
  1757. t[0]--;
  1758. freebn(a);
  1759. a = b;
  1760. b = t;
  1761. }
  1762. freebn(b);
  1763. return a;
  1764. }
  1765. /*
  1766. * Modular inverse, using Euclid's extended algorithm.
  1767. */
  1768. Bignum modinv(Bignum number, Bignum modulus)
  1769. {
  1770. Bignum a = copybn(modulus);
  1771. Bignum b = copybn(number);
  1772. Bignum xp = copybn(Zero);
  1773. Bignum x = copybn(One);
  1774. int sign = +1;
  1775. assert(number[number[0]] != 0);
  1776. assert(modulus[modulus[0]] != 0);
  1777. while (bignum_cmp(b, One) != 0) {
  1778. Bignum t, q;
  1779. if (bignum_cmp(b, Zero) == 0) {
  1780. /*
  1781. * Found a common factor between the inputs, so we cannot
  1782. * return a modular inverse at all.
  1783. */
  1784. freebn(b);
  1785. freebn(a);
  1786. freebn(xp);
  1787. freebn(x);
  1788. return NULL;
  1789. }
  1790. t = newbn(b[0]);
  1791. q = newbn(a[0]);
  1792. bigdivmod(a, b, t, q);
  1793. while (t[0] > 1 && t[t[0]] == 0)
  1794. t[0]--;
  1795. while (q[0] > 1 && q[q[0]] == 0)
  1796. q[0]--;
  1797. freebn(a);
  1798. a = b;
  1799. b = t;
  1800. t = xp;
  1801. xp = x;
  1802. x = bigmuladd(q, xp, t);
  1803. sign = -sign;
  1804. freebn(t);
  1805. freebn(q);
  1806. }
  1807. freebn(b);
  1808. freebn(a);
  1809. freebn(xp);
  1810. /* now we know that sign * x == 1, and that x < modulus */
  1811. if (sign < 0) {
  1812. /* set a new x to be modulus - x */
  1813. Bignum newx = newbn(modulus[0]);
  1814. BignumInt carry = 0;
  1815. int maxspot = 1;
  1816. int i;
  1817. for (i = 1; i <= (int)newx[0]; i++) {
  1818. BignumInt aword = (i <= (int)modulus[0] ? modulus[i] : 0);
  1819. BignumInt bword = (i <= (int)x[0] ? x[i] : 0);
  1820. newx[i] = aword - bword - carry;
  1821. bword = ~bword;
  1822. carry = carry ? (newx[i] >= bword) : (newx[i] > bword);
  1823. if (newx[i] != 0)
  1824. maxspot = i;
  1825. }
  1826. newx[0] = maxspot;
  1827. freebn(x);
  1828. x = newx;
  1829. }
  1830. /* and return. */
  1831. return x;
  1832. }
  1833. /*
  1834. * Render a bignum into decimal. Return a malloced string holding
  1835. * the decimal representation.
  1836. */
  1837. char *bignum_decimal(Bignum x)
  1838. {
  1839. int ndigits, ndigit;
  1840. int i;
  1841. bool iszero;
  1842. BignumInt carry;
  1843. char *ret;
  1844. BignumInt *workspace;
  1845. /*
  1846. * First, estimate the number of digits. Since log(10)/log(2)
  1847. * is just greater than 93/28 (the joys of continued fraction
  1848. * approximations...) we know that for every 93 bits, we need
  1849. * at most 28 digits. This will tell us how much to malloc.
  1850. *
  1851. * Formally: if x has i bits, that means x is strictly less
  1852. * than 2^i. Since 2 is less than 10^(28/93), this is less than
  1853. * 10^(28i/93). We need an integer power of ten, so we must
  1854. * round up (rounding down might make it less than x again).
  1855. * Therefore if we multiply the bit count by 28/93, rounding
  1856. * up, we will have enough digits.
  1857. *
  1858. * i=0 (i.e., x=0) is an irritating special case.
  1859. */
  1860. i = bignum_bitcount(x);
  1861. if (!i)
  1862. ndigits = 1; /* x = 0 */
  1863. else
  1864. ndigits = (28 * i + 92) / 93; /* multiply by 28/93 and round up */
  1865. ndigits++; /* allow for trailing \0 */
  1866. ret = snewn(ndigits, char);
  1867. /*
  1868. * Now allocate some workspace to hold the binary form as we
  1869. * repeatedly divide it by ten. Initialise this to the
  1870. * big-endian form of the number.
  1871. */
  1872. workspace = snewn(x[0], BignumInt);
  1873. for (i = 0; i < (int)x[0]; i++)
  1874. workspace[i] = x[x[0] - i];
  1875. /*
  1876. * Next, write the decimal number starting with the last digit.
  1877. * We use ordinary short division, dividing 10 into the
  1878. * workspace.
  1879. */
  1880. ndigit = ndigits - 1;
  1881. ret[ndigit] = '\0';
  1882. do {
  1883. iszero = true;
  1884. carry = 0;
  1885. for (i = 0; i < (int)x[0]; i++) {
  1886. /*
  1887. * Conceptually, we want to compute
  1888. *
  1889. * (carry << BIGNUM_INT_BITS) + workspace[i]
  1890. * -----------------------------------------
  1891. * 10
  1892. *
  1893. * but we don't have an integer type longer than BignumInt
  1894. * to work with. So we have to do it in pieces.
  1895. */
  1896. BignumInt q, r;
  1897. q = workspace[i] / 10;
  1898. r = workspace[i] % 10;
  1899. /* I want (BIGNUM_INT_MASK+1)/10 but can't say so directly! */
  1900. q += carry * ((BIGNUM_INT_MASK-9) / 10 + 1);
  1901. r += carry * ((BIGNUM_INT_MASK-9) % 10);
  1902. q += r / 10;
  1903. r %= 10;
  1904. workspace[i] = q;
  1905. carry = r;
  1906. if (workspace[i])
  1907. iszero = false;
  1908. }
  1909. ret[--ndigit] = (char) (carry + '0');
  1910. } while (!iszero);
  1911. /*
  1912. * There's a chance we've fallen short of the start of the
  1913. * string. Correct if so.
  1914. */
  1915. if (ndigit > 0)
  1916. memmove(ret, ret + ndigit, ndigits - ndigit);
  1917. /*
  1918. * Done.
  1919. */
  1920. smemclr(workspace, x[0] * sizeof(*workspace));
  1921. sfree(workspace);
  1922. return ret;
  1923. }