/** * @param PublicKey $publicKey * @return Buffer */ private function doSerialize(PublicKey $publicKey) { $math = $this->ecAdapter->getMath(); $point = $publicKey->getPoint(); $compressed = $publicKey->isCompressed(); $parser = new Parser('', $math); $parser->writeBytes(1, $this->getPrefix($compressed, $point)); $compressed ? $parser->writeBytes(32, Buffer::int($point->getX(), null, $math)) : $parser->writeBytes(32, Buffer::int($point->getX(), null, $math))->writeBytes(32, Buffer::int($point->getY(), null, $math)); return $parser->getBuffer(); }
/** * Attempt to calculate the public key recovery param by trial and error * * @param int|string $r * @param int|string $s * @param Buffer $messageHash * @param PublicKey $publicKey * @return int * @throws \Exception */ public function calcPubKeyRecoveryParam($r, $s, Buffer $messageHash, PublicKey $publicKey) { $Q = $publicKey->getPoint(); for ($i = 0; $i < 4; $i++) { try { $recover = $this->recover($messageHash, new CompactSignature($this, $r, $s, $i, $publicKey->isCompressed())); if ($recover->getPoint()->equals($Q)) { return $i; } } catch (\Exception $e) { continue; } } throw new \Exception('Failed to find valid recovery factor'); }