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