@noble/curves 1.9.2 → 1.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +186 -206
- package/_shortw_utils.d.ts +1 -0
- package/_shortw_utils.d.ts.map +1 -1
- package/_shortw_utils.js +1 -0
- package/_shortw_utils.js.map +1 -1
- package/abstract/bls.d.ts +87 -62
- package/abstract/bls.d.ts.map +1 -1
- package/abstract/bls.js +170 -163
- package/abstract/bls.js.map +1 -1
- package/abstract/curve.d.ts +109 -23
- package/abstract/curve.d.ts.map +1 -1
- package/abstract/curve.js +158 -156
- package/abstract/curve.js.map +1 -1
- package/abstract/edwards.d.ts +124 -70
- package/abstract/edwards.d.ts.map +1 -1
- package/abstract/edwards.js +212 -62
- package/abstract/edwards.js.map +1 -1
- package/abstract/hash-to-curve.d.ts +8 -4
- package/abstract/hash-to-curve.d.ts.map +1 -1
- package/abstract/hash-to-curve.js +23 -11
- package/abstract/hash-to-curve.js.map +1 -1
- package/abstract/modular.d.ts +8 -3
- package/abstract/modular.d.ts.map +1 -1
- package/abstract/modular.js +79 -35
- package/abstract/modular.js.map +1 -1
- package/abstract/montgomery.d.ts +17 -4
- package/abstract/montgomery.d.ts.map +1 -1
- package/abstract/montgomery.js +19 -3
- package/abstract/montgomery.js.map +1 -1
- package/abstract/tower.d.ts +3 -3
- package/abstract/tower.d.ts.map +1 -1
- package/abstract/tower.js.map +1 -1
- package/abstract/weierstrass.d.ts +142 -116
- package/abstract/weierstrass.d.ts.map +1 -1
- package/abstract/weierstrass.js +414 -335
- package/abstract/weierstrass.js.map +1 -1
- package/bls12-381.d.ts.map +1 -1
- package/bls12-381.js +4 -4
- package/bls12-381.js.map +1 -1
- package/ed25519.d.ts +52 -66
- package/ed25519.d.ts.map +1 -1
- package/ed25519.js +128 -155
- package/ed25519.js.map +1 -1
- package/ed448.d.ts +57 -58
- package/ed448.d.ts.map +1 -1
- package/ed448.js +114 -131
- package/ed448.js.map +1 -1
- package/esm/_shortw_utils.d.ts +1 -0
- package/esm/_shortw_utils.d.ts.map +1 -1
- package/esm/_shortw_utils.js +1 -0
- package/esm/_shortw_utils.js.map +1 -1
- package/esm/abstract/bls.d.ts +87 -62
- package/esm/abstract/bls.d.ts.map +1 -1
- package/esm/abstract/bls.js +171 -164
- package/esm/abstract/bls.js.map +1 -1
- package/esm/abstract/curve.d.ts +109 -23
- package/esm/abstract/curve.d.ts.map +1 -1
- package/esm/abstract/curve.js +156 -155
- package/esm/abstract/curve.js.map +1 -1
- package/esm/abstract/edwards.d.ts +124 -70
- package/esm/abstract/edwards.d.ts.map +1 -1
- package/esm/abstract/edwards.js +210 -62
- package/esm/abstract/edwards.js.map +1 -1
- package/esm/abstract/hash-to-curve.d.ts +8 -4
- package/esm/abstract/hash-to-curve.d.ts.map +1 -1
- package/esm/abstract/hash-to-curve.js +22 -11
- package/esm/abstract/hash-to-curve.js.map +1 -1
- package/esm/abstract/modular.d.ts +8 -3
- package/esm/abstract/modular.d.ts.map +1 -1
- package/esm/abstract/modular.js +79 -35
- package/esm/abstract/modular.js.map +1 -1
- package/esm/abstract/montgomery.d.ts +17 -4
- package/esm/abstract/montgomery.d.ts.map +1 -1
- package/esm/abstract/montgomery.js +19 -3
- package/esm/abstract/montgomery.js.map +1 -1
- package/esm/abstract/tower.d.ts +3 -3
- package/esm/abstract/tower.d.ts.map +1 -1
- package/esm/abstract/tower.js.map +1 -1
- package/esm/abstract/weierstrass.d.ts +142 -116
- package/esm/abstract/weierstrass.d.ts.map +1 -1
- package/esm/abstract/weierstrass.js +411 -333
- package/esm/abstract/weierstrass.js.map +1 -1
- package/esm/bls12-381.d.ts.map +1 -1
- package/esm/bls12-381.js +4 -4
- package/esm/bls12-381.js.map +1 -1
- package/esm/ed25519.d.ts +52 -66
- package/esm/ed25519.d.ts.map +1 -1
- package/esm/ed25519.js +131 -157
- package/esm/ed25519.js.map +1 -1
- package/esm/ed448.d.ts +57 -58
- package/esm/ed448.d.ts.map +1 -1
- package/esm/ed448.js +116 -132
- package/esm/ed448.js.map +1 -1
- package/esm/index.js +7 -9
- package/esm/index.js.map +1 -1
- package/esm/jubjub.d.ts +3 -3
- package/esm/jubjub.d.ts.map +1 -1
- package/esm/jubjub.js +3 -3
- package/esm/jubjub.js.map +1 -1
- package/esm/misc.d.ts +3 -5
- package/esm/misc.d.ts.map +1 -1
- package/esm/misc.js +0 -3
- package/esm/misc.js.map +1 -1
- package/esm/nist.d.ts +0 -6
- package/esm/nist.d.ts.map +1 -1
- package/esm/nist.js +31 -15
- package/esm/nist.js.map +1 -1
- package/esm/p256.d.ts +4 -0
- package/esm/p256.d.ts.map +1 -1
- package/esm/p256.js +4 -0
- package/esm/p256.js.map +1 -1
- package/esm/p384.d.ts +4 -1
- package/esm/p384.d.ts.map +1 -1
- package/esm/p384.js +4 -1
- package/esm/p384.js.map +1 -1
- package/esm/p521.d.ts +4 -0
- package/esm/p521.d.ts.map +1 -1
- package/esm/p521.js +4 -0
- package/esm/p521.js.map +1 -1
- package/esm/secp256k1.d.ts +32 -15
- package/esm/secp256k1.d.ts.map +1 -1
- package/esm/secp256k1.js +72 -67
- package/esm/secp256k1.js.map +1 -1
- package/esm/utils.d.ts +1 -1
- package/esm/utils.js +1 -1
- package/index.js +7 -9
- package/index.js.map +1 -1
- package/jubjub.d.ts +3 -3
- package/jubjub.d.ts.map +1 -1
- package/jubjub.js +3 -3
- package/jubjub.js.map +1 -1
- package/misc.d.ts +3 -5
- package/misc.d.ts.map +1 -1
- package/misc.js +0 -3
- package/misc.js.map +1 -1
- package/nist.d.ts +0 -6
- package/nist.d.ts.map +1 -1
- package/nist.js +31 -15
- package/nist.js.map +1 -1
- package/p256.d.ts +4 -0
- package/p256.d.ts.map +1 -1
- package/p256.js +4 -0
- package/p256.js.map +1 -1
- package/p384.d.ts +4 -1
- package/p384.d.ts.map +1 -1
- package/p384.js +4 -1
- package/p384.js.map +1 -1
- package/p521.d.ts +4 -0
- package/p521.d.ts.map +1 -1
- package/p521.js +4 -0
- package/p521.js.map +1 -1
- package/package.json +4 -2
- package/secp256k1.d.ts +32 -15
- package/secp256k1.d.ts.map +1 -1
- package/secp256k1.js +70 -65
- package/secp256k1.js.map +1 -1
- package/src/_shortw_utils.ts +1 -0
- package/src/abstract/bls.ts +319 -257
- package/src/abstract/curve.ts +226 -170
- package/src/abstract/edwards.ts +350 -139
- package/src/abstract/hash-to-curve.ts +33 -16
- package/src/abstract/modular.ts +86 -35
- package/src/abstract/montgomery.ts +36 -9
- package/src/abstract/tower.ts +4 -4
- package/src/abstract/weierstrass.ts +567 -474
- package/src/bls12-381.ts +28 -20
- package/src/ed25519.ts +161 -179
- package/src/ed448.ts +150 -156
- package/src/index.ts +7 -9
- package/src/jubjub.ts +3 -3
- package/src/misc.ts +3 -7
- package/src/nist.ts +40 -16
- package/src/p256.ts +4 -0
- package/src/p384.ts +4 -2
- package/src/p521.ts +4 -0
- package/src/secp256k1.ts +91 -73
- package/src/utils.ts +1 -1
- package/utils.d.ts +1 -1
- package/utils.js +1 -1
package/esm/abstract/curve.d.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Methods for elliptic curve multiplication by scalars.
|
|
3
|
+
* Contains wNAF, pippenger.
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
7
|
+
import { type Hex } from '../utils.ts';
|
|
1
8
|
import { type IField } from './modular.ts';
|
|
2
9
|
export type AffinePoint<T> = {
|
|
3
10
|
x: T;
|
|
@@ -15,24 +22,76 @@ export interface Group<T extends Group<T>> {
|
|
|
15
22
|
multiply(scalar: bigint): T;
|
|
16
23
|
toAffine?(invertedZ?: any): AffinePoint<any>;
|
|
17
24
|
}
|
|
25
|
+
/** Base interface for all elliptic curve Points. */
|
|
26
|
+
export interface CurvePoint<F, P extends CurvePoint<F, P>> extends Group<P> {
|
|
27
|
+
/** Affine x coordinate. Different from projective / extended X coordinate. */
|
|
28
|
+
x: F;
|
|
29
|
+
/** Affine y coordinate. Different from projective / extended Y coordinate. */
|
|
30
|
+
y: F;
|
|
31
|
+
Z?: F;
|
|
32
|
+
assertValidity(): void;
|
|
33
|
+
clearCofactor(): P;
|
|
34
|
+
is0(): boolean;
|
|
35
|
+
isTorsionFree(): boolean;
|
|
36
|
+
isSmallOrder(): boolean;
|
|
37
|
+
multiplyUnsafe(scalar: bigint): P;
|
|
38
|
+
/**
|
|
39
|
+
* Massively speeds up `p.multiply(n)` by using precompute tables (caching). See {@link wNAF}.
|
|
40
|
+
* @param isLazy calculate cache now. Default (true) ensures it's deferred to first `multiply()`
|
|
41
|
+
*/
|
|
42
|
+
precompute(windowSize?: number, isLazy?: boolean): P;
|
|
43
|
+
/** Converts point to 2D xy affine coordinates */
|
|
44
|
+
toAffine(invertedZ?: F): AffinePoint<F>;
|
|
45
|
+
toBytes(): Uint8Array;
|
|
46
|
+
toHex(): string;
|
|
47
|
+
}
|
|
48
|
+
/** Base interface for all elliptic curve Point constructors. */
|
|
49
|
+
export interface CurvePointCons<F, P extends CurvePoint<F, P>> extends GroupConstructor<P> {
|
|
50
|
+
BASE: P;
|
|
51
|
+
ZERO: P;
|
|
52
|
+
/** Field for basic curve math */
|
|
53
|
+
Fp: IField<F>;
|
|
54
|
+
/** Scalar field, for scalars in multiply and others */
|
|
55
|
+
Fn: IField<bigint>;
|
|
56
|
+
/** Creates point from x, y. Does NOT validate if the point is valid. Use `.assertValidity()`. */
|
|
57
|
+
fromAffine(p: AffinePoint<F>): P;
|
|
58
|
+
fromBytes(bytes: Uint8Array): P;
|
|
59
|
+
fromHex(hex: Hex): P;
|
|
60
|
+
}
|
|
61
|
+
export type GetPointConsF<PC> = PC extends CurvePointCons<infer F, any> ? F : never;
|
|
62
|
+
export type GetPointConsPoint<PC> = PC extends CurvePointCons<any, infer P> ? P : never;
|
|
63
|
+
export interface CurveInfo {
|
|
64
|
+
type: 'weierstrass' | 'edwards' | 'montgomery';
|
|
65
|
+
publicKeyHasPrefix?: boolean;
|
|
66
|
+
lengths: {
|
|
67
|
+
secret: number;
|
|
68
|
+
public: number;
|
|
69
|
+
publicUncompressed?: number;
|
|
70
|
+
signature: number;
|
|
71
|
+
seed: number;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
18
74
|
export type GroupConstructor<T> = {
|
|
19
75
|
BASE: T;
|
|
20
76
|
ZERO: T;
|
|
21
77
|
};
|
|
78
|
+
/** @deprecated */
|
|
22
79
|
export type ExtendedGroupConstructor<T> = GroupConstructor<T> & {
|
|
23
80
|
Fp: IField<any>;
|
|
24
81
|
Fn: IField<bigint>;
|
|
25
82
|
fromAffine(ap: AffinePoint<any>): T;
|
|
26
83
|
};
|
|
27
84
|
export type Mapper<T> = (i: T[]) => T[];
|
|
28
|
-
export declare function negateCt<T extends
|
|
85
|
+
export declare function negateCt<T extends {
|
|
86
|
+
negate: () => T;
|
|
87
|
+
}>(condition: boolean, item: T): T;
|
|
29
88
|
/**
|
|
30
89
|
* Takes a bunch of Projective Points but executes only one
|
|
31
90
|
* inversion on all of them. Inversion is very slow operation,
|
|
32
91
|
* so this improves performance massively.
|
|
33
92
|
* Optimization: converts a list of projective points to a list of identical points with Z=1.
|
|
34
93
|
*/
|
|
35
|
-
export declare function normalizeZ<
|
|
94
|
+
export declare function normalizeZ<PC extends CurvePointCons<any, any>, F = GetPointConsF<PC>, P extends CurvePoint<F, P> = GetPointConsPoint<PC>>(c: CurvePointCons<F, P>, points: P[]): P[];
|
|
36
95
|
/** Internal wNAF opts for specific W and scalarBits */
|
|
37
96
|
export type WOpts = {
|
|
38
97
|
windows: number;
|
|
@@ -41,26 +100,12 @@ export type WOpts = {
|
|
|
41
100
|
maxNumber: number;
|
|
42
101
|
shiftBy: bigint;
|
|
43
102
|
};
|
|
44
|
-
export type IWNAF<T extends Group<T>> = {
|
|
45
|
-
constTimeNegate: <T extends Group<T>>(condition: boolean, item: T) => T;
|
|
46
|
-
hasPrecomputes(elm: T): boolean;
|
|
47
|
-
unsafeLadder(elm: T, n: bigint, p?: T): T;
|
|
48
|
-
precomputeWindow(elm: T, W: number): Group<T>[];
|
|
49
|
-
getPrecomputes(W: number, P: T, transform?: Mapper<T>): T[];
|
|
50
|
-
wNAF(W: number, precomputes: T[], n: bigint): {
|
|
51
|
-
p: T;
|
|
52
|
-
f: T;
|
|
53
|
-
};
|
|
54
|
-
wNAFUnsafe(W: number, precomputes: T[], n: bigint, acc?: T): T;
|
|
55
|
-
wNAFCached(P: T, n: bigint, transform?: Mapper<T>): {
|
|
56
|
-
p: T;
|
|
57
|
-
f: T;
|
|
58
|
-
};
|
|
59
|
-
wNAFCachedUnsafe(P: T, n: bigint, transform?: Mapper<T>, prev?: T): T;
|
|
60
|
-
setWindowSize(P: T, W: number): void;
|
|
61
|
-
};
|
|
62
103
|
/**
|
|
63
104
|
* Elliptic curve multiplication of Point by scalar. Fragile.
|
|
105
|
+
* Table generation takes **30MB of ram and 10ms on high-end CPU**,
|
|
106
|
+
* but may take much longer on slow devices. Actual generation will happen on
|
|
107
|
+
* first call of `multiply()`. By default, `BASE` point is precomputed.
|
|
108
|
+
*
|
|
64
109
|
* Scalars should always be less than curve order: this should be checked inside of a curve itself.
|
|
65
110
|
* Creates precomputation tables for fast multiplication:
|
|
66
111
|
* - private scalar is split by fixed size windows of W bits
|
|
@@ -73,12 +118,53 @@ export type IWNAF<T extends Group<T>> = {
|
|
|
73
118
|
* @todo Research returning 2d JS array of windows, instead of a single window.
|
|
74
119
|
* This would allow windows to be in different memory locations
|
|
75
120
|
*/
|
|
76
|
-
export declare
|
|
121
|
+
export declare class wNAF<F, P extends CurvePoint<F, P>> {
|
|
122
|
+
private readonly BASE;
|
|
123
|
+
private readonly ZERO;
|
|
124
|
+
private readonly Fn;
|
|
125
|
+
readonly bits: number;
|
|
126
|
+
constructor(Point: CurvePointCons<F, P>, bits: number);
|
|
127
|
+
_unsafeLadder(elm: P, n: bigint, p?: P): P;
|
|
128
|
+
/**
|
|
129
|
+
* Creates a wNAF precomputation window. Used for caching.
|
|
130
|
+
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
131
|
+
* Number of precomputed points depends on the curve size:
|
|
132
|
+
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
133
|
+
* - 𝑊 is the window size
|
|
134
|
+
* - 𝑛 is the bitlength of the curve order.
|
|
135
|
+
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
136
|
+
* @param point Point instance
|
|
137
|
+
* @param W window size
|
|
138
|
+
* @returns precomputed point tables flattened to a single array
|
|
139
|
+
*/
|
|
140
|
+
private precomputeWindow;
|
|
141
|
+
/**
|
|
142
|
+
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
143
|
+
* More compact implementation:
|
|
144
|
+
* https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
|
|
145
|
+
* @returns real and fake (for const-time) points
|
|
146
|
+
*/
|
|
147
|
+
private wNAF;
|
|
148
|
+
/**
|
|
149
|
+
* Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
|
|
150
|
+
* @param acc accumulator point to add result of multiplication
|
|
151
|
+
* @returns point
|
|
152
|
+
*/
|
|
153
|
+
private wNAFUnsafe;
|
|
154
|
+
private getPrecomputes;
|
|
155
|
+
cached(point: P, scalar: bigint, transform?: Mapper<P>): {
|
|
156
|
+
p: P;
|
|
157
|
+
f: P;
|
|
158
|
+
};
|
|
159
|
+
unsafe(point: P, scalar: bigint, transform?: Mapper<P>, prev?: P): P;
|
|
160
|
+
createCache(P: P, W: number): void;
|
|
161
|
+
hasCache(elm: P): boolean;
|
|
162
|
+
}
|
|
77
163
|
/**
|
|
78
164
|
* Endomorphism-specific multiplication for Koblitz curves.
|
|
79
165
|
* Cost: 128 dbl, 0-256 adds.
|
|
80
166
|
*/
|
|
81
|
-
export declare function mulEndoUnsafe<T extends Group<T>>(
|
|
167
|
+
export declare function mulEndoUnsafe<T extends Group<T>>(Point: GroupConstructor<T>, point: T, k1: bigint, k2: bigint): {
|
|
82
168
|
p1: T;
|
|
83
169
|
p2: T;
|
|
84
170
|
};
|
|
@@ -90,7 +176,7 @@ export declare function mulEndoUnsafe<T extends Group<T>>(c: GroupConstructor<T>
|
|
|
90
176
|
* @param c Curve Point constructor
|
|
91
177
|
* @param fieldN field over CURVE.N - important that it's not over CURVE.P
|
|
92
178
|
* @param points array of L curve points
|
|
93
|
-
* @param scalars array of L scalars (aka
|
|
179
|
+
* @param scalars array of L scalars (aka secret keys / bigints)
|
|
94
180
|
*/
|
|
95
181
|
export declare function pippenger<T extends Group<T>>(c: GroupConstructor<T>, fieldN: IField<bigint>, points: T[], scalars: bigint[]): T;
|
|
96
182
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"curve.d.ts","sourceRoot":"","sources":["../../src/abstract/curve.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"curve.d.ts","sourceRoot":"","sources":["../../src/abstract/curve.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,sEAAsE;AACtE,OAAO,EAAmC,KAAK,GAAG,EAAE,MAAM,aAAa,CAAC;AACxE,OAAO,EAAgD,KAAK,MAAM,EAAE,MAAM,cAAc,CAAC;AAKzF,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI;IAC3B,CAAC,EAAE,CAAC,CAAC;IACL,CAAC,EAAE,CAAC,CAAC;CACN,GAAG;IAAE,CAAC,CAAC,EAAE,KAAK,CAAC;IAAC,CAAC,CAAC,EAAE,KAAK,CAAA;CAAE,CAAC;AAI7B,MAAM,WAAW,KAAK,CAAC,CAAC,SAAS,KAAK,CAAC,CAAC,CAAC;IACvC,MAAM,IAAI,CAAC,CAAC;IACZ,MAAM,IAAI,CAAC,CAAC;IACZ,GAAG,CAAC,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;IACjB,QAAQ,CAAC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AAC,CAAC,IAAI;IAAE,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC,MAAM,CAAC,CAAA;CAAE,CAAC;AAC5D,8CAA8C;AAC9C,wBAAgB,kBAAkB,CAAC,CAAC,EAClC,IAAI,EAAE,aAAa,GAAG,SAAS,EAC/B,KAAK,EAAE,gBAAgB,CAAC,CAAC,CAAC,EAC1B,SAAS,GAAE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAM,GAC/B,IAAI,CAAC,CAAC,CAAC,CAiBT"}
|
package/esm/abstract/curve.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Methods for elliptic curve multiplication by scalars.
|
|
3
|
-
* Contains wNAF, pippenger
|
|
3
|
+
* Contains wNAF, pippenger.
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
6
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
@@ -18,12 +18,9 @@ export function negateCt(condition, item) {
|
|
|
18
18
|
* so this improves performance massively.
|
|
19
19
|
* Optimization: converts a list of projective points to a list of identical points with Z=1.
|
|
20
20
|
*/
|
|
21
|
-
export function normalizeZ(c,
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
// @ts-ignore
|
|
25
|
-
const affined = points.map((p, i) => p.toAffine(toInv[i]));
|
|
26
|
-
return affined.map(c.fromAffine);
|
|
21
|
+
export function normalizeZ(c, points) {
|
|
22
|
+
const invertedZs = FpInvertBatch(c.Fp, points.map((p) => p.Z));
|
|
23
|
+
return points.map((p, i) => c.fromAffine(p.toAffine(invertedZs[i])));
|
|
27
24
|
}
|
|
28
25
|
function validateW(W, bits) {
|
|
29
26
|
if (!Number.isSafeInteger(W) || W <= 0 || W > bits)
|
|
@@ -82,6 +79,8 @@ function validateMSMScalars(scalars, field) {
|
|
|
82
79
|
const pointPrecomputes = new WeakMap();
|
|
83
80
|
const pointWindowSizes = new WeakMap();
|
|
84
81
|
function getW(P) {
|
|
82
|
+
// To disable precomputes:
|
|
83
|
+
// return 1;
|
|
85
84
|
return pointWindowSizes.get(P) || 1;
|
|
86
85
|
}
|
|
87
86
|
function assert0(n) {
|
|
@@ -90,6 +89,10 @@ function assert0(n) {
|
|
|
90
89
|
}
|
|
91
90
|
/**
|
|
92
91
|
* Elliptic curve multiplication of Point by scalar. Fragile.
|
|
92
|
+
* Table generation takes **30MB of ram and 10ms on high-end CPU**,
|
|
93
|
+
* but may take much longer on slow devices. Actual generation will happen on
|
|
94
|
+
* first call of `multiply()`. By default, `BASE` point is precomputed.
|
|
95
|
+
*
|
|
93
96
|
* Scalars should always be less than curve order: this should be checked inside of a curve itself.
|
|
94
97
|
* Creates precomputation tables for fast multiplication:
|
|
95
98
|
* - private scalar is split by fixed size windows of W bits
|
|
@@ -102,164 +105,162 @@ function assert0(n) {
|
|
|
102
105
|
* @todo Research returning 2d JS array of windows, instead of a single window.
|
|
103
106
|
* This would allow windows to be in different memory locations
|
|
104
107
|
*/
|
|
105
|
-
export
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
108
|
+
export class wNAF {
|
|
109
|
+
// Parametrized with a given Point class (not individual point)
|
|
110
|
+
constructor(Point, bits) {
|
|
111
|
+
this.BASE = Point.BASE;
|
|
112
|
+
this.ZERO = Point.ZERO;
|
|
113
|
+
this.Fn = Point.Fn;
|
|
114
|
+
this.bits = bits;
|
|
115
|
+
}
|
|
116
|
+
// non-const time multiplication ladder
|
|
117
|
+
_unsafeLadder(elm, n, p = this.ZERO) {
|
|
118
|
+
let d = elm;
|
|
119
|
+
while (n > _0n) {
|
|
120
|
+
if (n & _1n)
|
|
121
|
+
p = p.add(d);
|
|
122
|
+
d = d.double();
|
|
123
|
+
n >>= _1n;
|
|
124
|
+
}
|
|
125
|
+
return p;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Creates a wNAF precomputation window. Used for caching.
|
|
129
|
+
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
130
|
+
* Number of precomputed points depends on the curve size:
|
|
131
|
+
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
132
|
+
* - 𝑊 is the window size
|
|
133
|
+
* - 𝑛 is the bitlength of the curve order.
|
|
134
|
+
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
135
|
+
* @param point Point instance
|
|
136
|
+
* @param W window size
|
|
137
|
+
* @returns precomputed point tables flattened to a single array
|
|
138
|
+
*/
|
|
139
|
+
precomputeWindow(point, W) {
|
|
140
|
+
const { windows, windowSize } = calcWOpts(W, this.bits);
|
|
141
|
+
const points = [];
|
|
142
|
+
let p = point;
|
|
143
|
+
let base = p;
|
|
144
|
+
for (let window = 0; window < windows; window++) {
|
|
145
|
+
base = p;
|
|
146
|
+
points.push(base);
|
|
147
|
+
// i=1, bc we skip 0
|
|
148
|
+
for (let i = 1; i < windowSize; i++) {
|
|
149
|
+
base = base.add(p);
|
|
141
150
|
points.push(base);
|
|
142
|
-
// i=1, bc we skip 0
|
|
143
|
-
for (let i = 1; i < windowSize; i++) {
|
|
144
|
-
base = base.add(p);
|
|
145
|
-
points.push(base);
|
|
146
|
-
}
|
|
147
|
-
p = base.double();
|
|
148
151
|
}
|
|
149
|
-
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
152
|
+
p = base.double();
|
|
153
|
+
}
|
|
154
|
+
return points;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
158
|
+
* More compact implementation:
|
|
159
|
+
* https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
|
|
160
|
+
* @returns real and fake (for const-time) points
|
|
161
|
+
*/
|
|
162
|
+
wNAF(W, precomputes, n) {
|
|
163
|
+
// Scalar should be smaller than field order
|
|
164
|
+
if (!this.Fn.isValid(n))
|
|
165
|
+
throw new Error('invalid scalar');
|
|
166
|
+
// Accumulators
|
|
167
|
+
let p = this.ZERO;
|
|
168
|
+
let f = this.BASE;
|
|
169
|
+
// This code was first written with assumption that 'f' and 'p' will never be infinity point:
|
|
170
|
+
// since each addition is multiplied by 2 ** W, it cannot cancel each other. However,
|
|
171
|
+
// there is negate now: it is possible that negated element from low value
|
|
172
|
+
// would be the same as high element, which will create carry into next window.
|
|
173
|
+
// It's not obvious how this can fail, but still worth investigating later.
|
|
174
|
+
const wo = calcWOpts(W, this.bits);
|
|
175
|
+
for (let window = 0; window < wo.windows; window++) {
|
|
176
|
+
// (n === _0n) is handled and not early-exited. isEven and offsetF are used for noise
|
|
177
|
+
const { nextN, offset, isZero, isNeg, isNegF, offsetF } = calcOffsets(n, window, wo);
|
|
178
|
+
n = nextN;
|
|
179
|
+
if (isZero) {
|
|
180
|
+
// bits are 0: add garbage to fake point
|
|
181
|
+
// Important part for const-time getPublicKey: add random "noise" point to f.
|
|
182
|
+
f = f.add(negateCt(isNegF, precomputes[offsetF]));
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
// bits are 1: add to result point
|
|
186
|
+
p = p.add(negateCt(isNeg, precomputes[offset]));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
assert0(n);
|
|
190
|
+
// Return both real and fake points: JIT won't eliminate f.
|
|
191
|
+
// At this point there is a way to F be infinity-point even if p is not,
|
|
192
|
+
// which makes it less const-time: around 1 bigint multiply.
|
|
193
|
+
return { p, f };
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
|
|
197
|
+
* @param acc accumulator point to add result of multiplication
|
|
198
|
+
* @returns point
|
|
199
|
+
*/
|
|
200
|
+
wNAFUnsafe(W, precomputes, n, acc = this.ZERO) {
|
|
201
|
+
const wo = calcWOpts(W, this.bits);
|
|
202
|
+
for (let window = 0; window < wo.windows; window++) {
|
|
203
|
+
if (n === _0n)
|
|
204
|
+
break; // Early-exit, skip 0 value
|
|
205
|
+
const { nextN, offset, isZero, isNeg } = calcOffsets(n, window, wo);
|
|
206
|
+
n = nextN;
|
|
207
|
+
if (isZero) {
|
|
208
|
+
// Window bits are 0: skip processing.
|
|
209
|
+
// Move to next window.
|
|
210
|
+
continue;
|
|
186
211
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
// which makes it less const-time: around 1 bigint multiply.
|
|
191
|
-
return { p, f };
|
|
192
|
-
},
|
|
193
|
-
/**
|
|
194
|
-
* Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
|
|
195
|
-
* @param W window size
|
|
196
|
-
* @param precomputes precomputed tables
|
|
197
|
-
* @param n scalar (we don't check here, but should be less than curve order)
|
|
198
|
-
* @param acc accumulator point to add result of multiplication
|
|
199
|
-
* @returns point
|
|
200
|
-
*/
|
|
201
|
-
wNAFUnsafe(W, precomputes, n, acc = c.ZERO) {
|
|
202
|
-
const wo = calcWOpts(W, bits);
|
|
203
|
-
for (let window = 0; window < wo.windows; window++) {
|
|
204
|
-
if (n === _0n)
|
|
205
|
-
break; // Early-exit, skip 0 value
|
|
206
|
-
const { nextN, offset, isZero, isNeg } = calcOffsets(n, window, wo);
|
|
207
|
-
n = nextN;
|
|
208
|
-
if (isZero) {
|
|
209
|
-
// Window bits are 0: skip processing.
|
|
210
|
-
// Move to next window.
|
|
211
|
-
continue;
|
|
212
|
-
}
|
|
213
|
-
else {
|
|
214
|
-
const item = precomputes[offset];
|
|
215
|
-
acc = acc.add(isNeg ? item.negate() : item); // Re-using acc allows to save adds in MSM
|
|
216
|
-
}
|
|
212
|
+
else {
|
|
213
|
+
const item = precomputes[offset];
|
|
214
|
+
acc = acc.add(isNeg ? item.negate() : item); // Re-using acc allows to save adds in MSM
|
|
217
215
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
216
|
+
}
|
|
217
|
+
assert0(n);
|
|
218
|
+
return acc;
|
|
219
|
+
}
|
|
220
|
+
getPrecomputes(W, point, transform) {
|
|
221
|
+
// Calculate precomputes on a first run, reuse them after
|
|
222
|
+
let comp = pointPrecomputes.get(point);
|
|
223
|
+
if (!comp) {
|
|
224
|
+
comp = this.precomputeWindow(point, W);
|
|
225
|
+
if (W !== 1) {
|
|
226
|
+
// Doing transform outside of if brings 15% perf hit
|
|
227
|
+
if (typeof transform === 'function')
|
|
228
|
+
comp = transform(comp);
|
|
229
|
+
pointPrecomputes.set(point, comp);
|
|
232
230
|
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
return this.
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
}
|
|
231
|
+
}
|
|
232
|
+
return comp;
|
|
233
|
+
}
|
|
234
|
+
cached(point, scalar, transform) {
|
|
235
|
+
const W = getW(point);
|
|
236
|
+
return this.wNAF(W, this.getPrecomputes(W, point, transform), scalar);
|
|
237
|
+
}
|
|
238
|
+
unsafe(point, scalar, transform, prev) {
|
|
239
|
+
const W = getW(point);
|
|
240
|
+
if (W === 1)
|
|
241
|
+
return this._unsafeLadder(point, scalar, prev); // For W=1 ladder is ~x2 faster
|
|
242
|
+
return this.wNAFUnsafe(W, this.getPrecomputes(W, point, transform), scalar, prev);
|
|
243
|
+
}
|
|
244
|
+
// We calculate precomputes for elliptic curve point multiplication
|
|
245
|
+
// using windowed method. This specifies window size and
|
|
246
|
+
// stores precomputed values. Usually only base point would be precomputed.
|
|
247
|
+
createCache(P, W) {
|
|
248
|
+
validateW(W, this.bits);
|
|
249
|
+
pointWindowSizes.set(P, W);
|
|
250
|
+
pointPrecomputes.delete(P);
|
|
251
|
+
}
|
|
252
|
+
hasCache(elm) {
|
|
253
|
+
return getW(elm) !== 1;
|
|
254
|
+
}
|
|
254
255
|
}
|
|
255
256
|
/**
|
|
256
257
|
* Endomorphism-specific multiplication for Koblitz curves.
|
|
257
258
|
* Cost: 128 dbl, 0-256 adds.
|
|
258
259
|
*/
|
|
259
|
-
export function mulEndoUnsafe(
|
|
260
|
+
export function mulEndoUnsafe(Point, point, k1, k2) {
|
|
260
261
|
let acc = point;
|
|
261
|
-
let p1 =
|
|
262
|
-
let p2 =
|
|
262
|
+
let p1 = Point.ZERO;
|
|
263
|
+
let p2 = Point.ZERO;
|
|
263
264
|
while (k1 > _0n || k2 > _0n) {
|
|
264
265
|
if (k1 & _1n)
|
|
265
266
|
p1 = p1.add(acc);
|
|
@@ -279,7 +280,7 @@ export function mulEndoUnsafe(c, point, k1, k2) {
|
|
|
279
280
|
* @param c Curve Point constructor
|
|
280
281
|
* @param fieldN field over CURVE.N - important that it's not over CURVE.P
|
|
281
282
|
* @param points array of L curve points
|
|
282
|
-
* @param scalars array of L scalars (aka
|
|
283
|
+
* @param scalars array of L scalars (aka secret keys / bigints)
|
|
283
284
|
*/
|
|
284
285
|
export function pippenger(c, fieldN, points, scalars) {
|
|
285
286
|
// If we split scalars by some window (let's say 8 bits), every chunk will only
|