Failed Conditions
Push — v7 ( 896db9...bffd87 )
by Florent
03:38
created

CurveFp::recoverYfromX()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 2
eloc 14
nc 2
nop 2
1
<?php
2
3
/***********************************************************************
4
Copyright (C) 2012 Matyas Danter
5
6
Permission is hereby granted, free of charge, to any person obtaining
7
a copy of this software and associated documentation files (the "Software"),
8
to deal in the Software without restriction, including without limitation
9
the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
and/or sell copies of the Software, and to permit persons to whom the
11
Software is furnished to do so, subject to the following conditions:
12
13
The above copyright notice and this permission notice shall be included
14
in all copies or substantial portions of the Software.
15
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
20
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
OTHER DEALINGS IN THE SOFTWARE.
23
 *************************************************************************/
24
25
namespace Jose\Component\Core\Util\Ecc\Primitives;
26
27
use Jose\Component\Core\Util\Ecc\Math\GmpMath;
28
use Jose\Component\Core\Util\Ecc\Math\ModularArithmetic;
29
30
/**
31
 * This class is a representation of an EC over a field modulo a prime number
32
 *
33
 * Important objectives for this class are:
34
 * - Does the curve contain a point?
35
 * - Comparison of two curves.
36
 */
37
final class CurveFp
38
{
39
40
    /**
41
     * @var CurveParameters
42
     */
43
    private $parameters;
44
45
    /**
46
     *
47
     * @var GmpMath
48
     */
49
    private $adapter;
50
51
    /**
52
     *
53
     * @var ModularArithmetic
54
     */
55
    private $modAdapter;
56
57
    /**
58
     * Constructor that sets up the instance variables.
59
     *
60
     * @param CurveParameters $parameters
61
     */
62
    public function __construct(CurveParameters $parameters)
63
    {
64
        $this->parameters = $parameters;
65
        $this->adapter = new GmpMath();
66
        $this->modAdapter = new ModularArithmetic($this->parameters->getPrime());
67
    }
68
69
    /**
70
     * @return ModularArithmetic
71
     */
72
    public function getModAdapter(): ModularArithmetic
73
    {
74
        return $this->modAdapter;
75
    }
76
77
    /**
78
     * @param \GMP $x
79
     * @param \GMP $y
80
     * @param \GMP|null $order
81
     *
82
     * @return Point
83
     */
84
    public function getPoint(\GMP $x, \GMP $y, ?\GMP $order = null): Point
85
    {
86
        return new Point($this, $x, $y, $order);
87
    }
88
89
    /**
90
     * @return Point
91
     */
92
    public function getInfinity(): Point
93
    {
94
        return new Point($this, gmp_init(0, 10), gmp_init(0, 10), null, true);
95
    }
96
97
    /**
98
     * @param \GMP $x
99
     * @param \GMP $y
100
     * @param \GMP $order
101
     *
102
     * @return GeneratorPoint
103
     */
104
    public function getGenerator(\GMP $x, \GMP $y, \GMP $order): GeneratorPoint
105
    {
106
        return new GeneratorPoint($this, $x, $y, $order);
107
    }
108
109
    /**
110
     * @param \GMP $x
111
     * @param \GMP $y
112
     * @return bool
113
     */
114
    public function contains(\GMP $x, \GMP $y): bool
115
    {
116
        $math = $this->adapter;
117
118
        $eq_zero = $math->equals(
119
            $this->modAdapter->sub(
120
                $math->pow($y, 2),
121
                $math->add(
122
                    $math->add(
123
                        $math->pow($x, 3),
124
                        $math->mul($this->getA(), $x)
125
                    ),
126
                    $this->getB()
127
                )
128
            ),
129
            gmp_init(0, 10)
130
        );
131
132
        return $eq_zero;
133
    }
134
135
    /**
136
     * @return \GMP
137
     */
138
    public function getA(): \GMP
139
    {
140
        return $this->parameters->getA();
141
    }
142
143
    /**
144
     * @return \GMP
145
     */
146
    public function getB(): \GMP
147
    {
148
        return $this->parameters->getB();
149
    }
150
151
    /**
152
     * @return \GMP
153
     */
154
    public function getPrime(): \GMP
155
    {
156
        return $this->parameters->getPrime();
157
    }
158
159
    /**
160
     * @return int
161
     */
162
    public function getSize(): int
163
    {
164
        return $this->parameters->getSize();
165
    }
166
167
    /**
168
     * @param CurveFp $other
169
     *
170
     * @return int
171
     */
172
    public function cmp(CurveFp $other): int
173
    {
174
        $math = $this->adapter;
175
176
        $equal  = $math->equals($this->getA(), $other->getA());
177
        $equal &= $math->equals($this->getB(), $other->getB());
178
        $equal &= $math->equals($this->getPrime(), $other->getPrime());
179
180
        return $equal ? 0 : 1;
181
    }
182
183
    /**
184
     * @param CurveFp $other
185
     *
186
     * @return bool
187
     */
188
    public function equals(CurveFp $other): bool
189
    {
190
        return $this->cmp($other) === 0;
191
    }
192
}
193