1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SKien\PNServer\Utils; |
5
|
|
|
|
6
|
|
|
/* |
7
|
|
|
* extracted required classes and functions from package |
8
|
|
|
* spomky-labs/jose |
9
|
|
|
* https://github.com/Spomky-Labs/Jose |
10
|
|
|
* |
11
|
|
|
* @package PNServer |
12
|
|
|
* @version 1.0.0 |
13
|
|
|
* @copyright MIT License - see the copyright below and LICENSE file for details |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
/* |
17
|
|
|
* ********************************************************************* |
18
|
|
|
* Copyright (c) 2014-2016 Spomky-Labs |
19
|
|
|
* |
20
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
21
|
|
|
* a copy of this software and associated documentation files (the "Software"), |
22
|
|
|
* to deal in the Software without restriction, including without limitation |
23
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
24
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the |
25
|
|
|
* Software is furnished to do so, subject to the following conditions: |
26
|
|
|
* |
27
|
|
|
* The above copyright notice and this permission notice shall be included |
28
|
|
|
* in all copies or substantial portions of the Software. |
29
|
|
|
* |
30
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
31
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
32
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
33
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES |
34
|
|
|
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
35
|
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
36
|
|
|
* OTHER DEALINGS IN THE SOFTWARE. |
37
|
|
|
* *********************************************************************** |
38
|
|
|
*/ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @internal |
42
|
|
|
*/ |
43
|
|
|
class Math |
44
|
|
|
{ |
45
|
|
|
public static function cmp(\GMP $first, \GMP $other) : int |
46
|
|
|
{ |
47
|
|
|
return \gmp_cmp($first, $other); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function equals(\GMP $first, \GMP $other) : bool |
51
|
|
|
{ |
52
|
|
|
return 0 === \gmp_cmp($first, $other); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public static function mod(\GMP $number, \GMP $modulus) : \GMP |
56
|
|
|
{ |
57
|
|
|
return \gmp_mod($number, $modulus); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function add(\GMP $augend, \GMP $addend) : \GMP |
61
|
|
|
{ |
62
|
|
|
return \gmp_add($augend, $addend); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public static function sub(\GMP $minuend, \GMP $subtrahend) : \GMP |
66
|
|
|
{ |
67
|
|
|
return \gmp_sub($minuend, $subtrahend); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public static function mul(\GMP $multiplier, \GMP $multiplicand) : \GMP |
71
|
|
|
{ |
72
|
|
|
return \gmp_mul($multiplier, $multiplicand); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public static function pow(\GMP $base, int $exponent) : \GMP |
76
|
|
|
{ |
77
|
|
|
return \gmp_pow($base, $exponent); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public static function bitwiseAnd(\GMP $first, \GMP $other) : \GMP |
81
|
|
|
{ |
82
|
|
|
return \gmp_and($first, $other); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public static function bitwiseXor(\GMP $first, \GMP $other) : \GMP |
86
|
|
|
{ |
87
|
|
|
return \gmp_xor($first, $other); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public static function toString(\GMP $value) : string |
91
|
|
|
{ |
92
|
|
|
return \gmp_strval($value); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param \GMP $a |
97
|
|
|
* @param \GMP $m |
98
|
|
|
* @return \GMP|false |
99
|
|
|
*/ |
100
|
|
|
public static function inverseMod(\GMP $a, \GMP $m) |
101
|
|
|
{ |
102
|
|
|
return \gmp_invert($a, $m); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param int|string $number |
107
|
|
|
* @param int $from |
108
|
|
|
* @param int $to |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public static function baseConvert($number, int $from, int $to) : string |
112
|
|
|
{ |
113
|
|
|
return \gmp_strval(\gmp_init($number, $from), $to); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public static function rightShift(\GMP $number, int $positions) : \GMP |
117
|
|
|
{ |
118
|
|
|
// when using \gmp_div, phpStan says: Method SKien\PNServer\Utils\Math::rightShift() should return GMP but returns resource. ? |
119
|
|
|
return \gmp_div_q($number, \gmp_pow(\gmp_init(2, 10), $positions)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public static function modSub(\GMP $minuend, \GMP $subtrahend, \GMP $modulus) : \GMP |
123
|
|
|
{ |
124
|
|
|
return self::mod(self::sub($minuend, $subtrahend), $modulus); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public static function modMul(\GMP $multiplier, \GMP $muliplicand, \GMP $modulus) : \GMP |
128
|
|
|
{ |
129
|
|
|
return self::mod(self::mul($multiplier, $muliplicand), $modulus); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public static function modDiv(\GMP $dividend, \GMP $divisor, \GMP $modulus) : \GMP |
133
|
|
|
{ |
134
|
|
|
$moddiv = gmp_init(0); |
135
|
|
|
$invmod = self::inverseMod($divisor, $modulus); |
136
|
|
|
if ($invmod !== false) { |
|
|
|
|
137
|
|
|
$moddiv = self::mul($dividend, $invmod); |
138
|
|
|
} |
139
|
|
|
return $moddiv; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|