summaryrefslogtreecommitdiffstats
path: root/vendor/fgrosse/phpasn1/lib/Utility/BigIntegerGmp.php
blob: 899f5f7a480da998d134a20e96df19b6e281cd9c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/*
 * This file is part of the PHPASN1 library.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace FG\Utility;

/**
 * Class BigIntegerGmp
 * Integer representation of big numbers using the GMP extension to perform operations.
 * @package FG\Utility
 * @internal
 */
class BigIntegerGmp extends BigInteger
{
    /**
     * Resource handle.
     * @var \GMP
     */
    protected $_rh;

    public function __clone()
    {
        $this->_rh = gmp_add($this->_rh, 0);
    }

    protected function _fromString($str)
    {
        $this->_rh = gmp_init($str, 10);
    }

    protected function _fromInteger($integer)
    {
        $this->_rh = gmp_init($integer, 10);
    }

    public function __toString()
    {
        return gmp_strval($this->_rh, 10);
    }

    public function toInteger()
    {
        if ($this->compare(PHP_INT_MAX) > 0 || $this->compare(PHP_INT_MIN) < 0) {
            throw new \OverflowException(sprintf('Can not represent %s as integer.', $this));
        }
        return gmp_intval($this->_rh);
    }

    public function isNegative()
    {
        return gmp_sign($this->_rh) === -1;
    }

    protected function _unwrap($number)
    {
        if ($number instanceof self) {
            return $number->_rh;
        }
        return $number;
    }

    public function compare($number)
    {
        return gmp_cmp($this->_rh, $this->_unwrap($number));
    }

    public function add($b)
    {
        $ret = new self();
        $ret->_rh = gmp_add($this->_rh, $this->_unwrap($b));
        return $ret;
    }

    public function subtract($b)
    {
        $ret = new self();
        $ret->_rh = gmp_sub($this->_rh, $this->_unwrap($b));
        return $ret;
    }

    public function multiply($b)
    {
        $ret = new self();
        $ret->_rh = gmp_mul($this->_rh, $this->_unwrap($b));
        return $ret;
    }

    public function modulus($b)
    {
        $ret = new self();
        $ret->_rh = gmp_mod($this->_rh, $this->_unwrap($b));
        return $ret;
    }

    public function toPower($b)
    {
        if ($b instanceof self) {
            // gmp_pow accepts just an integer
            if ($b->compare(PHP_INT_MAX) > 0) {
                throw new \UnexpectedValueException('Unable to raise to power greater than PHP_INT_MAX.');
            }
            $b = gmp_intval($b->_rh);
        }
        $ret = new self();
        $ret->_rh = gmp_pow($this->_rh, $b);
        return $ret;
    }

    public function shiftRight($bits=8)
    {
        $ret = new self();
        $ret->_rh = gmp_div($this->_rh, gmp_pow(2, $bits));
        return $ret;
    }

    public function shiftLeft($bits=8)
    {
        $ret = new self();
        $ret->_rh = gmp_mul($this->_rh, gmp_pow(2, $bits));
        return $ret;
    }

    public function absoluteValue()
    {
        $ret = new self();
        $ret->_rh = gmp_abs($this->_rh);
        return $ret;
    }
}