@noble/curves 0.6.4 → 0.7.1

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 (172) hide show
  1. package/README.md +486 -298
  2. package/{lib/_shortw_utils.d.ts → _shortw_utils.d.ts} +1 -1
  3. package/_shortw_utils.d.ts.map +1 -0
  4. package/{lib/_shortw_utils.js → _shortw_utils.js} +2 -0
  5. package/_shortw_utils.js.map +1 -0
  6. package/{lib/abstract → abstract}/bls.d.ts +4 -9
  7. package/abstract/bls.d.ts.map +1 -0
  8. package/{lib/abstract → abstract}/bls.js +13 -26
  9. package/abstract/bls.js.map +1 -0
  10. package/{lib/abstract → abstract}/curve.d.ts +1 -0
  11. package/abstract/curve.d.ts.map +1 -0
  12. package/{lib/abstract → abstract}/curve.js +1 -0
  13. package/abstract/curve.js.map +1 -0
  14. package/{lib/abstract → abstract}/edwards.d.ts +1 -0
  15. package/abstract/edwards.d.ts.map +1 -0
  16. package/{lib/abstract → abstract}/edwards.js +9 -15
  17. package/abstract/edwards.js.map +1 -0
  18. package/{lib/abstract → abstract}/hash-to-curve.d.ts +5 -5
  19. package/abstract/hash-to-curve.d.ts.map +1 -0
  20. package/{lib/abstract → abstract}/hash-to-curve.js +41 -38
  21. package/abstract/hash-to-curve.js.map +1 -0
  22. package/{lib/abstract → abstract}/modular.d.ts +1 -0
  23. package/abstract/modular.d.ts.map +1 -0
  24. package/{lib/abstract → abstract}/modular.js +2 -1
  25. package/abstract/modular.js.map +1 -0
  26. package/{lib/abstract → abstract}/montgomery.d.ts +4 -3
  27. package/abstract/montgomery.d.ts.map +1 -0
  28. package/{lib/abstract → abstract}/montgomery.js +12 -9
  29. package/abstract/montgomery.js.map +1 -0
  30. package/{lib/abstract → abstract}/poseidon.d.ts +1 -0
  31. package/abstract/poseidon.d.ts.map +1 -0
  32. package/{lib/abstract → abstract}/poseidon.js +1 -0
  33. package/abstract/poseidon.js.map +1 -0
  34. package/{lib/abstract → abstract}/utils.d.ts +12 -1
  35. package/abstract/utils.d.ts.map +1 -0
  36. package/{lib/abstract → abstract}/utils.js +96 -10
  37. package/abstract/utils.js.map +1 -0
  38. package/{lib/abstract → abstract}/weierstrass.d.ts +6 -6
  39. package/abstract/weierstrass.d.ts.map +1 -0
  40. package/{lib/abstract → abstract}/weierstrass.js +74 -115
  41. package/abstract/weierstrass.js.map +1 -0
  42. package/{lib/bls12-381.d.ts → bls12-381.d.ts} +1 -0
  43. package/bls12-381.d.ts.map +1 -0
  44. package/{lib/bls12-381.js → bls12-381.js} +41 -7
  45. package/bls12-381.js.map +1 -0
  46. package/{lib/bn.d.ts → bn.d.ts} +1 -0
  47. package/bn.d.ts.map +1 -0
  48. package/{lib/bn.js → bn.js} +1 -0
  49. package/bn.js.map +1 -0
  50. package/{lib/ed25519.d.ts → ed25519.d.ts} +2 -1
  51. package/ed25519.d.ts.map +1 -0
  52. package/{lib/ed25519.js → ed25519.js} +6 -5
  53. package/ed25519.js.map +1 -0
  54. package/{lib/ed448.d.ts → ed448.d.ts} +2 -1
  55. package/ed448.d.ts.map +1 -0
  56. package/{lib/ed448.js → ed448.js} +4 -3
  57. package/ed448.js.map +1 -0
  58. package/{lib/esm → esm}/_shortw_utils.js +2 -0
  59. package/esm/_shortw_utils.js.map +1 -0
  60. package/{lib/esm → esm}/abstract/bls.js +14 -27
  61. package/esm/abstract/bls.js.map +1 -0
  62. package/{lib/esm → esm}/abstract/curve.js +1 -0
  63. package/esm/abstract/curve.js.map +1 -0
  64. package/{lib/esm → esm}/abstract/edwards.js +9 -15
  65. package/esm/abstract/edwards.js.map +1 -0
  66. package/{lib/esm → esm}/abstract/hash-to-curve.js +40 -36
  67. package/esm/abstract/hash-to-curve.js.map +1 -0
  68. package/{lib/esm → esm}/abstract/modular.js +2 -1
  69. package/esm/abstract/modular.js.map +1 -0
  70. package/{lib/esm → esm}/abstract/montgomery.js +12 -9
  71. package/esm/abstract/montgomery.js.map +1 -0
  72. package/{lib/esm → esm}/abstract/poseidon.js +1 -0
  73. package/esm/abstract/poseidon.js.map +1 -0
  74. package/{lib/esm → esm}/abstract/utils.js +93 -9
  75. package/esm/abstract/utils.js.map +1 -0
  76. package/{lib/esm → esm}/abstract/weierstrass.js +74 -115
  77. package/esm/abstract/weierstrass.js.map +1 -0
  78. package/{lib/esm → esm}/bls12-381.js +41 -7
  79. package/esm/bls12-381.js.map +1 -0
  80. package/{lib/esm → esm}/bn.js +1 -0
  81. package/esm/bn.js.map +1 -0
  82. package/{lib/esm → esm}/ed25519.js +7 -6
  83. package/esm/ed25519.js.map +1 -0
  84. package/{lib/esm → esm}/ed448.js +4 -3
  85. package/esm/ed448.js.map +1 -0
  86. package/{lib → esm}/index.js +1 -0
  87. package/esm/index.js.map +1 -0
  88. package/{lib/esm → esm}/jubjub.js +1 -0
  89. package/esm/jubjub.js.map +1 -0
  90. package/{lib/esm → esm}/p192.js +1 -0
  91. package/esm/p192.js.map +1 -0
  92. package/{lib/esm → esm}/p224.js +1 -0
  93. package/esm/p224.js.map +1 -0
  94. package/{lib/esm → esm}/p256.js +2 -1
  95. package/esm/p256.js.map +1 -0
  96. package/{lib/esm → esm}/p384.js +2 -1
  97. package/esm/p384.js.map +1 -0
  98. package/{lib/esm → esm}/p521.js +2 -1
  99. package/esm/p521.js.map +1 -0
  100. package/{lib/esm → esm}/package.json +0 -0
  101. package/{lib/esm → esm}/pasta.js +1 -0
  102. package/esm/pasta.js.map +1 -0
  103. package/{lib/esm → esm}/secp256k1.js +51 -50
  104. package/esm/secp256k1.js.map +1 -0
  105. package/{lib/esm → esm}/stark.js +5 -4
  106. package/esm/stark.js.map +1 -0
  107. package/index.d.ts +1 -0
  108. package/index.d.ts.map +1 -0
  109. package/index.js +3 -0
  110. package/index.js.map +1 -0
  111. package/{lib/jubjub.d.ts → jubjub.d.ts} +1 -0
  112. package/jubjub.d.ts.map +1 -0
  113. package/{lib/jubjub.js → jubjub.js} +1 -0
  114. package/jubjub.js.map +1 -0
  115. package/{lib/p192.d.ts → p192.d.ts} +1 -2
  116. package/p192.d.ts.map +1 -0
  117. package/{lib/p192.js → p192.js} +1 -0
  118. package/p192.js.map +1 -0
  119. package/{lib/p224.d.ts → p224.d.ts} +1 -2
  120. package/p224.d.ts.map +1 -0
  121. package/{lib/p224.js → p224.js} +1 -0
  122. package/p224.js.map +1 -0
  123. package/{lib/p256.d.ts → p256.d.ts} +2 -3
  124. package/p256.d.ts.map +1 -0
  125. package/{lib/p256.js → p256.js} +2 -1
  126. package/p256.js.map +1 -0
  127. package/{lib/p384.d.ts → p384.d.ts} +2 -3
  128. package/p384.d.ts.map +1 -0
  129. package/{lib/p384.js → p384.js} +2 -1
  130. package/p384.js.map +1 -0
  131. package/{lib/p521.d.ts → p521.d.ts} +2 -3
  132. package/p521.d.ts.map +1 -0
  133. package/{lib/p521.js → p521.js} +2 -1
  134. package/p521.js.map +1 -0
  135. package/package.json +84 -79
  136. package/{lib/pasta.d.ts → pasta.d.ts} +1 -0
  137. package/pasta.d.ts.map +1 -0
  138. package/{lib/pasta.js → pasta.js} +1 -0
  139. package/pasta.js.map +1 -0
  140. package/{lib/secp256k1.d.ts → secp256k1.d.ts} +20 -6
  141. package/secp256k1.d.ts.map +1 -0
  142. package/{lib/secp256k1.js → secp256k1.js} +48 -47
  143. package/secp256k1.js.map +1 -0
  144. package/src/_shortw_utils.ts +20 -0
  145. package/src/abstract/bls.ts +376 -0
  146. package/src/abstract/curve.ts +199 -0
  147. package/src/abstract/edwards.ts +479 -0
  148. package/src/abstract/hash-to-curve.ts +220 -0
  149. package/src/abstract/modular.ts +417 -0
  150. package/src/abstract/montgomery.ts +186 -0
  151. package/src/abstract/poseidon.ts +119 -0
  152. package/src/abstract/utils.ts +246 -0
  153. package/src/abstract/weierstrass.ts +1177 -0
  154. package/src/bls12-381.ts +1274 -0
  155. package/src/bn.ts +21 -0
  156. package/src/ed25519.ts +428 -0
  157. package/src/ed448.ts +241 -0
  158. package/{lib/esm/index.js → src/index.ts} +0 -1
  159. package/src/jubjub.ts +58 -0
  160. package/src/p192.ts +25 -0
  161. package/src/p224.ts +25 -0
  162. package/src/p256.ts +53 -0
  163. package/src/p384.ts +57 -0
  164. package/src/p521.ts +57 -0
  165. package/src/pasta.ts +31 -0
  166. package/src/secp256k1.ts +270 -0
  167. package/src/stark.ts +356 -0
  168. package/{lib/stark.d.ts → stark.d.ts} +1 -1
  169. package/stark.d.ts.map +1 -0
  170. package/{lib/stark.js → stark.js} +5 -4
  171. package/stark.js.map +1 -0
  172. package/lib/index.d.ts +0 -0
