It was a very bad idea to use #include "../e_os.h" -- when this occurs
[openssl.git] / crypto / bn / bntest.c
1 /* crypto/bn/bntest.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62
63 #include "openssl/e_os.h"
64
65 #include <openssl/bio.h>
66 #include <openssl/bn.h>
67 #include <openssl/rand.h>
68 #include <openssl/x509.h>
69 #include <openssl/err.h>
70
71 #ifdef WINDOWS
72 #include "../bio/bss_file.c"
73 #endif
74
75 int test_add(BIO *bp);
76 int test_sub(BIO *bp);
77 int test_lshift1(BIO *bp);
78 int test_lshift(BIO *bp,BN_CTX *ctx);
79 int test_rshift1(BIO *bp);
80 int test_rshift(BIO *bp,BN_CTX *ctx);
81 int test_div(BIO *bp,BN_CTX *ctx);
82 int test_div_recp(BIO *bp,BN_CTX *ctx);
83 int test_mul(BIO *bp);
84 int test_sqr(BIO *bp,BN_CTX *ctx);
85 int test_mont(BIO *bp,BN_CTX *ctx);
86 int test_mod(BIO *bp,BN_CTX *ctx);
87 int test_mod_mul(BIO *bp,BN_CTX *ctx);
88 int test_mod_exp(BIO *bp,BN_CTX *ctx);
89 int test_exp(BIO *bp,BN_CTX *ctx);
90 int rand_neg(void);
91 static int results=0;
92
93 #ifdef NO_STDIO
94 #define APPS_WIN16
95 #include "bss_file.c"
96 #endif
97
98 int main(int argc, char *argv[])
99         {
100         BN_CTX *ctx;
101         BIO *out;
102         char *outfile=NULL;
103
104         srand((unsigned int)time(NULL));
105
106         argc--;
107         argv++;
108         while (argc >= 1)
109                 {
110                 if (strcmp(*argv,"-results") == 0)
111                         results=1;
112                 else if (strcmp(*argv,"-out") == 0)
113                         {
114                         if (--argc < 1) break;
115                         outfile= *(++argv);
116                         }
117                 argc--;
118                 argv++;
119                 }
120
121
122         ctx=BN_CTX_new();
123         if (ctx == NULL) exit(1);
124
125         out=BIO_new(BIO_s_file());
126         if (out == NULL) exit(1);
127         if (outfile == NULL)
128                 {
129                 BIO_set_fp(out,stdout,BIO_NOCLOSE);
130                 }
131         else
132                 {
133                 if (!BIO_write_filename(out,outfile))
134                         {
135                         perror(outfile);
136                         exit(1);
137                         }
138                 }
139
140         if (!results)
141                 BIO_puts(out,"obase=16\nibase=16\n");
142
143         fprintf(stderr,"test BN_add\n");
144         if (!test_add(out)) goto err;
145         fflush(stdout);
146
147         fprintf(stderr,"test BN_sub\n");
148         if (!test_sub(out)) goto err;
149         fflush(stdout);
150
151         fprintf(stderr,"test BN_lshift1\n");
152         if (!test_lshift1(out)) goto err;
153         fflush(stdout);
154
155         fprintf(stderr,"test BN_lshift\n");
156         if (!test_lshift(out,ctx)) goto err;
157         fflush(stdout);
158
159         fprintf(stderr,"test BN_rshift1\n");
160         if (!test_rshift1(out)) goto err;
161         fflush(stdout);
162
163         fprintf(stderr,"test BN_rshift\n");
164         if (!test_rshift(out,ctx)) goto err;
165         fflush(stdout);
166
167         fprintf(stderr,"test BN_sqr\n");
168         if (!test_sqr(out,ctx)) goto err;
169         fflush(stdout);
170
171         fprintf(stderr,"test BN_mul\n");
172         if (!test_mul(out)) goto err;
173         fflush(stdout);
174
175         fprintf(stderr,"test BN_div\n");
176         if (!test_div(out,ctx)) goto err;
177         fflush(stdout);
178
179         fprintf(stderr,"test BN_div_recp\n");
180         if (!test_div_recp(out,ctx)) goto err;
181         fflush(stdout);
182
183         fprintf(stderr,"test BN_mod\n");
184         if (!test_mod(out,ctx)) goto err;
185         fflush(stdout);
186
187         fprintf(stderr,"test BN_mod_mul\n");
188         if (!test_mod_mul(out,ctx)) goto err;
189         fflush(stdout);
190
191 /*
192         fprintf(stderr,"test BN_mont\n");
193         if (!test_mont(out,ctx)) goto err;
194         fflush(stdout);
195 */
196         fprintf(stderr,"test BN_mod_exp\n");
197         if (!test_mod_exp(out,ctx)) goto err;
198         fflush(stdout);
199
200         fprintf(stderr,"test BN_exp\n");
201         if (!test_exp(out,ctx)) goto err;
202         fflush(stdout);
203
204 /**/
205         exit(0);
206 err:
207         ERR_load_crypto_strings();
208         ERR_print_errors(out);
209         exit(1);
210         return(1);
211         }
212
213 int test_add(BIO *bp)
214         {
215         BIGNUM a,b,c;
216         int i;
217         int j;
218
219         BN_init(&a);
220         BN_init(&b);
221         BN_init(&c);
222
223         BN_rand(&a,512,0,0);
224         for (i=0; i<100; i++)
225                 {
226                 BN_rand(&b,450+i,0,0);
227                 a.neg=rand_neg();
228                 b.neg=rand_neg();
229                 if (bp == NULL)
230                         for (j=0; j<10000; j++)
231                                 BN_add(&c,&a,&b);
232                 BN_add(&c,&a,&b);
233                 if (bp != NULL)
234                         {
235                         if (!results)
236                                 {
237                                 BN_print(bp,&a);
238                                 BIO_puts(bp," + ");
239                                 BN_print(bp,&b);
240                                 BIO_puts(bp," - ");
241                                 }
242                         BN_print(bp,&c);
243                         BIO_puts(bp,"\n");
244                         }
245                 a.neg=!a.neg;
246                 b.neg=!b.neg;
247                 BN_add(&c,&c,&b);
248                 BN_add(&c,&c,&a);
249                 if(!BN_is_zero(&c))
250                     {
251                     BIO_puts(bp,"Add test failed!\n");
252                     return 0;
253                     }
254                 }
255         BN_free(&a);
256         BN_free(&b);
257         BN_free(&c);
258         return(1);
259         }
260
261 int test_sub(BIO *bp)
262         {
263         BIGNUM a,b,c;
264         int i;
265         int j;
266
267         BN_init(&a);
268         BN_init(&b);
269         BN_init(&c);
270
271         BN_rand(&a,512,0,0);
272         for (i=0; i<100; i++)
273                 {
274                 BN_rand(&b,400+i,0,0);
275                 a.neg=rand_neg();
276                 b.neg=rand_neg();
277                 if (bp == NULL)
278                         for (j=0; j<10000; j++)
279                                 BN_sub(&c,&a,&b);
280                 BN_sub(&c,&a,&b);
281                 if (bp != NULL)
282                         {
283                         if (!results)
284                                 {
285                                 BN_print(bp,&a);
286                                 BIO_puts(bp," - ");
287                                 BN_print(bp,&b);
288                                 BIO_puts(bp," - ");
289                                 }
290                         BN_print(bp,&c);
291                         BIO_puts(bp,"\n");
292                         }
293                 BN_add(&c,&c,&b);
294                 BN_sub(&c,&c,&a);
295                 if(!BN_is_zero(&c))
296                     {
297                     BIO_puts(bp,"Subtract test failed!\n");
298                     return 0;
299                     }
300                 }
301         BN_free(&a);
302         BN_free(&b);
303         BN_free(&c);
304         return(1);
305         }
306
307 int test_div(BIO *bp, BN_CTX *ctx)
308         {
309         BIGNUM a,b,c,d,e;
310         int i;
311         int j;
312
313         BN_init(&a);
314         BN_init(&b);
315         BN_init(&c);
316         BN_init(&d);
317         BN_init(&e);
318
319         BN_rand(&a,400,0,0);
320         for (i=0; i<100; i++)
321                 {
322                 BN_rand(&b,50+i,0,0);
323                 a.neg=rand_neg();
324                 b.neg=rand_neg();
325                 if (bp == NULL)
326                         for (j=0; j<100; j++)
327                                 BN_div(&d,&c,&a,&b,ctx);
328                 BN_div(&d,&c,&a,&b,ctx);
329                 if (bp != NULL)
330                         {
331                         if (!results)
332                                 {
333                                 BN_print(bp,&a);
334                                 BIO_puts(bp," / ");
335                                 BN_print(bp,&b);
336                                 BIO_puts(bp," - ");
337                                 }
338                         BN_print(bp,&d);
339                         BIO_puts(bp,"\n");
340
341                         if (!results)
342                                 {
343                                 BN_print(bp,&a);
344                                 BIO_puts(bp," % ");
345                                 BN_print(bp,&b);
346                                 BIO_puts(bp," - ");
347                                 }
348                         BN_print(bp,&c);
349                         BIO_puts(bp,"\n");
350                         }
351                 BN_mul(&e,&d,&b,ctx);
352                 BN_add(&d,&e,&c);
353                 BN_sub(&d,&d,&a);
354                 if(!BN_is_zero(&d))
355                     {
356                     BIO_puts(bp,"Division test failed!\n");
357                     return 0;
358                     }
359                 }
360         BN_free(&a);
361         BN_free(&b);
362         BN_free(&c);
363         BN_free(&d);
364         BN_free(&e);
365         return(1);
366         }
367
368 int test_div_recp(BIO *bp, BN_CTX *ctx)
369         {
370         BIGNUM a,b,c,d,e;
371         BN_RECP_CTX recp;
372         int i;
373         int j;
374
375         BN_RECP_CTX_init(&recp);
376         BN_init(&a);
377         BN_init(&b);
378         BN_init(&c);
379         BN_init(&d);
380         BN_init(&e);
381
382         BN_rand(&a,400,0,0);
383         for (i=0; i<100; i++)
384                 {
385                 BN_rand(&b,50+i,0,0);
386                 a.neg=rand_neg();
387                 b.neg=rand_neg();
388                 BN_RECP_CTX_set(&recp,&b,ctx);
389                 if (bp == NULL)
390                         for (j=0; j<100; j++)
391                                 BN_div_recp(&d,&c,&a,&recp,ctx);
392                 BN_div_recp(&d,&c,&a,&recp,ctx);
393                 if (bp != NULL)
394                         {
395                         if (!results)
396                                 {
397                                 BN_print(bp,&a);
398                                 BIO_puts(bp," / ");
399                                 BN_print(bp,&b);
400                                 BIO_puts(bp," - ");
401                                 }
402                         BN_print(bp,&d);
403                         BIO_puts(bp,"\n");
404
405                         if (!results)
406                                 {
407                                 BN_print(bp,&a);
408                                 BIO_puts(bp," % ");
409                                 BN_print(bp,&b);
410                                 BIO_puts(bp," - ");
411                                 }
412                         BN_print(bp,&c);
413                         BIO_puts(bp,"\n");
414                         }
415                 BN_mul(&e,&d,&b,ctx);
416                 BN_add(&d,&e,&c);
417                 BN_sub(&d,&d,&a);
418                 if(!BN_is_zero(&d))
419                     {
420                     BIO_puts(bp,"Reciprocal division test failed!\n");
421                     return 0;
422                     }
423                 }
424         BN_free(&a);
425         BN_free(&b);
426         BN_free(&c);
427         BN_free(&d);
428         BN_free(&e);
429         BN_RECP_CTX_free(&recp);
430         return(1);
431         }
432
433 int test_mul(BIO *bp)
434         {
435         BIGNUM a,b,c,d,e;
436         int i;
437         int j;
438         BN_CTX ctx;
439
440         BN_CTX_init(&ctx);
441         BN_init(&a);
442         BN_init(&b);
443         BN_init(&c);
444         BN_init(&d);
445         BN_init(&e);
446
447         BN_rand(&a,200,0,0);
448         for (i=0; i<100; i++)
449                 {
450                 BN_rand(&b,250+i,0,0);
451                 BN_rand(&b,200,0,0);
452                 a.neg=rand_neg();
453                 b.neg=rand_neg();
454                 if (bp == NULL)
455                         for (j=0; j<100; j++)
456                                 BN_mul(&c,&a,&b,&ctx);
457                 BN_mul(&c,&a,&b,&ctx);
458                 if (bp != NULL)
459                         {
460                         if (!results)
461                                 {
462                                 BN_print(bp,&a);
463                                 BIO_puts(bp," * ");
464                                 BN_print(bp,&b);
465                                 BIO_puts(bp," - ");
466                                 }
467                         BN_print(bp,&c);
468                         BIO_puts(bp,"\n");
469                         }
470                 BN_div(&d,&e,&c,&a,&ctx);
471                 BN_sub(&d,&d,&b);
472                 if(!BN_is_zero(&d) || !BN_is_zero(&e))
473                     {
474                     BIO_puts(bp,"Multiplication test failed!\n");
475                     return 0;
476                     }
477                 }
478         BN_free(&a);
479         BN_free(&b);
480         BN_free(&c);
481         BN_free(&d);
482         BN_free(&e);
483         BN_CTX_free(&ctx);
484         return(1);
485         }
486
487 int test_sqr(BIO *bp, BN_CTX *ctx)
488         {
489         BIGNUM a,c,d,e;
490         int i;
491         int j;
492
493         BN_init(&a);
494         BN_init(&c);
495         BN_init(&d);
496         BN_init(&e);
497
498         for (i=0; i<40; i++)
499                 {
500                 BN_rand(&a,40+i*10,0,0);
501                 a.neg=rand_neg();
502                 if (bp == NULL)
503                         for (j=0; j<100; j++)
504                                 BN_sqr(&c,&a,ctx);
505                 BN_sqr(&c,&a,ctx);
506                 if (bp != NULL)
507                         {
508                         if (!results)
509                                 {
510                                 BN_print(bp,&a);
511                                 BIO_puts(bp," * ");
512                                 BN_print(bp,&a);
513                                 BIO_puts(bp," - ");
514                                 }
515                         BN_print(bp,&c);
516                         BIO_puts(bp,"\n");
517                         }
518                 BN_div(&d,&e,&c,&a,ctx);
519                 BN_sub(&d,&d,&a);
520                 if(!BN_is_zero(&d) || !BN_is_zero(&e))
521                     {
522                     BIO_puts(bp,"Square test failed!\n");
523                     return 0;
524                     }
525                 }
526         BN_free(&a);
527         BN_free(&c);
528         BN_free(&d);
529         BN_free(&e);
530         return(1);
531         }
532
533 int test_mont(BIO *bp, BN_CTX *ctx)
534         {
535         BIGNUM a,b,c,d,A,B;
536         BIGNUM n;
537         int i;
538         int j;
539         BN_MONT_CTX *mont;
540
541         BN_init(&a);
542         BN_init(&b);
543         BN_init(&c);
544         BN_init(&d);
545         BN_init(&A);
546         BN_init(&B);
547         BN_init(&n);
548
549         mont=BN_MONT_CTX_new();
550
551         BN_rand(&a,100,0,0); /**/
552         BN_rand(&b,100,0,0); /**/
553         for (i=0; i<10; i++)
554                 {
555                 BN_rand(&n,(100%BN_BITS2+1)*BN_BITS2*i*BN_BITS2,0,1); /**/
556                 BN_MONT_CTX_set(mont,&n,ctx);
557
558                 BN_to_montgomery(&A,&a,mont,ctx);
559                 BN_to_montgomery(&B,&b,mont,ctx);
560
561                 if (bp == NULL)
562                         for (j=0; j<100; j++)
563                                 BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/
564                 BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/
565                 BN_from_montgomery(&A,&c,mont,ctx);/**/
566                 if (bp != NULL)
567                         {
568                         if (!results)
569                                 {
570 #ifdef undef
571 fprintf(stderr,"%d * %d %% %d\n",
572 BN_num_bits(&a),
573 BN_num_bits(&b),
574 BN_num_bits(mont->N));
575 #endif
576                                 BN_print(bp,&a);
577                                 BIO_puts(bp," * ");
578                                 BN_print(bp,&b);
579                                 BIO_puts(bp," % ");
580                                 BN_print(bp,&(mont->N));
581                                 BIO_puts(bp," - ");
582                                 }
583                         BN_print(bp,&A);
584                         BIO_puts(bp,"\n");
585                         }
586                 BN_mod_mul(&d,&a,&b,&n,ctx);
587                 BN_sub(&d,&d,&A);
588                 if(!BN_is_zero(&d))
589                     {
590                     BIO_puts(bp,"Montgomery multiplication test failed!\n");
591                     return 0;
592                     }
593                 }
594         BN_MONT_CTX_free(mont);
595         BN_free(&a);
596         BN_free(&b);
597         BN_free(&c);
598         BN_free(&d);
599         BN_free(&A);
600         BN_free(&B);
601         BN_free(&n);
602         return(1);
603         }
604
605 int test_mod(BIO *bp, BN_CTX *ctx)
606         {
607         BIGNUM *a,*b,*c,*d,*e;
608         int i;
609         int j;
610
611         a=BN_new();
612         b=BN_new();
613         c=BN_new();
614         d=BN_new();
615         e=BN_new();
616
617         BN_rand(a,1024,0,0); /**/
618         for (i=0; i<20; i++)
619                 {
620                 BN_rand(b,450+i*10,0,0); /**/
621                 a->neg=rand_neg();
622                 b->neg=rand_neg();
623                 if (bp == NULL)
624                         for (j=0; j<100; j++)
625                                 BN_mod(c,a,b,ctx);/**/
626                 BN_mod(c,a,b,ctx);/**/
627                 if (bp != NULL)
628                         {
629                         if (!results)
630                                 {
631                                 BN_print(bp,a);
632                                 BIO_puts(bp," % ");
633                                 BN_print(bp,b);
634                                 BIO_puts(bp," - ");
635                                 }
636                         BN_print(bp,c);
637                         BIO_puts(bp,"\n");
638                         }
639                 BN_div(d,e,a,b,ctx);
640                 BN_sub(e,e,c);
641                 if(!BN_is_zero(e))
642                     {
643                     BIO_puts(bp,"Modulo test failed!\n");
644                     return 0;
645                     }
646                 }
647         BN_free(a);
648         BN_free(b);
649         BN_free(c);
650         BN_free(d);
651         BN_free(e);
652         return(1);
653         }
654
655 int test_mod_mul(BIO *bp, BN_CTX *ctx)
656         {
657         BIGNUM *a,*b,*c,*d,*e;
658         int i;
659
660         a=BN_new();
661         b=BN_new();
662         c=BN_new();
663         d=BN_new();
664         e=BN_new();
665
666         BN_rand(c,1024,0,0); /**/
667         for (i=0; i<10; i++)
668                 {
669                 BN_rand(a,475+i*10,0,0); /**/
670                 BN_rand(b,425+i*10,0,0); /**/
671                 a->neg=rand_neg();
672                 b->neg=rand_neg();
673         /*      if (bp == NULL)
674                         for (j=0; j<100; j++)
675                                 BN_mod_mul(d,a,b,c,ctx);*/ /**/
676
677                 if (!BN_mod_mul(e,a,b,c,ctx))
678                         {
679                         unsigned long l;
680
681                         while ((l=ERR_get_error()))
682                                 fprintf(stderr,"ERROR:%s\n",
683                                         ERR_error_string(l,NULL));
684                         exit(1);
685                         }
686                 if (bp != NULL)
687                         {
688                         if (!results)
689                                 {
690                                 BN_print(bp,a);
691                                 BIO_puts(bp," * ");
692                                 BN_print(bp,b);
693                                 BIO_puts(bp," % ");
694                                 BN_print(bp,c);
695                                 BIO_puts(bp," - ");
696                                 }
697                         BN_print(bp,e);
698                         BIO_puts(bp,"\n");
699                         }
700                 BN_mul(d,a,b,ctx);
701                 BN_sub(d,d,e);
702                 BN_div(a,b,d,c,ctx);
703                 if(!BN_is_zero(b))
704                     {
705                     BIO_puts(bp,"Modulo multiply test failed!\n");
706                     return 0;
707                     }
708                 }
709         BN_free(a);
710         BN_free(b);
711         BN_free(c);
712         BN_free(d);
713         BN_free(e);
714         return(1);
715         }
716
717 int test_mod_exp(BIO *bp, BN_CTX *ctx)
718         {
719         BIGNUM *a,*b,*c,*d,*e;
720         int i;
721
722         a=BN_new();
723         b=BN_new();
724         c=BN_new();
725         d=BN_new();
726         e=BN_new();
727
728         BN_rand(c,30,0,1); /* must be odd for montgomery */
729         for (i=0; i<6; i++)
730                 {
731                 BN_rand(a,20+i*5,0,0); /**/
732                 BN_rand(b,2+i,0,0); /**/
733
734                 if (!BN_mod_exp(d,a,b,c,ctx))
735                         return(00);
736
737                 if (bp != NULL)
738                         {
739                         if (!results)
740                                 {
741                                 BN_print(bp,a);
742                                 BIO_puts(bp," ^ ");
743                                 BN_print(bp,b);
744                                 BIO_puts(bp," % ");
745                                 BN_print(bp,c);
746                                 BIO_puts(bp," - ");
747                                 }
748                         BN_print(bp,d);
749                         BIO_puts(bp,"\n");
750                         }
751                 BN_exp(e,a,b,ctx);
752                 BN_sub(e,e,d);
753                 BN_div(a,b,e,c,ctx);
754                 if(!BN_is_zero(b))
755                     {
756                     BIO_puts(bp,"Modulo exponentiation test failed!\n");
757                     return 0;
758                     }
759                 }
760         BN_free(a);
761         BN_free(b);
762         BN_free(c);
763         BN_free(d);
764         BN_free(e);
765         return(1);
766         }
767
768 int test_exp(BIO *bp, BN_CTX *ctx)
769         {
770         BIGNUM *a,*b,*d,*e,*one;
771         int i;
772
773         a=BN_new();
774         b=BN_new();
775         d=BN_new();
776         e=BN_new();
777         one=BN_new();
778         BN_one(one);
779
780         for (i=0; i<6; i++)
781                 {
782                 BN_rand(a,20+i*5,0,0); /**/
783                 BN_rand(b,2+i,0,0); /**/
784
785                 if (!BN_exp(d,a,b,ctx))
786                         return(00);
787
788                 if (bp != NULL)
789                         {
790                         if (!results)
791                                 {
792                                 BN_print(bp,a);
793                                 BIO_puts(bp," ^ ");
794                                 BN_print(bp,b);
795                                 BIO_puts(bp," - ");
796                                 }
797                         BN_print(bp,d);
798                         BIO_puts(bp,"\n");
799                         }
800                 BN_one(e);
801                 for( ; !BN_is_zero(b) ; BN_sub(b,b,one))
802                     BN_mul(e,e,a,ctx);
803                 BN_sub(e,e,d);
804                 if(!BN_is_zero(e))
805                     {
806                     BIO_puts(bp,"Exponentiation test failed!\n");
807                     return 0;
808                     }
809                 }
810         BN_free(a);
811         BN_free(b);
812         BN_free(d);
813         BN_free(e);
814         BN_free(one);
815         return(1);
816         }
817
818 int test_lshift(BIO *bp,BN_CTX *ctx)
819         {
820         BIGNUM *a,*b,*c,*d;
821         int i;
822
823         a=BN_new();
824         b=BN_new();
825         c=BN_new();
826         d=BN_new();
827         BN_one(c);
828
829         BN_rand(a,200,0,0); /**/
830         a->neg=rand_neg();
831         for (i=0; i<70; i++)
832                 {
833                 BN_lshift(b,a,i+1);
834                 BN_add(c,c,c);
835                 if (bp != NULL)
836                         {
837                         if (!results)
838                                 {
839                                 BN_print(bp,a);
840                                 BIO_puts(bp," * ");
841                                 BN_print(bp,c);
842                                 BIO_puts(bp," - ");
843                                 }
844                         BN_print(bp,b);
845                         BIO_puts(bp,"\n");
846                         }
847                 BN_mul(d,a,c,ctx);
848                 BN_sub(d,d,b);
849                 if(!BN_is_zero(d))
850                     {
851                     BIO_puts(bp,"Left shift test failed!\n");
852                     return 0;
853                     }
854                 }
855         BN_free(a);
856         BN_free(b);
857         BN_free(c);
858         BN_free(d);
859         return(1);
860         }
861
862 int test_lshift1(BIO *bp)
863         {
864         BIGNUM *a,*b,*c;
865         int i;
866
867         a=BN_new();
868         b=BN_new();
869         c=BN_new();
870
871         BN_rand(a,200,0,0); /**/
872         a->neg=rand_neg();
873         for (i=0; i<70; i++)
874                 {
875                 BN_lshift1(b,a);
876                 if (bp != NULL)
877                         {
878                         if (!results)
879                                 {
880                                 BN_print(bp,a);
881                                 BIO_puts(bp," * 2");
882                                 BIO_puts(bp," - ");
883                                 }
884                         BN_print(bp,b);
885                         BIO_puts(bp,"\n");
886                         }
887                 BN_add(c,a,a);
888                 BN_sub(a,b,c);
889                 if(!BN_is_zero(a))
890                     {
891                     BIO_puts(bp,"Left shift one test failed!\n");
892                     return 0;
893                     }
894                 
895                 BN_copy(a,b);
896                 }
897         BN_free(a);
898         BN_free(b);
899         BN_free(c);
900         return(1);
901         }
902
903 int test_rshift(BIO *bp,BN_CTX *ctx)
904         {
905         BIGNUM *a,*b,*c,*d,*e;
906         int i;
907
908         a=BN_new();
909         b=BN_new();
910         c=BN_new();
911         d=BN_new();
912         e=BN_new();
913         BN_one(c);
914
915         BN_rand(a,200,0,0); /**/
916         a->neg=rand_neg();
917         for (i=0; i<70; i++)
918                 {
919                 BN_rshift(b,a,i+1);
920                 BN_add(c,c,c);
921                 if (bp != NULL)
922                         {
923                         if (!results)
924                                 {
925                                 BN_print(bp,a);
926                                 BIO_puts(bp," / ");
927                                 BN_print(bp,c);
928                                 BIO_puts(bp," - ");
929                                 }
930                         BN_print(bp,b);
931                         BIO_puts(bp,"\n");
932                         }
933                 BN_div(d,e,a,c,ctx);
934                 BN_sub(d,d,b);
935                 if(!BN_is_zero(d))
936                     {
937                     BIO_puts(bp,"Right shift test failed!\n");
938                     return 0;
939                     }
940                 }
941         BN_free(a);
942         BN_free(b);
943         BN_free(c);
944         BN_free(d);
945         BN_free(e);
946         return(1);
947         }
948
949 int test_rshift1(BIO *bp)
950         {
951         BIGNUM *a,*b,*c;
952         int i;
953
954         a=BN_new();
955         b=BN_new();
956         c=BN_new();
957
958         BN_rand(a,200,0,0); /**/
959         a->neg=rand_neg();
960         for (i=0; i<70; i++)
961                 {
962                 BN_rshift1(b,a);
963                 if (bp != NULL)
964                         {
965                         if (!results)
966                                 {
967                                 BN_print(bp,a);
968                                 BIO_puts(bp," / 2");
969                                 BIO_puts(bp," - ");
970                                 }
971                         BN_print(bp,b);
972                         BIO_puts(bp,"\n");
973                         }
974                 BN_sub(c,a,b);
975                 BN_sub(c,c,b);
976                 if(!BN_is_zero(c) && !BN_is_one(c))
977                     {
978                     BIO_puts(bp,"Right shift one test failed!\n");
979                     return 0;
980                     }
981                 BN_copy(a,b);
982                 }
983         BN_free(a);
984         BN_free(b);
985         BN_free(c);
986         return(1);
987         }
988
989 int rand_neg(void)
990         {
991         static unsigned int neg=0;
992         static int sign[8]={0,0,0,1,1,0,1,1};
993
994         return(sign[(neg++)%8]);
995         }