Ejemplo n.º 1
0
 public static function num2str($num)
 {
     switch (BigInt::support()) {
         case 'gmp':
             $str = gmp_strval($num, 16);
             $len = strlen($str);
             if ($len % 2 == 1) {
                 $str = '0' . $str;
             }
             return pack("H*", $str);
         case 'big_int':
             $str = bi_to_str($num, 16);
             $len = strlen($str);
             if ($len % 2 == 1) {
                 $str = '0' . $str;
             }
             return pack("H*", $str);
         case 'bcmath':
             bcscale(0);
             $str = "";
             while (bccomp($num, '0') == 1) {
                 $str = chr(bcmod($num, '256')) . $str;
                 $num = bcdiv($num, '256');
             }
             return $str;
         case '':
         default:
             return BigInt::_num2str($num);
     }
 }
Ejemplo n.º 2
0
 for ($i = 0; $i < $n; $i++) {
     if (mt_rand(0, 1)) {
         gmp_setbit($result, $i);
     }
 }
 if ($s) {
     gmp_setbit($result, $n - 1);
 }
 return $result;
Ejemplo n.º 3
0
 function bcsqrt($a)
 {
     return bi_to_str(bi_sqrt($a));
 }
Ejemplo n.º 4
0
echo 'pow($a, 4) = [', bi_to_str($c), "]<br/>\n";
// argument of bi_fact() must have integer type (not BIG_INT)
$c = bi_fact(100);
echo '100! = [', bi_to_str($c), "]<br/>\n";
$c = bi_sqrt($a);
echo 'sqrt($a) = [', bi_to_str($c), "]<br/>\n";
$c = bi_sqrt_rem($a);
echo 'sqrt_rem($a) = [', bi_to_str($c), "]<br/>\n";
$c = bi_gcd($a, $b);
echo 'GCD($a, $b) = [', bi_to_str($c), "]<br/>\n";
// Miller-Rabin primality test of $a
echo 'miller_test($a, $b) = ', bi_miller_test($a, $b), "<br/>\n";
// primality test of $a
echo 'is_prime($a) = ', bi_is_prime($a), "<br/>\n";
// Jacobi symbol ($a|$b)
echo 'jacobi($a, $b) = ', bi_jacobi($a, $b), "<br/>\n";
$c = bi_div_extended($a, $b);
echo '$a = $b * ', bi_to_str($c[0]), ' + ', bi_to_str($c[1]), "<br/>\n";
$c = bi_gcd_extended($a, $b);
echo 'abs($a) * ', bi_to_str($c[1]), ' + abs($b) * ', bi_to_str($c[2]), ' = ', bi_to_str($c[0]), "<br/>\n";
// serialize $a into sequence of bytes
$str = bi_serialize($a);
echo '$str = serialize($a) = [', base64_encode($str), "]<br/>\n";
// unserialize $a from sequence of bytes
$a = bi_unserialize($str);
echo 'unserialize($str) = [', bi_to_str($a), "]<br/>\n";
// show package info
var_dump(bi_info());
echo 'the list of all functions in BIG_INT module:', "<br/>\n";
echo implode(", ", get_extension_funcs('big_int'));
echo "<br/>\n";
Ejemplo n.º 5
0
/**
    example:
    usage of user function as random number generator
    for bi_rand() function.
    bi_rand() uses only lower byte of value, returned by
    user function.
*/
require_once dirname(__FILE__) . '/std_header.php';
echo "const generator:<br>\n";
echo bi_to_str(bi_rand(100, 'const_generator'), 16), "<br>\n\n";
echo "time generator:<br>\n";
echo bi_to_str(bi_rand(100, 'time_generator'), 16), "<br>\n\n";
echo "microtime generator:<br>\n";
echo bi_to_str(bi_rand(100, 'microtime_generator'), 16), "<br>\n\n";
echo "static generator:<br>\n";
echo bi_to_str(bi_rand(100, 'static_generator'), 16), "<br>\n\n";
exit;
/***************************************************************************/
/**
    'constant' number generator
*/
function const_generator()
{
    return 0xff;
}
/**
    time() number generator
*/
function time_generator()
{
    //sleep(1);
Ejemplo n.º 6
0
<?php

$two = bi_from_str('2');
$four = bi_add($two, $two);
// Use bi_to_str() to get strings from big_int resources
print bi_to_str($four);
// prints 4
// Computing large factorials very quickly
$factorial = bi_fact(20);
Ejemplo n.º 7
0
require_once dirname(__FILE__) . '/std_header.php';
$a = bi_from_str('1110110110110101011000100010110011101110111', 2);
$b = bi_from_str('11010010101111101010000101010100011101', 2);
echo '$a = ', bi_to_str($a, 2), "\n";
echo '$b = ', bi_to_str($b, 2), "\n";
////////////////////////////////////////////////
$c = bi_andnot($a, $b);
echo 'bi_andnot($a, $b) = ', bi_to_str($c, 2), "\n";
$c = bi_xor($a, $b);
echo 'bi_xor($a, $b) = ', bi_to_str($c, 2), "\n";
$c = bi_or($a, $b);
echo 'bi_or($a, $b) = ', bi_to_str($c, 2), "\n";
$c = bi_and($a, $b);
echo 'bi_and($a, $b) = ', bi_to_str($c, 2), "\n";
////////////////////////////////////////////////
$c = bi_andnot($a, $b, 2);
echo 'bi_andnot($a, $b, 2) = ', bi_to_str($c, 2), "\n";
$c = bi_xor($a, $b, 5);
echo 'bi_xor($a, $b, 5) = ', bi_to_str($c, 2), "\n";
$c = bi_or($a, $b, 32);
echo 'bi_or($a, $b, 32) = ', bi_to_str($c, 2), "\n";
$c = bi_or($a, $b, 33);
echo 'bi_or($a, $b, 33) = ', bi_to_str($c, 2), "\n";
$c = bi_or($a, $b, 70);
echo 'bi_or($a, $b, 70) = ', bi_to_str($c, 2), "\n";
$c = bi_and($a, $b, 7);
echo 'bi_and($a, $b, 7) = ', bi_to_str($c, 2), "\n";
////////////////////////////////////////////////
echo '$a = ', bi_to_str($a, 2), "\n";
echo '$b = ', bi_to_str($b, 2), "\n";