-
Notifications
You must be signed in to change notification settings - Fork 7.9k
make bcpowmod stricter by not returning false, instead throw exception #5747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
25d6aa1
to
2028e1e
Compare
2028e1e
to
fb5bdf3
Compare
fb5bdf3
to
bd13881
Compare
I see that CI failed, but I don't understand what exactly failed |
You need to update
|
bd13881
to
cb5131b
Compare
@nikic by the way. I'm curious why I can't change this function php-src/ext/bcmath/libbcmath/src/raisemod.c Lines 62 to 63 in 6bc375f
to look like this int
bc_raisemod (bc_num base, bc_num expo, bc_num mod, bc_num *result, int scale)
{
bc_num power, exponent, modulus, temp;
/* Check for correct numbers. */
if (bc_is_zero(mod)) return -1;
power = bc_copy_num (base);
exponent = bc_copy_num (expo);
modulus = bc_copy_num (mod);
temp = bc_copy_num (BCG(_one_));
bc_raise(power, exponent, &temp, scale);
bc_free_num (&power);
bc_free_num (&exponent);
bc_modulo(temp, modulus, &temp, scale);
/* Assign the value. */
bc_free_num (&modulus);
bc_free_num (result);
*result = temp;
return 0; /* Everything is OK. */ Am I missing something? echo bcpowmod("10", "2147483648", "2047"); gives error
I know that it's memory leak somewhere. Going to try to debug it myself, but appreciate if you can help with this |
Ok, I kind of figured out that it's echo bcpow("10", "2147483648"); and it also failed upd: valgrind haven't found any memory leaks. Those algorithms implmenented in |
@vladyslavstartsev The point of modular exponentiation is exactly that it does not require computing the full exponentiation. This is why bcpowmod exists, instead of letting people use bcpow + bcmod. I don't think it makes sense to support negative exponents in this function. It will likely only confuse people, because it would interpret the negative exponent in the sense of a division, not in the sense of a modular multiplicative inverse, which is what people usually mean when talking about modular exponentiation with negative exponents. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fine, maybe @kocsismate wants to check this aligns with error conventions.
@nikic It looks all fine for me as well :) |
--TEST-- | ||
bc_raisemod's expo can't be negative | ||
--CREDITS-- | ||
Gabriel Caruso ([email protected]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, wait, what my name is doing here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, too late, sorry :S You can remove it from master though :)
There are 2 points on this
bcpowmod
is the only function left onbcmath
lib that may return false on failure. Let's fix thatThe only one question that I have for you guys - what kind of exception should I throw when we have negative exponent?
i left
todo
in code, will remove after I get answers