PHP-OpenID-0.0.8.3 released

Carl Howells chowells at janrain.com
Fri Sep 16 10:04:51 PDT 2005


Eww.  Don't do that.  That isn't just slow...  That's SLOW, SLOW, SLOW.

I don't know PHP, so here's a function in python:

def powmod(base, exp, mod):
     square = base % mod
     result = 1
     while exp > 0:
         if exp & 1:
             result = (result * square) % mod
         square = (square * square) % mod
         exp /= 2

     return result

For any kind of large exponent (which Diffie-Hellman definitely 
qualifies as using), that function will be FAR faster than just:

def powmod(base, exp, mod):
     return (base ** exp) % mod

It's far faster because it keeps the number of digits involved in the 
exponentiation under control.

Use an equivalent of the first form, instead of an equivalent of the 
second form.  The performance difference is phenomenal.

Carl

Dan Libby wrote:
> You're right.  Since bcpowmod is php5-only and bcmath is pretty commonly
> installed on php 4.x, it is best to support the old functions.   Here is
> a patch that does so.  It will also be in the next release.
> 
> regards,
> 
> Dan Libby
> 
> 
> --- oid_util.php        16 Sep 2005 00:05:56 -0000      1.2
> +++ oid_util.php        16 Sep 2005 01:05:31 -0000      1.3
> @@ -348,6 +348,9 @@
>          else if( function_exists( 'bcpowmod' ) ) {
>              return bcpowmod( $base, $exponent, $modulus );
>          }
> +        else if( function_exists( 'bcpow' ) && function_exists( 'bcmod'
> ) ) {
> +            return bcmod(bcpow($base, $exponent), $modulus);
> +        }
>          // Warning: here we give up on php and call python or perl to
> help us out.
>          // I'd like to replace this with native php code. Anyone know of
>          // a good/fast implementation of powmod in php?
> 
> 



More information about the yadis mailing list