1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jose\Component\Core\Util\Ecc\Primitives; |
4
|
|
|
|
5
|
|
|
use Jose\Component\Core\Util\Ecc\Math\GmpMath; |
6
|
|
|
use Jose\Component\Core\Util\Ecc\Crypto\Key\PrivateKey; |
7
|
|
|
use Jose\Component\Core\Util\Ecc\Crypto\Key\PublicKey; |
8
|
|
|
use Jose\Component\Core\Util\Ecc\Random\RandomGeneratorFactory; |
9
|
|
|
use Jose\Component\Core\Util\Ecc\Random\RandomNumberGenerator; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Curve point from which public and private keys can be derived. |
13
|
|
|
*/ |
14
|
|
|
final class GeneratorPoint extends Point |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \Jose\Component\Core\Util\Ecc\Random\DebugDecorator|RandomNumberGenerator|null |
18
|
|
|
*/ |
19
|
|
|
private $generator; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param GmpMath $adapter |
23
|
|
|
* @param CurveFp $curve |
24
|
|
|
* @param \GMP $x |
25
|
|
|
* @param \GMP $y |
26
|
|
|
* @param \GMP $order |
27
|
|
|
* @param RandomNumberGenerator $generator |
28
|
|
|
*/ |
29
|
|
|
public function __construct( |
30
|
|
|
GmpMath $adapter, |
31
|
|
|
CurveFp $curve, |
32
|
|
|
\GMP $x, |
33
|
|
|
\GMP $y, |
34
|
|
|
\GMP $order, |
35
|
|
|
RandomNumberGenerator $generator = null |
36
|
|
|
) { |
37
|
|
|
$this->generator = $generator ?: RandomGeneratorFactory::getRandomGenerator(); |
38
|
|
|
parent::__construct($adapter, $curve, $x, $y, $order); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Verifies validity of given coordinates against the current point and its point. |
43
|
|
|
* |
44
|
|
|
* @todo Check if really necessary here (only used for testing in lib) |
45
|
|
|
* @param \GMP $x |
46
|
|
|
* @param \GMP $y |
47
|
|
|
* @return boolean |
48
|
|
|
*/ |
49
|
|
|
public function isValid(\GMP $x, \GMP $y) |
50
|
|
|
{ |
51
|
|
|
|
52
|
|
|
$math = $this->getAdapter(); |
53
|
|
|
|
54
|
|
|
$n = $this->getOrder(); |
55
|
|
|
$zero = gmp_init(0, 10); |
56
|
|
|
$curve = $this->getCurve(); |
57
|
|
|
|
58
|
|
|
if ($math->cmp($x, $zero) < 0 || $math->cmp($n, $x) <= 0 || $math->cmp($y, $zero) < 0 || $math->cmp($n, $y) <= 0) { |
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (! $curve->contains($x, $y)) { |
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$point = $curve->getPoint($x, $y)->mul($n); |
67
|
|
|
|
68
|
|
|
if (! $point->isInfinity()) { |
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return PrivateKey |
77
|
|
|
*/ |
78
|
|
|
public function createPrivateKey() |
79
|
|
|
{ |
80
|
|
|
$secret = $this->generator->generate($this->getOrder()); |
81
|
|
|
|
82
|
|
|
return new PrivateKey($this->getAdapter(), $this, $secret); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param \GMP $x |
87
|
|
|
* @param \GMP $y |
88
|
|
|
* @param \GMP $order |
89
|
|
|
* @return PublicKey |
90
|
|
|
*/ |
91
|
|
|
public function getPublicKeyFrom(\GMP $x, \GMP $y, \GMP $order = null) |
92
|
|
|
{ |
93
|
|
|
$pubPoint = $this->getCurve()->getPoint($x, $y, $order); |
94
|
|
|
return new PublicKey($this->getAdapter(), $this, $pubPoint); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param \GMP $secretMultiplier |
99
|
|
|
* @return PrivateKey |
100
|
|
|
*/ |
101
|
|
|
public function getPrivateKeyFrom(\GMP $secretMultiplier) |
102
|
|
|
{ |
103
|
|
|
return new PrivateKey($this->getAdapter(), $this, $secretMultiplier); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|