1 | <?php |
||
21 | class EcAdapter implements EcAdapterInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var Math |
||
25 | */ |
||
26 | private $math; |
||
27 | |||
28 | /** |
||
29 | * @var GeneratorPoint |
||
30 | */ |
||
31 | private $generator; |
||
32 | |||
33 | /** |
||
34 | * @param Math $math |
||
35 | * @param GeneratorPoint $generator |
||
36 | */ |
||
37 | 26 | public function __construct(Math $math, GeneratorPoint $generator) |
|
42 | |||
43 | /** |
||
44 | * @return Math |
||
45 | */ |
||
46 | 640 | public function getMath() |
|
50 | |||
51 | /** |
||
52 | * @return GeneratorPoint |
||
53 | */ |
||
54 | 293 | public function getGenerator() |
|
58 | |||
59 | /** |
||
60 | * @param int|string $scalar |
||
61 | * @param bool|false $compressed |
||
62 | * @return PrivateKey |
||
63 | */ |
||
64 | 125 | public function getPrivateKey($scalar, $compressed = false) |
|
68 | |||
69 | /** |
||
70 | * @param PointInterface $point |
||
71 | * @param bool|false $compressed |
||
72 | * @return PublicKey |
||
73 | */ |
||
74 | 105 | public function getPublicKey(PointInterface $point, $compressed = false) |
|
78 | |||
79 | /** |
||
80 | * @param int|string $r |
||
81 | * @param int|string $s |
||
82 | * @return Signature |
||
83 | */ |
||
84 | public function getSignature($r, $s) |
||
88 | |||
89 | /** |
||
90 | * @param BufferInterface $messageHash |
||
91 | * @param PublicKey $publicKey |
||
92 | * @param Signature $signature |
||
93 | * @return bool |
||
94 | */ |
||
95 | 47 | private function doVerify(BufferInterface $messageHash, PublicKey $publicKey, Signature $signature) |
|
120 | |||
121 | /** |
||
122 | * @param BufferInterface $messageHash |
||
123 | * @param PublicKeyInterface $publicKey |
||
124 | * @param SignatureInterface $signature |
||
125 | * @return bool |
||
126 | */ |
||
127 | 47 | public function verify(BufferInterface $messageHash, PublicKeyInterface $publicKey, SignatureInterface $signature) |
|
133 | |||
134 | /** |
||
135 | * @param BufferInterface $messageHash |
||
136 | * @param PrivateKey $privateKey |
||
137 | * @param RbgInterface|null $rbg |
||
138 | * @return Signature |
||
139 | */ |
||
140 | 46 | private function doSign(BufferInterface $messageHash, PrivateKey $privateKey, RbgInterface $rbg = null) |
|
184 | |||
185 | /** |
||
186 | * @param BufferInterface $messageHash |
||
187 | * @param PrivateKeyInterface $privateKey |
||
188 | * @param RbgInterface $rbg |
||
189 | * @return SignatureInterface |
||
190 | * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure |
||
191 | */ |
||
192 | 46 | public function sign(BufferInterface $messageHash, PrivateKeyInterface $privateKey, RbgInterface $rbg = null) |
|
197 | |||
198 | /** |
||
199 | * @param BufferInterface $messageHash |
||
200 | * @param CompactSignatureInterface $signature |
||
201 | * @return PublicKey |
||
202 | * @throws \Exception |
||
203 | */ |
||
204 | 17 | public function recover(BufferInterface $messageHash, CompactSignatureInterface $signature) |
|
261 | |||
262 | /** |
||
263 | * Attempt to calculate the public key recovery param by trial and error |
||
264 | * |
||
265 | * @param int $r |
||
266 | * @param int $s |
||
267 | * @param BufferInterface $messageHash |
||
268 | * @param PublicKey $publicKey |
||
269 | * @return int |
||
270 | * @throws \Exception |
||
271 | */ |
||
272 | 15 | public function calcPubKeyRecoveryParam($r, $s, BufferInterface $messageHash, PublicKey $publicKey) |
|
273 | { |
||
274 | 15 | $Q = $publicKey->getPoint(); |
|
275 | 15 | for ($i = 0; $i < 4; $i++) { |
|
276 | try { |
||
277 | 15 | $recover = $this->recover($messageHash, new CompactSignature($this, $r, $s, $i, $publicKey->isCompressed())); |
|
278 | 12 | if ($recover->getPoint()->equals($Q)) { |
|
279 | 12 | return $i; |
|
280 | } |
||
281 | 5 | } catch (\Exception $e) { |
|
282 | 3 | continue; |
|
283 | } |
||
284 | 2 | } |
|
285 | |||
286 | 3 | throw new \Exception('Failed to find valid recovery factor'); |
|
287 | } |
||
288 | |||
289 | /** |
||
290 | * @param BufferInterface $messageHash |
||
291 | * @param PrivateKey $privateKey |
||
292 | * @param RbgInterface|null $rbg |
||
293 | * @return CompactSignature |
||
294 | * @throws \Exception |
||
295 | */ |
||
296 | 12 | private function doSignCompact(BufferInterface $messageHash, PrivateKey $privateKey, RbgInterface $rbg = null) |
|
310 | |||
311 | /** |
||
312 | * @param PrivateKeyInterface $privateKey |
||
313 | * @param BufferInterface $messageHash |
||
314 | * @param RbgInterface $rbg |
||
315 | * @return CompactSignature |
||
316 | */ |
||
317 | 12 | public function signCompact(BufferInterface $messageHash, PrivateKeyInterface $privateKey, RbgInterface $rbg = null) |
|
322 | |||
323 | /** |
||
324 | * @param BufferInterface $privateKey |
||
325 | * @return bool |
||
326 | */ |
||
327 | 129 | public function validatePrivateKey(BufferInterface $privateKey) |
|
333 | |||
334 | /** |
||
335 | * @param \GMP $element |
||
336 | * @param bool $half |
||
337 | * @return bool |
||
338 | */ |
||
339 | 50 | public function validateSignatureElement(\GMP $element, $half = false) |
|
349 | |||
350 | /** |
||
351 | * @param BufferInterface $publicKey |
||
352 | * @return PublicKeyInterface |
||
353 | * @throws \Exception |
||
354 | */ |
||
355 | 158 | public function publicKeyFromBuffer(BufferInterface $publicKey) |
|
373 | |||
374 | /** |
||
375 | * @param \GMP $xCoord |
||
376 | * @param string $prefix |
||
377 | * @return int|string |
||
378 | * @throws \Exception |
||
379 | */ |
||
380 | 102 | public function recoverYfromX(\GMP $xCoord, $prefix) |
|
409 | } |
||
410 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.