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