@toon-protocol/client-mcp 0.20.2 → 0.20.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-LN2OF264.js → chunk-6GXZ4KRO.js} +257 -1552
- package/dist/chunk-6GXZ4KRO.js.map +1 -0
- package/dist/{chunk-ZKSFO7M3.js → chunk-F3XAIOGQ.js} +2 -2
- package/dist/{chunk-IAOKAQLA.js → chunk-FDUYHYB2.js} +2 -2
- package/dist/{chunk-IAOKAQLA.js.map → chunk-FDUYHYB2.js.map} +1 -1
- package/dist/chunk-IYPIOSEF.js +3856 -0
- package/dist/chunk-IYPIOSEF.js.map +1 -0
- package/dist/{chunk-UPIAVE44.js → chunk-JFDVE4IA.js} +544 -4115
- package/dist/chunk-JFDVE4IA.js.map +1 -0
- package/dist/{chunk-WS3GVRQJ.js → chunk-TGIKIMXO.js} +9 -7
- package/dist/{chunk-WS3GVRQJ.js.map → chunk-TGIKIMXO.js.map} +1 -1
- package/dist/chunk-V7D5HJBT.js +1363 -0
- package/dist/chunk-V7D5HJBT.js.map +1 -0
- package/dist/daemon.js +6 -4
- package/dist/daemon.js.map +1 -1
- package/dist/{ed25519-VBPL32VX.js → ed25519-U6LNJH4K.js} +3 -2
- package/dist/index.js +7 -5
- package/dist/index.js.map +1 -1
- package/dist/mcp.js +6 -4
- package/dist/mcp.js.map +1 -1
- package/dist/mina-channel-deploy-CO2LMPPB-H2N5FX4G.js +12 -0
- package/dist/mina-channel-deploy-CO2LMPPB-H2N5FX4G.js.map +1 -0
- package/dist/{node-WPA2UDEH-RCLJ66AU.js → node-WPA2UDEH-HZP3B4FH.js} +3 -3
- package/dist/{node-WPA2UDEH-RCLJ66AU.js.map → node-WPA2UDEH-HZP3B4FH.js.map} +1 -1
- package/package.json +5 -5
- package/dist/chunk-LN2OF264.js.map +0 -1
- package/dist/chunk-UPIAVE44.js.map +0 -1
- /package/dist/{chunk-ZKSFO7M3.js.map → chunk-F3XAIOGQ.js.map} +0 -0
- /package/dist/{ed25519-VBPL32VX.js.map → ed25519-U6LNJH4K.js.map} +0 -0
|
@@ -0,0 +1,1363 @@
|
|
|
1
|
+
import { createRequire as __cr } from 'module'; const require = __cr(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
FpInvertBatch,
|
|
4
|
+
FpSqrtEven,
|
|
5
|
+
_DST_scalar,
|
|
6
|
+
aInRange,
|
|
7
|
+
abool,
|
|
8
|
+
abytes,
|
|
9
|
+
abytes2,
|
|
10
|
+
asafenumber,
|
|
11
|
+
asciiToBytes,
|
|
12
|
+
bytesToHex2 as bytesToHex,
|
|
13
|
+
bytesToNumberBE,
|
|
14
|
+
bytesToNumberLE,
|
|
15
|
+
concatBytes,
|
|
16
|
+
concatBytes2,
|
|
17
|
+
copyBytes,
|
|
18
|
+
createCurveFields,
|
|
19
|
+
createFROST,
|
|
20
|
+
createHasher2 as createHasher,
|
|
21
|
+
createKeygen,
|
|
22
|
+
equalBytes,
|
|
23
|
+
expand_message_xmd,
|
|
24
|
+
getMinHashLength,
|
|
25
|
+
hexToBytes,
|
|
26
|
+
hexToBytes2,
|
|
27
|
+
isBytes,
|
|
28
|
+
isNegativeLE,
|
|
29
|
+
mapHashToField,
|
|
30
|
+
mod,
|
|
31
|
+
normalizeZ,
|
|
32
|
+
notImplemented,
|
|
33
|
+
numberToBytesBE,
|
|
34
|
+
numberToBytesLE,
|
|
35
|
+
pippenger,
|
|
36
|
+
pow2,
|
|
37
|
+
randomBytes2 as randomBytes,
|
|
38
|
+
sha512,
|
|
39
|
+
validateObject,
|
|
40
|
+
validatePointCons,
|
|
41
|
+
wNAF
|
|
42
|
+
} from "./chunk-6GXZ4KRO.js";
|
|
43
|
+
|
|
44
|
+
// ../../node_modules/.pnpm/@noble+curves@2.2.0/node_modules/@noble/curves/abstract/edwards.js
|
|
45
|
+
var _0n = /* @__PURE__ */ BigInt(0);
|
|
46
|
+
var _1n = /* @__PURE__ */ BigInt(1);
|
|
47
|
+
var _2n = /* @__PURE__ */ BigInt(2);
|
|
48
|
+
var _8n = /* @__PURE__ */ BigInt(8);
|
|
49
|
+
function isEdValidXY(Fp2, CURVE, x, y) {
|
|
50
|
+
const x2 = Fp2.sqr(x);
|
|
51
|
+
const y2 = Fp2.sqr(y);
|
|
52
|
+
const left = Fp2.add(Fp2.mul(CURVE.a, x2), y2);
|
|
53
|
+
const right = Fp2.add(Fp2.ONE, Fp2.mul(CURVE.d, Fp2.mul(x2, y2)));
|
|
54
|
+
return Fp2.eql(left, right);
|
|
55
|
+
}
|
|
56
|
+
function edwards(params, extraOpts = {}) {
|
|
57
|
+
const opts = extraOpts;
|
|
58
|
+
const validated = createCurveFields("edwards", params, opts, opts.FpFnLE);
|
|
59
|
+
const { Fp: Fp2, Fn: Fn2 } = validated;
|
|
60
|
+
let CURVE = validated.CURVE;
|
|
61
|
+
const { h: cofactor } = CURVE;
|
|
62
|
+
validateObject(opts, {}, { uvRatio: "function" });
|
|
63
|
+
const MASK = _2n << BigInt(Fn2.BYTES * 8) - _1n;
|
|
64
|
+
const modP = (n) => Fp2.create(n);
|
|
65
|
+
const uvRatio2 = opts.uvRatio === void 0 ? (u, v) => {
|
|
66
|
+
try {
|
|
67
|
+
return { isValid: true, value: Fp2.sqrt(Fp2.div(u, v)) };
|
|
68
|
+
} catch (e) {
|
|
69
|
+
return { isValid: false, value: _0n };
|
|
70
|
+
}
|
|
71
|
+
} : opts.uvRatio;
|
|
72
|
+
if (!isEdValidXY(Fp2, CURVE, CURVE.Gx, CURVE.Gy))
|
|
73
|
+
throw new Error("bad curve params: generator point");
|
|
74
|
+
function acoord(title, n, banZero = false) {
|
|
75
|
+
const min = banZero ? _1n : _0n;
|
|
76
|
+
aInRange("coordinate " + title, n, min, MASK);
|
|
77
|
+
return n;
|
|
78
|
+
}
|
|
79
|
+
function aedpoint(other) {
|
|
80
|
+
if (!(other instanceof Point))
|
|
81
|
+
throw new Error("EdwardsPoint expected");
|
|
82
|
+
}
|
|
83
|
+
class Point {
|
|
84
|
+
// base / generator point
|
|
85
|
+
static BASE = new Point(CURVE.Gx, CURVE.Gy, _1n, modP(CURVE.Gx * CURVE.Gy));
|
|
86
|
+
// zero / infinity / identity point
|
|
87
|
+
static ZERO = new Point(_0n, _1n, _1n, _0n);
|
|
88
|
+
// 0, 1, 1, 0
|
|
89
|
+
// math field
|
|
90
|
+
static Fp = Fp2;
|
|
91
|
+
// scalar field
|
|
92
|
+
static Fn = Fn2;
|
|
93
|
+
X;
|
|
94
|
+
Y;
|
|
95
|
+
Z;
|
|
96
|
+
T;
|
|
97
|
+
constructor(X, Y, Z, T) {
|
|
98
|
+
this.X = acoord("x", X);
|
|
99
|
+
this.Y = acoord("y", Y);
|
|
100
|
+
this.Z = acoord("z", Z, true);
|
|
101
|
+
this.T = acoord("t", T);
|
|
102
|
+
Object.freeze(this);
|
|
103
|
+
}
|
|
104
|
+
static CURVE() {
|
|
105
|
+
return CURVE;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Create one extended Edwards point from affine coordinates.
|
|
109
|
+
* Does NOT validate that the point is on-curve or torsion-free.
|
|
110
|
+
* Use `.assertValidity()` on adversarial inputs.
|
|
111
|
+
*/
|
|
112
|
+
static fromAffine(p) {
|
|
113
|
+
if (p instanceof Point)
|
|
114
|
+
throw new Error("extended point not allowed");
|
|
115
|
+
const { x, y } = p || {};
|
|
116
|
+
acoord("x", x);
|
|
117
|
+
acoord("y", y);
|
|
118
|
+
return new Point(x, y, _1n, modP(x * y));
|
|
119
|
+
}
|
|
120
|
+
// Uses algo from RFC8032 5.1.3.
|
|
121
|
+
static fromBytes(bytes, zip215 = false) {
|
|
122
|
+
const len = Fp2.BYTES;
|
|
123
|
+
const { a, d } = CURVE;
|
|
124
|
+
bytes = copyBytes(abytes2(bytes, len, "point"));
|
|
125
|
+
abool(zip215, "zip215");
|
|
126
|
+
const normed = copyBytes(bytes);
|
|
127
|
+
const lastByte = bytes[len - 1];
|
|
128
|
+
normed[len - 1] = lastByte & ~128;
|
|
129
|
+
const y = bytesToNumberLE(normed);
|
|
130
|
+
const max = zip215 ? MASK : Fp2.ORDER;
|
|
131
|
+
aInRange("point.y", y, _0n, max);
|
|
132
|
+
const y2 = modP(y * y);
|
|
133
|
+
const u = modP(y2 - _1n);
|
|
134
|
+
const v = modP(d * y2 - a);
|
|
135
|
+
let { isValid, value: x } = uvRatio2(u, v);
|
|
136
|
+
if (!isValid)
|
|
137
|
+
throw new Error("bad point: invalid y coordinate");
|
|
138
|
+
const isXOdd = (x & _1n) === _1n;
|
|
139
|
+
const isLastByteOdd = (lastByte & 128) !== 0;
|
|
140
|
+
if (!zip215 && x === _0n && isLastByteOdd)
|
|
141
|
+
throw new Error("bad point: x=0 and x_0=1");
|
|
142
|
+
if (isLastByteOdd !== isXOdd)
|
|
143
|
+
x = modP(-x);
|
|
144
|
+
return Point.fromAffine({ x, y });
|
|
145
|
+
}
|
|
146
|
+
static fromHex(hex, zip215 = false) {
|
|
147
|
+
return Point.fromBytes(hexToBytes2(hex), zip215);
|
|
148
|
+
}
|
|
149
|
+
get x() {
|
|
150
|
+
return this.toAffine().x;
|
|
151
|
+
}
|
|
152
|
+
get y() {
|
|
153
|
+
return this.toAffine().y;
|
|
154
|
+
}
|
|
155
|
+
precompute(windowSize = 8, isLazy = true) {
|
|
156
|
+
wnaf.createCache(this, windowSize);
|
|
157
|
+
if (!isLazy)
|
|
158
|
+
this.multiply(_2n);
|
|
159
|
+
return this;
|
|
160
|
+
}
|
|
161
|
+
// Useful in fromAffine() - not for fromBytes(), which always created valid points.
|
|
162
|
+
assertValidity() {
|
|
163
|
+
const p = this;
|
|
164
|
+
const { a, d } = CURVE;
|
|
165
|
+
if (p.is0())
|
|
166
|
+
throw new Error("bad point: ZERO");
|
|
167
|
+
const { X, Y, Z, T } = p;
|
|
168
|
+
const X2 = modP(X * X);
|
|
169
|
+
const Y2 = modP(Y * Y);
|
|
170
|
+
const Z2 = modP(Z * Z);
|
|
171
|
+
const Z4 = modP(Z2 * Z2);
|
|
172
|
+
const aX2 = modP(X2 * a);
|
|
173
|
+
const left = modP(Z2 * modP(aX2 + Y2));
|
|
174
|
+
const right = modP(Z4 + modP(d * modP(X2 * Y2)));
|
|
175
|
+
if (left !== right)
|
|
176
|
+
throw new Error("bad point: equation left != right (1)");
|
|
177
|
+
const XY = modP(X * Y);
|
|
178
|
+
const ZT = modP(Z * T);
|
|
179
|
+
if (XY !== ZT)
|
|
180
|
+
throw new Error("bad point: equation left != right (2)");
|
|
181
|
+
}
|
|
182
|
+
// Compare one point to another.
|
|
183
|
+
equals(other) {
|
|
184
|
+
aedpoint(other);
|
|
185
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
186
|
+
const { X: X2, Y: Y2, Z: Z2 } = other;
|
|
187
|
+
const X1Z2 = modP(X1 * Z2);
|
|
188
|
+
const X2Z1 = modP(X2 * Z1);
|
|
189
|
+
const Y1Z2 = modP(Y1 * Z2);
|
|
190
|
+
const Y2Z1 = modP(Y2 * Z1);
|
|
191
|
+
return X1Z2 === X2Z1 && Y1Z2 === Y2Z1;
|
|
192
|
+
}
|
|
193
|
+
is0() {
|
|
194
|
+
return this.equals(Point.ZERO);
|
|
195
|
+
}
|
|
196
|
+
negate() {
|
|
197
|
+
return new Point(modP(-this.X), this.Y, this.Z, modP(-this.T));
|
|
198
|
+
}
|
|
199
|
+
// Fast algo for doubling Extended Point.
|
|
200
|
+
// https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#doubling-dbl-2008-hwcd
|
|
201
|
+
// Cost: 4M + 4S + 1*a + 6add + 1*2.
|
|
202
|
+
double() {
|
|
203
|
+
const { a } = CURVE;
|
|
204
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
205
|
+
const A = modP(X1 * X1);
|
|
206
|
+
const B = modP(Y1 * Y1);
|
|
207
|
+
const C = modP(_2n * modP(Z1 * Z1));
|
|
208
|
+
const D = modP(a * A);
|
|
209
|
+
const x1y1 = X1 + Y1;
|
|
210
|
+
const E = modP(modP(x1y1 * x1y1) - A - B);
|
|
211
|
+
const G = D + B;
|
|
212
|
+
const F = G - C;
|
|
213
|
+
const H = D - B;
|
|
214
|
+
const X3 = modP(E * F);
|
|
215
|
+
const Y3 = modP(G * H);
|
|
216
|
+
const T3 = modP(E * H);
|
|
217
|
+
const Z3 = modP(F * G);
|
|
218
|
+
return new Point(X3, Y3, Z3, T3);
|
|
219
|
+
}
|
|
220
|
+
// Fast algo for adding 2 Extended Points.
|
|
221
|
+
// https://hyperelliptic.org/EFD/g1p/auto-twisted-extended.html#addition-add-2008-hwcd
|
|
222
|
+
// Cost: 9M + 1*a + 1*d + 7add.
|
|
223
|
+
add(other) {
|
|
224
|
+
aedpoint(other);
|
|
225
|
+
const { a, d } = CURVE;
|
|
226
|
+
const { X: X1, Y: Y1, Z: Z1, T: T1 } = this;
|
|
227
|
+
const { X: X2, Y: Y2, Z: Z2, T: T2 } = other;
|
|
228
|
+
const A = modP(X1 * X2);
|
|
229
|
+
const B = modP(Y1 * Y2);
|
|
230
|
+
const C = modP(T1 * d * T2);
|
|
231
|
+
const D = modP(Z1 * Z2);
|
|
232
|
+
const E = modP((X1 + Y1) * (X2 + Y2) - A - B);
|
|
233
|
+
const F = D - C;
|
|
234
|
+
const G = D + C;
|
|
235
|
+
const H = modP(B - a * A);
|
|
236
|
+
const X3 = modP(E * F);
|
|
237
|
+
const Y3 = modP(G * H);
|
|
238
|
+
const T3 = modP(E * H);
|
|
239
|
+
const Z3 = modP(F * G);
|
|
240
|
+
return new Point(X3, Y3, Z3, T3);
|
|
241
|
+
}
|
|
242
|
+
subtract(other) {
|
|
243
|
+
aedpoint(other);
|
|
244
|
+
return this.add(other.negate());
|
|
245
|
+
}
|
|
246
|
+
// Constant-time multiplication.
|
|
247
|
+
multiply(scalar) {
|
|
248
|
+
if (!Fn2.isValidNot0(scalar))
|
|
249
|
+
throw new RangeError("invalid scalar: expected 1 <= sc < curve.n");
|
|
250
|
+
const { p, f } = wnaf.cached(this, scalar, (p2) => normalizeZ(Point, p2));
|
|
251
|
+
return normalizeZ(Point, [p, f])[0];
|
|
252
|
+
}
|
|
253
|
+
// Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
254
|
+
// It's faster, but should only be used when you don't care about
|
|
255
|
+
// an exposed private key e.g. sig verification.
|
|
256
|
+
// Keeps the same subgroup-scalar contract: 0 is allowed for public-scalar callers, but
|
|
257
|
+
// n and larger values are rejected instead of being reduced mod n to the identity point.
|
|
258
|
+
multiplyUnsafe(scalar) {
|
|
259
|
+
if (!Fn2.isValid(scalar))
|
|
260
|
+
throw new RangeError("invalid scalar: expected 0 <= sc < curve.n");
|
|
261
|
+
if (scalar === _0n)
|
|
262
|
+
return Point.ZERO;
|
|
263
|
+
if (this.is0() || scalar === _1n)
|
|
264
|
+
return this;
|
|
265
|
+
return wnaf.unsafe(this, scalar, (p) => normalizeZ(Point, p));
|
|
266
|
+
}
|
|
267
|
+
// Checks if point is of small order.
|
|
268
|
+
// If you add something to small order point, you will have "dirty"
|
|
269
|
+
// point with torsion component.
|
|
270
|
+
// Clears cofactor and checks if the result is 0.
|
|
271
|
+
isSmallOrder() {
|
|
272
|
+
return this.clearCofactor().is0();
|
|
273
|
+
}
|
|
274
|
+
// Multiplies point by curve order and checks if the result is 0.
|
|
275
|
+
// Returns `false` is the point is dirty.
|
|
276
|
+
isTorsionFree() {
|
|
277
|
+
return wnaf.unsafe(this, CURVE.n).is0();
|
|
278
|
+
}
|
|
279
|
+
// Converts Extended point to default (x, y) coordinates.
|
|
280
|
+
// Can accept precomputed Z^-1 - for example, from invertBatch.
|
|
281
|
+
toAffine(invertedZ) {
|
|
282
|
+
const p = this;
|
|
283
|
+
let iz = invertedZ;
|
|
284
|
+
const { X, Y, Z } = p;
|
|
285
|
+
const is0 = p.is0();
|
|
286
|
+
if (iz == null)
|
|
287
|
+
iz = is0 ? _8n : Fp2.inv(Z);
|
|
288
|
+
const x = modP(X * iz);
|
|
289
|
+
const y = modP(Y * iz);
|
|
290
|
+
const zz = Fp2.mul(Z, iz);
|
|
291
|
+
if (is0)
|
|
292
|
+
return { x: _0n, y: _1n };
|
|
293
|
+
if (zz !== _1n)
|
|
294
|
+
throw new Error("invZ was invalid");
|
|
295
|
+
return { x, y };
|
|
296
|
+
}
|
|
297
|
+
clearCofactor() {
|
|
298
|
+
if (cofactor === _1n)
|
|
299
|
+
return this;
|
|
300
|
+
return this.multiplyUnsafe(cofactor);
|
|
301
|
+
}
|
|
302
|
+
toBytes() {
|
|
303
|
+
const { x, y } = this.toAffine();
|
|
304
|
+
const bytes = Fp2.toBytes(y);
|
|
305
|
+
bytes[bytes.length - 1] |= x & _1n ? 128 : 0;
|
|
306
|
+
return bytes;
|
|
307
|
+
}
|
|
308
|
+
toHex() {
|
|
309
|
+
return bytesToHex(this.toBytes());
|
|
310
|
+
}
|
|
311
|
+
toString() {
|
|
312
|
+
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
const wnaf = new wNAF(Point, Fn2.BITS);
|
|
316
|
+
if (Fn2.BITS >= 8)
|
|
317
|
+
Point.BASE.precompute(8);
|
|
318
|
+
Object.freeze(Point.prototype);
|
|
319
|
+
Object.freeze(Point);
|
|
320
|
+
return Point;
|
|
321
|
+
}
|
|
322
|
+
var PrimeEdwardsPoint = class {
|
|
323
|
+
static BASE;
|
|
324
|
+
static ZERO;
|
|
325
|
+
static Fp;
|
|
326
|
+
static Fn;
|
|
327
|
+
ep;
|
|
328
|
+
/**
|
|
329
|
+
* Wrap one internal Edwards representative directly.
|
|
330
|
+
* This is not a canonical encoding boundary: alternate Edwards
|
|
331
|
+
* representatives may still describe the same abstract wrapper element.
|
|
332
|
+
*/
|
|
333
|
+
constructor(ep) {
|
|
334
|
+
this.ep = ep;
|
|
335
|
+
}
|
|
336
|
+
// Static methods that must be implemented by subclasses
|
|
337
|
+
static fromBytes(_bytes) {
|
|
338
|
+
notImplemented();
|
|
339
|
+
}
|
|
340
|
+
static fromHex(_hex) {
|
|
341
|
+
notImplemented();
|
|
342
|
+
}
|
|
343
|
+
get x() {
|
|
344
|
+
return this.toAffine().x;
|
|
345
|
+
}
|
|
346
|
+
get y() {
|
|
347
|
+
return this.toAffine().y;
|
|
348
|
+
}
|
|
349
|
+
// Common implementations
|
|
350
|
+
clearCofactor() {
|
|
351
|
+
return this;
|
|
352
|
+
}
|
|
353
|
+
assertValidity() {
|
|
354
|
+
this.ep.assertValidity();
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Return affine coordinates of the current internal Edwards representative.
|
|
358
|
+
* This is a convenience helper, not a canonical Ristretto/Decaf encoding.
|
|
359
|
+
* Equal abstract elements may expose different `x` / `y`; use
|
|
360
|
+
* `toBytes()` / `fromBytes()` for canonical roundtrips.
|
|
361
|
+
*/
|
|
362
|
+
toAffine(invertedZ) {
|
|
363
|
+
return this.ep.toAffine(invertedZ);
|
|
364
|
+
}
|
|
365
|
+
toHex() {
|
|
366
|
+
return bytesToHex(this.toBytes());
|
|
367
|
+
}
|
|
368
|
+
toString() {
|
|
369
|
+
return this.toHex();
|
|
370
|
+
}
|
|
371
|
+
isTorsionFree() {
|
|
372
|
+
return true;
|
|
373
|
+
}
|
|
374
|
+
isSmallOrder() {
|
|
375
|
+
return false;
|
|
376
|
+
}
|
|
377
|
+
add(other) {
|
|
378
|
+
this.assertSame(other);
|
|
379
|
+
return this.init(this.ep.add(other.ep));
|
|
380
|
+
}
|
|
381
|
+
subtract(other) {
|
|
382
|
+
this.assertSame(other);
|
|
383
|
+
return this.init(this.ep.subtract(other.ep));
|
|
384
|
+
}
|
|
385
|
+
multiply(scalar) {
|
|
386
|
+
return this.init(this.ep.multiply(scalar));
|
|
387
|
+
}
|
|
388
|
+
multiplyUnsafe(scalar) {
|
|
389
|
+
return this.init(this.ep.multiplyUnsafe(scalar));
|
|
390
|
+
}
|
|
391
|
+
double() {
|
|
392
|
+
return this.init(this.ep.double());
|
|
393
|
+
}
|
|
394
|
+
negate() {
|
|
395
|
+
return this.init(this.ep.negate());
|
|
396
|
+
}
|
|
397
|
+
precompute(windowSize, isLazy) {
|
|
398
|
+
this.ep.precompute(windowSize, isLazy);
|
|
399
|
+
return this;
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
function eddsa(Point, cHash, eddsaOpts = {}) {
|
|
403
|
+
if (typeof cHash !== "function")
|
|
404
|
+
throw new Error('"hash" function param is required');
|
|
405
|
+
const hash = cHash;
|
|
406
|
+
const opts = eddsaOpts;
|
|
407
|
+
validateObject(opts, {}, {
|
|
408
|
+
adjustScalarBytes: "function",
|
|
409
|
+
randomBytes: "function",
|
|
410
|
+
domain: "function",
|
|
411
|
+
prehash: "function",
|
|
412
|
+
zip215: "boolean",
|
|
413
|
+
mapToCurve: "function"
|
|
414
|
+
});
|
|
415
|
+
const { prehash } = opts;
|
|
416
|
+
const { BASE, Fp: Fp2, Fn: Fn2 } = Point;
|
|
417
|
+
const outputLen = hash.outputLen;
|
|
418
|
+
const expectedLen = 2 * Fp2.BYTES;
|
|
419
|
+
if (outputLen !== void 0) {
|
|
420
|
+
asafenumber(outputLen, "hash.outputLen");
|
|
421
|
+
if (outputLen !== expectedLen)
|
|
422
|
+
throw new Error(`hash.outputLen must be ${expectedLen}, got ${outputLen}`);
|
|
423
|
+
}
|
|
424
|
+
const randomBytes2 = opts.randomBytes === void 0 ? randomBytes : opts.randomBytes;
|
|
425
|
+
const adjustScalarBytes2 = opts.adjustScalarBytes === void 0 ? (bytes) => bytes : opts.adjustScalarBytes;
|
|
426
|
+
const domain = opts.domain === void 0 ? (data, ctx, phflag) => {
|
|
427
|
+
abool(phflag, "phflag");
|
|
428
|
+
if (ctx.length || phflag)
|
|
429
|
+
throw new Error("Contexts/pre-hash are not supported");
|
|
430
|
+
return data;
|
|
431
|
+
} : opts.domain;
|
|
432
|
+
function modN_LE(hash2) {
|
|
433
|
+
return Fn2.create(bytesToNumberLE(hash2));
|
|
434
|
+
}
|
|
435
|
+
function getPrivateScalar(key) {
|
|
436
|
+
const len = lengths.secretKey;
|
|
437
|
+
abytes2(key, lengths.secretKey, "secretKey");
|
|
438
|
+
const hashed = abytes2(hash(key), 2 * len, "hashedSecretKey");
|
|
439
|
+
const head = adjustScalarBytes2(hashed.slice(0, len));
|
|
440
|
+
const prefix = hashed.slice(len, 2 * len);
|
|
441
|
+
const scalar = modN_LE(head);
|
|
442
|
+
return { head, prefix, scalar };
|
|
443
|
+
}
|
|
444
|
+
function getExtendedPublicKey(secretKey) {
|
|
445
|
+
const { head, prefix, scalar } = getPrivateScalar(secretKey);
|
|
446
|
+
const point = BASE.multiply(scalar);
|
|
447
|
+
const pointBytes = point.toBytes();
|
|
448
|
+
return { head, prefix, scalar, point, pointBytes };
|
|
449
|
+
}
|
|
450
|
+
function getPublicKey(secretKey) {
|
|
451
|
+
return getExtendedPublicKey(secretKey).pointBytes;
|
|
452
|
+
}
|
|
453
|
+
function hashDomainToScalar(context = Uint8Array.of(), ...msgs) {
|
|
454
|
+
const msg = concatBytes2(...msgs);
|
|
455
|
+
return modN_LE(hash(domain(msg, abytes2(context, void 0, "context"), !!prehash)));
|
|
456
|
+
}
|
|
457
|
+
function sign(msg, secretKey, options = {}) {
|
|
458
|
+
msg = abytes2(msg, void 0, "message");
|
|
459
|
+
if (prehash)
|
|
460
|
+
msg = prehash(msg);
|
|
461
|
+
const { prefix, scalar, pointBytes } = getExtendedPublicKey(secretKey);
|
|
462
|
+
const r = hashDomainToScalar(options.context, prefix, msg);
|
|
463
|
+
const R = BASE.multiply(r).toBytes();
|
|
464
|
+
const k = hashDomainToScalar(options.context, R, pointBytes, msg);
|
|
465
|
+
const s = Fn2.create(r + k * scalar);
|
|
466
|
+
if (!Fn2.isValid(s))
|
|
467
|
+
throw new Error("sign failed: invalid s");
|
|
468
|
+
const rs = concatBytes2(R, Fn2.toBytes(s));
|
|
469
|
+
return abytes2(rs, lengths.signature, "result");
|
|
470
|
+
}
|
|
471
|
+
const verifyOpts = {
|
|
472
|
+
zip215: opts.zip215
|
|
473
|
+
};
|
|
474
|
+
function verify(sig, msg, publicKey, options = verifyOpts) {
|
|
475
|
+
const { context } = options;
|
|
476
|
+
const zip215 = options.zip215 === void 0 ? !!verifyOpts.zip215 : options.zip215;
|
|
477
|
+
const len = lengths.signature;
|
|
478
|
+
sig = abytes2(sig, len, "signature");
|
|
479
|
+
msg = abytes2(msg, void 0, "message");
|
|
480
|
+
publicKey = abytes2(publicKey, lengths.publicKey, "publicKey");
|
|
481
|
+
if (zip215 !== void 0)
|
|
482
|
+
abool(zip215, "zip215");
|
|
483
|
+
if (prehash)
|
|
484
|
+
msg = prehash(msg);
|
|
485
|
+
const mid = len / 2;
|
|
486
|
+
const r = sig.subarray(0, mid);
|
|
487
|
+
const s = bytesToNumberLE(sig.subarray(mid, len));
|
|
488
|
+
let A, R, SB;
|
|
489
|
+
try {
|
|
490
|
+
A = Point.fromBytes(publicKey, zip215);
|
|
491
|
+
R = Point.fromBytes(r, zip215);
|
|
492
|
+
SB = BASE.multiplyUnsafe(s);
|
|
493
|
+
} catch (error) {
|
|
494
|
+
return false;
|
|
495
|
+
}
|
|
496
|
+
if (!zip215 && A.isSmallOrder())
|
|
497
|
+
return false;
|
|
498
|
+
const k = hashDomainToScalar(context, r, publicKey, msg);
|
|
499
|
+
const RkA = R.add(A.multiplyUnsafe(k));
|
|
500
|
+
return RkA.subtract(SB).clearCofactor().is0();
|
|
501
|
+
}
|
|
502
|
+
const _size = Fp2.BYTES;
|
|
503
|
+
const lengths = {
|
|
504
|
+
secretKey: _size,
|
|
505
|
+
publicKey: _size,
|
|
506
|
+
signature: 2 * _size,
|
|
507
|
+
seed: _size
|
|
508
|
+
};
|
|
509
|
+
function randomSecretKey(seed) {
|
|
510
|
+
seed = seed === void 0 ? randomBytes2(lengths.seed) : seed;
|
|
511
|
+
return abytes2(seed, lengths.seed, "seed");
|
|
512
|
+
}
|
|
513
|
+
function isValidSecretKey(key) {
|
|
514
|
+
return isBytes(key) && key.length === lengths.secretKey;
|
|
515
|
+
}
|
|
516
|
+
function isValidPublicKey(key, zip215) {
|
|
517
|
+
try {
|
|
518
|
+
return !!Point.fromBytes(key, zip215 === void 0 ? verifyOpts.zip215 : zip215);
|
|
519
|
+
} catch (error) {
|
|
520
|
+
return false;
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
const utils = {
|
|
524
|
+
getExtendedPublicKey,
|
|
525
|
+
randomSecretKey,
|
|
526
|
+
isValidSecretKey,
|
|
527
|
+
isValidPublicKey,
|
|
528
|
+
/**
|
|
529
|
+
* Converts ed public key to x public key. Uses formula:
|
|
530
|
+
* - ed25519:
|
|
531
|
+
* - `(u, v) = ((1+y)/(1-y), sqrt(-486664)*u/x)`
|
|
532
|
+
* - `(x, y) = (sqrt(-486664)*u/v, (u-1)/(u+1))`
|
|
533
|
+
* - ed448:
|
|
534
|
+
* - `(u, v) = ((y-1)/(y+1), sqrt(156324)*u/x)`
|
|
535
|
+
* - `(x, y) = (sqrt(156324)*u/v, (1+u)/(1-u))`
|
|
536
|
+
*/
|
|
537
|
+
toMontgomery(publicKey) {
|
|
538
|
+
const { y } = Point.fromBytes(publicKey);
|
|
539
|
+
const size = lengths.publicKey;
|
|
540
|
+
const is25519 = size === 32;
|
|
541
|
+
if (!is25519 && size !== 57)
|
|
542
|
+
throw new Error("only defined for 25519 and 448");
|
|
543
|
+
const u = is25519 ? Fp2.div(_1n + y, _1n - y) : Fp2.div(y - _1n, y + _1n);
|
|
544
|
+
return Fp2.toBytes(u);
|
|
545
|
+
},
|
|
546
|
+
toMontgomerySecret(secretKey) {
|
|
547
|
+
const size = lengths.secretKey;
|
|
548
|
+
abytes2(secretKey, size);
|
|
549
|
+
const hashed = hash(secretKey.subarray(0, size));
|
|
550
|
+
return adjustScalarBytes2(hashed).subarray(0, size);
|
|
551
|
+
}
|
|
552
|
+
};
|
|
553
|
+
Object.freeze(lengths);
|
|
554
|
+
Object.freeze(utils);
|
|
555
|
+
return Object.freeze({
|
|
556
|
+
keygen: createKeygen(randomSecretKey, getPublicKey),
|
|
557
|
+
getPublicKey,
|
|
558
|
+
sign,
|
|
559
|
+
verify,
|
|
560
|
+
utils,
|
|
561
|
+
Point,
|
|
562
|
+
lengths
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
// ../../node_modules/.pnpm/@noble+curves@2.2.0/node_modules/@noble/curves/abstract/montgomery.js
|
|
567
|
+
var _0n2 = BigInt(0);
|
|
568
|
+
var _1n2 = BigInt(1);
|
|
569
|
+
var _2n2 = BigInt(2);
|
|
570
|
+
function validateOpts(curve) {
|
|
571
|
+
validateObject(curve, {
|
|
572
|
+
P: "bigint",
|
|
573
|
+
type: "string",
|
|
574
|
+
adjustScalarBytes: "function",
|
|
575
|
+
powPminus2: "function"
|
|
576
|
+
}, {
|
|
577
|
+
randomBytes: "function"
|
|
578
|
+
});
|
|
579
|
+
return Object.freeze({ ...curve });
|
|
580
|
+
}
|
|
581
|
+
function montgomery(curveDef) {
|
|
582
|
+
const CURVE = validateOpts(curveDef);
|
|
583
|
+
const { P, type, adjustScalarBytes: adjustScalarBytes2, powPminus2, randomBytes: rand } = CURVE;
|
|
584
|
+
const is25519 = type === "x25519";
|
|
585
|
+
if (!is25519 && type !== "x448")
|
|
586
|
+
throw new Error("invalid type");
|
|
587
|
+
const randomBytes_ = rand === void 0 ? randomBytes : rand;
|
|
588
|
+
const montgomeryBits = is25519 ? 255 : 448;
|
|
589
|
+
const fieldLen = is25519 ? 32 : 56;
|
|
590
|
+
const Gu = is25519 ? BigInt(9) : BigInt(5);
|
|
591
|
+
const a24 = is25519 ? BigInt(121665) : BigInt(39081);
|
|
592
|
+
const minScalar = is25519 ? _2n2 ** BigInt(254) : _2n2 ** BigInt(447);
|
|
593
|
+
const maxAdded = is25519 ? BigInt(8) * _2n2 ** BigInt(251) - _1n2 : BigInt(4) * _2n2 ** BigInt(445) - _1n2;
|
|
594
|
+
const maxScalar = minScalar + maxAdded + _1n2;
|
|
595
|
+
const modP = (n) => mod(n, P);
|
|
596
|
+
const GuBytes = encodeU(Gu);
|
|
597
|
+
function encodeU(u) {
|
|
598
|
+
return numberToBytesLE(modP(u), fieldLen);
|
|
599
|
+
}
|
|
600
|
+
function decodeU(u) {
|
|
601
|
+
const _u = copyBytes(abytes2(u, fieldLen, "uCoordinate"));
|
|
602
|
+
if (is25519)
|
|
603
|
+
_u[31] &= 127;
|
|
604
|
+
return modP(bytesToNumberLE(_u));
|
|
605
|
+
}
|
|
606
|
+
function decodeScalar(scalar) {
|
|
607
|
+
return bytesToNumberLE(adjustScalarBytes2(copyBytes(abytes2(scalar, fieldLen, "scalar"))));
|
|
608
|
+
}
|
|
609
|
+
function scalarMult(scalar, u) {
|
|
610
|
+
const pu = montgomeryLadder(decodeU(u), decodeScalar(scalar));
|
|
611
|
+
if (pu === _0n2)
|
|
612
|
+
throw new Error("invalid private or public key received");
|
|
613
|
+
return encodeU(pu);
|
|
614
|
+
}
|
|
615
|
+
function scalarMultBase(scalar) {
|
|
616
|
+
return scalarMult(scalar, GuBytes);
|
|
617
|
+
}
|
|
618
|
+
const getPublicKey = scalarMultBase;
|
|
619
|
+
const getSharedSecret = scalarMult;
|
|
620
|
+
function cswap(swap, x_2, x_3) {
|
|
621
|
+
const dummy = modP(swap * (x_2 - x_3));
|
|
622
|
+
x_2 = modP(x_2 - dummy);
|
|
623
|
+
x_3 = modP(x_3 + dummy);
|
|
624
|
+
return { x_2, x_3 };
|
|
625
|
+
}
|
|
626
|
+
function montgomeryLadder(u, scalar) {
|
|
627
|
+
aInRange("u", u, _0n2, P);
|
|
628
|
+
aInRange("scalar", scalar, minScalar, maxScalar);
|
|
629
|
+
const k = scalar;
|
|
630
|
+
const x_1 = u;
|
|
631
|
+
let x_2 = _1n2;
|
|
632
|
+
let z_2 = _0n2;
|
|
633
|
+
let x_3 = u;
|
|
634
|
+
let z_3 = _1n2;
|
|
635
|
+
let swap = _0n2;
|
|
636
|
+
for (let t = BigInt(montgomeryBits - 1); t >= _0n2; t--) {
|
|
637
|
+
const k_t = k >> t & _1n2;
|
|
638
|
+
swap ^= k_t;
|
|
639
|
+
({ x_2, x_3 } = cswap(swap, x_2, x_3));
|
|
640
|
+
({ x_2: z_2, x_3: z_3 } = cswap(swap, z_2, z_3));
|
|
641
|
+
swap = k_t;
|
|
642
|
+
const A = x_2 + z_2;
|
|
643
|
+
const AA = modP(A * A);
|
|
644
|
+
const B = x_2 - z_2;
|
|
645
|
+
const BB = modP(B * B);
|
|
646
|
+
const E = AA - BB;
|
|
647
|
+
const C = x_3 + z_3;
|
|
648
|
+
const D = x_3 - z_3;
|
|
649
|
+
const DA = modP(D * A);
|
|
650
|
+
const CB = modP(C * B);
|
|
651
|
+
const dacb = DA + CB;
|
|
652
|
+
const da_cb = DA - CB;
|
|
653
|
+
x_3 = modP(dacb * dacb);
|
|
654
|
+
z_3 = modP(x_1 * modP(da_cb * da_cb));
|
|
655
|
+
x_2 = modP(AA * BB);
|
|
656
|
+
z_2 = modP(E * (AA + modP(a24 * E)));
|
|
657
|
+
}
|
|
658
|
+
({ x_2, x_3 } = cswap(swap, x_2, x_3));
|
|
659
|
+
({ x_2: z_2, x_3: z_3 } = cswap(swap, z_2, z_3));
|
|
660
|
+
const z2 = powPminus2(z_2);
|
|
661
|
+
return modP(x_2 * z2);
|
|
662
|
+
}
|
|
663
|
+
const lengths = {
|
|
664
|
+
secretKey: fieldLen,
|
|
665
|
+
publicKey: fieldLen,
|
|
666
|
+
seed: fieldLen
|
|
667
|
+
};
|
|
668
|
+
const randomSecretKey = (seed) => {
|
|
669
|
+
seed = seed === void 0 ? randomBytes_(fieldLen) : seed;
|
|
670
|
+
abytes2(seed, lengths.seed, "seed");
|
|
671
|
+
return seed;
|
|
672
|
+
};
|
|
673
|
+
const utils = { randomSecretKey };
|
|
674
|
+
Object.freeze(lengths);
|
|
675
|
+
Object.freeze(utils);
|
|
676
|
+
return Object.freeze({
|
|
677
|
+
keygen: createKeygen(randomSecretKey, getPublicKey),
|
|
678
|
+
getSharedSecret,
|
|
679
|
+
getPublicKey,
|
|
680
|
+
scalarMult,
|
|
681
|
+
scalarMultBase,
|
|
682
|
+
utils,
|
|
683
|
+
GuBytes: GuBytes.slice(),
|
|
684
|
+
lengths
|
|
685
|
+
});
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
// ../../node_modules/.pnpm/@noble+curves@2.2.0/node_modules/@noble/curves/abstract/oprf.js
|
|
689
|
+
var _DST_scalarBytes = /* @__PURE__ */ asciiToBytes(_DST_scalar);
|
|
690
|
+
function createOPRF(opts) {
|
|
691
|
+
validateObject(opts, {
|
|
692
|
+
name: "string",
|
|
693
|
+
hash: "function",
|
|
694
|
+
hashToScalar: "function",
|
|
695
|
+
hashToGroup: "function"
|
|
696
|
+
});
|
|
697
|
+
validatePointCons(opts.Point);
|
|
698
|
+
const { name, Point, hash } = opts;
|
|
699
|
+
const { Fn: Fn2 } = Point;
|
|
700
|
+
const hashToGroup = (msg, ctx) => opts.hashToGroup(msg, {
|
|
701
|
+
DST: concatBytes2(asciiToBytes("HashToGroup-"), ctx)
|
|
702
|
+
});
|
|
703
|
+
const hashToScalarPrefixed = (msg, ctx) => opts.hashToScalar(msg, { DST: concatBytes2(_DST_scalarBytes, ctx) });
|
|
704
|
+
const randomScalar = (rng = randomBytes) => {
|
|
705
|
+
const t = mapHashToField(rng(getMinHashLength(Fn2.ORDER)), Fn2.ORDER, Fn2.isLE);
|
|
706
|
+
return Fn2.isLE ? bytesToNumberLE(t) : bytesToNumberBE(t);
|
|
707
|
+
};
|
|
708
|
+
const msm = (points, scalars) => pippenger(Point, points, scalars);
|
|
709
|
+
const getCtx = (mode) => concatBytes2(asciiToBytes("OPRFV1-"), new Uint8Array([mode]), asciiToBytes("-" + name));
|
|
710
|
+
const ctxOPRF = getCtx(0);
|
|
711
|
+
const ctxVOPRF = getCtx(1);
|
|
712
|
+
const ctxPOPRF = getCtx(2);
|
|
713
|
+
function encode(...args) {
|
|
714
|
+
const res2 = [];
|
|
715
|
+
for (const a of args) {
|
|
716
|
+
if (typeof a === "number")
|
|
717
|
+
res2.push(numberToBytesBE(a, 2));
|
|
718
|
+
else if (typeof a === "string")
|
|
719
|
+
res2.push(asciiToBytes(a));
|
|
720
|
+
else {
|
|
721
|
+
abytes2(a);
|
|
722
|
+
res2.push(numberToBytesBE(a.length, 2), a);
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
return concatBytes2(...res2);
|
|
726
|
+
}
|
|
727
|
+
const inputBytes = (title, bytes) => {
|
|
728
|
+
abytes2(bytes, void 0, title);
|
|
729
|
+
if (bytes.length > 65535)
|
|
730
|
+
throw new Error(`"${title}" expected Uint8Array of length <= 65535, got length=${bytes.length}`);
|
|
731
|
+
return bytes;
|
|
732
|
+
};
|
|
733
|
+
const hashInput = (...bytes) => hash(encode(...bytes, "Finalize"));
|
|
734
|
+
function getTranscripts(B, C, D, ctx) {
|
|
735
|
+
const Bm = B.toBytes();
|
|
736
|
+
const seed = hash(encode(Bm, concatBytes2(asciiToBytes("Seed-"), ctx)));
|
|
737
|
+
const res2 = [];
|
|
738
|
+
for (let i = 0; i < C.length; i++) {
|
|
739
|
+
const Ci = C[i].toBytes();
|
|
740
|
+
const Di = D[i].toBytes();
|
|
741
|
+
const di = hashToScalarPrefixed(encode(seed, i, Ci, Di, "Composite"), ctx);
|
|
742
|
+
res2.push(di);
|
|
743
|
+
}
|
|
744
|
+
return res2;
|
|
745
|
+
}
|
|
746
|
+
function computeComposites(B, C, D, ctx) {
|
|
747
|
+
const T = getTranscripts(B, C, D, ctx);
|
|
748
|
+
const M = msm(C, T);
|
|
749
|
+
const Z = msm(D, T);
|
|
750
|
+
return { M, Z };
|
|
751
|
+
}
|
|
752
|
+
function computeCompositesFast(k, B, C, D, ctx) {
|
|
753
|
+
const T = getTranscripts(B, C, D, ctx);
|
|
754
|
+
const M = msm(C, T);
|
|
755
|
+
const Z = M.multiply(k);
|
|
756
|
+
return { M, Z };
|
|
757
|
+
}
|
|
758
|
+
function challengeTranscript(B, M, Z, t2, t3, ctx) {
|
|
759
|
+
const [Bm, a0, a1, a2, a3] = [B, M, Z, t2, t3].map((i) => i.toBytes());
|
|
760
|
+
return hashToScalarPrefixed(encode(Bm, a0, a1, a2, a3, "Challenge"), ctx);
|
|
761
|
+
}
|
|
762
|
+
function generateProof(ctx, k, B, C, D, rng) {
|
|
763
|
+
const { M, Z } = computeCompositesFast(k, B, C, D, ctx);
|
|
764
|
+
const r = randomScalar(rng);
|
|
765
|
+
const t2 = Point.BASE.multiply(r);
|
|
766
|
+
const t3 = M.multiply(r);
|
|
767
|
+
const c = challengeTranscript(B, M, Z, t2, t3, ctx);
|
|
768
|
+
const s = Fn2.sub(r, Fn2.mul(c, k));
|
|
769
|
+
return concatBytes2(...[c, s].map((i) => Fn2.toBytes(i)));
|
|
770
|
+
}
|
|
771
|
+
function verifyProof(ctx, B, C, D, proof) {
|
|
772
|
+
abytes2(proof, 2 * Fn2.BYTES);
|
|
773
|
+
const { M, Z } = computeComposites(B, C, D, ctx);
|
|
774
|
+
const [c, s] = [proof.subarray(0, Fn2.BYTES), proof.subarray(Fn2.BYTES)].map((f) => Fn2.fromBytes(f));
|
|
775
|
+
const t2 = Point.BASE.multiply(s).add(B.multiply(c));
|
|
776
|
+
const t3 = M.multiply(s).add(Z.multiply(c));
|
|
777
|
+
const expectedC = challengeTranscript(B, M, Z, t2, t3, ctx);
|
|
778
|
+
if (!Fn2.eql(c, expectedC))
|
|
779
|
+
throw new Error("proof verification failed");
|
|
780
|
+
}
|
|
781
|
+
function generateKeyPair() {
|
|
782
|
+
const skS = randomScalar();
|
|
783
|
+
const pkS = Point.BASE.multiply(skS);
|
|
784
|
+
return { secretKey: Fn2.toBytes(skS), publicKey: pkS.toBytes() };
|
|
785
|
+
}
|
|
786
|
+
function deriveKeyPair(ctx, seed, info) {
|
|
787
|
+
abytes2(seed, 32, "seed");
|
|
788
|
+
info = inputBytes("keyInfo", info);
|
|
789
|
+
const dst = concatBytes2(asciiToBytes("DeriveKeyPair"), ctx);
|
|
790
|
+
const msg = concatBytes2(seed, encode(info), Uint8Array.of(0));
|
|
791
|
+
for (let counter = 0; counter <= 255; counter++) {
|
|
792
|
+
msg[msg.length - 1] = counter;
|
|
793
|
+
const skS = opts.hashToScalar(msg, { DST: dst });
|
|
794
|
+
if (Fn2.is0(skS))
|
|
795
|
+
continue;
|
|
796
|
+
return {
|
|
797
|
+
secretKey: Fn2.toBytes(skS),
|
|
798
|
+
publicKey: Point.BASE.multiply(skS).toBytes()
|
|
799
|
+
};
|
|
800
|
+
}
|
|
801
|
+
throw new Error("Cannot derive key");
|
|
802
|
+
}
|
|
803
|
+
const wirePoint = (label, bytes) => {
|
|
804
|
+
const point = Point.fromBytes(bytes);
|
|
805
|
+
if (point.equals(Point.ZERO))
|
|
806
|
+
throw new Error(label + " point at infinity");
|
|
807
|
+
return point;
|
|
808
|
+
};
|
|
809
|
+
function blind(ctx, input, rng = randomBytes) {
|
|
810
|
+
input = inputBytes("input", input);
|
|
811
|
+
const blind2 = randomScalar(rng);
|
|
812
|
+
const inputPoint = hashToGroup(input, ctx);
|
|
813
|
+
if (inputPoint.equals(Point.ZERO))
|
|
814
|
+
throw new Error("Input point at infinity");
|
|
815
|
+
const blinded = inputPoint.multiply(blind2);
|
|
816
|
+
return { blind: Fn2.toBytes(blind2), blinded: blinded.toBytes() };
|
|
817
|
+
}
|
|
818
|
+
function evaluate(ctx, secretKey, input) {
|
|
819
|
+
input = inputBytes("input", input);
|
|
820
|
+
const skS = Fn2.fromBytes(secretKey);
|
|
821
|
+
const inputPoint = hashToGroup(input, ctx);
|
|
822
|
+
if (inputPoint.equals(Point.ZERO))
|
|
823
|
+
throw new Error("Input point at infinity");
|
|
824
|
+
const unblinded = inputPoint.multiply(skS).toBytes();
|
|
825
|
+
return hashInput(input, unblinded);
|
|
826
|
+
}
|
|
827
|
+
const oprf = Object.freeze({
|
|
828
|
+
generateKeyPair,
|
|
829
|
+
deriveKeyPair: (seed, keyInfo) => deriveKeyPair(ctxOPRF, seed, keyInfo),
|
|
830
|
+
blind: (input, rng = randomBytes) => blind(ctxOPRF, input, rng),
|
|
831
|
+
blindEvaluate(secretKey, blindedPoint) {
|
|
832
|
+
const skS = Fn2.fromBytes(secretKey);
|
|
833
|
+
const elm = wirePoint("blinded", blindedPoint);
|
|
834
|
+
return elm.multiply(skS).toBytes();
|
|
835
|
+
},
|
|
836
|
+
finalize(input, blindBytes, evaluatedBytes) {
|
|
837
|
+
input = inputBytes("input", input);
|
|
838
|
+
const blind2 = Fn2.fromBytes(blindBytes);
|
|
839
|
+
const evalPoint = wirePoint("evaluated", evaluatedBytes);
|
|
840
|
+
const unblinded = evalPoint.multiply(Fn2.inv(blind2)).toBytes();
|
|
841
|
+
return hashInput(input, unblinded);
|
|
842
|
+
},
|
|
843
|
+
evaluate: (secretKey, input) => evaluate(ctxOPRF, secretKey, input)
|
|
844
|
+
});
|
|
845
|
+
const voprf = Object.freeze({
|
|
846
|
+
generateKeyPair,
|
|
847
|
+
deriveKeyPair: (seed, keyInfo) => deriveKeyPair(ctxVOPRF, seed, keyInfo),
|
|
848
|
+
blind: (input, rng = randomBytes) => blind(ctxVOPRF, input, rng),
|
|
849
|
+
blindEvaluateBatch(secretKey, publicKey, blinded, rng = randomBytes) {
|
|
850
|
+
if (!Array.isArray(blinded))
|
|
851
|
+
throw new Error("expected array");
|
|
852
|
+
const skS = Fn2.fromBytes(secretKey);
|
|
853
|
+
const pkS = wirePoint("public key", publicKey);
|
|
854
|
+
const blindedPoints = blinded.map((i) => wirePoint("blinded", i));
|
|
855
|
+
const evaluated = blindedPoints.map((i) => i.multiply(skS));
|
|
856
|
+
const proof = generateProof(ctxVOPRF, skS, pkS, blindedPoints, evaluated, rng);
|
|
857
|
+
return { evaluated: evaluated.map((i) => i.toBytes()), proof };
|
|
858
|
+
},
|
|
859
|
+
blindEvaluate(secretKey, publicKey, blinded, rng = randomBytes) {
|
|
860
|
+
const res2 = this.blindEvaluateBatch(secretKey, publicKey, [blinded], rng);
|
|
861
|
+
return { evaluated: res2.evaluated[0], proof: res2.proof };
|
|
862
|
+
},
|
|
863
|
+
finalizeBatch(items, publicKey, proof) {
|
|
864
|
+
if (!Array.isArray(items))
|
|
865
|
+
throw new Error("expected array");
|
|
866
|
+
const pkS = wirePoint("public key", publicKey);
|
|
867
|
+
const blindedPoints = items.map((i) => wirePoint("blinded", i.blinded));
|
|
868
|
+
const evalPoints = items.map((i) => wirePoint("evaluated", i.evaluated));
|
|
869
|
+
verifyProof(ctxVOPRF, pkS, blindedPoints, evalPoints, proof);
|
|
870
|
+
return items.map((i) => oprf.finalize(i.input, i.blind, i.evaluated));
|
|
871
|
+
},
|
|
872
|
+
finalize(input, blind2, evaluated, blinded, publicKey, proof) {
|
|
873
|
+
return this.finalizeBatch([{ input, blind: blind2, evaluated, blinded }], publicKey, proof)[0];
|
|
874
|
+
},
|
|
875
|
+
evaluate: (secretKey, input) => evaluate(ctxVOPRF, secretKey, input)
|
|
876
|
+
});
|
|
877
|
+
const poprf = (info) => {
|
|
878
|
+
info = inputBytes("info", info);
|
|
879
|
+
const m = hashToScalarPrefixed(encode("Info", info), ctxPOPRF);
|
|
880
|
+
const T = Point.BASE.multiply(m);
|
|
881
|
+
return Object.freeze({
|
|
882
|
+
generateKeyPair,
|
|
883
|
+
deriveKeyPair: (seed, keyInfo) => deriveKeyPair(ctxPOPRF, seed, keyInfo),
|
|
884
|
+
blind(input, publicKey, rng = randomBytes) {
|
|
885
|
+
input = inputBytes("input", input);
|
|
886
|
+
const pkS = wirePoint("public key", publicKey);
|
|
887
|
+
const tweakedKey = T.add(pkS);
|
|
888
|
+
if (tweakedKey.equals(Point.ZERO))
|
|
889
|
+
throw new Error("tweakedKey point at infinity");
|
|
890
|
+
const blind2 = randomScalar(rng);
|
|
891
|
+
const inputPoint = hashToGroup(input, ctxPOPRF);
|
|
892
|
+
if (inputPoint.equals(Point.ZERO))
|
|
893
|
+
throw new Error("Input point at infinity");
|
|
894
|
+
const blindedPoint = inputPoint.multiply(blind2);
|
|
895
|
+
return {
|
|
896
|
+
blind: Fn2.toBytes(blind2),
|
|
897
|
+
blinded: blindedPoint.toBytes(),
|
|
898
|
+
tweakedKey: tweakedKey.toBytes()
|
|
899
|
+
};
|
|
900
|
+
},
|
|
901
|
+
blindEvaluateBatch(secretKey, blinded, rng = randomBytes) {
|
|
902
|
+
if (!Array.isArray(blinded))
|
|
903
|
+
throw new Error("expected array");
|
|
904
|
+
const skS = Fn2.fromBytes(secretKey);
|
|
905
|
+
const t = Fn2.add(skS, m);
|
|
906
|
+
const invT = Fn2.inv(t);
|
|
907
|
+
const blindedPoints = blinded.map((i) => wirePoint("blinded", i));
|
|
908
|
+
const evalPoints = blindedPoints.map((i) => i.multiply(invT));
|
|
909
|
+
const tweakedKey = Point.BASE.multiply(t);
|
|
910
|
+
const proof = generateProof(ctxPOPRF, t, tweakedKey, evalPoints, blindedPoints, rng);
|
|
911
|
+
return { evaluated: evalPoints.map((i) => i.toBytes()), proof };
|
|
912
|
+
},
|
|
913
|
+
blindEvaluate(secretKey, blinded, rng = randomBytes) {
|
|
914
|
+
const res2 = this.blindEvaluateBatch(secretKey, [blinded], rng);
|
|
915
|
+
return { evaluated: res2.evaluated[0], proof: res2.proof };
|
|
916
|
+
},
|
|
917
|
+
finalizeBatch(items, proof, tweakedKey) {
|
|
918
|
+
if (!Array.isArray(items))
|
|
919
|
+
throw new Error("expected array");
|
|
920
|
+
const inputs = items.map((i) => inputBytes("input", i.input));
|
|
921
|
+
const evalPoints = items.map((i) => wirePoint("evaluated", i.evaluated));
|
|
922
|
+
verifyProof(ctxPOPRF, wirePoint("tweakedKey", tweakedKey), evalPoints, items.map((i) => wirePoint("blinded", i.blinded)), proof);
|
|
923
|
+
return items.map((i, j) => {
|
|
924
|
+
const blind2 = Fn2.fromBytes(i.blind);
|
|
925
|
+
const point = evalPoints[j].multiply(Fn2.inv(blind2)).toBytes();
|
|
926
|
+
return hashInput(inputs[j], info, point);
|
|
927
|
+
});
|
|
928
|
+
},
|
|
929
|
+
finalize(input, blind2, evaluated, blinded, proof, tweakedKey) {
|
|
930
|
+
return this.finalizeBatch([{ input, blind: blind2, evaluated, blinded }], proof, tweakedKey)[0];
|
|
931
|
+
},
|
|
932
|
+
evaluate(secretKey, input) {
|
|
933
|
+
input = inputBytes("input", input);
|
|
934
|
+
const skS = Fn2.fromBytes(secretKey);
|
|
935
|
+
const inputPoint = hashToGroup(input, ctxPOPRF);
|
|
936
|
+
if (inputPoint.equals(Point.ZERO))
|
|
937
|
+
throw new Error("Input point at infinity");
|
|
938
|
+
const t = Fn2.add(skS, m);
|
|
939
|
+
const invT = Fn2.inv(t);
|
|
940
|
+
const unblinded = inputPoint.multiply(invT).toBytes();
|
|
941
|
+
return hashInput(input, info, unblinded);
|
|
942
|
+
}
|
|
943
|
+
});
|
|
944
|
+
};
|
|
945
|
+
const res = { name, oprf, voprf, poprf, __tests: Object.freeze({ Fn: Fn2 }) };
|
|
946
|
+
return Object.freeze(res);
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
// ../../node_modules/.pnpm/@noble+curves@2.2.0/node_modules/@noble/curves/ed25519.js
|
|
950
|
+
var _0n3 = /* @__PURE__ */ BigInt(0);
|
|
951
|
+
var _1n3 = /* @__PURE__ */ BigInt(1);
|
|
952
|
+
var _2n3 = /* @__PURE__ */ BigInt(2);
|
|
953
|
+
var _3n = /* @__PURE__ */ BigInt(3);
|
|
954
|
+
var _5n = /* @__PURE__ */ BigInt(5);
|
|
955
|
+
var _8n2 = /* @__PURE__ */ BigInt(8);
|
|
956
|
+
var ed25519_CURVE_p = /* @__PURE__ */ BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed");
|
|
957
|
+
var ed25519_CURVE = /* @__PURE__ */ (() => ({
|
|
958
|
+
p: ed25519_CURVE_p,
|
|
959
|
+
n: BigInt("0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed"),
|
|
960
|
+
h: _8n2,
|
|
961
|
+
a: BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec"),
|
|
962
|
+
d: BigInt("0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3"),
|
|
963
|
+
Gx: BigInt("0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a"),
|
|
964
|
+
Gy: BigInt("0x6666666666666666666666666666666666666666666666666666666666666658")
|
|
965
|
+
}))();
|
|
966
|
+
function ed25519_pow_2_252_3(x) {
|
|
967
|
+
const _10n = BigInt(10), _20n = BigInt(20), _40n = BigInt(40), _80n = BigInt(80);
|
|
968
|
+
const P = ed25519_CURVE_p;
|
|
969
|
+
const x2 = x * x % P;
|
|
970
|
+
const b2 = x2 * x % P;
|
|
971
|
+
const b4 = pow2(b2, _2n3, P) * b2 % P;
|
|
972
|
+
const b5 = pow2(b4, _1n3, P) * x % P;
|
|
973
|
+
const b10 = pow2(b5, _5n, P) * b5 % P;
|
|
974
|
+
const b20 = pow2(b10, _10n, P) * b10 % P;
|
|
975
|
+
const b40 = pow2(b20, _20n, P) * b20 % P;
|
|
976
|
+
const b80 = pow2(b40, _40n, P) * b40 % P;
|
|
977
|
+
const b160 = pow2(b80, _80n, P) * b80 % P;
|
|
978
|
+
const b240 = pow2(b160, _80n, P) * b80 % P;
|
|
979
|
+
const b250 = pow2(b240, _10n, P) * b10 % P;
|
|
980
|
+
const pow_p_5_8 = pow2(b250, _2n3, P) * x % P;
|
|
981
|
+
return { pow_p_5_8, b2 };
|
|
982
|
+
}
|
|
983
|
+
function adjustScalarBytes(bytes) {
|
|
984
|
+
bytes[0] &= 248;
|
|
985
|
+
bytes[31] &= 127;
|
|
986
|
+
bytes[31] |= 64;
|
|
987
|
+
return bytes;
|
|
988
|
+
}
|
|
989
|
+
var ED25519_SQRT_M1 = /* @__PURE__ */ BigInt("19681161376707505956807079304988542015446066515923890162744021073123829784752");
|
|
990
|
+
function uvRatio(u, v) {
|
|
991
|
+
const P = ed25519_CURVE_p;
|
|
992
|
+
const v3 = mod(v * v * v, P);
|
|
993
|
+
const v7 = mod(v3 * v3 * v, P);
|
|
994
|
+
const pow = ed25519_pow_2_252_3(u * v7).pow_p_5_8;
|
|
995
|
+
let x = mod(u * v3 * pow, P);
|
|
996
|
+
const vx2 = mod(v * x * x, P);
|
|
997
|
+
const root1 = x;
|
|
998
|
+
const root2 = mod(x * ED25519_SQRT_M1, P);
|
|
999
|
+
const useRoot1 = vx2 === u;
|
|
1000
|
+
const useRoot2 = vx2 === mod(-u, P);
|
|
1001
|
+
const noRoot = vx2 === mod(-u * ED25519_SQRT_M1, P);
|
|
1002
|
+
if (useRoot1)
|
|
1003
|
+
x = root1;
|
|
1004
|
+
if (useRoot2 || noRoot)
|
|
1005
|
+
x = root2;
|
|
1006
|
+
if (isNegativeLE(x, P))
|
|
1007
|
+
x = mod(-x, P);
|
|
1008
|
+
return { isValid: useRoot1 || useRoot2, value: x };
|
|
1009
|
+
}
|
|
1010
|
+
var ed25519_Point = /* @__PURE__ */ edwards(ed25519_CURVE, { uvRatio });
|
|
1011
|
+
var Fp = /* @__PURE__ */ (() => ed25519_Point.Fp)();
|
|
1012
|
+
var Fn = /* @__PURE__ */ (() => ed25519_Point.Fn)();
|
|
1013
|
+
function ed25519_domain(data, ctx, phflag) {
|
|
1014
|
+
if (ctx.length > 255)
|
|
1015
|
+
throw new Error("Context is too big");
|
|
1016
|
+
return concatBytes(asciiToBytes("SigEd25519 no Ed25519 collisions"), new Uint8Array([phflag ? 1 : 0, ctx.length]), ctx, data);
|
|
1017
|
+
}
|
|
1018
|
+
function ed(opts) {
|
|
1019
|
+
return eddsa(ed25519_Point, sha512, Object.assign({ adjustScalarBytes, zip215: true }, opts));
|
|
1020
|
+
}
|
|
1021
|
+
var ed25519 = /* @__PURE__ */ ed({});
|
|
1022
|
+
var ed25519ctx = /* @__PURE__ */ ed({ domain: ed25519_domain });
|
|
1023
|
+
var ed25519ph = /* @__PURE__ */ ed({ domain: ed25519_domain, prehash: sha512 });
|
|
1024
|
+
var ed25519_FROST = /* @__PURE__ */ (() => createFROST({
|
|
1025
|
+
name: "FROST-ED25519-SHA512-v1",
|
|
1026
|
+
Point: ed25519_Point,
|
|
1027
|
+
validatePoint: (p) => {
|
|
1028
|
+
p.assertValidity();
|
|
1029
|
+
if (!p.isTorsionFree())
|
|
1030
|
+
throw new Error("bad point: not torsion-free");
|
|
1031
|
+
},
|
|
1032
|
+
hash: sha512,
|
|
1033
|
+
// RFC 9591 keeps H2 undecorated here for RFC 8032 compatibility. In createFROST(),
|
|
1034
|
+
// `H2: ''` becomes an empty DST prefix; the built-in hashToScalar fallback treats
|
|
1035
|
+
// that the same as omitted DST, even though custom hooks can still observe the empty bag.
|
|
1036
|
+
H2: ""
|
|
1037
|
+
}))();
|
|
1038
|
+
var x25519 = /* @__PURE__ */ (() => {
|
|
1039
|
+
const P = ed25519_CURVE_p;
|
|
1040
|
+
return montgomery({
|
|
1041
|
+
P,
|
|
1042
|
+
type: "x25519",
|
|
1043
|
+
powPminus2: (x) => {
|
|
1044
|
+
const { pow_p_5_8, b2 } = ed25519_pow_2_252_3(x);
|
|
1045
|
+
return mod(pow2(pow_p_5_8, _3n, P) * b2, P);
|
|
1046
|
+
},
|
|
1047
|
+
adjustScalarBytes
|
|
1048
|
+
});
|
|
1049
|
+
})();
|
|
1050
|
+
var ELL2_C1 = /* @__PURE__ */ (() => (ed25519_CURVE_p + _3n) / _8n2)();
|
|
1051
|
+
var ELL2_C2 = /* @__PURE__ */ (() => Fp.pow(_2n3, ELL2_C1))();
|
|
1052
|
+
var ELL2_C3 = /* @__PURE__ */ (() => Fp.sqrt(Fp.neg(Fp.ONE)))();
|
|
1053
|
+
function _map_to_curve_elligator2_curve25519(u) {
|
|
1054
|
+
const ELL2_C4 = (ed25519_CURVE_p - _5n) / _8n2;
|
|
1055
|
+
const ELL2_J = BigInt(486662);
|
|
1056
|
+
let tv1 = Fp.sqr(u);
|
|
1057
|
+
tv1 = Fp.mul(tv1, _2n3);
|
|
1058
|
+
let xd = Fp.add(tv1, Fp.ONE);
|
|
1059
|
+
let x1n = Fp.neg(ELL2_J);
|
|
1060
|
+
let tv2 = Fp.sqr(xd);
|
|
1061
|
+
let gxd = Fp.mul(tv2, xd);
|
|
1062
|
+
let gx1 = Fp.mul(tv1, ELL2_J);
|
|
1063
|
+
gx1 = Fp.mul(gx1, x1n);
|
|
1064
|
+
gx1 = Fp.add(gx1, tv2);
|
|
1065
|
+
gx1 = Fp.mul(gx1, x1n);
|
|
1066
|
+
let tv3 = Fp.sqr(gxd);
|
|
1067
|
+
tv2 = Fp.sqr(tv3);
|
|
1068
|
+
tv3 = Fp.mul(tv3, gxd);
|
|
1069
|
+
tv3 = Fp.mul(tv3, gx1);
|
|
1070
|
+
tv2 = Fp.mul(tv2, tv3);
|
|
1071
|
+
let y11 = Fp.pow(tv2, ELL2_C4);
|
|
1072
|
+
y11 = Fp.mul(y11, tv3);
|
|
1073
|
+
let y12 = Fp.mul(y11, ELL2_C3);
|
|
1074
|
+
tv2 = Fp.sqr(y11);
|
|
1075
|
+
tv2 = Fp.mul(tv2, gxd);
|
|
1076
|
+
let e1 = Fp.eql(tv2, gx1);
|
|
1077
|
+
let y1 = Fp.cmov(y12, y11, e1);
|
|
1078
|
+
let x2n = Fp.mul(x1n, tv1);
|
|
1079
|
+
let y21 = Fp.mul(y11, u);
|
|
1080
|
+
y21 = Fp.mul(y21, ELL2_C2);
|
|
1081
|
+
let y22 = Fp.mul(y21, ELL2_C3);
|
|
1082
|
+
let gx2 = Fp.mul(gx1, tv1);
|
|
1083
|
+
tv2 = Fp.sqr(y21);
|
|
1084
|
+
tv2 = Fp.mul(tv2, gxd);
|
|
1085
|
+
let e2 = Fp.eql(tv2, gx2);
|
|
1086
|
+
let y2 = Fp.cmov(y22, y21, e2);
|
|
1087
|
+
tv2 = Fp.sqr(y1);
|
|
1088
|
+
tv2 = Fp.mul(tv2, gxd);
|
|
1089
|
+
let e3 = Fp.eql(tv2, gx1);
|
|
1090
|
+
let xn = Fp.cmov(x2n, x1n, e3);
|
|
1091
|
+
let y = Fp.cmov(y2, y1, e3);
|
|
1092
|
+
let e4 = Fp.isOdd(y);
|
|
1093
|
+
y = Fp.cmov(y, Fp.neg(y), e3 !== e4);
|
|
1094
|
+
return { xMn: xn, xMd: xd, yMn: y, yMd: _1n3 };
|
|
1095
|
+
}
|
|
1096
|
+
var ELL2_C1_EDWARDS = /* @__PURE__ */ (() => FpSqrtEven(Fp, Fp.neg(BigInt(486664))))();
|
|
1097
|
+
function map_to_curve_elligator2_edwards25519(u) {
|
|
1098
|
+
const { xMn, xMd, yMn, yMd } = _map_to_curve_elligator2_curve25519(u);
|
|
1099
|
+
let xn = Fp.mul(xMn, yMd);
|
|
1100
|
+
xn = Fp.mul(xn, ELL2_C1_EDWARDS);
|
|
1101
|
+
let xd = Fp.mul(xMd, yMn);
|
|
1102
|
+
let yn = Fp.sub(xMn, xMd);
|
|
1103
|
+
let yd = Fp.add(xMn, xMd);
|
|
1104
|
+
let tv1 = Fp.mul(xd, yd);
|
|
1105
|
+
let e = Fp.eql(tv1, Fp.ZERO);
|
|
1106
|
+
xn = Fp.cmov(xn, Fp.ZERO, e);
|
|
1107
|
+
xd = Fp.cmov(xd, Fp.ONE, e);
|
|
1108
|
+
yn = Fp.cmov(yn, Fp.ONE, e);
|
|
1109
|
+
yd = Fp.cmov(yd, Fp.ONE, e);
|
|
1110
|
+
const [xd_inv, yd_inv] = FpInvertBatch(Fp, [xd, yd], true);
|
|
1111
|
+
return { x: Fp.mul(xn, xd_inv), y: Fp.mul(yn, yd_inv) };
|
|
1112
|
+
}
|
|
1113
|
+
var ed25519_hasher = /* @__PURE__ */ (() => createHasher(ed25519_Point, (scalars) => map_to_curve_elligator2_edwards25519(scalars[0]), {
|
|
1114
|
+
DST: "edwards25519_XMD:SHA-512_ELL2_RO_",
|
|
1115
|
+
encodeDST: "edwards25519_XMD:SHA-512_ELL2_NU_",
|
|
1116
|
+
p: ed25519_CURVE_p,
|
|
1117
|
+
m: 1,
|
|
1118
|
+
k: 128,
|
|
1119
|
+
expand: "xmd",
|
|
1120
|
+
hash: sha512
|
|
1121
|
+
}))();
|
|
1122
|
+
var SQRT_M1 = ED25519_SQRT_M1;
|
|
1123
|
+
var SQRT_AD_MINUS_ONE = /* @__PURE__ */ BigInt("25063068953384623474111414158702152701244531502492656460079210482610430750235");
|
|
1124
|
+
var INVSQRT_A_MINUS_D = /* @__PURE__ */ BigInt("54469307008909316920995813868745141605393597292927456921205312896311721017578");
|
|
1125
|
+
var ONE_MINUS_D_SQ = /* @__PURE__ */ BigInt("1159843021668779879193775521855586647937357759715417654439879720876111806838");
|
|
1126
|
+
var D_MINUS_ONE_SQ = /* @__PURE__ */ BigInt("40440834346308536858101042469323190826248399146238708352240133220865137265952");
|
|
1127
|
+
var invertSqrt = (number) => uvRatio(_1n3, number);
|
|
1128
|
+
var MAX_255B = /* @__PURE__ */ BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
1129
|
+
var bytes255ToNumberLE = (bytes) => Fp.create(bytesToNumberLE(bytes) & MAX_255B);
|
|
1130
|
+
function calcElligatorRistrettoMap(r0) {
|
|
1131
|
+
const { d } = ed25519_CURVE;
|
|
1132
|
+
const P = ed25519_CURVE_p;
|
|
1133
|
+
const mod2 = (n) => Fp.create(n);
|
|
1134
|
+
const r = mod2(SQRT_M1 * r0 * r0);
|
|
1135
|
+
const Ns = mod2((r + _1n3) * ONE_MINUS_D_SQ);
|
|
1136
|
+
let c = BigInt(-1);
|
|
1137
|
+
const D = mod2((c - d * r) * mod2(r + d));
|
|
1138
|
+
let { isValid: Ns_D_is_sq, value: s } = uvRatio(Ns, D);
|
|
1139
|
+
let s_ = mod2(s * r0);
|
|
1140
|
+
if (!isNegativeLE(s_, P))
|
|
1141
|
+
s_ = mod2(-s_);
|
|
1142
|
+
if (!Ns_D_is_sq)
|
|
1143
|
+
s = s_;
|
|
1144
|
+
if (!Ns_D_is_sq)
|
|
1145
|
+
c = r;
|
|
1146
|
+
const Nt = mod2(c * (r - _1n3) * D_MINUS_ONE_SQ - D);
|
|
1147
|
+
const s2 = s * s;
|
|
1148
|
+
const W0 = mod2((s + s) * D);
|
|
1149
|
+
const W1 = mod2(Nt * SQRT_AD_MINUS_ONE);
|
|
1150
|
+
const W2 = mod2(_1n3 - s2);
|
|
1151
|
+
const W3 = mod2(_1n3 + s2);
|
|
1152
|
+
return new ed25519_Point(mod2(W0 * W3), mod2(W2 * W1), mod2(W1 * W3), mod2(W0 * W2));
|
|
1153
|
+
}
|
|
1154
|
+
var _RistrettoPoint = class __RistrettoPoint extends PrimeEdwardsPoint {
|
|
1155
|
+
// Do NOT change syntax: the following gymnastics is done,
|
|
1156
|
+
// because typescript strips comments, which makes bundlers disable tree-shaking.
|
|
1157
|
+
// prettier-ignore
|
|
1158
|
+
static BASE = /* @__PURE__ */ (() => new __RistrettoPoint(ed25519_Point.BASE))();
|
|
1159
|
+
// prettier-ignore
|
|
1160
|
+
static ZERO = /* @__PURE__ */ (() => new __RistrettoPoint(ed25519_Point.ZERO))();
|
|
1161
|
+
// prettier-ignore
|
|
1162
|
+
static Fp = /* @__PURE__ */ (() => Fp)();
|
|
1163
|
+
// prettier-ignore
|
|
1164
|
+
static Fn = /* @__PURE__ */ (() => Fn)();
|
|
1165
|
+
constructor(ep) {
|
|
1166
|
+
super(ep);
|
|
1167
|
+
}
|
|
1168
|
+
/**
|
|
1169
|
+
* Create one Ristretto255 point from affine Edwards coordinates.
|
|
1170
|
+
* This wraps the internal Edwards representative directly and is not a
|
|
1171
|
+
* canonical ristretto255 decoding path.
|
|
1172
|
+
* Use `toBytes()` / `fromBytes()` if canonical ristretto255 bytes matter.
|
|
1173
|
+
*/
|
|
1174
|
+
static fromAffine(ap) {
|
|
1175
|
+
return new __RistrettoPoint(ed25519_Point.fromAffine(ap));
|
|
1176
|
+
}
|
|
1177
|
+
assertSame(other) {
|
|
1178
|
+
if (!(other instanceof __RistrettoPoint))
|
|
1179
|
+
throw new Error("RistrettoPoint expected");
|
|
1180
|
+
}
|
|
1181
|
+
init(ep) {
|
|
1182
|
+
return new __RistrettoPoint(ep);
|
|
1183
|
+
}
|
|
1184
|
+
static fromBytes(bytes) {
|
|
1185
|
+
abytes(bytes, 32);
|
|
1186
|
+
const { a, d } = ed25519_CURVE;
|
|
1187
|
+
const P = ed25519_CURVE_p;
|
|
1188
|
+
const mod2 = (n) => Fp.create(n);
|
|
1189
|
+
const s = bytes255ToNumberLE(bytes);
|
|
1190
|
+
if (!equalBytes(Fp.toBytes(s), bytes) || isNegativeLE(s, P))
|
|
1191
|
+
throw new Error("invalid ristretto255 encoding 1");
|
|
1192
|
+
const s2 = mod2(s * s);
|
|
1193
|
+
const u1 = mod2(_1n3 + a * s2);
|
|
1194
|
+
const u2 = mod2(_1n3 - a * s2);
|
|
1195
|
+
const u1_2 = mod2(u1 * u1);
|
|
1196
|
+
const u2_2 = mod2(u2 * u2);
|
|
1197
|
+
const v = mod2(a * d * u1_2 - u2_2);
|
|
1198
|
+
const { isValid, value: I } = invertSqrt(mod2(v * u2_2));
|
|
1199
|
+
const Dx = mod2(I * u2);
|
|
1200
|
+
const Dy = mod2(I * Dx * v);
|
|
1201
|
+
let x = mod2((s + s) * Dx);
|
|
1202
|
+
if (isNegativeLE(x, P))
|
|
1203
|
+
x = mod2(-x);
|
|
1204
|
+
const y = mod2(u1 * Dy);
|
|
1205
|
+
const t = mod2(x * y);
|
|
1206
|
+
if (!isValid || isNegativeLE(t, P) || y === _0n3)
|
|
1207
|
+
throw new Error("invalid ristretto255 encoding 2");
|
|
1208
|
+
return new __RistrettoPoint(new ed25519_Point(x, y, _1n3, t));
|
|
1209
|
+
}
|
|
1210
|
+
/**
|
|
1211
|
+
* Converts ristretto-encoded string to ristretto point.
|
|
1212
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode).
|
|
1213
|
+
* @param hex - Ristretto-encoded 32 bytes. Not every 32-byte string is valid ristretto encoding
|
|
1214
|
+
*/
|
|
1215
|
+
static fromHex(hex) {
|
|
1216
|
+
return __RistrettoPoint.fromBytes(hexToBytes(hex));
|
|
1217
|
+
}
|
|
1218
|
+
/**
|
|
1219
|
+
* Encodes ristretto point to Uint8Array.
|
|
1220
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-encode).
|
|
1221
|
+
*/
|
|
1222
|
+
toBytes() {
|
|
1223
|
+
let { X, Y, Z, T } = this.ep;
|
|
1224
|
+
const P = ed25519_CURVE_p;
|
|
1225
|
+
const mod2 = (n) => Fp.create(n);
|
|
1226
|
+
const u1 = mod2(mod2(Z + Y) * mod2(Z - Y));
|
|
1227
|
+
const u2 = mod2(X * Y);
|
|
1228
|
+
const u2sq = mod2(u2 * u2);
|
|
1229
|
+
const { value: invsqrt } = invertSqrt(mod2(u1 * u2sq));
|
|
1230
|
+
const D1 = mod2(invsqrt * u1);
|
|
1231
|
+
const D2 = mod2(invsqrt * u2);
|
|
1232
|
+
const zInv = mod2(D1 * D2 * T);
|
|
1233
|
+
let D;
|
|
1234
|
+
if (isNegativeLE(T * zInv, P)) {
|
|
1235
|
+
let _x = mod2(Y * SQRT_M1);
|
|
1236
|
+
let _y = mod2(X * SQRT_M1);
|
|
1237
|
+
X = _x;
|
|
1238
|
+
Y = _y;
|
|
1239
|
+
D = mod2(D1 * INVSQRT_A_MINUS_D);
|
|
1240
|
+
} else {
|
|
1241
|
+
D = D2;
|
|
1242
|
+
}
|
|
1243
|
+
if (isNegativeLE(X * zInv, P))
|
|
1244
|
+
Y = mod2(-Y);
|
|
1245
|
+
let s = mod2((Z - Y) * D);
|
|
1246
|
+
if (isNegativeLE(s, P))
|
|
1247
|
+
s = mod2(-s);
|
|
1248
|
+
return Fp.toBytes(s);
|
|
1249
|
+
}
|
|
1250
|
+
/**
|
|
1251
|
+
* Compares two Ristretto points.
|
|
1252
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-equals).
|
|
1253
|
+
*/
|
|
1254
|
+
equals(other) {
|
|
1255
|
+
this.assertSame(other);
|
|
1256
|
+
const { X: X1, Y: Y1 } = this.ep;
|
|
1257
|
+
const { X: X2, Y: Y2 } = other.ep;
|
|
1258
|
+
const mod2 = (n) => Fp.create(n);
|
|
1259
|
+
const one = mod2(X1 * Y2) === mod2(Y1 * X2);
|
|
1260
|
+
const two = mod2(Y1 * Y2) === mod2(X1 * X2);
|
|
1261
|
+
return one || two;
|
|
1262
|
+
}
|
|
1263
|
+
is0() {
|
|
1264
|
+
return this.equals(__RistrettoPoint.ZERO);
|
|
1265
|
+
}
|
|
1266
|
+
};
|
|
1267
|
+
Object.freeze(_RistrettoPoint.BASE);
|
|
1268
|
+
Object.freeze(_RistrettoPoint.ZERO);
|
|
1269
|
+
Object.freeze(_RistrettoPoint.prototype);
|
|
1270
|
+
Object.freeze(_RistrettoPoint);
|
|
1271
|
+
var ristretto255 = /* @__PURE__ */ Object.freeze({ Point: _RistrettoPoint });
|
|
1272
|
+
var ristretto255_hasher = Object.freeze({
|
|
1273
|
+
Point: _RistrettoPoint,
|
|
1274
|
+
/**
|
|
1275
|
+
* Spec: https://www.rfc-editor.org/rfc/rfc9380.html#name-hashing-to-ristretto255. Caveats:
|
|
1276
|
+
* * There are no test vectors
|
|
1277
|
+
* * encodeToCurve / mapToCurve is undefined
|
|
1278
|
+
* * mapToCurve would be `calcElligatorRistrettoMap(scalars[0])`, not ristretto255_map!
|
|
1279
|
+
* * hashToScalar is undefined too, so we just use OPRF implementation
|
|
1280
|
+
* * We cannot re-use 'createHasher', because ristretto255_map is different algorithm/RFC
|
|
1281
|
+
(os2ip -> bytes255ToNumberLE)
|
|
1282
|
+
* * mapToCurve == calcElligatorRistrettoMap, hashToCurve == ristretto255_map
|
|
1283
|
+
* * hashToScalar is undefined in RFC9380 for ristretto, so we use the OPRF
|
|
1284
|
+
version here. Using `bytes255ToNumblerLE` will create a different result
|
|
1285
|
+
if we use `bytes255ToNumberLE` as os2ip
|
|
1286
|
+
* * current version is closest to spec.
|
|
1287
|
+
*/
|
|
1288
|
+
hashToCurve(msg, options) {
|
|
1289
|
+
const DST = options?.DST === void 0 ? "ristretto255_XMD:SHA-512_R255MAP_RO_" : options.DST;
|
|
1290
|
+
const xmd = expand_message_xmd(msg, DST, 64, sha512);
|
|
1291
|
+
return ristretto255_hasher.deriveToCurve(xmd);
|
|
1292
|
+
},
|
|
1293
|
+
hashToScalar(msg, options = { DST: _DST_scalar }) {
|
|
1294
|
+
const xmd = expand_message_xmd(msg, options.DST, 64, sha512);
|
|
1295
|
+
return Fn.create(bytesToNumberLE(xmd));
|
|
1296
|
+
},
|
|
1297
|
+
/**
|
|
1298
|
+
* HashToCurve-like construction based on RFC 9496 (Element Derivation).
|
|
1299
|
+
* Converts 64 uniform random bytes into a curve point.
|
|
1300
|
+
*
|
|
1301
|
+
* WARNING: This represents an older hash-to-curve construction from before
|
|
1302
|
+
* RFC 9380 was finalized.
|
|
1303
|
+
* It was later reused as a component in the newer
|
|
1304
|
+
* `hash_to_ristretto255` function defined in RFC 9380.
|
|
1305
|
+
*/
|
|
1306
|
+
deriveToCurve(bytes) {
|
|
1307
|
+
abytes(bytes, 64);
|
|
1308
|
+
const r1 = bytes255ToNumberLE(bytes.subarray(0, 32));
|
|
1309
|
+
const R1 = calcElligatorRistrettoMap(r1);
|
|
1310
|
+
const r2 = bytes255ToNumberLE(bytes.subarray(32, 64));
|
|
1311
|
+
const R2 = calcElligatorRistrettoMap(r2);
|
|
1312
|
+
return new _RistrettoPoint(R1.add(R2));
|
|
1313
|
+
}
|
|
1314
|
+
});
|
|
1315
|
+
var ristretto255_oprf = /* @__PURE__ */ (() => createOPRF({
|
|
1316
|
+
name: "ristretto255-SHA512",
|
|
1317
|
+
Point: _RistrettoPoint,
|
|
1318
|
+
hash: sha512,
|
|
1319
|
+
hashToGroup: ristretto255_hasher.hashToCurve,
|
|
1320
|
+
hashToScalar: ristretto255_hasher.hashToScalar
|
|
1321
|
+
}))();
|
|
1322
|
+
var ristretto255_FROST = /* @__PURE__ */ (() => createFROST({
|
|
1323
|
+
name: "FROST-RISTRETTO255-SHA512-v1",
|
|
1324
|
+
Point: _RistrettoPoint,
|
|
1325
|
+
validatePoint: (p) => {
|
|
1326
|
+
p.assertValidity();
|
|
1327
|
+
},
|
|
1328
|
+
hash: sha512
|
|
1329
|
+
}))();
|
|
1330
|
+
var ED25519_TORSION_SUBGROUP = /* @__PURE__ */ Object.freeze([
|
|
1331
|
+
"0100000000000000000000000000000000000000000000000000000000000000",
|
|
1332
|
+
"c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac037a",
|
|
1333
|
+
"0000000000000000000000000000000000000000000000000000000000000080",
|
|
1334
|
+
"26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc05",
|
|
1335
|
+
"ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f",
|
|
1336
|
+
"26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc85",
|
|
1337
|
+
"0000000000000000000000000000000000000000000000000000000000000000",
|
|
1338
|
+
"c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac03fa"
|
|
1339
|
+
]);
|
|
1340
|
+
|
|
1341
|
+
export {
|
|
1342
|
+
ed25519,
|
|
1343
|
+
ed25519ctx,
|
|
1344
|
+
ed25519ph,
|
|
1345
|
+
ed25519_FROST,
|
|
1346
|
+
x25519,
|
|
1347
|
+
_map_to_curve_elligator2_curve25519,
|
|
1348
|
+
ed25519_hasher,
|
|
1349
|
+
ristretto255,
|
|
1350
|
+
ristretto255_hasher,
|
|
1351
|
+
ristretto255_oprf,
|
|
1352
|
+
ristretto255_FROST,
|
|
1353
|
+
ED25519_TORSION_SUBGROUP
|
|
1354
|
+
};
|
|
1355
|
+
/*! Bundled license information:
|
|
1356
|
+
|
|
1357
|
+
@noble/curves/abstract/edwards.js:
|
|
1358
|
+
@noble/curves/abstract/montgomery.js:
|
|
1359
|
+
@noble/curves/abstract/oprf.js:
|
|
1360
|
+
@noble/curves/ed25519.js:
|
|
1361
|
+
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
1362
|
+
*/
|
|
1363
|
+
//# sourceMappingURL=chunk-V7D5HJBT.js.map
|