-
Notifications
You must be signed in to change notification settings - Fork 420
/
PublicKey.php
178 lines (152 loc) · 5.17 KB
/
PublicKey.php
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
<?php
declare(strict_types=1);
namespace BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Key;
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Adapter\EcAdapter;
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Serializer\Key\PublicKeySerializer;
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Signature\Signature;
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\Buffer;
use BitWasp\Buffertools\BufferInterface;
class PublicKey extends Key implements PublicKeyInterface
{
/**
* @var EcAdapter
*/
private $ecAdapter;
/**
* @var bool|false
*/
private $compressed;
/**
* @var resource
*/
private $pubkey_t;
/**
* @param EcAdapter $ecAdapter
* @param resource $secp256k1_pubkey_t
* @param bool|false $compressed
*/
public function __construct(EcAdapter $ecAdapter, $secp256k1_pubkey_t, bool $compressed = false)
{
if (!is_resource($secp256k1_pubkey_t) ||
!get_resource_type($secp256k1_pubkey_t) === SECP256K1_TYPE_PUBKEY) {
throw new \InvalidArgumentException('Secp256k1\Key\PublicKey expects ' . SECP256K1_TYPE_PUBKEY . ' resource');
}
if (false === is_bool($compressed)) {
throw new \InvalidArgumentException('PublicKey: Compressed must be a boolean');
}
$this->ecAdapter = $ecAdapter;
$this->pubkey_t = $secp256k1_pubkey_t;
$this->compressed = $compressed;
}
/**
* @param BufferInterface $msg32
* @param SignatureInterface $signature
* @return bool
*/
public function verify(BufferInterface $msg32, SignatureInterface $signature): bool
{
$ctx = $this->ecAdapter->getContext();
$normalized = null;
secp256k1_ecdsa_signature_normalize($ctx, $normalized, $signature->getResource());
/** @var Signature $signature */
return (bool) secp256k1_ecdsa_verify($ctx, $normalized, $msg32->getBinary(), $this->pubkey_t);
}
/**
* @param PublicKey $other
* @return bool
*/
private function doEquals(PublicKey $other): bool
{
$context = $this->ecAdapter->getContext();
$pubA = '';
$pubB = '';
$flags = $this->compressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED;
if (!(secp256k1_ec_pubkey_serialize($context, $pubA, $this->pubkey_t, $flags) && secp256k1_ec_pubkey_serialize($context, $pubB, $other->pubkey_t, $flags))) {
throw new \RuntimeException('Unable to serialize public key during equals');
}
return hash_equals($pubA, $pubB);
}
/**
* @param PublicKeyInterface $other
* @return bool
*/
public function equals(PublicKeyInterface $other): bool
{
/** @var PublicKey $other */
return $this->doEquals($other);
}
/**
* @return bool|false
*/
public function isCompressed(): bool
{
return $this->compressed;
}
/**
* @return resource
*/
public function getResource()
{
return $this->pubkey_t;
}
/**
* @return resource
* @throws \Exception
*/
private function clonePubkey()
{
$context = $this->ecAdapter->getContext();
$serialized = '';
$flags = $this->compressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED;
if (1 !== secp256k1_ec_pubkey_serialize($context, $serialized, $this->pubkey_t, $flags)) {
throw new \Exception('Secp256k1: pubkey serialize');
}
/** @var resource $clone */
$clone = null;
if (1 !== secp256k1_ec_pubkey_parse($context, $clone, $serialized)) {
throw new \Exception('Secp256k1 pubkey parse');
}
return $clone;
}
/**
* @param \GMP $tweak
* @return KeyInterface
* @throws \Exception
*/
public function tweakAdd(\GMP $tweak): KeyInterface
{
$context = $this->ecAdapter->getContext();
$bin = Buffer::int(gmp_strval($tweak, 10), 32)->getBinary();
$clone = $this->clonePubkey();
if (1 !== secp256k1_ec_pubkey_tweak_add($context, $clone, $bin)) {
throw new \RuntimeException('Secp256k1: tweak add failed.');
}
return new PublicKey($this->ecAdapter, $clone, $this->compressed);
}
/**
* @param \GMP $tweak
* @return KeyInterface
* @throws \Exception
*/
public function tweakMul(\GMP $tweak): KeyInterface
{
$context = $this->ecAdapter->getContext();
$bin = Buffer::int(gmp_strval($tweak, 10), 32)->getBinary();
$clone = $this->clonePubkey();
if (1 !== secp256k1_ec_pubkey_tweak_mul($context, $clone, $bin)) {
throw new \RuntimeException('Secp256k1: tweak mul failed.');
}
return new PublicKey($this->ecAdapter, $clone, $this->compressed);
}
/**
* @return BufferInterface
*/
public function getBuffer(): BufferInterface
{
return (new PublicKeySerializer($this->ecAdapter))->serialize($this);
}
}