bntest64.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /*
  2. * Copyright (c) 1995 Colin Plumb. All rights reserved.
  3. * For licensing and other legal details, see the file legal.c.
  4. *
  5. * Test driver for low-level bignum library (64-bit version).
  6. * This access the low-level library directly. It is NOT an example of
  7. * how to program with the library normally! By accessing the library
  8. * at a low level, it is possible to exercise the smallest components
  9. * and thus localize bugs more accurately. This is especially useful
  10. * when writing assembly-language primitives.
  11. *
  12. * This also does timing tests on modular exponentiation. Modular
  13. * exponentiation is so computationally expensive that the fact that this
  14. * code omits one level of interface glue has no perceptible effect on
  15. * the results.
  16. */
  17. #include "zrtp.h"
  18. #ifndef HAVE_CONFIG_H
  19. #define HAVE_CONFIG_H 0
  20. #endif
  21. #if HAVE_CONFIG_H
  22. #include "bnconfig.h"
  23. #endif
  24. #define _ZTU_ "bntest"
  25. /*
  26. * Some compilers complain about #if FOO if FOO isn't defined,
  27. * so do the ANSI-mandated thing explicitly...
  28. */
  29. #ifndef NO_STDLIB_H
  30. #define NO_STDLIB_H 0
  31. #endif
  32. #ifndef NO_STRING_H
  33. #define NO_STRING_H 0
  34. #endif
  35. #ifndef HAVE_STRINGS_H
  36. #define HAVE_STRINGS_H 0
  37. #endif
  38. #include <stdio.h>
  39. #if !NO_STDLIB_H
  40. #include <stdlib.h> /* For strtol */
  41. #else
  42. long strtol(const char *, char **, int);
  43. #endif
  44. #if !NO_STRING_H
  45. #include <string.h> /* For memcpy */
  46. #elif HAVE_STRINGS_H
  47. #include <strings.h>
  48. #endif
  49. #include "lbn64.h"
  50. #include "kludge.h"
  51. #if BNYIELD
  52. int (*bnYield)(void) = 0;
  53. #endif
  54. /* Work with up to 2048-bit numbers */
  55. #define MAXBITS 3072
  56. #define SIZE (MAXBITS/64 + 1)
  57. /* Additive congruential random number generator, x[i] = x[i-24] + x[i-55] */
  58. static BNWORD64 randp[55];
  59. static BNWORD64 *randp1 = randp, *randp2 = randp+24;
  60. static BNWORD64
  61. rand64(void)
  62. {
  63. if (++randp2 == randp+55) {
  64. randp2 = randp;
  65. randp1++;
  66. } else if (++randp1 == randp+55) {
  67. randp1 = randp;
  68. }
  69. return *randp1 += *randp2;
  70. }
  71. /*
  72. * CRC-3_2: x^3_2+x^26+x^23+x^22+x^1_6+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1
  73. *
  74. * The additive congruential RNG is seeded with a single integer,
  75. * which is shuffled with a CRC polynomial to generate the initial
  76. * table values. The Polynomial is the same size as the words being
  77. * used.
  78. *
  79. * Thus, in the various versions of this library, we actually use this
  80. * polynomial as-is, this polynomial mod x^17, and this polynomial with
  81. * the leading coefficient deleted and replaced with x^6_4. As-is,
  82. * it's irreducible, so it has a long period. Modulo x^17, it factors as
  83. * (x^4+x^3+x^2+x+1) * (x^12+x^11+x^8+x^7+x^6+x^5+x^4+x^3+1),
  84. * which still has a large enough period (4095) for the use it's put to.
  85. * With the leading coefficient moved up, it factors as
  86. * (x^50+x^49+x^48+x^47+x^46+x^43+x^41+x^40+x^38+x^37+x^36+x^35+x^34+x^33+
  87. * x^31+x^30+x^29+x^28+x^27+x^25+x^23+x^18+x^1_6+x^15+x^14+x^13+x^11+x^9+
  88. * x^8+x^7+x^6+x^5+x^3+x^2+1)*(x^11+x^10+x^9+x^5+x^4+x^3+1)*(x^3+x+1),
  89. * which definitely has a long enough period to serve for initialization.
  90. *
  91. * The effort put into this PRNG is kind of unwarranted given the trivial
  92. * use it's being put to, but oh, well. It does have the nice advantage
  93. * of producing numbers that are portable between platforms, so if there's
  94. * a problem with one platform, you can compare all the intermediate
  95. * results with another platform.
  96. */
  97. #define POLY (BNWORD64)0x04c11db7
  98. static void
  99. srand64(BNWORD64 seed)
  100. {
  101. int i, j;
  102. for (i = 0; i < 55; i++) {
  103. for (j = 0; j < 64; j++)
  104. if (seed >> (64-1))
  105. seed = (seed << 1) ^ POLY;
  106. else
  107. seed <<= 1;
  108. randp[i] = seed;
  109. }
  110. for (i = 0; i < 3*55; i ++)
  111. rand64();
  112. }
  113. static void
  114. randnum(BNWORD64 *num, unsigned len)
  115. {
  116. while (len--)
  117. BIGLITTLE(*--num,*num++) = rand64();
  118. }
  119. static void
  120. bnprint64(BNWORD64 const *num, unsigned len)
  121. {
  122. BIGLITTLE(num -= len, num += len);
  123. while (len--)
  124. ZRTP_LOG(3, (_ZTU_, "%0*lX", 64/4, (unsigned long)BIGLITTLE(*num++,*--num)));
  125. }
  126. static void
  127. bnput64(char const *prompt, BNWORD64 const *num, unsigned len)
  128. {
  129. fputs(prompt, stdout);
  130. bnprint64(num, len);
  131. putchar('\n');
  132. }
  133. /*
  134. * One of our tests uses a known prime. The following selections were
  135. * taken from the tables at the end of Hans Reisel's "Prime Numbers and
  136. * Computer Methods for Factorization", second edition - an excellent book.
  137. * (ISBN 0-8176-3743-5 ISBN 3-71283-3743-5)
  138. */
  139. #if 0
  140. /* P31=1839605 17620282 38179967 87333633 from the factors of 3^256+2^256 */
  141. static unsigned char const prime[] = {
  142. 0x17,0x38,0x15,0xBC,0x8B,0xBB,0xE9,0xEF,0x01,0xA9,0xFD,0x3A,0x01
  143. };
  144. #elif 0
  145. /* P48=40554942 04557502 46193993 36199835 4279613_2 73199617 from the same */
  146. static unsigned char const prime[] = {
  147. 0x47,0x09,0x77,0x07,0xCF,0xFD,0xE1,0x54,0x3E,0x24,
  148. 0xF7,0xF1,0x7A,0x3E,0x91,0x51,0xCC,0xC7,0xD4,0x01
  149. };
  150. #elif 0
  151. /*
  152. * P75 = 450 552871280 97906895 47687014 5808213_2
  153. * 05219565 99525911 399679128 66003_258 91979521
  154. * from the factors of 4^128+3+128
  155. * (The "026" and "062" are to prevent a Bad String from appearing here.)
  156. */
  157. static unsigned char const prime[] = {
  158. 0xFF,0x00,0xFF,0x00,0xFF,0x01,0x06,0x4F,0xF8,0xED,
  159. 0xA3,0x37,0x23,0x2A,0x04,0xEA,0xF9,0x5F,0x30,0x4C,
  160. 0xAE,0xCD, 026,0x4E, 062,0x10,0x04,0x7D,0x0D,0x79,
  161. 0x01
  162. };
  163. #else
  164. /*
  165. * P75 = 6128 85659796 45277755 9123_2190 67300940
  166. * 51844953 78793489 59444670 35675855 57440257
  167. * from the factors of 5^128+4^128
  168. * (The "026" is to prevent a Bad String from appearing here.)
  169. */
  170. static unsigned char const prime[] = {
  171. 0x01,0x78,0x4B,0xA5,0xD3,0x30,0x03,0xEB,0x73,0xE6,
  172. 0x0F,0x4E,0x31,0x7D,0xBC,0xE2,0xA0,0xD4, 026,0x3F,
  173. 0x3C,0xEA,0x1B,0x44,0xAD,0x39,0xE7,0xE5,0xAD,0x19,
  174. 0x67,0x01
  175. };
  176. #endif
  177. static int
  178. usage(char const *name)
  179. {
  180. ZRTP_LOG(3, (_ZTU_, "Usage: %s [modbits [expbits [expbits2]]"
  181. "With no arguments, just runs test suite. If modbits is given, runs\n"
  182. "quick validation test, then runs timing tests of modular exponentiation.\n"
  183. "If expbits is given, it is used as an exponent size, otherwise it defaults\n"
  184. "to the same as modbits. If expbits2 is given it is used as the second\n"
  185. "exponent size in the double-exponentiation tests, otherwise it defaults\n"
  186. "to the same as expbits. All are limited to %u bits.\n",
  187. name, (unsigned)MAXBITS));
  188. return 1;
  189. }
  190. /* for libzrtp support */
  191. int
  192. bntest_main(int argc, char **argv)
  193. {
  194. unsigned i, j, k, l, m;
  195. int z;
  196. BNWORD64 t, carry, borrow;
  197. BNWORD64 a[SIZE], b[SIZE], c[SIZE], d[SIZE];
  198. BNWORD64 e[SIZE], f[SIZE];
  199. static BNWORD64 entries[sizeof(prime)*2][(sizeof(prime)-1)/(64/8)+1];
  200. BNWORD64 *array[sizeof(prime)*2];
  201. unsigned long modbits = 0, expbits = 0, expbits2 = 0;
  202. char *p;
  203. #define A BIGLITTLE((a+SIZE),a)
  204. #define B BIGLITTLE((b+SIZE),b)
  205. #define C BIGLITTLE((c+SIZE),c)
  206. #define D BIGLITTLE((d+SIZE),d)
  207. #define E BIGLITTLE((e+SIZE),e)
  208. #define F BIGLITTLE((f+SIZE),f)
  209. static unsigned const smallprimes[] = {
  210. 2, 3, 5, 7, 11, 13, 17, 19, 23, 27, 29, 31, 37, 41, 43
  211. };
  212. /* Set up array for precomputed modexp */
  213. for (i = 0; i < sizeof(array)/sizeof(*array); i++)
  214. array[i] = entries[i] BIG(+ SIZE);
  215. srand64(1);
  216. puts(BIGLITTLE("Big-endian machine","Little-endian machine"));
  217. if (argc >= 2) {
  218. modbits = strtoul(argv[1], &p, 0);
  219. if (!modbits || *p) {
  220. ZRTP_LOG(1, (_ZTU_, "Invalid modbits: %s", argv[1]));
  221. return usage(argv[0]);
  222. }
  223. }
  224. if (argc >= 3) {
  225. expbits = strtoul(argv[2], &p, 0);
  226. if (!expbits || *p) {
  227. ZRTP_LOG(1, (_ZTU_, "Invalid expbits: %s", argv[2]));
  228. return usage(argv[0]);
  229. }
  230. expbits2 = expbits;
  231. }
  232. if (argc >= 4) {
  233. expbits2 = strtoul(argv[3], &p, 0);
  234. if (!expbits2 || *p) {
  235. ZRTP_LOG(1, (_ZTU_, "Invalid expbits2: %s", argv[3]));
  236. return usage(argv[0]);
  237. }
  238. }
  239. if (argc >= 5) {
  240. ZRTP_LOG(1, (_ZTU_, "Too many arguments: %s", argv[4]));
  241. return usage(argv[0]);
  242. }
  243. /* B is a nice not-so-little prime */
  244. lbnInsertBigBytes_64(B, prime, 0, sizeof(prime));
  245. ((unsigned char *)c)[0] = 0;
  246. lbnInsertBigBytes_64(B, (unsigned char *)c, sizeof(prime), 1);
  247. lbnExtractBigBytes_64(B, (unsigned char *)c, 0, sizeof(prime)+1);
  248. i = (sizeof(prime)-1)/(64/8)+1; /* Size of array in words */
  249. if (((unsigned char *)c)[0] ||
  250. memcmp(prime, (unsigned char *)c+1, sizeof(prime)) != 0)
  251. {
  252. ZRTP_LOG(3, (_ZTU_, "Input != output!: "));
  253. for (k = 0; k < sizeof(prime); k++)
  254. ZRTP_LOG(3, (_ZTU_, "%02X ", prime[k]));
  255. putchar('\n');
  256. for (k = 0; k < sizeof(prime)+1; k++)
  257. ZRTP_LOG(3, (_ZTU_, "%02X ", ((unsigned char *)c)[k]));
  258. putchar('\n');
  259. bnput64("p = ", B, i);
  260. }
  261. /* Timing test code - only if requested on the command line */
  262. if (modbits) {
  263. #if CLOCK_AVAIL
  264. timetype start, stop;
  265. unsigned long cursec, expsec, twoexpsec, dblexpsec;
  266. unsigned curms, expms, twoexpms, dblexpms;
  267. expsec = twoexpsec = dblexpsec = 0;
  268. expms = twoexpms = dblexpms = 0;
  269. #endif
  270. lbnCopy_64(C,B,i);
  271. lbnSub1_64(C,i,1); /* C is exponent: p-1 */
  272. puts("Testing modexp with a known prime. "
  273. "All results should be 1.");
  274. bnput64("p = ", B, i);
  275. bnput64("p-1 = ", C, i);
  276. z = lbnTwoExpMod_64(A, C, i, B, i);
  277. if (z < 0)
  278. goto nomem;
  279. bnput64("2^(p-1) mod p = ", A, i);
  280. for (j = 0; j < 10; j++) {
  281. randnum(A,i);
  282. (void)lbnDiv_64(D,A,i,B,i);
  283. bnput64("a = ", A, i);
  284. z = lbnExpMod_64(D, A, i, C, i, B, i);
  285. if (z < 0)
  286. goto nomem;
  287. bnput64("a^(p-1) mod p = ", D, i);
  288. z = lbnBasePrecompBegin_64(array, (sizeof(prime)*8+4)/5, 5,
  289. A, i, B, i);
  290. if (z < 0)
  291. goto nomem;
  292. BIGLITTLE(D[-1],D[0]) = -1;
  293. z = lbnBasePrecompExp_64(D, (BNWORD64 const * const *)array,
  294. 5, C, i, B, i);
  295. if (z < 0)
  296. goto nomem;
  297. bnput64("a^(p-1) mod p = ", D, i);
  298. for (k = 0; k < 5; k++) {
  299. randnum(E,i);
  300. bnput64("e = ", E, i);
  301. z = lbnExpMod_64(D, A, i, E, i, B, i);
  302. if (z < 0)
  303. goto nomem;
  304. bnput64("a^e mod p = ", D, i);
  305. z = lbnBasePrecompExp_64(D, (BNWORD64 const * const *)array,
  306. 5, E, i, B, i);
  307. if (z < 0)
  308. goto nomem;
  309. bnput64("a^e mod p = ", D, i);
  310. }
  311. }
  312. ZRTP_LOG(3, (_ZTU_, "\n"
  313. "Timing exponentiations modulo a %d-bit modulus, i.e.\n"
  314. "2^<%d> mod <%d> bits, <%d>^<%d> mod <%d> bits and\n"
  315. "<%d>^<%d> * <%d>^<%d> mod <%d> bits",
  316. (int)modbits, (int)expbits, (int)modbits,
  317. (int)modbits, (int)expbits, (int)modbits,
  318. (int)modbits, (int)expbits, (int)modbits, (int)expbits2,
  319. (int)modbits));
  320. i = ((int)modbits-1)/64+1;
  321. k = ((int)expbits-1)/64+1;
  322. l = ((int)expbits2-1)/64+1;
  323. for (j = 0; j < 25; j++) {
  324. randnum(A,i); /* Base */
  325. randnum(B,k); /* Exponent */
  326. randnum(C,i); /* Modulus */
  327. randnum(D,i); /* Base2 */
  328. randnum(E,l); /* Exponent */
  329. /* Clip bases and mod to appropriate number of bits */
  330. t = ((BNWORD64)2<<((modbits-1)%64)) - 1;
  331. *(BIGLITTLE(A-i,A+i-1)) &= t;
  332. *(BIGLITTLE(C-i,C+i-1)) &= t;
  333. *(BIGLITTLE(D-i,D+i-1)) &= t;
  334. /* Make modulus large (msbit set) and odd (lsbit set) */
  335. *(BIGLITTLE(C-i,C+i-1)) |= (t >> 1) + 1;
  336. BIGLITTLE(C[-1],C[0]) |= 1;
  337. /* Clip exponent to appropriate number of bits */
  338. t = ((BNWORD64)2<<((expbits-1)%64)) - 1;
  339. *(BIGLITTLE(B-k,B+k-1)) &= t;
  340. /* Make exponent large (msbit set) */
  341. *(BIGLITTLE(B-k,B+k-1)) |= (t >> 1) + 1;
  342. /* The same for exponent 2 */
  343. t = ((BNWORD64)2<<((expbits2-1)%64)) - 1;
  344. *(BIGLITTLE(E-l,E+l-1)) &= t;
  345. *(BIGLITTLE(E-l,E+l-1)) |= (t >> 1) + 1;
  346. m = lbnBits_64(A, i);
  347. if (m > (unsigned)modbits) {
  348. bnput64("a = ", a, i);
  349. ZRTP_LOG(3, (_ZTU_, "%u bits, should be <= %d", m, (int)modbits));
  350. }
  351. m = lbnBits_64(B, k);
  352. if (m != (unsigned)expbits) {
  353. bnput64("b = ", b, i);
  354. ZRTP_LOG(3, (_ZTU_, "%u bits, should be %d", m, (int)expbits));
  355. }
  356. m = lbnBits_64(C, i);
  357. if (m != (unsigned)modbits) {
  358. bnput64("c = ", c, k);
  359. ZRTP_LOG(3, (_ZTU_, "%u bits, should be %d", m, (int)modbits));
  360. }
  361. m = lbnBits_64(D, i);
  362. if (m > (unsigned)modbits) {
  363. bnput64("d = ", d, i);
  364. ZRTP_LOG(3, (_ZTU_, "%u bits, should be <= %d", m, (int)modbits));
  365. }
  366. m = lbnBits_64(E, l);
  367. if (m != (unsigned)expbits2) {
  368. bnput64("e = ", e, i);
  369. ZRTP_LOG(3, (_ZTU_, "%u bits, should be %d", m, (int)expbits2));
  370. }
  371. #if CLOCK_AVAIL
  372. gettime(&start);
  373. #endif
  374. z = lbnTwoExpMod_64(A, B, k, C, i);
  375. if (z < 0)
  376. goto nomem;
  377. #if CLOCK_AVAIL
  378. gettime(&stop);
  379. subtime(stop, start);
  380. twoexpsec += cursec = sec(stop);
  381. twoexpms += curms = msec(stop);
  382. ZRTP_LOG(3, (_ZTU_, "2^<%d>:%4lu.%03u ", (int)expbits, cursec, curms));
  383. #else
  384. ZRTP_LOG(3, (_ZTU_, "<%d>^<%d> ", (int)modbits, (int)expbits));
  385. #endif
  386. fflush(stdout);
  387. #if CLOCK_AVAIL
  388. gettime(&start);
  389. #endif
  390. z = lbnExpMod_64(A, A, i, B, k, C, i);
  391. if (z < 0)
  392. goto nomem;
  393. #if CLOCK_AVAIL
  394. gettime(&stop);
  395. subtime(stop, start);
  396. expsec += cursec = sec(stop);
  397. expms += curms = msec(stop);
  398. ZRTP_LOG(3, (_ZTU_, "<%d>^<%d>:%4lu.%03u ",(int)modbits, (int)expbits, cursec, curms));
  399. fflush(stdout);
  400. gettime(&start);
  401. z = lbnDoubleExpMod_64(D, A, i, B, k, D, i, E, l,C,i);
  402. if (z < 0)
  403. goto nomem;
  404. gettime(&stop);
  405. subtime(stop, start);
  406. dblexpsec += cursec = sec(stop);
  407. dblexpms += curms = msec(stop);
  408. ZRTP_LOG(3, (_ZTU_, "<%d>^<%d>*<%d>^<%d>:%4lu.%03u",
  409. (int)modbits, (int)expbits,
  410. (int)modbits, (int)expbits2,
  411. cursec, curms));
  412. #else
  413. ZRTP_LOG(3, (_ZTU_, "<%d>^<%d>*<%d>^<%d>",
  414. (int)modbits, (int)expbits,
  415. (int)modbits, (int)expbits2));
  416. #endif
  417. }
  418. #if CLOCK_AVAIL
  419. twoexpms += (twoexpsec % j) * 1000;
  420. ZRTP_LOG(3, (_ZTU_, "2^<%d> mod <%d> bits AVERAGE: %4lu.%03u s",
  421. (int)expbits, (int)modbits, twoexpsec/j, twoexpms/j));
  422. expms += (expsec % j) * 1000;
  423. ZRTP_LOG(3, (_ZTU_, "<%d>^<%d> mod <%d> bits AVERAGE: %4lu.%03u s",
  424. (int)modbits, (int)expbits, (int)modbits, expsec/j, expms/j));
  425. dblexpms += (dblexpsec % j) * 1000;
  426. ZRTP_LOG(3, (_ZTU_, "<%d>^<%d> * <%d>^<%d> mod <%d> bits AVERAGE:"
  427. " %4lu.%03u s",
  428. (int)modbits, (int)expbits, (int)modbits,
  429. (int)expbits2,
  430. (int)modbits, dblexpsec/j, dblexpms/j));
  431. putchar('\n');
  432. #endif
  433. }
  434. puts("Beginning 1000 interations of sanity checking.\n"
  435. "Any output indicates a bug. No output is very strong\n"
  436. "evidence that all the important low-level bignum routines\n"
  437. "are working properly.\n");
  438. /*
  439. * If you change this loop to have an iteration 0, all results
  440. * are primted on that iteration. Useful to see what's going
  441. * on in case of major wierdness, but it produces a *lot* of
  442. * output.
  443. */
  444. #if (ZRTP_PLATFORM == ZP_WINCE) || (ZRTP_PLATFORM == ZP_SYMBIAN)
  445. for (j = 1; j <= 20; j++) {
  446. #else
  447. for (j = 1; j <= 1000; j++) {
  448. #endif
  449. /* Do the tests for lots of different number sizes. */
  450. for (i = 1; i <= SIZE/2; i++) {
  451. /* Make a random number i words long */
  452. do {
  453. randnum(A,i);
  454. } while (lbnNorm_64(A,i) < i);
  455. /* Checl lbnCmp - does a == a? */
  456. if (lbnCmp_64(A,A,i) || !j) {
  457. bnput64("a = ", A, i);
  458. ZRTP_LOG(3, (_ZTU_, "(a <=> a) = %d", lbnCmp_64(A,A,i)));
  459. }
  460. memcpy(c, a, sizeof(a));
  461. /* Check that the difference, after copy, is good. */
  462. if (lbnCmp_64(A,C,i) || !j) {
  463. bnput64("a = ", A, i);
  464. bnput64("c = ", C, i);
  465. ZRTP_LOG(3, (_ZTU_, "(a <=> c) = %d", lbnCmp_64(A,C,i)));
  466. }
  467. /* Generate a non-zero random t */
  468. do {
  469. t = rand64();
  470. } while (!t);
  471. /*
  472. * Add t to A. Check that:
  473. * - lbnCmp works in both directions, and
  474. * - A + t is greater than A. If there was a carry,
  475. * the result, less the carry, should be *less*
  476. * than A.
  477. */
  478. carry = lbnAdd1_64(A,i,t);
  479. if (lbnCmp_64(A,C,i) + lbnCmp_64(C,A,i) != 0 ||
  480. lbnCmp_64(A,C,i) != (carry ? -1 : 1) || !j)
  481. {
  482. bnput64("c = ", C, i);
  483. ZRTP_LOG(3, (_ZTU_, "t = %lX", (unsigned long)t));
  484. bnput64("a = c+t = ", A, i);
  485. ZRTP_LOG(3, (_ZTU_, "carry = %lX", (unsigned long)carry));
  486. ZRTP_LOG(3, (_ZTU_, "(a <=> c) = %d", lbnCmp_64(A,C,i)));
  487. ZRTP_LOG(3, (_ZTU_, "(c <=> a) = %d", lbnCmp_64(C,A,i)));
  488. }
  489. /* Subtract t again */
  490. memcpy(d, a, sizeof(a));
  491. borrow = lbnSub1_64(A,i,t);
  492. if (carry != borrow || lbnCmp_64(A,C,i) || !j) {
  493. bnput64("a = ", C, i);
  494. ZRTP_LOG(3, (_ZTU_, "t = %lX", (unsigned long)t));
  495. lbnAdd1_64(A,i,t);
  496. bnput64("a += t = ", A, i);
  497. ZRTP_LOG(3, (_ZTU_, "Carry = %lX", (unsigned long)carry));
  498. lbnSub1_64(A,i,t);
  499. bnput64("a -= t = ", A, i);
  500. ZRTP_LOG(3, (_ZTU_, "Borrow = %lX", (unsigned long)borrow));
  501. ZRTP_LOG(3, (_ZTU_, "(a <=> c) = %d", lbnCmp_64(A,C,i)));
  502. }
  503. /* Generate a random B */
  504. do {
  505. randnum(B,i);
  506. } while (lbnNorm_64(B,i) < i);
  507. carry = lbnAddN_64(A,B,i);
  508. memcpy(d, a, sizeof(a));
  509. borrow = lbnSubN_64(A,B,i);
  510. if (carry != borrow || lbnCmp_64(A,C,i) || !j) {
  511. bnput64("a = ", C, i);
  512. bnput64("b = ", B, i);
  513. bnput64("a += b = ", D, i);
  514. ZRTP_LOG(3, (_ZTU_, "Carry = %lX", (unsigned long)carry));
  515. bnput64("a -= b = ", A, i);
  516. ZRTP_LOG(3, (_ZTU_, "Borrow = %lX", (unsigned long)borrow));
  517. ZRTP_LOG(3, (_ZTU_, "(a <=> c) = %d", lbnCmp_64(A,C,i)));
  518. }
  519. /* D = B * t */
  520. lbnMulN1_64(D, B, i, t);
  521. memcpy(e, d, sizeof(e));
  522. /* D = A + B * t, "carry" is overflow */
  523. borrow = *(BIGLITTLE(D-i-1,D+i)) += lbnAddN_64(D,A,i);
  524. carry = lbnMulAdd1_64(A, B, i, t);
  525. /* Did MulAdd get the same answer as mul then add? */
  526. if (carry != borrow || lbnCmp_64(A, D, i) || !j) {
  527. bnput64("a = ", C, i);
  528. bnput64("b = ", B, i);
  529. ZRTP_LOG(3, (_ZTU_, "t = %lX", (unsigned long)t));
  530. bnput64("e = b * t = ", E, i+1);
  531. bnput64(" a + e = ", D, i+1);
  532. bnput64("a + b * t = ", A, i);
  533. ZRTP_LOG(3, (_ZTU_, "carry = %lX", (unsigned long)carry));
  534. }
  535. memcpy(d, a, sizeof(a));
  536. borrow = lbnMulSub1_64(A, B, i, t);
  537. /* Did MulSub perform the inverse of MulAdd */
  538. if (carry != borrow || lbnCmp_64(A,C,i) || !j) {
  539. bnput64(" a = ", C, i);
  540. bnput64(" b = ", B, i);
  541. bnput64("a += b*t = ", D, i);
  542. ZRTP_LOG(3, (_ZTU_, "Carry = %lX", (unsigned long)carry));
  543. bnput64("a -= b*t = ", A, i);
  544. ZRTP_LOG(3, (_ZTU_, "Borrow = %lX", (unsigned long)borrow));
  545. ZRTP_LOG(3, (_ZTU_, "(a <=> c) = %d", lbnCmp_64(A,C,i)));
  546. bnput64("b*t = ", E, i+1);
  547. }
  548. /* At this point we're done with t, so it's scratch */
  549. #if 0
  550. /* Extra debug code */
  551. lbnMulN1_64(C, A, i, BIGLITTLE(B[-1],B[0]));
  552. bnput64("a * b[0] = ", C, i+1);
  553. for (k = 1; k < i; k++) {
  554. carry = lbnMulAdd1_64(BIGLITTLE(C-k,C+k), A, i,
  555. *(BIGLITTLE(B-1-k,B+k)));
  556. *(BIGLITTLE(C-i-k,C+i+k)) = carry;
  557. bnput64("a * b[x] = ", C, i+k+1);
  558. }
  559. lbnMulN1_64(D, B, i, BIGLITTLE(A[-1],A[0]));
  560. bnput64("b * a[0] = ", D, i+1);
  561. for (k = 1; k < i; k++) {
  562. carry = lbnMulAdd1_64(BIGLITTLE(D-k,D+k), B, i,
  563. *(BIGLITTLE(A-1-k,A+k)));
  564. *(BIGLITTLE(D-i-k,D+i+k)) = carry;
  565. bnput64("b * a[x] = ", D, i+k+1);
  566. }
  567. #endif
  568. /* Does Mul work both ways symmetrically */
  569. lbnMul_64(C,A,i,B,i);
  570. lbnMul_64(D,B,i,A,i);
  571. if (lbnCmp_64(C,D,i+i) || !j) {
  572. bnput64("a = ", A, i);
  573. bnput64("b = ", B, i);
  574. bnput64("a * b = ", C, i+i);
  575. bnput64("b * a = ", D, i+i);
  576. ZRTP_LOG(3, (_ZTU_, "(a*b <=> b*a) = %d", lbnCmp_64(C,D,i+i)));
  577. }
  578. /* Check multiplication modulo some small things */
  579. /* 30030 = 2*3*5*11*13 */
  580. k = lbnModQ_64(C, i+i, 30030);
  581. for (l = 0;
  582. l < sizeof(smallprimes)/sizeof(*smallprimes);
  583. l++)
  584. {
  585. m = smallprimes[l];
  586. t = lbnModQ_64(C, i+i, m);
  587. carry = lbnModQ_64(A, i, m);
  588. borrow = lbnModQ_64(B, i, m);
  589. if (t != (carry * borrow) % m) {
  590. bnput64("a = ", A, i);
  591. ZRTP_LOG(3, (_ZTU_, "a mod %u = %u", m, (unsigned)carry));
  592. bnput64("b = ", B, i);
  593. ZRTP_LOG(3, (_ZTU_, "b mod %u = %u", m, (unsigned)borrow));
  594. bnput64("a*b = ", C, i+i);
  595. ZRTP_LOG(3, (_ZTU_, "a*b mod %u = %u", m, (unsigned)t));
  596. ZRTP_LOG(3, (_ZTU_, "expected %u", (unsigned)((carry*borrow)%m)));
  597. }
  598. /* Verify that (C % 30030) % m == C % m */
  599. if (m <= 13 && t != k % m) {
  600. ZRTP_LOG(3, (_ZTU_, "c mod 30030 = %u mod %u= %u", k, m, k%m));
  601. ZRTP_LOG(3, (_ZTU_, "c mod %u = %u", m, (unsigned)t));
  602. }
  603. }
  604. /* Generate an F less than A and B */
  605. do {
  606. randnum(F,i);
  607. } while (lbnCmp_64(F,A,i) >= 0 ||
  608. lbnCmp_64(F,B,i) >= 0);
  609. /* Add F to D (remember, D = A*B) */
  610. lbnAdd1_64(BIGLITTLE(D-i,D+i), i, lbnAddN_64(D, F, i));
  611. memcpy(c, d, sizeof(d));
  612. /*
  613. * Divide by A and check that quotient and remainder
  614. * match (remainder should be F, quotient should be B)
  615. */
  616. t = lbnDiv_64(E,C,i+i,A,i);
  617. if (t || lbnCmp_64(E,B,i) || lbnCmp_64(C, F, i) || !j) {
  618. bnput64("a = ", A, i);
  619. bnput64("b = ", B, i);
  620. bnput64("f = ", F, i);
  621. bnput64("a * b + f = ", D, i+i);
  622. ZRTP_LOG(3, (_ZTU_, "qhigh = %lX", (unsigned long)t));
  623. bnput64("(a*b+f) / a = ", E, i);
  624. bnput64("(a*b+f) % a = ", C, i);
  625. }
  626. memcpy(c, d, sizeof(d));
  627. /* Divide by B and check similarly */
  628. t = lbnDiv_64(E,C,i+i,B,i);
  629. if (lbnCmp_64(E,A,i) || lbnCmp_64(C, F, i) || !j) {
  630. bnput64("a = ", A, i);
  631. bnput64("b = ", B, i);
  632. bnput64("f = ", F, i);
  633. bnput64("a * b + f = ", D, i+i);
  634. ZRTP_LOG(3, (_ZTU_, "qhigh = %lX", (unsigned long)t));
  635. bnput64("(a*b+f) / b = ", E, i);
  636. bnput64("(a*b+f) % b = ", C, i);
  637. }
  638. /* Check that A*A == A^2 */
  639. lbnMul_64(C,A,i,A,i);
  640. lbnSquare_64(D,A,i);
  641. if (lbnCmp_64(C,D,i+i) || !j) {
  642. bnput64("a*a = ", C, i+i);
  643. bnput64("a^2 = ", D, i+i);
  644. ZRTP_LOG(3, (_ZTU_, "(a * a == a^2) = %d", lbnCmp_64(C,D,i+i)));
  645. }
  646. /* Compute a GCD */
  647. lbnCopy_64(C,A,i);
  648. lbnCopy_64(D,B,i);
  649. z = lbnGcd_64(C, i, D, i, &k);
  650. if (z < 0)
  651. goto nomem;
  652. /* z = 1 if GCD in D; z = 0 if GCD in C */
  653. /* Approximate check that the GCD came out right */
  654. for (l = 0;
  655. l < sizeof(smallprimes)/sizeof(*smallprimes);
  656. l++)
  657. {
  658. m = smallprimes[l];
  659. t = lbnModQ_64(z ? D : C, k, m);
  660. carry = lbnModQ_64(A, i, m);
  661. borrow = lbnModQ_64(B, i, m);
  662. if (!t != (!carry && !borrow)) {
  663. bnput64("a = ", A, i);
  664. ZRTP_LOG(3, (_ZTU_, "a mod %u = %u", m, (unsigned)carry));
  665. bnput64("b = ", B, i);
  666. ZRTP_LOG(3, (_ZTU_, "b mod %u = %u", m, (unsigned)borrow));
  667. bnput64("gcd(a,b) = ", z ? D : C, k);
  668. ZRTP_LOG(3, (_ZTU_, "gcd(a,b) mod %u = %u", m, (unsigned)t));
  669. }
  670. }
  671. /*
  672. * Do some Montgomery operations
  673. * Start with A > B, and also place a copy of B into C.
  674. * Then make A odd so it can be a Montgomery modulus.
  675. */
  676. if (lbnCmp_64(A, B, i) < 0) {
  677. memcpy(c, a, sizeof(c));
  678. memcpy(a, b, sizeof(a));
  679. memcpy(b, c, sizeof(b));
  680. } else {
  681. memcpy(c, b, sizeof(c));
  682. }
  683. BIGLITTLE(A[-1],A[0]) |= 1;
  684. /* Convert to and from */
  685. lbnToMont_64(B, i, A, i);
  686. lbnFromMont_64(B, A, i);
  687. if (lbnCmp_64(B, C, i)) {
  688. memcpy(b, c, sizeof(c));
  689. bnput64("mod = ", A, i);
  690. bnput64("input = ", B, i);
  691. lbnToMont_64(B, i, A, i);
  692. bnput64("mont = ", B, i);
  693. lbnFromMont_64(B, A, i);
  694. bnput64("output = ", B, i);
  695. }
  696. /* E = B^5 (mod A), no Montgomery ops */
  697. lbnSquare_64(E, B, i);
  698. (void)lbnDiv_64(BIGLITTLE(E-i,E+i),E,i+i,A,i);
  699. lbnSquare_64(D, E, i);
  700. (void)lbnDiv_64(BIGLITTLE(D-i,D+i),D,i+i,A,i);
  701. lbnMul_64(E, D, i, B, i);
  702. (void)lbnDiv_64(BIGLITTLE(E-i,E+i),E,i+i,A,i);
  703. /* D = B^5, using ExpMod */
  704. BIGLITTLE(F[-1],F[0]) = 5;
  705. z = lbnExpMod_64(D, B, i, F, 1, A, i);
  706. if (z < 0)
  707. goto nomem;
  708. if (lbnCmp_64(D, E, i) || !j) {
  709. bnput64("mod = ", A, i);
  710. bnput64("input = ", B, i);
  711. bnput64("input^5 = ", E, i);
  712. bnput64("input^5 = ", D, i);
  713. ZRTP_LOG(3, (_ZTU_, "a>b (x <=> y) = %d", lbnCmp_64(D,E,i)));
  714. }
  715. /* TODO: Test lbnTwoExpMod, lbnDoubleExpMod */
  716. } /* for (i) */
  717. ZRTP_LOG(3, (_ZTU_, "\r%d ", j));
  718. fflush(stdout);
  719. } /* for (j) */
  720. ZRTP_LOG(3, (_ZTU_, "%d iterations of up to %d 64-bit words completed.", j-1, i-1));
  721. return 0;
  722. nomem:
  723. ZRTP_LOG(3, (_ZTU_, "Out of memory"));
  724. return 1;
  725. }