1 | <?php |
||
18 | class PrivateKey extends Key implements PrivateKeyInterface |
||
19 | { |
||
20 | /** |
||
21 | * @var \GMP |
||
22 | */ |
||
23 | private $secret; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $secretBin; |
||
29 | |||
30 | /** |
||
31 | * @var bool |
||
32 | */ |
||
33 | private $compressed; |
||
34 | |||
35 | /** |
||
36 | * @var PublicKey |
||
37 | */ |
||
38 | private $publicKey; |
||
39 | |||
40 | /** |
||
41 | * @var EcAdapter |
||
42 | */ |
||
43 | private $ecAdapter; |
||
44 | |||
45 | /** |
||
46 | * @param EcAdapter $adapter |
||
47 | * @param \GMP $secret |
||
48 | * @param bool|false $compressed |
||
49 | * @throws \Exception |
||
50 | */ |
||
51 | 196 | public function __construct(EcAdapter $adapter, \GMP $secret, $compressed = false) |
|
52 | { |
||
53 | 196 | $buffer = Buffer::int(gmp_strval($secret, 10), 32, $adapter->getMath()); |
|
54 | 196 | if (!$adapter->validatePrivateKey($buffer)) { |
|
55 | 4 | throw new InvalidPrivateKey('Invalid private key'); |
|
56 | } |
||
57 | |||
58 | 192 | if (false === is_bool($compressed)) { |
|
59 | throw new \InvalidArgumentException('PrivateKey: Compressed argument must be a boolean'); |
||
60 | } |
||
61 | |||
62 | 192 | $this->ecAdapter = $adapter; |
|
63 | 192 | $this->secret = $secret; |
|
64 | 192 | $this->secretBin = $buffer->getBinary(); |
|
65 | 192 | $this->compressed = $compressed; |
|
66 | 192 | } |
|
67 | |||
68 | /** |
||
69 | * @param BufferInterface $msg32 |
||
70 | * @param RbgInterface|null $rbgInterface |
||
71 | * @return Signature |
||
72 | */ |
||
73 | public function sign(BufferInterface $msg32, RbgInterface $rbgInterface = null) |
||
77 | |||
78 | /** |
||
79 | * @return bool|false |
||
80 | */ |
||
81 | 128 | public function isCompressed() |
|
85 | |||
86 | /** |
||
87 | * @return int|string |
||
88 | */ |
||
89 | 112 | public function getSecret() |
|
90 | { |
||
91 | 112 | return $this->secret; |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * @return string |
||
96 | */ |
||
97 | 228 | public function getSecretBinary() |
|
101 | |||
102 | /** |
||
103 | * @return PublicKey |
||
104 | */ |
||
105 | 204 | public function getPublicKey() |
|
120 | |||
121 | /** |
||
122 | * @param \GMP $tweak |
||
123 | * @return PrivateKey |
||
124 | */ |
||
125 | 28 | public function tweakAdd(\GMP $tweak) |
|
126 | { |
||
127 | 28 | $adapter = $this->ecAdapter; |
|
128 | 28 | $math = $adapter->getMath(); |
|
129 | 28 | $context = $adapter->getContext(); |
|
130 | 28 | $privateKey = $this->getBinary(); // mod by reference |
|
131 | 28 | $tweak = Buffer::int($math->toString($tweak), 32, $math)->getBinary(); |
|
132 | 28 | $ret = \secp256k1_ec_privkey_tweak_add( |
|
133 | 14 | $context, |
|
134 | $privateKey, |
||
135 | $tweak |
||
136 | 14 | ); |
|
137 | |||
138 | 28 | if ($ret !== 1) { |
|
139 | throw new \RuntimeException('Secp256k1 privkey tweak add: failed'); |
||
140 | } |
||
141 | |||
142 | 28 | $secret = new Buffer($privateKey); |
|
143 | 28 | return $adapter->getPrivateKey($secret->getGmp(), $this->compressed); |
|
144 | } |
||
145 | |||
146 | /** |
||
147 | * @param \GMP $tweak |
||
148 | * @return PrivateKey |
||
149 | */ |
||
150 | 4 | public function tweakMul(\GMP $tweak) |
|
151 | { |
||
152 | 4 | $privateKey = $this->getBinary(); |
|
153 | 4 | $math = $this->ecAdapter->getMath(); |
|
154 | 4 | $tweak = Buffer::int($math->toString($tweak), 32, $math)->getBinary(); |
|
155 | 4 | $ret = \secp256k1_ec_privkey_tweak_mul( |
|
156 | 4 | $this->ecAdapter->getContext(), |
|
157 | $privateKey, |
||
158 | $tweak |
||
159 | 2 | ); |
|
160 | |||
161 | 4 | if ($ret !== 1) { |
|
162 | throw new \RuntimeException('Secp256k1 privkey tweak mul: failed'); |
||
163 | } |
||
164 | |||
165 | 4 | $secret = new Buffer($privateKey); |
|
166 | |||
167 | 4 | return $this->ecAdapter->getPrivateKey($secret->getGmp(), $this->compressed); |
|
168 | } |
||
169 | |||
170 | /** |
||
171 | * @param NetworkInterface $network |
||
172 | * @return string |
||
173 | */ |
||
174 | 12 | public function toWif(NetworkInterface $network = null) |
|
180 | |||
181 | /** |
||
182 | * @return BufferInterface |
||
183 | */ |
||
184 | 220 | public function getBuffer() |
|
188 | } |
||
189 |