| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
/* |
| 6 |
* The MIT License (MIT) |
| 7 |
* |
| 8 |
* Copyright (c) 2014-2021 Spomky-Labs |
| 9 |
* |
| 10 |
* This software may be modified and distributed under the terms |
| 11 |
* of the MIT license. See the LICENSE file for details. |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace WP_DEFENDER_VENDOR\Cose; |
| 15 |
|
| 16 |
use Brick\Math\BigInteger as BrickBigInteger; |
| 17 |
use function chr; |
| 18 |
use function hex2bin; |
| 19 |
use InvalidArgumentException; |
| 20 |
use function unpack; |
| 21 |
|
| 22 |
/** |
| 23 |
* @internal |
| 24 |
*/ |
| 25 |
class BigInteger |
| 26 |
{ |
| 27 |
/** |
| 28 |
* Holds the BigInteger's value. |
| 29 |
* |
| 30 |
* @var BrickBigInteger |
| 31 |
*/ |
| 32 |
private $value; |
| 33 |
|
| 34 |
private function __construct(BrickBigInteger $value) |
| 35 |
{ |
| 36 |
$this->value = $value; |
| 37 |
} |
| 38 |
|
| 39 |
public static function createFromBinaryString(string $value): self |
| 40 |
{ |
| 41 |
$res = unpack('H*', $value); |
| 42 |
if (false === $res) { |
| 43 |
throw new InvalidArgumentException('Unable to convert the data from binary'); |
| 44 |
} |
| 45 |
$data = current($res); |
| 46 |
|
| 47 |
return new self(BrickBigInteger::fromBase($data, 16)); |
| 48 |
} |
| 49 |
|
| 50 |
public static function createFromDecimal(int $value): self |
| 51 |
{ |
| 52 |
return new self(BrickBigInteger::of($value)); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Converts a BigInteger to a binary string. |
| 57 |
*/ |
| 58 |
public function toBytes(): string |
| 59 |
{ |
| 60 |
if ($this->value->isEqualTo(BrickBigInteger::zero())) { |
| 61 |
return ''; |
| 62 |
} |
| 63 |
|
| 64 |
$temp = $this->value->toBase(16); |
| 65 |
$temp = 0 !== (mb_strlen($temp, '8bit') & 1) ? '0'.$temp : $temp; |
| 66 |
$temp = hex2bin($temp); |
| 67 |
if (false === $temp) { |
| 68 |
throw new InvalidArgumentException('Unable to convert the data into binary'); |
| 69 |
} |
| 70 |
|
| 71 |
return ltrim($temp, chr(0)); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Adds two BigIntegers. |
| 76 |
* |
| 77 |
* @param BigInteger $y |
| 78 |
* |
| 79 |
* @return BigInteger |
| 80 |
*/ |
| 81 |
public function add(self $y): self |
| 82 |
{ |
| 83 |
$value = $this->value->plus($y->value); |
| 84 |
|
| 85 |
return new self($value); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Subtracts two BigIntegers. |
| 90 |
* |
| 91 |
* @param BigInteger $y |
| 92 |
* |
| 93 |
* @return BigInteger |
| 94 |
*/ |
| 95 |
public function subtract(self $y): self |
| 96 |
{ |
| 97 |
$value = $this->value->minus($y->value); |
| 98 |
|
| 99 |
return new self($value); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Multiplies two BigIntegers. |
| 104 |
* |
| 105 |
* @param BigInteger $x |
| 106 |
* |
| 107 |
* @return BigInteger |
| 108 |
*/ |
| 109 |
public function multiply(self $x): self |
| 110 |
{ |
| 111 |
$value = $this->value->multipliedBy($x->value); |
| 112 |
|
| 113 |
return new self($value); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Performs modular exponentiation. |
| 118 |
* |
| 119 |
* @param BigInteger $e |
| 120 |
* @param BigInteger $n |
| 121 |
* |
| 122 |
* @return BigInteger |
| 123 |
*/ |
| 124 |
public function modPow(self $e, self $n): self |
| 125 |
{ |
| 126 |
$value = $this->value->modPow($e->value, $n->value); |
| 127 |
|
| 128 |
return new self($value); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Performs modular exponentiation. |
| 133 |
* |
| 134 |
* @param BigInteger $d |
| 135 |
* |
| 136 |
* @return BigInteger |
| 137 |
*/ |
| 138 |
public function mod(self $d): self |
| 139 |
{ |
| 140 |
$value = $this->value->mod($d->value); |
| 141 |
|
| 142 |
return new self($value); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Compares two numbers. |
| 147 |
* |
| 148 |
* @param BigInteger $y |
| 149 |
*/ |
| 150 |
public function compare(self $y): int |
| 151 |
{ |
| 152 |
return $this->value->compareTo($y->value); |
| 153 |
} |
| 154 |
} |
| 155 |
|