Failed Conditions
Pull Request — master (#159)
by
unknown
07:54
created

BigInteger::equals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 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\Util;
13
14
use Assert\Assertion;
15
16
final class BigInteger
17
{
18
    /**
19
     * Holds the BigInteger's value.
20
     *
21
     * @var resource
22
     */
23
    private $value;
24
25
    /**
26
     * Converts base-10 and binary strings (base-256) to BigIntegers.
27
     *
28
     * @param \GMP|string $value
29
     * @param int         $base
30
     */
31
    private function __construct($value, $base)
32
    {
33
        if ($value instanceof \GMP) {
0 ignored issues
show
Bug introduced by
The class GMP does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
34
            $this->value = $value;
0 ignored issues
show
Documentation Bug introduced by
It seems like $value of type object<GMP> is incompatible with the declared type resource of property $value.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
35
36
            return;
37
        }
38
39
        $this->value = gmp_init($value, $base);
0 ignored issues
show
Documentation Bug introduced by
It seems like gmp_init($value, $base) of type object<GMP> is incompatible with the declared type resource of property $value.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40
    }
41
42
    /**
43
     * @param resource $value
44
     *
45
     * @return \Jose\Util\BigInteger
46
     */
47
    public static function createFromGMPResource($value)
48
    {
49
        Assertion::isInstanceOf($value, \GMP::class);
50
51
        return new self($value, 0);
0 ignored issues
show
Documentation introduced by
$value is of type resource, but the function expects a object<GMP>|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
52
    }
53
54
    /**
55
     * @param string $value
56
     *
57
     * @return \Jose\Util\BigInteger
58
     */
59
    public static function createFromBinaryString($value)
60
    {
61
        Assertion::string($value);
62
        $value = '0x'.bin2hex($value);
63
64
        return new self($value, 16);
65
    }
66
67
    /**
68
     * @param string $value
69
     *
70
     * @return \Jose\Util\BigInteger
71
     */
72
    public static function createFromDecimalString($value)
73
    {
74
        Assertion::string($value);
75
76
        return new self($value, 10);
77
    }
78
79
    /**
80
     * @param int $value
81
     *
82
     * @return \Jose\Util\BigInteger
83
     */
84
    public static function createFromDecimal($value)
85
    {
86
        Assertion::integer($value);
87
88
        return new self($value, 10);
89
    }
90
91
    /**
92
     * Converts a BigInteger to a binary string.
93
     *
94
     * @return string
95
     */
96
    public function toBytes()
97
    {
98
        if (gmp_cmp($this->value, gmp_init(0)) === 0) {
99
            return '';
100
        }
101
102
        $temp = gmp_strval(gmp_abs($this->value), 16);
103
        $temp = mb_strlen($temp, '8bit') & 1 ? '0'.$temp : $temp;
104
        $temp = hex2bin($temp);
105
106
        return ltrim($temp, chr(0));
107
    }
108
109
    /**
110
     * Adds two BigIntegers.
111
     *
112
     *  @param \Jose\Util\BigInteger $y
113
     *
114
     *  @return \Jose\Util\BigInteger
115
     */
116
    public function add(BigInteger $y)
117
    {
118
        $value = gmp_add($this->value, $y->value);
119
120
        return self::createFromGMPResource($value);
121
    }
122
123
    /**
124
     * Subtracts two BigIntegers.
125
     *
126
     *  @param \Jose\Util\BigInteger $y
127
     *
128
     *  @return \Jose\Util\BigInteger
129
     */
130
    public function subtract(BigInteger $y)
131
    {
132
        $value = gmp_sub($this->value, $y->value);
133
134
        return self::createFromGMPResource($value);
135
    }
136
137
    /**
138
     * Multiplies two BigIntegers.
139
     *
140
     * @param \Jose\Util\BigInteger $x
141
     *
142
     *  @return \Jose\Util\BigInteger
143
     */
144
    public function multiply(BigInteger $x)
145
    {
146
        $value = gmp_mul($this->value, $x->value);
147
148
        return self::createFromGMPResource($value);
149
    }
150
151
    /**
152
     * Divides two BigIntegers.
153
     *
154
     * @param BigInteger $x
155
     *
156
     * @return BigInteger
157
     */
158
    public function divide(BigInteger $x)
159
    {
160
        $value = gmp_div($this->value, $x->value);
161
162
        return self::createFromGMPResource($value);
163
    }
164
165
    /**
166
     * Performs modular exponentiation.
167
     *
168
     * @param \Jose\Util\BigInteger $e
169
     * @param \Jose\Util\BigInteger $n
170
     *
171
     * @return \Jose\Util\BigInteger
172
     */
173
    public function modPow(BigInteger $e, BigInteger $n)
174
    {
175
        $value = gmp_powm($this->value, $e->value, $n->value);
176
177
        return self::createFromGMPResource($value);
178
    }
179
180
    /**
181
     * Performs modular exponentiation.
182
     *
183
     * @param \Jose\Util\BigInteger $d
184
     *
185
     * @return \Jose\Util\BigInteger
186
     */
187
    public function mod(BigInteger $d)
188
    {
189
        $value = gmp_mod($this->value, $d->value);
190
191
        return self::createFromGMPResource($value);
192
    }
193
194
    /**
195
     * Calculates modular inverses.
196
     *
197
     * @param \Jose\Util\BigInteger $n
198
     *
199
     * @return \Jose\Util\BigInteger
200
     */
201
    public function modInverse(BigInteger $n)
202
    {
203
        $value = gmp_invert($this->value, $n->value);
204
        Assertion::isInstanceOf($value, \GMP::class);
205
206
        return self::createFromGMPResource($value);
207
    }
208
209
    /**
210
     * Calculates the greatest common denominator.
211
     *
212
     * @param BigInteger $x
213
     *
214
     * @return BigInteger
215
     */
216
    public function gcd(BigInteger $x)
217
    {
218
        $value = gmp_gcd($this->value, $x->value);
219
220
        return self::createFromGMPResource($value);
221
    }
222
223
    /**
224
     * Compares two numbers.
225
     *
226
     * @param \Jose\Util\BigInteger $y
227
     *
228
     * @return int < 0 if $this is less than $y; > 0 if $this is greater than $y, and 0 if they are equal.
229
     */
230
    public function compare(BigInteger $y)
231
    {
232
        return gmp_cmp($this->value, $y->value);
233
    }
234
235
    /**
236
     * Check whether two BigIntegers are the same.
237
     *
238
     * @param BigInteger $x
239
     *
240
     * @return bool
241
     */
242
    public function equals(BigInteger $x)
243
    {
244
        return 0 === gmp_cmp($this->value, $x->value);
245
    }
246
247
    /**
248
     * Whether this is an even number.
249
     *
250
     * @return bool
251
     */
252
    public function isEven()
253
    {
254
        $zero = self::createFromDecimal(0);
255
        $two = self::createFromDecimal(2);
256
257
        return $this->mod($two)->equals($zero);
258
    }
259
260
    /**
261
     * Generate a random integer between min and max.
262
     *
263
     * @param BigInteger $min
264
     * @param BigInteger $max
265
     *
266
     * @return BigInteger
267
     */
268
    public static function randomInt(BigInteger $min, BigInteger $max)
269
    {
270
        $value = gmp_random_range($min->value, $max->value);
271
272
        return self::createFromGMPResource($value);
273
    }
274
}
275