sshbn.c 55 KB

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