From 849dc38f3c86396d63a152a8ae36c9f9533b6e65 Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Wed, 25 Jan 2023 04:24:22 +0000 Subject: [PATCH] Change TypeError to Error --- README.md | 5 ++--- src/abstract/edwards.ts | 2 +- src/abstract/utils.ts | 4 ++-- src/ed25519.ts | 2 +- src/p521.ts | 5 ++--- src/secp256k1.ts | 8 ++++---- src/stark.ts | 4 ++-- test/ed25519.test.js | 2 +- test/secp256k1.test.js | 2 +- 9 files changed, 16 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index a3356dd..1b97a23 100644 --- a/README.md +++ b/README.md @@ -302,13 +302,12 @@ const shared = secp256k1.getSharedSecret(key, someonesPubkey); export type CurveFn = { CURVE: ReturnType; getPublicKey: (privateKey: PrivKey, isCompressed?: boolean) => Uint8Array; - getSharedSecret: (privateA: PrivKey, publicB: PubKey, isCompressed?: boolean) => Uint8Array; + getSharedSecret: (privateA: PrivKey, publicB: Hex, isCompressed?: boolean) => Uint8Array; sign: (msgHash: Hex, privKey: PrivKey, opts?: SignOpts) => SignatureType; - signUnhashed: (msg: Uint8Array, privKey: PrivKey, opts?: SignOpts) => SignatureType; verify: ( signature: Hex | SignatureType, msgHash: Hex, - publicKey: PubKey, + publicKey: Hex, opts?: { lowS?: boolean } ) => boolean; Point: PointConstructor; diff --git a/src/abstract/edwards.ts b/src/abstract/edwards.ts index 7acb010..5f24799 100644 --- a/src/abstract/edwards.ts +++ b/src/abstract/edwards.ts @@ -419,7 +419,7 @@ export function twistedEdwards(curveDef: CurveType): CurveFn { const wnaf = wNAF(ExtendedPoint, CURVE.nByteLength * 8); function assertExtPoint(other: unknown) { - if (!(other instanceof ExtendedPoint)) throw new TypeError('ExtendedPoint expected'); + if (!(other instanceof ExtendedPoint)) throw new Error('ExtendedPoint expected'); } // Little-endian SHA512 with modulo n function modnLE(hash: Uint8Array): bigint { diff --git a/src/abstract/utils.ts b/src/abstract/utils.ts index 5d0a6aa..d34210f 100644 --- a/src/abstract/utils.ts +++ b/src/abstract/utils.ts @@ -84,14 +84,14 @@ export function numberToHexUnpadded(num: number | bigint): string { } export function hexToNumber(hex: string): bigint { - if (!str(hex)) throw new TypeError('hexToNumber: expected string, got ' + typeof hex); + if (!str(hex)) throw new Error('hexToNumber: expected string, got ' + typeof hex); // Big Endian return BigInt(`0x${hex}`); } // Caching slows it down 2-3x export function hexToBytes(hex: string): Uint8Array { - if (!str(hex)) throw new TypeError('hexToBytes: expected string, got ' + typeof hex); + if (!str(hex)) throw new Error('hexToBytes: expected string, got ' + typeof hex); if (hex.length % 2) throw new Error('hexToBytes: received invalid unpadded hex ' + hex.length); const array = new Uint8Array(hex.length / 2); for (let i = 0; i < array.length; i++) { diff --git a/src/ed25519.ts b/src/ed25519.ts index b71bd77..377d44a 100644 --- a/src/ed25519.ts +++ b/src/ed25519.ts @@ -240,7 +240,7 @@ export const x25519 = montgomery({ }); function assertRstPoint(other: unknown) { - if (!(other instanceof RistrettoPoint)) throw new TypeError('RistrettoPoint expected'); + if (!(other instanceof RistrettoPoint)) throw new Error('RistrettoPoint expected'); } // √(-1) aka √(a) aka 2^((p-1)/4) const SQRT_M1 = BigInt( diff --git a/src/p521.ts b/src/p521.ts index 1525983..7af0928 100644 --- a/src/p521.ts +++ b/src/p521.ts @@ -38,8 +38,7 @@ export const P521 = createCurve({ Gy: BigInt('0x011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650'), h: BigInt(1), lowS: false, - // P521 keys could be 130, 131, 132 bytes - which doesn't play nicely. - // We ensure all keys are 132 bytes. + // P521 keys could be 130, 131, 132 bytes. We normalize to 132 bytes. // Does not replace validation; invalid keys would still be rejected. normalizePrivateKey(key: PrivKey) { if (typeof key === 'bigint') return key; @@ -47,7 +46,7 @@ export const P521 = createCurve({ if (typeof key !== 'string' || !([130, 131, 132].includes(key.length))) { throw new Error('Invalid key'); } - return key.padStart(66 * 2, '0'); + return key.padStart(66 * 2, '0'); // ensure it's always 132 bytes }, } as const, sha512); export const secp521r1 = P521; diff --git a/src/secp256k1.ts b/src/secp256k1.ts index 430ea75..d9af787 100644 --- a/src/secp256k1.ts +++ b/src/secp256k1.ts @@ -230,7 +230,7 @@ class SchnorrSignature { const bytes = ensureBytes(hex); const len = 32; // group length if (bytes.length !== 2 * len) - throw new TypeError( + throw new Error( `SchnorrSignature.fromHex: expected ${2 * len} bytes, not ${bytes.length}` ); const r = bytesToNumberBE(bytes.subarray(0, len)); @@ -266,12 +266,12 @@ function schnorrSign( privateKey: PrivKey, auxRand: Hex = randomBytes(32) ): Uint8Array { - if (message == null) throw new TypeError(`sign: Expected valid message, not "${message}"`); + if (message == null) throw new Error(`sign: Expected valid message, not "${message}"`); const m = ensureBytes(message); // checks for isWithinCurveOrder const { x: px, scalar: d } = schnorrGetScalar(normalizePrivateKey(privateKey)); const rand = ensureBytes(auxRand); - if (rand.length !== 32) throw new TypeError('sign: Expected 32 bytes of aux randomness'); + if (rand.length !== 32) throw new Error('sign: Expected 32 bytes of aux randomness'); const tag = taggedHash; const t0h = tag(TAGS.aux, rand); const t = numTo32b(d ^ bytesToNumberBE(t0h)); @@ -301,7 +301,7 @@ function schnorrVerify(signature: Hex, message: Hex, publicKey: Hex): boolean { // Finalize // R = s⋅G - e⋅P // -eP == (n-e)P - const R = secp256k1.ProjectivePoint.BASE.multiplyAndAddUnsafe( + const R = secp256k1.ProjectivePoint.BASE.mulAddQUnsafe( P, normalizePrivateKey(s), mod(-e, secp256k1.CURVE.n) diff --git a/src/stark.ts b/src/stark.ts index d7ac9d4..e42a603 100644 --- a/src/stark.ts +++ b/src/stark.ts @@ -62,7 +62,7 @@ export const starkCurve = weierstrass({ // Custom Starknet type conversion functions that can handle 0x and unpadded hex function hexToBytes0x(hex: string): Uint8Array { if (typeof hex !== 'string') { - throw new TypeError('hexToBytes: expected string, got ' + typeof hex); + throw new Error('hexToBytes: expected string, got ' + typeof hex); } hex = strip0x(hex); if (hex.length & 1) hex = '0' + hex; // padding @@ -79,7 +79,7 @@ function hexToBytes0x(hex: string): Uint8Array { } function hexToNumber0x(hex: string): bigint { if (typeof hex !== 'string') { - throw new TypeError('hexToNumber: expected string, got ' + typeof hex); + throw new Error('hexToNumber: expected string, got ' + typeof hex); } // Big Endian // TODO: strip vs no strip? diff --git a/test/ed25519.test.js b/test/ed25519.test.js index 40b967c..79607c1 100644 --- a/test/ed25519.test.js +++ b/test/ed25519.test.js @@ -29,7 +29,7 @@ describe('ed25519', () => { function utf8ToBytes(str) { if (typeof str !== 'string') { - throw new TypeError(`utf8ToBytes expected string, got ${typeof str}`); + throw new Error(`utf8ToBytes expected string, got ${typeof str}`); } return new TextEncoder().encode(str); } diff --git a/test/secp256k1.test.js b/test/secp256k1.test.js index 88e6731..ad9338b 100644 --- a/test/secp256k1.test.js +++ b/test/secp256k1.test.js @@ -25,7 +25,7 @@ const toBEHex = (n) => n.toString(16).padStart(64, '0'); function hexToNumber(hex) { if (typeof hex !== 'string') { - throw new TypeError('hexToNumber: expected string, got ' + typeof hex); + throw new Error('hexToNumber: expected string, got ' + typeof hex); } // Big Endian return BigInt(`0x${hex}`);