note 57241 added to function.bcpowmod

From: Date: Wed, 28 Sep 2005 04:46:36 +0000
Subject: note 57241 added to function.bcpowmod
Groups: php.notes 
Request: Send a blank email to php-notes+get-95909@lists.php.net to get a copy of this message
Versions of PHP prior to 5 do not have bcpowmod in their repertoire. This routine simulates this function using bcdiv, bcmod and bcmul. It is useful to have bcpowmod available because it is commonly used to implement the RSA algorithm. The function bcpowmod(v, e, m) is supposedly equivalent to bcmod(bcpow(v, e), m). However, for the large numbers used as keys in the RSA algorithm, the bcpow function generates a number so big as to overflow it. For any exponent greater than a few tens of thousands, bcpow overflows and returns 1. This routine will iterate through a loop squaring the result, modulo the modulus, for every one-bit in the exponent. The exponent is shifted right by one bit for each iteration. When it has been reduced to zero, the calculation ends. This method may be slower than bcpowmod but at least it works. function PowModSim($Value, $Exponent, $Modulus) { // Check if simulation is even necessary. if (function_exists("bcpowmod")) return (bcpowmod($Value, $Exponent, $Modulus)); // Loop until the exponent is reduced to zero. $Result = "1"; while (TRUE) { if (bcmod($Exponent, 2) == "1") $Result = bcmod(bcmul($Result, $Value), $Modulus); if (($Exponent = bcdiv($Exponent, 2)) == "0") break; $Value = bcmod(bcmul($Value, $Value), $Modulus); } return ($Result); } ---- Manual Page -- http://www.php.net/manual/en/function.bcpowmod.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+57241 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57241&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57241&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57241&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57241&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57241&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57241&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57241&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+57241&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#95909) next »