1 /* $NetBSD: mtest.c,v 1.1.1.2 2014/04/24 12:45:39 pettai Exp $ */
3 /* makes a bignum test harness with NUM tests per operation
5 * the output is made in the following format [one parameter per line]
15 So for example "a * b mod n" would be
23 e.g. if a=3, b=4 n=11 then
36 #define THE_MASK 32767
46 void rand_num(mp_int
*a
)
49 unsigned char buf
[2048];
51 size
= 1 + ((fgetc(rng
)<<8) + fgetc(rng
)) % 101;
52 buf
[0] = (fgetc(rng
)&1)?1:0;
53 fread(buf
+1, 1, size
, rng
);
54 while (buf
[1] == 0) buf
[1] = fgetc(rng
);
55 mp_read_raw(a
, buf
, 1+size
);
58 void rand_num2(mp_int
*a
)
61 unsigned char buf
[2048];
63 size
= 10 + ((fgetc(rng
)<<8) + fgetc(rng
)) % 101;
64 buf
[0] = (fgetc(rng
)&1)?1:0;
65 fread(buf
+1, 1, size
, rng
);
66 while (buf
[1] == 0) buf
[1] = fgetc(rng
);
67 mp_read_raw(a
, buf
, 1+size
);
70 #define mp_to64(a, b) mp_toradix(a, b, 64)
86 /* initial (2^n - 1)^2 testing, makes sure the comba multiplier works [it has the new carry code] */
89 for (n = 1; n < 8192; n++) {
93 printf("%s\n%s\n", buf, buf);
103 rng
= fopen("/dev/urandom", "rb");
105 rng
= fopen("/dev/random", "rb");
107 fprintf(stderr
, "\nWarning: stdin used as random source\n\n");
115 if (clock() - t1
> CLOCKS_PER_SEC
) {
162 mp_div(&a
, &b
, &c
, &d
);
186 mp_mul_2d(&b
, n
, &b
);
198 mp_div_2d(&b
, n
, &b
, NULL
);
238 // if (c.dp[0]&1) mp_add_d(&c, 1, &c);
239 a
.sign
= b
.sign
= c
.sign
= 0;
240 mp_exptmod(&a
, &b
, &c
, &d
);
250 } else if (n
== 10) {
257 if (mp_cmp_d(&c
, 1) != 0) continue;
258 if (mp_cmp_d(&b
, 1) == 0) continue;
259 mp_invmod(&a
, &b
, &c
);
267 } else if (n
== 11) {
276 } else if (n
== 12) {
284 } else if (n
== 13) {
286 tmp
= abs(rand()) & THE_MASK
;
287 mp_add_d(&a
, tmp
, &b
);
290 printf("%s\n%d\n", buf
, tmp
);
293 } else if (n
== 14) {
295 tmp
= abs(rand()) & THE_MASK
;
296 mp_sub_d(&a
, tmp
, &b
);
299 printf("%s\n%d\n", buf
, tmp
);
308 /* Source: /cvs/libtom/libtommath/mtest/mtest.c,v */
310 /* Date: 2005/05/05 14:38:47 */