/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ import { IField } from './modular.js'; export type AffinePoint = { x: T; y: T; } & { z?: never; t?: never; }; export interface Group> { double(): T; negate(): T; add(other: T): T; subtract(other: T): T; equals(other: T): boolean; multiply(scalar: bigint): T; } export type GroupConstructor = { BASE: T; ZERO: T; }; export type Mapper = (i: T[]) => T[]; export type IWNAF> = { constTimeNegate: >(condition: boolean, item: T) => T; hasPrecomputes(elm: T): boolean; unsafeLadder(elm: T, n: bigint, p?: T): T; precomputeWindow(elm: T, W: number): Group[]; wNAF(W: number, precomputes: T[], n: bigint): { p: T; f: T; }; wNAFUnsafe(W: number, precomputes: T[], n: bigint, acc?: T): T; getPrecomputes(W: number, P: T, transform: Mapper): T[]; wNAFCached(P: T, n: bigint, transform: Mapper): { p: T; f: T; }; wNAFCachedUnsafe(P: T, n: bigint, transform: Mapper, prev?: T): T; setWindowSize(P: T, W: number): void; }; /** * Elliptic curve multiplication of Point by scalar. Fragile. * Scalars should always be less than curve order: this should be checked inside of a curve itself. * Creates precomputation tables for fast multiplication: * - private scalar is split by fixed size windows of W bits * - every window point is collected from window's table & added to accumulator * - since windows are different, same point inside tables won't be accessed more than once per calc * - each multiplication is 'Math.ceil(CURVE_ORDER / 𝑊) + 1' point additions (fixed for any scalar) * - +1 window is neccessary for wNAF * - wNAF reduces table size: 2x less memory + 2x faster generation, but 10% slower multiplication * * @todo Research returning 2d JS array of windows, instead of a single window. * This would allow windows to be in different memory locations */ export declare function wNAF>(c: GroupConstructor, bits: number): IWNAF; export type BasicCurve = { Fp: IField; n: bigint; nBitLength?: number; nByteLength?: number; h: bigint; hEff?: bigint; Gx: T; Gy: T; allowInfinityPoint?: boolean; }; export declare function validateBasic(curve: BasicCurve & T): Readonly<{ readonly nBitLength: number; readonly nByteLength: number; } & BasicCurve & T & { p: bigint; }>; /** * Pippenger algorithm for multi-scalar multiplication (MSM, Pa + Qb + Rc + ...). * 30x faster vs naive addition on L=4096, 10x faster with precomputes. * For N=254bit, L=1, it does: 1024 ADD + 254 DBL. For L=5: 1536 ADD + 254 DBL. * Algorithmically constant-time (for same L), even when 1 point + scalar, or when scalar = 0. * @param c Curve Point constructor * @param fieldN field over CURVE.N - important that it's not over CURVE.P * @param points array of L curve points * @param scalars array of L scalars (aka private keys / bigints) */ export declare function pippenger>(c: GroupConstructor, fieldN: IField, points: T[], scalars: bigint[]): T;