forked from Bit-Wasp/bitcoin-php
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathPublicKey.php
More file actions
191 lines (169 loc) · 4.75 KB
/
PublicKey.php
File metadata and controls
191 lines (169 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
declare(strict_types=1);
namespace BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Key;
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Adapter\EcAdapter;
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Serializer\Key\PublicKeySerializer;
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\Key;
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\KeyInterface;
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
use BitWasp\Bitcoin\Crypto\EcAdapter\Signature\SignatureInterface;
use BitWasp\Buffertools\BufferInterface;
use Mdanter\Ecc\Crypto\Signature\Signer;
use Mdanter\Ecc\Primitives\CurveFpInterface;
use Mdanter\Ecc\Primitives\GeneratorPoint;
use Mdanter\Ecc\Primitives\PointInterface;
class PublicKey extends Key implements PublicKeyInterface, \Mdanter\Ecc\Crypto\Key\PublicKeyInterface
{
/**
* @var EcAdapter
*/
private $ecAdapter;
/**
* @var PointInterface
*/
private $point;
/**
* @var string
*/
private $prefix;
/**
* @var bool
*/
private $compressed;
/**
* PublicKey constructor.
* @param EcAdapter $ecAdapter
* @param PointInterface $point
* @param bool $compressed
* @param string $prefix
*/
public function __construct(
EcAdapter $ecAdapter,
PointInterface $point,
bool $compressed = false,
?string $prefix = null
) {
$this->ecAdapter = $ecAdapter;
$this->point = $point;
$this->prefix = $prefix;
$this->compressed = $compressed;
}
/**
* @return GeneratorPoint
*/
public function getGenerator(): GeneratorPoint
{
return $this->ecAdapter->getGenerator();
}
/**
* @return \Mdanter\Ecc\Primitives\CurveFpInterface
*/
public function getCurve(): CurveFpInterface
{
return $this->ecAdapter->getGenerator()->getCurve();
}
/**
* @return string
*/
public function getPrefix()
{
return $this->prefix;
}
/**
* @return PointInterface
*/
public function getPoint(): PointInterface
{
return $this->point;
}
/**
* @param BufferInterface $msg32
* @param SignatureInterface $signature
* @return bool
*/
public function verify(BufferInterface $msg32, SignatureInterface $signature): bool
{
$hash = gmp_init($msg32->getHex(), 16);
$signer = new Signer($this->ecAdapter->getMath());
return $signer->verify($this, $signature, $hash);
}
/**
* @param \GMP $tweak
* @return KeyInterface
*/
public function tweakAdd(\GMP $tweak): KeyInterface
{
$offset = $this->ecAdapter->getGenerator()->mul($tweak);
$newPoint = $this->point->add($offset);
return new PublicKey($this->ecAdapter, $newPoint, $this->compressed);
}
/**
* @param \GMP $tweak
* @return KeyInterface
*/
public function tweakMul(\GMP $tweak): KeyInterface
{
$point = $this->point->mul($tweak);
return new PublicKey($this->ecAdapter, $point, $this->compressed);
}
/**
* @param BufferInterface $publicKey
* @return bool
*/
public static function isCompressedOrUncompressed(BufferInterface $publicKey): bool
{
$vchPubKey = $publicKey->getBinary();
if ($publicKey->getSize() < self::LENGTH_COMPRESSED) {
return false;
}
if ($vchPubKey[0] === self::KEY_UNCOMPRESSED) {
if ($publicKey->getSize() !== self::LENGTH_UNCOMPRESSED) {
// Invalid length for uncompressed key
return false;
}
} elseif (in_array($vchPubKey[0], [
self::KEY_COMPRESSED_EVEN,
self::KEY_COMPRESSED_ODD
])) {
if ($publicKey->getSize() !== self::LENGTH_COMPRESSED) {
return false;
}
} else {
return false;
}
return true;
}
/**
* @return bool
*/
public function isCompressed(): bool
{
return $this->compressed;
}
/**
* @param PublicKey $other
* @return bool
*/
private function doEquals(PublicKey $other): bool
{
return $this->compressed === $other->compressed
&& $this->point->equals($other->point)
&& (($this->prefix === null || $other->prefix === null) || ($this->prefix === $other->prefix));
}
/**
* @param PublicKeyInterface $other
* @return bool
*/
public function equals(PublicKeyInterface $other): bool
{
/** @var self $other */
return $this->doEquals($other);
}
/**
* @return BufferInterface
*/
public function getBuffer(): BufferInterface
{
return (new PublicKeySerializer($this->ecAdapter))->serialize($this);
}
}