Failed Conditions
Push — v7 ( a687dc...e264c8 )
by Florent
02:28
created

ModularArithmetic::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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