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