1 /* makes a bignum test harness with NUM tests per operation
3 * the output is made in the following format [one parameter per line]
13 So for example "a * b mod n" would be
21 e.g. if a=3, b=4 n=11 then
34 #define THE_MASK 32767
44 void rand_num(mp_int
*a
)
47 unsigned char buf
[2048];
49 size
= 1 + ((fgetc(rng
)<<8) + fgetc(rng
)) % 101;
50 buf
[0] = (fgetc(rng
)&1)?1:0;
51 fread(buf
+1, 1, size
, rng
);
52 while (buf
[1] == 0) buf
[1] = fgetc(rng
);
53 mp_read_raw(a
, buf
, 1+size
);
56 void rand_num2(mp_int
*a
)
59 unsigned char buf
[2048];
61 size
= 10 + ((fgetc(rng
)<<8) + fgetc(rng
)) % 101;
62 buf
[0] = (fgetc(rng
)&1)?1:0;
63 fread(buf
+1, 1, size
, rng
);
64 while (buf
[1] == 0) buf
[1] = fgetc(rng
);
65 mp_read_raw(a
, buf
, 1+size
);
68 #define mp_to64(a, b) mp_toradix(a, b, 64)
84 /* initial (2^n - 1)^2 testing, makes sure the comba multiplier works [it has the new carry code] */
87 for (n = 1; n < 8192; n++) {
91 printf("%s\n%s\n", buf, buf);
101 rng
= fopen("/dev/urandom", "rb");
103 rng
= fopen("/dev/random", "rb");
105 fprintf(stderr
, "\nWarning: stdin used as random source\n\n");
113 if (clock() - t1
> CLOCKS_PER_SEC
) {
160 mp_div(&a
, &b
, &c
, &d
);
184 mp_mul_2d(&b
, n
, &b
);
196 mp_div_2d(&b
, n
, &b
, NULL
);
236 // if (c.dp[0]&1) mp_add_d(&c, 1, &c);
237 a
.sign
= b
.sign
= c
.sign
= 0;
238 mp_exptmod(&a
, &b
, &c
, &d
);
248 } else if (n
== 10) {
255 if (mp_cmp_d(&c
, 1) != 0) continue;
256 if (mp_cmp_d(&b
, 1) == 0) continue;
257 mp_invmod(&a
, &b
, &c
);
265 } else if (n
== 11) {
274 } else if (n
== 12) {
282 } else if (n
== 13) {
284 tmp
= abs(rand()) & THE_MASK
;
285 mp_add_d(&a
, tmp
, &b
);
288 printf("%s\n%d\n", buf
, tmp
);
291 } else if (n
== 14) {
293 tmp
= abs(rand()) & THE_MASK
;
294 mp_sub_d(&a
, tmp
, &b
);
297 printf("%s\n%d\n", buf
, tmp
);
306 /* $Source: /cvs/libtom/libtommath/mtest/mtest.c,v $ */
307 /* $Revision: 1.2 $ */
308 /* $Date: 2005/05/05 14:38:47 $ */