]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/bn/bntest.c
*.org files are gone for good.
[thirdparty/openssl.git] / crypto / bn / bntest.c
CommitLineData
d02b48c6 1/* crypto/bn/bntest.c */
58964a49 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
58964a49 62#include "e_os.h"
d02b48c6
RE
63#include "bio.h"
64#include "bn.h"
65#include "rand.h"
66#include "x509.h"
67#include "err.h"
68
58964a49
RE
69#ifdef WINDOWS
70#include "../bio/bss_file.c"
71#endif
72
d02b48c6 73#ifndef NOPROTO
dfeab068
RE
74int test_add(BIO *bp);
75int test_sub(BIO *bp);
76int test_lshift1(BIO *bp);
77int test_lshift(BIO *bp);
78int test_rshift1(BIO *bp);
79int test_rshift(BIO *bp);
80int test_div(BIO *bp,BN_CTX *ctx);
81int test_div_recp(BIO *bp,BN_CTX *ctx);
82int test_mul(BIO *bp);
83int test_sqr(BIO *bp,BN_CTX *ctx);
84int test_mont(BIO *bp,BN_CTX *ctx);
85int test_mod(BIO *bp,BN_CTX *ctx);
86int test_mod_mul(BIO *bp,BN_CTX *ctx);
87int test_mod_exp(BIO *bp,BN_CTX *ctx);
a0a54079 88int test_exp(BIO *bp,BN_CTX *ctx);
d02b48c6
RE
89int rand_neg(void);
90#else
91int test_add ();
92int test_sub ();
93int test_lshift1 ();
94int test_lshift ();
95int test_rshift1 ();
96int test_rshift ();
97int test_div ();
98int test_mul ();
99int test_sqr ();
100int test_mont ();
101int test_mod ();
102int test_mod_mul ();
103int test_mod_exp ();
a0a54079 104int test_exp ();
d02b48c6
RE
105int rand_neg();
106#endif
107
108static int results=0;
109
58964a49 110#ifdef NO_STDIO
d02b48c6 111#define APPS_WIN16
58964a49 112#include "bss_file.c"
d02b48c6
RE
113#endif
114
6b691a5c 115int main(int argc, char *argv[])
d02b48c6
RE
116 {
117 BN_CTX *ctx;
118 BIO *out;
119 char *outfile=NULL;
120
121 srand((unsigned int)time(NULL));
122
123 argc--;
124 argv++;
125 while (argc >= 1)
126 {
127 if (strcmp(*argv,"-results") == 0)
128 results=1;
129 else if (strcmp(*argv,"-out") == 0)
130 {
131 if (--argc < 1) break;
132 outfile= *(++argv);
133 }
134 argc--;
135 argv++;
136 }
137
138
139 ctx=BN_CTX_new();
140 if (ctx == NULL) exit(1);
141
142 out=BIO_new(BIO_s_file());
143 if (out == NULL) exit(1);
144 if (outfile == NULL)
145 {
146 BIO_set_fp(out,stdout,BIO_NOCLOSE);
147 }
148 else
149 {
150 if (!BIO_write_filename(out,outfile))
151 {
152 perror(outfile);
153 exit(1);
154 }
155 }
156
157 if (!results)
158 BIO_puts(out,"obase=16\nibase=16\n");
159
160 fprintf(stderr,"test BN_add\n");
161 if (!test_add(out)) goto err;
162 fflush(stdout);
163
164 fprintf(stderr,"test BN_sub\n");
165 if (!test_sub(out)) goto err;
166 fflush(stdout);
167
168 fprintf(stderr,"test BN_lshift1\n");
169 if (!test_lshift1(out)) goto err;
170 fflush(stdout);
171
172 fprintf(stderr,"test BN_lshift\n");
173 if (!test_lshift(out)) goto err;
174 fflush(stdout);
175
176 fprintf(stderr,"test BN_rshift1\n");
177 if (!test_rshift1(out)) goto err;
178 fflush(stdout);
179
180 fprintf(stderr,"test BN_rshift\n");
181 if (!test_rshift(out)) goto err;
182 fflush(stdout);
183
58964a49
RE
184 fprintf(stderr,"test BN_sqr\n");
185 if (!test_sqr(out,ctx)) goto err;
d02b48c6
RE
186 fflush(stdout);
187
188 fprintf(stderr,"test BN_mul\n");
189 if (!test_mul(out)) goto err;
190 fflush(stdout);
191
58964a49
RE
192 fprintf(stderr,"test BN_div\n");
193 if (!test_div(out,ctx)) goto err;
194 fflush(stdout);
195
dfeab068
RE
196 fprintf(stderr,"test BN_div_recp\n");
197 if (!test_div_recp(out,ctx)) goto err;
198 fflush(stdout);
199
58964a49
RE
200 fprintf(stderr,"test BN_mod\n");
201 if (!test_mod(out,ctx)) goto err;
d02b48c6
RE
202 fflush(stdout);
203
204 fprintf(stderr,"test BN_mod_mul\n");
205 if (!test_mod_mul(out,ctx)) goto err;
206 fflush(stdout);
207
208/*
209 fprintf(stderr,"test BN_mont\n");
210 if (!test_mont(out,ctx)) goto err;
211 fflush(stdout);
212*/
213 fprintf(stderr,"test BN_mod_exp\n");
214 if (!test_mod_exp(out,ctx)) goto err;
215 fflush(stdout);
216
a0a54079
MC
217 fprintf(stderr,"test BN_exp\n");
218 if (!test_exp(out,ctx)) goto err;
219 fflush(stdout);
220
d02b48c6
RE
221/**/
222 exit(0);
223err:
224 ERR_load_crypto_strings();
225 ERR_print_errors(out);
226 exit(1);
227 return(1);
228 }
229
6b691a5c 230int test_add(BIO *bp)
d02b48c6 231 {
dfeab068 232 BIGNUM a,b,c;
d02b48c6
RE
233 int i;
234 int j;
235
dfeab068
RE
236 BN_init(&a);
237 BN_init(&b);
238 BN_init(&c);
d02b48c6 239
dfeab068 240 BN_rand(&a,512,0,0);
d02b48c6
RE
241 for (i=0; i<100; i++)
242 {
dfeab068
RE
243 BN_rand(&b,450+i,0,0);
244 a.neg=rand_neg();
245 b.neg=rand_neg();
d02b48c6
RE
246 if (bp == NULL)
247 for (j=0; j<10000; j++)
dfeab068
RE
248 BN_add(&c,&a,&b);
249 BN_add(&c,&a,&b);
d02b48c6
RE
250 if (bp != NULL)
251 {
252 if (!results)
253 {
dfeab068 254 BN_print(bp,&a);
d02b48c6 255 BIO_puts(bp," + ");
dfeab068 256 BN_print(bp,&b);
d02b48c6
RE
257 BIO_puts(bp," - ");
258 }
dfeab068 259 BN_print(bp,&c);
d02b48c6
RE
260 BIO_puts(bp,"\n");
261 }
262 }
dfeab068
RE
263 BN_free(&a);
264 BN_free(&b);
265 BN_free(&c);
d02b48c6
RE
266 return(1);
267 }
268
6b691a5c 269int test_sub(BIO *bp)
d02b48c6 270 {
dfeab068 271 BIGNUM a,b,c;
d02b48c6
RE
272 int i;
273 int j;
274
dfeab068
RE
275 BN_init(&a);
276 BN_init(&b);
277 BN_init(&c);
d02b48c6 278
dfeab068 279 BN_rand(&a,512,0,0);
d02b48c6
RE
280 for (i=0; i<100; i++)
281 {
dfeab068
RE
282 BN_rand(&b,400+i,0,0);
283 a.neg=rand_neg();
284 b.neg=rand_neg();
d02b48c6
RE
285 if (bp == NULL)
286 for (j=0; j<10000; j++)
dfeab068
RE
287 BN_sub(&c,&a,&b);
288 BN_sub(&c,&a,&b);
d02b48c6
RE
289 if (bp != NULL)
290 {
291 if (!results)
292 {
dfeab068 293 BN_print(bp,&a);
d02b48c6 294 BIO_puts(bp," - ");
dfeab068 295 BN_print(bp,&b);
d02b48c6
RE
296 BIO_puts(bp," - ");
297 }
dfeab068 298 BN_print(bp,&c);
d02b48c6
RE
299 BIO_puts(bp,"\n");
300 }
301 }
dfeab068
RE
302 BN_free(&a);
303 BN_free(&b);
304 BN_free(&c);
d02b48c6
RE
305 return(1);
306 }
307
6b691a5c 308int test_div(BIO *bp, BN_CTX *ctx)
d02b48c6 309 {
dfeab068 310 BIGNUM a,b,c,d;
d02b48c6
RE
311 int i;
312 int j;
313
dfeab068
RE
314 BN_init(&a);
315 BN_init(&b);
316 BN_init(&c);
317 BN_init(&d);
d02b48c6 318
dfeab068 319 BN_rand(&a,400,0,0);
d02b48c6
RE
320 for (i=0; i<100; i++)
321 {
dfeab068
RE
322 BN_rand(&b,50+i,0,0);
323 a.neg=rand_neg();
324 b.neg=rand_neg();
d02b48c6
RE
325 if (bp == NULL)
326 for (j=0; j<100; j++)
dfeab068
RE
327 BN_div(&d,&c,&a,&b,ctx);
328 BN_div(&d,&c,&a,&b,ctx);
d02b48c6
RE
329 if (bp != NULL)
330 {
331 if (!results)
332 {
dfeab068 333 BN_print(bp,&a);
d02b48c6 334 BIO_puts(bp," / ");
dfeab068 335 BN_print(bp,&b);
d02b48c6
RE
336 BIO_puts(bp," - ");
337 }
dfeab068 338 BN_print(bp,&d);
d02b48c6
RE
339 BIO_puts(bp,"\n");
340
341 if (!results)
342 {
dfeab068 343 BN_print(bp,&a);
d02b48c6 344 BIO_puts(bp," % ");
dfeab068 345 BN_print(bp,&b);
d02b48c6
RE
346 BIO_puts(bp," - ");
347 }
dfeab068 348 BN_print(bp,&c);
d02b48c6
RE
349 BIO_puts(bp,"\n");
350 }
351 }
dfeab068
RE
352 BN_free(&a);
353 BN_free(&b);
354 BN_free(&c);
355 BN_free(&d);
356 return(1);
357 }
358
6b691a5c 359int test_div_recp(BIO *bp, BN_CTX *ctx)
dfeab068
RE
360 {
361 BIGNUM a,b,c,d;
362 BN_RECP_CTX recp;
363 int i;
364 int j;
365
366 BN_RECP_CTX_init(&recp);
367 BN_init(&a);
368 BN_init(&b);
369 BN_init(&c);
370 BN_init(&d);
371
372 BN_rand(&a,400,0,0);
373 for (i=0; i<100; i++)
374 {
375 BN_rand(&b,50+i,0,0);
376 a.neg=rand_neg();
377 b.neg=rand_neg();
378 BN_RECP_CTX_set(&recp,&b,ctx);
379 if (bp == NULL)
380 for (j=0; j<100; j++)
381 BN_div_recp(&d,&c,&a,&recp,ctx);
382 BN_div_recp(&d,&c,&a,&recp,ctx);
383 if (bp != NULL)
384 {
385 if (!results)
386 {
387 BN_print(bp,&a);
388 BIO_puts(bp," / ");
389 BN_print(bp,&b);
390 BIO_puts(bp," - ");
391 }
392 BN_print(bp,&d);
393 BIO_puts(bp,"\n");
394
395 if (!results)
396 {
397 BN_print(bp,&a);
398 BIO_puts(bp," % ");
399 BN_print(bp,&b);
400 BIO_puts(bp," - ");
401 }
402 BN_print(bp,&c);
403 BIO_puts(bp,"\n");
404 }
405 }
406 BN_free(&a);
407 BN_free(&b);
408 BN_free(&c);
409 BN_free(&d);
410 BN_RECP_CTX_free(&recp);
d02b48c6
RE
411 return(1);
412 }
413
6b691a5c 414int test_mul(BIO *bp)
d02b48c6 415 {
dfeab068 416 BIGNUM a,b,c;
d02b48c6
RE
417 int i;
418 int j;
dfeab068 419 BN_CTX ctx;
d02b48c6 420
dfeab068
RE
421 BN_CTX_init(&ctx);
422 BN_init(&a);
423 BN_init(&b);
424 BN_init(&c);
d02b48c6 425
dfeab068 426 BN_rand(&a,200,0,0);
d02b48c6
RE
427 for (i=0; i<100; i++)
428 {
dfeab068
RE
429 BN_rand(&b,250+i,0,0);
430 BN_rand(&b,200,0,0);
431 a.neg=rand_neg();
432 b.neg=rand_neg();
d02b48c6
RE
433 if (bp == NULL)
434 for (j=0; j<100; j++)
dfeab068
RE
435 BN_mul(&c,&a,&b,&ctx);
436 BN_mul(&c,&a,&b,&ctx);
437/*bn_do(&c,&a,&b,ctx); */
d02b48c6
RE
438 if (bp != NULL)
439 {
440 if (!results)
441 {
dfeab068 442 BN_print(bp,&a);
d02b48c6 443 BIO_puts(bp," * ");
dfeab068 444 BN_print(bp,&b);
d02b48c6
RE
445 BIO_puts(bp," - ");
446 }
dfeab068 447 BN_print(bp,&c);
d02b48c6
RE
448 BIO_puts(bp,"\n");
449 }
450 }
dfeab068
RE
451 BN_free(&a);
452 BN_free(&b);
453 BN_free(&c);
454 BN_CTX_free(&ctx);
d02b48c6
RE
455 return(1);
456 }
457
6b691a5c 458int test_sqr(BIO *bp, BN_CTX *ctx)
d02b48c6 459 {
dfeab068 460 BIGNUM a,c;
d02b48c6
RE
461 int i;
462 int j;
463
dfeab068
RE
464 BN_init(&a);
465 BN_init(&c);
d02b48c6
RE
466
467 for (i=0; i<40; i++)
468 {
dfeab068
RE
469 BN_rand(&a,40+i*10,0,0);
470 a.neg=rand_neg();
d02b48c6
RE
471 if (bp == NULL)
472 for (j=0; j<100; j++)
dfeab068
RE
473 BN_sqr(&c,&a,ctx);
474 BN_sqr(&c,&a,ctx);
d02b48c6
RE
475 if (bp != NULL)
476 {
477 if (!results)
478 {
dfeab068 479 BN_print(bp,&a);
d02b48c6 480 BIO_puts(bp," * ");
dfeab068 481 BN_print(bp,&a);
d02b48c6
RE
482 BIO_puts(bp," - ");
483 }
dfeab068 484 BN_print(bp,&c);
d02b48c6
RE
485 BIO_puts(bp,"\n");
486 }
487 }
dfeab068
RE
488 BN_free(&a);
489 BN_free(&c);
d02b48c6
RE
490 return(1);
491 }
492
6b691a5c 493int test_mont(BIO *bp, BN_CTX *ctx)
d02b48c6 494 {
dfeab068
RE
495 BIGNUM a,b,c,A,B;
496 BIGNUM n;
d02b48c6
RE
497 int i;
498 int j;
499 BN_MONT_CTX *mont;
500
dfeab068
RE
501 BN_init(&a);
502 BN_init(&b);
503 BN_init(&c);
504 BN_init(&A);
505 BN_init(&B);
506 BN_init(&n);
d02b48c6
RE
507
508 mont=BN_MONT_CTX_new();
509
dfeab068
RE
510 BN_rand(&a,100,0,0); /**/
511 BN_rand(&b,100,0,0); /**/
d02b48c6
RE
512 for (i=0; i<10; i++)
513 {
dfeab068
RE
514 BN_rand(&n,(100%BN_BITS2+1)*BN_BITS2*i*BN_BITS2,0,1); /**/
515 BN_MONT_CTX_set(mont,&n,ctx);
d02b48c6 516
dfeab068
RE
517 BN_to_montgomery(&A,&a,mont,ctx);
518 BN_to_montgomery(&B,&b,mont,ctx);
d02b48c6
RE
519
520 if (bp == NULL)
521 for (j=0; j<100; j++)
dfeab068
RE
522 BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/
523 BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/
524 BN_from_montgomery(&A,&c,mont,ctx);/**/
d02b48c6
RE
525 if (bp != NULL)
526 {
527 if (!results)
528 {
529#ifdef undef
530fprintf(stderr,"%d * %d %% %d\n",
dfeab068
RE
531BN_num_bits(&a),
532BN_num_bits(&b),
d02b48c6
RE
533BN_num_bits(mont->N));
534#endif
dfeab068 535 BN_print(bp,&a);
d02b48c6 536 BIO_puts(bp," * ");
dfeab068 537 BN_print(bp,&b);
d02b48c6 538 BIO_puts(bp," % ");
dfeab068 539 BN_print(bp,&(mont->N));
d02b48c6
RE
540 BIO_puts(bp," - ");
541 }
dfeab068 542 BN_print(bp,&A);
d02b48c6
RE
543 BIO_puts(bp,"\n");
544 }
545 }
546 BN_MONT_CTX_free(mont);
dfeab068
RE
547 BN_free(&a);
548 BN_free(&b);
549 BN_free(&c);
d02b48c6
RE
550 return(1);
551 }
552
6b691a5c 553int test_mod(BIO *bp, BN_CTX *ctx)
d02b48c6
RE
554 {
555 BIGNUM *a,*b,*c;
556 int i;
557 int j;
558
559 a=BN_new();
560 b=BN_new();
561 c=BN_new();
562
563 BN_rand(a,1024,0,0); /**/
564 for (i=0; i<20; i++)
565 {
566 BN_rand(b,450+i*10,0,0); /**/
567 a->neg=rand_neg();
568 b->neg=rand_neg();
569 if (bp == NULL)
570 for (j=0; j<100; j++)
571 BN_mod(c,a,b,ctx);/**/
572 BN_mod(c,a,b,ctx);/**/
573 if (bp != NULL)
574 {
575 if (!results)
576 {
577 BN_print(bp,a);
578 BIO_puts(bp," % ");
579 BN_print(bp,b);
580 BIO_puts(bp," - ");
581 }
582 BN_print(bp,c);
583 BIO_puts(bp,"\n");
584 }
585 }
586 BN_free(a);
587 BN_free(b);
588 BN_free(c);
589 return(1);
590 }
591
6b691a5c 592int test_mod_mul(BIO *bp, BN_CTX *ctx)
d02b48c6
RE
593 {
594 BIGNUM *a,*b,*c,*d,*e;
595 int i;
596
597 a=BN_new();
598 b=BN_new();
599 c=BN_new();
600 d=BN_new();
601 e=BN_new();
602
603 BN_rand(c,1024,0,0); /**/
604 for (i=0; i<10; i++)
605 {
606 BN_rand(a,475+i*10,0,0); /**/
607 BN_rand(b,425+i*10,0,0); /**/
608 a->neg=rand_neg();
609 b->neg=rand_neg();
610 /* if (bp == NULL)
611 for (j=0; j<100; j++)
612 BN_mod_mul(d,a,b,c,ctx);*/ /**/
613
614 if (!BN_mod_mul(e,a,b,c,ctx))
615 {
616 unsigned long l;
617
618 while ((l=ERR_get_error()))
619 fprintf(stderr,"ERROR:%s\n",
620 ERR_error_string(l,NULL));
621 exit(1);
622 }
623 if (bp != NULL)
624 {
625 if (!results)
626 {
627 BN_print(bp,a);
628 BIO_puts(bp," * ");
629 BN_print(bp,b);
630 BIO_puts(bp," % ");
631 BN_print(bp,c);
632 BIO_puts(bp," - ");
633 }
634 BN_print(bp,e);
635 BIO_puts(bp,"\n");
636 }
637 }
638 BN_free(a);
639 BN_free(b);
640 BN_free(c);
641 BN_free(d);
642 BN_free(e);
643 return(1);
644 }
645
6b691a5c 646int test_mod_exp(BIO *bp, BN_CTX *ctx)
d02b48c6
RE
647 {
648 BIGNUM *a,*b,*c,*d,*e;
649 int i;
650
651 a=BN_new();
652 b=BN_new();
653 c=BN_new();
654 d=BN_new();
655 e=BN_new();
656
657 BN_rand(c,30,0,1); /* must be odd for montgomery */
658 for (i=0; i<6; i++)
659 {
660 BN_rand(a,20+i*5,0,0); /**/
661 BN_rand(b,2+i,0,0); /**/
662
663 if (!BN_mod_exp(d,a,b,c,ctx))
664 return(00);
665
666 if (bp != NULL)
667 {
668 if (!results)
669 {
670 BN_print(bp,a);
671 BIO_puts(bp," ^ ");
672 BN_print(bp,b);
673 BIO_puts(bp," % ");
674 BN_print(bp,c);
675 BIO_puts(bp," - ");
676 }
677 BN_print(bp,d);
678 BIO_puts(bp,"\n");
679 }
680 }
681 BN_free(a);
682 BN_free(b);
683 BN_free(c);
684 BN_free(d);
685 BN_free(e);
686 return(1);
687 }
688
6b691a5c 689int test_exp(BIO *bp, BN_CTX *ctx)
a0a54079
MC
690 {
691 BIGNUM *a,*b,*d,*e;
692 int i;
693
694 a=BN_new();
695 b=BN_new();
696 d=BN_new();
697 e=BN_new();
698
699 for (i=0; i<6; i++)
700 {
701 BN_rand(a,20+i*5,0,0); /**/
702 BN_rand(b,2+i,0,0); /**/
703
704 if (!BN_exp(d,a,b,ctx))
705 return(00);
706
707 if (bp != NULL)
708 {
709 if (!results)
710 {
711 BN_print(bp,a);
712 BIO_puts(bp," ^ ");
713 BN_print(bp,b);
714 BIO_puts(bp," - ");
715 }
716 BN_print(bp,d);
717 BIO_puts(bp,"\n");
718 }
719 }
720 BN_free(a);
721 BN_free(b);
722 BN_free(d);
723 BN_free(e);
724 return(1);
725 }
726
6b691a5c 727int test_lshift(BIO *bp)
d02b48c6
RE
728 {
729 BIGNUM *a,*b,*c;
730 int i;
731
732 a=BN_new();
733 b=BN_new();
734 c=BN_new();
735 BN_one(c);
736
737 BN_rand(a,200,0,0); /**/
738 a->neg=rand_neg();
739 for (i=0; i<70; i++)
740 {
741 BN_lshift(b,a,i+1);
742 BN_add(c,c,c);
743 if (bp != NULL)
744 {
745 if (!results)
746 {
747 BN_print(bp,a);
748 BIO_puts(bp," * ");
749 BN_print(bp,c);
750 BIO_puts(bp," - ");
751 }
752 BN_print(bp,b);
753 BIO_puts(bp,"\n");
754 }
755 }
756 BN_free(a);
757 BN_free(b);
758 BN_free(c);
759 return(1);
760 }
761
6b691a5c 762int test_lshift1(BIO *bp)
d02b48c6
RE
763 {
764 BIGNUM *a,*b;
765 int i;
766
767 a=BN_new();
768 b=BN_new();
769
770 BN_rand(a,200,0,0); /**/
771 a->neg=rand_neg();
772 for (i=0; i<70; i++)
773 {
774 BN_lshift1(b,a);
775 if (bp != NULL)
776 {
777 if (!results)
778 {
779 BN_print(bp,a);
780 BIO_puts(bp," * 2");
781 BIO_puts(bp," - ");
782 }
783 BN_print(bp,b);
784 BIO_puts(bp,"\n");
785 }
786 BN_copy(a,b);
787 }
788 BN_free(a);
789 BN_free(b);
790 return(1);
791 }
792
6b691a5c 793int test_rshift(BIO *bp)
d02b48c6
RE
794 {
795 BIGNUM *a,*b,*c;
796 int i;
797
798 a=BN_new();
799 b=BN_new();
800 c=BN_new();
801 BN_one(c);
802
803 BN_rand(a,200,0,0); /**/
804 a->neg=rand_neg();
805 for (i=0; i<70; i++)
806 {
807 BN_rshift(b,a,i+1);
808 BN_add(c,c,c);
809 if (bp != NULL)
810 {
811 if (!results)
812 {
813 BN_print(bp,a);
814 BIO_puts(bp," / ");
815 BN_print(bp,c);
816 BIO_puts(bp," - ");
817 }
818 BN_print(bp,b);
819 BIO_puts(bp,"\n");
820 }
821 }
822 BN_free(a);
823 BN_free(b);
824 BN_free(c);
825 return(1);
826 }
827
6b691a5c 828int test_rshift1(BIO *bp)
d02b48c6
RE
829 {
830 BIGNUM *a,*b;
831 int i;
832
833 a=BN_new();
834 b=BN_new();
835
836 BN_rand(a,200,0,0); /**/
837 a->neg=rand_neg();
838 for (i=0; i<70; i++)
839 {
840 BN_rshift1(b,a);
841 if (bp != NULL)
842 {
843 if (!results)
844 {
845 BN_print(bp,a);
846 BIO_puts(bp," / 2");
847 BIO_puts(bp," - ");
848 }
849 BN_print(bp,b);
850 BIO_puts(bp,"\n");
851 }
852 BN_copy(a,b);
853 }
854 BN_free(a);
855 BN_free(b);
856 return(1);
857 }
858
6b691a5c 859int rand_neg(void)
d02b48c6
RE
860 {
861 static unsigned int neg=0;
862 static int sign[8]={0,0,0,1,1,0,1,1};
863
864 return(sign[(neg++)%8]);
865 }