@decryption/curves 0.2.0

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 (69) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +75 -0
  3. package/abstract/bls.d.ts +410 -0
  4. package/abstract/bls.js +433 -0
  5. package/abstract/curve.d.ts +441 -0
  6. package/abstract/curve.js +622 -0
  7. package/abstract/edwards.d.ts +319 -0
  8. package/abstract/edwards.js +697 -0
  9. package/abstract/fft.d.ts +438 -0
  10. package/abstract/fft.js +580 -0
  11. package/abstract/frost.d.ts +292 -0
  12. package/abstract/frost.js +705 -0
  13. package/abstract/hash-to-curve.d.ts +238 -0
  14. package/abstract/hash-to-curve.js +353 -0
  15. package/abstract/modular.d.ts +550 -0
  16. package/abstract/modular.js +872 -0
  17. package/abstract/montgomery.d.ts +95 -0
  18. package/abstract/montgomery.js +180 -0
  19. package/abstract/oprf.d.ts +354 -0
  20. package/abstract/oprf.js +358 -0
  21. package/abstract/poseidon.d.ts +198 -0
  22. package/abstract/poseidon.js +471 -0
  23. package/abstract/tower.d.ts +213 -0
  24. package/abstract/tower.js +963 -0
  25. package/abstract/weierstrass.d.ts +587 -0
  26. package/abstract/weierstrass.js +1421 -0
  27. package/bls12-381.d.ts +23 -0
  28. package/bls12-381.js +680 -0
  29. package/bn254.d.ts +74 -0
  30. package/bn254.js +248 -0
  31. package/ed25519.d.ts +211 -0
  32. package/ed25519.js +626 -0
  33. package/ed448.d.ts +184 -0
  34. package/ed448.js +596 -0
  35. package/esm/abstract/bls.js +429 -0
  36. package/esm/abstract/curve.js +610 -0
  37. package/esm/abstract/edwards.js +691 -0
  38. package/esm/abstract/fft.js +567 -0
  39. package/esm/abstract/frost.js +702 -0
  40. package/esm/abstract/hash-to-curve.js +345 -0
  41. package/esm/abstract/modular.js +849 -0
  42. package/esm/abstract/montgomery.js +177 -0
  43. package/esm/abstract/oprf.js +355 -0
  44. package/esm/abstract/poseidon.js +462 -0
  45. package/esm/abstract/tower.js +925 -0
  46. package/esm/abstract/weierstrass.js +1411 -0
  47. package/esm/bls12-381.js +677 -0
  48. package/esm/bn254.js +244 -0
  49. package/esm/ed25519.js +622 -0
  50. package/esm/ed448.js +593 -0
  51. package/esm/index.js +38 -0
  52. package/esm/misc.js +211 -0
  53. package/esm/nist.js +266 -0
  54. package/esm/secp256k1.js +465 -0
  55. package/esm/utils.js +597 -0
  56. package/esm/webcrypto.js +362 -0
  57. package/index.d.ts +0 -0
  58. package/index.js +38 -0
  59. package/misc.d.ts +113 -0
  60. package/misc.js +216 -0
  61. package/nist.d.ts +136 -0
  62. package/nist.js +269 -0
  63. package/package.json +46 -0
  64. package/secp256k1.d.ts +152 -0
  65. package/secp256k1.js +468 -0
  66. package/utils.d.ts +535 -0
  67. package/utils.js +626 -0
  68. package/webcrypto.d.ts +240 -0
  69. package/webcrypto.js +365 -0
