@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.
Files changed (179) hide show
  1. package/README.md +186 -206
  2. package/_shortw_utils.d.ts +1 -0
  3. package/_shortw_utils.d.ts.map +1 -1
  4. package/_shortw_utils.js +1 -0
  5. package/_shortw_utils.js.map +1 -1
  6. package/abstract/bls.d.ts +87 -62
  7. package/abstract/bls.d.ts.map +1 -1
  8. package/abstract/bls.js +170 -163
  9. package/abstract/bls.js.map +1 -1
  10. package/abstract/curve.d.ts +109 -23
  11. package/abstract/curve.d.ts.map +1 -1
  12. package/abstract/curve.js +158 -156
  13. package/abstract/curve.js.map +1 -1
  14. package/abstract/edwards.d.ts +124 -70
  15. package/abstract/edwards.d.ts.map +1 -1
  16. package/abstract/edwards.js +212 -62
  17. package/abstract/edwards.js.map +1 -1
  18. package/abstract/hash-to-curve.d.ts +8 -4
  19. package/abstract/hash-to-curve.d.ts.map +1 -1
  20. package/abstract/hash-to-curve.js +23 -11
  21. package/abstract/hash-to-curve.js.map +1 -1
  22. package/abstract/modular.d.ts +8 -3
  23. package/abstract/modular.d.ts.map +1 -1
  24. package/abstract/modular.js +79 -35
  25. package/abstract/modular.js.map +1 -1
  26. package/abstract/montgomery.d.ts +17 -4
  27. package/abstract/montgomery.d.ts.map +1 -1
  28. package/abstract/montgomery.js +19 -3
  29. package/abstract/montgomery.js.map +1 -1
  30. package/abstract/tower.d.ts +3 -3
  31. package/abstract/tower.d.ts.map +1 -1
  32. package/abstract/tower.js.map +1 -1
  33. package/abstract/weierstrass.d.ts +142 -116
  34. package/abstract/weierstrass.d.ts.map +1 -1
  35. package/abstract/weierstrass.js +414 -335
  36. package/abstract/weierstrass.js.map +1 -1
  37. package/bls12-381.d.ts.map +1 -1
  38. package/bls12-381.js +4 -4
  39. package/bls12-381.js.map +1 -1
  40. package/ed25519.d.ts +52 -66
  41. package/ed25519.d.ts.map +1 -1
  42. package/ed25519.js +128 -155
  43. package/ed25519.js.map +1 -1
  44. package/ed448.d.ts +57 -58
  45. package/ed448.d.ts.map +1 -1
  46. package/ed448.js +114 -131
  47. package/ed448.js.map +1 -1
  48. package/esm/_shortw_utils.d.ts +1 -0
  49. package/esm/_shortw_utils.d.ts.map +1 -1
  50. package/esm/_shortw_utils.js +1 -0
  51. package/esm/_shortw_utils.js.map +1 -1
  52. package/esm/abstract/bls.d.ts +87 -62
  53. package/esm/abstract/bls.d.ts.map +1 -1
  54. package/esm/abstract/bls.js +171 -164
  55. package/esm/abstract/bls.js.map +1 -1
  56. package/esm/abstract/curve.d.ts +109 -23
  57. package/esm/abstract/curve.d.ts.map +1 -1
  58. package/esm/abstract/curve.js +156 -155
  59. package/esm/abstract/curve.js.map +1 -1
  60. package/esm/abstract/edwards.d.ts +124 -70
  61. package/esm/abstract/edwards.d.ts.map +1 -1
  62. package/esm/abstract/edwards.js +210 -62
  63. package/esm/abstract/edwards.js.map +1 -1
  64. package/esm/abstract/hash-to-curve.d.ts +8 -4
  65. package/esm/abstract/hash-to-curve.d.ts.map +1 -1
  66. package/esm/abstract/hash-to-curve.js +22 -11
  67. package/esm/abstract/hash-to-curve.js.map +1 -1
  68. package/esm/abstract/modular.d.ts +8 -3
  69. package/esm/abstract/modular.d.ts.map +1 -1
  70. package/esm/abstract/modular.js +79 -35
  71. package/esm/abstract/modular.js.map +1 -1
  72. package/esm/abstract/montgomery.d.ts +17 -4
  73. package/esm/abstract/montgomery.d.ts.map +1 -1
  74. package/esm/abstract/montgomery.js +19 -3
  75. package/esm/abstract/montgomery.js.map +1 -1
  76. package/esm/abstract/tower.d.ts +3 -3
  77. package/esm/abstract/tower.d.ts.map +1 -1
  78. package/esm/abstract/tower.js.map +1 -1
  79. package/esm/abstract/weierstrass.d.ts +142 -116
  80. package/esm/abstract/weierstrass.d.ts.map +1 -1
  81. package/esm/abstract/weierstrass.js +411 -333
  82. package/esm/abstract/weierstrass.js.map +1 -1
  83. package/esm/bls12-381.d.ts.map +1 -1
  84. package/esm/bls12-381.js +4 -4
  85. package/esm/bls12-381.js.map +1 -1
  86. package/esm/ed25519.d.ts +52 -66
  87. package/esm/ed25519.d.ts.map +1 -1
  88. package/esm/ed25519.js +131 -157
  89. package/esm/ed25519.js.map +1 -1
  90. package/esm/ed448.d.ts +57 -58
  91. package/esm/ed448.d.ts.map +1 -1
  92. package/esm/ed448.js +116 -132
  93. package/esm/ed448.js.map +1 -1
  94. package/esm/index.js +7 -9
  95. package/esm/index.js.map +1 -1
  96. package/esm/jubjub.d.ts +3 -3
  97. package/esm/jubjub.d.ts.map +1 -1
  98. package/esm/jubjub.js +3 -3
  99. package/esm/jubjub.js.map +1 -1
  100. package/esm/misc.d.ts +3 -5
  101. package/esm/misc.d.ts.map +1 -1
  102. package/esm/misc.js +0 -3
  103. package/esm/misc.js.map +1 -1
  104. package/esm/nist.d.ts +0 -6
  105. package/esm/nist.d.ts.map +1 -1
  106. package/esm/nist.js +31 -15
  107. package/esm/nist.js.map +1 -1
  108. package/esm/p256.d.ts +4 -0
  109. package/esm/p256.d.ts.map +1 -1
  110. package/esm/p256.js +4 -0
  111. package/esm/p256.js.map +1 -1
  112. package/esm/p384.d.ts +4 -1
  113. package/esm/p384.d.ts.map +1 -1
  114. package/esm/p384.js +4 -1
  115. package/esm/p384.js.map +1 -1
  116. package/esm/p521.d.ts +4 -0
  117. package/esm/p521.d.ts.map +1 -1
  118. package/esm/p521.js +4 -0
  119. package/esm/p521.js.map +1 -1
  120. package/esm/secp256k1.d.ts +32 -15
  121. package/esm/secp256k1.d.ts.map +1 -1
  122. package/esm/secp256k1.js +72 -67
  123. package/esm/secp256k1.js.map +1 -1
  124. package/esm/utils.d.ts +1 -1
  125. package/esm/utils.js +1 -1
  126. package/index.js +7 -9
  127. package/index.js.map +1 -1
  128. package/jubjub.d.ts +3 -3
  129. package/jubjub.d.ts.map +1 -1
  130. package/jubjub.js +3 -3
  131. package/jubjub.js.map +1 -1
  132. package/misc.d.ts +3 -5
  133. package/misc.d.ts.map +1 -1
  134. package/misc.js +0 -3
  135. package/misc.js.map +1 -1
  136. package/nist.d.ts +0 -6
  137. package/nist.d.ts.map +1 -1
  138. package/nist.js +31 -15
  139. package/nist.js.map +1 -1
  140. package/p256.d.ts +4 -0
  141. package/p256.d.ts.map +1 -1
  142. package/p256.js +4 -0
  143. package/p256.js.map +1 -1
  144. package/p384.d.ts +4 -1
  145. package/p384.d.ts.map +1 -1
  146. package/p384.js +4 -1
  147. package/p384.js.map +1 -1
  148. package/p521.d.ts +4 -0
  149. package/p521.d.ts.map +1 -1
  150. package/p521.js +4 -0
  151. package/p521.js.map +1 -1
  152. package/package.json +4 -2
  153. package/secp256k1.d.ts +32 -15
  154. package/secp256k1.d.ts.map +1 -1
  155. package/secp256k1.js +70 -65
  156. package/secp256k1.js.map +1 -1
  157. package/src/_shortw_utils.ts +1 -0
  158. package/src/abstract/bls.ts +319 -257
  159. package/src/abstract/curve.ts +226 -170
  160. package/src/abstract/edwards.ts +350 -139
  161. package/src/abstract/hash-to-curve.ts +33 -16
  162. package/src/abstract/modular.ts +86 -35
  163. package/src/abstract/montgomery.ts +36 -9
  164. package/src/abstract/tower.ts +4 -4
  165. package/src/abstract/weierstrass.ts +567 -474
  166. package/src/bls12-381.ts +28 -20
  167. package/src/ed25519.ts +161 -179
  168. package/src/ed448.ts +150 -156
  169. package/src/index.ts +7 -9
  170. package/src/jubjub.ts +3 -3
  171. package/src/misc.ts +3 -7
  172. package/src/nist.ts +40 -16
  173. package/src/p256.ts +4 -0
  174. package/src/p384.ts +4 -2
  175. package/src/p521.ts +4 -0
  176. package/src/secp256k1.ts +91 -73
  177. package/src/utils.ts +1 -1
  178. package/utils.d.ts +1 -1
  179. package/utils.js +1 -1
