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