diff --git a/src/abstract/utils.ts b/src/abstract/utils.ts index b6768fc..afc6135 100644 --- a/src/abstract/utils.ts +++ b/src/abstract/utils.ts @@ -40,19 +40,24 @@ export type BasicCurve = { allowInfinityPoint?: boolean; }; +// Bans floats and integers above 2^53-1 +export function isPositiveInt(num: any): num is number { + return typeof num === 'number' && Number.isSafeInteger(num) && num > 0; +} + export function validateOpts(curve: BasicCurve & T) { mod.validateField(curve.Fp); for (const i of ['n', 'h'] as const) { - if (typeof curve[i] !== 'bigint') - throw new Error(`Invalid curve param ${i}=${curve[i]} (${typeof curve[i]})`); + const val = curve[i]; + if (typeof val !== 'bigint') throw new Error(`Invalid curve param ${i}=${val} (${typeof val})`); } if (!curve.Fp.isValid(curve.Gx)) throw new Error('Invalid generator X coordinate Fp element'); if (!curve.Fp.isValid(curve.Gy)) throw new Error('Invalid generator Y coordinate Fp element'); for (const i of ['nBitLength', 'nByteLength'] as const) { - if (curve[i] === undefined) continue; // Optional - if (!Number.isSafeInteger(curve[i])) - throw new Error(`Invalid curve param ${i}=${curve[i]} (${typeof curve[i]})`); + const val = curve[i]; + if (val === undefined) continue; // Optional + if (!isPositiveInt(val)) throw new Error(`Invalid curve param ${i}=${val} (${typeof val})`); } // Set defaults return Object.freeze({ ...nLength(curve.n, curve.nBitLength), ...curve } as const); diff --git a/src/ed448.ts b/src/ed448.ts index c96fc49..122c755 100644 --- a/src/ed448.ts +++ b/src/ed448.ts @@ -138,7 +138,8 @@ const ED448_DEF = { ), // Finite field 𝔽p over which we'll do calculations; 2n ** 448n - 2n ** 224n - 1n Fp, - // Subgroup order: how many points ed448 has; 2n**446n - 13818066809895115352007386748515426880336692474882178609894547503885n + // Subgroup order: how many points curve has; + // 2n**446n - 13818066809895115352007386748515426880336692474882178609894547503885n n: BigInt( '181709681073901722637330951972001133588410340171829515070372549795146003961539585716195755291692375963310293709091662304773755859649779' ),