1 | <?php |
||
5 | final class GmpMath |
||
6 | { |
||
7 | /** |
||
8 | * @param \GMP $first |
||
9 | * @param \GMP $other |
||
10 | * @return int |
||
11 | */ |
||
12 | public function cmp(\GMP $first, \GMP $other): int |
||
13 | { |
||
14 | return gmp_cmp($first, $other); |
||
15 | } |
||
16 | |||
17 | /** |
||
18 | * @param \GMP $first |
||
19 | * @param \GMP $other |
||
20 | * @return bool |
||
21 | */ |
||
22 | public function equals(\GMP $first, \GMP $other): bool |
||
23 | { |
||
24 | return gmp_cmp($first, $other) === 0; |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * @param \GMP $number |
||
29 | * @param \GMP $modulus |
||
30 | * @return \GMP |
||
31 | */ |
||
32 | public function mod(\GMP $number, \GMP $modulus): \GMP |
||
36 | |||
37 | /** |
||
38 | * @param \GMP $augend |
||
39 | * @param \GMP $addend |
||
40 | * @return \GMP |
||
41 | */ |
||
42 | public function add(\GMP $augend, \GMP $addend): \GMP |
||
43 | { |
||
44 | return gmp_add($augend, $addend); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param \GMP $minuend |
||
49 | * @param \GMP $subtrahend |
||
50 | * @return \GMP |
||
51 | */ |
||
52 | public function sub(\GMP $minuend, \GMP $subtrahend): \GMP |
||
53 | { |
||
54 | return gmp_sub($minuend, $subtrahend); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @param \GMP $multiplier |
||
59 | * @param \GMP $multiplicand |
||
60 | * @return \GMP |
||
61 | */ |
||
62 | public function mul(\GMP $multiplier, \GMP $multiplicand): \GMP |
||
63 | { |
||
64 | return gmp_mul($multiplier, $multiplicand); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param \GMP $dividend |
||
69 | * @param \GMP $divisor |
||
70 | * @return \GMP |
||
71 | */ |
||
72 | public function div(\GMP $dividend, \GMP $divisor): \GMP |
||
76 | |||
77 | /** |
||
78 | * @param \GMP $base |
||
79 | * @param $exponent |
||
80 | * @return \GMP |
||
81 | */ |
||
82 | public function pow(\GMP $base, $exponent): \GMP |
||
83 | { |
||
84 | return gmp_pow($base, $exponent); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param \GMP $first |
||
89 | * @param \GMP $other |
||
90 | * @return \GMP |
||
91 | */ |
||
92 | public function bitwiseAnd(\GMP $first, \GMP $other): \GMP |
||
93 | { |
||
94 | return gmp_and($first, $other); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param \GMP $number |
||
99 | * @param $positions |
||
100 | * @return \GMP |
||
101 | */ |
||
102 | public function rightShift(\GMP $number, $positions): \GMP |
||
103 | { |
||
104 | // Shift 1 right = div / 2 |
||
105 | return gmp_div($number, gmp_pow(gmp_init(2, 10), $positions)); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param \GMP $first |
||
110 | * @param \GMP $other |
||
111 | * @return \GMP |
||
112 | */ |
||
113 | public function bitwiseXor(\GMP $first, \GMP $other): \GMP |
||
114 | { |
||
115 | return gmp_xor($first, $other); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param \GMP $value |
||
120 | * @return string |
||
121 | */ |
||
122 | public function toString(\GMP $value): string |
||
126 | |||
127 | /** |
||
128 | * @param $dec |
||
129 | * @return string |
||
130 | */ |
||
131 | public function decHex($dec): string |
||
132 | { |
||
133 | $dec = gmp_init($dec, 10); |
||
134 | |||
135 | if (gmp_cmp($dec, 0) < 0) { |
||
147 | |||
148 | /** |
||
149 | * @param \GMP $base |
||
150 | * @param \GMP $exponent |
||
151 | * @param \GMP $modulus |
||
152 | * @return \GMP |
||
153 | */ |
||
154 | public function powmod(\GMP $base, \GMP $exponent, \GMP $modulus): \GMP |
||
162 | |||
163 | /** |
||
164 | * @param \GMP $a |
||
165 | * @param \GMP $m |
||
166 | * @return \GMP |
||
167 | */ |
||
168 | public function inverseMod(\GMP $a, \GMP $m): \GMP |
||
172 | |||
173 | /** |
||
174 | * @param \GMP $a |
||
175 | * @param \GMP $n |
||
176 | * @return int |
||
177 | */ |
||
178 | public function jacobi(\GMP $a, \GMP $n): int |
||
182 | |||
183 | /** |
||
184 | * @param $s |
||
185 | * @return \GMP |
||
186 | */ |
||
187 | public function stringToInt($s): \GMP |
||
198 | |||
199 | /** |
||
200 | * @param $number |
||
201 | * @param $from |
||
202 | * @param $to |
||
203 | * @return string |
||
204 | */ |
||
205 | public function baseConvert($number, $from, $to): string |
||
209 | |||
210 | /** |
||
211 | * @return NumberTheory |
||
212 | */ |
||
213 | public function getNumberTheory(): NumberTheory |
||
217 | |||
218 | /** |
||
219 | * @param \GMP $modulus |
||
220 | * |
||
221 | * @return ModularArithmetic |
||
222 | */ |
||
223 | public function getModularArithmetic(\GMP $modulus): ModularArithmetic |
||
227 | } |
||
228 |