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