2 # Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the OpenSSL license (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
17 if (scalar @_ != 1) { return "NaN"; }
22 my ($sign, $hex) = (shift =~ /^([+\-]?)(.*)$/);
23 $hex = "0x".$hex if $hex !~ /^0x/;
28 @_ = __multiplier(@_);
29 while (scalar @_ > 1 && $_[1] =~ /^[\+\-]$/) {
30 my $operand1 = Math::BigInt->from_hex(__canonhex(shift));
32 @_ = __multiplier(@_);
33 my $operand2 = Math::BigInt->from_hex(__canonhex(shift));
34 if ($operator eq "+") {
35 $operand1->badd($operand2);
36 } elsif ($operator eq "-") {
37 $operand1->bsub($operand2);
39 die "SOMETHING WENT AWFULLY WRONG";
41 unshift @_, $operand1->as_hex();
48 while (scalar @_ > 1 && $_[1] =~ /^[\*\/%]$/) {
49 my $operand1 = Math::BigInt->from_hex(__canonhex(shift));
52 my $operand2 = Math::BigInt->from_hex(__canonhex(shift));
53 if ($operator eq "*") {
54 $operand1->bmul($operand2);
55 } elsif ($operator eq "/") {
56 # Math::BigInt->bdiv() is documented to do floored division,
57 # i.e. 1 / -4 = -1, while bc and OpenSSL BN_div do truncated
58 # division, i.e. 1 / -4 = 0. We need to make the operation
59 # work like OpenSSL's BN_div to be able to verify.
60 my $neg = ($operand1->is_neg()
61 ? !$operand2->is_neg() : $operand2->is_neg());
64 $operand1->bdiv($operand2);
65 if ($neg) { $operand1->bneg(); }
66 } elsif ($operator eq "%") {
67 # Here's a bit of a quirk...
68 # With OpenSSL's BN, as well as bc, the result of -10 % 3 is -1
69 # while Math::BigInt, the result is 2.
70 # The latter is mathematically more correct, but...
71 my $o1isneg = $operand1->is_neg();
73 # Math::BigInt does something different with a negative modulus,
74 # while OpenSSL's BN and bc treat it like a positive number...
76 $operand1->bmod($operand2);
77 if ($o1isneg) { $operand1->bneg(); }
79 die "SOMETHING WENT AWFULLY WRONG";
81 unshift @_, $operand1->as_hex();
88 while (scalar @_ > 1 && $_[1] eq "^") {
89 my $operand1 = Math::BigInt->from_hex(__canonhex(shift));
92 my $operand2 = Math::BigInt->from_hex(__canonhex(shift));
93 $operand1->bpow($operand2);
94 unshift @_, $operand1->as_hex();
99 # returns array ( $result, @remaining )
101 if (scalar @_ > 0 && $_[0] eq "(") {
103 my @result = __adder(@_);
104 if (scalar @_ == 0 || $_[0] ne ")") {