1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License (MIT) |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2014-2017 Spomky-Labs |
7
|
|
|
* |
8
|
|
|
* This software may be modified and distributed under the terms |
9
|
|
|
* of the MIT license. See the LICENSE file for details. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jose\Component\Core\Util\Ecc; |
13
|
|
|
|
14
|
|
|
final class Math |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param \GMP $first |
18
|
|
|
* @param \GMP $other |
19
|
|
|
* |
20
|
|
|
* @return int |
21
|
|
|
*/ |
22
|
|
|
public static function cmp(\GMP $first, \GMP $other): int |
23
|
|
|
{ |
24
|
|
|
return gmp_cmp($first, $other); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param \GMP $first |
29
|
|
|
* @param \GMP $other |
30
|
|
|
* |
31
|
|
|
* @return bool |
32
|
|
|
*/ |
33
|
|
|
public static function equals(\GMP $first, \GMP $other): bool |
34
|
|
|
{ |
35
|
|
|
return gmp_cmp($first, $other) === 0; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param \GMP $number |
40
|
|
|
* @param \GMP $modulus |
41
|
|
|
* |
42
|
|
|
* @return \GMP |
43
|
|
|
*/ |
44
|
|
|
public static function mod(\GMP $number, \GMP $modulus): \GMP |
45
|
|
|
{ |
46
|
|
|
return gmp_mod($number, $modulus); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param \GMP $augend |
51
|
|
|
* @param \GMP $addend |
52
|
|
|
* |
53
|
|
|
* @return \GMP |
54
|
|
|
*/ |
55
|
|
|
public static function add(\GMP $augend, \GMP $addend): \GMP |
56
|
|
|
{ |
57
|
|
|
return gmp_add($augend, $addend); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param \GMP $minuend |
62
|
|
|
* @param \GMP $subtrahend |
63
|
|
|
* |
64
|
|
|
* @return \GMP |
65
|
|
|
*/ |
66
|
|
|
public static function sub(\GMP $minuend, \GMP $subtrahend): \GMP |
67
|
|
|
{ |
68
|
|
|
return gmp_sub($minuend, $subtrahend); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param \GMP $multiplier |
73
|
|
|
* @param \GMP $multiplicand |
74
|
|
|
* |
75
|
|
|
* @return \GMP |
76
|
|
|
*/ |
77
|
|
|
public static function mul(\GMP $multiplier, \GMP $multiplicand): \GMP |
78
|
|
|
{ |
79
|
|
|
return gmp_mul($multiplier, $multiplicand); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param \GMP $base |
84
|
|
|
* @param int $exponent |
85
|
|
|
* |
86
|
|
|
* @return \GMP |
87
|
|
|
*/ |
88
|
|
|
public static function pow(\GMP $base, int $exponent): \GMP |
89
|
|
|
{ |
90
|
|
|
return gmp_pow($base, $exponent); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param \GMP $first |
95
|
|
|
* @param \GMP $other |
96
|
|
|
* |
97
|
|
|
* @return \GMP |
98
|
|
|
*/ |
99
|
|
|
public static function bitwiseAnd(\GMP $first, \GMP $other): \GMP |
100
|
|
|
{ |
101
|
|
|
return gmp_and($first, $other); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param \GMP $first |
106
|
|
|
* @param \GMP $other |
107
|
|
|
* |
108
|
|
|
* @return \GMP |
109
|
|
|
*/ |
110
|
|
|
public static function bitwiseXor(\GMP $first, \GMP $other): \GMP |
111
|
|
|
{ |
112
|
|
|
return gmp_xor($first, $other); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param \GMP $value |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public static function toString(\GMP $value): string |
121
|
|
|
{ |
122
|
|
|
return gmp_strval($value); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param \GMP $a |
127
|
|
|
* @param \GMP $m |
128
|
|
|
* |
129
|
|
|
* @return \GMP |
130
|
|
|
*/ |
131
|
|
|
public static function inverseMod(\GMP $a, \GMP $m): \GMP |
132
|
|
|
{ |
133
|
|
|
return gmp_invert($a, $m); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $number |
138
|
|
|
* @param int $from |
139
|
|
|
* @param int $to |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public static function baseConvert(string $number, int $from, int $to): string |
144
|
|
|
{ |
145
|
|
|
return gmp_strval(gmp_init($number, $from), $to); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|