Change functions to ANSI C.
[openssl.git] / crypto / bn / bntest.c
index 9ebd68b42954124e4f8dea21d3be3caedd416e17..ab93138de8de62c201eaf25221e4ff5c7a92cf98 100644 (file)
 #endif
 
 #ifndef NOPROTO
-int test_add (BIO *bp);
-int test_sub (BIO *bp);
-int test_lshift1 (BIO *bp);
-int test_lshift (BIO *bp);
-int test_rshift1 (BIO *bp);
-int test_rshift (BIO *bp);
-int test_div (BIO *bp,BN_CTX *ctx);
-int test_mul (BIO *bp);
-int test_sqr (BIO *bp,BN_CTX *ctx);
-int test_mont (BIO *bp,BN_CTX *ctx);
-int test_mod (BIO *bp,BN_CTX *ctx);
-int test_mod_mul (BIO *bp,BN_CTX *ctx);
-int test_mod_exp (BIO *bp,BN_CTX *ctx);
+int test_add(BIO *bp);
+int test_sub(BIO *bp);
+int test_lshift1(BIO *bp);
+int test_lshift(BIO *bp);
+int test_rshift1(BIO *bp);
+int test_rshift(BIO *bp);
+int test_div(BIO *bp,BN_CTX *ctx);
+int test_div_recp(BIO *bp,BN_CTX *ctx);
+int test_mul(BIO *bp);
+int test_sqr(BIO *bp,BN_CTX *ctx);
+int test_mont(BIO *bp,BN_CTX *ctx);
+int test_mod(BIO *bp,BN_CTX *ctx);
+int test_mod_mul(BIO *bp,BN_CTX *ctx);
+int test_mod_exp(BIO *bp,BN_CTX *ctx);
+int test_exp(BIO *bp,BN_CTX *ctx);
 int rand_neg(void);
 #else
 int test_add ();
@@ -99,6 +101,7 @@ int test_mont ();
 int test_mod ();
 int test_mod_mul ();
 int test_mod_exp ();
+int test_exp ();
 int rand_neg();
 #endif
 
@@ -109,9 +112,7 @@ static int results=0;
 #include "bss_file.c"
 #endif
 
-int main(argc,argv)
-int argc;
-char *argv[];
+int main(int argc, char *argv[])
        {
        BN_CTX *ctx;
        BIO *out;
@@ -192,6 +193,10 @@ char *argv[];
        if (!test_div(out,ctx)) goto err;
        fflush(stdout);
 
+       fprintf(stderr,"test BN_div_recp\n");
+       if (!test_div_recp(out,ctx)) goto err;
+       fflush(stdout);
+
        fprintf(stderr,"test BN_mod\n");
        if (!test_mod(out,ctx)) goto err;
        fflush(stdout);
@@ -209,6 +214,10 @@ char *argv[];
        if (!test_mod_exp(out,ctx)) goto err;
        fflush(stdout);
 
+       fprintf(stderr,"test BN_exp\n");
+       if (!test_exp(out,ctx)) goto err;
+       fflush(stdout);
+
 /**/
        exit(0);
 err:
@@ -218,281 +227,330 @@ err:
        return(1);
        }
 
-int test_add(bp)
-BIO *bp;
+int test_add(BIO *bp)
        {
-       BIGNUM *a,*b,*c;
+       BIGNUM a,b,c;
        int i;
        int j;
 
-       a=BN_new();
-       b=BN_new();
-       c=BN_new();
+       BN_init(&a);
+       BN_init(&b);
+       BN_init(&c);
 
-       BN_rand(a,512,0,0);
+       BN_rand(&a,512,0,0);
        for (i=0; i<100; i++)
                {
-               BN_rand(b,450+i,0,0);
-               a->neg=rand_neg();
-               b->neg=rand_neg();
+               BN_rand(&b,450+i,0,0);
+               a.neg=rand_neg();
+               b.neg=rand_neg();
                if (bp == NULL)
                        for (j=0; j<10000; j++)
-                               BN_add(c,a,b);
-               BN_add(c,a,b);
+                               BN_add(&c,&a,&b);
+               BN_add(&c,&a,&b);
                if (bp != NULL)
                        {
                        if (!results)
                                {
-                               BN_print(bp,a);
+                               BN_print(bp,&a);
                                BIO_puts(bp," + ");
-                               BN_print(bp,b);
+                               BN_print(bp,&b);
                                BIO_puts(bp," - ");
                                }
-                       BN_print(bp,c);
+                       BN_print(bp,&c);
                        BIO_puts(bp,"\n");
                        }
                }
-       BN_free(a);
-       BN_free(b);
-       BN_free(c);
+       BN_free(&a);
+       BN_free(&b);
+       BN_free(&c);
        return(1);
        }
 
