Skip to content

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

Closed
wants to merge 1 commit into from

Conversation

vv12131415
Copy link
Contributor

There are 2 points on this

  1. bcpowmod is the only function left on bcmath lib that may return false on failure. Let's fix that
  2. next release is major, so we can break things a littlebit

The 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

@vv12131415 vv12131415 force-pushed the bcpowmod-strict branch 2 times, most recently from 25d6aa1 to 2028e1e Compare June 20, 2020 15:47
@vv12131415
Copy link
Contributor Author

I see that CI failed, but I don't understand what exactly failed

@nikic
Copy link
Member

nikic commented Jun 20, 2020

You need to update

F1("bcpowmod", MAY_BE_FALSE | MAY_BE_STRING),

@vv12131415
Copy link
Contributor Author

@nikic by the way.
Since bcpow can have exponent as negative number, I think throwing exception is kind of workaround.

I'm curious why I can't change this function

int
bc_raisemod (bc_num base, bc_num expo, bc_num mod, bc_num *result, int scale)

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?
I have failed test ext/bcmath/tests/bcpowmod.phpt
this one

echo bcpowmod("10", "2147483648", "2047");

gives error

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 1052672 bytes)

I know that it's memory leak somewhere. Going to try to debug it myself, but appreciate if you can help with this

@vv12131415
Copy link
Contributor Author

vv12131415 commented Jun 20, 2020

Ok, I kind of figured out that it's bcpow
I've tried to do

echo bcpow("10", "2147483648");

and it also failed


upd: valgrind haven't found any memory leaks.

Those algorithms implmenented in bcpow and bcpowmod implemented differently

@nikic
Copy link
Member

nikic commented Jun 20, 2020

@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.

Copy link
Member

@nikic nikic left a 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.

@kocsismate
Copy link
Member

@nikic It looks all fine for me as well :)

--TEST--
bc_raisemod's expo can't be negative
--CREDITS--
Gabriel Caruso ([email protected])
Copy link
Contributor

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?

Copy link
Member

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 :)

@php-pulls php-pulls closed this in 2c97b40 Jun 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants