Completed
Push — master ( a22894...37130c )
by thomas
62:03 queued 58:09
created

Math::lessThanEq()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Math;
4
5
use \Mdanter\Ecc\Math\GmpMath;
6
use Mdanter\Ecc\Util\NumberSize;
7
8
class Math extends GmpMath
9
{
10
    /**
11
     * @return BinaryMath
12
     */
13 66
    public function getBinaryMath()
14
    {
15 66
        return new BinaryMath($this);
16
    }
17
18
    /**
19
     * @param $integer
20
     * @return bool
21
     */
22 159
    public function isEven($integer)
23
    {
24 159
        return $this->cmp($this->mod($integer, gmp_init(2)), gmp_init(0)) === 0;
25
    }
26
27
    /**
28
     * @param \GMP $int
29
     * @param \GMP $otherInt
30
     * @return \GMP
31
     */
32 186
    public function bitwiseOr(\GMP $int, \GMP $otherInt)
33
    {
34 186
        return gmp_or($int, $otherInt);
35
    }
36
37
    /**
38
     * Similar to gmp_div_qr, return a tuple containing the
39
     * result and the remainder
40
     *
41
     * @param \GMP $dividend
42
     * @param \GMP $divisor
43
     * @return array
44
     */
45 156
    public function divQr(\GMP $dividend, \GMP $divisor)
46
    {
47
        // $div = n / q
48 156
        $div = $this->div($dividend, $divisor);
49
        // $remainder = n - (n / q) * q
50 156
        $remainder = $this->sub($dividend, $this->mul($div, $divisor));
51 156
        return array($div, $remainder);
52
    }
53
54
    /**
55
     * @param int $compact
56
     * @param bool|false $isNegative
57
     * @param bool|false $isOverflow
58
     * @return \GMP
59
     */
60 630
    public function writeCompact($compact, &$isNegative, &$isOverflow)
61
    {
62 630
        $compact = gmp_init($compact, 10);
63 630
        $size = $this->rightShift($compact, 24);
64 630
        $word = $this->bitwiseAnd($compact, gmp_init(0x007fffff));
65 630
        if ($this->cmp($size, gmp_init(3)) <= 0) {
66 39
            $positions = $this->toString($this->mul(gmp_init(8), $this->sub(gmp_init(3), $size)));
67 39
            $word = $this->rightShift($word, $positions);
68 26
        } else {
69 591
            $positions = $this->toString($this->mul(gmp_init(8), $this->sub($size, gmp_init(3))));
70 591
            $word = $this->leftShift($word, $positions);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->leftShift($word, $positions); of type resource adds the type resource to the return on line 83 which is incompatible with the return type documented by BitWasp\Bitcoin\Math\Math::writeCompact of type GMP.
Loading history...
71
        }
72
73
        // isNegative: $word != 0 && $uint32 & 0x00800000 != 0
74
        // isOverflow: $word != 0 && (($size > 34) || ($word > 0xff && $size > 33) || ($word > 0xffff && $size  >32))
75 630
        $zero = gmp_init(0);
76 630
        $isNegative = ($this->cmp($word, $zero) !== 0) && ($this->cmp($this->bitwiseAnd($compact, gmp_init(0x00800000)), $zero) === 1);
0 ignored issues
show
Bug introduced by
It seems like $word defined by $this->leftShift($word, $positions) on line 70 can also be of type resource; however, Mdanter\Ecc\Math\GmpMath::cmp() does only seem to accept object<GMP>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
77 630
        $isOverflow = $this->cmp($word, $zero) !== 0 && (
0 ignored issues
show
Bug introduced by
It seems like $word defined by $this->leftShift($word, $positions) on line 70 can also be of type resource; however, Mdanter\Ecc\Math\GmpMath::cmp() does only seem to accept object<GMP>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
78 597
                ($this->cmp($size, gmp_init(34)) > 0)
79 594
                || ($this->cmp($word, gmp_init(0xff)) > 0 && $this->cmp($size, gmp_init(33)) > 0)
0 ignored issues
show
Bug introduced by
It seems like $word defined by $this->leftShift($word, $positions) on line 70 can also be of type resource; however, Mdanter\Ecc\Math\GmpMath::cmp() does only seem to accept object<GMP>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
80 606
                || ($this->cmp($word, gmp_init(0xffff)) > 0 && $this->cmp($size, gmp_init(32)) > 0)
0 ignored issues
show
Bug introduced by
It seems like $word defined by $this->leftShift($word, $positions) on line 70 can also be of type resource; however, Mdanter\Ecc\Math\GmpMath::cmp() does only seem to accept object<GMP>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
81 420
            );
82
83 630
        return $word;
84
    }
85
86
    /**
87
     * @param \GMP $integer
88
     * @return \GMP
89
     */
90 54
    public function getLow64(\GMP $integer)
91
    {
92 54
        $bits = gmp_strval($integer, 2);
93 54
        $bits = substr($bits, 0, 64);
94 54
        $bits = str_pad($bits, 64, '0', STR_PAD_LEFT);
95 54
        return gmp_init($bits, 2);
96
    }
97
98
    /**
99
     * @param \GMP $integer
100
     * @param bool $fNegative
101
     * @return \GMP
102
     */
103 57
    public function parseCompact(\GMP $integer, $fNegative)
104
    {
105 57
        if (!is_bool($fNegative)) {
106 3
            throw new \InvalidArgumentException('CompactInteger::read() - flag must be boolean!');
107
        }
108
109 54
        $size = (int) NumberSize::bnNumBytes($this, $integer);
110 54
        if ($size <= 3) {
111 42
            $compact = $this->leftShift($this->getLow64($integer), (8 * (3 - $size)));
112 28
        } else {
113 12
            $compact = $this->rightShift($integer, 8 * ($size - 3));
114 12
            $compact = $this->getLow64($compact);
115
        }
116
117 54
        if ($this->cmp($this->bitwiseAnd($compact, gmp_init(0x00800000, 10)), gmp_init(0)) > 0) {
118 3
            $compact = $this->rightShift($compact, 8);
119 3
            $size = $size + 1;
120 2
        }
121
122 54
        $compact = $this->bitwiseOr($compact, $this->leftShift(gmp_init($size, 10), 24));
0 ignored issues
show
Bug introduced by
It seems like $compact defined by $this->bitwiseOr($compac...p_init($size, 10), 24)) on line 122 can also be of type resource; however, BitWasp\Bitcoin\Math\Math::bitwiseOr() does only seem to accept object<GMP>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
123 54
        if ($fNegative && $this->cmp($this->bitwiseAnd($compact, gmp_init(0x007fffff)), gmp_init(0)) > 0) { /// ?
124 6
            $compact = $this->bitwiseOr($compact, gmp_init(0x00800000));
125 4
        }
126
127 54
        return $compact;
128
    }
129
}
130