-int test_sub(bp)
-BIO *bp;
+int test_sub(BIO *bp)
        {
-       BIGNUM *a,*b,*c;
+       BIGNUM a,b,c;
        int i;
        int j;
 
-       a=BN_new();
-       b=BN_new();
-       c=BN_new();
+       BN_init(&a);
+       BN_init(&b);
+       BN_init(&c);
 
-       BN_rand(a,512,0,0);
+       BN_rand(&a,512,0,0);
        for (i=0; i<100; i++)
                {
-               BN_rand(b,400+i,0,0);
-               a->neg=rand_neg();
-               b->neg=rand_neg();
+               BN_rand(&b,400+i,0,0);
+               a.neg=rand_neg();
+               b.neg=rand_neg();
                if (bp == NULL)
                        for (j=0; j<10000; j++)
-                               BN_sub(c,a,b);
-               BN_sub(c,a,b);
+                               BN_sub(&c,&a,&b);
+               BN_sub(&c,&a,&b);
                if (bp != NULL)
                        {
                        if (!results)
                                {
-                               BN_print(bp,a);
+                               BN_print(bp,&a);
                                BIO_puts(bp," - ");
-                               BN_print(bp,b);
+                               BN_print(bp,&b);
                                BIO_puts(bp," - ");
                                }
-                       BN_print(bp,c);
+                       BN_print(bp,&c);
                        BIO_puts(bp,"\n");
                        }
                }
-       BN_free(a);
-       BN_free(b);
-       BN_free(c);
+       BN_free(&a);
+       BN_free(&b);
+       BN_free(&c);
        return(1);
        }
 
-int test_div(bp,ctx)
-BIO *bp;
-BN_CTX *ctx;
+int test_div(BIO *bp, BN_CTX *ctx)
        {
-       BIGNUM *a,*b,*c,*d;
+       BIGNUM a,b,c,d;
        int i;
        int j;
 
-       a=BN_new();
-       b=BN_new();
-       c=BN_new();
-       d=BN_new();
+       BN_init(&a);
+       BN_init(&b);
+       BN_init(&c);
+       BN_init(&d);
 
-       BN_rand(a,400,0,0);
+       BN_rand(&a,400,0,0);
        for (i=0; i<100; i++)
                {
-               BN_rand(b,50+i,0,0);
-               a->neg=rand_neg();
-               b->neg=rand_neg();
+               BN_rand(&b,50+i,0,0);
+               a.neg=rand_neg();
+               b.neg=rand_neg();
                if (bp == NULL)
                        for (j=0; j<100; j++)
-                               BN_div(d,c,a,b,ctx);
-               BN_div(d,c,a,b,ctx);
+                               BN_div(&d,&c,&a,&b,ctx);
+               BN_div(&d,&c,&a,&b,ctx);
                if (bp != NULL)
                        {
                        if (!results)
                                {
-                               BN_print(bp,a);
+                               BN_print(bp,&a);
                                BIO_puts(bp," / ");
-                               BN_print(bp,b);
+                               BN_print(bp,&b);
                                BIO_puts(bp," - ");
                                }
-                       BN_print(bp,d);
+                       BN_print(bp,&d);
                        BIO_puts(bp,"\n");
 
                        if (!results)
                                {
-                               BN_print(bp,a);
+                               BN_print(bp,&a);
                                BIO_puts(bp," % ");
-                               BN_print(bp,b);
+                               BN_print(bp,&b);
                                BIO_puts(bp," - ");
                                }
-                       BN_print(bp,c);
+                       BN_print(bp,&c);
                        BIO_puts(bp,"\n");
                        }
                }
-       BN_free(a);
-       BN_free(b);
-       BN_free(c);
-       BN_free(d);
+       BN_free(&a);
+       BN_free(&b);
+       BN_free(&c);
+       BN_free(&d);
        return(1);
        }
 
