Skip to content

Commit abb38db

Browse files
committed
weierstrass: freeze Signature on creation
1 parent a5b3137 commit abb38db

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/abstract/weierstrass.ts

+1
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,7 @@ export function weierstrass(curveDef: CurveType): CurveFn {
909909
this.s = s;
910910
if (recovery != null) this.recovery = recovery;
911911
this.assertValidity();
912+
Object.freeze(this);
912913
}
913914

914915
// pair (bytes of r, bytes of s)

test/secp256k1.test.js

+19-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
import { json } from './utils.js';
2-
import { hexToBytes, bytesToHex as hex } from '@noble/hashes/utils';
3-
import { deepStrictEqual, throws } from 'node:assert';
1+
import { bytesToHex as hex, hexToBytes } from '@noble/hashes/utils';
42
import * as fc from 'fast-check';
3+
import { describe, should } from 'micro-should';
4+
import { deepStrictEqual, throws } from 'node:assert';
55
import { readFileSync } from 'node:fs';
6-
import { should, describe } from 'micro-should';
6+
import { json } from './utils.js';
77
// prettier-ignore
88
import {
9-
secp, sigFromDER, sigToDER, selectHash, normVerifySig, mod, bytesToNumberBE, numberToBytesBE
9+
bytesToNumberBE,
10+
mod,
11+
normVerifySig,
12+
numberToBytesBE,
13+
secp,
14+
selectHash,
15+
sigFromDER, sigToDER
1016
} from './secp256k1.helpers.js';
1117

1218
const ecdsa = json('./vectors/secp256k1/ecdsa.json');
@@ -428,11 +434,15 @@ describe('secp256k1', () => {
428434
const s = 115792089237316195423570985008687907852837564279074904382605163141518162728904n;
429435

430436
const pub = new Point(x, y, 1n).toRawBytes();
431-
const signature = new secp.Signature(2n, 2n);
432-
signature.r = r;
433-
signature.s = s;
437+
const sig = new secp.Signature(2n, 2n);
438+
throws(() => {
439+
sig.r = r;
440+
sig.s = s;
441+
});
442+
const sigHex =
443+
'0000000000000000000000000000000000000000000000000000000000000001fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd04917c8';
434444

435-
const verified = secp.verify(signature, msg, pub);
445+
const verified = secp.verify(sigHex, msg, pub);
436446
// Verifies, but it shouldn't, because signature S > curve order
437447
deepStrictEqual(verified, false);
438448
});

0 commit comments

Comments
 (0)