php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52167 gmp_pow does not accept gmp values
Submitted: 2010-06-24 09:39 UTC Modified: 2010-06-30 20:46 UTC
From: mark at hell dot ne dot jp Assigned:
Status: Not a bug Package: GNU MP related
PHP Version: 5.3.2 OS: Linux
Private report: No CVE-ID: None
 [2010-06-24 09:39 UTC] mark at hell dot ne dot jp
Description:
------------
gmp_pow throws a "expects parameter 2 to be long" error when passed a gmp value.

Works nicely with PHP 5.2.x but not with PHP 5.3.

I am doing some maths for encryption with php which use large integers and I have 
to compute pow(a,b) with both values being large integers (~512 bits).

I'm opening this bug so we can investigate why gmp_pow() has been modified to 
only accept integers as second argument, and if there is no good reason make it 
accept gmp values again.

My guess is this change was done on the PHP_5_2 branch but not ported to PHP_5_3. 
Unfortunately svn blame couldn't fetch anything interesting to know why this 
became like this.

Test script:
---------------
$a=gmp_init(48974);
$b=gmp_init(55);
$c=gmp_pow($a,$b);
var_dump(gmp_intval($c));

Expected result:
----------------
int(7944261659914959072)

Actual result:
--------------
Warning: gmp_pow() expects parameter 2 to be long, resource given in Command line 
code on line 1
int(0)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-24 18:03 UTC] kalle@php.net
-Type: Bug +Type: Feature/Change Request
 [2010-06-24 18:03 UTC] kalle@php.net
PHP 5.3 introduced unified parameter parsing, whereas in 5.2 there were alot of places manually converting types that were not compatible.

gmp_pow()'s $exp parameter converted to an integer (long) no matter what type it had passed. In 5.3+ it was changed to require stricly to be compatible types to do type convertion.

However I do not know the exact design reason for why you cannot pass an resource and only a long, so I've changed this to be a feature request
 [2010-06-30 20:46 UTC] felipe@php.net
-Status: Open +Status: Bogus -Type: Feature/Change Request +Type: Bug
 [2010-06-30 20:46 UTC] felipe@php.net
In 5.2 what happens is that the resource id is converted to long. Which is not the expected behavior in many cases. i.e. it does not uses the gmp value as might looks like. 

<?php

$a=gmp_init(48974);
$b=gmp_init(55);
var_dump($b); // resource(5) of type (GMP integer)
$c=gmp_pow($a,$b);
var_dump(gmp_intval($c));
$c=gmp_pow($a,5); // equal to below
var_dump(gmp_intval($c));

?>

So consider the 5.3+ version as alerting about this false positive cases.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Mon Sep 16 08:01:26 2019 UTC