-int test_mul(bp)
-BIO *bp;
+int test_div_recp(BIO *bp, BN_CTX *ctx)
        {
-       BIGNUM *a,*b,*c;
+       BIGNUM a,b,c,d;
+       BN_RECP_CTX recp;
        int i;
        int j;
 
-       a=BN_new();
-       b=BN_new();
-       c=BN_new();
+       BN_RECP_CTX_init(&recp);
+       BN_init(&a);
+       BN_init(&b);
+       BN_init(&c);
+       BN_init(&d);
 
-       BN_rand(a,200,0,0);
+       BN_rand(&a,400,0,0);
        for (i=0; i<100; i++)
                {
-               BN_rand(b,250+i,0,0);
-               a->neg=rand_neg();
-               b->neg=rand_neg();
+               BN_rand(&b,50+i,0,0);
+               a.neg=rand_neg();
+               b.neg=rand_neg();
+               BN_RECP_CTX_set(&recp,&b,ctx);
                if (bp == NULL)
                        for (j=0; j<100; j++)
-                               BN_mul(c,a,b);
-               BN_mul(c,a,b);
+                               BN_div_recp(&d,&c,&a,&recp,ctx);
+               BN_div_recp(&d,&c,&a,&recp,ctx);
                if (bp != NULL)
                        {
                        if (!results)
                                {
-                               BN_print(bp,a);
+                               BN_print(bp,&a);
+                               BIO_puts(bp," / ");
+                               BN_print(bp,&b);
+                               BIO_puts(bp," - ");
+                               }
+                       BN_print(bp,&d);
+                       BIO_puts(bp,"\n");
+
+                       if (!results)
+                               {
+                               BN_print(bp,&a);
+                               BIO_puts(bp," % ");
+                               BN_print(bp,&b);
+                               BIO_puts(bp," - ");
+                               }
+                       BN_print(bp,&c);
+                       BIO_puts(bp,"\n");
+                       }
+               }
+       BN_free(&a);
+       BN_free(&b);
+       BN_free(&c);
+       BN_free(&d);
+       BN_RECP_CTX_free(&recp);
+       return(1);
+       }
+
+int test_mul(BIO *bp)
+       {
+       BIGNUM a,b,c;
+       int i;
+       int j;
+       BN_CTX ctx;
+
+       BN_CTX_init(&ctx);
+       BN_init(&a);
+       BN_init(&b);
+       BN_init(&c);
+
+       BN_rand(&a,200,0,0);
+       for (i=0; i<100; i++)
+               {
+               BN_rand(&b,250+i,0,0);
+               BN_rand(&b,200,0,0);
+               a.neg=rand_neg();
+               b.neg=rand_neg();
+               if (bp == NULL)
+                       for (j=0; j<100; j++)
+                               BN_mul(&c,&a,&b,&ctx);
+               BN_mul(&c,&a,&b,&ctx);
+/*bn_do(&c,&a,&b,ctx); */
+               if (bp != NULL)
+                       {
+                       if (!results)
+                               {
+                               BN_print(bp,&a);
                                BIO_puts(bp," * ");
-                               BN_print(bp,b);
+                               BN_print(bp,&b);
                                BIO_puts(bp," - ");
                                }
-                       BN_print(bp,c);
+                       BN_print(bp,&c);
                        BIO_puts(bp,"\n");
                        }
                }
-       BN_free(a);
-       BN_free(b);
-       BN_free(c);
+       BN_free(&a);
+       BN_free(&b);
+       BN_free(&c);
+       BN_CTX_free(&ctx);
        return(1);
        }
 
