utils: new util; ed448: small adjustment

This commit is contained in:
Paul Miller 2023-01-12 19:39:43 +00:00
parent bfe929aac3
commit e45d7c2d25
No known key found for this signature in database
GPG Key ID: 697079DA6878B89B
2 changed files with 12 additions and 6 deletions

@ -40,19 +40,24 @@ export type BasicCurve<T> = {
allowInfinityPoint?: boolean; 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<FP, T>(curve: BasicCurve<FP> & T) { export function validateOpts<FP, T>(curve: BasicCurve<FP> & T) {
mod.validateField(curve.Fp); mod.validateField(curve.Fp);
for (const i of ['n', 'h'] as const) { for (const i of ['n', 'h'] as const) {
if (typeof curve[i] !== 'bigint') const val = curve[i];
throw new Error(`Invalid curve param ${i}=${curve[i]} (${typeof 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.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'); if (!curve.Fp.isValid(curve.Gy)) throw new Error('Invalid generator Y coordinate Fp element');
for (const i of ['nBitLength', 'nByteLength'] as const) { for (const i of ['nBitLength', 'nByteLength'] as const) {
if (curve[i] === undefined) continue; // Optional const val = curve[i];
if (!Number.isSafeInteger(curve[i])) if (val === undefined) continue; // Optional
throw new Error(`Invalid curve param ${i}=${curve[i]} (${typeof curve[i]})`); if (!isPositiveInt(val)) throw new Error(`Invalid curve param ${i}=${val} (${typeof val})`);
} }
// Set defaults // Set defaults
return Object.freeze({ ...nLength(curve.n, curve.nBitLength), ...curve } as const); return Object.freeze({ ...nLength(curve.n, curve.nBitLength), ...curve } as const);

@ -138,7 +138,8 @@ const ED448_DEF = {
), ),
// Finite field 𝔽p over which we'll do calculations; 2n ** 448n - 2n ** 224n - 1n // Finite field 𝔽p over which we'll do calculations; 2n ** 448n - 2n ** 224n - 1n
Fp, Fp,
// Subgroup order: how many points ed448 has; 2n**446n - 13818066809895115352007386748515426880336692474882178609894547503885n // Subgroup order: how many points curve has;
// 2n**446n - 13818066809895115352007386748515426880336692474882178609894547503885n
n: BigInt( n: BigInt(
'181709681073901722637330951972001133588410340171829515070372549795146003961539585716195755291692375963310293709091662304773755859649779' '181709681073901722637330951972001133588410340171829515070372549795146003961539585716195755291692375963310293709091662304773755859649779'
), ),