1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2017 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 Jose\Component\Core\Util\Ecc; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Math. |
18
|
|
|
*/ |
19
|
|
|
final class Math |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @param \GMP $first |
23
|
|
|
* @param \GMP $other |
24
|
|
|
* |
25
|
|
|
* @return int |
26
|
|
|
*/ |
27
|
|
|
public static function cmp(\GMP $first, \GMP $other): int |
28
|
|
|
{ |
29
|
|
|
return gmp_cmp($first, $other); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param \GMP $first |
34
|
|
|
* @param \GMP $other |
35
|
|
|
* |
36
|
|
|
* @return bool |
37
|
|
|
*/ |
38
|
|
|
public static function equals(\GMP $first, \GMP $other): bool |
39
|
|
|
{ |
40
|
|
|
return gmp_cmp($first, $other) === 0; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param \GMP $number |
45
|
|
|
* @param \GMP $modulus |
46
|
|
|
* |
47
|
|
|
* @return \GMP |
48
|
|
|
*/ |
49
|
|
|
public static function mod(\GMP $number, \GMP $modulus): \GMP |
50
|
|
|
{ |
51
|
|
|
return gmp_mod($number, $modulus); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param \GMP $augend |
56
|
|
|
* @param \GMP $addend |
57
|
|
|
* |
58
|
|
|
* @return \GMP |
59
|
|
|
*/ |
60
|
|
|
public static function add(\GMP $augend, \GMP $addend): \GMP |
61
|
|
|
{ |
62
|
|
|
return gmp_add($augend, $addend); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param \GMP $minuend |
67
|
|
|
* @param \GMP $subtrahend |
68
|
|
|
* |
69
|
|
|
* @return \GMP |
70
|
|
|
*/ |
71
|
|
|
public static function sub(\GMP $minuend, \GMP $subtrahend): \GMP |
72
|
|
|
{ |
73
|
|
|
return gmp_sub($minuend, $subtrahend); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param \GMP $multiplier |
78
|
|
|
* @param \GMP $multiplicand |
79
|
|
|
* |
80
|
|
|
* @return \GMP |
81
|
|
|
*/ |
82
|
|
|
public static function mul(\GMP $multiplier, \GMP $multiplicand): \GMP |
83
|
|
|
{ |
84
|
|
|
return gmp_mul($multiplier, $multiplicand); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param \GMP $base |
89
|
|
|
* @param int $exponent |
90
|
|
|
* |
91
|
|
|
* @return \GMP |
92
|
|
|
*/ |
93
|
|
|
public static function pow(\GMP $base, int $exponent): \GMP |
94
|
|
|
{ |
95
|
|
|
return gmp_pow($base, $exponent); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param \GMP $first |
100
|
|
|
* @param \GMP $other |
101
|
|
|
* |
102
|
|
|
* @return \GMP |
103
|
|
|
*/ |
104
|
|
|
public static function bitwiseAnd(\GMP $first, \GMP $other): \GMP |
105
|
|
|
{ |
106
|
|
|
return gmp_and($first, $other); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param \GMP $first |
111
|
|
|
* @param \GMP $other |
112
|
|
|
* |
113
|
|
|
* @return \GMP |
114
|
|
|
*/ |
115
|
|
|
public static function bitwiseXor(\GMP $first, \GMP $other): \GMP |
116
|
|
|
{ |
117
|
|
|
return gmp_xor($first, $other); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param \GMP $value |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public static function toString(\GMP $value): string |
126
|
|
|
{ |
127
|
|
|
return gmp_strval($value); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param \GMP $a |
132
|
|
|
* @param \GMP $m |
133
|
|
|
* |
134
|
|
|
* @return \GMP |
135
|
|
|
*/ |
136
|
|
|
public static function inverseMod(\GMP $a, \GMP $m): \GMP |
137
|
|
|
{ |
138
|
|
|
return gmp_invert($a, $m); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $number |
143
|
|
|
* @param int $from |
144
|
|
|
* @param int $to |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
public static function baseConvert(string $number, int $from, int $to): string |
149
|
|
|
{ |
150
|
|
|
return gmp_strval(gmp_init($number, $from), $to); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param \GMP $number |
155
|
|
|
* @param int $positions |
156
|
|
|
* |
157
|
|
|
* @return \GMP |
158
|
|
|
*/ |
159
|
|
|
public static function rightShift(\GMP $number, int $positions): \GMP |
160
|
|
|
{ |
161
|
|
|
return gmp_div($number, gmp_pow(gmp_init(2, 10), $positions)); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param string $s |
166
|
|
|
* |
167
|
|
|
* @return \GMP |
168
|
|
|
*/ |
169
|
|
|
public static function stringToInt(string $s): \GMP |
170
|
|
|
{ |
171
|
|
|
$result = gmp_init(0, 10); |
172
|
|
|
$sLen = mb_strlen($s, '8bit'); |
173
|
|
|
|
174
|
|
|
for ($c = 0; $c < $sLen; ++$c) { |
175
|
|
|
$result = gmp_add(gmp_mul(256, $result), gmp_init(ord($s[$c]), 10)); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $result; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|