-
Notifications
You must be signed in to change notification settings - Fork 446
Expand file tree
/
Copy pathPrivateKey.php
More file actions
204 lines (178 loc) · 6.17 KB
/
PrivateKey.php
File metadata and controls
204 lines (178 loc) · 6.17 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
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
declare(strict_types=1);
namespace BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Key;
use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Adapter\EcAdapter;
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Serializer\Key\PrivateKeySerializer;
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Signature\CompactSignature;
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Signature\SchnorrSigner;
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\XOnlyPublicKeyInterface;
use BitWasp\Bitcoin\Crypto\EcAdapter\Signature\CompactSignatureInterface;
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Signature\Signature;
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\Key;
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\KeyInterface;
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PrivateKeyInterface;
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
use BitWasp\Bitcoin\Crypto\EcAdapter\Signature\SchnorrSignatureInterface;
use BitWasp\Bitcoin\Crypto\EcAdapter\Signature\SignatureInterface;
use BitWasp\Bitcoin\Crypto\Random\RbgInterface;
use BitWasp\Bitcoin\Crypto\Random\Rfc6979;
use BitWasp\Bitcoin\Exceptions\InvalidPrivateKey;
use BitWasp\Bitcoin\Network\NetworkInterface;
use BitWasp\Bitcoin\Serializer\Key\PrivateKey\WifPrivateKeySerializer;
use BitWasp\Buffertools\Buffer;
use BitWasp\Buffertools\BufferInterface;
use Mdanter\Ecc\Crypto\Signature\Signer;
class PrivateKey extends Key implements PrivateKeyInterface
{
/**
* @var \GMP
*/
private $secretMultiplier;
/**
* @var bool
*/
private $compressed;
/**
* @var PublicKey
*/
private $publicKey;
/**
* @var EcAdapter
*/
private $ecAdapter;
/**
* @param EcAdapter $ecAdapter
* @param \GMP $int
* @param bool $compressed
* @throws InvalidPrivateKey
*/
public function __construct(EcAdapter $ecAdapter, \GMP $int, bool $compressed = false)
{
if (false === $ecAdapter->validatePrivateKey(Buffer::int(gmp_strval($int, 10), 32))) {
throw new InvalidPrivateKey('Invalid private key - must be less than curve order.');
}
$this->ecAdapter = $ecAdapter;
$this->secretMultiplier = $int;
$this->compressed = $compressed;
}
/**
* @return \GMP
*/
public function getSecret(): \GMP
{
return $this->secretMultiplier;
}
/**
* @param BufferInterface $msg32
* @param RbgInterface|null $rbg
* @return Signature
*/
public function sign(BufferInterface $msg32, RbgInterface $rbg = null): SignatureInterface
{
$rbg = $rbg ?: new Rfc6979($this->ecAdapter, $this, $msg32);
$randomK = gmp_init($rbg->bytes(32)->getHex(), 16);
$hash = gmp_init($msg32->getHex(), 16);
$math = $this->ecAdapter->getMath();
$signer = new Signer($math);
$signature = $signer->sign($this->ecAdapter->getGenerator()->getPrivateKeyFrom($this->secretMultiplier), $hash, $randomK);
$s = $signature->getS();
// if s is less than half the curve order, invert s
if (!$this->ecAdapter->validateSignatureElement($s, true)) {
$s = $math->sub($this->ecAdapter->getOrder(), $s);
}
return new Signature($this->ecAdapter, $signature->getR(), $s);
}
/**
* @param BufferInterface $msg32
* @param RbgInterface|null $rbg
* @return CompactSignatureInterface
* @throws \Exception
*/
public function signCompact(BufferInterface $msg32, RbgInterface $rbg = null): CompactSignatureInterface
{
$sign = $this->sign($msg32, $rbg);
// calculate the recovery param
// there should be a way to get this when signing too, but idk how ...
return new CompactSignature(
$this->ecAdapter,
$sign->getR(),
$sign->getS(),
$this->ecAdapter->calcPubKeyRecoveryParam($sign->getR(), $sign->getS(), $msg32, $this->getPublicKey()),
$this->isCompressed()
);
}
public function signSchnorr(BufferInterface $msg32): SchnorrSignatureInterface
{
$schnorr = new SchnorrSigner($this->ecAdapter);
return $schnorr->sign($this, $msg32);
}
/**
* @param \GMP $tweak
* @return KeyInterface
*/
public function tweakAdd(\GMP $tweak): KeyInterface
{
$adapter = $this->ecAdapter;
$modMath = $adapter->getMath()->getModularArithmetic($adapter->getGenerator()->getOrder());
return $adapter->getPrivateKey($modMath->add($tweak, $this->getSecret()), $this->compressed);
}
/**
* @param \GMP $tweak
* @return KeyInterface
*/
public function tweakMul(\GMP $tweak): KeyInterface
{
$adapter = $this->ecAdapter;
$modMath = $adapter->getMath()->getModularArithmetic($adapter->getGenerator()->getOrder());
return $adapter->getPrivateKey($modMath->mul($tweak, $this->getSecret()), $this->compressed);
}
/**
* {@inheritDoc}
*/
public function isCompressed(): bool
{
return $this->compressed;
}
/**
* Return the public key
*
* @return PublicKey
*/
public function getPublicKey(): PublicKeyInterface
{
if (null === $this->publicKey) {
$point = $this->ecAdapter->getGenerator()->mul($this->secretMultiplier);
$this->publicKey = new PublicKey($this->ecAdapter, $point, $this->compressed);
}
return $this->publicKey;
}
/**
* Return the public key
*
* @return XOnlyPublicKeyInterface
*/
public function getXOnlyPublicKey(): XOnlyPublicKeyInterface
{
return $this->getPublicKey()->asXOnlyPublicKey();
}
/**
* @param NetworkInterface $network
* @return string
*/
public function toWif(NetworkInterface $network = null): string
{
$network = $network ?: Bitcoin::getNetwork();
$serializer = new WifPrivateKeySerializer(
new PrivateKeySerializer($this->ecAdapter)
);
return $serializer->serialize($network, $this);
}
/**
* @return BufferInterface
*/
public function getBuffer(): BufferInterface
{
return (new PrivateKeySerializer($this->ecAdapter))->serialize($this);
}
}