1*0Sstevel@tonic-gate #include <openssl/bn.h>
2*0Sstevel@tonic-gate #include <openssl/rand.h>
3*0Sstevel@tonic-gate
Rand(n)4*0Sstevel@tonic-gate static int Rand(n)
5*0Sstevel@tonic-gate {
6*0Sstevel@tonic-gate unsigned char x[2];
7*0Sstevel@tonic-gate RAND_pseudo_bytes(x,2);
8*0Sstevel@tonic-gate return (x[0] + 2*x[1]);
9*0Sstevel@tonic-gate }
10*0Sstevel@tonic-gate
bug(char * m,BIGNUM * a,BIGNUM * b)11*0Sstevel@tonic-gate static void bug(char *m, BIGNUM *a, BIGNUM *b)
12*0Sstevel@tonic-gate {
13*0Sstevel@tonic-gate printf("%s!\na=",m);
14*0Sstevel@tonic-gate BN_print_fp(stdout, a);
15*0Sstevel@tonic-gate printf("\nb=");
16*0Sstevel@tonic-gate BN_print_fp(stdout, b);
17*0Sstevel@tonic-gate printf("\n");
18*0Sstevel@tonic-gate fflush(stdout);
19*0Sstevel@tonic-gate }
20*0Sstevel@tonic-gate
main()21*0Sstevel@tonic-gate main()
22*0Sstevel@tonic-gate {
23*0Sstevel@tonic-gate BIGNUM *a=BN_new(), *b=BN_new(), *c=BN_new(), *d=BN_new(),
24*0Sstevel@tonic-gate *C=BN_new(), *D=BN_new();
25*0Sstevel@tonic-gate BN_RECP_CTX *recp=BN_RECP_CTX_new();
26*0Sstevel@tonic-gate BN_CTX *ctx=BN_CTX_new();
27*0Sstevel@tonic-gate
28*0Sstevel@tonic-gate for(;;) {
29*0Sstevel@tonic-gate BN_pseudo_rand(a,Rand(),0,0);
30*0Sstevel@tonic-gate BN_pseudo_rand(b,Rand(),0,0);
31*0Sstevel@tonic-gate if (BN_is_zero(b)) continue;
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate BN_RECP_CTX_set(recp,b,ctx);
34*0Sstevel@tonic-gate if (BN_div(C,D,a,b,ctx) != 1)
35*0Sstevel@tonic-gate bug("BN_div failed",a,b);
36*0Sstevel@tonic-gate if (BN_div_recp(c,d,a,recp,ctx) != 1)
37*0Sstevel@tonic-gate bug("BN_div_recp failed",a,b);
38*0Sstevel@tonic-gate else if (BN_cmp(c,C) != 0 || BN_cmp(c,C) != 0)
39*0Sstevel@tonic-gate bug("mismatch",a,b);
40*0Sstevel@tonic-gate }
41*0Sstevel@tonic-gate }
42