Skip to content

Commit

Permalink
test: require @noble/curves to work
Browse files Browse the repository at this point in the history
  • Loading branch information
dcposch committed Sep 10, 2023
1 parent 93d243f commit 412ad1e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 37 deletions.
9 changes: 5 additions & 4 deletions test-vectors/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,13 @@ async function extractVectors(sourceName: string, sourceObj: any) {
// calculate SHA256 hash of msgBytes
const msgBytes = Buffer.from(msg, "hex");
const msgHash = Buffer.from(await crypto.subtle.digest(sha, msgBytes));
assert(msgHash.length === 32, "hash must be 256 bits");

vectors.push({
x,
y,
r,
s,
x: x.padStart(64, "0"),
y: y.padStart(64, "0"),
r: r.padStart(64, "0"),
s: s.padStart(64, "0"),
hash: msgHash.toString("hex"),
valid,
msg,
Expand Down
14 changes: 5 additions & 9 deletions test-vectors/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ async function main() {

for (const vector of vectors) {
// Convert hex strings to Uint8Arrays
const x = Buffer.from(vector.x.padStart(64, "0"), "hex");
const y = Buffer.from(vector.y.padStart(64, "0"), "hex");
const r = Buffer.from(vector.r.padStart(64, "0"), "hex");
const s = Buffer.from(vector.s.padStart(64, "0"), "hex");
const x = Buffer.from(vector.x, "hex");
const y = Buffer.from(vector.y, "hex");
const r = Buffer.from(vector.r, "hex");
const s = Buffer.from(vector.s, "hex");
const msg = Buffer.from(vector.msg, "hex");
const hash = Buffer.from(vector.hash, "hex");

Expand Down Expand Up @@ -59,11 +59,7 @@ async function main() {
// Verify signature using @noble/curves
const pub = new Uint8Array([0x04, ...x, ...y]);
const resultNoble = p256.verify(sig, hash, pub);
if (resultNoble !== vector.valid) {
console.log(
`@noble/curves returned ${resultNoble}, expected ${vector.valid} for ${vector.comment}`
);
}
assert(resultSubtle === vector.valid, "@noble/curves " + vector.comment);
}
}

Expand Down
Loading

0 comments on commit 412ad1e

Please sign in to comment.