function powmod($base, $exponent, $modulus) { if (function_exists('gmp_powm')) { // fast return gmp_strval(gmp_powm($base, $exponent, $modulus)); } if (function_exists('bi_powmod')) { // not tested return bi_sto_str(bi_powmod($base, $exponent, $modulus)); } if (function_exists('bcpowmod')) { // slow return bcpowmod($base, $exponent, $modulus); } // emulation, slow $square = bcmod($base, $modulus); $result = 1; while (bccomp($exponent, 0) > 0) { if (bcmod($exponent, 2)) { $result = bcmod(bcmul($result, $square), $modulus); } $square = bcmod(bcmul($square, $square), $modulus); $exponent = bcdiv($exponent, 2); } return $result; }
/** encrypts / decrypts $text with key ($e, $n) */ function encrypt_text($text, $e, $n) { $tmp = bi_unserialize($text); $e = bi_unserialize($e); $n = bi_unserialize($n); if (bi_cmp($tmp, $n) >= 0) { die('$text is too long to encrypt by key with length ' . bi_bit_len($n) . ' bits' . "<br/>\n"); } return bi_serialize(bi_powmod($tmp, $e, $n)); }
/** * Calculates pow($num, $pow) (mod $mod) * * @param big_int resource $num * @param big_int resource $pow * @param big_int resource $mod * @return big_int resource * @access public */ function powmod($num, $pow, $mod) { return bi_powmod($num, $pow, $mod); }
/** * Generates the secret key using the private key and prime * * @param string Public key passed in by the request * * @return string String containing the shared secret */ function fetch_shared_secret($foreignpubkey) { $this->secret = bi_powmod($foreignpubkey, $this->privatekey, $this->prime); return $this->secret; }
public static function powmod($x, $y, $m) { switch (BigInt::support()) { case 'gmp': return gmp_powm($x, $y, $m); case 'big_int': return bi_powmod($x, $y, $m); case 'bcmath': return bcpowmod($x, $y, $m); case '': default: return BigInt::_powmod($x, $y, $m); } }
function bcpowmod($a, $b, $c) { return bi_to_str(bi_powmod($a, $b, $c)); }
function powm($base, $exponent, $modulus) { if (function_exists('gmp_powm')) { return gmp_strval(gmp_powm($base, $exponent, $modulus)); } else { if (function_exists('bi_powmod')) { return bi_sto_str(bi_powmod($base, $exponent, $modulus)); } else { if (function_exists('bcpowmod')) { return bcpowmod($base, $exponent, $modulus); } else { if (preg_match("/^\\d+,\\d+,\\d+\$/", "{$base},{$exponent},{$modulus}")) { //@FIX: this is insecure - a bi-directional proc_open() is required if (is_executable("/usr/bin/python")) { $r = trim(`python -c "print pow({$base}, {$exponent}, {$modulus})"`); } else { $r = trim(`perl -e "use Math::BigInt; print Math::BigInt->new('{$base}')->bmodpow('{$exponent}', '{$modulus}')->bstr();"`); } if (preg_match("/^\\d+\$/", $r)) { return $r; } } } } } trigger_error("powmod: unsupported or non-integer argument", E_USER_ERROR); }
function bigint_powmod($x, $y, $m) { return bi_powmod($x, $y, $m); }
/* functions for modular arithmetic calculations */ echo '<h3>modular arithmetic functions</h3>' . "\n"; // find next pseudoprime number after $c $modulus = bi_next_prime($c); echo '$modulus = next_prime($c) = [', bi_to_str($modulus), "]<br/>\n"; $c = bi_addmod($a, $b, $modulus); echo '$a + $b (mod $modulus) = [', bi_to_str($c), "]<br/>\n"; $c = bi_submod($a, $b, $modulus); echo '$a - $b (mod $modulus) = [', bi_to_str($c), "]<br/>\n"; $c = bi_mulmod($a, $b, $modulus); echo '$a * $b (mod $modulus) = [', bi_to_str($c), "]<br/>\n"; $c = bi_divmod($a, $b, $modulus); echo '$a / $b (mod $modulus) = [', bi_to_str($c), "]<br/>\n"; $c = bi_powmod($a, $b, $modulus); echo 'pow($a, $b) (mod $modulus) = [', bi_to_str($c), "]<br/>\n"; $c = bi_factmod(1000, $modulus); echo '1000! (mod $modulus) = [', bi_to_str($c), "]<br/>\n"; $c = bi_absmod(-1, $modulus); echo '-1 (mod $modulus) = [', bi_to_str($c), "]<br/>\n"; $c = bi_invmod($a, $modulus); echo '1 / $a (mod $modulus) = [', bi_to_str($c), "]<br/>\n"; $c = bi_sqrmod($a, $modulus); echo 'sqr($a) (mod $modulus) = [', bi_to_str($c), "]<br/>\n"; echo 'cmp($a, $b) (mod $modulus) = ', bi_cmpmod($a, $b, $modulus), "<br/>\n"; /* other functions */ echo '<h3>other functions</h3>' . "\n"; /*
/** * Raise an arbitrary precision number to another, reduced by a specified modulus * * @param string $base The left operand, as a string. * @param string $exp The right operand, as a string. * @param string $mod The modulus, as a string. * @access public * @return string|null Returns the result as a string, or <b>NULL</b> if modulus is 0. */ public function powmod($base, $exp, $mod) { return bi_powmod($base, $exp, $mod); }