/** * @param PrivateKeyInterface $privateKey * @param Buffer $messageHash * @param RbgInterface $rbg * @return CompactSignature * @throws \Exception */ public function signCompact(Buffer $messageHash, PrivateKeyInterface $privateKey, RbgInterface $rbg = null) { $sigStr = ''; $recid = 0; $ret = \secp256k1_ecdsa_sign_compact($messageHash->getBinary(), $privateKey->getBuffer()->getBinary(), $sigStr, $recid); if ($ret === 1) { $sigStr = new Buffer($sigStr); return new CompactSignature($sigStr->slice(0, 32)->getInt(), $sigStr->slice(32, 32)->getInt(), $recid, $privateKey->isCompressed()); } throw new \Exception('Unable to create compact signature'); }
/** * @param Buffer $ip * @return string * @throws \Exception */ private function parseIpBuffer(Buffer $ip) { $end = $ip->slice(12, 4); return implode(".", array_map(function ($int) { return unpack("C", $int)[1]; }, str_split($end->getBinary(), 1))); }
/** * @param Buffer $entropy * @return array * @throws \Exception */ public function entropyToWords(Buffer $entropy) { $math = $this->ecAdapter->getMath(); $n = count($this->wordList); $wordArray = []; $chunks = $entropy->getSize() / 4; for ($i = 0; $i < $chunks; $i++) { $x = $entropy->slice(4 * $i, 4)->getInt(); $index1 = $math->mod($x, $n); $index2 = $math->mod($math->add($math->div($x, $n), $index1), $n); $index3 = $math->mod($math->add($math->div($math->div($x, $n), $n), $index2), $n); $wordArray[] = $this->wordList->getWord($index1); $wordArray[] = $this->wordList->getWord($index2); $wordArray[] = $this->wordList->getWord($index3); } return $wordArray; }
/** * @param Buffer $publicKey * @return PublicKeyInterface * @throws \Exception */ public function publicKeyFromBuffer(Buffer $publicKey) { $compressed = $publicKey->getSize() == PublicKey::LENGTH_COMPRESSED; $xCoord = $publicKey->slice(1, 32)->getInt(); return new PublicKey($this, $this->getGenerator()->getCurve()->getPoint($xCoord, $compressed ? $this->recoverYfromX($xCoord, $publicKey->slice(0, 1)->getHex()) : $publicKey->slice(33, 32)->getInt()), $compressed); }