Minor constification.
[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 rand_neg(void);
89 #else
90 int test_add ();
91 int test_sub ();
92 int test_lshift1 ();
93 int test_lshift ();
94 int test_rshift1 ();
95 int test_rshift ();
96 int test_div ();
97 int test_mul ();
98 int test_sqr ();
99 int test_mont ();
100 int test_mod ();
101 int test_mod_mul ();
102 int test_mod_exp ();
103 int rand_neg();
104 #endif
105
106 static int results=0;
107
108 #ifdef NO_STDIO
109 #define APPS_WIN16
110 #include "bss_file.c"
111 #endif
112
113 int main(argc,argv)
114 int argc;
115 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 /**/
218         exit(0);
219 err:
220         ERR_load_crypto_strings();
221         ERR_print_errors(out);
222         exit(1);
223         return(1);
224         }
225
226 int test_add(bp)
227 BIO *bp;
228         {
229         BIGNUM a,b,c;
230         int i;
231         int j;
232
233         BN_init(&a);
234         BN_init(&b);
235         BN_init(&c);
236
237         BN_rand(&a,512,0,0);
238         for (i=0; i<100; i++)
239                 {
240                 BN_rand(&b,450+i,0,0);
241                 a.neg=rand_neg();
242                 b.neg=rand_neg();
243                 if (bp == NULL)
244                         for (j=0; j<10000; j++)
245                                 BN_add(&c,&a,&b);
246                 BN_add(&c,&a,&b);
247                 if (bp != NULL)
248                         {
249                         if (!results)
250                                 {
251                                 BN_print(bp,&a);
252                                 BIO_puts(bp," + ");
253                                 BN_print(bp,&b);
254                                 BIO_puts(bp," - ");
255                                 }
256                         BN_print(bp,&c);
257                         BIO_puts(bp,"\n");
258                         }
259                 }
260         BN_free(&a);
261         BN_free(&b);
262         BN_free(&c);
263         return(1);
264         }
265
266 int test_sub(bp)
267 BIO *bp;
268         {
269         BIGNUM a,b,c;
270         int i;
271         int j;
272
273         BN_init(&a);
274         BN_init(&b);
275         BN_init(&c);
276
277         BN_rand(&a,512,0,0);
278         for (i=0; i<100; i++)
279                 {
280                 BN_rand(&b,400+i,0,0);
281                 a.neg=rand_neg();
282                 b.neg=rand_neg();
283                 if (bp == NULL)
284                         for (j=0; j<10000; j++)
285                                 BN_sub(&c,&a,&b);
286                 BN_sub(&c,&a,&b);
287                 if (bp != NULL)
288                         {
289                         if (!results)
290                                 {
291                                 BN_print(bp,&a);
292                                 BIO_puts(bp," - ");
293                                 BN_print(bp,&b);
294                                 BIO_puts(bp," - ");
295                                 }
296                         BN_print(bp,&c);
297                         BIO_puts(bp,"\n");
298                         }
299                 }
300         BN_free(&a);
301         BN_free(&b);
302         BN_free(&c);
303         return(1);
304         }
305
306 int test_div(bp,ctx)
307 BIO *bp;
308 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(bp,ctx)
360 BIO *bp;
361 BN_CTX *ctx;
362         {
363         BIGNUM a,b,c,d;
364         BN_RECP_CTX recp;
365         int i;
366         int j;
367
368         BN_RECP_CTX_init(&recp);
369         BN_init(&a);
370         BN_init(&b);
371         BN_init(&c);
372         BN_init(&d);
373
374         BN_rand(&a,400,0,0);
375         for (i=0; i<100; i++)
376                 {
377                 BN_rand(&b,50+i,0,0);
378                 a.neg=rand_neg();
379                 b.neg=rand_neg();
380                 BN_RECP_CTX_set(&recp,&b,ctx);
381                 if (bp == NULL)
382                         for (j=0; j<100; j++)
383                                 BN_div_recp(&d,&c,&a,&recp,ctx);
384                 BN_div_recp(&d,&c,&a,&recp,ctx);
385                 if (bp != NULL)
386                         {
387                         if (!results)
388                                 {
389                                 BN_print(bp,&a);
390                                 BIO_puts(bp," / ");
391                                 BN_print(bp,&b);
392                                 BIO_puts(bp," - ");
393                                 }
394                         BN_print(bp,&d);
395                         BIO_puts(bp,"\n");
396
397                         if (!results)
398                                 {
399                                 BN_print(bp,&a);
400                                 BIO_puts(bp," % ");
401                                 BN_print(bp,&b);
402                                 BIO_puts(bp," - ");
403                                 }
404                         BN_print(bp,&c);
405                         BIO_puts(bp,"\n");
406                         }
407                 }
408         BN_free(&a);
409         BN_free(&b);
410         BN_free(&c);
411         BN_free(&d);
412         BN_RECP_CTX_free(&recp);
413         return(1);
414         }
415
416 int test_mul(bp)
417 BIO *bp;
418         {
419         BIGNUM a,b,c;
420         int i;
421         int j;
422         BN_CTX ctx;
423
424         BN_CTX_init(&ctx);
425         BN_init(&a);
426         BN_init(&b);
427         BN_init(&c);
428
429         BN_rand(&a,200,0,0);
430         for (i=0; i<100; i++)
431                 {
432                 BN_rand(&b,250+i,0,0);
433                 BN_rand(&b,200,0,0);
434                 a.neg=rand_neg();
435                 b.neg=rand_neg();
436                 if (bp == NULL)
437                         for (j=0; j<100; j++)
438                                 BN_mul(&c,&a,&b,&ctx);
439                 BN_mul(&c,&a,&b,&ctx);
440 /*bn_do(&c,&a,&b,ctx); */
441                 if (bp != NULL)
442                         {
443                         if (!results)
444                                 {
445                                 BN_print(bp,&a);
446                                 BIO_puts(bp," * ");
447                                 BN_print(bp,&b);
448                                 BIO_puts(bp," - ");
449                                 }
450                         BN_print(bp,&c);
451                         BIO_puts(bp,"\n");
452                         }
453                 }
454         BN_free(&a);
455         BN_free(&b);
456         BN_free(&c);
457         BN_CTX_free(&ctx);
458         return(1);
459         }
460
461 int test_sqr(bp,ctx)
462 BIO *bp;
463 BN_CTX *ctx;
464         {
465         BIGNUM a,c;
466         int i;
467         int j;
468
469         BN_init(&a);
470         BN_init(&c);
471
472         for (i=0; i<40; i++)
473                 {
474                 BN_rand(&a,40+i*10,0,0);
475                 a.neg=rand_neg();
476                 if (bp == NULL)
477                         for (j=0; j<100; j++)
478                                 BN_sqr(&c,&a,ctx);
479                 BN_sqr(&c,&a,ctx);
480                 if (bp != NULL)
481                         {
482                         if (!results)
483                                 {
484                                 BN_print(bp,&a);
485                                 BIO_puts(bp," * ");
486                                 BN_print(bp,&a);
487                                 BIO_puts(bp," - ");
488                                 }
489                         BN_print(bp,&c);
490                         BIO_puts(bp,"\n");
491                         }
492                 }
493         BN_free(&a);
494         BN_free(&c);
495         return(1);
496         }
497
498 int test_mont(bp,ctx)
499 BIO *bp;
500 BN_CTX *ctx;
501         {
502         BIGNUM a,b,c,A,B;
503         BIGNUM n;
504         int i;
505         int j;
506         BN_MONT_CTX *mont;
507
508         BN_init(&a);
509         BN_init(&b);
510         BN_init(&c);
511         BN_init(&A);
512         BN_init(&B);
513         BN_init(&n);
514
515         mont=BN_MONT_CTX_new();
516
517         BN_rand(&a,100,0,0); /**/
518         BN_rand(&b,100,0,0); /**/
519         for (i=0; i<10; i++)
520                 {
521                 BN_rand(&n,(100%BN_BITS2+1)*BN_BITS2*i*BN_BITS2,0,1); /**/
522                 BN_MONT_CTX_set(mont,&n,ctx);
523
524                 BN_to_montgomery(&A,&a,mont,ctx);
525                 BN_to_montgomery(&B,&b,mont,ctx);
526
527                 if (bp == NULL)
528                         for (j=0; j<100; j++)
529                                 BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/
530                 BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/
531                 BN_from_montgomery(&A,&c,mont,ctx);/**/
532                 if (bp != NULL)
533                         {
534                         if (!results)
535                                 {
536 #ifdef undef
537 fprintf(stderr,"%d * %d %% %d\n",
538 BN_num_bits(&a),
539 BN_num_bits(&b),
540 BN_num_bits(mont->N));
541 #endif
542                                 BN_print(bp,&a);
543                                 BIO_puts(bp," * ");
544                                 BN_print(bp,&b);
545                                 BIO_puts(bp," % ");
546                                 BN_print(bp,&(mont->N));
547                                 BIO_puts(bp," - ");
548                                 }
549                         BN_print(bp,&A);
550                         BIO_puts(bp,"\n");
551                         }
552                 }
553         BN_MONT_CTX_free(mont);
554         BN_free(&a);
555         BN_free(&b);
556         BN_free(&c);
557         return(1);
558         }
559
560 int test_mod(bp,ctx)
561 BIO *bp;
562 BN_CTX *ctx;
563         {
564         BIGNUM *a,*b,*c;
565         int i;
566         int j;
567
568         a=BN_new();
569         b=BN_new();
570         c=BN_new();
571
572         BN_rand(a,1024,0,0); /**/
573         for (i=0; i<20; i++)
574                 {
575                 BN_rand(b,450+i*10,0,0); /**/
576                 a->neg=rand_neg();
577                 b->neg=rand_neg();
578                 if (bp == NULL)
579                         for (j=0; j<100; j++)
580                                 BN_mod(c,a,b,ctx);/**/
581                 BN_mod(c,a,b,ctx);/**/
582                 if (bp != NULL)
583                         {
584                         if (!results)
585                                 {
586                                 BN_print(bp,a);
587                                 BIO_puts(bp," % ");
588                                 BN_print(bp,b);
589                                 BIO_puts(bp," - ");
590                                 }
591                         BN_print(bp,c);
592                         BIO_puts(bp,"\n");
593                         }
594                 }
595         BN_free(a);
596         BN_free(b);
597         BN_free(c);
598         return(1);
599         }
600
601 int test_mod_mul(bp,ctx)
602 BIO *bp;
603 BN_CTX *ctx;
604         {
605         BIGNUM *a,*b,*c,*d,*e;
606         int i;
607
608         a=BN_new();
609         b=BN_new();
610         c=BN_new();
611         d=BN_new();
612         e=BN_new();
613
614         BN_rand(c,1024,0,0); /**/
615         for (i=0; i<10; i++)
616                 {
617                 BN_rand(a,475+i*10,0,0); /**/
618                 BN_rand(b,425+i*10,0,0); /**/
619                 a->neg=rand_neg();
620                 b->neg=rand_neg();
621         /*      if (bp == NULL)
622                         for (j=0; j<100; j++)
623                                 BN_mod_mul(d,a,b,c,ctx);*/ /**/
624
625                 if (!BN_mod_mul(e,a,b,c,ctx))
626                         {
627                         unsigned long l;
628
629                         while ((l=ERR_get_error()))
630                                 fprintf(stderr,"ERROR:%s\n",
631                                         ERR_error_string(l,NULL));
632                         exit(1);
633                         }
634                 if (bp != NULL)
635                         {
636                         if (!results)
637                                 {
638                                 BN_print(bp,a);
639                                 BIO_puts(bp," * ");
640                                 BN_print(bp,b);
641                                 BIO_puts(bp," % ");
642                                 BN_print(bp,c);
643                                 BIO_puts(bp," - ");
644                                 }
645                         BN_print(bp,e);
646                         BIO_puts(bp,"\n");
647                         }
648                 }
649         BN_free(a);
650         BN_free(b);
651         BN_free(c);
652         BN_free(d);
653         BN_free(e);
654         return(1);
655         }
656
657 int test_mod_exp(bp,ctx)
658 BIO *bp;
659 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,30,0,1); /* must be odd for montgomery */
671         for (i=0; i<6; i++)
672                 {
673                 BN_rand(a,20+i*5,0,0); /**/
674                 BN_rand(b,2+i,0,0); /**/
675
676                 if (!BN_mod_exp(d,a,b,c,ctx))
677                         return(00);
678
679                 if (bp != NULL)
680                         {
681                         if (!results)
682                                 {
683                                 BN_print(bp,a);
684                                 BIO_puts(bp," ^ ");
685                                 BN_print(bp,b);
686                                 BIO_puts(bp," % ");
687                                 BN_print(bp,c);
688                                 BIO_puts(bp," - ");
689                                 }
690                         BN_print(bp,d);
691                         BIO_puts(bp,"\n");
692                         }
693                 }
694         BN_free(a);
695         BN_free(b);
696         BN_free(c);
697         BN_free(d);
698         BN_free(e);
699         return(1);
700         }
701
702 int test_lshift(bp)
703 BIO *bp;
704         {
705         BIGNUM *a,*b,*c;
706         int i;
707
708         a=BN_new();
709         b=BN_new();
710         c=BN_new();
711         BN_one(c);
712
713         BN_rand(a,200,0,0); /**/
714         a->neg=rand_neg();
715         for (i=0; i<70; i++)
716                 {
717                 BN_lshift(b,a,i+1);
718                 BN_add(c,c,c);
719                 if (bp != NULL)
720                         {
721                         if (!results)
722                                 {
723                                 BN_print(bp,a);
724                                 BIO_puts(bp," * ");
725                                 BN_print(bp,c);
726                                 BIO_puts(bp," - ");
727                                 }
728                         BN_print(bp,b);
729                         BIO_puts(bp,"\n");
730                         }
731                 }
732         BN_free(a);
733         BN_free(b);
734         BN_free(c);
735         return(1);
736         }
737
738 int test_lshift1(bp)
739 BIO *bp;
740         {
741         BIGNUM *a,*b;
742         int i;
743
744         a=BN_new();
745         b=BN_new();
746
747         BN_rand(a,200,0,0); /**/
748         a->neg=rand_neg();
749         for (i=0; i<70; i++)
750                 {
751                 BN_lshift1(b,a);
752                 if (bp != NULL)
753                         {
754                         if (!results)
755                                 {
756                                 BN_print(bp,a);
757                                 BIO_puts(bp," * 2");
758                                 BIO_puts(bp," - ");
759                                 }
760                         BN_print(bp,b);
761                         BIO_puts(bp,"\n");
762                         }
763                 BN_copy(a,b);
764                 }
765         BN_free(a);
766         BN_free(b);
767         return(1);
768         }
769
770 int test_rshift(bp)
771 BIO *bp;
772         {
773         BIGNUM *a,*b,*c;
774         int i;
775
776         a=BN_new();
777         b=BN_new();
778         c=BN_new();
779         BN_one(c);
780
781         BN_rand(a,200,0,0); /**/
782         a->neg=rand_neg();
783         for (i=0; i<70; i++)
784                 {
785                 BN_rshift(b,a,i+1);
786                 BN_add(c,c,c);
787                 if (bp != NULL)
788                         {
789                         if (!results)
790                                 {
791                                 BN_print(bp,a);
792                                 BIO_puts(bp," / ");
793                                 BN_print(bp,c);
794                                 BIO_puts(bp," - ");
795                                 }
796                         BN_print(bp,b);
797                         BIO_puts(bp,"\n");
798                         }
799                 }
800         BN_free(a);
801         BN_free(b);
802         BN_free(c);
803         return(1);
804         }
805
806 int test_rshift1(bp)
807 BIO *bp;
808         {
809         BIGNUM *a,*b;
810         int i;
811
812         a=BN_new();
813         b=BN_new();
814
815         BN_rand(a,200,0,0); /**/
816         a->neg=rand_neg();
817         for (i=0; i<70; i++)
818                 {
819                 BN_rshift1(b,a);
820                 if (bp != NULL)
821                         {
822                         if (!results)
823                                 {
824                                 BN_print(bp,a);
825                                 BIO_puts(bp," / 2");
826                                 BIO_puts(bp," - ");
827                                 }
828                         BN_print(bp,b);
829                         BIO_puts(bp,"\n");
830                         }
831                 BN_copy(a,b);
832                 }
833         BN_free(a);
834         BN_free(b);
835         return(1);
836         }
837
838 int rand_neg()
839         {
840         static unsigned int neg=0;
841         static int sign[8]={0,0,0,1,1,0,1,1};
842
843         return(sign[(neg++)%8]);
844         }