@@ -0,0 +1,479 @@
1
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2
+ // Twisted Edwards curve. The formula is: ax² + y² = 1 + dx²y²
3
+ import { mod } from './modular.js';
4
+ import * as ut from './utils.js';
5
+ import { ensureBytes, FHash, Hex } from './utils.js';
6
+ import { Group, GroupConstructor, wNAF, BasicCurve, validateBasic, AffinePoint } from './curve.js';
7
+
8
+ // Be friendly to bad ECMAScript parsers by not using bigint literals like 123n
9
+ const _0n = BigInt(0);
10
+ const _1n = BigInt(1);
11
+ const _2n = BigInt(2);
12
+ const _8n = BigInt(8);
13
+
14
+ // Edwards curves must declare params a & d.
15
+ export type CurveType = BasicCurve<bigint> & {
16
+ a: bigint; // curve param a
17
+ d: bigint; // curve param d
18
+ hash: FHash; // Hashing
19
+ randomBytes: (bytesLength?: number) => Uint8Array; // CSPRNG
20
+ adjustScalarBytes?: (bytes: Uint8Array) => Uint8Array; // clears bits to get valid field elemtn
21
+ domain?: (data: Uint8Array, ctx: Uint8Array, phflag: boolean) => Uint8Array; // Used for hashing
22
+ uvRatio?: (u: bigint, v: bigint) => { isValid: boolean; value: bigint }; // Ratio √(u/v)
23
+ preHash?: FHash; // RFC 8032 pre-hashing of messages to sign() / verify()
24
+ mapToCurve?: (scalar: bigint[]) => AffinePoint<bigint>; // for hash-to-curve standard
25
+ };
26
+
27
+ function validateOpts(curve: CurveType) {
28
+ const opts = validateBasic(curve);
29
+ ut.validateObject(
30
+ curve,
31
+ {
32
+ hash: 'function',
33
+ a: 'bigint',
34
+ d: 'bigint',
35
+ randomBytes: 'function',
36
+ },
37
+ {
38
+ adjustScalarBytes: 'function',
39
+ domain: 'function',
40
+ uvRatio: 'function',
41
+ mapToCurve: 'function',
42
+ }
43
+ );
44
+ // Set defaults
45
+ return Object.freeze({ ...opts } as const);
46
+ }
47
+
48
+ // Instance of Extended Point with coordinates in X, Y, Z, T
49
+ export interface ExtPointType extends Group<ExtPointType> {
50
+ readonly ex: bigint;
51
+ readonly ey: bigint;
52
+ readonly ez: bigint;
53
+ readonly et: bigint;
54
+ assertValidity(): void;
55
+ multiply(scalar: bigint): ExtPointType;
56
+ multiplyUnsafe(scalar: bigint): ExtPointType;
57
+ isSmallOrder(): boolean;
58
+ isTorsionFree(): boolean;
59
+ clearCofactor(): ExtPointType;
60
+ toAffine(iz?: bigint): AffinePoint<bigint>;
61
+ }
62
+ // Static methods of Extended Point with coordinates in X, Y, Z, T
63
+ export interface ExtPointConstructor extends GroupConstructor<ExtPointType> {
64
+ new (x: bigint, y: bigint, z: bigint, t: bigint): ExtPointType;
65
+ fromAffine(p: AffinePoint<bigint>): ExtPointType;
66
+ fromHex(hex: Hex): ExtPointType;
67
+ fromPrivateKey(privateKey: Hex): ExtPointType;
68
+ }
69
+
70
+ export type CurveFn = {
71
+ CURVE: ReturnType<typeof validateOpts>;
72
+ getPublicKey: (privateKey: Hex) => Uint8Array;
73
+ sign: (message: Hex, privateKey: Hex) => Uint8Array;
74
+ verify: (sig: Hex, message: Hex, publicKey: Hex) => boolean;
75
+ ExtendedPoint: ExtPointConstructor;
76
+ utils: {
77
+ randomPrivateKey: () => Uint8Array;
78
+ getExtendedPublicKey: (key: Hex) => {
79
+ head: Uint8Array;
80
+ prefix: Uint8Array;
81
+ scalar: bigint;
82
+ point: ExtPointType;
83
+ pointBytes: Uint8Array;
84
+ };
85
+ };
86
+ };
87
+
88
+ // It is not generic twisted curve for now, but ed25519/ed448 generic implementation
89
+ export function twistedEdwards(curveDef: CurveType): CurveFn {
90
+ const CURVE = validateOpts(curveDef) as ReturnType<typeof validateOpts>;
91
+ const { Fp, n: CURVE_ORDER, preHash, hash: cHash, randomBytes, nByteLength, h: cofactor } = CURVE;
92
+ const MASK = _2n ** BigInt(nByteLength * 8);
93
+ const modP = Fp.create; // Function overrides
94
+
95
+ // sqrt(u/v)
96
+ const uvRatio =
97
+ CURVE.uvRatio ||
98
+ ((u: bigint, v: bigint) => {
99
+ try {
100
+ return { isValid: true, value: Fp.sqrt(u * Fp.inv(v)) };
101
+ } catch (e) {
102
+ return { isValid: false, value: _0n };
103
+ }
104
+ });
105
+ const adjustScalarBytes = CURVE.adjustScalarBytes || ((bytes: Uint8Array) => bytes); // NOOP
106
+ const domain =
107
+ CURVE.domain ||
108
+ ((data: Uint8Array, ctx: Uint8Array, phflag: boolean) => {
109
+ if (ctx.length || phflag) throw new Error('Contexts/pre-hash are not supported');
110
+ return data;
111
+ }); // NOOP
112
+ const inBig = (n: bigint) => typeof n === 'bigint' && 0n < n; // n in [1..]
113
+ const inRange = (n: bigint, max: bigint) => inBig(n) && inBig(max) && n < max; // n in [1..max-1]
114
+ const in0MaskRange = (n: bigint) => n === _0n || inRange(n, MASK); // n in [0..MASK-1]
115
+ function assertInRange(n: bigint, max: bigint) {
116
+ // n in [1..max-1]
117
+ if (inRange(n, max)) return n;
118
+ throw new Error(`Expected valid scalar < ${max}, got ${typeof n} ${n}`);
119
+ }
120
+ function assertGE0(n: bigint) {
121
+ // n in [0..CURVE_ORDER-1]
122
+ return n === _0n ? n : assertInRange(n, CURVE_ORDER); // GE = prime subgroup, not full group
123
+ }
124
+ const pointPrecomputes = new Map<Point, Point[]>();
125
+ function isPoint(other: unknown) {
126
+ if (!(other instanceof Point)) throw new Error('ExtendedPoint expected');
127
+ }
128
+ // Extended Point works in extended coordinates: (x, y, z, t) ∋ (x=x/z, y=y/z, t=xy).
129
+ // https://en.wikipedia.org/wiki/Twisted_Edwards_curve#Extended_coordinates
130
+ class Point implements ExtPointType {
131
+ static readonly BASE = new Point(CURVE.Gx, CURVE.Gy, _1n, modP(CURVE.Gx * CURVE.Gy));
132
+ static readonly ZERO = new Point(_0n, _1n, _1n, _0n); // 0, 1, 1, 0
133
+
134
+ constructor(
135
+ readonly ex: bigint,
136
+ readonly ey: bigint,
137
+ readonly ez: bigint,
138
+ readonly et: bigint
139
+ ) {
140
+ if (!in0MaskRange(ex)) throw new Error('x required');
141
+ if (!in0MaskRange(ey)) throw new Error('y required');
142
+ if (!in0MaskRange(ez)) throw new Error('z required');
143
+ if (!in0MaskRange(et)) throw new Error('t required');
144
+ }
145
+
146
+ get x(): bigint {
147
+ return this.toAffine().x;
148
+ }
149
+ get y(): bigint {
150
+ return this.toAffine().y;
151
+ }
152
+
153
+ static fromAffine(p: AffinePoint<bigint>): Point {
154
+ if (p instanceof Point) throw new Error('extended point not allowed');
155
+ const { x, y } = p || {};
156
+ if (!in0MaskRange(x) || !in0MaskRange(y)) throw new Error('invalid affine point');
157
+ return new Point(x, y, _1n, modP(x * y));
158
+ }
159
+ static normalizeZ(points: Point[]): Point[] {
160
+ const toInv = Fp.invertBatch(points.map((p) => p.ez));
161
+ return points.map((p, i) => p.toAffine(toInv[i])).map(Point.fromAffine);
162
+ }
163
+
164
+ // We calculate precomputes for elliptic curve point multiplication
165
+ // using windowed method. This specifies window size and
166
+ // stores precomputed values. Usually only base point would be precomputed.
167
+ _WINDOW_SIZE?: number;
168
+
169
+ // "Private method", don't use it directly
170
+ _setWindowSize(windowSize: number) {
171
+ this._WINDOW_SIZE = windowSize;
172
+ pointPrecomputes.delete(this);
173
+ }
174
+ // Not required for fromHex(), which always creates valid points.
175
+ // Could be useful for fromAffine().
176
+ assertValidity(): void {
177
+ const { a, d } = CURVE;
178
+ if (this.is0()) throw new Error('bad point: ZERO'); // TODO: optimize, with vars below?
179
+ // Equation in affine coordinates: ax² + y² = 1 + dx²y²
180
+ // Equation in projective coordinates (X/Z, Y/Z, Z): (aX² + Y²)Z² = Z⁴ + dX²Y²
181
+ const { ex: X, ey: Y, ez: Z, et: T } = this;
182
+ const X2 = modP(X * X); // X²
183
+ const Y2 = modP(Y * Y); // Y²
184
+ const Z2 = modP(Z * Z); // Z²
185
+ const Z4 = modP(Z2 * Z2); // Z⁴
186
+ const aX2 = modP(X2 * a); // aX²
187
+ const left = modP(Z2 * modP(aX2 + Y2)); // (aX² + Y²)Z²
188
+ const right = modP(Z4 + modP(d * modP(X2 * Y2))); // Z⁴ + dX²Y²
189
+ if (left !== right) throw new Error('bad point: equation left != right (1)');
190
+ // In Extended coordinates we also have T, which is x*y=T/Z: check X*Y == Z*T
191
+ const XY = modP(X * Y);
192
+ const ZT = modP(Z * T);
193
+ if (XY !== ZT) throw new Error('bad point: equation left != right (2)');
194
+ }
195
+
196
+ // Compare one point to another.
197
+ equals(other: Point): boolean {
198
+ isPoint(other);
199
+ const { ex: X1, ey: Y1, ez: Z1 } = this;
200
+ const { ex: X2, ey: Y2, ez: Z2 } = other;
201
+ const X1Z2 = modP(X1 * Z2);
202
+ const X2Z1 = modP(X2 * Z1);
203
+ const Y1Z2 = modP(Y1 * Z2);
204
+ const Y2Z1 = modP(Y2 * Z1);
205
+ return X1Z2 === X2Z1 && Y1Z2 === Y2Z1;
206
+ }
207
+
208
+ protected is0(): boolean {
209
+ return this.equals(Point.ZERO);
210
+ }
211
+
212
+ negate(): Point {
213
+ // Flips point sign to a negative one (-x, y in affine coords)
214
+ return new Point(modP(-this.ex), this.ey, this.ez, modP(-this.et));
215
+ }
216
+
217
+ // Fast algo for doubling Extended Point.
218
+ // https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#doubling-dbl-2008-hwcd
219
+ // Cost: 4M + 4S + 1*a + 6add + 1*2.
220
+ double(): Point {
221
+ const { a } = CURVE;
222
+ const { ex: X1, ey: Y1, ez: Z1 } = this;
223
+ const A = modP(X1 * X1); // A = X12
224
+ const B = modP(Y1 * Y1); // B = Y12
225
+ const C = modP(_2n * modP(Z1 * Z1)); // C = 2*Z12
226
+ const D = modP(a * A); // D = a*A
227
+ const x1y1 = X1 + Y1;
228
+ const E = modP(modP(x1y1 * x1y1) - A - B); // E = (X1+Y1)2-A-B
229
+ const G = D + B; // G = D+B
230
+ const F = G - C; // F = G-C
231
+ const H = D - B; // H = D-B
232
+ const X3 = modP(E * F); // X3 = E*F
233
+ const Y3 = modP(G * H); // Y3 = G*H
234
+ const T3 = modP(E * H); // T3 = E*H
235
+ const Z3 = modP(F * G); // Z3 = F*G
236
+ return new Point(X3, Y3, Z3, T3);
237
+ }
238
+
239
+ // Fast algo for adding 2 Extended Points.
240
+ // https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#addition-add-2008-hwcd
241
+ // Cost: 9M + 1*a + 1*d + 7add.
242
+ add(other: Point) {
243
+ isPoint(other);
244
+ const { a, d } = CURVE;
245
+ const { ex: X1, ey: Y1, ez: Z1, et: T1 } = this;
246
+ const { ex: X2, ey: Y2, ez: Z2, et: T2 } = other;
247
+ // Faster algo for adding 2 Extended Points when curve's a=-1.
248
+ // http://hyperelliptic.org/EFD/g1p/auto-twisted-extended-1.html#addition-add-2008-hwcd-4
249
+ // Cost: 8M + 8add + 2*2.
250
+ // Note: It does not check whether the `other` point is valid.
251
+ if (a === BigInt(-1)) {
252
+ const A = modP((Y1 - X1) * (Y2 + X2));
253
+ const B = modP((Y1 + X1) * (Y2 - X2));
254
+ const F = modP(B - A);
255
+ if (F === _0n) return this.double(); // Same point. Tests say it doesn't affect timing
256
+ const C = modP(Z1 * _2n * T2);
257
+ const D = modP(T1 * _2n * Z2);
258
+ const E = D + C;
259
+ const G = B + A;
260
+ const H = D - C;
261
+ const X3 = modP(E * F);
262
+ const Y3 = modP(G * H);
263
+ const T3 = modP(E * H);
264
+ const Z3 = modP(F * G);
265
+ return new Point(X3, Y3, Z3, T3);
266
+ }
267
+ const A = modP(X1 * X2); // A = X1*X2
268
+ const B = modP(Y1 * Y2); // B = Y1*Y2
269
+ const C = modP(T1 * d * T2); // C = T1*d*T2
270
+ const D = modP(Z1 * Z2); // D = Z1*Z2
271
+ const E = modP((X1 + Y1) * (X2 + Y2) - A - B); // E = (X1+Y1)*(X2+Y2)-A-B
272
+ const F = D - C; // F = D-C
273
+ const G = D + C; // G = D+C
274
+ const H = modP(B - a * A); // H = B-a*A
275
+ const X3 = modP(E * F); // X3 = E*F
276
+ const Y3 = modP(G * H); // Y3 = G*H
277
+ const T3 = modP(E * H); // T3 = E*H
278
+ const Z3 = modP(F * G); // Z3 = F*G
279
+
280
+ return new Point(X3, Y3, Z3, T3);
281
+ }
282
+
283
+ subtract(other: Point): Point {
284
+ return this.add(other.negate());
285
+ }
286
+
287
+ private wNAF(n: bigint): { p: Point; f: Point } {
288
+ return wnaf.wNAFCached(this, pointPrecomputes, n, Point.normalizeZ);
289
+ }
290
+
291
+ // Constant-time multiplication.
292
+ multiply(scalar: bigint): Point {
293
+ const { p, f } = this.wNAF(assertInRange(scalar, CURVE_ORDER));
294
+ return Point.normalizeZ([p, f])[0];
295
+ }
296
+
297
+ // Non-constant-time multiplication. Uses double-and-add algorithm.
298
+ // It's faster, but should only be used when you don't care about
299
+ // an exposed private key e.g. sig verification.
300
+ multiplyUnsafe(scalar: bigint): Point {
301
+ let n = assertGE0(scalar);
302
+ if (n === _0n) return I;
303
+ if (this.equals(I) || n === _1n) return this;
304
+ if (this.equals(G)) return this.wNAF(n).p;
305
+ return wnaf.unsafeLadder(this, n);
306
+ }
307
+
308
+ // Checks if point is of small order.
309
+ // If you add something to small order point, you will have "dirty"
310
+ // point with torsion component.
311
+ // Multiplies point by cofactor and checks if the result is 0.
312
+ isSmallOrder(): boolean {
313
+ return this.multiplyUnsafe(cofactor).is0();
314
+ }
315
+
316
+ // Multiplies point by curve order and checks if the result is 0.
317
+ // Returns `false` is the point is dirty.
318
+ isTorsionFree(): boolean {
319
+ return wnaf.unsafeLadder(this, CURVE_ORDER).is0();
320
+ }
321
+
322
+ // Converts Extended point to default (x, y) coordinates.
323
+ // Can accept precomputed Z^-1 - for example, from invertBatch.
324
+ toAffine(iz?: bigint): AffinePoint<bigint> {
325
+ const { ex: x, ey: y, ez: z } = this;
326
+ const is0 = this.is0();
327
+ if (iz == null) iz = is0 ? _8n : (Fp.inv(z) as bigint); // 8 was chosen arbitrarily
328
+ const ax = modP(x * iz);
329
+ const ay = modP(y * iz);
330
+ const zz = modP(z * iz);
331
+ if (is0) return { x: _0n, y: _1n };
332
+ if (zz !== _1n) throw new Error('invZ was invalid');
333
+ return { x: ax, y: ay };
334
+ }
335
+
336
+ clearCofactor(): Point {
337
+ const { h: cofactor } = CURVE;
338
+ if (cofactor === _1n) return this;
339
+ return this.multiplyUnsafe(cofactor);
340
+ }
341
+
342
+ // Converts hash string or Uint8Array to Point.
343
+ // Uses algo from RFC8032 5.1.3.
344
+ static fromHex(hex: Hex, strict = true): Point {
345
+ const { d, a } = CURVE;
346
+ const len = Fp.BYTES;
347
+ hex = ensureBytes('pointHex', hex, len); // copy hex to a new array
348
+ const normed = hex.slice(); // copy again, we'll manipulate it
349
+ const lastByte = hex[len - 1]; // select last byte
350
+ normed[len - 1] = lastByte & ~0x80; // clear last bit
351
+ const y = ut.bytesToNumberLE(normed);
352
+ if (y === _0n) {
353
+ // y=0 is allowed
354
+ } else {
355
+ // RFC8032 prohibits >= p, but ZIP215 doesn't
356
+ if (strict) assertInRange(y, Fp.ORDER); // strict=true [1..P-1] (2^255-19-1 for ed25519)
357
+ else assertInRange(y, MASK); // strict=false [1..MASK-1] (2^256-1 for ed25519)
358
+ }
359
+
360
+ // Ed25519: x² = (y²-1)/(dy²+1) mod p. Ed448: x² = (y²-1)/(dy²-1) mod p. Generic case:
361
+ // ax²+y²=1+dx²y² => y²-1=dx²y²-ax² => y²-1=x²(dy²-a) => x²=(y²-1)/(dy²-a)
362
+ const y2 = modP(y * y); // denominator is always non-0 mod p.
363
+ const u = modP(y2 - _1n); // u = y² - 1
364
+ const v = modP(d * y2 - a); // v = d y² + 1.
365
+ let { isValid, value: x } = uvRatio(u, v); // √(u/v)
366
+ if (!isValid) throw new Error('Point.fromHex: invalid y coordinate');
367
+ const isXOdd = (x & _1n) === _1n; // There are 2 square roots. Use x_0 bit to select proper
368
+ const isLastByteOdd = (lastByte & 0x80) !== 0; // if x=0 and x_0 = 1, fail
369
+ if (isLastByteOdd !== isXOdd) x = modP(-x); // if x_0 != x mod 2, set x = p-x
370
+ return Point.fromAffine({ x, y });
371
+ }
372
+ static fromPrivateKey(privKey: Hex) {
373
+ return getExtendedPublicKey(privKey).point;
374
+ }
375
+ toRawBytes(): Uint8Array {
376
+ const { x, y } = this.toAffine();
377
+ const bytes = ut.numberToBytesLE(y, Fp.BYTES); // each y has 2 x values (x, -y)
378
+ bytes[bytes.length - 1] |= x & _1n ? 0x80 : 0; // when compressing, it's enough to store y
379
+ return bytes; // and use the last byte to encode sign of x
380
+ }
381
+ toHex(): string {
382
+ return ut.bytesToHex(this.toRawBytes()); // Same as toRawBytes, but returns string.
383
+ }
384
+ }
385
+ const { BASE: G, ZERO: I } = Point;
386
+ const wnaf = wNAF(Point, nByteLength * 8);
387
+
388
+ function modN(a: bigint) {
389
+ return mod(a, CURVE_ORDER);
390
+ }
391
+ // Little-endian SHA512 with modulo n
392
+ function modN_LE(hash: Uint8Array): bigint {
393
+ return modN(ut.bytesToNumberLE(hash));
394
+ }
395
+
396
+ /** Convenience method that creates public key and other stuff. RFC8032 5.1.5 */
397
+ function getExtendedPublicKey(key: Hex) {
398
+ const len = nByteLength;
399
+ key = ensureBytes('private key', key, len);
400
+ // Hash private key with curve's hash function to produce uniformingly random input
401
+ // Check byte lengths: ensure(64, h(ensure(32, key)))
402
+ const hashed = ensureBytes('hashed private key', cHash(key), 2 * len);
403
+ const head = adjustScalarBytes(hashed.slice(0, len)); // clear first half bits, produce FE
404
+ const prefix = hashed.slice(len, 2 * len); // second half is called key prefix (5.1.6)
405
+ const scalar = modN_LE(head); // The actual private scalar
406
+ const point = G.multiply(scalar); // Point on Edwards curve aka public key
407
+ const pointBytes = point.toRawBytes(); // Uint8Array representation
408
+ return { head, prefix, scalar, point, pointBytes };
409
+ }
410
+
411
+ // Calculates EdDSA pub key. RFC8032 5.1.5. Privkey is hashed. Use first half with 3 bits cleared
412
+ function getPublicKey(privKey: Hex): Uint8Array {
413
+ return getExtendedPublicKey(privKey).pointBytes;
414
+ }
415
+
416
+ // int('LE', SHA512(dom2(F, C) || msgs)) mod N
417
+ function hashDomainToScalar(context: Hex = new Uint8Array(), ...msgs: Uint8Array[]) {
418
+ const msg = ut.concatBytes(...msgs);
419
+ return modN_LE(cHash(domain(msg, ensureBytes('context', context), !!preHash)));
420
+ }
421
+
422
+ /** Signs message with privateKey. RFC8032 5.1.6 */
423
+ function sign(msg: Hex, privKey: Hex, context?: Hex): Uint8Array {
424
+ msg = ensureBytes('message', msg);
425
+ if (preHash) msg = preHash(msg); // for ed25519ph etc.
426
+ const { prefix, scalar, pointBytes } = getExtendedPublicKey(privKey);
427
+ const r = hashDomainToScalar(context, prefix, msg); // r = dom2(F, C) || prefix || PH(M)
428
+ const R = G.multiply(r).toRawBytes(); // R = rG
429
+ const k = hashDomainToScalar(context, R, pointBytes, msg); // R || A || PH(M)
430
+ const s = modN(r + k * scalar); // S = (r + k * s) mod L
431
+ assertGE0(s); // 0 <= s < l
432
+ const res = ut.concatBytes(R, ut.numberToBytesLE(s, Fp.BYTES));
433
+ return ensureBytes('result', res, nByteLength * 2); // 64-byte signature
434
+ }
435
+
436
+ function verify(sig: Hex, msg: Hex, publicKey: Hex, context?: Hex): boolean {
437
+ const len = Fp.BYTES; // Verifies EdDSA signature against message and public key. RFC8032 5.1.7.
438
+ sig = ensureBytes('signature', sig, 2 * len); // An extended group equation is checked.
439
+ msg = ensureBytes('message', msg); // ZIP215 compliant, which means not fully RFC8032 compliant.
440
+ if (preHash) msg = preHash(msg); // for ed25519ph, etc
441
+ const A = Point.fromHex(publicKey, false); // Check for s bounds, hex validity
442
+ const R = Point.fromHex(sig.slice(0, len), false); // 0 <= R < 2^256: ZIP215 R can be >= P
443
+ const s = ut.bytesToNumberLE(sig.slice(len, 2 * len)); // 0 <= s < l
444
+ const SB = G.multiplyUnsafe(s);
445
+ const k = hashDomainToScalar(context, R.toRawBytes(), A.toRawBytes(), msg);
446
+ const RkA = R.add(A.multiplyUnsafe(k));
447
+ // [8][S]B = [8]R + [8][k]A'
448
+ return RkA.subtract(SB).clearCofactor().equals(Point.ZERO);
449
+ }
450
+
451
+ G._setWindowSize(8); // Enable precomputes. Slows down first publicKey computation by 20ms.
452
+
453
+ const utils = {
454
+ getExtendedPublicKey,
455
+ // ed25519 private keys are uniform 32b. No need to check for modulo bias, like in secp256k1.
456
+ randomPrivateKey: (): Uint8Array => randomBytes(Fp.BYTES),
457
+
458
+ /**
459
+ * We're doing scalar multiplication (used in getPublicKey etc) with precomputed BASE_POINT
460
+ * values. This slows down first getPublicKey() by milliseconds (see Speed section),
461
+ * but allows to speed-up subsequent getPublicKey() calls up to 20x.
462
+ * @param windowSize 2, 4, 8, 16
463
+ */
464
+ precompute(windowSize = 8, point = Point.BASE): typeof Point.BASE {
465
+ point._setWindowSize(windowSize);
466
+ point.multiply(BigInt(3));
467
+ return point;
468
+ },
469
+ };
470
+
471
+ return {
472
+ CURVE,
473
+ getPublicKey,
474
+ sign,
475
+ verify,
476
+ ExtendedPoint: Point,
477
+ utils,
478
+ };
479
+ }
@@ -0,0 +1,220 @@
1
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2
+ import type { Group, GroupConstructor, AffinePoint } from './curve.js';
3
+ import { mod, Field } from './modular.js';
4
+ import { CHash, concatBytes, utf8ToBytes, validateObject } from './utils.js';
5
+
6
+ export type Opts = {
7
+ DST: string; // DST: a domain separation tag, defined in section 2.2.5
8
+ encodeDST: string;
9
+ p: bigint; // characteristic of F, where F is a finite field of characteristic p and order q = p^m
10
+ m: number; // extension degree of F, m >= 1
11
+ k: number; // k: the target security level for the suite in bits, defined in section 5.1
12
+ expand?: 'xmd' | 'xof'; // use a message that has already been processed by expand_message_xmd
13
+ // Hash functions for: expand_message_xmd is appropriate for use with a
14
+ // wide range of hash functions, including SHA-2, SHA-3, BLAKE2, and others.
15
+ // BBS+ uses blake2: https://github.com/hyperledger/aries-framework-go/issues/2247
16
+ // TODO: verify that hash is shake if expand==='xof' via types
17
+ hash: CHash;
18
+ };
19
+
20
+ // Octet Stream to Integer (bytesToNumberBE)
21
+ function os2ip(bytes: Uint8Array): bigint {
22
+ let result = 0n;
23
+ for (let i = 0; i < bytes.length; i++) {
24
+ result <<= 8n;
25
+ result += BigInt(bytes[i]);
26
+ }
27
+ return result;
28
+ }
29
+
30
+ // Integer to Octet Stream
31
+ function i2osp(value: number, length: number): Uint8Array {
32
+ if (value < 0 || value >= 1 << (8 * length)) {
33
+ throw new Error(`bad I2OSP call: value=${value} length=${length}`);
34
+ }
35
+ const res = Array.from({ length }).fill(0) as number[];
36
+ for (let i = length - 1; i >= 0; i--) {
37
+ res[i] = value & 0xff;
38
+ value >>>= 8;
39
+ }
40
+ return new Uint8Array(res);
41
+ }
42
+
43
+ function strxor(a: Uint8Array, b: Uint8Array): Uint8Array {
44
+ const arr = new Uint8Array(a.length);
45
+ for (let i = 0; i < a.length; i++) {
46
+ arr[i] = a[i] ^ b[i];
47
+ }
48
+ return arr;
49
+ }
50
+
51
+ function isBytes(item: unknown): void {
52
+ if (!(item instanceof Uint8Array)) throw new Error('Uint8Array expected');
53
+ }
54
+ function isNum(item: unknown): void {
55
+ if (!Number.isSafeInteger(item)) throw new Error('number expected');
56
+ }
57
+
58
+ // Produces a uniformly random byte string using a cryptographic hash function H that outputs b bits
59
+ // https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-11#section-5.4.1
60
+ export function expand_message_xmd(
61
+ msg: Uint8Array,
62
+ DST: Uint8Array,
63
+ lenInBytes: number,
64
+ H: CHash
65
+ ): Uint8Array {
66
+ isBytes(msg);
67
+ isBytes(DST);
68
+ isNum(lenInBytes);
69
+ // https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-16#section-5.3.3
70
+ if (DST.length > 255) DST = H(concatBytes(utf8ToBytes('H2C-OVERSIZE-DST-'), DST));
71
+ const b_in_bytes = H.outputLen;
72
+ const r_in_bytes = H.blockLen;
73
+ const ell = Math.ceil(lenInBytes / b_in_bytes);
74
+ if (ell > 255) throw new Error('Invalid xmd length');
75
+ const DST_prime = concatBytes(DST, i2osp(DST.length, 1));
76
+ const Z_pad = i2osp(0, r_in_bytes);
77
+ const l_i_b_str = i2osp(lenInBytes, 2);
78
+ const b = new Array<Uint8Array>(ell);
79
+ const b_0 = H(concatBytes(Z_pad, msg, l_i_b_str, i2osp(0, 1), DST_prime));
80
+ b[0] = H(concatBytes(b_0, i2osp(1, 1), DST_prime));
81
+ for (let i = 1; i <= ell; i++) {
82
+ const args = [strxor(b_0, b[i - 1]), i2osp(i + 1, 1), DST_prime];
83
+ b[i] = H(concatBytes(...args));
84
+ }
85
+ const pseudo_random_bytes = concatBytes(...b);
86
+ return pseudo_random_bytes.slice(0, lenInBytes);
87
+ }
88
+
89
+ export function expand_message_xof(
90
+ msg: Uint8Array,
91
+ DST: Uint8Array,
92
+ lenInBytes: number,
93
+ k: number,
94
+ H: CHash
95
+ ): Uint8Array {
96
+ isBytes(msg);
97
+ isBytes(DST);
98
+ isNum(lenInBytes);
99
+ // https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-16#section-5.3.3
100
+ // DST = H('H2C-OVERSIZE-DST-' || a_very_long_DST, Math.ceil((lenInBytes * k) / 8));
101
+ if (DST.length > 255) {
102
+ const dkLen = Math.ceil((2 * k) / 8);
103
+ DST = H.create({ dkLen }).update(utf8ToBytes('H2C-OVERSIZE-DST-')).update(DST).digest();
104
+ }
105
+ if (lenInBytes > 65535 || DST.length > 255)
106
+ throw new Error('expand_message_xof: invalid lenInBytes');
107
+ return (
108
+ H.create({ dkLen: lenInBytes })
109
+ .update(msg)
110
+ .update(i2osp(lenInBytes, 2))
111
+ // 2. DST_prime = DST || I2OSP(len(DST), 1)
112
+ .update(DST)
113
+ .update(i2osp(DST.length, 1))
114
+ .digest()
115
+ );
116
+ }
117
+
118
+ /**
119
+ * Hashes arbitrary-length byte strings to a list of one or more elements of a finite field F
120
+ * https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-11#section-5.3
121
+ * @param msg a byte string containing the message to hash
122
+ * @param count the number of elements of F to output
123
+ * @param options `{DST: string, p: bigint, m: number, k: number, expand: 'xmd' | 'xof', hash: H}`
124
+ * @returns [u_0, ..., u_(count - 1)], a list of field elements.
125
+ */
126
+ export function hash_to_field(msg: Uint8Array, count: number, options: Opts): bigint[][] {
127
+ const { p, k, m, hash, expand, DST: _DST } = options;
128
+ isBytes(msg);
129
+ isNum(count);
130
+ if (typeof _DST !== 'string') throw new Error('DST must be valid');
131
+ const log2p = p.toString(2).length;
132
+ const L = Math.ceil((log2p + k) / 8); // section 5.1 of ietf draft link above
133
+ const len_in_bytes = count * m * L;
134
+ const DST = utf8ToBytes(_DST);
135
+ const pseudo_random_bytes =
136
+ expand === 'xmd'
137
+ ? expand_message_xmd(msg, DST, len_in_bytes, hash)
138
+ : expand === 'xof'
139
+ ? expand_message_xof(msg, DST, len_in_bytes, k, hash)
140
+ : msg;
141
+ const u = new Array(count);
142
+ for (let i = 0; i < count; i++) {
143
+ const e = new Array(m);
144
+ for (let j = 0; j < m; j++) {
145
+ const elm_offset = L * (j + i * m);
146
+ const tv = pseudo_random_bytes.subarray(elm_offset, elm_offset + L);
147
+ e[j] = mod(os2ip(tv), p);
148
+ }
149
+ u[i] = e;
150
+ }
151
+ return u;
152
+ }
153
+
154
+ export function isogenyMap<T, F extends Field<T>>(field: F, map: [T[], T[], T[], T[]]) {
155
+ // Make same order as in spec
156
+ const COEFF = map.map((i) => Array.from(i).reverse());
157
+ return (x: T, y: T) => {
158
+ const [xNum, xDen, yNum, yDen] = COEFF.map((val) =>
159
+ val.reduce((acc, i) => field.add(field.mul(acc, x), i))
160
+ );
161
+ x = field.div(xNum, xDen); // xNum / xDen
162
+ y = field.mul(y, field.div(yNum, yDen)); // y * (yNum / yDev)
163
+ return { x, y };
164
+ };
165
+ }
166
+
167
+ export interface H2CPoint<T> extends Group<H2CPoint<T>> {
168
+ add(rhs: H2CPoint<T>): H2CPoint<T>;
169
+ toAffine(iz?: bigint): AffinePoint<T>;
170
+ clearCofactor(): H2CPoint<T>;
171
+ assertValidity(): void;
172
+ }
173
+
174
+ export interface H2CPointConstructor<T> extends GroupConstructor<H2CPoint<T>> {
175
+ fromAffine(ap: AffinePoint<T>): H2CPoint<T>;
176
+ }
177
+
178
+ export type MapToCurve<T> = (scalar: bigint[]) => AffinePoint<T>;
179
+
180
+ // Separated from initialization opts, so users won't accidentally change per-curve parameters
181
+ // (changing DST is ok!)
182
+ export type htfBasicOpts = { DST: string };
183
+
184
+ export function createHasher<T>(
185
+ Point: H2CPointConstructor<T>,
186
+ mapToCurve: MapToCurve<T>,
187
+ def: Opts
188
+ ) {
189
+ validateObject(def, {
190
+ DST: 'string',
191
+ p: 'bigint',
192
+ m: 'isSafeInteger',
193
+ k: 'isSafeInteger',
194
+ hash: 'hash',
195
+ });
196
+ if (def.expand !== 'xmd' && def.expand !== 'xof' && def.expand !== undefined)
197
+ throw new Error('Invalid htf/expand');
198
+ if (typeof mapToCurve !== 'function')
199
+ throw new Error('hashToCurve: mapToCurve() has not been defined');
200
+ return {
201
+ // Encodes byte string to elliptic curve
202
+ // https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-11#section-3
203
+ hashToCurve(msg: Uint8Array, options?: htfBasicOpts) {
204
+ const u = hash_to_field(msg, 2, { ...def, DST: def.DST, ...options } as Opts);
205
+ const u0 = Point.fromAffine(mapToCurve(u[0]));
206
+ const u1 = Point.fromAffine(mapToCurve(u[1]));
207
+ const P = u0.add(u1).clearCofactor();
208
+ P.assertValidity();
209
+ return P;
210
+ },
211
+
212
+ // https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-16#section-3
213
+ encodeToCurve(msg: Uint8Array, options?: htfBasicOpts) {
214
+ const u = hash_to_field(msg, 1, { ...def, DST: def.encodeDST, ...options } as Opts);
215
+ const P = Point.fromAffine(mapToCurve(u[0])).clearCofactor();
216
+ P.assertValidity();
217
+ return P;
218
+ },
219
+ };
220
+ }