bn/bn_const.c: make it indent-friendly.
[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  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60  *
61  * Portions of the attached software ("Contribution") are developed by 
62  * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
63  *
64  * The Contribution is licensed pursuant to the Eric Young open source
65  * license provided above.
66  *
67  * The binary polynomial arithmetic software is originally written by 
68  * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
69  *
70  */
71
72 /* Until the key-gen callbacks are modified to use newer prototypes, we allow
73  * deprecated functions for openssl-internal code */
74 #ifdef OPENSSL_NO_DEPRECATED
75 #undef OPENSSL_NO_DEPRECATED
76 #endif
77
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <string.h>
81
82 #include "e_os.h"
83
84 #include <openssl/bio.h>
85 #include <openssl/bn.h>
86 #include <openssl/rand.h>
87 #include <openssl/x509.h>
88 #include <openssl/err.h>
89
90 const int num0 = 100; /* number of tests */
91 const int num1 = 50;  /* additional tests for some functions */
92 const int num2 = 5;   /* number of tests for slow functions */
93
94 int test_add(BIO *bp);
95 int test_sub(BIO *bp);
96 int test_lshift1(BIO *bp);
97 int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_);
98 int test_rshift1(BIO *bp);
99 int test_rshift(BIO *bp,BN_CTX *ctx);
100 int test_div(BIO *bp,BN_CTX *ctx);
101 int test_div_word(BIO *bp);
102 int test_div_recp(BIO *bp,BN_CTX *ctx);
103 int test_mul(BIO *bp);
104 int test_sqr(BIO *bp,BN_CTX *ctx);
105 int test_mont(BIO *bp,BN_CTX *ctx);
106 int test_mod(BIO *bp,BN_CTX *ctx);
107 int test_mod_mul(BIO *bp,BN_CTX *ctx);
108 int test_mod_exp(BIO *bp,BN_CTX *ctx);
109 int test_mod_exp_mont_consttime(BIO *bp,BN_CTX *ctx);
110 int test_mod_exp_mont5(BIO *bp, BN_CTX *ctx);
111 int test_exp(BIO *bp,BN_CTX *ctx);
112 int test_gf2m_add(BIO *bp);
113 int test_gf2m_mod(BIO *bp);
114 int test_gf2m_mod_mul(BIO *bp,BN_CTX *ctx);
115 int test_gf2m_mod_sqr(BIO *bp,BN_CTX *ctx);
116 int test_gf2m_mod_inv(BIO *bp,BN_CTX *ctx);
117 int test_gf2m_mod_div(BIO *bp,BN_CTX *ctx);
118 int test_gf2m_mod_exp(BIO *bp,BN_CTX *ctx);
119 int test_gf2m_mod_sqrt(BIO *bp,BN_CTX *ctx);
120 int test_gf2m_mod_solve_quad(BIO *bp,BN_CTX *ctx);
121 int test_kron(BIO *bp,BN_CTX *ctx);
122 int test_sqrt(BIO *bp,BN_CTX *ctx);
123 int rand_neg(void);
124 static int results=0;
125
126 static unsigned char lst[]="\xC6\x4F\x43\x04\x2A\xEA\xCA\x6E\x58\x36\x80\x5B\xE8\xC9"
127 "\x9B\x04\x5D\x48\x36\xC2\xFD\x16\xC9\x64\xF0";
128
129 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
130
131 static void message(BIO *out, char *m)
132         {
133         fprintf(stderr, "test %s\n", m);
134         BIO_puts(out, "print \"test ");
135         BIO_puts(out, m);
136         BIO_puts(out, "\\n\"\n");
137         }
138
139 int main(int argc, char *argv[])
140         {
141         BN_CTX *ctx;
142         BIO *out;
143         char *outfile=NULL;
144
145         results = 0;
146
147         RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
148
149         argc--;
150         argv++;
151         while (argc >= 1)
152                 {
153                 if (strcmp(*argv,"-results") == 0)
154                         results=1;
155                 else if (strcmp(*argv,"-out") == 0)
156                         {
157                         if (--argc < 1) break;
158                         outfile= *(++argv);
159                         }
160                 argc--;
161                 argv++;
162                 }
163
164
165         ctx=BN_CTX_new();
166         if (ctx == NULL) EXIT(1);
167
168         out=BIO_new(BIO_s_file());
169         if (out == NULL) EXIT(1);
170         if (outfile == NULL)
171                 {
172                 BIO_set_fp(out,stdout,BIO_NOCLOSE);
173                 }
174         else
175                 {
176                 if (!BIO_write_filename(out,outfile))
177                         {
178                         perror(outfile);
179                         EXIT(1);
180                         }
181                 }
182
183         if (!results)
184                 BIO_puts(out,"obase=16\nibase=16\n");
185
186         message(out,"BN_add");
187         if (!test_add(out)) goto err;
188         (void)BIO_flush(out);
189
190         message(out,"BN_sub");
191         if (!test_sub(out)) goto err;
192         (void)BIO_flush(out);
193
194         message(out,"BN_lshift1");
195         if (!test_lshift1(out)) goto err;
196         (void)BIO_flush(out);
197
198         message(out,"BN_lshift (fixed)");
199         if (!test_lshift(out,ctx,BN_bin2bn(lst,sizeof(lst)-1,NULL)))
200             goto err;
201         (void)BIO_flush(out);
202
203         message(out,"BN_lshift");
204         if (!test_lshift(out,ctx,NULL)) goto err;
205         (void)BIO_flush(out);
206
207         message(out,"BN_rshift1");
208         if (!test_rshift1(out)) goto err;
209         (void)BIO_flush(out);
210
211         message(out,"BN_rshift");
212         if (!test_rshift(out,ctx)) goto err;
213         (void)BIO_flush(out);
214
215         message(out,"BN_sqr");
216         if (!test_sqr(out,ctx)) goto err;
217         (void)BIO_flush(out);
218
219         message(out,"BN_mul");
220         if (!test_mul(out)) goto err;
221         (void)BIO_flush(out);
222
223         message(out,"BN_div");
224         if (!test_div(out,ctx)) goto err;
225         (void)BIO_flush(out);
226
227         message(out,"BN_div_word");
228         if (!test_div_word(out)) goto err;
229         (void)BIO_flush(out);
230
231         message(out,"BN_div_recp");
232         if (!test_div_recp(out,ctx)) goto err;
233         (void)BIO_flush(out);
234
235         message(out,"BN_mod");
236         if (!test_mod(out,ctx)) goto err;
237         (void)BIO_flush(out);
238
239         message(out,"BN_mod_mul");
240         if (!test_mod_mul(out,ctx)) goto err;
241         (void)BIO_flush(out);
242
243         message(out,"BN_mont");
244         if (!test_mont(out,ctx)) goto err;
245         (void)BIO_flush(out);
246
247         message(out,"BN_mod_exp");
248         if (!test_mod_exp(out,ctx)) goto err;
249         (void)BIO_flush(out);
250
251         message(out,"BN_mod_exp_mont_consttime");
252         if (!test_mod_exp_mont_consttime(out,ctx)) goto err;
253         if (!test_mod_exp_mont5(out,ctx)) goto err;
254         (void)BIO_flush(out);
255
256         message(out,"BN_exp");
257         if (!test_exp(out,ctx)) goto err;
258         (void)BIO_flush(out);
259
260         message(out,"BN_kronecker");
261         if (!test_kron(out,ctx)) goto err;
262         (void)BIO_flush(out);
263
264         message(out,"BN_mod_sqrt");
265         if (!test_sqrt(out,ctx)) goto err;
266         (void)BIO_flush(out);
267 #ifndef OPENSSL_NO_EC2M
268         message(out,"BN_GF2m_add");
269         if (!test_gf2m_add(out)) goto err;
270         (void)BIO_flush(out);
271
272         message(out,"BN_GF2m_mod");
273         if (!test_gf2m_mod(out)) goto err;
274         (void)BIO_flush(out);
275
276         message(out,"BN_GF2m_mod_mul");
277         if (!test_gf2m_mod_mul(out,ctx)) goto err;
278         (void)BIO_flush(out);
279
280         message(out,"BN_GF2m_mod_sqr");
281         if (!test_gf2m_mod_sqr(out,ctx)) goto err;
282         (void)BIO_flush(out);
283
284         message(out,"BN_GF2m_mod_inv");
285         if (!test_gf2m_mod_inv(out,ctx)) goto err;
286         (void)BIO_flush(out);
287
288         message(out,"BN_GF2m_mod_div");
289         if (!test_gf2m_mod_div(out,ctx)) goto err;
290         (void)BIO_flush(out);
291
292         message(out,"BN_GF2m_mod_exp");
293         if (!test_gf2m_mod_exp(out,ctx)) goto err;
294         (void)BIO_flush(out);
295
296         message(out,"BN_GF2m_mod_sqrt");
297         if (!test_gf2m_mod_sqrt(out,ctx)) goto err;
298         (void)BIO_flush(out);
299
300         message(out,"BN_GF2m_mod_solve_quad");
301         if (!test_gf2m_mod_solve_quad(out,ctx)) goto err;
302         (void)BIO_flush(out);
303 #endif
304         BN_CTX_free(ctx);
305         BIO_free(out);
306
307         EXIT(0);
308 err:
309         BIO_puts(out,"1\n"); /* make sure the Perl script fed by bc notices
310                               * the failure, see test_bn in test/Makefile.ssl
311                               */
312         (void)BIO_flush(out);
313         ERR_load_crypto_strings();
314         ERR_print_errors_fp(stderr);
315         EXIT(1);
316         return(1);
317         }
318
319 int test_add(BIO *bp)
320         {
321         BIGNUM a,b,c;
322         int i;
323
324         BN_init(&a);
325         BN_init(&b);
326         BN_init(&c);
327
328         BN_bntest_rand(&a,512,0,0);
329         for (i=0; i<num0; i++)
330                 {
331                 BN_bntest_rand(&b,450+i,0,0);
332                 a.neg=rand_neg();
333                 b.neg=rand_neg();
334                 BN_add(&c,&a,&b);
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,&c);
345                         BIO_puts(bp,"\n");
346                         }
347                 a.neg=!a.neg;
348                 b.neg=!b.neg;
349                 BN_add(&c,&c,&b);
350                 BN_add(&c,&c,&a);
351                 if(!BN_is_zero(&c))
352                     {
353                     fprintf(stderr,"Add test failed!\n");
354                     return 0;
355                     }
356                 }
357         BN_free(&a);
358         BN_free(&b);
359         BN_free(&c);
360         return(1);
361         }
362
363 int test_sub(BIO *bp)
364         {
365         BIGNUM a,b,c;
366         int i;
367
368         BN_init(&a);
369         BN_init(&b);
370         BN_init(&c);
371
372         for (i=0; i<num0+num1; i++)
373                 {
374                 if (i < num1)
375                         {
376                         BN_bntest_rand(&a,512,0,0);
377                         BN_copy(&b,&a);
378                         if (BN_set_bit(&a,i)==0) return(0);
379                         BN_add_word(&b,i);
380                         }
381                 else
382                         {
383                         BN_bntest_rand(&b,400+i-num1,0,0);
384                         a.neg=rand_neg();
385                         b.neg=rand_neg();
386                         }
387                 BN_sub(&c,&a,&b);
388                 if (bp != NULL)
389                         {
390                         if (!results)
391                                 {
392                                 BN_print(bp,&a);
393                                 BIO_puts(bp," - ");
394                                 BN_print(bp,&b);
395                                 BIO_puts(bp," - ");
396                                 }
397                         BN_print(bp,&c);
398                         BIO_puts(bp,"\n");
399                         }
400                 BN_add(&c,&c,&b);
401                 BN_sub(&c,&c,&a);
402                 if(!BN_is_zero(&c))
403                     {
404                     fprintf(stderr,"Subtract test failed!\n");
405                     return 0;
406                     }
407                 }
408         BN_free(&a);
409         BN_free(&b);
410         BN_free(&c);
411         return(1);
412         }
413
414 int test_div(BIO *bp, BN_CTX *ctx)
415         {
416         BIGNUM a,b,c,d,e;
417         int i;
418
419         BN_init(&a);
420         BN_init(&b);
421         BN_init(&c);
422         BN_init(&d);
423         BN_init(&e);
424
425         for (i=0; i<num0+num1; i++)
426                 {
427                 if (i < num1)
428                         {
429                         BN_bntest_rand(&a,400,0,0);
430                         BN_copy(&b,&a);
431                         BN_lshift(&a,&a,i);
432                         BN_add_word(&a,i);
433                         }
434                 else
435                         BN_bntest_rand(&b,50+3*(i-num1),0,0);
436                 a.neg=rand_neg();
437                 b.neg=rand_neg();
438                 BN_div(&d,&c,&a,&b,ctx);
439                 if (bp != NULL)
440                         {
441                         if (!results)
442                                 {
443                                 BN_print(bp,&a);
444                                 BIO_puts(bp," / ");
445                                 BN_print(bp,&b);
446                                 BIO_puts(bp," - ");
447                                 }
448                         BN_print(bp,&d);
449                         BIO_puts(bp,"\n");
450
451                         if (!results)
452                                 {
453                                 BN_print(bp,&a);
454                                 BIO_puts(bp," % ");
455                                 BN_print(bp,&b);
456                                 BIO_puts(bp," - ");
457                                 }
458                         BN_print(bp,&c);
459                         BIO_puts(bp,"\n");
460                         }
461                 BN_mul(&e,&d,&b,ctx);
462                 BN_add(&d,&e,&c);
463                 BN_sub(&d,&d,&a);
464                 if(!BN_is_zero(&d))
465                     {
466                     fprintf(stderr,"Division test failed!\n");
467                     return 0;
468                     }
469                 }
470         BN_free(&a);
471         BN_free(&b);
472         BN_free(&c);
473         BN_free(&d);
474         BN_free(&e);
475         return(1);
476         }
477
478 static void print_word(BIO *bp,BN_ULONG w)
479         {
480 #ifdef SIXTY_FOUR_BIT
481         if (sizeof(w) > sizeof(unsigned long))
482                 {
483                 unsigned long   h=(unsigned long)(w>>32),
484                                 l=(unsigned long)(w);
485
486                 if (h)  BIO_printf(bp,"%lX%08lX",h,l);
487                 else    BIO_printf(bp,"%lX",l);
488                 return;
489                 }
490 #endif
491         BIO_printf(bp,BN_HEX_FMT1,w);
492         }
493
494 int test_div_word(BIO *bp)
495         {
496         BIGNUM   a,b;
497         BN_ULONG r,s;
498         int i;
499
500         BN_init(&a);
501         BN_init(&b);
502
503         for (i=0; i<num0; i++)
504                 {
505                 do {
506                         BN_bntest_rand(&a,512,-1,0);
507                         BN_bntest_rand(&b,BN_BITS2,-1,0);
508                         s = b.d[0];
509                 } while (!s);
510
511                 BN_copy(&b, &a);
512                 r = BN_div_word(&b, s);
513
514                 if (bp != NULL)
515                         {
516                         if (!results)
517                                 {
518                                 BN_print(bp,&a);
519                                 BIO_puts(bp," / ");
520                                 print_word(bp,s);
521                                 BIO_puts(bp," - ");
522                                 }
523                         BN_print(bp,&b);
524                         BIO_puts(bp,"\n");
525
526                         if (!results)
527                                 {
528                                 BN_print(bp,&a);
529                                 BIO_puts(bp," % ");
530                                 print_word(bp,s);
531                                 BIO_puts(bp," - ");
532                                 }
533                         print_word(bp,r);
534                         BIO_puts(bp,"\n");
535                         }
536                 BN_mul_word(&b,s);
537                 BN_add_word(&b,r);
538                 BN_sub(&b,&a,&b);
539                 if(!BN_is_zero(&b))
540                     {
541                     fprintf(stderr,"Division (word) test failed!\n");
542                     return 0;
543                     }
544                 }
545         BN_free(&a);
546         BN_free(&b);
547         return(1);
548         }
549
550 int test_div_recp(BIO *bp, BN_CTX *ctx)
551         {
552         BIGNUM a,b,c,d,e;
553         BN_RECP_CTX recp;
554         int i;
555
556         BN_RECP_CTX_init(&recp);
557         BN_init(&a);
558         BN_init(&b);
559         BN_init(&c);
560         BN_init(&d);
561         BN_init(&e);
562
563         for (i=0; i<num0+num1; i++)
564                 {
565                 if (i < num1)
566                         {
567                         BN_bntest_rand(&a,400,0,0);
568                         BN_copy(&b,&a);
569                         BN_lshift(&a,&a,i);
570                         BN_add_word(&a,i);
571                         }
572                 else
573                         BN_bntest_rand(&b,50+3*(i-num1),0,0);
574                 a.neg=rand_neg();
575                 b.neg=rand_neg();
576                 BN_RECP_CTX_set(&recp,&b,ctx);
577                 BN_div_recp(&d,&c,&a,&recp,ctx);
578                 if (bp != NULL)
579                         {
580                         if (!results)
581                                 {
582                                 BN_print(bp,&a);
583                                 BIO_puts(bp," / ");
584                                 BN_print(bp,&b);
585                                 BIO_puts(bp," - ");
586                                 }
587                         BN_print(bp,&d);
588                         BIO_puts(bp,"\n");
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                 BN_mul(&e,&d,&b,ctx);
601                 BN_add(&d,&e,&c);
602                 BN_sub(&d,&d,&a);
603                 if(!BN_is_zero(&d))
604                     {
605                     fprintf(stderr,"Reciprocal division test failed!\n");
606                     fprintf(stderr,"a=");
607                     BN_print_fp(stderr,&a);
608                     fprintf(stderr,"\nb=");
609                     BN_print_fp(stderr,&b);
610                     fprintf(stderr,"\n");
611                     return 0;
612                     }
613                 }
614         BN_free(&a);
615         BN_free(&b);
616         BN_free(&c);
617         BN_free(&d);
618         BN_free(&e);
619         BN_RECP_CTX_free(&recp);
620         return(1);
621         }
622
623 int test_mul(BIO *bp)
624         {
625         BIGNUM a,b,c,d,e;
626         int i;
627         BN_CTX *ctx;
628
629         ctx = BN_CTX_new();
630         if (ctx == NULL) EXIT(1);
631         
632         BN_init(&a);
633         BN_init(&b);
634         BN_init(&c);
635         BN_init(&d);
636         BN_init(&e);
637
638         for (i=0; i<num0+num1; i++)
639                 {
640                 if (i <= num1)
641                         {
642                         BN_bntest_rand(&a,100,0,0);
643                         BN_bntest_rand(&b,100,0,0);
644                         }
645                 else
646                         BN_bntest_rand(&b,i-num1,0,0);
647                 a.neg=rand_neg();
648                 b.neg=rand_neg();
649                 BN_mul(&c,&a,&b,ctx);
650                 if (bp != NULL)
651                         {
652                         if (!results)
653                                 {
654                                 BN_print(bp,&a);
655                                 BIO_puts(bp," * ");
656                                 BN_print(bp,&b);
657                                 BIO_puts(bp," - ");
658                                 }
659                         BN_print(bp,&c);
660                         BIO_puts(bp,"\n");
661                         }
662                 BN_div(&d,&e,&c,&a,ctx);
663                 BN_sub(&d,&d,&b);
664                 if(!BN_is_zero(&d) || !BN_is_zero(&e))
665                     {
666                     fprintf(stderr,"Multiplication test failed!\n");
667                     return 0;
668                     }
669                 }
670         BN_free(&a);
671         BN_free(&b);
672         BN_free(&c);
673         BN_free(&d);
674         BN_free(&e);
675         BN_CTX_free(ctx);
676         return(1);
677         }
678
679 int test_sqr(BIO *bp, BN_CTX *ctx)
680         {
681         BIGNUM *a,*c,*d,*e;
682         int i, ret = 0;
683
684         a = BN_new();
685         c = BN_new();
686         d = BN_new();
687         e = BN_new();
688         if (a == NULL || c == NULL || d == NULL || e == NULL)
689                 {
690                 goto err;
691                 }
692
693         for (i=0; i<num0; i++)
694                 {
695                 BN_bntest_rand(a,40+i*10,0,0);
696                 a->neg=rand_neg();
697                 BN_sqr(c,a,ctx);
698                 if (bp != NULL)
699                         {
700                         if (!results)
701                                 {
702                                 BN_print(bp,a);
703                                 BIO_puts(bp," * ");
704                                 BN_print(bp,a);
705                                 BIO_puts(bp," - ");
706                                 }
707                         BN_print(bp,c);
708                         BIO_puts(bp,"\n");
709                         }
710                 BN_div(d,e,c,a,ctx);
711                 BN_sub(d,d,a);
712                 if(!BN_is_zero(d) || !BN_is_zero(e))
713                         {
714                         fprintf(stderr,"Square test failed!\n");
715                         goto err;
716                         }
717                 }
718
719         /* Regression test for a BN_sqr overflow bug. */
720         BN_hex2bn(&a,
721                 "80000000000000008000000000000001"
722                 "FFFFFFFFFFFFFFFE0000000000000000");
723         BN_sqr(c, a, ctx);
724         if (bp != NULL)
725                 {
726                 if (!results)
727                         {
728                         BN_print(bp,a);
729                         BIO_puts(bp," * ");
730                         BN_print(bp,a);
731                         BIO_puts(bp," - ");
732                         }
733                 BN_print(bp,c);
734                 BIO_puts(bp,"\n");
735                 }
736         BN_mul(d, a, a, ctx);
737         if (BN_cmp(c, d))
738                 {
739                 fprintf(stderr, "Square test failed: BN_sqr and BN_mul produce "
740                         "different results!\n");
741                 goto err;
742                 }
743
744         /* Regression test for a BN_sqr overflow bug. */
745         BN_hex2bn(&a,
746                 "80000000000000000000000080000001"
747                 "FFFFFFFE000000000000000000000000");
748         BN_sqr(c, a, ctx);
749         if (bp != NULL)
750                 {
751                 if (!results)
752                         {
753                         BN_print(bp,a);
754                         BIO_puts(bp," * ");
755                         BN_print(bp,a);
756                         BIO_puts(bp," - ");
757                         }
758                 BN_print(bp,c);
759                 BIO_puts(bp,"\n");
760                 }
761         BN_mul(d, a, a, ctx);
762         if (BN_cmp(c, d))
763                 {
764                 fprintf(stderr, "Square test failed: BN_sqr and BN_mul produce "
765                         "different results!\n");
766                 goto err;
767                 }
768         ret = 1;
769 err:
770         if (a != NULL) BN_free(a);
771         if (c != NULL) BN_free(c);
772         if (d != NULL) BN_free(d);
773         if (e != NULL) BN_free(e);
774         return ret;
775         }
776
777 int test_mont(BIO *bp, BN_CTX *ctx)
778         {
779         BIGNUM a,b,c,d,A,B;
780         BIGNUM n;
781         int i;
782         BN_MONT_CTX *mont;
783
784         BN_init(&a);
785         BN_init(&b);
786         BN_init(&c);
787         BN_init(&d);
788         BN_init(&A);
789         BN_init(&B);
790         BN_init(&n);
791
792         mont=BN_MONT_CTX_new();
793         if (mont == NULL)
794                 return 0;
795
796         BN_bntest_rand(&a,100,0,0);
797         BN_bntest_rand(&b,100,0,0);
798         for (i=0; i<num2; i++)
799                 {
800                 int bits = (200*(i+1))/num2;
801
802                 if (bits == 0)
803                         continue;
804                 BN_bntest_rand(&n,bits,0,1);
805                 BN_MONT_CTX_set(mont,&n,ctx);
806
807                 BN_nnmod(&a,&a,&n,ctx);
808                 BN_nnmod(&b,&b,&n,ctx);
809
810                 BN_to_montgomery(&A,&a,mont,ctx);
811                 BN_to_montgomery(&B,&b,mont,ctx);
812
813                 BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);
814                 BN_from_montgomery(&A,&c,mont,ctx);
815                 if (bp != NULL)
816                         {
817                         if (!results)
818                                 {
819 #ifdef undef
820 fprintf(stderr,"%d * %d %% %d\n",
821 BN_num_bits(&a),
822 BN_num_bits(&b),
823 BN_num_bits(mont->N));
824 #endif
825                                 BN_print(bp,&a);
826                                 BIO_puts(bp," * ");
827                                 BN_print(bp,&b);
828                                 BIO_puts(bp," % ");
829                                 BN_print(bp,&(mont->N));
830                                 BIO_puts(bp," - ");
831                                 }
832                         BN_print(bp,&A);
833                         BIO_puts(bp,"\n");
834                         }
835                 BN_mod_mul(&d,&a,&b,&n,ctx);
836                 BN_sub(&d,&d,&A);
837                 if(!BN_is_zero(&d))
838                     {
839                     fprintf(stderr,"Montgomery multiplication test failed!\n");
840                     return 0;
841                     }
842                 }
843         BN_MONT_CTX_free(mont);
844         BN_free(&a);
845         BN_free(&b);
846         BN_free(&c);
847         BN_free(&d);
848         BN_free(&A);
849         BN_free(&B);
850         BN_free(&n);
851         return(1);
852         }
853
854 int test_mod(BIO *bp, BN_CTX *ctx)
855         {
856         BIGNUM *a,*b,*c,*d,*e;
857         int i;
858
859         a=BN_new();
860         b=BN_new();
861         c=BN_new();
862         d=BN_new();
863         e=BN_new();
864
865         BN_bntest_rand(a,1024,0,0);
866         for (i=0; i<num0; i++)
867                 {
868                 BN_bntest_rand(b,450+i*10,0,0);
869                 a->neg=rand_neg();
870                 b->neg=rand_neg();
871                 BN_mod(c,a,b,ctx);
872                 if (bp != NULL)
873                         {
874                         if (!results)
875                                 {
876                                 BN_print(bp,a);
877                                 BIO_puts(bp," % ");
878                                 BN_print(bp,b);
879                                 BIO_puts(bp," - ");
880                                 }
881                         BN_print(bp,c);
882                         BIO_puts(bp,"\n");
883                         }
884                 BN_div(d,e,a,b,ctx);
885                 BN_sub(e,e,c);
886                 if(!BN_is_zero(e))
887                     {
888                     fprintf(stderr,"Modulo test failed!\n");
889                     return 0;
890                     }
891                 }
892         BN_free(a);
893         BN_free(b);
894         BN_free(c);
895         BN_free(d);
896         BN_free(e);
897         return(1);
898         }
899
900 int test_mod_mul(BIO *bp, BN_CTX *ctx)
901         {
902         BIGNUM *a,*b,*c,*d,*e;
903         int i,j;
904
905         a=BN_new();
906         b=BN_new();
907         c=BN_new();
908         d=BN_new();
909         e=BN_new();
910
911         for (j=0; j<3; j++) {
912         BN_bntest_rand(c,1024,0,0);
913         for (i=0; i<num0; i++)
914                 {
915                 BN_bntest_rand(a,475+i*10,0,0);
916                 BN_bntest_rand(b,425+i*11,0,0);
917                 a->neg=rand_neg();
918                 b->neg=rand_neg();
919                 if (!BN_mod_mul(e,a,b,c,ctx))
920                         {
921                         unsigned long l;
922
923                         while ((l=ERR_get_error()))
924                                 fprintf(stderr,"ERROR:%s\n",
925                                         ERR_error_string(l,NULL));
926                         EXIT(1);
927                         }
928                 if (bp != NULL)
929                         {
930                         if (!results)
931                                 {
932                                 BN_print(bp,a);
933                                 BIO_puts(bp," * ");
934                                 BN_print(bp,b);
935                                 BIO_puts(bp," % ");
936                                 BN_print(bp,c);
937                                 if ((a->neg ^ b->neg) && !BN_is_zero(e))
938                                         {
939                                         /* If  (a*b) % c  is negative,  c  must be added
940                                          * in order to obtain the normalized remainder
941                                          * (new with OpenSSL 0.9.7, previous versions of
942                                          * BN_mod_mul could generate negative results)
943                                          */
944                                         BIO_puts(bp," + ");
945                                         BN_print(bp,c);
946                                         }
947                                 BIO_puts(bp," - ");
948                                 }
949                         BN_print(bp,e);
950                         BIO_puts(bp,"\n");
951                         }
952                 BN_mul(d,a,b,ctx);
953                 BN_sub(d,d,e);
954                 BN_div(a,b,d,c,ctx);
955                 if(!BN_is_zero(b))
956                     {
957                     fprintf(stderr,"Modulo multiply test failed!\n");
958                     ERR_print_errors_fp(stderr);
959                     return 0;
960                     }
961                 }
962         }
963         BN_free(a);
964         BN_free(b);
965         BN_free(c);
966         BN_free(d);
967         BN_free(e);
968         return(1);
969         }
970
971 int test_mod_exp(BIO *bp, BN_CTX *ctx)
972         {
973         BIGNUM *a,*b,*c,*d,*e;
974         int i;
975
976         a=BN_new();
977         b=BN_new();
978         c=BN_new();
979         d=BN_new();
980         e=BN_new();
981
982         BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */
983         for (i=0; i<num2; i++)
984                 {
985                 BN_bntest_rand(a,20+i*5,0,0);
986                 BN_bntest_rand(b,2+i,0,0);
987
988                 if (!BN_mod_exp(d,a,b,c,ctx))
989                         return(0);
990
991                 if (bp != NULL)
992                         {
993                         if (!results)
994                                 {
995                                 BN_print(bp,a);
996                                 BIO_puts(bp," ^ ");
997                                 BN_print(bp,b);
998                                 BIO_puts(bp," % ");
999                                 BN_print(bp,c);
1000                                 BIO_puts(bp," - ");
1001                                 }
1002                         BN_print(bp,d);
1003                         BIO_puts(bp,"\n");
1004                         }
1005                 BN_exp(e,a,b,ctx);
1006                 BN_sub(e,e,d);
1007                 BN_div(a,b,e,c,ctx);
1008                 if(!BN_is_zero(b))
1009                     {
1010                     fprintf(stderr,"Modulo exponentiation test failed!\n");
1011                     return 0;
1012                     }
1013                 }
1014         BN_free(a);
1015         BN_free(b);
1016         BN_free(c);
1017         BN_free(d);
1018         BN_free(e);
1019         return(1);
1020         }
1021
1022 int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx)
1023         {
1024         BIGNUM *a,*b,*c,*d,*e;
1025         int i;
1026
1027         a=BN_new();
1028         b=BN_new();
1029         c=BN_new();
1030         d=BN_new();
1031         e=BN_new();
1032
1033         BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */
1034         for (i=0; i<num2; i++)
1035                 {
1036                 BN_bntest_rand(a,20+i*5,0,0);
1037                 BN_bntest_rand(b,2+i,0,0);
1038
1039                 if (!BN_mod_exp_mont_consttime(d,a,b,c,ctx,NULL))
1040                         return(00);
1041
1042                 if (bp != NULL)
1043                         {
1044                         if (!results)
1045                                 {
1046                                 BN_print(bp,a);
1047                                 BIO_puts(bp," ^ ");
1048                                 BN_print(bp,b);
1049                                 BIO_puts(bp," % ");
1050                                 BN_print(bp,c);
1051                                 BIO_puts(bp," - ");
1052                                 }
1053                         BN_print(bp,d);
1054                         BIO_puts(bp,"\n");
1055                         }
1056                 BN_exp(e,a,b,ctx);
1057                 BN_sub(e,e,d);
1058                 BN_div(a,b,e,c,ctx);
1059                 if(!BN_is_zero(b))
1060                     {
1061                     fprintf(stderr,"Modulo exponentiation test failed!\n");
1062                     return 0;
1063                     }
1064                 }
1065         BN_free(a);
1066         BN_free(b);
1067         BN_free(c);
1068         BN_free(d);
1069         BN_free(e);
1070         return(1);
1071         }
1072
1073 /* Test constant-time modular exponentiation with 1024-bit inputs,
1074  * which on x86_64 cause a different code branch to be taken.
1075  */
1076 int test_mod_exp_mont5(BIO *bp, BN_CTX *ctx)
1077         {
1078         BIGNUM *a,*p,*m,*d,*e;
1079
1080         BN_MONT_CTX *mont;
1081
1082         a=BN_new();
1083         p=BN_new();
1084         m=BN_new();
1085         d=BN_new();
1086         e=BN_new();
1087
1088         mont = BN_MONT_CTX_new();
1089
1090         BN_bntest_rand(m,1024,0,1); /* must be odd for montgomery */
1091         /* Zero exponent */
1092         BN_bntest_rand(a,1024,0,0);
1093         BN_zero(p);
1094         if(!BN_mod_exp_mont_consttime(d,a,p,m,ctx,NULL))
1095                 return 0;
1096         if(!BN_is_one(d))
1097                 {
1098                 fprintf(stderr, "Modular exponentiation test failed!\n");
1099                 return 0;
1100                 }
1101         /* Zero input */
1102         BN_bntest_rand(p,1024,0,0);
1103         BN_zero(a);
1104         if(!BN_mod_exp_mont_consttime(d,a,p,m,ctx,NULL))
1105                 return 0;
1106         if(!BN_is_zero(d))
1107                 {
1108                 fprintf(stderr, "Modular exponentiation test failed!\n");
1109                 return 0;
1110                 }
1111         /* Craft an input whose Montgomery representation is 1,
1112          * i.e., shorter than the modulus m, in order to test
1113          * the const time precomputation scattering/gathering.
1114          */
1115         BN_one(a);
1116         BN_MONT_CTX_set(mont,m,ctx);
1117         if(!BN_from_montgomery(e,a,mont,ctx))
1118                 return 0;
1119         if(!BN_mod_exp_mont_consttime(d,e,p,m,ctx,NULL))
1120                 return 0;
1121         if(!BN_mod_exp_simple(a,e,p,m,ctx))
1122                 return 0;
1123         if(BN_cmp(a,d) != 0)
1124                 {
1125                 fprintf(stderr,"Modular exponentiation test failed!\n");
1126                 return 0;
1127                 }
1128         /* Finally, some regular test vectors. */
1129         BN_bntest_rand(e,1024,0,0);
1130         if(!BN_mod_exp_mont_consttime(d,e,p,m,ctx,NULL))
1131                 return 0;
1132         if(!BN_mod_exp_simple(a,e,p,m,ctx))
1133                 return 0;
1134         if(BN_cmp(a,d) != 0)
1135                 {
1136                 fprintf(stderr,"Modular exponentiation test failed!\n");
1137                 return 0;
1138                 }
1139         BN_free(a);
1140         BN_free(p);
1141         BN_free(m);
1142         BN_free(d);
1143         BN_free(e);
1144         return(1);
1145         }
1146
1147 int test_exp(BIO *bp, BN_CTX *ctx)
1148         {
1149         BIGNUM *a,*b,*d,*e,*one;
1150         int i;
1151
1152         a=BN_new();
1153         b=BN_new();
1154         d=BN_new();
1155         e=BN_new();
1156         one=BN_new();
1157         BN_one(one);
1158
1159         for (i=0; i<num2; i++)
1160                 {
1161                 BN_bntest_rand(a,20+i*5,0,0);
1162                 BN_bntest_rand(b,2+i,0,0);
1163
1164                 if (BN_exp(d,a,b,ctx) <= 0)
1165                         return(0);
1166
1167                 if (bp != NULL)
1168                         {
1169                         if (!results)
1170                                 {
1171                                 BN_print(bp,a);
1172                                 BIO_puts(bp," ^ ");
1173                                 BN_print(bp,b);
1174                                 BIO_puts(bp," - ");
1175                                 }
1176                         BN_print(bp,d);
1177                         BIO_puts(bp,"\n");
1178                         }
1179                 BN_one(e);
1180                 for( ; !BN_is_zero(b) ; BN_sub(b,b,one))
1181                     BN_mul(e,e,a,ctx);
1182                 BN_sub(e,e,d);
1183                 if(!BN_is_zero(e))
1184                     {
1185                     fprintf(stderr,"Exponentiation test failed!\n");
1186                     return 0;
1187                     }
1188                 }
1189         BN_free(a);
1190         BN_free(b);
1191         BN_free(d);
1192         BN_free(e);
1193         BN_free(one);
1194         return(1);
1195         }
1196 #ifndef OPENSSL_NO_EC2M
1197 int test_gf2m_add(BIO *bp)
1198         {
1199         BIGNUM a,b,c;
1200         int i, ret = 0;
1201
1202         BN_init(&a);
1203         BN_init(&b);
1204         BN_init(&c);
1205
1206         for (i=0; i<num0; i++)
1207                 {
1208                 BN_rand(&a,512,0,0);
1209                 BN_copy(&b, BN_value_one());
1210                 a.neg=rand_neg();
1211                 b.neg=rand_neg();
1212                 BN_GF2m_add(&c,&a,&b);
1213 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1214                 if (bp != NULL)
1215                         {
1216                         if (!results)
1217                                 {
1218                                 BN_print(bp,&a);
1219                                 BIO_puts(bp," ^ ");
1220                                 BN_print(bp,&b);
1221                                 BIO_puts(bp," = ");
1222                                 }
1223                         BN_print(bp,&c);
1224                         BIO_puts(bp,"\n");
1225                         }
1226 #endif
1227                 /* Test that two added values have the correct parity. */
1228                 if((BN_is_odd(&a) && BN_is_odd(&c)) || (!BN_is_odd(&a) && !BN_is_odd(&c)))
1229                         {
1230                     fprintf(stderr,"GF(2^m) addition test (a) failed!\n");
1231                         goto err;
1232                         }
1233                 BN_GF2m_add(&c,&c,&c);
1234                 /* Test that c + c = 0. */
1235                 if(!BN_is_zero(&c))
1236                     {
1237                     fprintf(stderr,"GF(2^m) addition test (b) failed!\n");
1238                         goto err;
1239                     }
1240                 }
1241         ret = 1;
1242   err:
1243         BN_free(&a);
1244         BN_free(&b);
1245         BN_free(&c);
1246         return ret;
1247         }
1248
1249 int test_gf2m_mod(BIO *bp)
1250         {
1251         BIGNUM *a,*b[2],*c,*d,*e;
1252         int i, j, ret = 0;
1253         int p0[] = {163,7,6,3,0,-1};
1254         int p1[] = {193,15,0,-1};
1255
1256         a=BN_new();
1257         b[0]=BN_new();
1258         b[1]=BN_new();
1259         c=BN_new();
1260         d=BN_new();
1261         e=BN_new();
1262
1263         BN_GF2m_arr2poly(p0, b[0]);
1264         BN_GF2m_arr2poly(p1, b[1]);
1265
1266         for (i=0; i<num0; i++)
1267                 {
1268                 BN_bntest_rand(a, 1024, 0, 0);
1269                 for (j=0; j < 2; j++)
1270                         {
1271                         BN_GF2m_mod(c, a, b[j]);
1272 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1273                         if (bp != NULL)
1274                                 {
1275                                 if (!results)
1276                                         {
1277                                         BN_print(bp,a);
1278                                         BIO_puts(bp," % ");
1279                                         BN_print(bp,b[j]);
1280                                         BIO_puts(bp," - ");
1281                                         BN_print(bp,c);
1282                                         BIO_puts(bp,"\n");
1283                                         }
1284                                 }
1285 #endif
1286                         BN_GF2m_add(d, a, c);
1287                         BN_GF2m_mod(e, d, b[j]);
1288                         /* Test that a + (a mod p) mod p == 0. */
1289                         if(!BN_is_zero(e))
1290                                 {
1291                                 fprintf(stderr,"GF(2^m) modulo test failed!\n");
1292                                 goto err;
1293                                 }
1294                         }
1295                 }
1296         ret = 1;
1297   err:
1298         BN_free(a);
1299         BN_free(b[0]);
1300         BN_free(b[1]);
1301         BN_free(c);
1302         BN_free(d);
1303         BN_free(e);
1304         return ret;
1305         }
1306
1307 int test_gf2m_mod_mul(BIO *bp,BN_CTX *ctx)
1308         {
1309         BIGNUM *a,*b[2],*c,*d,*e,*f,*g,*h;
1310         int i, j, ret = 0;
1311         int p0[] = {163,7,6,3,0,-1};
1312         int p1[] = {193,15,0,-1};
1313
1314         a=BN_new();
1315         b[0]=BN_new();
1316         b[1]=BN_new();
1317         c=BN_new();
1318         d=BN_new();
1319         e=BN_new();
1320         f=BN_new();
1321         g=BN_new();
1322         h=BN_new();
1323
1324         BN_GF2m_arr2poly(p0, b[0]);
1325         BN_GF2m_arr2poly(p1, b[1]);
1326
1327         for (i=0; i<num0; i++)
1328                 {
1329                 BN_bntest_rand(a, 1024, 0, 0);
1330                 BN_bntest_rand(c, 1024, 0, 0);
1331                 BN_bntest_rand(d, 1024, 0, 0);
1332                 for (j=0; j < 2; j++)
1333                         {
1334                         BN_GF2m_mod_mul(e, a, c, b[j], ctx);
1335 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1336                         if (bp != NULL)
1337                                 {
1338                                 if (!results)
1339                                         {
1340                                         BN_print(bp,a);
1341                                         BIO_puts(bp," * ");
1342                                         BN_print(bp,c);
1343                                         BIO_puts(bp," % ");
1344                                         BN_print(bp,b[j]);
1345                                         BIO_puts(bp," - ");
1346                                         BN_print(bp,e);
1347                                         BIO_puts(bp,"\n");
1348                                         }
1349                                 }
1350 #endif
1351                         BN_GF2m_add(f, a, d);
1352                         BN_GF2m_mod_mul(g, f, c, b[j], ctx);
1353                         BN_GF2m_mod_mul(h, d, c, b[j], ctx);
1354                         BN_GF2m_add(f, e, g);
1355                         BN_GF2m_add(f, f, h);
1356                         /* Test that (a+d)*c = a*c + d*c. */
1357                         if(!BN_is_zero(f))
1358                                 {
1359                                 fprintf(stderr,"GF(2^m) modular multiplication test failed!\n");
1360                                 goto err;
1361                                 }
1362                         }
1363                 }
1364         ret = 1;
1365   err:
1366         BN_free(a);
1367         BN_free(b[0]);
1368         BN_free(b[1]);
1369         BN_free(c);
1370         BN_free(d);
1371         BN_free(e);
1372         BN_free(f);
1373         BN_free(g);
1374         BN_free(h);
1375         return ret;
1376         }
1377
1378 int test_gf2m_mod_sqr(BIO *bp,BN_CTX *ctx)
1379         {
1380         BIGNUM *a,*b[2],*c,*d;
1381         int i, j, ret = 0;
1382         int p0[] = {163,7,6,3,0,-1};
1383         int p1[] = {193,15,0,-1};
1384
1385         a=BN_new();
1386         b[0]=BN_new();
1387         b[1]=BN_new();
1388         c=BN_new();
1389         d=BN_new();
1390
1391         BN_GF2m_arr2poly(p0, b[0]);
1392         BN_GF2m_arr2poly(p1, b[1]);
1393
1394         for (i=0; i<num0; i++)
1395                 {
1396                 BN_bntest_rand(a, 1024, 0, 0);
1397                 for (j=0; j < 2; j++)
1398                         {
1399                         BN_GF2m_mod_sqr(c, a, b[j], ctx);
1400                         BN_copy(d, a);
1401                         BN_GF2m_mod_mul(d, a, d, b[j], ctx);
1402 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1403                         if (bp != NULL)
1404                                 {
1405                                 if (!results)
1406                                         {
1407                                         BN_print(bp,a);
1408                                         BIO_puts(bp," ^ 2 % ");
1409                                         BN_print(bp,b[j]);
1410                                         BIO_puts(bp, " = ");
1411                                         BN_print(bp,c);
1412                                         BIO_puts(bp,"; a * a = ");
1413                                         BN_print(bp,d);
1414                                         BIO_puts(bp,"\n");
1415                                         }
1416                                 }
1417 #endif
1418                         BN_GF2m_add(d, c, d);
1419                         /* Test that a*a = a^2. */
1420                         if(!BN_is_zero(d))
1421                                 {
1422                                 fprintf(stderr,"GF(2^m) modular squaring test failed!\n");
1423                                 goto err;
1424                                 }
1425                         }
1426                 }
1427         ret = 1;
1428   err:
1429         BN_free(a);
1430         BN_free(b[0]);
1431         BN_free(b[1]);
1432         BN_free(c);
1433         BN_free(d);
1434         return ret;
1435         }
1436
1437 int test_gf2m_mod_inv(BIO *bp,BN_CTX *ctx)
1438         {
1439         BIGNUM *a,*b[2],*c,*d;
1440         int i, j, ret = 0;
1441         int p0[] = {163,7,6,3,0,-1};
1442         int p1[] = {193,15,0,-1};
1443
1444         a=BN_new();
1445         b[0]=BN_new();
1446         b[1]=BN_new();
1447         c=BN_new();
1448         d=BN_new();
1449
1450         BN_GF2m_arr2poly(p0, b[0]);
1451         BN_GF2m_arr2poly(p1, b[1]);
1452
1453         for (i=0; i<num0; i++)
1454                 {
1455                 BN_bntest_rand(a, 512, 0, 0); 
1456                 for (j=0; j < 2; j++)
1457                         {
1458                         BN_GF2m_mod_inv(c, a, b[j], ctx);
1459                         BN_GF2m_mod_mul(d, a, c, b[j], ctx);
1460 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1461                         if (bp != NULL)
1462                                 {
1463                                 if (!results)
1464                                         {
1465                                         BN_print(bp,a);
1466                                         BIO_puts(bp, " * ");
1467                                         BN_print(bp,c);
1468                                         BIO_puts(bp," - 1 % ");
1469                                         BN_print(bp,b[j]);
1470                                         BIO_puts(bp,"\n");
1471                                         }
1472                                 }
1473 #endif
1474                         /* Test that ((1/a)*a) = 1. */
1475                         if(!BN_is_one(d))
1476                                 {
1477                                 fprintf(stderr,"GF(2^m) modular inversion test failed!\n");
1478                                 goto err;
1479                                 }
1480                         }
1481                 }
1482         ret = 1;
1483   err:
1484         BN_free(a);
1485         BN_free(b[0]);
1486         BN_free(b[1]);
1487         BN_free(c);
1488         BN_free(d);
1489         return ret;
1490         }
1491
1492 int test_gf2m_mod_div(BIO *bp,BN_CTX *ctx)
1493         {
1494         BIGNUM *a,*b[2],*c,*d,*e,*f;
1495         int i, j, ret = 0;
1496         int p0[] = {163,7,6,3,0,-1};
1497         int p1[] = {193,15,0,-1};
1498
1499         a=BN_new();
1500         b[0]=BN_new();
1501         b[1]=BN_new();
1502         c=BN_new();
1503         d=BN_new();
1504         e=BN_new();
1505         f=BN_new();
1506
1507         BN_GF2m_arr2poly(p0, b[0]);
1508         BN_GF2m_arr2poly(p1, b[1]);
1509
1510         for (i=0; i<num0; i++)
1511                 {
1512                 BN_bntest_rand(a, 512, 0, 0); 
1513                 BN_bntest_rand(c, 512, 0, 0);
1514                 for (j=0; j < 2; j++)
1515                         {
1516                         BN_GF2m_mod_div(d, a, c, b[j], ctx);
1517                         BN_GF2m_mod_mul(e, d, c, b[j], ctx);
1518                         BN_GF2m_mod_div(f, a, e, b[j], ctx);
1519 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1520                         if (bp != NULL)
1521                                 {
1522                                 if (!results)
1523                                         {
1524                                         BN_print(bp,a);
1525                                         BIO_puts(bp, " = ");
1526                                         BN_print(bp,c);
1527                                         BIO_puts(bp," * ");
1528                                         BN_print(bp,d);
1529                                         BIO_puts(bp, " % ");
1530                                         BN_print(bp,b[j]);
1531                                         BIO_puts(bp,"\n");
1532                                         }
1533                                 }
1534 #endif
1535                         /* Test that ((a/c)*c)/a = 1. */
1536                         if(!BN_is_one(f))
1537                                 {
1538                                 fprintf(stderr,"GF(2^m) modular division test failed!\n");
1539                                 goto err;
1540                                 }
1541                         }
1542                 }
1543         ret = 1;
1544   err:
1545         BN_free(a);
1546         BN_free(b[0]);
1547         BN_free(b[1]);
1548         BN_free(c);
1549         BN_free(d);
1550         BN_free(e);
1551         BN_free(f);
1552         return ret;
1553         }
1554
1555 int test_gf2m_mod_exp(BIO *bp,BN_CTX *ctx)
1556         {
1557         BIGNUM *a,*b[2],*c,*d,*e,*f;
1558         int i, j, ret = 0;
1559         int p0[] = {163,7,6,3,0,-1};
1560         int p1[] = {193,15,0,-1};
1561
1562         a=BN_new();
1563         b[0]=BN_new();
1564         b[1]=BN_new();
1565         c=BN_new();
1566         d=BN_new();
1567         e=BN_new();
1568         f=BN_new();
1569
1570         BN_GF2m_arr2poly(p0, b[0]);
1571         BN_GF2m_arr2poly(p1, b[1]);
1572
1573         for (i=0; i<num0; i++)
1574                 {
1575                 BN_bntest_rand(a, 512, 0, 0);
1576                 BN_bntest_rand(c, 512, 0, 0);
1577                 BN_bntest_rand(d, 512, 0, 0);
1578                 for (j=0; j < 2; j++)
1579                         {
1580                         BN_GF2m_mod_exp(e, a, c, b[j], ctx);
1581                         BN_GF2m_mod_exp(f, a, d, b[j], ctx);
1582                         BN_GF2m_mod_mul(e, e, f, b[j], ctx);
1583                         BN_add(f, c, d);
1584                         BN_GF2m_mod_exp(f, a, f, b[j], ctx);
1585 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1586                         if (bp != NULL)
1587                                 {
1588                                 if (!results)
1589                                         {
1590                                         BN_print(bp,a);
1591                                         BIO_puts(bp, " ^ (");
1592                                         BN_print(bp,c);
1593                                         BIO_puts(bp," + ");
1594                                         BN_print(bp,d);
1595                                         BIO_puts(bp, ") = ");
1596                                         BN_print(bp,e);
1597                                         BIO_puts(bp, "; - ");
1598                                         BN_print(bp,f);
1599                                         BIO_puts(bp, " % ");
1600                                         BN_print(bp,b[j]);
1601                                         BIO_puts(bp,"\n");
1602                                         }
1603                                 }
1604 #endif
1605                         BN_GF2m_add(f, e, f);
1606                         /* Test that a^(c+d)=a^c*a^d. */
1607                         if(!BN_is_zero(f))
1608                                 {
1609                                 fprintf(stderr,"GF(2^m) modular exponentiation test failed!\n");
1610                                 goto err;
1611                                 }
1612                         }
1613                 }
1614         ret = 1;
1615   err:
1616         BN_free(a);
1617         BN_free(b[0]);
1618         BN_free(b[1]);
1619         BN_free(c);
1620         BN_free(d);
1621         BN_free(e);
1622         BN_free(f);
1623         return ret;
1624         }
1625
1626 int test_gf2m_mod_sqrt(BIO *bp,BN_CTX *ctx)
1627         {
1628         BIGNUM *a,*b[2],*c,*d,*e,*f;
1629         int i, j, ret = 0;
1630         int p0[] = {163,7,6,3,0,-1};
1631         int p1[] = {193,15,0,-1};
1632
1633         a=BN_new();
1634         b[0]=BN_new();
1635         b[1]=BN_new();
1636         c=BN_new();
1637         d=BN_new();
1638         e=BN_new();
1639         f=BN_new();
1640
1641         BN_GF2m_arr2poly(p0, b[0]);
1642         BN_GF2m_arr2poly(p1, b[1]);
1643
1644         for (i=0; i<num0; i++)
1645                 {
1646                 BN_bntest_rand(a, 512, 0, 0);
1647                 for (j=0; j < 2; j++)
1648                         {
1649                         BN_GF2m_mod(c, a, b[j]);
1650                         BN_GF2m_mod_sqrt(d, a, b[j], ctx);
1651                         BN_GF2m_mod_sqr(e, d, b[j], ctx);
1652 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1653                         if (bp != NULL)
1654                                 {
1655                                 if (!results)
1656                                         {
1657                                         BN_print(bp,d);
1658                                         BIO_puts(bp, " ^ 2 - ");
1659                                         BN_print(bp,a);
1660                                         BIO_puts(bp,"\n");
1661                                         }
1662                                 }
1663 #endif
1664                         BN_GF2m_add(f, c, e);
1665                         /* Test that d^2 = a, where d = sqrt(a). */
1666                         if(!BN_is_zero(f))
1667                                 {
1668                                 fprintf(stderr,"GF(2^m) modular square root test failed!\n");
1669                                 goto err;
1670                                 }
1671                         }
1672                 }
1673         ret = 1;
1674   err:
1675         BN_free(a);
1676         BN_free(b[0]);
1677         BN_free(b[1]);
1678         BN_free(c);
1679         BN_free(d);
1680         BN_free(e);
1681         BN_free(f);
1682         return ret;
1683         }
1684
1685 int test_gf2m_mod_solve_quad(BIO *bp,BN_CTX *ctx)
1686         {
1687         BIGNUM *a,*b[2],*c,*d,*e;
1688         int i, j, s = 0, t, ret = 0;
1689         int p0[] = {163,7,6,3,0,-1};
1690         int p1[] = {193,15,0,-1};
1691
1692         a=BN_new();
1693         b[0]=BN_new();
1694         b[1]=BN_new();
1695         c=BN_new();
1696         d=BN_new();
1697         e=BN_new();
1698
1699         BN_GF2m_arr2poly(p0, b[0]);
1700         BN_GF2m_arr2poly(p1, b[1]);
1701
1702         for (i=0; i<num0; i++)
1703                 {
1704                 BN_bntest_rand(a, 512, 0, 0);
1705                 for (j=0; j < 2; j++)
1706                         {
1707                         t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
1708                         if (t)
1709                                 {
1710                                 s++;
1711                                 BN_GF2m_mod_sqr(d, c, b[j], ctx);
1712                                 BN_GF2m_add(d, c, d);
1713                                 BN_GF2m_mod(e, a, b[j]);
1714 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1715                                 if (bp != NULL)
1716                                         {
1717                                         if (!results)
1718                                                 {
1719                                                 BN_print(bp,c);
1720                                                 BIO_puts(bp, " is root of z^2 + z = ");
1721                                                 BN_print(bp,a);
1722                                                 BIO_puts(bp, " % ");
1723                                                 BN_print(bp,b[j]);
1724                                                 BIO_puts(bp, "\n");
1725                                                 }
1726                                         }
1727 #endif
1728                                 BN_GF2m_add(e, e, d);
1729                                 /* Test that solution of quadratic c satisfies c^2 + c = a. */
1730                                 if(!BN_is_zero(e))
1731                                         {
1732                                         fprintf(stderr,"GF(2^m) modular solve quadratic test failed!\n");
1733                                         goto err;
1734                                         }
1735
1736                                 }
1737                         else 
1738                                 {
1739 #if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1740                                 if (bp != NULL)
1741                                         {
1742                                         if (!results)
1743                                                 {
1744                                                 BIO_puts(bp, "There are no roots of z^2 + z = ");
1745                                                 BN_print(bp,a);
1746                                                 BIO_puts(bp, " % ");
1747                                                 BN_print(bp,b[j]);
1748                                                 BIO_puts(bp, "\n");
1749                                                 }
1750                                         }
1751 #endif
1752                                 }
1753                         }
1754                 }
1755         if (s == 0)
1756                 {       
1757                 fprintf(stderr,"All %i tests of GF(2^m) modular solve quadratic resulted in no roots;\n", num0);
1758                 fprintf(stderr,"this is very unlikely and probably indicates an error.\n");
1759                 goto err;
1760                 }
1761         ret = 1;
1762   err:
1763         BN_free(a);
1764         BN_free(b[0]);
1765         BN_free(b[1]);
1766         BN_free(c);
1767         BN_free(d);
1768         BN_free(e);
1769         return ret;
1770         }
1771 #endif
1772 static int genprime_cb(int p, int n, BN_GENCB *arg)
1773         {
1774         char c='*';
1775
1776         if (p == 0) c='.';
1777         if (p == 1) c='+';
1778         if (p == 2) c='*';
1779         if (p == 3) c='\n';
1780         putc(c, stderr);
1781         fflush(stderr);
1782         return 1;
1783         }
1784
1785 int test_kron(BIO *bp, BN_CTX *ctx)
1786         {
1787         BN_GENCB cb;
1788         BIGNUM *a,*b,*r,*t;
1789         int i;
1790         int legendre, kronecker;
1791         int ret = 0;
1792
1793         a = BN_new();
1794         b = BN_new();
1795         r = BN_new();
1796         t = BN_new();
1797         if (a == NULL || b == NULL || r == NULL || t == NULL) goto err;
1798
1799         BN_GENCB_set(&cb, genprime_cb, NULL);
1800         
1801         /* We test BN_kronecker(a, b, ctx) just for  b  odd (Jacobi symbol).
1802          * In this case we know that if  b  is prime, then BN_kronecker(a, b, ctx)
1803          * is congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol).
1804          * So we generate a random prime  b  and compare these values
1805          * for a number of random  a's.  (That is, we run the Solovay-Strassen
1806          * primality test to confirm that  b  is prime, except that we
1807          * don't want to test whether  b  is prime but whether BN_kronecker
1808          * works.) */
1809
1810         if (!BN_generate_prime_ex(b, 512, 0, NULL, NULL, &cb)) goto err;
1811         b->neg = rand_neg();
1812         putc('\n', stderr);
1813
1814         for (i = 0; i < num0; i++)
1815                 {
1816                 if (!BN_bntest_rand(a, 512, 0, 0)) goto err;
1817                 a->neg = rand_neg();
1818
1819                 /* t := (|b|-1)/2  (note that b is odd) */
1820                 if (!BN_copy(t, b)) goto err;
1821                 t->neg = 0;
1822                 if (!BN_sub_word(t, 1)) goto err;
1823                 if (!BN_rshift1(t, t)) goto err;
1824                 /* r := a^t mod b */
1825                 b->neg=0;
1826                 
1827                 if (!BN_mod_exp_recp(r, a, t, b, ctx)) goto err;
1828                 b->neg=1;
1829
1830                 if (BN_is_word(r, 1))
1831                         legendre = 1;
1832                 else if (BN_is_zero(r))
1833                         legendre = 0;
1834                 else
1835                         {
1836                         if (!BN_add_word(r, 1)) goto err;
1837                         if (0 != BN_ucmp(r, b))
1838                                 {
1839                                 fprintf(stderr, "Legendre symbol computation failed\n");
1840                                 goto err;
1841                                 }
1842                         legendre = -1;
1843                         }
1844                 
1845                 kronecker = BN_kronecker(a, b, ctx);
1846                 if (kronecker < -1) goto err;
1847                 /* we actually need BN_kronecker(a, |b|) */
1848                 if (a->neg && b->neg)
1849                         kronecker = -kronecker;
1850                 
1851                 if (legendre != kronecker)
1852                         {
1853                         fprintf(stderr, "legendre != kronecker; a = ");
1854                         BN_print_fp(stderr, a);
1855                         fprintf(stderr, ", b = ");
1856                         BN_print_fp(stderr, b);
1857                         fprintf(stderr, "\n");
1858                         goto err;
1859                         }
1860
1861                 putc('.', stderr);
1862                 fflush(stderr);
1863                 }
1864
1865         putc('\n', stderr);
1866         fflush(stderr);
1867         ret = 1;
1868  err:
1869         if (a != NULL) BN_free(a);
1870         if (b != NULL) BN_free(b);
1871         if (r != NULL) BN_free(r);
1872         if (t != NULL) BN_free(t);
1873         return ret;
1874         }
1875
1876 int test_sqrt(BIO *bp, BN_CTX *ctx)
1877         {
1878         BN_GENCB cb;
1879         BIGNUM *a,*p,*r;
1880         int i, j;
1881         int ret = 0;
1882
1883         a = BN_new();
1884         p = BN_new();
1885         r = BN_new();
1886         if (a == NULL || p == NULL || r == NULL) goto err;
1887
1888         BN_GENCB_set(&cb, genprime_cb, NULL);
1889
1890         for (i = 0; i < 16; i++)
1891                 {
1892                 if (i < 8)
1893                         {
1894                         unsigned primes[8] = { 2, 3, 5, 7, 11, 13, 17, 19 };
1895                         
1896                         if (!BN_set_word(p, primes[i])) goto err;
1897                         }
1898                 else
1899                         {
1900                         if (!BN_set_word(a, 32)) goto err;
1901                         if (!BN_set_word(r, 2*i + 1)) goto err;
1902                 
1903                         if (!BN_generate_prime_ex(p, 256, 0, a, r, &cb)) goto err;
1904                         putc('\n', stderr);
1905                         }
1906                 p->neg = rand_neg();
1907
1908                 for (j = 0; j < num2; j++)
1909                         {
1910                         /* construct 'a' such that it is a square modulo p,
1911                          * but in general not a proper square and not reduced modulo p */
1912                         if (!BN_bntest_rand(r, 256, 0, 3)) goto err;
1913                         if (!BN_nnmod(r, r, p, ctx)) goto err;
1914                         if (!BN_mod_sqr(r, r, p, ctx)) goto err;
1915                         if (!BN_bntest_rand(a, 256, 0, 3)) goto err;
1916                         if (!BN_nnmod(a, a, p, ctx)) goto err;
1917                         if (!BN_mod_sqr(a, a, p, ctx)) goto err;
1918                         if (!BN_mul(a, a, r, ctx)) goto err;
1919                         if (rand_neg())
1920                                 if (!BN_sub(a, a, p)) goto err;
1921
1922                         if (!BN_mod_sqrt(r, a, p, ctx)) goto err;
1923                         if (!BN_mod_sqr(r, r, p, ctx)) goto err;
1924
1925                         if (!BN_nnmod(a, a, p, ctx)) goto err;
1926
1927                         if (BN_cmp(a, r) != 0)
1928                                 {
1929                                 fprintf(stderr, "BN_mod_sqrt failed: a = ");
1930                                 BN_print_fp(stderr, a);
1931                                 fprintf(stderr, ", r = ");
1932                                 BN_print_fp(stderr, r);
1933                                 fprintf(stderr, ", p = ");
1934                                 BN_print_fp(stderr, p);
1935                                 fprintf(stderr, "\n");
1936                                 goto err;
1937                                 }
1938
1939                         putc('.', stderr);
1940                         fflush(stderr);
1941                         }
1942                 
1943                 putc('\n', stderr);
1944                 fflush(stderr);
1945                 }
1946         ret = 1;
1947  err:
1948         if (a != NULL) BN_free(a);
1949         if (p != NULL) BN_free(p);
1950         if (r != NULL) BN_free(r);
1951         return ret;
1952         }
1953
1954 int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_)
1955         {
1956         BIGNUM *a,*b,*c,*d;
1957         int i;
1958
1959         b=BN_new();
1960         c=BN_new();
1961         d=BN_new();
1962         BN_one(c);
1963
1964         if(a_)
1965             a=a_;
1966         else
1967             {
1968             a=BN_new();
1969             BN_bntest_rand(a,200,0,0);
1970             a->neg=rand_neg();
1971             }
1972         for (i=0; i<num0; i++)
1973                 {
1974                 BN_lshift(b,a,i+1);
1975                 BN_add(c,c,c);
1976                 if (bp != NULL)
1977                         {
1978                         if (!results)
1979                                 {
1980                                 BN_print(bp,a);
1981                                 BIO_puts(bp," * ");
1982                                 BN_print(bp,c);
1983                                 BIO_puts(bp," - ");
1984                                 }
1985                         BN_print(bp,b);
1986                         BIO_puts(bp,"\n");
1987                         }
1988                 BN_mul(d,a,c,ctx);
1989                 BN_sub(d,d,b);
1990                 if(!BN_is_zero(d))
1991                     {
1992                     fprintf(stderr,"Left shift test failed!\n");
1993                     fprintf(stderr,"a=");
1994                     BN_print_fp(stderr,a);
1995                     fprintf(stderr,"\nb=");
1996                     BN_print_fp(stderr,b);
1997                     fprintf(stderr,"\nc=");
1998                     BN_print_fp(stderr,c);
1999                     fprintf(stderr,"\nd=");
2000                     BN_print_fp(stderr,d);
2001                     fprintf(stderr,"\n");
2002                     return 0;
2003                     }
2004                 }
2005         BN_free(a);
2006         BN_free(b);
2007         BN_free(c);
2008         BN_free(d);
2009         return(1);
2010         }
2011
2012 int test_lshift1(BIO *bp)
2013         {
2014         BIGNUM *a,*b,*c;
2015         int i;
2016
2017         a=BN_new();
2018         b=BN_new();
2019         c=BN_new();
2020
2021         BN_bntest_rand(a,200,0,0);
2022         a->neg=rand_neg();
2023         for (i=0; i<num0; i++)
2024                 {
2025                 BN_lshift1(b,a);
2026                 if (bp != NULL)
2027                         {
2028                         if (!results)
2029                                 {
2030                                 BN_print(bp,a);
2031                                 BIO_puts(bp," * 2");
2032                                 BIO_puts(bp," - ");
2033                                 }
2034                         BN_print(bp,b);
2035                         BIO_puts(bp,"\n");
2036                         }
2037                 BN_add(c,a,a);
2038                 BN_sub(a,b,c);
2039                 if(!BN_is_zero(a))
2040                     {
2041                     fprintf(stderr,"Left shift one test failed!\n");
2042                     return 0;
2043                     }
2044                 
2045                 BN_copy(a,b);
2046                 }
2047         BN_free(a);
2048         BN_free(b);
2049         BN_free(c);
2050         return(1);
2051         }
2052
2053 int test_rshift(BIO *bp,BN_CTX *ctx)
2054         {
2055         BIGNUM *a,*b,*c,*d,*e;
2056         int i;
2057
2058         a=BN_new();
2059         b=BN_new();
2060         c=BN_new();
2061         d=BN_new();
2062         e=BN_new();
2063         BN_one(c);
2064
2065         BN_bntest_rand(a,200,0,0);
2066         a->neg=rand_neg();
2067         for (i=0; i<num0; i++)
2068                 {
2069                 BN_rshift(b,a,i+1);
2070                 BN_add(c,c,c);
2071                 if (bp != NULL)
2072                         {
2073                         if (!results)
2074                                 {
2075                                 BN_print(bp,a);
2076                                 BIO_puts(bp," / ");
2077                                 BN_print(bp,c);
2078                                 BIO_puts(bp," - ");
2079                                 }
2080                         BN_print(bp,b);
2081                         BIO_puts(bp,"\n");
2082                         }
2083                 BN_div(d,e,a,c,ctx);
2084                 BN_sub(d,d,b);
2085                 if(!BN_is_zero(d))
2086                     {
2087                     fprintf(stderr,"Right shift test failed!\n");
2088                     return 0;
2089                     }
2090                 }
2091         BN_free(a);
2092         BN_free(b);
2093         BN_free(c);
2094         BN_free(d);
2095         BN_free(e);
2096         return(1);
2097         }
2098
2099 int test_rshift1(BIO *bp)
2100         {
2101         BIGNUM *a,*b,*c;
2102         int i;
2103
2104         a=BN_new();
2105         b=BN_new();
2106         c=BN_new();
2107
2108         BN_bntest_rand(a,200,0,0);
2109         a->neg=rand_neg();
2110         for (i=0; i<num0; i++)
2111                 {
2112                 BN_rshift1(b,a);
2113                 if (bp != NULL)
2114                         {
2115                         if (!results)
2116                                 {
2117                                 BN_print(bp,a);
2118                                 BIO_puts(bp," / 2");
2119                                 BIO_puts(bp," - ");
2120                                 }
2121                         BN_print(bp,b);
2122                         BIO_puts(bp,"\n");
2123                         }
2124                 BN_sub(c,a,b);
2125                 BN_sub(c,c,b);
2126                 if(!BN_is_zero(c) && !BN_abs_is_word(c, 1))
2127                     {
2128                     fprintf(stderr,"Right shift one test failed!\n");
2129                     return 0;
2130                     }
2131                 BN_copy(a,b);
2132                 }
2133         BN_free(a);
2134         BN_free(b);
2135         BN_free(c);
2136         return(1);
2137         }
2138
2139 int rand_neg(void)
2140         {
2141         static unsigned int neg=0;
2142         static int sign[8]={0,0,0,1,1,0,1,1};
2143
2144         return(sign[(neg++)%8]);
2145         }