forked from tornado-packages/noble-curves
Change TypeError to Error
This commit is contained in:
parent
0422e6ef38
commit
849dc38f3c
@ -302,13 +302,12 @@ const shared = secp256k1.getSharedSecret(key, someonesPubkey);
|
||||
export type CurveFn = {
|
||||
CURVE: ReturnType<typeof validateOpts>;
|
||||
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;
|
||||
|
@ -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 {
|
||||
|
@ -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++) {
|
||||
|
@ -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(
|
||||
|
@ -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;
|
||||
|
@ -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)
|
||||
|
@ -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?
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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}`);
|
||||
|
Loading…
Reference in New Issue
Block a user