@bsv/sdk
Version:
BSV Blockchain Software Development Kit
2,074 lines (1,471 loc) • 167 kB
Markdown
# API
Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
## Interfaces
| |
| --- |
| [JacobianPointBI](#interface-jacobianpointbi) |
| [SignatureHashCache](#interface-signaturehashcache) |
Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
---
### Interface: JacobianPointBI
```ts
export interface JacobianPointBI {
X: bigint;
Y: bigint;
Z: bigint;
}
```
Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
---
### Interface: SignatureHashCache
```ts
export interface SignatureHashCache {
hashPrevouts?: number[];
hashSequence?: number[];
hashOutputsAll?: number[];
hashOutputsSingle?: Map<number, number[]>;
}
```
Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
---
## Classes
| | | |
| --- | --- | --- |
| [BasePoint](#class-basepoint) | [PointInFiniteField](#class-pointinfinitefield) | [SHA256HMAC](#class-sha256hmac) |
| [BigNumber](#class-bignumber) | [Polynomial](#class-polynomial) | [SHA512](#class-sha512) |
| [Curve](#class-curve) | [PrivateKey](#class-privatekey) | [SHA512HMAC](#class-sha512hmac) |
| [DRBG](#class-drbg) | [PublicKey](#class-publickey) | [Schnorr](#class-schnorr) |
| [JacobianPoint](#class-jacobianpoint) | [RIPEMD160](#class-ripemd160) | [Secp256r1](#class-secp256r1) |
| [K256](#class-k256) | [Reader](#class-reader) | [Signature](#class-signature) |
| [KeyShares](#class-keyshares) | [ReductionContext](#class-reductioncontext) | [SymmetricKey](#class-symmetrickey) |
| [Mersenne](#class-mersenne) | [SHA1](#class-sha1) | [TransactionSignature](#class-transactionsignature) |
| [MontgomoryMethod](#class-montgomorymethod) | [SHA1HMAC](#class-sha1hmac) | [Writer](#class-writer) |
| [Point](#class-point) | [SHA256](#class-sha256) | |
Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
---
### Class: BasePoint
Base class for Point (affine coordinates) and JacobianPoint classes,
defining their curve and type.
```ts
export default abstract class BasePoint {
curve: Curve;
type: "affine" | "jacobian";
precomputed: {
doubles?: {
step: number;
points: BasePoint[];
};
naf?: {
wnd: number;
points: BasePoint[];
};
beta?: BasePoint | null;
} | null;
constructor(type: "affine" | "jacobian")
}
```
See also: [Curve](./primitives.md#class-curve)
Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
---
### Class: BigNumber
JavaScript numbers are only precise up to 53 bits. Since Bitcoin relies on
256-bit cryptography, this BigNumber class enables operations on larger
numbers.
```ts
export default class BigNumber {
public static readonly zeros: string[]
static readonly groupSizes: number[]
static readonly groupBases: number[]
static readonly wordSize: number = 26;
public red: ReductionContext | null;
public get negative(): number
public set negative(val: number)
public get words(): number[]
public set words(newWords: number[])
public get length(): number
static isBN(num: any): boolean
static max(left: BigNumber, right: BigNumber): BigNumber
static min(left: BigNumber, right: BigNumber): BigNumber
constructor(number: number | string | number[] | bigint | undefined = 0, base: number | "be" | "le" | "hex" = 10, endian: "be" | "le" = "be")
copy(dest: BigNumber): void
static move(dest: BigNumber, src: BigNumber): void
clone(): BigNumber
expand(size: number): this
strip(): this
normSign(): this { if (this._magnitude === 0n)
this._sign = 0; return this; }
inspect(): string
toString(base: number | "hex" = 10, padding: number = 1): string
toNumber(): number
toBigInt(): bigint
toJSON(): string
toArray(endian: "le" | "be" = "be", length?: number): number[]
bitLength(): number { if (this._magnitude === 0n)
return 0; return this._magnitude.toString(2).length; }
static toBitArray(num: BigNumber): Array<0 | 1>
toBitArray(): Array<0 | 1>
zeroBits(): number
byteLength(): number { if (this._magnitude === 0n)
return 0; return Math.ceil(this.bitLength() / 8); }
toTwos(width: number): BigNumber
fromTwos(width: number): BigNumber
isNeg(): boolean
neg(): BigNumber
ineg(): this { if (this._magnitude !== 0n)
this._sign = this._sign === 1 ? 0 : 1; return this; }
iuor(num: BigNumber): this
iuand(num: BigNumber): this
iuxor(num: BigNumber): this
ior(num: BigNumber): this
iand(num: BigNumber): this
ixor(num: BigNumber): this
or(num: BigNumber): BigNumber
uor(num: BigNumber): BigNumber
and(num: BigNumber): BigNumber
uand(num: BigNumber): BigNumber
xor(num: BigNumber): BigNumber
uxor(num: BigNumber): BigNumber
inotn(width: number): this
notn(width: number): BigNumber
setn(bit: number, val: any): this { this.assert(typeof bit === "number" && bit >= 0); const Bb = BigInt(bit); if (val === 1 || val === true)
this._magnitude |= (1n << Bb);
else
this._magnitude &= ~(1n << Bb); const wnb = Math.floor(bit / BigNumber.wordSize) + 1; this._nominalWordLength = Math.max(this._nominalWordLength, wnb); this._finishInitialization(); return this.strip(); }
iadd(num: BigNumber): this
add(num: BigNumber): BigNumber
isub(num: BigNumber): this
sub(num: BigNumber): BigNumber
mul(num: BigNumber): BigNumber
imul(num: BigNumber): this
imuln(num: number): this
muln(num: number): BigNumber
sqr(): BigNumber
isqr(): this
pow(num: BigNumber): BigNumber
iushln(bits: number | bigint): this
ishln(bits: number | bigint): this
iushrn(bits: number | bigint, hint?: number, extended?: BigNumber): this
ishrn(bits: number | bigint, hint?: number, extended?: BigNumber): this
shln(bits: number | bigint): BigNumber
ushln(bits: number | bigint): BigNumber
shrn(bits: number | bigint): BigNumber
ushrn(bits: number | bigint): BigNumber
testn(bit: number): boolean
imaskn(bits: number): this
maskn(bits: number): BigNumber
iaddn(num: number): this
_iaddn(num: number): this
isubn(num: number): this
addn(num: number): BigNumber
subn(num: number): BigNumber
iabs(): this
abs(): BigNumber
divmod(num: BigNumber, mode?: "div" | "mod", positive?: boolean): any
div(num: BigNumber): BigNumber
mod(num: BigNumber): BigNumber
umod(num: BigNumber): BigNumber
divRound(num: BigNumber): BigNumber
modrn(numArg: number): number
idivn(num: number): this
divn(num: number): BigNumber
egcd(p: BigNumber): {
a: BigNumber;
b: BigNumber;
gcd: BigNumber;
}
gcd(num: BigNumber): BigNumber
invm(num: BigNumber): BigNumber
isEven(): boolean
isOdd(): boolean
andln(num: number): number
bincn(bit: number): this
isZero(): boolean
cmpn(num: number): 1 | 0 | -1 { this.assert(Math.abs(num) <= BigNumber.MAX_IMULN_ARG, "Number is too big"); const tV = this._getSignedValue(); const nV = BigInt(num); if (tV < nV)
return -1; if (tV > nV)
return 1; return 0; }
cmp(num: BigNumber): 1 | 0 | -1 { const tV = this._getSignedValue(); const nV = num._getSignedValue(); if (tV < nV)
return -1; if (tV > nV)
return 1; return 0; }
ucmp(num: BigNumber): 1 | 0 | -1 { if (this._magnitude < num._magnitude)
return -1; if (this._magnitude > num._magnitude)
return 1; return 0; }
gtn(num: number): boolean
gt(num: BigNumber): boolean
gten(num: number): boolean
gte(num: BigNumber): boolean
ltn(num: number): boolean
lt(num: BigNumber): boolean
lten(num: number): boolean
lte(num: BigNumber): boolean
eqn(num: number): boolean
eq(num: BigNumber): boolean
toRed(ctx: ReductionContext): BigNumber
fromRed(): BigNumber
forceRed(ctx: ReductionContext): this
redAdd(num: BigNumber): BigNumber
redIAdd(num: BigNumber): BigNumber
redSub(num: BigNumber): BigNumber
redISub(num: BigNumber): BigNumber
redShl(num: number): BigNumber
redMul(num: BigNumber): BigNumber
redIMul(num: BigNumber): BigNumber
redSqr(): BigNumber
redISqr(): BigNumber
redSqrt(): BigNumber
redInvm(): BigNumber
redNeg(): BigNumber
redPow(num: BigNumber): BigNumber
static fromHex(hex: string, endian?: "le" | "be" | "little" | "big"): BigNumber
toHex(byteLength: number = 0): string
static fromJSON(str: string): BigNumber
static fromNumber(n: number): BigNumber
static fromString(str: string, base?: number | "hex"): BigNumber
static fromSm(bytes: number[], endian: "big" | "little" = "big"): BigNumber
toSm(endian: "big" | "little" = "big"): number[]
static fromBits(bits: number, strict: boolean = false): BigNumber
toBits(): number
static fromScriptNum(num: number[], requireMinimal: boolean = false, maxNumSize?: number): BigNumber
toScriptNum(): number[]
_invmp(p: BigNumber): BigNumber
mulTo(num: BigNumber, out: BigNumber): BigNumber
}
```
See also: [ReductionContext](./primitives.md#class-reductioncontext), [red](./primitives.md#function-red), [toArray](./primitives.md#variable-toarray), [toHex](./primitives.md#variable-tohex)
#### Constructor
```ts
constructor(number: number | string | number[] | bigint | undefined = 0, base: number | "be" | "le" | "hex" = 10, endian: "be" | "le" = "be")
```
Argument Details
+ **number**
+ The number (various types accepted) to construct a BigNumber from. Default is 0.
+ **base**
+ The base of number provided. By default is 10.
+ **endian**
+ The endianness provided. By default is 'big endian'.
#### Property red
Reduction context of the big number.
```ts
public red: ReductionContext | null
```
See also: [ReductionContext](./primitives.md#class-reductioncontext)
#### Property wordSize
The word size of big number chunks.
```ts
static readonly wordSize: number = 26
```
Example
```ts
console.log(BigNumber.wordSize); // output: 26
```
#### Method _invmp
Compute the multiplicative inverse of the current BigNumber in the modulus field specified by `p`.
The multiplicative inverse is a number which when multiplied with the current BigNumber gives '1' in the modulus field.
```ts
_invmp(p: BigNumber): BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
Returns
The multiplicative inverse `BigNumber` in the modulus field specified by `p`.
Argument Details
+ **p**
+ The `BigNumber` specifying the modulus field.
#### Method bitLength
Calculates the number of bits required to represent the BigNumber.
```ts
bitLength(): number { if (this._magnitude === 0n)
return 0; return this._magnitude.toString(2).length; }
```
Returns
The bit length of the BigNumber.
#### Method byteLength
Calculates the number of bytes required to represent the BigNumber.
```ts
byteLength(): number { if (this._magnitude === 0n)
return 0; return Math.ceil(this.bitLength() / 8); }
```
Returns
The byte length of the BigNumber.
#### Method fromBits
Creates a BigNumber from a number representing the "bits" value in a block header.
```ts
static fromBits(bits: number, strict: boolean = false): BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
Returns
Returns a BigNumber equivalent to the "bits" value in a block header.
Argument Details
+ **bits**
+ The number representing the bits value in a block header.
+ **strict**
+ If true, an error is thrown if the number has negative bit set.
Throws
Will throw an error if `strict` is `true` and the number has negative bit set.
#### Method fromHex
Creates a BigNumber from a hexadecimal string.
```ts
static fromHex(hex: string, endian?: "le" | "be" | "little" | "big"): BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
Returns
Returns a BigNumber created from the hexadecimal input string.
Argument Details
+ **hex**
+ The hexadecimal string to create a BigNumber from.
+ **endian**
+ Optional endianness for parsing the hex string.
Example
```ts
const exampleHex = 'a1b2c3';
const bigNumber = BigNumber.fromHex(exampleHex);
```
#### Method fromJSON
Creates a BigNumber from a JSON-serialized string.
```ts
static fromJSON(str: string): BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
Returns
Returns a BigNumber created from the JSON input string.
Argument Details
+ **str**
+ The JSON-serialized string to create a BigNumber from.
#### Method fromNumber
Creates a BigNumber from a number.
```ts
static fromNumber(n: number): BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
Returns
Returns a BigNumber equivalent to the input number.
Argument Details
+ **n**
+ The number to create a BigNumber from.
#### Method fromScriptNum
Creates a BigNumber from the format used in Bitcoin scripts.
```ts
static fromScriptNum(num: number[], requireMinimal: boolean = false, maxNumSize?: number): BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
Returns
Returns a BigNumber equivalent to the number used in a Bitcoin script.
Argument Details
+ **num**
+ The number in the format used in Bitcoin scripts.
+ **requireMinimal**
+ If true, non-minimally encoded values will throw an error.
+ **maxNumSize**
+ The maximum allowed size for the number.
#### Method fromSm
Creates a BigNumber from a signed magnitude number.
```ts
static fromSm(bytes: number[], endian: "big" | "little" = "big"): BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
Returns
Returns a BigNumber equivalent to the signed magnitude number interpreted with specified endianess.
Argument Details
+ **bytes**
+ The signed magnitude number to convert to a BigNumber.
+ **endian**
+ Defines endianess. If not provided, big endian is assumed.
#### Method fromString
Creates a BigNumber from a string, considering an optional base.
```ts
static fromString(str: string, base?: number | "hex"): BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
Returns
Returns a BigNumber equivalent to the string after conversion from the specified base.
Argument Details
+ **str**
+ The string to create a BigNumber from.
+ **base**
+ The base used for conversion. If not provided, base 10 is assumed.
#### Method isBN
Checks whether a value is an instance of BigNumber. Regular JS numbers fail this check.
```ts
static isBN(num: any): boolean
```
Returns
- Returns a boolean value determining whether or not the checked num parameter is a BigNumber.
Argument Details
+ **num**
+ The value to be checked.
#### Method max
Returns the bigger value between two BigNumbers
```ts
static max(left: BigNumber, right: BigNumber): BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
Returns
- Returns the bigger BigNumber between left and right.
Argument Details
+ **left**
+ The first BigNumber to be compared.
+ **right**
+ The second BigNumber to be compared.
#### Method min
Returns the smaller value between two BigNumbers
```ts
static min(left: BigNumber, right: BigNumber): BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
Returns
- Returns the smaller value between left and right.
Argument Details
+ **left**
+ The first BigNumber to be compared.
+ **right**
+ The second BigNumber to be compared.
#### Method mulTo
Performs multiplication between the BigNumber instance and a given BigNumber.
It chooses the multiplication method based on the lengths of the numbers to optimize execution time.
```ts
mulTo(num: BigNumber, out: BigNumber): BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
Returns
The BigNumber resulting from the multiplication operation.
Argument Details
+ **num**
+ The BigNumber multiply with.
+ **out**
+ The BigNumber where to store the result.
#### Method toArray
Converts the BigNumber instance to an array of bytes.
```ts
toArray(endian: "le" | "be" = "be", length?: number): number[]
```
Returns
Array of bytes representing the BigNumber.
Argument Details
+ **endian**
+ Endianness of the output array, defaults to 'be'.
+ **length**
+ Optional length of the output array.
#### Method toBigInt
Returns the signed BigInt representation of this BigNumber without any safety checks.
```ts
toBigInt(): bigint
```
Returns
bigint value for this BigNumber.
#### Method toBitArray
Converts a BigNumber to an array of bits.
```ts
static toBitArray(num: BigNumber): Array<0 | 1>
```
See also: [BigNumber](./primitives.md#class-bignumber)
Returns
An array of bits.
Argument Details
+ **num**
+ The BigNumber to convert.
#### Method toBits
Converts this BigNumber to a number representing the "bits" value in a block header.
```ts
toBits(): number
```
Returns
Returns a number equivalent to the "bits" value in a block header.
#### Method toHex
Converts this BigNumber to a hexadecimal string.
```ts
toHex(byteLength: number = 0): string
```
Returns
Returns a string representing the hexadecimal value of this BigNumber.
Argument Details
+ **length**
+ The minimum length of the hex string
Example
```ts
const bigNumber = new BigNumber(255)
const hex = bigNumber.toHex()
```
#### Method toJSON
Converts the BigNumber instance to a JSON-formatted string.
```ts
toJSON(): string
```
Returns
The JSON string representation of the BigNumber instance.
#### Method toNumber
Converts the BigNumber instance to a JavaScript number.
Please note that JavaScript numbers are only precise up to 53 bits.
```ts
toNumber(): number
```
Returns
The JavaScript number representation of the BigNumber instance.
Throws
If the BigNumber instance cannot be safely stored in a JavaScript number
#### Method toScriptNum
Converts this BigNumber to a number in the format used in Bitcoin scripts.
```ts
toScriptNum(): number[]
```
Returns
Returns the equivalent to this BigNumber as a Bitcoin script number.
#### Method toSm
Converts this BigNumber to a signed magnitude number.
```ts
toSm(endian: "big" | "little" = "big"): number[]
```
Returns
Returns an array equivalent to this BigNumber interpreted as a signed magnitude with specified endianess.
Argument Details
+ **endian**
+ Defines endianess. If not provided, big endian is assumed.
#### Method toString
function toString() { [native code] }
Converts the BigNumber instance to a string representation.
```ts
toString(base: number | "hex" = 10, padding: number = 1): string
```
Returns
The string representation of the BigNumber instance
Argument Details
+ **base**
+ The base for representing number. Default is 10. Other accepted values are 16 and 'hex'.
+ **padding**
+ Represents the minimum number of digits to represent the BigNumber as a string. Default is 1.
#### Method zeroBits
Returns the number of trailing zero bits in the big number.
```ts
zeroBits(): number
```
Returns
Returns the number of trailing zero bits
in the binary representation of the big number.
Example
```ts
const bn = new BigNumber('8'); // binary: 1000
const zeroBits = bn.zeroBits(); // 3
```
Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
---
### Class: Curve
```ts
export default class Curve {
p: BigNumber;
red: ReductionContext;
redN: BigNumber | null;
zero: BigNumber;
one: BigNumber;
two: BigNumber;
g: Point;
n: BigNumber;
a: BigNumber;
b: BigNumber;
tinv: BigNumber;
zeroA: boolean;
threeA: boolean;
endo: {
beta: BigNumber;
lambda: BigNumber;
basis: Array<{
a: BigNumber;
b: BigNumber;
}>;
} | undefined;
_endoWnafT1: BigNumber[];
_endoWnafT2: BigNumber[];
_wnafT1: BigNumber[];
_wnafT2: BigNumber[];
_wnafT3: BigNumber[];
_wnafT4: BigNumber[];
_bitLength: number;
static assert(expression: unknown, message: string = "Elliptic curve assertion failed"): void
getNAF(num: BigNumber, w: number, bits: number): number[]
getJSF(k1: BigNumber, k2: BigNumber): number[][]
static cachedProperty(obj, name: string, computer): void
static parseBytes(bytes: string | number[]): number[]
static intFromLE(bytes: number[]): BigNumber
constructor()
_getEndomorphism(conf): {
beta: BigNumber;
lambda: BigNumber;
basis: Array<{
a: BigNumber;
b: BigNumber;
}>;
} | undefined
_getEndoRoots(num: BigNumber): [
BigNumber,
BigNumber
]
_getEndoBasis(lambda: BigNumber): [
{
a: BigNumber;
b: BigNumber;
},
{
a: BigNumber;
b: BigNumber;
}
]
_endoSplit(k: BigNumber): {
k1: BigNumber;
k2: BigNumber;
}
validate(point: Point): boolean
}
```
See also: [BigNumber](./primitives.md#class-bignumber), [Point](./primitives.md#class-point), [ReductionContext](./primitives.md#class-reductioncontext), [red](./primitives.md#function-red)
Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
---
### Class: DRBG
HMAC-DRBG used **only** for deterministic ECDSA nonce generation.
This implementation follows the RFC 6979-style HMAC-DRBG construction for secp256k1
and is wired internally into the ECDSA signing code. It is **not forward-secure**
and MUST NOT be used as a general-purpose DRBG, key generator, or randomness source.
Security note:
- Intended scope: internal ECDSA nonce generation with fixed-size inputs.
- Out-of-scope: generic randomness, long-lived session keys, or any context
where forward secrecy is required.
- API stability: this class is internal.
Example
```ts
const drbg = new DRBG('af12de...', '123ef...');
```
```ts
export default class DRBG {
K: number[];
V: number[];
constructor(entropy: number[] | string, nonce: number[] | string)
hmac(): SHA256HMAC
update(seed?: number[]): void
generate(len: number): string
}
```
See also: [SHA256HMAC](./primitives.md#class-sha256hmac)
#### Method generate
Generates deterministic random hexadecimal string of given length.
In every generation process, it also updates the internal state `K` and `V`.
```ts
generate(len: number): string
```
Returns
The required deterministic random hexadecimal string.
Argument Details
+ **len**
+ The length of required random number.
Example
```ts
const randomHex = drbg.generate(256);
```
#### Method hmac
Generates HMAC using the K value of the instance. This method is used internally for operations.
```ts
hmac(): SHA256HMAC
```
See also: [SHA256HMAC](./primitives.md#class-sha256hmac)
Returns
The SHA256HMAC object created with K value.
Example
```ts
const hmac = drbg.hmac();
```
#### Method update
Updates the `K` and `V` values of the instance based on the seed.
The seed if not provided uses `V` as seed.
```ts
update(seed?: number[]): void
```
Returns
Nothing, but updates the internal state `K` and `V` value.
Argument Details
+ **seed**
+ an optional value that used to update `K` and `V`. Default is `undefined`.
Example
```ts
drbg.update('e13af...');
```
Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
---
### Class: JacobianPoint
The `JacobianPoint` class extends the `BasePoint` class for handling Jacobian coordinates on an Elliptic Curve.
This class defines the properties and the methods needed to work with points in Jacobian coordinates.
The Jacobian coordinates represent a point (x, y, z) on an Elliptic Curve such that the usual (x, y) coordinates are given by (x/z^2, y/z^3).
Example
```ts
const pointJ = new JacobianPoint('3', '4', '1');
```
```ts
export default class JacobianPoint extends BasePoint {
x: BigNumber;
y: BigNumber;
z: BigNumber;
zOne: boolean;
constructor(x: string | BigNumber | null, y: string | BigNumber | null, z: string | BigNumber | null)
toP(): Point
neg(): JacobianPoint
add(p: JacobianPoint): JacobianPoint
mixedAdd(p: Point): JacobianPoint
dblp(pow: number): JacobianPoint
dbl(): JacobianPoint
eq(p: Point | JacobianPoint): boolean
eqXToP(x: BigNumber): boolean
inspect(): string
isInfinity(): boolean
}
```
See also: [BasePoint](./primitives.md#class-basepoint), [BigNumber](./primitives.md#class-bignumber), [Point](./primitives.md#class-point)
#### Constructor
Constructs a new `JacobianPoint` instance.
```ts
constructor(x: string | BigNumber | null, y: string | BigNumber | null, z: string | BigNumber | null)
```
See also: [BigNumber](./primitives.md#class-bignumber)
Argument Details
+ **x**
+ If `null`, the x-coordinate will default to the curve's defined 'one' constant.
If `x` is not a BigNumber, `x` will be converted to a `BigNumber` assuming it is a hex string.
+ **y**
+ If `null`, the y-coordinate will default to the curve's defined 'one' constant.
If `y` is not a BigNumber, `y` will be converted to a `BigNumber` assuming it is a hex string.
+ **z**
+ If `null`, the z-coordinate will default to 0.
If `z` is not a BigNumber, `z` will be converted to a `BigNumber` assuming it is a hex string.
Example
```ts
const pointJ1 = new JacobianPoint(null, null, null); // creates point at infinity
const pointJ2 = new JacobianPoint('3', '4', '1'); // creates point (3, 4, 1)
```
#### Property x
The `x` coordinate of the point in the Jacobian form.
```ts
x: BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
#### Property y
The `y` coordinate of the point in the Jacobian form.
```ts
y: BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
#### Property z
The `z` coordinate of the point in the Jacobian form.
```ts
z: BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
#### Property zOne
Flag that indicates if the `z` coordinate is one.
```ts
zOne: boolean
```
#### Method add
Addition operation in the Jacobian coordinates. It takes a Jacobian point as an argument
and returns a new Jacobian point as a result of the addition. In the special cases,
when either one of the points is the point at infinity, it will return the other point.
```ts
add(p: JacobianPoint): JacobianPoint
```
See also: [JacobianPoint](./primitives.md#class-jacobianpoint)
Returns
Returns a new Jacobian point as the result of the addition.
Argument Details
+ **p**
+ The Jacobian point to be added.
Example
```ts
const p1 = new JacobianPoint(x1, y1, z1)
const p2 = new JacobianPoint(x2, y2, z2)
const result = p1.add(p2)
```
#### Method dbl
Point doubling operation in the Jacobian coordinates. A special case is when the point is the point at infinity, in this case, this function will return the point itself.
```ts
dbl(): JacobianPoint
```
See also: [JacobianPoint](./primitives.md#class-jacobianpoint)
Returns
Returns a new Jacobian point as the result of the doubling.
Example
```ts
const jp = new JacobianPoint(x, y, z)
const result = jp.dbl()
```
#### Method dblp
Multiple doubling operation. It doubles the Jacobian point as many times as the pow parameter specifies. If pow is 0 or the point is the point at infinity, it will return the point itself.
```ts
dblp(pow: number): JacobianPoint
```
See also: [JacobianPoint](./primitives.md#class-jacobianpoint)
Returns
Returns a new Jacobian point as the result of multiple doublings.
Argument Details
+ **pow**
+ The number of times the point should be doubled.
Example
```ts
const jp = new JacobianPoint(x, y, z)
const result = jp.dblp(3)
```
#### Method eq
Equality check operation. It checks whether the affine or Jacobian point is equal to this Jacobian point.
```ts
eq(p: Point | JacobianPoint): boolean
```
See also: [JacobianPoint](./primitives.md#class-jacobianpoint), [Point](./primitives.md#class-point)
Returns
Returns true if the points are equal, otherwise returns false.
Argument Details
+ **p**
+ The affine or Jacobian point to compare with.
Example
```ts
const jp1 = new JacobianPoint(x1, y1, z1)
const jp2 = new JacobianPoint(x2, y2, z2)
const areEqual = jp1.eq(jp2)
```
#### Method eqXToP
Equality check operation in relation to an x coordinate of a point in projective coordinates.
It checks whether the x coordinate of the Jacobian point is equal to the provided x coordinate
of a point in projective coordinates.
```ts
eqXToP(x: BigNumber): boolean
```
See also: [BigNumber](./primitives.md#class-bignumber)
Returns
Returns true if the x coordinates are equal, otherwise returns false.
Argument Details
+ **x**
+ The x coordinate of a point in projective coordinates.
Example
```ts
const jp = new JacobianPoint(x1, y1, z1)
const isXEqual = jp.eqXToP(x2)
```
#### Method inspect
Returns the string representation of the JacobianPoint instance.
```ts
inspect(): string
```
Returns
Returns the string description of the JacobianPoint. If the JacobianPoint represents a point at infinity, the return value of this function is '<EC JPoint Infinity>'. For a normal point, it returns the string description format as '<EC JPoint x: x-coordinate y: y-coordinate z: z-coordinate>'.
Example
```ts
const point = new JacobianPoint('5', '6', '1');
console.log(point.inspect()); // Output: '<EC JPoint x: 5 y: 6 z: 1>'
```
#### Method isInfinity
Checks whether the JacobianPoint instance represents a point at infinity.
```ts
isInfinity(): boolean
```
Returns
Returns true if the JacobianPoint's z-coordinate equals to zero (which represents the point at infinity in Jacobian coordinates). Returns false otherwise.
Example
```ts
const point = new JacobianPoint('5', '6', '0');
console.log(point.isInfinity()); // Output: true
```
#### Method mixedAdd
Mixed addition operation. This function combines the standard point addition with
the transformation from the affine to Jacobian coordinates. It first converts
the affine point to Jacobian, and then preforms the addition.
```ts
mixedAdd(p: Point): JacobianPoint
```
See also: [JacobianPoint](./primitives.md#class-jacobianpoint), [Point](./primitives.md#class-point)
Returns
Returns the result of the mixed addition as a new Jacobian point.
Argument Details
+ **p**
+ The affine point to be added.
Example
```ts
const jp = new JacobianPoint(x1, y1, z1)
const ap = new Point(x2, y2)
const result = jp.mixedAdd(ap)
```
#### Method neg
Negation operation. It returns the additive inverse of the Jacobian point.
```ts
neg(): JacobianPoint
```
See also: [JacobianPoint](./primitives.md#class-jacobianpoint)
Returns
Returns a new Jacobian point as the result of the negation.
Example
```ts
const jp = new JacobianPoint(x, y, z)
const result = jp.neg()
```
#### Method toP
Converts the `JacobianPoint` object instance to standard affine `Point` format and returns `Point` type.
```ts
toP(): Point
```
See also: [Point](./primitives.md#class-point)
Returns
The `Point`(affine) object representing the same point as the original `JacobianPoint`.
If the initial `JacobianPoint` represents point at infinity, an instance of `Point` at infinity is returned.
Example
```ts
const pointJ = new JacobianPoint('3', '4', '1');
const pointP = pointJ.toP(); // The point in affine coordinates.
```
Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
---
### Class: K256
A class representing K-256, a prime number with optimizations, specifically used in the secp256k1 curve.
It extends the functionalities of the Mersenne class.
K-256 prime is represented as 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f'
Example
```ts
const k256 = new K256();
```
```ts
export default class K256 extends Mersenne {
constructor()
split(input: BigNumber, output: BigNumber): void
imulK(num: BigNumber): BigNumber
}
```
See also: [BigNumber](./primitives.md#class-bignumber), [Mersenne](./primitives.md#class-mersenne)
#### Constructor
Constructor for the K256 class.
Creates an instance of K256 using the super constructor from Mersenne.
```ts
constructor()
```
Example
```ts
const k256 = new K256();
```
#### Method imulK
Multiplies a BigNumber ('num') with the constant 'K' in-place and returns the result.
'K' is equal to 0x1000003d1 or in decimal representation: [ 64, 977 ].
```ts
imulK(num: BigNumber): BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
Returns
Returns the mutated BigNumber after multiplication.
Argument Details
+ **num**
+ The BigNumber to multiply with K.
Example
```ts
const number = new BigNumber(12345);
const result = k256.imulK(number);
```
#### Method split
Splits a BigNumber into a new BigNumber based on specific computation
rules. This method modifies the input and output big numbers.
```ts
split(input: BigNumber, output: BigNumber): void
```
See also: [BigNumber](./primitives.md#class-bignumber)
Argument Details
+ **input**
+ The BigNumber to be split.
+ **output**
+ The BigNumber that results from the split.
Example
```ts
const input = new BigNumber(3456);
const output = new BigNumber(0);
k256.split(input, output);
```
Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
---
### Class: KeyShares
Example
```ts
const key = PrivateKey.fromShares(shares)
```
```ts
export class KeyShares {
points: PointInFiniteField[];
threshold: number;
integrity: string;
constructor(points: PointInFiniteField[], threshold: number, integrity: string)
static fromBackupFormat(shares: string[]): KeyShares
toBackupFormat(): string[]
}
```
See also: [PointInFiniteField](./primitives.md#class-pointinfinitefield)
Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
---
### Class: Mersenne
A representation of a pseudo-Mersenne prime.
A pseudo-Mersenne prime has the general form 2^n - k, where n and k are integers.
```ts
export default class Mersenne {
name: string;
p: BigNumber;
k: BigNumber;
n: number;
constructor(name: string, p: string)
ireduce(num: BigNumber): BigNumber
split(input: BigNumber, out: BigNumber): void
imulK(num: BigNumber): BigNumber
}
```
See also: [BigNumber](./primitives.md#class-bignumber)
#### Constructor
```ts
constructor(name: string, p: string)
```
Argument Details
+ **name**
+ An identifier for the Mersenne instance.
+ **p**
+ A string representation of the pseudo-Mersenne prime, expressed in hexadecimal.
Example
```ts
const mersenne = new Mersenne('M31', '7FFFFFFF');
```
#### Property k
The constant subtracted from 2^n to derive a pseudo-Mersenne prime.
```ts
k: BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
#### Property n
The exponent which determines the magnitude of the prime.
```ts
n: number
```
#### Property name
The identifier for the Mersenne instance.
```ts
name: string
```
#### Property p
BigNumber equivalent to 2^n - k.
```ts
p: BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
#### Method imulK
Performs an in-place multiplication of the parameter by constant k.
```ts
imulK(num: BigNumber): BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
Returns
The result of the multiplication, in BigNumber format.
Argument Details
+ **num**
+ The BigNumber to multiply with k.
Example
```ts
const multiplied = mersenne.imulK(new BigNumber('2345', 16));
```
#### Method ireduce
Reduces an input BigNumber in place, under the assumption that
it is less than the square of the pseudo-Mersenne prime.
```ts
ireduce(num: BigNumber): BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
Returns
The reduced BigNumber.
Argument Details
+ **num**
+ The BigNumber to be reduced.
Example
```ts
const reduced = mersenne.ireduce(new BigNumber('2345', 16));
```
#### Method split
Shifts bits of the input BigNumber to the right, in place,
to meet the magnitude of the pseudo-Mersenne prime.
```ts
split(input: BigNumber, out: BigNumber): void
```
See also: [BigNumber](./primitives.md#class-bignumber)
Argument Details
+ **input**
+ The BigNumber to be shifted (will contain HI part).
+ **out**
+ The BigNumber to hold the shifted result (LO part).
Example
```ts
mersenne.split(new BigNumber('2345', 16), new BigNumber());
```
Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
---
### Class: MontgomoryMethod
Represents a Montgomery reduction context, which is a mathematical method
for performing modular multiplication without division.
Montgomery reduction is an algorithm used mainly in cryptography which can
help to speed up calculations in contexts where there are many repeated
computations.
This class extends the `ReductionContext` class.
```ts
export default class MontgomoryMethod extends ReductionContext {
shift: number;
r: BigNumber;
r2: BigNumber;
rinv: BigNumber;
minv: BigNumber;
constructor(m: BigNumber | "k256")
convertTo(num: BigNumber): BigNumber
convertFrom(num: BigNumber): BigNumber
imul(a: BigNumber, b: BigNumber): BigNumber
mul(a: BigNumber, b: BigNumber): BigNumber
invm(a: BigNumber): BigNumber
}
```
See also: [BigNumber](./primitives.md#class-bignumber), [ReductionContext](./primitives.md#class-reductioncontext)
#### Constructor
```ts
constructor(m: BigNumber | "k256")
```
See also: [BigNumber](./primitives.md#class-bignumber)
Argument Details
+ **m**
+ The modulus to be used for the Montgomery method reductions.
#### Property minv
The modular multiplicative inverse of `m` mod `r`.
```ts
minv: BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
#### Property r
The 2^shift, shifted left by the bit length of modulus `m`.
```ts
r: BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
#### Property r2
The square of `r` modulo `m`.
```ts
r2: BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
#### Property rinv
The modular multiplicative inverse of `r` mod `m`.
```ts
rinv: BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
#### Property shift
The number of bits in the modulus.
```ts
shift: number
```
#### Method convertFrom
Converts a number from the Montgomery domain back to the original domain.
```ts
convertFrom(num: BigNumber): BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
Returns
The result of the conversion from the Montgomery domain.
Argument Details
+ **num**
+ The number to be converted from the Montgomery domain.
Example
```ts
const montMethod = new MontgomoryMethod(m);
const convertedNum = montMethod.convertFrom(num);
```
#### Method convertTo
Converts a number into the Montgomery domain.
```ts
convertTo(num: BigNumber): BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
Returns
The result of the conversion into the Montgomery domain.
Argument Details
+ **num**
+ The number to be converted into the Montgomery domain.
Example
```ts
const montMethod = new MontgomoryMethod(m);
const convertedNum = montMethod.convertTo(num);
```
#### Method imul
Performs an in-place multiplication of two numbers in the Montgomery domain.
```ts
imul(a: BigNumber, b: BigNumber): BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
Returns
The result of the in-place multiplication.
Argument Details
+ **a**
+ The first number to multiply.
+ **b**
+ The second number to multiply.
Example
```ts
const montMethod = new MontgomoryMethod(m);
const product = montMethod.imul(a, b);
```
#### Method invm
Calculates the modular multiplicative inverse of a number in the Montgomery domain.
```ts
invm(a: BigNumber): BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
Returns
The modular multiplicative inverse of 'a'.
Argument Details
+ **a**
+ The number to compute the modular multiplicative inverse of.
Example
```ts
const montMethod = new MontgomoryMethod(m);
const inverse = montMethod.invm(a);
```
#### Method mul
Performs the multiplication of two numbers in the Montgomery domain.
```ts
mul(a: BigNumber, b: BigNumber): BigNumber
```
See also: [BigNumber](./primitives.md#class-bignumber)
Returns
The result of the multiplication.
Argument Details
+ **a**
+ The first number to multiply.
+ **b**
+ The second number to multiply.
Example
```ts
const montMethod = new MontgomoryMethod(m);
const product = montMethod.mul(a, b);
```
Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
---
### Class: Point
`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.
```ts
export default class Point extends BasePoint {
x: BigNumber | null;
y: BigNumber | null;
inf: boolean;
static _assertOnCurve(p: Point): Point
static fromDER(bytes: number[]): Point
static fromString(str: string): Point
static fromX(x: BigNumber | number | number[] | string, odd: boolean): Point
static fromJSON(obj: string | any[], isRed: boolean): Point
constructor(x: BigNumber | number | number[] | string | null, y: BigNumber | number | number[] | string | null, isRed: boolean = true)
validate(): boolean
encode(compact: boolean = true, enc?: "hex"): number[] | string
toString(): string
toJSON(): [
BigNumber | null,
BigNumber | null,
{
doubles: {
step: any;
points: any[];
} | undefined;
naf: {
wnd: any;
points: any[];
} | undefined;
}?
]
inspect(): string
isInfinity(): boolean
add(p: Point): Point
dbl(): Point
getX(): BigNumber
getY(): BigNumber
mul(k: BigNumber | number | number[] | string): Point
mulAdd(k1: BigNumber, p2: Point, k2: BigNumber): Point
jmulAdd(k1: BigNumber, p2: Point, k2: BigNumber): JPoint
eq(p: Point): boolean
neg(_precompute?: boolean): Point
dblp(k: number): Point
toJ(): JPoint
}
```
See also: [BasePoint](./primitives.md#class-basepoint), [BigNumber](./primitives.md#class-bignumber), [encode](./primitives.md#variable-encode)
#### Constructor
```ts
constructor(x: BigNumber | number | number[] | string | null, y: BigNumber | number | number[] | string | null, isRed: boolean = true)
```
See also: [BigNumber](./primitives.md#class-bignumber)
Argument Details
+ **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.
+ **y**
+ The y-coordinate of the point, similar to x.
+ **isRed**
+ A boolean indicating if the point is a member of the field of integers modulo the k256 prime. Default is true.
Example
```ts
new Point('abc123', 'def456');
new Point(null, null); // Generates Infinity point.
```
#### Property inf
Flag to record if the point is at infinity in the Elliptic Curve.
```ts
inf: boolean
```
#### Property x
The x-coordinate of the point.
```ts
x: BigNumber | null
```
See also: [BigNumber](./primitives.md#class-bignumber)
#### Property y
The y-coordinate of the point.
```ts
y: BigNumber | null
```
See also: [BigNumber](./primitives.md#class-bignumber)
#### Method add
Adds another Point to this Point, returning a new Point.
```ts
add(p: Point): Point
```
See also: [Point](./primitives.md#class-point)
Returns
A new Point that results from the addition.
Argument Details
+ **p**
+ The Point to add to this one.
Example
```ts
const p1 = new Point(1, 2);
const p2 = new Point(2, 3);
const result = p1.add(p2);
```
#### Method dbl
Doubles the current point.
```ts
dbl(): Point
```
See also: [Point](./primitives.md#class-point)
Example
```ts
const P = new Point('123', '456');
const result = P.dbl();
```
#### Method dblp
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.
```ts
dblp(k: number): Point
```
See also: [Point](./primitives.md#class-point)
Returns
The Point after 'k' "doubling" operations have been performed.
Argument Details
+ **k**
+ The number of times the "doubling" operation is to be performed on the Point.
Example
```ts
const p = new Point(5, 20);
const doubledPoint = p.dblp(10); // returns the point after "doubled" 10 times
```
#### Method encode
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.
```ts
encode(compact: boolean = true, enc?: "hex"): number[] | string
```
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.
Argument Details
+ **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.
+ **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'.
Example
```ts
const aPoint = new Point(x, y);
const encodedPointArray = aPoint.encode();
const encodedPointHex = aPoint.encode(true, 'hex');
```
#### Method eq
Checks if the Point instance is equal to another given Point.
```ts
eq(p: Point): boolean
```
See also: [Point](./primitives.md#class-point)
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.
Argument Details
+ **p**
+ The Point to be checked if equal to the current instance.
Example
```ts
const p1 = new Point(5, 20);
const p2 = new Point(5, 20);
const areEqual = p1.eq(p2); // returns true
```
#### Method fromDER
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.
```ts
static fromDER(bytes: number[]): Point
```
See also: [Point](./primitives.md#class-point)
Returns
Returns a new point representing the given string.
Argument Details
+ **bytes**
+ The point representation number array.
Throws
`Error` If the point number[] value has a wrong length.
`Error` If the point format is unknown.
Example
```ts
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);
```
#### Method fromJSON
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.
```ts
static fromJSON(obj: string | any[], isRed: boolean): Point
```
See also: [Point](./primitives.md#class-point)
Returns
Returns a new point based on the deserialized JSON object.
Argument Details
+ **obj**
+ An object or array that holds the data for the point.
+ **isRed**
+ A boolean to direct how the Point is constructed from the JSON object.
Example
```ts
const serializedPoint = '{"x":52,"y":15}';
const point = Point.fromJSON(serializedPoint, true);
```
#### Method fromString
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.
```ts
static fromString(str: string): Point
```
See also: [Point](./primitives.md#class-point)
Returns
Returns a new point representing the given string.
Argument Details
+ **str**
+ The point representation string.
Throws
`Error` If the point string value has a wrong length.
`Error` If the point format is unknown.
Example
```ts
const pointStr = 'abcdef';
const point = Point.fromString(pointStr);
```
#### Method fromX
Generates a point from an x coordinate and a boolean indicating wheth