Skip to content

Commit

Permalink
test: Sync-up secp256k1 with other package
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Nov 20, 2024
1 parent 2374281 commit 464e980
Showing 1 changed file with 37 additions and 13 deletions.
50 changes: 37 additions & 13 deletions test/secp256k1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,21 @@ describe('secp256k1', () => {
});

describe('sign()', () => {
should('create deterministic signatures with RFC 6979', () => {
should('create deterministic signatures with RFC 6979', async () => {
for (const vector of ecdsa.valid) {
let usig = secp.sign(vector.m, vector.d);
let sig = usig.toCompactHex();
const vsig = vector.signature;
deepStrictEqual(sig.slice(0, 64), vsig.slice(0, 64));
deepStrictEqual(sig.slice(64, 128), vsig.slice(64, 128));

if (secp.signAsync) {
let usig = await secp.signAsync(vector.m, vector.d);
let sig = usig.toCompactHex();
const vsig = vector.signature;
deepStrictEqual(sig.slice(0, 64), vsig.slice(0, 64));
deepStrictEqual(sig.slice(64, 128), vsig.slice(64, 128));
}
}
});

Expand Down Expand Up @@ -285,7 +293,7 @@ describe('secp256k1', () => {
deepStrictEqual(sigToDER(secp.Signature.fromCompact(rs)), exp);
}
});
should('handle {extraData} option', () => {
should('handle {extraEntropy} option', () => {
const ent1 = '0000000000000000000000000000000000000000000000000000000000000000';
const ent2 = '0000000000000000000000000000000000000000000000000000000000000001';
const ent3 = '6e723d3fd94ed5d2b6bdd4f123364b0f3ca52af829988a63f8afe91d29db1c33';
Expand All @@ -306,7 +314,7 @@ describe('secp256k1', () => {
}
});

should('handle one byte {extraData}', () => {
should('handle one byte {extraEntropy}', () => {
const extraEntropy = '01';
const privKey = hexToBytes(
'0101010101010101010101010101010101010101010101010101010101010101'
Expand All @@ -319,7 +327,7 @@ describe('secp256k1', () => {
);
});

should('handle 48 bytes {extraData}', () => {
should('handle 48 bytes {extraEntropy}', () => {
const extraEntropy =
'000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000001';
const privKey = hexToBytes(
Expand All @@ -335,22 +343,30 @@ describe('secp256k1', () => {
});

describe('verify()', () => {
should('verify signature', () => {
should('verify signature', async () => {
const MSG = '01'.repeat(32);
const PRIV_KEY = 0x2n;
const signature = secp.sign(MSG, PRIV_KEY);
const publicKey = secp.getPublicKey(PRIV_KEY);
deepStrictEqual(publicKey.length, 33);
const signature = secp.sign(MSG, PRIV_KEY);
deepStrictEqual(secp.verify(signature, MSG, publicKey), true);
if (secp.signAsync) {
const signature = await secp.signAsync(MSG, PRIV_KEY);
deepStrictEqual(secp.verify(signature, MSG, publicKey), true);
}
});
should(' not verify signature with wrong public key', () => {
should(' not verify signature with wrong public key', async () => {
const MSG = '01'.repeat(32);
const PRIV_KEY = '01'.repeat(32);
const WRONG_PRIV_KEY = '02'.repeat(32);
const signature = secp.sign(MSG, PRIV_KEY);
const publicKey = Point.fromPrivateKey(WRONG_PRIV_KEY).toHex();
deepStrictEqual(publicKey.length, 66);
const signature = secp.sign(MSG, PRIV_KEY);
deepStrictEqual(secp.verify(signature, MSG, publicKey), false);
if (secp.signAsync) {
const signature = await secp.signAsync(MSG, PRIV_KEY);
deepStrictEqual(secp.verify(signature, MSG, publicKey), false);
}
});
should('not verify signature with wrong hash', () => {
const MSG = '01'.repeat(32);
Expand All @@ -363,11 +379,19 @@ describe('secp256k1', () => {
});
should('verify random signatures', () =>
fc.assert(
fc.property(FC_BIGINT, fc.hexaString({ minLength: 64, maxLength: 64 }), (privKey, msg) => {
const pub = secp.getPublicKey(privKey);
const sig = secp.sign(msg, privKey);
deepStrictEqual(secp.verify(sig, msg, pub), true);
})
fc.asyncProperty(
FC_BIGINT,
fc.hexaString({ minLength: 64, maxLength: 64 }),
async (privKey, msg) => {
const pub = secp.getPublicKey(privKey);
const sig = secp.sign(msg, privKey);
deepStrictEqual(secp.verify(sig, msg, pub), true);
if (secp.signAsync) {
const sig = await secp.signAsync(msg, privKey);
deepStrictEqual(secp.verify(sig, msg, pub), true);
}
}
)
)
);
should('not verify signature with invalid r/s', () => {
Expand Down

0 comments on commit 464e980

Please sign in to comment.