-int test_sqr(bp,ctx)
-BIO *bp;
-BN_CTX *ctx;
+int test_sqr(BIO *bp, BN_CTX *ctx)
        {
-       BIGNUM *a,*c;
+       BIGNUM a,c;
        int i;
        int j;
 
-       a=BN_new();
-       c=BN_new();
+       BN_init(&a);
+       BN_init(&c);
 
        for (i=0; i<40; i++)
                {
-               BN_rand(a,40+i*10,0,0);
-               a->neg=rand_neg();
+               BN_rand(&a,40+i*10,0,0);
+               a.neg=rand_neg();
                if (bp == NULL)
                        for (j=0; j<100; j++)
-                               BN_sqr(c,a,ctx);
-               BN_sqr(c,a,ctx);
+                               BN_sqr(&c,&a,ctx);
+               BN_sqr(&c,&a,ctx);
                if (bp != NULL)
                        {
                        if (!results)
                                {
-                               BN_print(bp,a);
+                               BN_print(bp,&a);
                                BIO_puts(bp," * ");
-                               BN_print(bp,a);
+                               BN_print(bp,&a);
                                BIO_puts(bp," - ");
                                }
-                       BN_print(bp,c);
+                       BN_print(bp,&c);
                        BIO_puts(bp,"\n");
                        }
                }
-       BN_free(a);
-       BN_free(c);
+       BN_free(&a);
+       BN_free(&c);
        return(1);
        }
 
-int test_mont(bp,ctx)
-BIO *bp;
-BN_CTX *ctx;
+int test_mont(BIO *bp, BN_CTX *ctx)
        {
-       BIGNUM *a,*b,*c,*A,*B;
-       BIGNUM *n;
+       BIGNUM a,b,c,A,B;
+       BIGNUM n;
        int i;
        int j;
        BN_MONT_CTX *mont;
 
-       a=BN_new();
-       b=BN_new();
-       c=BN_new();
-       A=BN_new();
-       B=BN_new();
-       n=BN_new();
+       BN_init(&a);
+       BN_init(&b);
+       BN_init(&c);
+       BN_init(&A);
+       BN_init(&B);
+       BN_init(&n);
 
        mont=BN_MONT_CTX_new();
 
-       BN_rand(a,100,0,0); /**/
-       BN_rand(b,100,0,0); /**/
+       BN_rand(&a,100,0,0); /**/
+       BN_rand(&b,100,0,0); /**/
        for (i=0; i<10; i++)
                {
-               BN_rand(n,(100%BN_BITS2+1)*BN_BITS2*i*BN_BITS2,0,1); /**/
-               BN_MONT_CTX_set(mont,n,ctx);
+               BN_rand(&n,(100%BN_BITS2+1)*BN_BITS2*i*BN_BITS2,0,1); /**/
+               BN_MONT_CTX_set(mont,&n,ctx);
 
-               BN_to_montgomery(A,a,mont,ctx);
-               BN_to_montgomery(B,b,mont,ctx);
+               BN_to_montgomery(&A,&a,mont,ctx);
+               BN_to_montgomery(&B,&b,mont,ctx);
 
                if (bp == NULL)
                        for (j=0; j<100; j++)
-                               BN_mod_mul_montgomery(c,A,B,mont,ctx);/**/
-               BN_mod_mul_montgomery(c,A,B,mont,ctx);/**/
-               BN_from_montgomery(A,c,mont,ctx);/**/
+                               BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/
+               BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/
+               BN_from_montgomery(&A,&c,mont,ctx);/**/
                if (bp != NULL)
                        {
                        if (!results)
                                {
 #ifdef undef
 fprintf(stderr,"%d * %d %% %d\n",
-BN_num_bits(a),
-BN_num_bits(b),
+BN_num_bits(&a),
+BN_num_bits(&b),
 BN_num_bits(mont->N));
 #endif
-                               BN_print(bp,a);
+                               BN_print(bp,&a);
                                BIO_puts(bp," * ");
-                               BN_print(bp,b);
+                               BN_print(bp,&b);
                                BIO_puts(bp," % ");
-                               BN_print(bp,mont->N);
+                               BN_print(bp,&(mont->N));
                                BIO_puts(bp," - ");
                                }
-                       BN_print(bp,A);
+                       BN_print(bp,&A);
                        BIO_puts(bp,"\n");
                        }
                }
        BN_MONT_CTX_free(mont);
-       BN_free(a);
-       BN_free(b);
-       BN_free(c);
+       BN_free(&a);
+       BN_free(&b);
+       BN_free(&c);
        return(1);
        }
 
