PublicKey::getGenerator()   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 0
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Mdanter\Ecc\Crypto\Key;
5
6
/**
7
 * *********************************************************************
8
 * Copyright (C) 2012 Matyas Danter
9
 *
10
 * Permission is hereby granted, free of charge, to any person obtaining
11
 * a copy of this software and associated documentation files (the "Software"),
12
 * to deal in the Software without restriction, including without limitation
13
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14
 * and/or sell copies of the Software, and to permit persons to whom the
15
 * Software is furnished to do so, subject to the following conditions:
16
 *
17
 * The above copyright notice and this permission notice shall be included
18
 * in all copies or substantial portions of the Software.
19
 *
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
24
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26
 * OTHER DEALINGS IN THE SOFTWARE.
27
 * ***********************************************************************
28
 */
29
30
use Mdanter\Ecc\Exception\PublicKeyException;
31
use Mdanter\Ecc\Math\GmpMathInterface;
32
use Mdanter\Ecc\Primitives\CurveFpInterface;
33
use Mdanter\Ecc\Primitives\GeneratorPoint;
34
use Mdanter\Ecc\Primitives\PointInterface;
35
36
/**
37
 * This class serves as public- private key exchange for signature verification
38
 */
39
class PublicKey implements PublicKeyInterface
40
{
41
    /**
42
     *
43
     * @var CurveFpInterface
44
     */
45
    private $curve;
46
47
    /**
48
     *
49
     * @var GeneratorPoint
50
     */
51
    private $generator;
52
53
    /**
54
     *
55
     * @var PointInterface
56
     */
57
    private $point;
58
59
    /**
60
     *
61
     * @var GmpMathInterface
62
     */
63
    private $adapter;
64
65
    /**
66
     * Initialize a new PublicKey instance.
67
     *
68
     * @param  GmpMathInterface  $adapter
69
     * @param  GeneratorPoint    $generator
70
     * @param  PointInterface    $point
71
     */
72
    public function __construct(GmpMathInterface $adapter, GeneratorPoint $generator, PointInterface $point)
73
    {
74
        $this->curve = $generator->getCurve();
75
        $this->generator = $generator;
76
        $this->point = $point;
77
        $this->adapter = $adapter;
78
79
        // step 1: not point at infinity.
80
        if ($point->isInfinity()) {
81
            throw new PublicKeyException($generator, $point, "Cannot use point at infinity for public key");
82
        }
83
84
        // step 2 full & partial public key validation routine
85
        if ($adapter->cmp($point->getX(), gmp_init(0, 10)) < 0 || $adapter->cmp($this->curve->getPrime(), $point->getX()) < 0
0 ignored issues
show
Bug introduced by
It seems like gmp_init(0, 10) 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

85
        if ($adapter->cmp($point->getX(), /** @scrutinizer ignore-type */ gmp_init(0, 10)) < 0 || $adapter->cmp($this->curve->getPrime(), $point->getX()) < 0
Loading history...
86
            || $adapter->cmp($point->getY(), gmp_init(0, 10)) < 0 || $adapter->cmp($this->curve->getPrime(), $point->getY()) < 0
87
        ) {
88
            throw new PublicKeyException($generator, $point, "Point has x and y out of range.");
89
        }
90
91
        // Sanity check. Point (x,y) values are qualified against it's
92
        // generator and curve. Here we ensure the Point and Generator
93
        // are the same.
94
        if (!$generator->getCurve()->equals($point->getCurve())) {
95
            throw new PublicKeyException($generator, $point, "Curve for given point not in common with GeneratorPoint");
96
        }
97
    }
98
99
    /**
100
     * {@inheritDoc}
101
     * @see \Mdanter\Ecc\Crypto\Key\PublicKeyInterface::getCurve()
102
     */
103
    public function getCurve(): CurveFpInterface
104
    {
105
        return $this->curve;
106
    }
107
108
    /**
109
     * {$inheritDoc}
110
     * @see \Mdanter\Ecc\Crypto\Key\PublicKeyInterface::getGenerator()
111
     */
112
    public function getGenerator(): GeneratorPoint
113
    {
114
        return $this->generator;
115
    }
116
117
    /**
118
     * {@inheritDoc}
119
     * @see \Mdanter\Ecc\Crypto\Key\PublicKeyInterface::getPoint()
120
     */
121
    public function getPoint(): PointInterface
122
    {
123
        return $this->point;
124
    }
125
}
126