package/esm/ed448.js ADDED
@@ -0,0 +1,593 @@
1
+ /**
2
+ * Edwards448 (also called Goldilocks) curve with following addons:
3
+ * - X448 ECDH
4
+ * - Decaf cofactor elimination
5
+ * - Elligator hash-to-group / point indistinguishability
6
+ * Conforms to RFC 8032 https://www.rfc-editor.org/rfc/rfc8032.html#section-5.2
7
+ * @module
8
+ */
9
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
10
+ import { shake256 } from '@decryption/hashes/sha3';
11
+ import { concatBytes, hexToBytes, createHasher as wrapConstructor } from '@decryption/hashes/utils';
12
+ import { eddsa, edwards, PrimeEdwardsPoint, } from './abstract/edwards';
13
+ import { createFROST } from './abstract/frost';
14
+ import { _DST_scalar, createHasher, expand_message_xof, } from './abstract/hash-to-curve';
15
+ import { Field, FpInvertBatch, isNegativeLE, mod, pow2 } from './abstract/modular';
16
+ import { montgomery } from './abstract/montgomery';
17
+ import { createOPRF } from './abstract/oprf';
18
+ import { abytes, asciiToBytes, bytesToNumberLE, equalBytes, } from './utils';
19
+ // edwards448 curve
20
+ // a = 1n
21
+ // d = Fp.neg(39081n)
22
+ // Finite field 2n**448n - 2n**224n - 1n
23
+ // Subgroup order
24
+ // 2n**446n - 13818066809895115352007386748515426880336692474882178609894547503885n
25
+ const ed448_CURVE_p = /* @__PURE__ */ BigInt('0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffffffffffffffffffffffffffffffffffffffffffffffffffff');
26
+ const ed448_CURVE = /* @__PURE__ */ (() => ({
27
+ p: ed448_CURVE_p,
28
+ n: BigInt('0x3fffffffffffffffffffffffffffffffffffffffffffffffffffffff7cca23e9c44edb49aed63690216cc2728dc58f552378c292ab5844f3'),
29
+ h: BigInt(4),
30
+ a: BigInt(1),
31
+ d: BigInt('0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffffffffffffffffffffffffffffffffffffffffffffffff6756'),
32
+ Gx: BigInt('0x4f1970c66bed0ded221d15a622bf36da9e146570470f1767ea6de324a3d3a46412ae1af72ab66511433b80e18b00938e2626a82bc70cc05e'),
33
+ Gy: BigInt('0x693f46716eb6bc248876203756c9c7624bea73736ca3984087789c1e05a0c2d73ad3ff1ce67c39c4fdbd132c4ed7c8ad9808795bf230fa14'),
34
+ }))();
35
+ // This is not RFC 8032 edwards448 / Goldilocks (`ed448` below, d = -39081).
36
+ // It is NIST SP 800-186 §3.2.3.3 E448, the Curve448-isomorphic Edwards model
37
+ // also described in draft-ietf-lwig-curve-representations-23 Appendix M, with
38
+ // d = 39082/39081 and Gy = 3/2.
39
+ // RFC 7748's literal Edwards point / birational map are wrong here: the literal
40
+ // point is the wrong-sign (Gx, -Gy) order-2*n variant. Keep the corrected
41
+ // prime-order (Gx, Gy) base so Point.BASE stays a subgroup generator, which is
42
+ // what noble's generic Edwards API expects.
43
+ const E448_CURVE = /* @__PURE__ */ (() => Object.assign({}, ed448_CURVE, {
44
+ d: BigInt('0xd78b4bdc7f0daf19f24f38c29373a2ccad46157242a50f37809b1da3412a12e79ccc9c81264cfe9ad080997058fb61c4243cc32dbaa156b9'),
45
+ Gx: BigInt('0x79a70b2b70400553ae7c9df416c792c61128751ac92969240c25a07d728bdc93e21f7787ed6972249de732f38496cd11698713093e9c04fc'),
46
+ Gy: BigInt('0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000000000000000000000000000000000000000000000000001'),
47
+ }))();
48
+ const shake256_114 = /* @__PURE__ */ wrapConstructor(() => shake256.create({ dkLen: 114 }));
49
+ const shake256_64 = /* @__PURE__ */ wrapConstructor(() => shake256.create({ dkLen: 64 }));
50
+ // prettier-ignore
51
+ const _1n = /* @__PURE__ */ BigInt(1), _2n = /* @__PURE__ */ BigInt(2), _3n = /* @__PURE__ */ BigInt(3), _4n = /* @__PURE__ */ BigInt(4), _11n = /* @__PURE__ */ BigInt(11);
52
+ // prettier-ignore
53
+ const _22n = /* @__PURE__ */ BigInt(22), _44n = /* @__PURE__ */ BigInt(44), _88n = /* @__PURE__ */ BigInt(88), _223n = /* @__PURE__ */ BigInt(223);
54
+ // powPminus3div4 calculates z = x^k mod p, where k = (p-3)/4.
55
+ // Used for efficient square root calculation.
56
+ // ((P-3)/4).toString(2) would produce bits [223x 1, 0, 222x 1]
57
+ function ed448_pow_Pminus3div4(x) {
58
+ const P = ed448_CURVE_p;
59
+ const b2 = (x * x * x) % P;
60
+ const b3 = (b2 * b2 * x) % P;
61
+ const b6 = (pow2(b3, _3n, P) * b3) % P;
62
+ const b9 = (pow2(b6, _3n, P) * b3) % P;
63
+ const b11 = (pow2(b9, _2n, P) * b2) % P;
64
+ const b22 = (pow2(b11, _11n, P) * b11) % P;
65
+ const b44 = (pow2(b22, _22n, P) * b22) % P;
66
+ const b88 = (pow2(b44, _44n, P) * b44) % P;
67
+ const b176 = (pow2(b88, _88n, P) * b88) % P;
68
+ const b220 = (pow2(b176, _44n, P) * b44) % P;
69
+ const b222 = (pow2(b220, _2n, P) * b2) % P;
70
+ const b223 = (pow2(b222, _1n, P) * x) % P;
71
+ return (pow2(b223, _223n, P) * b222) % P;
72
+ }
73
+ // Mutates and returns the provided buffer in place. The final `bytes[56] = 0`
74
+ // write is the Ed448 path; for 56-byte X448 inputs it is an out-of-bounds no-op.
75
+ function adjustScalarBytes(bytes) {
76
+ // Section 5: Likewise, for X448, set the two least significant bits of the first byte to 0,
77
+ bytes[0] &= 252; // 0b11111100
78
+ // and the most significant bit of the last byte to 1.
79
+ bytes[55] |= 128; // 0b10000000
80
+ // NOTE: is NOOP for 56 bytes scalars (X25519/X448)
81
+ bytes[56] = 0; // Byte outside of group (456 buts vs 448 bits)
82
+ return bytes;
83
+ }
84
+ // Constant-time Ed448 decode helper for RFC 8032 §5.2.3 steps 2-3. Unlike
85
+ // `SQRT_RATIO_M1`, the returned `value` only has the documented meaning when
86
+ // `isValid` is true.
87
+ function uvRatio(u, v) {
88
+ const P = ed448_CURVE_p;
89
+ // https://www.rfc-editor.org/rfc/rfc8032#section-5.2.3
90
+ // To compute the square root of (u/v), the first step is to compute the
91
+ // candidate root x = (u/v)^((p+1)/4). This can be done using the
92
+ // following trick, to use a single modular powering for both the
93
+ // inversion of v and the square root:
94
+ // x = (u/v)^((p+1)/4) = u³v(u⁵v³)^((p-3)/4) (mod p)
95
+ const u2v = mod(u * u * v, P); // u²v
96
+ const u3v = mod(u2v * u, P); // u³v
97
+ const u5v3 = mod(u3v * u2v * v, P); // u⁵v³
98
+ const root = ed448_pow_Pminus3div4(u5v3);
99
+ const x = mod(u3v * root, P);
100
+ // Verify that root is exists
101
+ const x2 = mod(x * x, P); // x²
102
+ // If vx² = u, the recovered x-coordinate is x. Otherwise, no
103
+ // square root exists, and the decoding fails.
104
+ return { isValid: mod(x2 * v, P) === u, value: x };
105
+ }
106
+ // Finite field 2n**448n - 2n**224n - 1n
107
+ // RFC 8032 encodes Ed448 field/scalar elements in 57 bytes even though field
108
+ // values fit in 448 bits and scalars in 446 bits. Noble models that with a
109
+ // 456-bit storage width so the final-octet x-sign bit (bit 455) still fits in
110
+ // the shared little-endian container.
111
+ const Fp = /* @__PURE__ */ (() => Field(ed448_CURVE_p, { BITS: 456, isLE: true }))();
112
+ // Same 57-byte container shape as `Fp`; canonical scalar encodings still have
113
+ // the top ten bits clear per RFC 8032.
114
+ const Fn = /* @__PURE__ */ (() => Field(ed448_CURVE.n, { BITS: 456, isLE: true }))();
115
+ // Generic 56-byte field shape used by decaf448 and raw X448 u-coordinates.
116
+ // Plain `Field` decoding stays canonical here, so callers that want RFC 7748's
117
+ // modulo-p acceptance must reduce externally.
118
+ const Fp448 = /* @__PURE__ */ (() => Field(ed448_CURVE_p, { BITS: 448, isLE: true }))();
119
+ // Strict 56-byte scalar parser matching RFC 9496's recommended canonical form.
120
+ const Fn448 = /* @__PURE__ */ (() => Field(ed448_CURVE.n, { BITS: 448, isLE: true }))();
121
+ // SHAKE256(dom4(phflag,context)||x, 114)
122
+ // RFC 8032 `dom4` prefix. Empty contexts are valid; the accepted length range
123
+ // is 0..255 octets inclusive.
124
+ function dom4(data, ctx, phflag) {
125
+ if (ctx.length > 255)
126
+ throw new Error('context must be smaller than 255, got: ' + ctx.length);
127
+ return concatBytes(asciiToBytes('SigEd448'), new Uint8Array([phflag ? 1 : 0, ctx.length]), ctx, data);
128
+ }
129
+ const ed448_Point = /* @__PURE__ */ edwards(ed448_CURVE, { Fp, Fn, uvRatio });
130
+ // Shared internal factory for both `ed448` and `ed448ph`; callers are only
131
+ // expected to override narrow family options such as prehashing.
132
+ function ed4(opts) {
133
+ return eddsa(ed448_Point, shake256_114, Object.assign({ adjustScalarBytes, domain: dom4 }, opts));
134
+ }
135
+ /**
136
+ * ed448 EdDSA curve and methods.
137
+ * @example
138
+ * Generate one Ed448 keypair, sign a message, and verify it.
139
+ *
140
+ * ```js
141
+ * import { ed448 } from './ed448';
142
+ * const { secretKey, publicKey } = ed448.keygen();
143
+ * // const publicKey = ed448.getPublicKey(secretKey);
144
+ * const msg = new TextEncoder().encode('hello noble');
145
+ * const sig = ed448.sign(msg, secretKey);
146
+ * const isValid = ed448.verify(sig, msg, publicKey);
147
+ * ```
148
+ */
149
+ export const ed448 = /* @__PURE__ */ ed4({});
150
+ // There is no ed448ctx, since ed448 supports ctx by default
151
+ /**
152
+ * Prehashed version of ed448. See {@link ed448}
153
+ * @example
154
+ * Use the prehashed Ed448 variant for one message.
155
+ *
156
+ * ```ts
157
+ * const { secretKey, publicKey } = ed448ph.keygen();
158
+ * const msg = new TextEncoder().encode('hello noble');
159
+ * const sig = ed448ph.sign(msg, secretKey);
160
+ * const isValid = ed448ph.verify(sig, msg, publicKey);
161
+ * ```
162
+ */
163
+ export const ed448ph = /* @__PURE__ */ ed4({ prehash: shake256_64 });
164
+ /**
165
+ * E448 here is NIST SP 800-186 §3.2.3.3 E448, the Edwards representation of
166
+ * Curve448, not RFC 8032 edwards448 / Goldilocks.
167
+ * Goldilocks is the separate 4-isogenous curve exposed as `ed448`.
168
+ * We keep the corrected prime-order base here; RFC 7748's literal Edwards
169
+ * point / map are wrong for this curve model, and the literal point is the
170
+ * wrong-sign order-2*n variant.
171
+ * @param X - Projective X coordinate.
172
+ * @param Y - Projective Y coordinate.
173
+ * @param Z - Projective Z coordinate.
174
+ * @param T - Projective T coordinate.
175
+ * @example
176
+ * Multiply the E448 base point.
177
+ *
178
+ * ```ts
179
+ * const point = E448.BASE.multiply(2n);
180
+ * ```
181
+ */
182
+ export const E448 = /* @__PURE__ */ edwards(E448_CURVE);
183
+ /**
184
+ * ECDH using curve448 aka x448.
185
+ * The wrapper aborts on all-zero shared secrets by default, and seeded
186
+ * `keygen(seed)` reuses the provided 56-byte seed buffer instead of copying it.
187
+ *
188
+ * @example
189
+ * Derive one shared secret between two X448 peers.
190
+ *
191
+ * ```js
192
+ * import { x448 } from './ed448';
193
+ * const alice = x448.keygen();
194
+ * const bob = x448.keygen();
195
+ * const shared = x448.getSharedSecret(alice.secretKey, bob.publicKey);
196
+ * ```
197
+ */
198
+ export const x448 = /* @__PURE__ */ (() => {
199
+ const P = ed448_CURVE_p;
200
+ return montgomery({
201
+ P,
202
+ type: 'x448',
203
+ powPminus2: (x) => {
204
+ const Pminus3div4 = ed448_pow_Pminus3div4(x);
205
+ const Pminus3 = pow2(Pminus3div4, _2n, P);
206
+ return mod(Pminus3 * x, P); // Pminus3 * x = Pminus2
207
+ },
208
+ adjustScalarBytes,
209
+ });
210
+ })();
211
+ // Hash To Curve Elligator2 Map
212
+ // 1. c1 = (q - 3) / 4 # Integer arithmetic
213
+ const ELL2_C1 = /* @__PURE__ */ (() => (ed448_CURVE_p - BigInt(3)) / BigInt(4))();
214
+ const ELL2_J = /* @__PURE__ */ BigInt(156326);
215
+ // Returns RFC 9380 Appendix G.2.3 rational Montgomery numerators/denominators
216
+ // `{ xn, xd, yn, yd }`, not an affine point.
217
+ function map_to_curve_elligator2_curve448(u) {
218
+ let tv1 = Fp.sqr(u); // 1. tv1 = u^2
219
+ let e1 = Fp.eql(tv1, Fp.ONE); // 2. e1 = tv1 == 1
220
+ tv1 = Fp.cmov(tv1, Fp.ZERO, e1); // 3. tv1 = CMOV(tv1, 0, e1) # If Z * u^2 == -1, set tv1 = 0
221
+ let xd = Fp.sub(Fp.ONE, tv1); // 4. xd = 1 - tv1
222
+ let x1n = Fp.neg(ELL2_J); // 5. x1n = -J
223
+ let tv2 = Fp.sqr(xd); // 6. tv2 = xd^2
224
+ let gxd = Fp.mul(tv2, xd); // 7. gxd = tv2 * xd # gxd = xd^3
225
+ let gx1 = Fp.mul(tv1, Fp.neg(ELL2_J)); // 8. gx1 = -J * tv1 # x1n + J * xd
226
+ gx1 = Fp.mul(gx1, x1n); // 9. gx1 = gx1 * x1n # x1n^2 + J * x1n * xd
227
+ gx1 = Fp.add(gx1, tv2); // 10. gx1 = gx1 + tv2 # x1n^2 + J * x1n * xd + xd^2
228
+ gx1 = Fp.mul(gx1, x1n); // 11. gx1 = gx1 * x1n # x1n^3 + J * x1n^2 * xd + x1n * xd^2
229
+ let tv3 = Fp.sqr(gxd); // 12. tv3 = gxd^2
230
+ tv2 = Fp.mul(gx1, gxd); // 13. tv2 = gx1 * gxd # gx1 * gxd
231
+ tv3 = Fp.mul(tv3, tv2); // 14. tv3 = tv3 * tv2 # gx1 * gxd^3
232
+ let y1 = Fp.pow(tv3, ELL2_C1); // 15. y1 = tv3^c1 # (gx1 * gxd^3)^((p - 3) / 4)
233
+ y1 = Fp.mul(y1, tv2); // 16. y1 = y1 * tv2 # gx1 * gxd * (gx1 * gxd^3)^((p - 3) / 4)
234
+ // 17. x2n = -tv1 * x1n # x2 = x2n / xd = -1 * u^2 * x1n / xd
235
+ let x2n = Fp.mul(x1n, Fp.neg(tv1));
236
+ let y2 = Fp.mul(y1, u); // 18. y2 = y1 * u
237
+ y2 = Fp.cmov(y2, Fp.ZERO, e1); // 19. y2 = CMOV(y2, 0, e1)
238
+ tv2 = Fp.sqr(y1); // 20. tv2 = y1^2
239
+ tv2 = Fp.mul(tv2, gxd); // 21. tv2 = tv2 * gxd
240
+ let e2 = Fp.eql(tv2, gx1); // 22. e2 = tv2 == gx1
241
+ let xn = Fp.cmov(x2n, x1n, e2); // 23. xn = CMOV(x2n, x1n, e2) # If e2, x = x1, else x = x2
242
+ let y = Fp.cmov(y2, y1, e2); // 24. y = CMOV(y2, y1, e2) # If e2, y = y1, else y = y2
243
+ let e3 = Fp.isOdd(y); // 25. e3 = sgn0(y) == 1 # Fix sign of y
244
+ y = Fp.cmov(y, Fp.neg(y), e2 !== e3); // 26. y = CMOV(y, -y, e2 XOR e3)
245
+ return { xn, xd, yn: y, yd: Fp.ONE }; // 27. return (xn, xd, y, 1)
246
+ }
247
+ // Returns affine `{ x, y }` after inverting the Appendix G.2.4 denominators.
248
+ function map_to_curve_elligator2_edwards448(u) {
249
+ // 1. (xn, xd, yn, yd) = map_to_curve_elligator2_curve448(u)
250
+ let { xn, xd, yn, yd } = map_to_curve_elligator2_curve448(u);
251
+ let xn2 = Fp.sqr(xn); // 2. xn2 = xn^2
252
+ let xd2 = Fp.sqr(xd); // 3. xd2 = xd^2
253
+ let xd4 = Fp.sqr(xd2); // 4. xd4 = xd2^2
254
+ let yn2 = Fp.sqr(yn); // 5. yn2 = yn^2
255
+ let yd2 = Fp.sqr(yd); // 6. yd2 = yd^2
256
+ let xEn = Fp.sub(xn2, xd2); // 7. xEn = xn2 - xd2
257
+ let tv2 = Fp.sub(xEn, xd2); // 8. tv2 = xEn - xd2
258
+ xEn = Fp.mul(xEn, xd2); // 9. xEn = xEn * xd2
259
+ xEn = Fp.mul(xEn, yd); // 10. xEn = xEn * yd
260
+ xEn = Fp.mul(xEn, yn); // 11. xEn = xEn * yn
261
+ xEn = Fp.mul(xEn, _4n); // 12. xEn = xEn * 4
262
+ tv2 = Fp.mul(tv2, xn2); // 13. tv2 = tv2 * xn2
263
+ tv2 = Fp.mul(tv2, yd2); // 14. tv2 = tv2 * yd2
264
+ let tv3 = Fp.mul(yn2, _4n); // 15. tv3 = 4 * yn2
265
+ let tv1 = Fp.add(tv3, yd2); // 16. tv1 = tv3 + yd2
266
+ tv1 = Fp.mul(tv1, xd4); // 17. tv1 = tv1 * xd4
267
+ let xEd = Fp.add(tv1, tv2); // 18. xEd = tv1 + tv2
268
+ tv2 = Fp.mul(tv2, xn); // 19. tv2 = tv2 * xn
269
+ let tv4 = Fp.mul(xn, xd4); // 20. tv4 = xn * xd4
270
+ let yEn = Fp.sub(tv3, yd2); // 21. yEn = tv3 - yd2
271
+ yEn = Fp.mul(yEn, tv4); // 22. yEn = yEn * tv4
272
+ yEn = Fp.sub(yEn, tv2); // 23. yEn = yEn - tv2
273
+ tv1 = Fp.add(xn2, xd2); // 24. tv1 = xn2 + xd2
274
+ tv1 = Fp.mul(tv1, xd2); // 25. tv1 = tv1 * xd2
275
+ tv1 = Fp.mul(tv1, xd); // 26. tv1 = tv1 * xd
276
+ tv1 = Fp.mul(tv1, yn2); // 27. tv1 = tv1 * yn2
277
+ tv1 = Fp.mul(tv1, BigInt(-2)); // 28. tv1 = -2 * tv1
278
+ let yEd = Fp.add(tv2, tv1); // 29. yEd = tv2 + tv1
279
+ tv4 = Fp.mul(tv4, yd2); // 30. tv4 = tv4 * yd2
280
+ yEd = Fp.add(yEd, tv4); // 31. yEd = yEd + tv4
281
+ tv1 = Fp.mul(xEd, yEd); // 32. tv1 = xEd * yEd
282
+ let e = Fp.eql(tv1, Fp.ZERO); // 33. e = tv1 == 0
283
+ xEn = Fp.cmov(xEn, Fp.ZERO, e); // 34. xEn = CMOV(xEn, 0, e)
284
+ xEd = Fp.cmov(xEd, Fp.ONE, e); // 35. xEd = CMOV(xEd, 1, e)
285
+ yEn = Fp.cmov(yEn, Fp.ONE, e); // 36. yEn = CMOV(yEn, 1, e)
286
+ yEd = Fp.cmov(yEd, Fp.ONE, e); // 37. yEd = CMOV(yEd, 1, e)
287
+ const inv = FpInvertBatch(Fp, [xEd, yEd], true); // batch division
288
+ return { x: Fp.mul(xEn, inv[0]), y: Fp.mul(yEn, inv[1]) }; // 38. return (xEn, xEd, yEn, yEd)
289
+ }
290
+ /**
291
+ * Hashing / encoding to ed448 points / field. RFC 9380 methods.
292
+ * Public `mapToCurve()` consumes one field element bigint for `m = 1`, and RFC
293
+ * Appendix J vectors use the special `QUUX-V01-*` test DST overrides rather
294
+ * than the default suite IDs below.
295
+ * @example
296
+ * Hash one message onto the ed448 curve.
297
+ *
298
+ * ```ts
299
+ * const point = ed448_hasher.hashToCurve(new TextEncoder().encode('hello noble'));
300
+ * ```
301
+ */
302
+ export const ed448_hasher = /* @__PURE__ */ (() => createHasher(ed448_Point, (scalars) => map_to_curve_elligator2_edwards448(scalars[0]), {
303
+ DST: 'edwards448_XOF:SHAKE256_ELL2_RO_',
304
+ encodeDST: 'edwards448_XOF:SHAKE256_ELL2_NU_',
305
+ p: ed448_CURVE_p,
306
+ m: 1,
307
+ k: 224,
308
+ expand: 'xof',
309
+ hash: shake256,
310
+ }))();
311
+ /**
312
+ * FROST threshold signatures over ed448. RFC 9591.
313
+ * @example
314
+ * Create one trusted-dealer package for 2-of-3 ed448 signing.
315
+ *
316
+ * ```ts
317
+ * const alice = ed448_FROST.Identifier.derive('alice@example.com');
318
+ * const bob = ed448_FROST.Identifier.derive('bob@example.com');
319
+ * const carol = ed448_FROST.Identifier.derive('carol@example.com');
320
+ * const deal = ed448_FROST.trustedDealer({ min: 2, max: 3 }, [alice, bob, carol]);
321
+ * ```
322
+ */
323
+ export const ed448_FROST = /* @__PURE__ */ (() => createFROST({
324
+ name: 'FROST-ED448-SHAKE256-v1',
325
+ Point: ed448_Point,
326
+ validatePoint: (p) => {
327
+ p.assertValidity();
328
+ if (!p.isTorsionFree())
329
+ throw new Error('bad point: not torsion-free');
330
+ },
331
+ // Group: edwards448 [RFC8032], where Ne = 57 and Ns = 57.
332
+ // Fn is 57 bytes, Fp is 57 bytes too
333
+ Fn,
334
+ hash: shake256_114,
335
+ H2: 'SigEd448\0\0',
336
+ }))();
337
+ // 1-d
338
+ const ONE_MINUS_D = /* @__PURE__ */ BigInt('39082');
339
+ // 1-2d
340
+ const ONE_MINUS_TWO_D = /* @__PURE__ */ BigInt('78163');
341
+ // √(-d)
342
+ const SQRT_MINUS_D = /* @__PURE__ */ BigInt('98944233647732219769177004876929019128417576295529901074099889598043702116001257856802131563896515373927712232092845883226922417596214');
343
+ // 1 / √(-d)
344
+ const INVSQRT_MINUS_D = /* @__PURE__ */ BigInt('315019913931389607337177038330951043522456072897266928557328499619017160722351061360252776265186336876723201881398623946864393857820716');
345
+ // RFC 9496 `SQRT_RATIO_M1` must return `CT_ABS(s)`, i.e. the nonnegative root.
346
+ // Keep this Decaf-local: RFC 9496 decode/encode/map formulas depend on that
347
+ // canonical representative, while ordinary Ed448 decoding still uses `uvRatio()`
348
+ // plus the public sign bit from RFC 8032.
349
+ const sqrtRatioM1 = (u, v) => {
350
+ const P = ed448_CURVE_p;
351
+ const { isValid, value } = uvRatio(u, v);
352
+ return { isValid, value: isNegativeLE(value, P) ? Fp448.create(-value) : value };
353
+ };
354
+ const invertSqrt = (number) => sqrtRatioM1(_1n, number);
355
+ /**
356
+ * Elligator map for hash-to-curve of decaf448.
357
+ * Primary formula source is RFC 9496 §5.3.4. Step 1 intentionally reduces the
358
+ * input modulo `p`, and the return value is the internal Edwards
359
+ * representation, not a public decaf encoding.
360
+ */
361
+ function calcElligatorDecafMap(r0) {
362
+ const { d, p: P } = ed448_CURVE;
363
+ const mod = (n) => Fp448.create(n);
364
+ const r = mod(-(r0 * r0)); // 1
365
+ const u0 = mod(d * (r - _1n)); // 2
366
+ const u1 = mod((u0 + _1n) * (u0 - r)); // 3
367
+ const { isValid: was_square, value: v } = sqrtRatioM1(ONE_MINUS_TWO_D, mod((r + _1n) * u1)); // 4
368
+ let v_prime = v; // 5
369
+ if (!was_square)
370
+ v_prime = mod(r0 * v);
371
+ let sgn = _1n; // 6
372
+ if (!was_square)
373
+ sgn = mod(-_1n);
374
+ const s = mod(v_prime * (r + _1n)); // 7
375
+ let s_abs = s;
376
+ if (isNegativeLE(s, P))
377
+ s_abs = mod(-s);
378
+ const s2 = s * s;
379
+ const W0 = mod(s_abs * _2n); // 8
380
+ const W1 = mod(s2 + _1n); // 9
381
+ const W2 = mod(s2 - _1n); // 10
382
+ const W3 = mod(v_prime * s * (r - _1n) * ONE_MINUS_TWO_D + sgn); // 11
383
+ return new ed448_Point(mod(W0 * W3), mod(W2 * W1), mod(W1 * W3), mod(W0 * W2));
384
+ }
385
+ // Keep the Decaf448 base representative literal here: deriving it with
386
+ // `new _DecafPoint(ed448_Point.BASE).multiplyUnsafe(2)` forces eager WNAF precomputes and
387
+ // adds about 100ms to `ed448.js` import time.
388
+ const DECAF_BASE_X = /* @__PURE__ */ BigInt('0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa955555555555555555555555555555555555555555555555555555555');
389
+ const DECAF_BASE_Y = /* @__PURE__ */ BigInt('0xae05e9634ad7048db359d6205086c2b0036ed7a035884dd7b7e36d728ad8c4b80d6565833a2a3098bbbcb2bed1cda06bdaeafbcdea9386ed');
390
+ const DECAF_BASE_T = /* @__PURE__ */ BigInt('0x696d84643374bace9d70983a12aa9d461da74d2d5c35e8d97ba72c3aba4450a5d29274229bd22c1d5e3a6474ee4ffb0e7a9e200a28eee402');
391
+ /**
392
+ * Each ed448/EdwardsPoint has 4 different equivalent points. This can be
393
+ * a source of bugs for protocols like ring signatures. Decaf was created to solve this.
394
+ * Decaf point operates in X:Y:Z:T extended coordinates like EdwardsPoint,
395
+ * but it should work in its own namespace: do not combine those two.
396
+ * See [RFC9496](https://www.rfc-editor.org/rfc/rfc9496).
397
+ */
398
+ class _DecafPoint extends PrimeEdwardsPoint {
399
+ // The following gymnastics is done because typescript strips comments otherwise
400
+ // prettier-ignore
401
+ static BASE =
402
+ /* @__PURE__ */ (() => new _DecafPoint(new ed448_Point(DECAF_BASE_X, DECAF_BASE_Y, _1n, DECAF_BASE_T)))();
403
+ // prettier-ignore
404
+ static ZERO =
405
+ /* @__PURE__ */ (() => new _DecafPoint(ed448_Point.ZERO))();
406
+ // prettier-ignore
407
+ static Fp =
408
+ /* @__PURE__ */ (() => Fp448)();
409
+ // prettier-ignore
410
+ static Fn =
411
+ /* @__PURE__ */ (() => Fn448)();
412
+ constructor(ep) {
413
+ super(ep);
414
+ }
415
+ /**
416
+ * Create one Decaf448 point from affine Edwards coordinates.
417
+ * This wraps the internal Edwards representative directly and is not a
418
+ * canonical decaf448 decoding path.
419
+ * Use `toBytes()` / `fromBytes()` if canonical decaf448 bytes matter.
420
+ */
421
+ static fromAffine(ap) {
422
+ return new _DecafPoint(ed448_Point.fromAffine(ap));
423
+ }
424
+ assertSame(other) {
425
+ if (!(other instanceof _DecafPoint))
426
+ throw new Error('DecafPoint expected');
427
+ }
428
+ init(ep) {
429
+ return new _DecafPoint(ep);
430
+ }
431
+ static fromBytes(bytes) {
432
+ abytes(bytes, 56);
433
+ const { d, p: P } = ed448_CURVE;
434
+ const mod = (n) => Fp448.create(n);
435
+ const s = Fp448.fromBytes(bytes);
436
+ // 1. Check that s_bytes is the canonical encoding of a field element, or else abort.
437
+ // 2. Check that s is non-negative, or else abort
438
+ if (!equalBytes(Fn448.toBytes(s), bytes) || isNegativeLE(s, P))
439
+ throw new Error('invalid decaf448 encoding 1');
440
+ const s2 = mod(s * s); // 1
441
+ const u1 = mod(_1n + s2); // 2
442
+ const u1sq = mod(u1 * u1);
443
+ const u2 = mod(u1sq - _4n * d * s2); // 3
444
+ const { isValid, value: invsqrt } = invertSqrt(mod(u2 * u1sq)); // 4
445
+ let u3 = mod((s + s) * invsqrt * u1 * SQRT_MINUS_D); // 5
446
+ if (isNegativeLE(u3, P))
447
+ u3 = mod(-u3);
448
+ const x = mod(u3 * invsqrt * u2 * INVSQRT_MINUS_D); // 6
449
+ const y = mod((_1n - s2) * invsqrt * u1); // 7
450
+ const t = mod(x * y); // 8
451
+ if (!isValid)
452
+ throw new Error('invalid decaf448 encoding 2');
453
+ return new _DecafPoint(new ed448_Point(x, y, _1n, t));
454
+ }
455
+ /**
456
+ * Converts decaf-encoded string to decaf point.
457
+ * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode-2).
458
+ * @param hex - Decaf-encoded 56 bytes. Not every 56-byte string is valid decaf encoding
459
+ */
460
+ static fromHex(hex) {
461
+ return _DecafPoint.fromBytes(hexToBytes(hex));
462
+ }
463
+ /**
464
+ * Encodes decaf point to Uint8Array.
465
+ * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-encode-2).
466
+ */
467
+ toBytes() {
468
+ const { X, Z, T } = this.ep;
469
+ const P = ed448_CURVE.p;
470
+ const mod = (n) => Fp448.create(n);
471
+ const u1 = mod(mod(X + T) * mod(X - T)); // 1
472
+ const x2 = mod(X * X);
473
+ const { value: invsqrt } = invertSqrt(mod(u1 * ONE_MINUS_D * x2)); // 2
474
+ let ratio = mod(invsqrt * u1 * SQRT_MINUS_D); // 3
475
+ if (isNegativeLE(ratio, P))
476
+ ratio = mod(-ratio);
477
+ const u2 = mod(INVSQRT_MINUS_D * ratio * Z - T); // 4
478
+ let s = mod(ONE_MINUS_D * invsqrt * X * u2); // 5
479
+ if (isNegativeLE(s, P))
480
+ s = mod(-s);
481
+ return Fn448.toBytes(s);
482
+ }
483
+ /**
484
+ * Compare one point to another.
485
+ * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-equals-2).
486
+ */
487
+ equals(other) {
488
+ this.assertSame(other);
489
+ const { X: X1, Y: Y1 } = this.ep;
490
+ const { X: X2, Y: Y2 } = other.ep;
491
+ // (x1 * y2 == y1 * x2)
492
+ return Fp448.create(X1 * Y2) === Fp448.create(Y1 * X2);
493
+ }
494
+ is0() {
495
+ return this.equals(_DecafPoint.ZERO);
496
+ }
497
+ }
498
+ Object.freeze(_DecafPoint.BASE);
499
+ Object.freeze(_DecafPoint.ZERO);
500
+ Object.freeze(_DecafPoint.prototype);
501
+ Object.freeze(_DecafPoint);
502
+ /** Prime-order Decaf448 group bundle. */
503
+ export const decaf448 = /* @__PURE__ */ Object.freeze({ Point: _DecafPoint });
504
+ /**
505
+ * Hashing to decaf448 points / field. RFC 9380 methods.
506
+ * `hashToCurve()` is RFC 9380 `hash_to_decaf448`, `deriveToCurve()` is RFC
507
+ * 9496 element derivation, and `hashToScalar()` is a library helper layered on
508
+ * top of RFC 9496 scalar reduction.
509
+ * @example
510
+ * Hash one message onto decaf448.
511
+ *
512
+ * ```ts
513
+ * const point = decaf448_hasher.hashToCurve(new TextEncoder().encode('hello noble'));
514
+ * ```
515
+ */
516
+ export const decaf448_hasher = Object.freeze({
517
+ Point: _DecafPoint,
518
+ hashToCurve(msg, options) {
519
+ // Preserve explicit empty/invalid DST overrides so expand_message_xof() can reject them.
520
+ const DST = options?.DST === undefined ? 'decaf448_XOF:SHAKE256_D448MAP_RO_' : options.DST;
521
+ return decaf448_hasher.deriveToCurve(expand_message_xof(msg, DST, 112, 224, shake256));
522
+ },
523
+ /**
524
+ * Warning: has big modulo bias of 2^-64.
525
+ * RFC is invalid. RFC says "use 64-byte xof", while for 2^-112 bias
526
+ * it must use 84-byte xof (56+56/2), not 64.
527
+ */
528
+ hashToScalar(msg, options = { DST: _DST_scalar }) {
529
+ // Can't use `Fn448.fromBytes()`. 64-byte input => 56-byte field element
530
+ const xof = expand_message_xof(msg, options.DST, 64, 256, shake256);
531
+ return Fn448.create(bytesToNumberLE(xof));
532
+ },
533
+ /**
534
+ * HashToCurve-like construction based on RFC 9496 (Element Derivation).
535
+ * Converts 112 uniform random bytes into a curve point.
536
+ *
537
+ * WARNING: This represents an older hash-to-curve construction from before
538
+ * RFC 9380 was finalized.
539
+ * It was later reused as a component in the newer
540
+ * `hash_to_decaf448` function defined in RFC 9380.
541
+ */
542
+ deriveToCurve(bytes) {
543
+ abytes(bytes, 112);
544
+ const skipValidation = true;
545
+ // Note: Similar to the field element decoding described in
546
+ // [RFC7748], and unlike the field element decoding described in
547
+ // Section 5.3.1, non-canonical values are accepted.
548
+ const r1 = Fp448.create(Fp448.fromBytes(bytes.subarray(0, 56), skipValidation));
549
+ const R1 = calcElligatorDecafMap(r1);
550
+ const r2 = Fp448.create(Fp448.fromBytes(bytes.subarray(56, 112), skipValidation));
551
+ const R2 = calcElligatorDecafMap(r2);
552
+ return new _DecafPoint(R1.add(R2));
553
+ },
554
+ });
555
+ /**
556
+ * decaf448 OPRF, defined in RFC 9497.
557
+ * @example
558
+ * Run one blind/evaluate/finalize OPRF round over decaf448.
559
+ *
560
+ * ```ts
561
+ * const input = new TextEncoder().encode('hello noble');
562
+ * const keys = decaf448_oprf.oprf.generateKeyPair();
563
+ * const blind = decaf448_oprf.oprf.blind(input);
564
+ * const evaluated = decaf448_oprf.oprf.blindEvaluate(keys.secretKey, blind.blinded);
565
+ * const output = decaf448_oprf.oprf.finalize(input, blind.blind, evaluated);
566
+ * ```
567
+ */
568
+ export const decaf448_oprf = /* @__PURE__ */ (() => createOPRF({
569
+ name: 'decaf448-SHAKE256',
570
+ Point: _DecafPoint,
571
+ hash: (msg) => shake256(msg, { dkLen: 64 }),
572
+ hashToGroup: decaf448_hasher.hashToCurve,
573
+ hashToScalar: decaf448_hasher.hashToScalar,
574
+ }))();
575
+ /**
576
+ * Weird / bogus points, useful for debugging.
577
+ * Unlike ed25519, there is no ed448 generator point which can produce full T subgroup.
578
+ * Instead, the torsion subgroup here is cyclic of order 4, generated by
579
+ * `(1, 0)`, and the array below lists that subgroup set (Klein four-group).
580
+ * @example
581
+ * Decode one known torsion point for debugging.
582
+ *
583
+ * ```ts
584
+ * import { ED448_TORSION_SUBGROUP, ed448 } from './ed448';
585
+ * const point = ed448.Point.fromHex(ED448_TORSION_SUBGROUP[1]);
586
+ * ```
587
+ */
588
+ export const ED448_TORSION_SUBGROUP = /* @__PURE__ */ Object.freeze([
589
+ '010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
590
+ 'fefffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffffffffffffffffffffffffffffffffffffffffffffffffff00',
591
+ '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
592
+ '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080',
593
+ ]);
package/esm/index.js ADDED
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ /**
3
+ * Audited & minimal JS implementation of elliptic curve cryptography.
4
+ * @module
5
+ * @example
6
+ ```js
7
+ import { secp256k1, schnorr } from './secp256k1';
8
+ import { ed25519, ed25519ph, ed25519ctx, x25519, ristretto255 } from './ed25519';
9
+ import { ed448, ed448ph, x448, decaf448 } from './ed448';
10
+ import { p256, p384, p521 } from './nist';
11
+ import { bls12_381 } from './bls12-381';
12
+ import { bn254 } from './bn254';
13
+ import {
14
+ jubjub,
15
+ babyjubjub,
16
+ brainpoolP256r1,
17
+ brainpoolP384r1,
18
+ brainpoolP512r1,
19
+ } from './misc';
20
+ import * as webcrypto from './webcrypto';
21
+
22
+ // hash-to-curve
23
+ import { secp256k1_hasher } from './secp256k1';
24
+ import { p256_hasher, p384_hasher, p521_hasher } from './nist';
25
+ import { ristretto255_hasher } from './ed25519';
26
+ import { decaf448_hasher } from './ed448';
27
+
28
+ // OPRFs
29
+ import { p256_oprf, p384_oprf, p521_oprf } from './nist';
30
+ import { ristretto255_oprf } from './ed25519';
31
+ import { decaf448_oprf } from './ed448';
32
+
33
+ // utils
34
+ import { bytesToHex, hexToBytes, concatBytes } from './abstract/utils';
35
+ import { Field } from './abstract/modular';
36
+ ```
37
+ */
38
+ throw new Error('root module cannot be imported: import submodules instead. Check out README');