Failed Conditions
Push — v7 ( 896db9...bffd87 )
by Florent
03:38
created

GmpMath::intToString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A GmpMath::getModularArithmetic() 0 4 1
1
<?php
2
3
namespace Jose\Component\Core\Util\Ecc\Math;
4
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
33
    {
34
        return gmp_mod($number, $modulus);
35
    }
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
73
    {
74
        return gmp_div($dividend, $divisor);
75
    }
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
123
    {
124
        return gmp_strval($value);
125
    }
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) {
136
            throw new \InvalidArgumentException('Unable to convert negative integer to string');
137
        }
138
139
        $hex = gmp_strval($dec, 16);
140
141
        if (mb_strlen($hex, '8bit') % 2 !== 0) {
142
            $hex = '0'.$hex;
143
        }
144
145
        return $hex;
146
    }
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
155
    {
156
        if ($this->cmp($exponent, gmp_init(0, 10)) < 0) {
157
            throw new \InvalidArgumentException("Negative exponents (" . $this->toString($exponent) . ") not allowed.");
158
        }
159
160
        return gmp_powm($base, $exponent, $modulus);
161
    }
162
163
    /**
164
     * @param \GMP $a
165
     * @param \GMP $m
166
     * @return \GMP
167
     */
168
    public function inverseMod(\GMP $a, \GMP $m): \GMP
169
    {
170
        return gmp_invert($a, $m);
171
    }
172
173
    /**
174
     * @param \GMP $a
175
     * @param \GMP $n
176
     * @return int
177
     */
178
    public function jacobi(\GMP $a, \GMP $n): int
179
    {
180
        return gmp_jacobi($a, $n);
181
    }
182
183
    /**
184
     * @param $s
185
     * @return \GMP
186
     */
187
    public function stringToInt($s): \GMP
188
    {
189
        $result = gmp_init(0, 10);
190
        $sLen = mb_strlen($s, '8bit');
191
192
        for ($c = 0; $c < $sLen; $c ++) {
193
            $result = gmp_add(gmp_mul(256, $result), gmp_init(ord($s[$c]), 10));
194
        }
195
196
        return $result;
197
    }
198
199
    /**
200
     * @param $number
201
     * @param $from
202
     * @param $to
203
     * @return string
204
     */
205
    public function baseConvert($number, $from, $to): string
206
    {
207
        return gmp_strval(gmp_init($number, $from), $to);
208
    }
209
210
    /**
211
     * @return NumberTheory
212
     */
213
    public function getNumberTheory(): NumberTheory
214
    {
215
        return new NumberTheory();
216
    }
217
218
    /**
219
     * @param \GMP $modulus
220
     *
221
     * @return ModularArithmetic
222
     */
223
    public function getModularArithmetic(\GMP $modulus): ModularArithmetic
224
    {
225
        return new ModularArithmetic($modulus);
226
    }
227
}
228