Skip to content

Commit

Permalink
fixes in line with new secp256k1 api
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Kerin authored and afk11 committed Jul 16, 2018
1 parent dfa17d9 commit 513a3b7
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/Crypto/EcAdapter/Impl/Secp256k1/Adapter/EcAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function getContext()
*/
private function doRecover(BufferInterface $msg32, CompactSignature $compactSig): PublicKey
{
$publicKey = '';
$publicKey = null;
/** @var resource $publicKey */
$context = $this->context;
$sig = $compactSig->getResource();
Expand Down
19 changes: 9 additions & 10 deletions src/Crypto/EcAdapter/Impl/Secp256k1/Key/PrivateKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,11 @@ public function sign(BufferInterface $msg32, RbgInterface $rbgInterface = null):
{
$context = $this->ecAdapter->getContext();

/** @var resource $sig_t */
$sig_t = '';
$sig_t = null;
if (1 !== secp256k1_ecdsa_sign($context, $sig_t, $msg32->getBinary(), $this->secretBin)) {
throw new \RuntimeException('Secp256k1: failed to sign');
}

/** @var resource $sig_t */
$derSig = '';
secp256k1_ecdsa_signature_serialize_der($context, $derSig, $sig_t);

Expand All @@ -106,14 +105,15 @@ public function signCompact(BufferInterface $msg32, RbgInterface $rbfInterface =
{
$context = $this->ecAdapter->getContext();

$sig_t = '';
$sig_t = null;
if (1 !== secp256k1_ecdsa_sign_recoverable($context, $sig_t, $msg32->getBinary(), $this->secretBin)) {
throw new \RuntimeException('Secp256k1: failed to sign');
}

$recid = '';
/** @var resource $sig_t
*/
$recid = 0;
$ser = '';
if (!secp256k1_ecdsa_recoverable_signature_serialize_compact($context, $sig_t, $ser, $recid)) {
if (!secp256k1_ecdsa_recoverable_signature_serialize_compact($context, $ser, $recid, $sig_t)) {
throw new \RuntimeException('Failed to obtain recid');
}

Expand Down Expand Up @@ -160,12 +160,11 @@ public function getPublicKey()
{
if (null === $this->publicKey) {
$context = $this->ecAdapter->getContext();
$publicKey_t = '';
/** @var resource $publicKey_t */
$publicKey_t = null;
if (1 !== secp256k1_ec_pubkey_create($context, $publicKey_t, $this->getBinary())) {
throw new \RuntimeException('Failed to create public key');
}

/** @var resource $publicKey_t */
$this->publicKey = new PublicKey($this->ecAdapter, $publicKey_t, $this->compressed);
}

Expand Down
7 changes: 5 additions & 2 deletions src/Crypto/EcAdapter/Impl/Secp256k1/Key/PublicKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ public function __construct(EcAdapter $ecAdapter, $secp256k1_pubkey_t, bool $com
*/
public function verify(BufferInterface $msg32, SignatureInterface $signature): bool
{
$ctx = $this->ecAdapter->getContext();
$normalized = null;
secp256k1_ecdsa_signature_normalize($ctx, $normalized, $signature->getResource());
/** @var Signature $signature */
return (bool) secp256k1_ecdsa_verify($this->ecAdapter->getContext(), $signature->getResource(), $msg32->getBinary(), $this->pubkey_t);
return (bool) secp256k1_ecdsa_verify($ctx, $normalized, $msg32->getBinary(), $this->pubkey_t);
}

/**
Expand Down Expand Up @@ -121,7 +124,7 @@ private function clonePubkey()
}

/** @var resource $clone */
$clone = '';
$clone = null;
if (1 !== secp256k1_ec_pubkey_parse($context, $clone, $serialized)) {
throw new \Exception('Secp256k1 pubkey parse');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,11 @@ public function serialize(PublicKeyInterface $publicKey): BufferInterface
public function parse(BufferInterface $buffer): PublicKeyInterface
{
$binary = $buffer->getBinary();
$pubkey_t = '';
/** @var resource $pubkey_t */
$pubkey_t = null;
if (!secp256k1_ec_pubkey_parse($this->ecAdapter->getContext(), $pubkey_t, $binary)) {
throw new \RuntimeException('Secp256k1 failed to parse public key');
}

/** @var resource $pubkey_t */
return new PublicKey(
$this->ecAdapter,
$pubkey_t,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public function __construct(EcAdapter $ecAdapter)
private function doSerialize(CompactSignature $signature)
{
$sig_t = '';
$recid = '';
if (!secp256k1_ecdsa_recoverable_signature_serialize_compact($this->ecAdapter->getContext(), $signature->getResource(), $sig_t, $recid)) {
$recid = 0;
if (!secp256k1_ecdsa_recoverable_signature_serialize_compact($this->ecAdapter->getContext(), $sig_t, $recid, $signature->getResource())) {
throw new \RuntimeException('Secp256k1 serialize compact failure');
}

Expand Down Expand Up @@ -73,12 +73,11 @@ public function parse(BufferInterface $buffer): CompactSignatureInterface
$isCompressed = ($recoveryFlags & 4) !== 0;
$recoveryId = $recoveryFlags - ($isCompressed ? 4 : 0);

$sig_t = '';
/** @var resource $sig_t */
$sig_t = null;
if (!secp256k1_ecdsa_recoverable_signature_parse_compact($this->ecAdapter->getContext(), $sig_t, $sig->getBinary(), $recoveryId)) {
throw new \RuntimeException('Unable to parse compact signature');
}

/** @var resource $sig_t */
return new CompactSignature($this->ecAdapter, $sig_t, $recoveryId, $isCompressed);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function parse(BufferInterface $derSignature): SignatureInterface
$derSignature = (new Parser($derSignature))->getBuffer();
$binary = $derSignature->getBinary();

$sig_t = '';
$sig_t = null;
/** @var resource $sig_t */
if (!ecdsa_signature_parse_der_lax($this->ecAdapter->getContext(), $sig_t, $binary)) {
throw new \RuntimeException('Secp256k1: parse der failure');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public function __construct(EcAdapter $ecAdapter, $secp256k1_ecdsa_signature_t,
}

$ser = '';
$recidout = '';
secp256k1_ecdsa_recoverable_signature_serialize_compact($ecAdapter->getContext(), $secp256k1_ecdsa_signature_t, $ser, $recidout);
$recidout = 0;
secp256k1_ecdsa_recoverable_signature_serialize_compact($ecAdapter->getContext(), $ser, $recidout, $secp256k1_ecdsa_signature_t);
list ($r, $s) = array_map(
function ($val) use ($math) {
return (new Buffer($val))->getGmp();
Expand Down

0 comments on commit 513a3b7

Please sign in to comment.