2 # Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the Apache License 2.0 (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
9 # Run the tests specified in bntests.txt, as a check against OpenSSL.
14 my $EXPECTED_FAILURES = 0;
20 my ($sign, $hex) = ($x =~ /^([+\-]?)(.*)$/);
22 $hex = '0x' . $hex if $hex !~ /^0x/;
23 return Math::BigInt->from_hex($sign.$hex);
31 if ( defined $s{'Sum'} ) {
33 my $sum = bn($s{'Sum'});
36 return if $sum == $a + $b;
37 } elsif ( defined $s{'LShift1'} ) {
39 my $lshift1 = bn($s{'LShift1'});
41 return if $lshift1 == $a->bmul(2);
42 } elsif ( defined $s{'LShift'} ) {
44 my $lshift = bn($s{'LShift'});
47 return if $lshift == $a->blsft($n);
48 } elsif ( defined $s{'RShift'} ) {
50 my $rshift = bn($s{'RShift'});
53 return if $rshift == $a->brsft($n);
54 } elsif ( defined $s{'Square'} ) {
56 my $square = bn($s{'Square'});
58 return if $square == $a->bmul($a);
59 } elsif ( defined $s{'Product'} ) {
61 my $product = bn($s{'Product'});
64 return if $product == $a->bmul($b);
65 } elsif ( defined $s{'Quotient'} ) {
67 # Remainder = A - B * Quotient
68 my $quotient = bn($s{'Quotient'});
69 my $remainder = bn($s{'Remainder'});
73 # First the remainder test.
75 my $rempassed = $remainder == $a->bsub($b) ? 1 : 0;
77 # Math::BigInt->bdiv() is documented to do floored division,
78 # i.e. 1 / -4 = -1, while OpenSSL BN_div does truncated
79 # division, i.e. 1 / -4 = 0. We need to make the operation
80 # work like OpenSSL's BN_div to be able to verify.
83 my $neg = $a->is_neg() ? !$b->is_neg() : $b->is_neg();
88 return if $rempassed && $quotient == $a;
89 } elsif ( defined $s{'ModMul'} ) {
90 # ModMul = (A * B) mod M
91 my $modmul = bn($s{'ModMul'});
96 return if $modmul == $a->bmod($m);
97 } elsif ( defined $s{'ModExp'} ) {
98 # ModExp = (A ** E) mod M
99 my $modexp = bn($s{'ModExp'});
103 return if $modexp == $a->bmodpow($e, $m);
104 } elsif ( defined $s{'Exp'} ) {
105 my $exp = bn($s{'Exp'});
108 return if $exp == $a ** $e;
109 } elsif ( defined $s{'ModSqrt'} ) {
110 # (ModSqrt * ModSqrt) mod P = A mod P
111 my $modsqrt = bn($s{'ModSqrt'});
114 $modsqrt->bmul($modsqrt);
117 return if $modsqrt == $a;
119 print "# Unknown test: ";
122 print "# #$failures Test (before line $lineno) failed\n";
123 foreach ( keys %s ) {
124 print "$_ = $s{$_}\n";
129 my $infile = shift || 'bntests.txt';
130 die "No such file, $infile" unless -f $infile;
131 open my $IN, $infile || die "Can't read $infile, $!\n";
140 if ( keys %stanza ) {
141 evaluate($l, %stanza);
146 # Parse 'key = value'
147 if ( ! /\s*([^\s]*)\s*=\s*(.*)\s*/ ) {
148 print "Skipping $_\n";
153 evaluate($l, %stanza) if keys %stanza;
154 die "Got $failures, expected $EXPECTED_FAILURES"
155 if $infile eq 'bntests.txt' and $failures != $EXPECTED_FAILURES;