@@ -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 Group<T>>(condition: boolean, item: T): T;
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<T>(c: ExtendedGroupConstructor<T>, property: 'pz' | 'ez', points: T[]): T[];
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 function wNAF<T extends Group<T>>(c: GroupConstructor<T>, bits: number): IWNAF<T>;
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>>(c: GroupConstructor<T>, point: T, k1: bigint, k2: bigint): {
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 private keys / bigints)
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":"AAOA,OAAO,EAAwB,KAAK,MAAM,EAA0B,MAAM,cAAc,CAAC;AAKzF,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI;IAC3B,CAAC,EAAE,CAAC,CAAC;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C,EAC1B,SAAS,GAAE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAM,GAC/B,IAAI,CAAC,CAAC,CAAC,CAiBT"}
@@ -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, property, points) {
22
- const getz = property === 'pz' ? (p) => p.pz : (p) => p.ez;
23
- const toInv = FpInvertBatch(c.Fp, points.map(getz));
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 function wNAF(c, bits) {
106
- return {
107
- constTimeNegate: negateCt,
108
- hasPrecomputes(elm) {
109
- return getW(elm) !== 1;
110
- },
111
- // non-const time multiplication ladder
112
- unsafeLadder(elm, n, p = c.ZERO) {
113
- let d = elm;
114
- while (n > _0n) {
115
- if (n & _1n)
116
- p = p.add(d);
117
- d = d.double();
118
- n >>= _1n;
119
- }
120
- return p;
121
- },
122
- /**
123
- * Creates a wNAF precomputation window. Used for caching.
124
- * Default window size is set by `utils.precompute()` and is equal to 8.
125
- * Number of precomputed points depends on the curve size:
126
- * 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
127
- * - 𝑊 is the window size
128
- * - 𝑛 is the bitlength of the curve order.
129
- * For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
130
- * @param elm Point instance
131
- * @param W window size
132
- * @returns precomputed point tables flattened to a single array
133
- */
134
- precomputeWindow(elm, W) {
135
- const { windows, windowSize } = calcWOpts(W, bits);
136
- const points = [];
137
- let p = elm;
138
- let base = p;
139
- for (let window = 0; window < windows; window++) {
140
- base = p;
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
- return points;
150
- },
151
- /**
152
- * Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
153
- * @param W window size
154
- * @param precomputes precomputed tables
155
- * @param n scalar (we don't check here, but should be less than curve order)
156
- * @returns real and fake (for const-time) points
157
- */
158
- wNAF(W, precomputes, n) {
159
- // Smaller version:
160
- // https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
161
- // TODO: check the scalar is less than group order?
162
- // wNAF behavior is undefined otherwise. But have to carefully remove
163
- // other checks before wNAF. ORDER == bits here.
164
- // Accumulators
165
- let p = c.ZERO;
166
- let f = c.BASE;
167
- // This code was first written with assumption that 'f' and 'p' will never be infinity point:
168
- // since each addition is multiplied by 2 ** W, it cannot cancel each other. However,
169
- // there is negate now: it is possible that negated element from low value
170
- // would be the same as high element, which will create carry into next window.
171
- // It's not obvious how this can fail, but still worth investigating later.
172
- const wo = calcWOpts(W, bits);
173
- for (let window = 0; window < wo.windows; window++) {
174
- // (n === _0n) is handled and not early-exited. isEven and offsetF are used for noise
175
- const { nextN, offset, isZero, isNeg, isNegF, offsetF } = calcOffsets(n, window, wo);
176
- n = nextN;
177
- if (isZero) {
178
- // bits are 0: add garbage to fake point
179
- // Important part for const-time getPublicKey: add random "noise" point to f.
180
- f = f.add(negateCt(isNegF, precomputes[offsetF]));
181
- }
182
- else {
183
- // bits are 1: add to result point
184
- p = p.add(negateCt(isNeg, precomputes[offset]));
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
- assert0(n);
188
- // Return both real and fake points: JIT won't eliminate f.
189
- // At this point there is a way to F be infinity-point even if p is not,
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
- assert0(n);
219
- return acc;
220
- },
221
- getPrecomputes(W, P, transform) {
222
- // Calculate precomputes on a first run, reuse them after
223
- let comp = pointPrecomputes.get(P);
224
- if (!comp) {
225
- comp = this.precomputeWindow(P, W);
226
- if (W !== 1) {
227
- // Doing transform outside of if brings 15% perf hit
228
- if (typeof transform === 'function')
229
- comp = transform(comp);
230
- pointPrecomputes.set(P, comp);
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
- return comp;
234
- },
235
- wNAFCached(P, n, transform) {
236
- const W = getW(P);
237
- return this.wNAF(W, this.getPrecomputes(W, P, transform), n);
238
- },
239
- wNAFCachedUnsafe(P, n, transform, prev) {
240
- const W = getW(P);
241
- if (W === 1)
242
- return this.unsafeLadder(P, n, prev); // For W=1 ladder is ~x2 faster
243
- return this.wNAFUnsafe(W, this.getPrecomputes(W, P, transform), n, prev);
244
- },
245
- // We calculate precomputes for elliptic curve point multiplication
246
- // using windowed method. This specifies window size and
247
- // stores precomputed values. Usually only base point would be precomputed.
248
- setWindowSize(P, W) {
249
- validateW(W, bits);
250
- pointWindowSizes.set(P, W);
251
- pointPrecomputes.delete(P);
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(c, point, k1, k2) {
260
+ export function mulEndoUnsafe(Point, point, k1, k2) {
260
261
  let acc = point;
261
- let p1 = c.ZERO;
262
- let p2 = c.ZERO;
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 private keys / bigints)
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