Skip to content

Commit 513a3b7

Browse files
Thomas Kerinafk11
Thomas Kerin
authored andcommitted
fixes in line with new secp256k1 api
1 parent dfa17d9 commit 513a3b7

File tree

7 files changed

+24
-24
lines changed

7 files changed

+24
-24
lines changed

src/Crypto/EcAdapter/Impl/Secp256k1/Adapter/EcAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public function getContext()
128128
*/
129129
private function doRecover(BufferInterface $msg32, CompactSignature $compactSig): PublicKey
130130
{
131-
$publicKey = '';
131+
$publicKey = null;
132132
/** @var resource $publicKey */
133133
$context = $this->context;
134134
$sig = $compactSig->getResource();

src/Crypto/EcAdapter/Impl/Secp256k1/Key/PrivateKey.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,11 @@ public function sign(BufferInterface $msg32, RbgInterface $rbgInterface = null):
7979
{
8080
$context = $this->ecAdapter->getContext();
8181

82-
/** @var resource $sig_t */
83-
$sig_t = '';
82+
$sig_t = null;
8483
if (1 !== secp256k1_ecdsa_sign($context, $sig_t, $msg32->getBinary(), $this->secretBin)) {
8584
throw new \RuntimeException('Secp256k1: failed to sign');
8685
}
87-
86+
/** @var resource $sig_t */
8887
$derSig = '';
8988
secp256k1_ecdsa_signature_serialize_der($context, $derSig, $sig_t);
9089

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

109-
$sig_t = '';
108+
$sig_t = null;
110109
if (1 !== secp256k1_ecdsa_sign_recoverable($context, $sig_t, $msg32->getBinary(), $this->secretBin)) {
111110
throw new \RuntimeException('Secp256k1: failed to sign');
112111
}
113-
114-
$recid = '';
112+
/** @var resource $sig_t
113+
*/
114+
$recid = 0;
115115
$ser = '';
116-
if (!secp256k1_ecdsa_recoverable_signature_serialize_compact($context, $sig_t, $ser, $recid)) {
116+
if (!secp256k1_ecdsa_recoverable_signature_serialize_compact($context, $ser, $recid, $sig_t)) {
117117
throw new \RuntimeException('Failed to obtain recid');
118118
}
119119

@@ -160,12 +160,11 @@ public function getPublicKey()
160160
{
161161
if (null === $this->publicKey) {
162162
$context = $this->ecAdapter->getContext();
163-
$publicKey_t = '';
164-
/** @var resource $publicKey_t */
163+
$publicKey_t = null;
165164
if (1 !== secp256k1_ec_pubkey_create($context, $publicKey_t, $this->getBinary())) {
166165
throw new \RuntimeException('Failed to create public key');
167166
}
168-
167+
/** @var resource $publicKey_t */
169168
$this->publicKey = new PublicKey($this->ecAdapter, $publicKey_t, $this->compressed);
170169
}
171170

src/Crypto/EcAdapter/Impl/Secp256k1/Key/PublicKey.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@ public function __construct(EcAdapter $ecAdapter, $secp256k1_pubkey_t, bool $com
5959
*/
6060
public function verify(BufferInterface $msg32, SignatureInterface $signature): bool
6161
{
62+
$ctx = $this->ecAdapter->getContext();
63+
$normalized = null;
64+
secp256k1_ecdsa_signature_normalize($ctx, $normalized, $signature->getResource());
6265
/** @var Signature $signature */
63-
return (bool) secp256k1_ecdsa_verify($this->ecAdapter->getContext(), $signature->getResource(), $msg32->getBinary(), $this->pubkey_t);
66+
return (bool) secp256k1_ecdsa_verify($ctx, $normalized, $msg32->getBinary(), $this->pubkey_t);
6467
}
6568

6669
/**
@@ -121,7 +124,7 @@ private function clonePubkey()
121124
}
122125

123126
/** @var resource $clone */
124-
$clone = '';
127+
$clone = null;
125128
if (1 !== secp256k1_ec_pubkey_parse($context, $clone, $serialized)) {
126129
throw new \Exception('Secp256k1 pubkey parse');
127130
}

src/Crypto/EcAdapter/Impl/Secp256k1/Serializer/Key/PublicKeySerializer.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,11 @@ public function serialize(PublicKeyInterface $publicKey): BufferInterface
6767
public function parse(BufferInterface $buffer): PublicKeyInterface
6868
{
6969
$binary = $buffer->getBinary();
70-
$pubkey_t = '';
71-
/** @var resource $pubkey_t */
70+
$pubkey_t = null;
7271
if (!secp256k1_ec_pubkey_parse($this->ecAdapter->getContext(), $pubkey_t, $binary)) {
7372
throw new \RuntimeException('Secp256k1 failed to parse public key');
7473
}
75-
74+
/** @var resource $pubkey_t */
7675
return new PublicKey(
7776
$this->ecAdapter,
7877
$pubkey_t,

src/Crypto/EcAdapter/Impl/Secp256k1/Serializer/Signature/CompactSignatureSerializer.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public function __construct(EcAdapter $ecAdapter)
3333
private function doSerialize(CompactSignature $signature)
3434
{
3535
$sig_t = '';
36-
$recid = '';
37-
if (!secp256k1_ecdsa_recoverable_signature_serialize_compact($this->ecAdapter->getContext(), $signature->getResource(), $sig_t, $recid)) {
36+
$recid = 0;
37+
if (!secp256k1_ecdsa_recoverable_signature_serialize_compact($this->ecAdapter->getContext(), $sig_t, $recid, $signature->getResource())) {
3838
throw new \RuntimeException('Secp256k1 serialize compact failure');
3939
}
4040

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

76-
$sig_t = '';
77-
/** @var resource $sig_t */
76+
$sig_t = null;
7877
if (!secp256k1_ecdsa_recoverable_signature_parse_compact($this->ecAdapter->getContext(), $sig_t, $sig->getBinary(), $recoveryId)) {
7978
throw new \RuntimeException('Unable to parse compact signature');
8079
}
81-
80+
/** @var resource $sig_t */
8281
return new CompactSignature($this->ecAdapter, $sig_t, $recoveryId, $isCompressed);
8382
}
8483
}

src/Crypto/EcAdapter/Impl/Secp256k1/Serializer/Signature/DerSignatureSerializer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function parse(BufferInterface $derSignature): SignatureInterface
9595
$derSignature = (new Parser($derSignature))->getBuffer();
9696
$binary = $derSignature->getBinary();
9797

98-
$sig_t = '';
98+
$sig_t = null;
9999
/** @var resource $sig_t */
100100
if (!ecdsa_signature_parse_der_lax($this->ecAdapter->getContext(), $sig_t, $binary)) {
101101
throw new \RuntimeException('Secp256k1: parse der failure');

src/Crypto/EcAdapter/Impl/Secp256k1/Signature/CompactSignature.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public function __construct(EcAdapter $ecAdapter, $secp256k1_ecdsa_signature_t,
5151
}
5252

5353
$ser = '';
54-
$recidout = '';
55-
secp256k1_ecdsa_recoverable_signature_serialize_compact($ecAdapter->getContext(), $secp256k1_ecdsa_signature_t, $ser, $recidout);
54+
$recidout = 0;
55+
secp256k1_ecdsa_recoverable_signature_serialize_compact($ecAdapter->getContext(), $ser, $recidout, $secp256k1_ecdsa_signature_t);
5656
list ($r, $s) = array_map(
5757
function ($val) use ($math) {
5858
return (new Buffer($val))->getGmp();

0 commit comments

Comments
 (0)