| 1 |
<?php |
| 2 |
|
| 3 |
namespace Safe; |
| 4 |
|
| 5 |
use Safe\Exceptions\GmpException; |
| 6 |
|
| 7 |
/** |
| 8 |
* Calculates the binomial coefficient C(n, k). |
| 9 |
* |
| 10 |
* @param \GMP|string|int $n Either a GMP number resource in PHP 5.5 and earlier, a GMP object in PHP 5.6 and later, or a numeric string provided that it is possible to convert the latter to a number. |
| 11 |
* @param int $k |
| 12 |
* @return \GMP Returns the binomial coefficient C(n, k). |
| 13 |
* @throws GmpException |
| 14 |
* |
| 15 |
*/ |
| 16 |
function gmp_binomial($n, int $k): \GMP |
| 17 |
{ |
| 18 |
error_clear_last(); |
| 19 |
$result = \gmp_binomial($n, $k); |
| 20 |
if ($result === false) { |
| 21 |
throw GmpException::createFromPhpError(); |
| 22 |
} |
| 23 |
return $result; |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
/** |
| 28 |
* Export a GMP number to a binary string |
| 29 |
* |
| 30 |
* @param \GMP|string|int $gmpnumber The GMP number being exported |
| 31 |
* @param int $word_size Default value is 1. The number of bytes in each chunk of binary data. This is mainly used in conjunction with the options parameter. |
| 32 |
* @param int $options Default value is GMP_MSW_FIRST | GMP_NATIVE_ENDIAN. |
| 33 |
* @return string Returns a string. |
| 34 |
* @throws GmpException |
| 35 |
* |
| 36 |
*/ |
| 37 |
function gmp_export($gmpnumber, int $word_size = 1, int $options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN): string |
| 38 |
{ |
| 39 |
error_clear_last(); |
| 40 |
$result = \gmp_export($gmpnumber, $word_size, $options); |
| 41 |
if ($result === false) { |
| 42 |
throw GmpException::createFromPhpError(); |
| 43 |
} |
| 44 |
return $result; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
/** |
| 49 |
* Import a GMP number from a binary string |
| 50 |
* |
| 51 |
* @param string $data The binary string being imported |
| 52 |
* @param int $word_size Default value is 1. The number of bytes in each chunk of binary data. This is mainly used in conjunction with the options parameter. |
| 53 |
* @param int $options Default value is GMP_MSW_FIRST | GMP_NATIVE_ENDIAN. |
| 54 |
* @return \GMP Returns a GMP number. |
| 55 |
* @throws GmpException |
| 56 |
* |
| 57 |
*/ |
| 58 |
function gmp_import(string $data, int $word_size = 1, int $options = GMP_MSW_FIRST | GMP_NATIVE_ENDIAN): \GMP |
| 59 |
{ |
| 60 |
error_clear_last(); |
| 61 |
$result = \gmp_import($data, $word_size, $options); |
| 62 |
if ($result === false) { |
| 63 |
throw GmpException::createFromPhpError(); |
| 64 |
} |
| 65 |
return $result; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
/** |
| 70 |
* |
| 71 |
* |
| 72 |
* @param \GMP|string|int $seed The seed to be set for the gmp_random, |
| 73 |
* gmp_random_bits, and |
| 74 |
* gmp_random_range functions. |
| 75 |
* |
| 76 |
* Either a GMP number resource in PHP 5.5 and earlier, a GMP object in PHP 5.6 and later, or a numeric string provided that it is possible to convert the latter to a number. |
| 77 |
* @throws GmpException |
| 78 |
* |
| 79 |
*/ |
| 80 |
function gmp_random_seed($seed): void |
| 81 |
{ |
| 82 |
error_clear_last(); |
| 83 |
$result = \gmp_random_seed($seed); |
| 84 |
if ($result === false) { |
| 85 |
throw GmpException::createFromPhpError(); |
| 86 |
} |
| 87 |
} |
| 88 |
|