-int test_mod(bp,ctx)
-BIO *bp;
-BN_CTX *ctx;
+int test_mod(BIO *bp, BN_CTX *ctx)
        {
        BIGNUM *a,*b,*c;
        int i;
@@ -531,9 +589,7 @@ BN_CTX *ctx;
        return(1);
        }
 
-int test_mod_mul(bp,ctx)
-BIO *bp;
-BN_CTX *ctx;
+int test_mod_mul(BIO *bp, BN_CTX *ctx)
        {
        BIGNUM *a,*b,*c,*d,*e;
        int i;
@@ -587,9 +643,7 @@ BN_CTX *ctx;
        return(1);
        }
 
-int test_mod_exp(bp,ctx)
-BIO *bp;
-BN_CTX *ctx;
+int test_mod_exp(BIO *bp, BN_CTX *ctx)
        {
        BIGNUM *a,*b,*c,*d,*e;
        int i;
@@ -632,8 +686,45 @@ BN_CTX *ctx;
        return(1);
        }
 
-int test_lshift(bp)
-BIO *bp;
+int test_exp(BIO *bp, BN_CTX *ctx)
+       {
+       BIGNUM *a,*b,*d,*e;
+       int i;
+
+       a=BN_new();
+       b=BN_new();
+       d=BN_new();
+       e=BN_new();
+
+       for (i=0; i<6; i++)
+               {
+               BN_rand(a,20+i*5,0,0); /**/
+               BN_rand(b,2+i,0,0); /**/
+
+               if (!BN_exp(d,a,b,ctx))
+                       return(00);
+
+               if (bp != NULL)
+                       {
+                       if (!results)
+                               {
+                               BN_print(bp,a);
+                               BIO_puts(bp," ^ ");
+                               BN_print(bp,b);
+                               BIO_puts(bp," - ");
+                               }
+                       BN_print(bp,d);
+                       BIO_puts(bp,"\n");
+                       }
+               }
+       BN_free(a);
+       BN_free(b);
+       BN_free(d);
+       BN_free(e);
+       return(1);
+       }
+
+int test_lshift(BIO *bp)
        {
        BIGNUM *a,*b,*c;
        int i;
@@ -668,8 +759,7 @@ BIO *bp;
        return(1);
        }
 
-int test_lshift1(bp)
-BIO *bp;
+int test_lshift1(BIO *bp)
        {
        BIGNUM *a,*b;
        int i;
@@ -700,8 +790,7 @@ BIO *bp;
        return(1);
        }
 
-int test_rshift(bp)
-BIO *bp;
+int test_rshift(BIO *bp)
        {
        BIGNUM *a,*b,*c;
        int i;
@@ -736,8 +825,7 @@ BIO *bp;
        return(1);
        }
 
-int test_rshift1(bp)
-BIO *bp;
+int test_rshift1(BIO *bp)
        {
        BIGNUM *a,*b;
        int i;
@@ -768,7 +856,7 @@ BIO *bp;
        return(1);
        }
 
-int rand_neg()
+int rand_neg(void)
        {
        static unsigned int neg=0;
        static int sign[8]={0,0,0,1,1,0,1,1};