UNPKG

@bsv/sdk

Version:

BSV Blockchain Software Development Kit

362 lines 14.5 kB
import BasePoint from './BasePoint.js'; import JPoint from './JacobianPoint.js'; import BigNumber from './BigNumber.js'; export declare const BI_ZERO = 0n; export declare const BI_ONE = 1n; export declare const BI_TWO = 2n; export declare const BI_THREE = 3n; export declare const BI_FOUR = 4n; export declare const BI_EIGHT = 8n; export declare const P_BIGINT = 115792089237316195423570985008687907853269984665640564039457584007908834671663n; export declare const N_BIGINT = 115792089237316195423570985008687907852837564279074904382605163141518161494337n; export declare const MASK_256: bigint; export declare function red(x: bigint): bigint; export declare const biMod: (a: bigint) => bigint; export declare const biModSub: (a: bigint, b: bigint) => bigint; export declare const biModMul: (a: bigint, b: bigint) => bigint; export declare const biModAdd: (a: bigint, b: bigint) => bigint; export declare const biModInv: (a: bigint) => bigint; export declare const biModSqr: (a: bigint) => bigint; export declare const biModPow: (base: bigint, exp: bigint) => bigint; export declare const P_PLUS1_DIV4: bigint; export declare const biModSqrt: (a: bigint) => bigint | null; export declare const GX_BIGINT: bigint; export declare const GY_BIGINT: bigint; export interface JacobianPointBI { X: bigint; Y: bigint; Z: bigint; } export declare const jpDouble: (P: JacobianPointBI) => JacobianPointBI; export declare const jpAdd: (P: JacobianPointBI, Q: JacobianPointBI) => JacobianPointBI; export declare const jpNeg: (P: JacobianPointBI) => JacobianPointBI; export declare const scalarMultiplyWNAF: (k: bigint, P0: { x: bigint; y: bigint; }, window?: number) => JacobianPointBI; export declare const modN: (a: bigint) => bigint; export declare const modMulN: (a: bigint, b: bigint) => bigint; /** modular inverse modulo n with plain extended‑gcd (not constant‑time) */ export declare const modInvN: (a: bigint) => bigint; /** * `Point` class is a representation of an elliptic curve point with affine coordinates. * It extends the functionality of BasePoint and carries x, y coordinates of point on the curve. * It also introduces new methods for handling Point operations in elliptic curve. * * @class Point * @extends {BasePoint} * * @property x - The x-coordinate of the point. * @property y - The y-coordinate of the point. * @property inf - Flag to record if the point is at infinity in the Elliptic Curve. */ export default class Point extends BasePoint { x: BigNumber | null; y: BigNumber | null; inf: boolean; /** * Creates a point object from a given Array. These numbers can represent coordinates in hex format, or points * in multiple established formats. * The function verifies the integrity of the provided data and throws errors if inconsistencies are found. * * @method fromDER * @static * @param bytes - The point representation number array. * @returns Returns a new point representing the given string. * @throws `Error` If the point number[] value has a wrong length. * @throws `Error` If the point format is unknown. * * @example * const derPoint = [ 2, 18, 123, 108, 125, 83, 1, 251, 164, 214, 16, 119, 200, 216, 210, 193, 251, 193, 129, 67, 97, 146, 210, 216, 77, 254, 18, 6, 150, 190, 99, 198, 128 ]; * const point = Point.fromDER(derPoint); */ static fromDER(bytes: number[]): Point; /** * Creates a point object from a given string. This string can represent coordinates in hex format, or points * in multiple established formats. * The function verifies the integrity of the provided data and throws errors if inconsistencies are found. * * @method fromString * @static * * @param str The point representation string. * @returns Returns a new point representing the given string. * @throws `Error` If the point string value has a wrong length. * @throws `Error` If the point format is unknown. * * @example * const pointStr = 'abcdef'; * const point = Point.fromString(pointStr); */ static fromString(str: string): Point; /** * Generates a point from an x coordinate and a boolean indicating whether the corresponding * y coordinate is odd. * * @method fromX * @static * @param x - The x coordinate of the point. * @param odd - Boolean indicating whether the corresponding y coordinate is odd or not. * @returns Returns the new point. * @throws `Error` If the point is invalid. * * @example * const xCoordinate = new BigNumber('10'); * const point = Point.fromX(xCoordinate, true); */ static fromX(x: BigNumber | number | number[] | string, odd: boolean): Point; /** * Generates a point from a serialized JSON object. The function accounts for different options in the JSON object, * including precomputed values for optimization of EC operations, and calls another helper function to turn nested * JSON points into proper Point objects. * * @method fromJSON * @static * @param obj - An object or array that holds the data for the point. * @param isRed - A boolean to direct how the Point is constructed from the JSON object. * @returns Returns a new point based on the deserialized JSON object. * * @example * const serializedPoint = '{"x":52,"y":15}'; * const point = Point.fromJSON(serializedPoint, true); */ static fromJSON(obj: string | any[], isRed: boolean): Point; /** * @constructor * @param x - The x-coordinate of the point. May be a number, a BigNumber, a string (which will be interpreted as hex), a number array, or null. If null, an "Infinity" point is constructed. * @param y - The y-coordinate of the point, similar to x. * @param isRed - A boolean indicating if the point is a member of the field of integers modulo the k256 prime. Default is true. * * @example * new Point('abc123', 'def456'); * new Point(null, null); // Generates Infinity point. */ constructor(x: BigNumber | number | number[] | string | null, y: BigNumber | number | number[] | string | null, isRed?: boolean); /** * Validates if a point belongs to the curve. Follows the short Weierstrass * equation for elliptic curves: y^2 = x^3 + ax + b. * * @method validate * @returns {boolean} true if the point is on the curve, false otherwise. * * @example * const aPoint = new Point(x, y); * const isValid = aPoint.validate(); */ validate(): boolean; /** * Encodes the coordinates of a point into an array or a hexadecimal string. * The details of encoding are determined by the optional compact and enc parameters. * * @method encode * @param compact - If true, an additional prefix byte 0x02 or 0x03 based on the 'y' coordinate being even or odd respectively is used. If false, byte 0x04 is used. * @param enc - Expects the string 'hex' if hexadecimal string encoding is required instead of an array of numbers. * @throws Will throw an error if the specified encoding method is not recognized. Expects 'hex'. * @returns If enc is undefined, a byte array representation of the point will be returned. if enc is 'hex', a hexadecimal string representation of the point will be returned. * * @example * const aPoint = new Point(x, y); * const encodedPointArray = aPoint.encode(); * const encodedPointHex = aPoint.encode(true, 'hex'); */ encode(compact?: boolean, enc?: 'hex'): number[] | string; /** * Converts the point coordinates to a hexadecimal string. A wrapper method * for encode. Byte 0x02 or 0x03 is used as prefix based on the 'y' coordinate being even or odd respectively. * * @method toString * @returns {string} A hexadecimal string representation of the point coordinates. * * @example * const aPoint = new Point(x, y); * const stringPoint = aPoint.toString(); */ toString(): string; /** * Exports the x and y coordinates of the point, and the precomputed doubles and non-adjacent form (NAF) for optimization. The output is an array. * * @method toJSON * @returns An Array where first two elements are the coordinates of the point and optional third element is an object with doubles and NAF points. * * @example * const aPoint = new Point(x, y); * const jsonPoint = aPoint.toJSON(); */ toJSON(): [ BigNumber | null, BigNumber | null, { doubles: { step: any; points: any[]; } | undefined; naf: { wnd: any; points: any[]; } | undefined; }? ]; /** * Provides the point coordinates in a human-readable string format for debugging purposes. * * @method inspect * @returns String of the format '<EC Point x: x-coordinate y: y-coordinate>', or '<EC Point Infinity>' if the point is at infinity. * * @example * const aPoint = new Point(x, y); * console.log(aPoint.inspect()); */ inspect(): string; /** * Checks if the point is at infinity. * @method isInfinity * @returns Returns whether or not the point is at infinity. * * @example * const p = new Point(null, null); * console.log(p.isInfinity()); // outputs: true */ isInfinity(): boolean; /** * Adds another Point to this Point, returning a new Point. * * @method add * @param p - The Point to add to this one. * @returns A new Point that results from the addition. * * @example * const p1 = new Point(1, 2); * const p2 = new Point(2, 3); * const result = p1.add(p2); */ add(p: Point): Point; /** * Doubles the current point. * * @method dbl * * @example * const P = new Point('123', '456'); * const result = P.dbl(); * */ dbl(): Point; /** * Returns X coordinate of point * * @example * const P = new Point('123', '456'); * const x = P.getX(); */ getX(): BigNumber; /** * Returns X coordinate of point * * @example * const P = new Point('123', '456'); * const x = P.getX(); */ getY(): BigNumber; /** * Multiplies this Point by a scalar value, returning a new Point. * * @method mul * @param k - The scalar value to multiply this Point by. * @returns A new Point that results from the multiplication. * * @example * const p = new Point(1, 2); * const result = p.mul(2); // this doubles the Point */ mul(k: BigNumber | number | number[] | string): Point; /** * Performs a multiplication and addition operation in a single step. * Multiplies this Point by k1, adds the resulting Point to the result of p2 multiplied by k2. * * @method mulAdd * @param k1 - The scalar value to multiply this Point by. * @param p2 - The other Point to be involved in the operation. * @param k2 - The scalar value to multiply the Point p2 by. * @returns A Point that results from the combined multiplication and addition operations. * * @example * const p1 = new Point(1, 2); * const p2 = new Point(2, 3); * const result = p1.mulAdd(2, p2, 3); */ mulAdd(k1: BigNumber, p2: Point, k2: BigNumber): Point; /** * Performs the Jacobian multiplication and addition operation in a single * step. Instead of returning a regular Point, the result is a JacobianPoint. * * @method jmulAdd * @param k1 - The scalar value to multiply this Point by. * @param p2 - The other Point to be involved in the operation * @param k2 - The scalar value to multiply the Point p2 by. * @returns A JacobianPoint that results from the combined multiplication and addition operation. * * @example * const p1 = new Point(1, 2); * const p2 = new Point(2, 3); * const result = p1.jmulAdd(2, p2, 3); */ jmulAdd(k1: BigNumber, p2: Point, k2: BigNumber): JPoint; /** * Checks if the Point instance is equal to another given Point. * * @method eq * @param p - The Point to be checked if equal to the current instance. * * @returns Whether the two Point instances are equal. Both the 'x' and 'y' coordinates have to match, and both points have to either be valid or at infinity for equality. If both conditions are true, it returns true, else it returns false. * * @example * const p1 = new Point(5, 20); * const p2 = new Point(5, 20); * const areEqual = p1.eq(p2); // returns true */ eq(p: Point): boolean; /** * Negate a point. The negation of a point P is the mirror of P about x-axis. * * @method neg * * @example * const P = new Point('123', '456'); * const result = P.neg(); */ neg(_precompute?: boolean): Point; /** * Performs the "doubling" operation on the Point a given number of times. * This is used in elliptic curve operations to perform multiplication by 2, multiple times. * If the point is at infinity, it simply returns the point because doubling * a point at infinity is still infinity. * * @method dblp * @param k - The number of times the "doubling" operation is to be performed on the Point. * @returns The Point after 'k' "doubling" operations have been performed. * * @example * const p = new Point(5, 20); * const doubledPoint = p.dblp(10); // returns the point after "doubled" 10 times */ dblp(k: number): Point; /** * Converts the point to a Jacobian point. If the point is at infinity, the corresponding Jacobian point * will also be at infinity. * * @method toJ * @returns Returns a new Jacobian point based on the current point. * * @example * const point = new Point(xCoordinate, yCoordinate); * const jacobianPoint = point.toJ(); */ toJ(): JPoint; private _getBeta; private _fixedNafMul; private _wnafMulAdd; private _endoWnafMulAdd; private _hasDoubles; private _getDoubles; private _getNAFPoints; } //# sourceMappingURL=Point.d.ts.map