ModularArithmetic::pow()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Mdanter\Ecc\Math;
4
5
class ModularArithmetic
6
{
7
    /**
8
     * @var GmpMathInterface
9
     */
10
    private $adapter;
11
12
    /**
13
     * @var \GMP
14
     */
15
    private $modulus;
16
17
    /**
18
     * @param GmpMathInterface $adapter
19
     * @param \GMP $modulus
20
     */
21
    public function __construct(GmpMathInterface $adapter, \GMP $modulus)
22
    {
23
        $this->adapter = $adapter;
24
        $this->modulus = $modulus;
25
    }
26
27
    /**
28
     * @param \GMP $augend
29
     * @param \GMP $addend
30
     * @return \GMP
31
     */
32
    public function add(\GMP $augend, \GMP $addend): \GMP
33
    {
34
        return $this->adapter->mod($this->adapter->add($augend, $addend), $this->modulus);
35
    }
36
37
    /**
38
     * @param \GMP $minuend
39
     * @param \GMP $subtrahend
40
     * @return \GMP
41
     */
42
    public function sub(\GMP $minuend, \GMP $subtrahend): \GMP
43
    {
44
        return $this->adapter->mod($this->adapter->sub($minuend, $subtrahend), $this->modulus);
45
    }
46
47
    /**
48
     * @param \GMP $multiplier
49
     * @param \GMP $muliplicand
50
     * @return \GMP
51
     */
52
    public function mul(\GMP $multiplier, \GMP $muliplicand): \GMP
53
    {
54
        return $this->adapter->mod($this->adapter->mul($multiplier, $muliplicand), $this->modulus);
55
    }
56
57
    /**
58
     * @param \GMP $dividend
59
     * @param \GMP $divisor
60
     * @return \GMP
61
     */
62
    public function div(\GMP $dividend, \GMP $divisor): \GMP
63
    {
64
        return $this->mul($dividend, $this->adapter->inverseMod($divisor, $this->modulus));
65
    }
66
67
    /**
68
     * @param \GMP $base
69
     * @param \GMP $exponent
70
     * @return \GMP
71
     */
72
    public function pow(\GMP $base, \GMP $exponent): \GMP
73
    {
74
        return $this->adapter->powmod($base, $exponent, $this->modulus);
75
    }
76
}
77