Don't install e_os.h in include/openssl, use it only as a local
[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 #ifndef FLAT_INC
64 # include "../e_os.h"
65 #else
66 # include "e_os.h"
67 #endif
68
69 #include <openssl/bio.h>
70 #include <openssl/bn.h>
71 #include <openssl/rand.h>
72 #include <openssl/x509.h>
73 #include <openssl/err.h>
74
75 #ifdef WINDOWS
76 #include "../bio/bss_file.c"
77 #endif
78
79 int test_add(BIO *bp);
80 int test_sub(BIO *bp);
81 int test_lshift1(BIO *bp);
82 int test_lshift(BIO *bp,BN_CTX *ctx);
83 int test_rshift1(BIO *bp);
84 int test_rshift(BIO *bp,BN_CTX *ctx);
85 int test_div(BIO *bp,BN_CTX *ctx);
86 int test_div_recp(BIO *bp,BN_CTX *ctx);
87 int test_mul(BIO *bp);
88 int test_sqr(BIO *bp,BN_CTX *ctx);
89 int test_mont(BIO *bp,BN_CTX *ctx);
90 int test_mod(BIO *bp,BN_CTX *ctx);
91 int test_mod_mul(BIO *bp,BN_CTX *ctx);
92 int test_mod_exp(BIO *bp,BN_CTX *ctx);
93 int test_exp(BIO *bp,BN_CTX *ctx);
94 int rand_neg(void);
95 static int results=0;
96
97 #ifdef NO_STDIO
98 #define APPS_WIN16
99 #include "bss_file.c"
100 #endif
101
102 int main(int argc, char *argv[])
103         {
104         BN_CTX *ctx;
105         BIO *out;
106         char *outfile=NULL;
107
108         srand((unsigned int)time(NULL));
109
110         argc--;
111         argv++;
112         while (argc >= 1)
113                 {
114                 if (strcmp(*argv,"-results") == 0)
115                         results=1;
116                 else if (strcmp(*argv,"-out") == 0)
117                         {
118                         if (--argc < 1) break;
119                         outfile= *(++argv);
120                         }
121                 argc--;
122                 argv++;
123                 }
124
125
126         ctx=BN_CTX_new();
127         if (ctx == NULL) exit(1);
128
129         out=BIO_new(BIO_s_file());
130         if (out == NULL) exit(1);
131         if (outfile == NULL)
132                 {
133                 BIO_set_fp(out,stdout,BIO_NOCLOSE);
134                 }
135         else
136                 {
137                 if (!BIO_write_filename(out,outfile))
138                         {
139                         perror(outfile);
140                         exit(1);
141                         }
142                 }
143
144         if (!results)
145                 BIO_puts(out,"obase=16\nibase=16\n");
146
147         fprintf(stderr,"test BN_add\n");
148         if (!test_add(out)) goto err;
149         fflush(stdout);
150
151         fprintf(stderr,"test BN_sub\n");
152         if (!test_sub(out)) goto err;
153         fflush(stdout);
154
155         fprintf(stderr,"test BN_lshift1\n");
156         if (!test_lshift1(out)) goto err;
157         fflush(stdout);
158
159         fprintf(stderr,"test BN_lshift\n");
160         if (!test_lshift(out,ctx)) goto err;
161         fflush(stdout);
162
163         fprintf(stderr,"test BN_rshift1\n");
164         if (!test_rshift1(out)) goto err;
165         fflush(stdout);
166
167         fprintf(stderr,"test BN_rshift\n");
168         if (!test_rshift(out,ctx)) goto err;
169         fflush(stdout);
170
171         fprintf(stderr,"test BN_sqr\n");
172         if (!test_sqr(out,ctx)) goto err;
173         fflush(stdout);
174
175         fprintf(stderr,"test BN_mul\n");
176         if (!test_mul(out)) goto err;
177         fflush(stdout);
178
179         fprintf(stderr,"test BN_div\n");
180         if (!test_div(out,ctx)) goto err;
181         fflush(stdout);
182
183         fprintf(stderr,"test BN_div_recp\n");
184         if (!test_div_recp(out,ctx)) goto err;
185         fflush(stdout);
186
187         fprintf(stderr,"test BN_mod\n");
188         if (!test_mod(out,ctx)) goto err;
189         fflush(stdout);
190
191         fprintf(stderr,"test BN_mod_mul\n");
192         if (!test_mod_mul(out,ctx)) goto err;
193         fflush(stdout);
194
195 /*
196         fprintf(stderr,"test BN_mont\n");
197         if (!test_mont(out,ctx)) goto err;
198         fflush(stdout);
199 */
200         fprintf(stderr,"test BN_mod_exp\n");
201         if (!test_mod_exp(out,ctx)) goto err;
202         fflush(stdout);
203
204         fprintf(stderr,"test BN_exp\n");
205         if (!test_exp(out,ctx)) goto err;
206         fflush(stdout);
207
208 /**/
209         exit(0);
210 err:
211         ERR_load_crypto_strings();
212         ERR_print_errors(out);
213         exit(1);
214         return(1);
215         }
216
217 int test_add(BIO *bp)
218         {
219         BIGNUM a,b,c;
220         int i;
221         int j;
222
223         BN_init(&a);
224         BN_init(&b);
225         BN_init(&c);
226
227         BN_rand(&a,512,0,0);
228         for (i=0; i<100; i++)
229                 {
230                 BN_rand(&b,450+i,0,0);
231                 a.neg=rand_neg();
232                 b.neg=rand_neg();
233                 if (bp == NULL)
234                         for (j=0; j<10000; j++)
235                                 BN_add(&c,&a,&b);
236                 BN_add(&c,&a,&b);
237                 if (bp != NULL)
238                         {
239                         if (!results)
240                                 {
241                                 BN_print(bp,&a);
242                                 BIO_puts(bp," + ");
243                                 BN_print(bp,&b);
244                                 BIO_puts(bp," - ");
245                                 }
246                         BN_print(bp,&c);
247                         BIO_puts(bp,"\n");
248                         }
249                 a.neg=!a.neg;
250                 b.neg=!b.neg;
251                 BN_add(&c,&c,&b);
252                 BN_add(&c,&c,&a);
253                 if(!BN_is_zero(&c))
254                     {
255                     BIO_puts(bp,"Add test failed!\n");
256                     return 0;
257                     }
258                 }
259         BN_free(&a);
260         BN_free(&b);
261         BN_free(&c);
262         return(1);
263         }
264
265 int test_sub(BIO *bp)
266         {
267         BIGNUM a,b,c;
268         int i;
269         int j;
270
271         BN_init(&a);
272         BN_init(&b);
273         BN_init(&c);
274
275         BN_rand(&a,512,0,0);
276         for (i=0; i<100; i++)
277                 {
278                 BN_rand(&b,400+i,0,0);
279                 a.neg=rand_neg();
280                 b.neg=rand_neg();
281                 if (bp == NULL)
282                         for (j=0; j<10000; j++)
283                                 BN_sub(&c,&a,&b);
284                 BN_sub(&c,&a,&b);
285                 if (bp != NULL)
286                         {
287                         if (!results)
288                                 {
289                                 BN_print(bp,&a);
290                                 BIO_puts(bp," - ");
291                                 BN_print(bp,&b);
292                                 BIO_puts(bp," - ");
293                                 }
294                         BN_print(bp,&c);
295                         BIO_puts(bp,"\n");
296                         }
297                 BN_add(&c,&c,&b);
298                 BN_sub(&c,&c,&a);
299                 if(!BN_is_zero(&c))
300                     {
301                     BIO_puts(bp,"Subtract test failed!\n");
302                     return 0;
303                     }
304                 }
305         BN_free(&a);
306         BN_free(&b);
307         BN_free(&c);
308         return(1);
309         }
310
311 int test_div(BIO *bp, BN_CTX *ctx)
312         {
313         BIGNUM a,b,c,d,e;
314         int i;
315         int j;
316
317         BN_init(&a);
318         BN_init(&b);
319         BN_init(&c);
320         BN_init(&d);
321         BN_init(&e);
322
323         BN_rand(&a,400,0,0);
324         for (i=0; i<100; i++)
325                 {
326                 BN_rand(&b,50+i,0,0);
327                 a.neg=rand_neg();
328                 b.neg=rand_neg();
329                 if (bp == NULL)
330                         for (j=0; j<100; j++)
331                                 BN_div(&d,&c,&a,&b,ctx);
332                 BN_div(&d,&c,&a,&b,ctx);
333                 if (bp != NULL)
334                         {
335                         if (!results)
336                                 {
337                                 BN_print(bp,&a);
338                                 BIO_puts(bp," / ");
339                                 BN_print(bp,&b);
340                                 BIO_puts(bp," - ");
341                                 }
342                         BN_print(bp,&d);
343                         BIO_puts(bp,"\n");
344
345                         if (!results)
346                                 {
347                                 BN_print(bp,&a);
348                                 BIO_puts(bp," % ");
349                                 BN_print(bp,&b);
350                                 BIO_puts(bp," - ");
351                                 }
352                         BN_print(bp,&c);
353                         BIO_puts(bp,"\n");
354                         }
355                 BN_mul(&e,&d,&b,ctx);
356                 BN_add(&d,&e,&c);
357                 BN_sub(&d,&d,&a);
358                 if(!BN_is_zero(&d))
359                     {
360                     BIO_puts(bp,"Division test failed!\n");
361                     return 0;
362                     }
363                 }
364         BN_free(&a);
365         BN_free(&b);
366         BN_free(&c);
367         BN_free(&d);
368         BN_free(&e);
369         return(1);
370         }
371
372 int test_div_recp(BIO *bp, BN_CTX *ctx)
373         {
374         BIGNUM a,b,c,d,e;
375         BN_RECP_CTX recp;
376         int i;
377         int j;
378
379         BN_RECP_CTX_init(&recp);
380         BN_init(&a);
381         BN_init(&b);
382         BN_init(&c);
383         BN_init(&d);
384         BN_init(&e);
385
386         BN_rand(&a,400,0,0);
387         for (i=0; i<100; i++)
388                 {
389                 BN_rand(&b,50+i,0,0);
390                 a.neg=rand_neg();
391                 b.neg=rand_neg();
392                 BN_RECP_CTX_set(&recp,&b,ctx);
393                 if (bp == NULL)
394                         for (j=0; j<100; j++)
395                                 BN_div_recp(&d,&c,&a,&recp,ctx);
396                 BN_div_recp(&d,&c,&a,&recp,ctx);
397                 if (bp != NULL)
398                         {
399                         if (!results)
400                                 {
401                                 BN_print(bp,&a);
402                                 BIO_puts(bp," / ");
403                                 BN_print(bp,&b);
404                                 BIO_puts(bp," - ");
405                                 }
406                         BN_print(bp,&d);
407                         BIO_puts(bp,"\n");
408
409                         if (!results)
410                                 {
411                                 BN_print(bp,&a);
412                                 BIO_puts(bp," % ");
413                                 BN_print(bp,&b);
414                                 BIO_puts(bp," - ");
415                                 }
416                         BN_print(bp,&c);
417                         BIO_puts(bp,"\n");
418                         }
419                 BN_mul(&e,&d,&b,ctx);
420                 BN_add(&d,&e,&c);
421                 BN_sub(&d,&d,&a);
422                 if(!BN_is_zero(&d))
423                     {
424                     BIO_puts(bp,"Reciprocal division test failed!\n");
425                     return 0;
426                     }
427                 }
428         BN_free(&a);
429         BN_free(&b);
430         BN_free(&c);
431         BN_free(&d);
432         BN_free(&e);
433         BN_RECP_CTX_free(&recp);
434         return(1);
435         }
436
437 int test_mul(BIO *bp)
438         {
439         BIGNUM a,b,c,d,e;
440         int i;
441         int j;
442         BN_CTX ctx;
443
444         BN_CTX_init(&ctx);
445         BN_init(&a);
446         BN_init(&b);
447         BN_init(&c);
448         BN_init(&d);
449         BN_init(&e);
450
451         BN_rand(&a,200,0,0);
452         for (i=0; i<100; i++)
453                 {
454                 BN_rand(&b,250+i,0,0);
455                 BN_rand(&b,200,0,0);
456                 a.neg=rand_neg();
457                 b.neg=rand_neg();
458                 if (bp == NULL)
459                         for (j=0; j<100; j++)
460                                 BN_mul(&c,&a,&b,&ctx);
461                 BN_mul(&c,&a,&b,&ctx);
462                 if (bp != NULL)
463                         {
464                         if (!results)
465                                 {
466                                 BN_print(bp,&a);
467                                 BIO_puts(bp," * ");
468                                 BN_print(bp,&b);
469                                 BIO_puts(bp," - ");
470                                 }
471                         BN_print(bp,&c);
472                         BIO_puts(bp,"\n");
473                         }
474                 BN_div(&d,&e,&c,&a,&ctx);
475                 BN_sub(&d,&d,&b);
476                 if(!BN_is_zero(&d) || !BN_is_zero(&e))
477                     {
478                     BIO_puts(bp,"Multiplication test failed!\n");
479                     return 0;
480                     }
481                 }
482         BN_free(&a);
483         BN_free(&b);
484         BN_free(&c);
485         BN_free(&d);
486         BN_free(&e);
487         BN_CTX_free(&ctx);
488         return(1);
489         }
490
491 int test_sqr(BIO *bp, BN_CTX *ctx)
492         {
493         BIGNUM a,c,d,e;
494         int i;
495         int j;
496
497         BN_init(&a);
498         BN_init(&c);
499         BN_init(&d);
500         BN_init(&e);
501
502         for (i=0; i<40; i++)
503                 {
504                 BN_rand(&a,40+i*10,0,0);
505                 a.neg=rand_neg();
506                 if (bp == NULL)
507                         for (j=0; j<100; j++)
508                                 BN_sqr(&c,&a,ctx);
509                 BN_sqr(&c,&a,ctx);
510                 if (bp != NULL)
511                         {
512                         if (!results)
513                                 {
514                                 BN_print(bp,&a);
515                                 BIO_puts(bp," * ");
516                                 BN_print(bp,&a);
517                                 BIO_puts(bp," - ");
518                                 }
519                         BN_print(bp,&c);
520                         BIO_puts(bp,"\n");
521                         }
522                 BN_div(&d,&e,&c,&a,ctx);
523                 BN_sub(&d,&d,&a);
524                 if(!BN_is_zero(&d) || !BN_is_zero(&e))
525                     {
526                     BIO_puts(bp,"Square test failed!\n");
527                     return 0;
528                     }
529                 }
530         BN_free(&a);
531         BN_free(&c);
532         BN_free(&d);
533         BN_free(&e);
534         return(1);
535         }
536
537 int test_mont(BIO *bp, BN_CTX *ctx)
538         {
539         BIGNUM a,b,c,d,A,B;
540         BIGNUM n;
541         int i;
542         int j;
543         BN_MONT_CTX *mont;
544
545         BN_init(&a);
546         BN_init(&b);
547         BN_init(&c);
548         BN_init(&d);
549         BN_init(&A);
550         BN_init(&B);
551         BN_init(&n);
552
553         mont=BN_MONT_CTX_new();
554
555         BN_rand(&a,100,0,0); /**/
556         BN_rand(&b,100,0,0); /**/
557         for (i=0; i<10; i++)
558                 {
559                 BN_rand(&n,(100%BN_BITS2+1)*BN_BITS2*i*BN_BITS2,0,1); /**/
560                 BN_MONT_CTX_set(mont,&n,ctx);
561
562                 BN_to_montgomery(&A,&a,mont,ctx);
563                 BN_to_montgomery(&B,&b,mont,ctx);
564
565                 if (bp == NULL)
566                         for (j=0; j<100; j++)
567                                 BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/
568                 BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/
569                 BN_from_montgomery(&A,&c,mont,ctx);/**/
570                 if (bp != NULL)
571                         {
572                         if (!results)
573                                 {
574 #ifdef undef
575 fprintf(stderr,"%d * %d %% %d\n",
576 BN_num_bits(&a),
577 BN_num_bits(&b),
578 BN_num_bits(mont->N));
579 #endif
580                                 BN_print(bp,&a);
581                                 BIO_puts(bp," * ");
582                                 BN_print(bp,&b);
583                                 BIO_puts(bp," % ");
584                                 BN_print(bp,&(mont->N));
585                                 BIO_puts(bp," - ");
586                                 }
587                         BN_print(bp,&A);
588                         BIO_puts(bp,"\n");
589                         }
590                 BN_mod_mul(&d,&a,&b,&n,ctx);
591                 BN_sub(&d,&d,&A);
592                 if(!BN_is_zero(&d))
593                     {
594                     BIO_puts(bp,"Montgomery multiplication test failed!\n");
595                     return 0;
596                     }
597                 }
598         BN_MONT_CTX_free(mont);
599         BN_free(&a);
600         BN_free(&b);
601         BN_free(&c);
602         BN_free(&d);
603         BN_free(&A);
604         BN_free(&B);
605         BN_free(&n);
606         return(1);
607         }
608
609 int test_mod(BIO *bp, BN_CTX *ctx)
610         {
611         BIGNUM *a,*b,*c,*d,*e;
612         int i;
613         int j;
614
615         a=BN_new();
616         b=BN_new();
617         c=BN_new();
618         d=BN_new();
619         e=BN_new();
620
621         BN_rand(a,1024,0,0); /**/
622         for (i=0; i<20; i++)
623                 {
624                 BN_rand(b,450+i*10,0,0); /**/
625                 a->neg=rand_neg();
626                 b->neg=rand_neg();
627                 if (bp == NULL)
628                         for (j=0; j<100; j++)
629                                 BN_mod(c,a,b,ctx);/**/
630                 BN_mod(c,a,b,ctx);/**/
631                 if (bp != NULL)
632                         {
633                         if (!results)
634                                 {
635                                 BN_print(bp,a);
636                                 BIO_puts(bp," % ");
637                                 BN_print(bp,b);
638                                 BIO_puts(bp," - ");
639                                 }
640                         BN_print(bp,c);
641                         BIO_puts(bp,"\n");
642                         }
643                 BN_div(d,e,a,b,ctx);
644                 BN_sub(e,e,c);
645                 if(!BN_is_zero(e))
646                     {
647                     BIO_puts(bp,"Modulo test failed!\n");
648                     return 0;
649                     }
650                 }
651         BN_free(a);
652         BN_free(b);
653         BN_free(c);
654         BN_free(d);
655         BN_free(e);
656         return(1);
657         }
658
659 int test_mod_mul(BIO *bp, BN_CTX *ctx)
660         {
661         BIGNUM *a,*b,*c,*d,*e;
662         int i;
663
664         a=BN_new();
665         b=BN_new();
666         c=BN_new();
667         d=BN_new();
668         e=BN_new();
669
670         BN_rand(c,1024,0,0); /**/
671         for (i=0; i<10; i++)
672                 {
673                 BN_rand(a,475+i*10,0,0); /**/
674                 BN_rand(b,425+i*10,0,0); /**/
675                 a->neg=rand_neg();
676                 b->neg=rand_neg();
677         /*      if (bp == NULL)
678                         for (j=0; j<100; j++)
679                                 BN_mod_mul(d,a,b,c,ctx);*/ /**/
680
681                 if (!BN_mod_mul(e,a,b,c,ctx))
682                         {
683                         unsigned long l;
684
685                         while ((l=ERR_get_error()))
686                                 fprintf(stderr,"ERROR:%s\n",
687                                         ERR_error_string(l,NULL));
688                         exit(1);
689                         }
690                 if (bp != NULL)
691                         {
692                         if (!results)
693                                 {
694                                 BN_print(bp,a);
695                                 BIO_puts(bp," * ");
696                                 BN_print(bp,b);
697                                 BIO_puts(bp," % ");
698                                 BN_print(bp,c);
699                                 BIO_puts(bp," - ");
700                                 }
701                         BN_print(bp,e);
702                         BIO_puts(bp,"\n");
703                         }
704                 BN_mul(d,a,b,ctx);
705                 BN_sub(d,d,e);
706                 BN_div(a,b,d,c,ctx);
707                 if(!BN_is_zero(b))
708                     {
709                     BIO_puts(bp,"Modulo multiply test failed!\n");
710                     return 0;
711                     }
712                 }
713         BN_free(a);
714         BN_free(b);
715         BN_free(c);
716         BN_free(d);
717         BN_free(e);
718         return(1);
719         }
720
721 int test_mod_exp(BIO *bp, BN_CTX *ctx)
722         {
723         BIGNUM *a,*b,*c,*d,*e;
724         int i;
725
726         a=BN_new();
727         b=BN_new();
728         c=BN_new();
729         d=BN_new();
730         e=BN_new();
731
732         BN_rand(c,30,0,1); /* must be odd for montgomery */
733         for (i=0; i<6; i++)
734                 {
735                 BN_rand(a,20+i*5,0,0); /**/
736                 BN_rand(b,2+i,0,0); /**/
737
738                 if (!BN_mod_exp(d,a,b,c,ctx))
739                         return(00);
740
741                 if (bp != NULL)
742                         {
743                         if (!results)
744                                 {
745                                 BN_print(bp,a);
746                                 BIO_puts(bp," ^ ");
747                                 BN_print(bp,b);
748                                 BIO_puts(bp," % ");
749                                 BN_print(bp,c);
750                                 BIO_puts(bp," - ");
751                                 }
752                         BN_print(bp,d);
753                         BIO_puts(bp,"\n");
754                         }
755                 BN_exp(e,a,b,ctx);
756                 BN_sub(e,e,d);
757                 BN_div(a,b,e,c,ctx);
758                 if(!BN_is_zero(b))
759                     {
760                     BIO_puts(bp,"Modulo exponentiation test failed!\n");
761                     return 0;
762                     }
763                 }
764         BN_free(a);
765         BN_free(b);
766         BN_free(c);
767         BN_free(d);
768         BN_free(e);
769         return(1);
770         }
771
772 int test_exp(BIO *bp, BN_CTX *ctx)
773         {
774         BIGNUM *a,*b,*d,*e,*one;
775         int i;
776
777         a=BN_new();
778         b=BN_new();
779         d=BN_new();
780         e=BN_new();
781         one=BN_new();
782         BN_one(one);
783
784         for (i=0; i<6; i++)
785                 {
786                 BN_rand(a,20+i*5,0,0); /**/
787                 BN_rand(b,2+i,0,0); /**/
788
789                 if (!BN_exp(d,a,b,ctx))
790                         return(00);
791
792                 if (bp != NULL)
793                         {
794                         if (!results)
795                                 {
796                                 BN_print(bp,a);
797                                 BIO_puts(bp," ^ ");
798                                 BN_print(bp,b);
799                                 BIO_puts(bp," - ");
800                                 }
801                         BN_print(bp,d);
802                         BIO_puts(bp,"\n");
803                         }
804                 BN_one(e);
805                 for( ; !BN_is_zero(b) ; BN_sub(b,b,one))
806                     BN_mul(e,e,a,ctx);
807                 BN_sub(e,e,d);
808                 if(!BN_is_zero(e))
809                     {
810                     BIO_puts(bp,"Exponentiation test failed!\n");
811                     return 0;
812                     }
813                 }
814         BN_free(a);
815         BN_free(b);
816         BN_free(d);
817         BN_free(e);
818         BN_free(one);
819         return(1);
820         }
821
822 int test_lshift(BIO *bp,BN_CTX *ctx)
823         {
824         BIGNUM *a,*b,*c,*d;
825         int i;
826
827         a=BN_new();
828         b=BN_new();
829         c=BN_new();
830         d=BN_new();
831         BN_one(c);
832
833         BN_rand(a,200,0,0); /**/
834         a->neg=rand_neg();
835         for (i=0; i<70; i++)
836                 {
837                 BN_lshift(b,a,i+1);
838                 BN_add(c,c,c);
839                 if (bp != NULL)
840                         {
841                         if (!results)
842                                 {
843                                 BN_print(bp,a);
844                                 BIO_puts(bp," * ");
845                                 BN_print(bp,c);
846                                 BIO_puts(bp," - ");
847                                 }
848                         BN_print(bp,b);
849                         BIO_puts(bp,"\n");
850                         }
851                 BN_mul(d,a,c,ctx);
852                 BN_sub(d,d,b);
853                 if(!BN_is_zero(d))
854                     {
855                     BIO_puts(bp,"Left shift test failed!\n");
856                     return 0;
857                     }
858                 }
859         BN_free(a);
860         BN_free(b);
861         BN_free(c);
862         BN_free(d);
863         return(1);
864         }
865
866 int test_lshift1(BIO *bp)
867         {
868         BIGNUM *a,*b,*c;
869         int i;
870
871         a=BN_new();
872         b=BN_new();
873         c=BN_new();
874
875         BN_rand(a,200,0,0); /**/
876         a->neg=rand_neg();
877         for (i=0; i<70; i++)
878                 {
879                 BN_lshift1(b,a);
880                 if (bp != NULL)
881                         {
882                         if (!results)
883                                 {
884                                 BN_print(bp,a);
885                                 BIO_puts(bp," * 2");
886                                 BIO_puts(bp," - ");
887                                 }
888                         BN_print(bp,b);
889                         BIO_puts(bp,"\n");
890                         }
891                 BN_add(c,a,a);
892                 BN_sub(a,b,c);
893                 if(!BN_is_zero(a))
894                     {
895                     BIO_puts(bp,"Left shift one test failed!\n");
896                     return 0;
897                     }
898                 
899                 BN_copy(a,b);
900                 }
901         BN_free(a);
902         BN_free(b);
903         BN_free(c);
904         return(1);
905         }
906
907 int test_rshift(BIO *bp,BN_CTX *ctx)
908         {
909         BIGNUM *a,*b,*c,*d,*e;
910         int i;
911
912         a=BN_new();
913         b=BN_new();
914         c=BN_new();
915         d=BN_new();
916         e=BN_new();
917         BN_one(c);
918
919         BN_rand(a,200,0,0); /**/
920         a->neg=rand_neg();
921         for (i=0; i<70; i++)
922                 {
923                 BN_rshift(b,a,i+1);
924                 BN_add(c,c,c);
925                 if (bp != NULL)
926                         {
927                         if (!results)
928                                 {
929                                 BN_print(bp,a);
930                                 BIO_puts(bp," / ");
931                                 BN_print(bp,c);
932                                 BIO_puts(bp," - ");
933                                 }
934                         BN_print(bp,b);
935                         BIO_puts(bp,"\n");
936                         }
937                 BN_div(d,e,a,c,ctx);
938                 BN_sub(d,d,b);
939                 if(!BN_is_zero(d))
940                     {
941                     BIO_puts(bp,"Right shift test failed!\n");
942                     return 0;
943                     }
944                 }
945         BN_free(a);
946         BN_free(b);
947         BN_free(c);
948         BN_free(d);
949         BN_free(e);
950         return(1);
951         }
952
953 int test_rshift1(BIO *bp)
954         {
955         BIGNUM *a,*b,*c;
956         int i;
957
958         a=BN_new();
959         b=BN_new();
960         c=BN_new();
961
962         BN_rand(a,200,0,0); /**/
963         a->neg=rand_neg();
964         for (i=0; i<70; i++)
965                 {
966                 BN_rshift1(b,a);
967                 if (bp != NULL)
968                         {
969                         if (!results)
970                                 {
971                                 BN_print(bp,a);
972                                 BIO_puts(bp," / 2");
973                                 BIO_puts(bp," - ");
974                                 }
975                         BN_print(bp,b);
976                         BIO_puts(bp,"\n");
977                         }
978                 BN_sub(c,a,b);
979                 BN_sub(c,c,b);
980                 if(!BN_is_zero(c) && !BN_is_one(c))
981                     {
982                     BIO_puts(bp,"Right shift one test failed!\n");
983                     return 0;
984                     }
985                 BN_copy(a,b);
986                 }
987         BN_free(a);
988         BN_free(b);
989         BN_free(c);
990         return(1);
991         }
992
993 int rand_neg(void)
994         {
995         static unsigned int neg=0;
996         static int sign[8]={0,0,0,1,1,0,1,1};
997
998         return(sign[(neg++)%8]);
999         }