Math::BigInt does floored divs, BN_div does truncated div, compensate
authorRichard Levitte <levitte@openssl.org>
Fri, 25 Mar 2016 23:07:50 +0000 (00:07 +0100)
committerRichard Levitte <levitte@openssl.org>
Sun, 27 Mar 2016 19:39:23 +0000 (21:39 +0200)
According to documentation, perl's Math::BigInt does floored division,
i.e. the bdiv function does 1 / -4 = -1.  OpenSSL's BN_div, as well as
bc, do truncated division, i.e. 1 / -4 = 0.

We need to compensate for that difference in test/recipes/bc.pl to
make sure to verify the bntest results under its own conditions, by
dividing the absolute values of the given numbers and fixup the
result's negativity afterwards.

Closes RT#4485

Reviewed-by: Viktor Dukhovni <viktor@openssl.org>
test/recipes/bc.pl

index 29a4a8a84a0789064d04a86e4fd54ac6a70c2f0e..f7d4dc681d7d95431cb67bc7df56e34f53bf7ca4 100644 (file)
@@ -46,7 +46,16 @@ sub __multiplier {
        if ($operator eq "*") {
            $operand1->bmul($operand2);
        } elsif ($operator eq "/") {
        if ($operator eq "*") {
            $operand1->bmul($operand2);
        } elsif ($operator eq "/") {
+           # Math::BigInt->bdiv() is documented to do floored division,
+           # i.e. 1 / -4 = -1, while bc and OpenSSL BN_div do truncated
+           # division, i.e. 1 / -4 = 0.  We need to make the operation
+           # work like OpenSSL's BN_div to be able to verify.
+           my $neg = ($operand1->is_neg()
+                      ? !$operand2->is_neg() : $operand2->is_neg());
+           $operand1->babs();
+           $operand2->babs();
            $operand1->bdiv($operand2);
            $operand1->bdiv($operand2);
+           if ($neg) { $operand1->bneg(); }
        } elsif ($operator eq "%") {
            # Here's a bit of a quirk...
            # With OpenSSL's BN, as well as bc, the result of -10 % 3 is -1
        } elsif ($operator eq "%") {
            # Here's a bit of a quirk...
            # With OpenSSL's BN, as well as bc, the result of -10 % 3 is -1