mtest.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* makes a bignum test harness with NUM tests per operation
  2. *
  3. * the output is made in the following format [one parameter per line]
  4. operation
  5. operand1
  6. operand2
  7. [... operandN]
  8. result1
  9. result2
  10. [... resultN]
  11. So for example "a * b mod n" would be
  12. mulmod
  13. a
  14. b
  15. n
  16. a*b mod n
  17. e.g. if a=3, b=4 n=11 then
  18. mulmod
  19. 3
  20. 4
  21. 11
  22. 1
  23. */
  24. #ifdef MP_8BIT
  25. #define THE_MASK 127
  26. #else
  27. #define THE_MASK 32767
  28. #endif
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <time.h>
  32. #include "mpi.c"
  33. FILE *rng;
  34. void rand_num(mp_int *a)
  35. {
  36. int n, size;
  37. unsigned char buf[2048];
  38. size = 1 + ((fgetc(rng)<<8) + fgetc(rng)) % 101;
  39. buf[0] = (fgetc(rng)&1)?1:0;
  40. fread(buf+1, 1, size, rng);
  41. while (buf[1] == 0) buf[1] = fgetc(rng);
  42. mp_read_raw(a, buf, 1+size);
  43. }
  44. void rand_num2(mp_int *a)
  45. {
  46. int n, size;
  47. unsigned char buf[2048];
  48. size = 10 + ((fgetc(rng)<<8) + fgetc(rng)) % 101;
  49. buf[0] = (fgetc(rng)&1)?1:0;
  50. fread(buf+1, 1, size, rng);
  51. while (buf[1] == 0) buf[1] = fgetc(rng);
  52. mp_read_raw(a, buf, 1+size);
  53. }
  54. #define mp_to64(a, b) mp_toradix(a, b, 64)
  55. int main(void)
  56. {
  57. int n, tmp;
  58. mp_int a, b, c, d, e;
  59. clock_t t1;
  60. char buf[4096];
  61. mp_init(&a);
  62. mp_init(&b);
  63. mp_init(&c);
  64. mp_init(&d);
  65. mp_init(&e);
  66. /* initial (2^n - 1)^2 testing, makes sure the comba multiplier works [it has the new carry code] */
  67. /*
  68. mp_set(&a, 1);
  69. for (n = 1; n < 8192; n++) {
  70. mp_mul(&a, &a, &c);
  71. printf("mul\n");
  72. mp_to64(&a, buf);
  73. printf("%s\n%s\n", buf, buf);
  74. mp_to64(&c, buf);
  75. printf("%s\n", buf);
  76. mp_add_d(&a, 1, &a);
  77. mp_mul_2(&a, &a);
  78. mp_sub_d(&a, 1, &a);
  79. }
  80. */
  81. rng = fopen("/dev/urandom", "rb");
  82. if (rng == NULL) {
  83. rng = fopen("/dev/random", "rb");
  84. if (rng == NULL) {
  85. fprintf(stderr, "\nWarning: stdin used as random source\n\n");
  86. rng = stdin;
  87. }
  88. }
  89. t1 = clock();
  90. for (;;) {
  91. #if 0
  92. if (clock() - t1 > CLOCKS_PER_SEC) {
  93. sleep(2);
  94. t1 = clock();
  95. }
  96. #endif
  97. n = fgetc(rng) % 15;
  98. if (n == 0) {
  99. /* add tests */
  100. rand_num(&a);
  101. rand_num(&b);
  102. mp_add(&a, &b, &c);
  103. printf("add\n");
  104. mp_to64(&a, buf);
  105. printf("%s\n", buf);
  106. mp_to64(&b, buf);
  107. printf("%s\n", buf);
  108. mp_to64(&c, buf);
  109. printf("%s\n", buf);
  110. } else if (n == 1) {
  111. /* sub tests */
  112. rand_num(&a);
  113. rand_num(&b);
  114. mp_sub(&a, &b, &c);
  115. printf("sub\n");
  116. mp_to64(&a, buf);
  117. printf("%s\n", buf);
  118. mp_to64(&b, buf);
  119. printf("%s\n", buf);
  120. mp_to64(&c, buf);
  121. printf("%s\n", buf);
  122. } else if (n == 2) {
  123. /* mul tests */
  124. rand_num(&a);
  125. rand_num(&b);
  126. mp_mul(&a, &b, &c);
  127. printf("mul\n");
  128. mp_to64(&a, buf);
  129. printf("%s\n", buf);
  130. mp_to64(&b, buf);
  131. printf("%s\n", buf);
  132. mp_to64(&c, buf);
  133. printf("%s\n", buf);
  134. } else if (n == 3) {
  135. /* div tests */
  136. rand_num(&a);
  137. rand_num(&b);
  138. mp_div(&a, &b, &c, &d);
  139. printf("div\n");
  140. mp_to64(&a, buf);
  141. printf("%s\n", buf);
  142. mp_to64(&b, buf);
  143. printf("%s\n", buf);
  144. mp_to64(&c, buf);
  145. printf("%s\n", buf);
  146. mp_to64(&d, buf);
  147. printf("%s\n", buf);
  148. } else if (n == 4) {
  149. /* sqr tests */
  150. rand_num(&a);
  151. mp_sqr(&a, &b);
  152. printf("sqr\n");
  153. mp_to64(&a, buf);
  154. printf("%s\n", buf);
  155. mp_to64(&b, buf);
  156. printf("%s\n", buf);
  157. } else if (n == 5) {
  158. /* mul_2d test */
  159. rand_num(&a);
  160. mp_copy(&a, &b);
  161. n = fgetc(rng) & 63;
  162. mp_mul_2d(&b, n, &b);
  163. mp_to64(&a, buf);
  164. printf("mul2d\n");
  165. printf("%s\n", buf);
  166. printf("%d\n", n);
  167. mp_to64(&b, buf);
  168. printf("%s\n", buf);
  169. } else if (n == 6) {
  170. /* div_2d test */
  171. rand_num(&a);
  172. mp_copy(&a, &b);
  173. n = fgetc(rng) & 63;
  174. mp_div_2d(&b, n, &b, NULL);
  175. mp_to64(&a, buf);
  176. printf("div2d\n");
  177. printf("%s\n", buf);
  178. printf("%d\n", n);
  179. mp_to64(&b, buf);
  180. printf("%s\n", buf);
  181. } else if (n == 7) {
  182. /* gcd test */
  183. rand_num(&a);
  184. rand_num(&b);
  185. a.sign = MP_ZPOS;
  186. b.sign = MP_ZPOS;
  187. mp_gcd(&a, &b, &c);
  188. printf("gcd\n");
  189. mp_to64(&a, buf);
  190. printf("%s\n", buf);
  191. mp_to64(&b, buf);
  192. printf("%s\n", buf);
  193. mp_to64(&c, buf);
  194. printf("%s\n", buf);
  195. } else if (n == 8) {
  196. /* lcm test */
  197. rand_num(&a);
  198. rand_num(&b);
  199. a.sign = MP_ZPOS;
  200. b.sign = MP_ZPOS;
  201. mp_lcm(&a, &b, &c);
  202. printf("lcm\n");
  203. mp_to64(&a, buf);
  204. printf("%s\n", buf);
  205. mp_to64(&b, buf);
  206. printf("%s\n", buf);
  207. mp_to64(&c, buf);
  208. printf("%s\n", buf);
  209. } else if (n == 9) {
  210. /* exptmod test */
  211. rand_num2(&a);
  212. rand_num2(&b);
  213. rand_num2(&c);
  214. // if (c.dp[0]&1) mp_add_d(&c, 1, &c);
  215. a.sign = b.sign = c.sign = 0;
  216. mp_exptmod(&a, &b, &c, &d);
  217. printf("expt\n");
  218. mp_to64(&a, buf);
  219. printf("%s\n", buf);
  220. mp_to64(&b, buf);
  221. printf("%s\n", buf);
  222. mp_to64(&c, buf);
  223. printf("%s\n", buf);
  224. mp_to64(&d, buf);
  225. printf("%s\n", buf);
  226. } else if (n == 10) {
  227. /* invmod test */
  228. rand_num2(&a);
  229. rand_num2(&b);
  230. b.sign = MP_ZPOS;
  231. a.sign = MP_ZPOS;
  232. mp_gcd(&a, &b, &c);
  233. if (mp_cmp_d(&c, 1) != 0) continue;
  234. if (mp_cmp_d(&b, 1) == 0) continue;
  235. mp_invmod(&a, &b, &c);
  236. printf("invmod\n");
  237. mp_to64(&a, buf);
  238. printf("%s\n", buf);
  239. mp_to64(&b, buf);
  240. printf("%s\n", buf);
  241. mp_to64(&c, buf);
  242. printf("%s\n", buf);
  243. } else if (n == 11) {
  244. rand_num(&a);
  245. mp_mul_2(&a, &a);
  246. mp_div_2(&a, &b);
  247. printf("div2\n");
  248. mp_to64(&a, buf);
  249. printf("%s\n", buf);
  250. mp_to64(&b, buf);
  251. printf("%s\n", buf);
  252. } else if (n == 12) {
  253. rand_num2(&a);
  254. mp_mul_2(&a, &b);
  255. printf("mul2\n");
  256. mp_to64(&a, buf);
  257. printf("%s\n", buf);
  258. mp_to64(&b, buf);
  259. printf("%s\n", buf);
  260. } else if (n == 13) {
  261. rand_num2(&a);
  262. tmp = abs(rand()) & THE_MASK;
  263. mp_add_d(&a, tmp, &b);
  264. printf("add_d\n");
  265. mp_to64(&a, buf);
  266. printf("%s\n%d\n", buf, tmp);
  267. mp_to64(&b, buf);
  268. printf("%s\n", buf);
  269. } else if (n == 14) {
  270. rand_num2(&a);
  271. tmp = abs(rand()) & THE_MASK;
  272. mp_sub_d(&a, tmp, &b);
  273. printf("sub_d\n");
  274. mp_to64(&a, buf);
  275. printf("%s\n%d\n", buf, tmp);
  276. mp_to64(&b, buf);
  277. printf("%s\n", buf);
  278. }
  279. }
  280. fclose(rng);
  281. return 0;
  282. }
  283. /* $Source: /cvs/libtom/libtommath/mtest/mtest.c,v $ */
  284. /* $Revision: 1.2 $ */
  285. /* $Date: 2005/05/05 14:38:47 $ */