Skip to content

Commit c2987b3

Browse files
authored
Merge pull request #21 from paulmillr/master
Fix tests
2 parents 02d11a7 + 8a51824 commit c2987b3

File tree

3 files changed

+16
-22
lines changed

3 files changed

+16
-22
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"dependencies": {
2727
"micro-base": "^0.10.1",
2828
"@noble/hashes": "^0.5.7",
29-
"@noble/secp256k1": "^1.3.3"
29+
"@noble/secp256k1": "^1.4.0"
3030
},
3131
"browser": {
3232
"crypto": false

src/secp256k1-compat.ts

+13-19
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { sha256 } from "@noble/hashes/sha256";
22
import * as secp from "./secp256k1";
33
import { assertBool, assertBytes, hexToBytes, toHex } from "./utils";
44

5-
// Legacy compatibility layer for elliptic via noble-secp256k1
6-
// Use `secp256k1` module directly instead
5+
// Use `secp256k1` module directly.
6+
// This is a legacy compatibility layer for the npm package `secp256k1` via noble-secp256k1
77

88
// Copy-paste from secp256k1, maybe export it?
99
const bytesToNumber = (bytes: Uint8Array) => hexToNumber(toHex(bytes));
@@ -116,7 +116,6 @@ export function ecdsaSign(
116116
}
117117
const [signature, recid] = secp.signSync(msgHash, privateKey, {
118118
recovered: true,
119-
canonical: true,
120119
der: false
121120
});
122121
return { signature: output(out, 64, signature), recid };
@@ -150,13 +149,14 @@ export function ecdsaVerify(
150149
if (r >= ORDER || s >= ORDER) {
151150
throw new Error("Cannot parse signature");
152151
}
152+
const pub = secp.Point.fromHex(publicKey); // should not throw error
153153
let sig;
154154
try {
155155
sig = getSignature(signature);
156156
} catch (error) {
157157
return false;
158158
}
159-
return secp.verify(sig, msgHash, publicKey);
159+
return secp.verify(sig, msgHash, pub);
160160
}
161161

162162
export function privateKeyTweakAdd(
@@ -234,10 +234,10 @@ export function publicKeyTweakAdd(
234234
assertBool(compressed);
235235
const p1 = secp.Point.fromHex(publicKey);
236236
const p2 = secp.Point.fromPrivateKey(tweak);
237-
if (p2.equals(secp.Point.ZERO)) {
237+
const point = p1.add(p2);
238+
if (p2.equals(secp.Point.ZERO) || point.equals(secp.Point.ZERO)) {
238239
throw new Error("Tweak must not be zero");
239240
}
240-
const point = p1.add(p2);
241241
return output(out, compressed ? 33 : 65, point.toRawBytes(compressed));
242242
}
243243

@@ -254,7 +254,7 @@ export function publicKeyTweakMul(
254254
if (bn === 0n) {
255255
throw new Error("Tweak must not be zero");
256256
}
257-
if (bn <= 0 || bn >= ORDER) {
257+
if (bn <= 1 || bn >= ORDER) {
258258
throw new Error("Tweak is zero or bigger than curve order");
259259
}
260260
const point = secp.Point.fromHex(publicKey).multiply(bn);
@@ -267,23 +267,17 @@ export function privateKeyTweakMul(
267267
): Uint8Array {
268268
assertBytes(privateKey, 32);
269269
assertBytes(tweak, 32);
270-
let bn = bytesToNumber(tweak);
271-
if (bn === 0n) {
272-
throw new Error("Tweak must not be zero");
273-
}
274-
if (bn >= ORDER) {
275-
throw new Error("Tweak bigger than curve order");
276-
}
277-
bn = mod(bn * bytesToNumber(privateKey), ORDER);
278-
if (bn >= ORDER) {
279-
bn -= ORDER;
270+
const bn = bytesToNumber(tweak);
271+
if (bn <= 1 || bn >= ORDER) {
272+
throw new Error("Tweak is zero or bigger than curve order");
280273
}
281-
if (bn === 0n) {
274+
const res = mod(bn * bytesToNumber(privateKey), ORDER);
275+
if (res === 0n) {
282276
throw new Error(
283277
"The tweak was out of range or the resulted private key is invalid"
284278
);
285279
}
286-
privateKey.set(hexToBytes(numberToHex(bn)));
280+
privateKey.set(hexToBytes(numberToHex(res)));
287281
return privateKey;
288282
}
289283
// internal -> DER

test/test-vectors/secp256k1.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as secp from "../../src/secp256k1";
22
import { deepStrictEqual } from "./assert";
33

4-
describe("curve-secp256k1", () => {
4+
describe("secp256k1", () => {
55
it("should verify msg bb5a...", async () => {
66
const msg =
77
"bb5a52f42f9c9261ed4361f59422a1e30036e7c32b270c8807a419feca605023";
@@ -11,6 +11,6 @@ describe("curve-secp256k1", () => {
1111
const s = 115792089237316195423570985008687907852837564279074904382605163141518161494334n;
1212
const pub = new secp.Point(x, y);
1313
const sig = new secp.Signature(r, s);
14-
deepStrictEqual(secp.verify(sig, msg, pub), true);
14+
deepStrictEqual(secp.verify(sig, msg, pub, { strict: false }), true);
1515
});
1616
});

0 commit comments

Comments
 (0)