BigInteger
1 year ago
BinaryField
1 year ago
Common
1 year ago
PrimeField
1 year ago
.htaccess
1 year ago
BigInteger.php
1 year ago
BinaryField.php
1 year ago
PrimeField.php
1 year ago
index.html
1 year ago
web.config
1 year ago
PrimeField.php
111 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Prime Finite Fields |
| 5 | * |
| 6 | * Utilizes the factory design pattern |
| 7 | * |
| 8 | * PHP version 5 and 7 |
| 9 | * |
| 10 | * @author Jim Wigginton <terrafrost@php.net> |
| 11 | * @copyright 2017 Jim Wigginton |
| 12 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
| 13 | * @link http://pear.php.net/package/Math_BigInteger |
| 14 | */ |
| 15 | |
| 16 | declare(strict_types=1); |
| 17 | |
| 18 | namespace phpseclib3\Math; |
| 19 | |
| 20 | use phpseclib3\Math\Common\FiniteField; |
| 21 | use phpseclib3\Math\PrimeField\Integer; |
| 22 | |
| 23 | /** |
| 24 | * Prime Finite Fields |
| 25 | * |
| 26 | * @author Jim Wigginton <terrafrost@php.net> |
| 27 | */ |
| 28 | class PrimeField extends FiniteField |
| 29 | { |
| 30 | /** |
| 31 | * Instance Counter |
| 32 | * |
| 33 | * @var int |
| 34 | */ |
| 35 | private static $instanceCounter = 0; |
| 36 | |
| 37 | /** |
| 38 | * Keeps track of current instance |
| 39 | * |
| 40 | * @var int |
| 41 | */ |
| 42 | protected $instanceID; |
| 43 | |
| 44 | /** |
| 45 | * Default constructor |
| 46 | */ |
| 47 | public function __construct(BigInteger $modulo) |
| 48 | { |
| 49 | if (!$modulo->isPrime()) { |
| 50 | throw new \phpseclib3\Exception\UnexpectedValueException('PrimeField requires a prime number be passed to the constructor'); |
| 51 | } |
| 52 | |
| 53 | $this->instanceID = self::$instanceCounter++; |
| 54 | Integer::setModulo($this->instanceID, $modulo); |
| 55 | Integer::setRecurringModuloFunction($this->instanceID, $modulo->createRecurringModuloFunction()); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Use a custom defined modular reduction function |
| 60 | */ |
| 61 | public function setReduction(\Closure $func): void |
| 62 | { |
| 63 | $this->reduce = $func->bindTo($this, $this); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns an instance of a dynamically generated PrimeFieldInteger class |
| 68 | */ |
| 69 | public function newInteger(BigInteger $num): Integer |
| 70 | { |
| 71 | return new Integer($this->instanceID, $num); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns an integer on the finite field between one and the prime modulo |
| 76 | */ |
| 77 | public function randomInteger(): Integer |
| 78 | { |
| 79 | static $one; |
| 80 | if (!isset($one)) { |
| 81 | $one = new BigInteger(1); |
| 82 | } |
| 83 | |
| 84 | return new Integer($this->instanceID, BigInteger::randomRange($one, Integer::getModulo($this->instanceID))); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns the length of the modulo in bytes |
| 89 | */ |
| 90 | public function getLengthInBytes(): int |
| 91 | { |
| 92 | return Integer::getModulo($this->instanceID)->getLengthInBytes(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns the length of the modulo in bits |
| 97 | */ |
| 98 | public function getLength(): int |
| 99 | { |
| 100 | return Integer::getModulo($this->instanceID)->getLength(); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Destructor |
| 105 | */ |
| 106 | public function __destruct() |
| 107 | { |
| 108 | Integer::cleanupCache($this->instanceID); |
| 109 | } |
| 110 | } |
| 111 |