I've attached a tentative patch for test/recipes/bc.pl. Would you be willing to
try it out?

Vid Fre, 25 Mar 2016 kl. 21.29.50, skrev levitte:
> Vid Fre, 25 Mar 2016 kl. 20.51.10, skrev craigbe...@mac.com:
> > I reported this to the Math::BigInt maintainer, who explains that the
> > change fixes a bug in which, "The code did truncated division whereas
> > the documentation said that floored division was used" and he changed
> > the code to match the documentation. See:
>
> BN_div does truncated division, which is expressed in other terms in
> the comment in crypto/bn/bn_div.c:
>
> /*- * BN_div computes dv := num / divisor, rounding towards * zero,
> and sets up
> rm such that dv*divisor + rm = num holds. * Thus: * dv->neg == num-
> >neg ^
> divisor->neg (unless the result is zero) * rm->neg == num->neg (unless
> the
> remainder is zero) * If 'dv' or 'rm' is NULL, the respective value is
> not
> returned. */
> keywords being "rounding towards zero".
> > Or is truncated division intentional and we now have a problem using
> > Math::BigInt as a reference since that’s not what it does anymore?
>
> I wouldn't dare change BN_div's behaviour in this regard. It might not
> be
> as mathematically correct, but there's too much else possibly relying
> on
> the current behaviour.
>
> However, if you have a look at test/recipes/bc.pl, you'll see that
> there's
> a "fix" of modulo because there's already a difference between what
> OpenSSL's modulo does and what Math::BigInt's modulo does. I don't
> think it would be too hard to do something similar for division.
>
> > but I have also reproduced the test failures on OS X by just doing:
> >
> > $ sudo cpan -i Math::BigInt
>
> Ok.
>
> --
> Richard Levitte
> levi...@openssl.org


--
Richard Levitte
levi...@openssl.org

-- 
Ticket here: http://rt.openssl.org/Ticket/Display.html?id=4485
Please log in as guest with password guest if prompted

diff --git a/test/recipes/bc.pl b/test/recipes/bc.pl
index 29a4a8a..f7d4dc6 100644
--- a/test/recipes/bc.pl
+++ b/test/recipes/bc.pl
@@ -46,7 +46,16 @@ sub __multiplier {
 	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);
+	    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
-- 
openssl-dev mailing list
To unsubscribe: https://mta.openssl.org/mailman/listinfo/openssl-dev

Reply via email to