1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Key; |
4
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Adapter\EcAdapter; |
6
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Serializer\Key\PublicKeySerializer; |
7
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\Key; |
8
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface; |
9
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Signature\SignatureInterface; |
10
|
|
|
use BitWasp\Buffertools\BufferInterface; |
11
|
|
|
|
12
|
|
|
class PublicKey extends Key implements PublicKeyInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var EcAdapter |
16
|
|
|
*/ |
17
|
|
|
private $ecAdapter; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var bool|false |
21
|
|
|
*/ |
22
|
|
|
private $compressed; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var resource |
26
|
|
|
*/ |
27
|
|
|
private $pubkey_t; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param EcAdapter $ecAdapter |
31
|
|
|
* @param resource $secp256k1_pubkey_t |
32
|
|
|
* @param bool|false $compressed |
33
|
|
|
*/ |
34
|
372 |
|
public function __construct(EcAdapter $ecAdapter, $secp256k1_pubkey_t, $compressed = false) |
35
|
|
|
{ |
36
|
372 |
|
if (!is_resource($secp256k1_pubkey_t) || |
37
|
372 |
|
!get_resource_type($secp256k1_pubkey_t) === SECP256K1_TYPE_PUBKEY) { |
38
|
|
|
throw new \InvalidArgumentException('Secp256k1\Key\PublicKey expects ' . SECP256K1_TYPE_PUBKEY . ' resource'); |
39
|
|
|
} |
40
|
|
|
|
41
|
372 |
|
if (false === is_bool($compressed)) { |
42
|
|
|
throw new \InvalidArgumentException('PublicKey: Compressed must be a boolean'); |
43
|
|
|
} |
44
|
|
|
|
45
|
372 |
|
$this->ecAdapter = $ecAdapter; |
46
|
372 |
|
$this->pubkey_t = $secp256k1_pubkey_t; |
47
|
372 |
|
$this->compressed = $compressed; |
48
|
372 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param BufferInterface $msg32 |
52
|
|
|
* @param SignatureInterface $signature |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
|
|
public function verify(BufferInterface $msg32, SignatureInterface $signature) |
56
|
|
|
{ |
57
|
|
|
return $this->ecAdapter->verify($msg32, $this, $signature); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return bool|false |
62
|
|
|
*/ |
63
|
351 |
|
public function isCompressed() |
64
|
|
|
{ |
65
|
351 |
|
return $this->compressed; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return resource |
70
|
|
|
*/ |
71
|
303 |
|
public function getResource() |
72
|
|
|
{ |
73
|
303 |
|
return $this->pubkey_t; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return resource |
78
|
|
|
* @throws \Exception |
79
|
|
|
*/ |
80
|
9 |
|
private function clonePubkey() |
81
|
|
|
{ |
82
|
9 |
|
$context = $this->ecAdapter->getContext(); |
83
|
9 |
|
$serialized = ''; |
84
|
9 |
|
if (1 !== secp256k1_ec_pubkey_serialize($context, $serialized, $this->pubkey_t, $this->compressed)) { |
85
|
|
|
throw new \Exception('Secp256k1: pubkey serialize'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** @var resource $clone */ |
89
|
9 |
|
$clone = ''; |
90
|
9 |
|
if (1 !== secp256k1_ec_pubkey_parse($context, $clone, $serialized)) { |
91
|
|
|
throw new \Exception('Secp256k1 pubkey parse'); |
92
|
|
|
} |
93
|
|
|
|
94
|
9 |
|
return $clone; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param int $tweak |
99
|
|
|
* @return PublicKey |
100
|
|
|
* @throws \Exception |
101
|
|
|
*/ |
102
|
6 |
|
public function tweakAdd($tweak) |
103
|
|
|
{ |
104
|
6 |
|
$context = $this->ecAdapter->getContext(); |
105
|
6 |
|
$math = $this->ecAdapter->getMath(); |
106
|
6 |
|
$bin = pack('H*', str_pad($math->decHex($tweak), 64, '0', STR_PAD_LEFT)); |
107
|
|
|
|
108
|
6 |
|
$clone = $this->clonePubkey(); |
109
|
6 |
|
if (1 !== secp256k1_ec_pubkey_tweak_add($context, $clone, $bin)) { |
110
|
|
|
throw new \RuntimeException('Secp256k1: tweak add failed.'); |
111
|
|
|
} |
112
|
|
|
|
113
|
6 |
|
return new PublicKey($this->ecAdapter, $clone, $this->compressed); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param int $tweak |
118
|
|
|
* @return PublicKey |
119
|
|
|
* @throws \Exception |
120
|
|
|
*/ |
121
|
3 |
|
public function tweakMul($tweak) |
122
|
|
|
{ |
123
|
3 |
|
$context = $this->ecAdapter->getContext(); |
124
|
3 |
|
$math = $this->ecAdapter->getMath(); |
125
|
3 |
|
$bin = pack('H*', str_pad($math->decHex($tweak), 64, '0', STR_PAD_LEFT)); |
126
|
|
|
|
127
|
3 |
|
$clone = $this->clonePubkey(); |
128
|
3 |
|
if (1 !== secp256k1_ec_pubkey_tweak_mul($context, $clone, $bin)) { |
129
|
|
|
throw new \RuntimeException('Secp256k1: tweak mul failed.'); |
130
|
|
|
} |
131
|
|
|
|
132
|
3 |
|
return new PublicKey($this->ecAdapter, $clone, $this->compressed); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return BufferInterface |
137
|
|
|
*/ |
138
|
294 |
|
public function getBuffer() |
139
|
|
|
{ |
140
|
294 |
|
return (new PublicKeySerializer($this->ecAdapter))->serialize($this); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|