GeneratorPoint::isValid()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 24
rs 8.8333
1
<?php
2
declare(strict_types=1);
3
4
namespace Mdanter\Ecc\Primitives;
5
6
use Mdanter\Ecc\Crypto\Key\PrivateKeyInterface;
7
use Mdanter\Ecc\Crypto\Key\PublicKeyInterface;
8
use Mdanter\Ecc\Math\GmpMathInterface;
9
use Mdanter\Ecc\Crypto\Key\PrivateKey;
10
use Mdanter\Ecc\Crypto\Key\PublicKey;
11
use Mdanter\Ecc\Random\RandomGeneratorFactory;
12
use Mdanter\Ecc\Random\RandomNumberGeneratorInterface;
13
14
/**
15
 * Curve point from which public and private keys can be derived.
16
 */
17
class GeneratorPoint extends Point
18
{
19
    /**
20
     * @var RandomNumberGeneratorInterface
21
     */
22
    private $generator;
23
24
    /**
25
     * @param GmpMathInterface               $adapter
26
     * @param CurveFpInterface               $curve
27
     * @param \GMP                           $x
28
     * @param \GMP                           $y
29
     * @param \GMP                           $order
30
     * @param RandomNumberGeneratorInterface $generator
31
     */
32
    public function __construct(
33
        GmpMathInterface $adapter,
34
        CurveFpInterface $curve,
35
        \GMP $x,
36
        \GMP $y,
37
        \GMP $order,
38
        RandomNumberGeneratorInterface $generator = null
39
    ) {
40
        $this->generator = $generator ?: RandomGeneratorFactory::getRandomGenerator();
41
        parent::__construct($adapter, $curve, $x, $y, $order);
42
    }
43
44
    /**
45
     * Verifies validity of given coordinates against the current point and its point.
46
     *
47
     * @todo   Check if really necessary here (only used for testing in lib)
48
     * @param  \GMP $x
49
     * @param  \GMP $y
50
     * @return bool
51
     */
52
    public function isValid(\GMP $x, \GMP $y): bool
53
    {
54
       
55
        $math = $this->getAdapter();
56
57
        $n = $this->getOrder();
58
        $zero = gmp_init(0, 10);
59
        $curve = $this->getCurve();
60
61
        if ($math->cmp($x, $zero) < 0 || $math->cmp($n, $x) <= 0 || $math->cmp($y, $zero) < 0 || $math->cmp($n, $y) <= 0) {
0 ignored issues
show
Bug introduced by
It seems like $zero can also be of type resource; however, parameter $other of Mdanter\Ecc\Math\GmpMathInterface::cmp() does only seem to accept GMP, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
        if ($math->cmp($x, /** @scrutinizer ignore-type */ $zero) < 0 || $math->cmp($n, $x) <= 0 || $math->cmp($y, $zero) < 0 || $math->cmp($n, $y) <= 0) {
Loading history...
62
            return false;
63
        }
64
65
        if (! $curve->contains($x, $y)) {
66
            return false;
67
        }
68
69
        $point = $curve->getPoint($x, $y)->mul($n);
70
71
        if (! $point->isInfinity()) {
72
            return false;
73
        }
74
75
        return true;
76
    }
77
78
    /**
79
     * @return PrivateKeyInterface
80
     */
81
    public function createPrivateKey(): PrivateKeyInterface
82
    {
83
        $secret = $this->generator->generate($this->getOrder());
84
85
        return new PrivateKey($this->getAdapter(), $this, $secret);
86
    }
87
88
    /**
89
     * @param \GMP $x
90
     * @param \GMP $y
91
     * @return PublicKeyInterface
92
     */
93
    public function getPublicKeyFrom(\GMP $x, \GMP $y): PublicKeyInterface
94
    {
95
        $pubPoint = $this->getCurve()->getPoint($x, $y, $this->getOrder());
96
        return new PublicKey($this->getAdapter(), $this, $pubPoint);
97
    }
98
99
    /**
100
     * @param \GMP $secretMultiplier
101
     * @return PrivateKeyInterface
102
     */
103
    public function getPrivateKeyFrom(\GMP $secretMultiplier): PrivateKeyInterface
104
    {
105
        return new PrivateKey($this->getAdapter(), $this, $secretMultiplier);
106
    }
107
}
108