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