Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier committed Feb 16, 2022
1 parent 48341d5 commit 6413638
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
47 changes: 25 additions & 22 deletions NBitcoin.Tests/key_tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,28 +198,31 @@ public void key_test1()
}
// compact signatures (with key recovery)

byte[] csign1 = null, csign2 = null, csign1C = null, csign2C = null;

csign1 = key1.SignCompact(hashMsg);
csign2 = key2.SignCompact(hashMsg);
csign1C = key1C.SignCompact(hashMsg);
csign2C = key2C.SignCompact(hashMsg);

PubKey rkey1 = null, rkey2 = null, rkey1C = null, rkey2C = null;
rkey1 = PubKey.RecoverCompact(hashMsg, csign1);
rkey2 = PubKey.RecoverCompact(hashMsg, csign2);
rkey1C = PubKey.RecoverCompact(hashMsg, csign1C);
rkey2C = PubKey.RecoverCompact(hashMsg, csign2C);

Assert.True(rkey1.ToHex() == pubkey1.ToHex());
Assert.True(rkey2.ToHex() == pubkey2.ToHex());
Assert.True(rkey1C.ToHex() == pubkey1C.ToHex());
Assert.True(rkey2C.ToHex() == pubkey2C.ToHex());

Assert.True(sign1.IsLowR && sign1.ToDER().Length <= 70);
Assert.True(sign2.IsLowR && sign2.ToDER().Length <= 70);
Assert.True(sign1C.IsLowR && sign1C.ToDER().Length <= 70);
Assert.True(sign2C.IsLowR && sign2C.ToDER().Length <= 70);
CompactSignature csign1 = null, csign2 = null, csign1C = null, csign2C = null;

if (key1.IsCompressed)
{
csign1 = key1.SignCompact(hashMsg);
csign2 = key2.SignCompact(hashMsg);
csign1C = key1C.SignCompact(hashMsg);
csign2C = key2C.SignCompact(hashMsg);

PubKey rkey1 = null, rkey2 = null, rkey1C = null, rkey2C = null;
rkey1 = PubKey.RecoverCompact(hashMsg, csign1);
rkey2 = PubKey.RecoverCompact(hashMsg, csign2);
rkey1C = PubKey.RecoverCompact(hashMsg, csign1C);
rkey2C = PubKey.RecoverCompact(hashMsg, csign2C);

Assert.True(rkey1.ToHex() == pubkey1.ToHex());
Assert.True(rkey2.ToHex() == pubkey2.ToHex());
Assert.True(rkey1C.ToHex() == pubkey1C.ToHex());
Assert.True(rkey2C.ToHex() == pubkey2C.ToHex());

Assert.True(sign1.IsLowR && sign1.ToDER().Length <= 70);
Assert.True(sign2.IsLowR && sign2.ToDER().Length <= 70);
Assert.True(sign1C.IsLowR && sign1C.ToDER().Length <= 70);
Assert.True(sign2C.IsLowR && sign2C.ToDER().Length <= 70);
}
}
}

Expand Down
5 changes: 2 additions & 3 deletions NBitcoin/Key.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,8 @@ public CompactSignature SignCompact(uint256 hash, bool forceLowR)
AssertNotDisposed();
#if HAS_SPAN
byte[] sigBytes = new byte[64];
int rec = -1;
var sig = new Secp256k1.SecpRecoverableECDSASignature(_ECKey.Sign(hash, forceLowR, out rec), rec);
sig.WriteToSpanCompact(sigBytes, out int recid);
var sig = new Secp256k1.SecpRecoverableECDSASignature(_ECKey.Sign(hash, forceLowR, out var rec), rec);
sig.WriteToSpanCompact(sigBytes, out _);
return new CompactSignature(rec, sigBytes);
#else
var sig = _ECKey.Sign(hash, forceLowR);
Expand Down

0 comments on commit 6413638

Please sign in to comment.