@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.
- package/LICENSE +21 -0
- package/README.md +75 -0
- package/abstract/bls.d.ts +410 -0
- package/abstract/bls.js +433 -0
- package/abstract/curve.d.ts +441 -0
- package/abstract/curve.js +622 -0
- package/abstract/edwards.d.ts +319 -0
- package/abstract/edwards.js +697 -0
- package/abstract/fft.d.ts +438 -0
- package/abstract/fft.js +580 -0
- package/abstract/frost.d.ts +292 -0
- package/abstract/frost.js +705 -0
- package/abstract/hash-to-curve.d.ts +238 -0
- package/abstract/hash-to-curve.js +353 -0
- package/abstract/modular.d.ts +550 -0
- package/abstract/modular.js +872 -0
- package/abstract/montgomery.d.ts +95 -0
- package/abstract/montgomery.js +180 -0
- package/abstract/oprf.d.ts +354 -0
- package/abstract/oprf.js +358 -0
- package/abstract/poseidon.d.ts +198 -0
- package/abstract/poseidon.js +471 -0
- package/abstract/tower.d.ts +213 -0
- package/abstract/tower.js +963 -0
- package/abstract/weierstrass.d.ts +587 -0
- package/abstract/weierstrass.js +1421 -0
- package/bls12-381.d.ts +23 -0
- package/bls12-381.js +680 -0
- package/bn254.d.ts +74 -0
- package/bn254.js +248 -0
- package/ed25519.d.ts +211 -0
- package/ed25519.js +626 -0
- package/ed448.d.ts +184 -0
- package/ed448.js +596 -0
- package/esm/abstract/bls.js +429 -0
- package/esm/abstract/curve.js +610 -0
- package/esm/abstract/edwards.js +691 -0
- package/esm/abstract/fft.js +567 -0
- package/esm/abstract/frost.js +702 -0
- package/esm/abstract/hash-to-curve.js +345 -0
- package/esm/abstract/modular.js +849 -0
- package/esm/abstract/montgomery.js +177 -0
- package/esm/abstract/oprf.js +355 -0
- package/esm/abstract/poseidon.js +462 -0
- package/esm/abstract/tower.js +925 -0
- package/esm/abstract/weierstrass.js +1411 -0
- package/esm/bls12-381.js +677 -0
- package/esm/bn254.js +244 -0
- package/esm/ed25519.js +622 -0
- package/esm/ed448.js +593 -0
- package/esm/index.js +38 -0
- package/esm/misc.js +211 -0
- package/esm/nist.js +266 -0
- package/esm/secp256k1.js +465 -0
- package/esm/utils.js +597 -0
- package/esm/webcrypto.js +362 -0
- package/index.d.ts +0 -0
- package/index.js +38 -0
- package/misc.d.ts +113 -0
- package/misc.js +216 -0
- package/nist.d.ts +136 -0
- package/nist.js +269 -0
- package/package.json +46 -0
- package/secp256k1.d.ts +152 -0
- package/secp256k1.js +468 -0
- package/utils.d.ts +535 -0
- package/utils.js +626 -0
- package/webcrypto.d.ts +240 -0
- package/webcrypto.js +365 -0
package/esm/secp256k1.js
ADDED
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SECG secp256k1. See [pdf](https://www.secg.org/sec2-v2.pdf).
|
|
3
|
+
*
|
|
4
|
+
* Belongs to Koblitz curves: it has efficiently-computable GLV endomorphism ψ,
|
|
5
|
+
* check out {@link EndomorphismOpts}. Seems to be rigid (not backdoored).
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
9
|
+
import { sha256 } from '@decryption/hashes/sha2';
|
|
10
|
+
import { randomBytes } from '@decryption/hashes/utils';
|
|
11
|
+
import { createKeygen } from './abstract/curve';
|
|
12
|
+
import { createFROST, } from './abstract/frost';
|
|
13
|
+
import { createHasher, isogenyMap } from './abstract/hash-to-curve';
|
|
14
|
+
import { Field, mapHashToField, pow2 } from './abstract/modular';
|
|
15
|
+
import { ecdsa, mapToCurveSimpleSWU, weierstrass, } from './abstract/weierstrass';
|
|
16
|
+
import { abytes, asciiToBytes, bytesToNumberBE, concatBytes, } from './utils';
|
|
17
|
+
// Seems like generator was produced from some seed:
|
|
18
|
+
// `Pointk1.BASE.multiply(Pointk1.Fn.inv(2n, N)).toAffine().x`
|
|
19
|
+
// // gives short x 0x3b78ce563f89a0ed9414f5aa28ad0d96d6795f9c63n
|
|
20
|
+
const secp256k1_CURVE = {
|
|
21
|
+
p: BigInt('0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f'),
|
|
22
|
+
n: BigInt('0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141'),
|
|
23
|
+
h: BigInt(1),
|
|
24
|
+
a: BigInt(0),
|
|
25
|
+
b: BigInt(7),
|
|
26
|
+
Gx: BigInt('0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'),
|
|
27
|
+
Gy: BigInt('0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8'),
|
|
28
|
+
};
|
|
29
|
+
const secp256k1_ENDO = {
|
|
30
|
+
beta: BigInt('0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee'),
|
|
31
|
+
basises: [
|
|
32
|
+
[BigInt('0x3086d221a7d46bcde86c90e49284eb15'), -BigInt('0xe4437ed6010e88286f547fa90abfe4c3')],
|
|
33
|
+
[BigInt('0x114ca50f7a8e2f3f657c1108d9d44cfd8'), BigInt('0x3086d221a7d46bcde86c90e49284eb15')],
|
|
34
|
+
],
|
|
35
|
+
};
|
|
36
|
+
const _0n = /* @__PURE__ */ BigInt(0);
|
|
37
|
+
const _2n = /* @__PURE__ */ BigInt(2);
|
|
38
|
+
/**
|
|
39
|
+
* √n = n^((p+1)/4) for fields p = 3 mod 4. We unwrap the loop and multiply bit-by-bit.
|
|
40
|
+
* (P+1n/4n).toString(2) would produce bits [223x 1, 0, 22x 1, 4x 0, 11, 00]
|
|
41
|
+
*/
|
|
42
|
+
function sqrtMod(y) {
|
|
43
|
+
const P = secp256k1_CURVE.p;
|
|
44
|
+
// prettier-ignore
|
|
45
|
+
const _3n = BigInt(3), _6n = BigInt(6), _11n = BigInt(11), _22n = BigInt(22);
|
|
46
|
+
// prettier-ignore
|
|
47
|
+
const _23n = BigInt(23), _44n = BigInt(44), _88n = BigInt(88);
|
|
48
|
+
const b2 = (y * y * y) % P; // x^3, 11
|
|
49
|
+
const b3 = (b2 * b2 * y) % P; // x^7
|
|
50
|
+
const b6 = (pow2(b3, _3n, P) * b3) % P;
|
|
51
|
+
const b9 = (pow2(b6, _3n, P) * b3) % P;
|
|
52
|
+
const b11 = (pow2(b9, _2n, P) * b2) % P;
|
|
53
|
+
const b22 = (pow2(b11, _11n, P) * b11) % P;
|
|
54
|
+
const b44 = (pow2(b22, _22n, P) * b22) % P;
|
|
55
|
+
const b88 = (pow2(b44, _44n, P) * b44) % P;
|
|
56
|
+
const b176 = (pow2(b88, _88n, P) * b88) % P;
|
|
57
|
+
const b220 = (pow2(b176, _44n, P) * b44) % P;
|
|
58
|
+
const b223 = (pow2(b220, _3n, P) * b3) % P;
|
|
59
|
+
const t1 = (pow2(b223, _23n, P) * b22) % P;
|
|
60
|
+
const t2 = (pow2(t1, _6n, P) * b2) % P;
|
|
61
|
+
const root = pow2(t2, _2n, P);
|
|
62
|
+
if (!Fpk1.eql(Fpk1.sqr(root), y))
|
|
63
|
+
throw new Error('Cannot find square root');
|
|
64
|
+
return root;
|
|
65
|
+
}
|
|
66
|
+
const Fpk1 = Field(secp256k1_CURVE.p, { sqrt: sqrtMod });
|
|
67
|
+
const Pointk1 = /* @__PURE__ */ weierstrass(secp256k1_CURVE, {
|
|
68
|
+
Fp: Fpk1,
|
|
69
|
+
endo: secp256k1_ENDO,
|
|
70
|
+
});
|
|
71
|
+
/**
|
|
72
|
+
* secp256k1 curve: ECDSA and ECDH methods.
|
|
73
|
+
*
|
|
74
|
+
* Uses sha256 to hash messages. To use a different hash,
|
|
75
|
+
* pass `{ prehash: false }` to sign / verify.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* Generate one secp256k1 keypair, sign a message, and verify it.
|
|
79
|
+
*
|
|
80
|
+
* ```js
|
|
81
|
+
* import { secp256k1 } from './secp256k1';
|
|
82
|
+
* const { secretKey, publicKey } = secp256k1.keygen();
|
|
83
|
+
* // const publicKey = secp256k1.getPublicKey(secretKey);
|
|
84
|
+
* const msg = new TextEncoder().encode('hello noble');
|
|
85
|
+
* const sig = secp256k1.sign(msg, secretKey);
|
|
86
|
+
* const isValid = secp256k1.verify(sig, msg, publicKey);
|
|
87
|
+
* // const sigKeccak = secp256k1.sign(keccak256(msg), secretKey, { prehash: false });
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export const secp256k1 = /* @__PURE__ */ ecdsa(Pointk1, sha256);
|
|
91
|
+
// Schnorr signatures are superior to ECDSA from above. Below is Schnorr-specific BIP0340 code.
|
|
92
|
+
// https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki
|
|
93
|
+
/** An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)] */
|
|
94
|
+
const TAGGED_HASH_PREFIXES = {};
|
|
95
|
+
// BIP-340 phrases tags as UTF-8, but all current standardized names here are 7-bit ASCII.
|
|
96
|
+
function taggedHash(tag, ...messages) {
|
|
97
|
+
let tagP = TAGGED_HASH_PREFIXES[tag];
|
|
98
|
+
if (tagP === undefined) {
|
|
99
|
+
const tagH = sha256(asciiToBytes(tag));
|
|
100
|
+
tagP = concatBytes(tagH, tagH);
|
|
101
|
+
TAGGED_HASH_PREFIXES[tag] = tagP;
|
|
102
|
+
}
|
|
103
|
+
return sha256(concatBytes(tagP, ...messages));
|
|
104
|
+
}
|
|
105
|
+
// ECDSA compact points are 33-byte. Schnorr is 32: we strip first byte 0x02 or 0x03
|
|
106
|
+
const pointToBytes = (point) => point.toBytes(true).slice(1);
|
|
107
|
+
const hasEven = (y) => y % _2n === _0n;
|
|
108
|
+
// Calculate point, scalar and bytes
|
|
109
|
+
function schnorrGetExtPubKey(priv) {
|
|
110
|
+
const { Fn, BASE } = Pointk1;
|
|
111
|
+
const d_ = Fn.fromBytes(priv);
|
|
112
|
+
const p = BASE.multiply(d_); // P = d'⋅G; 0 < d' < n check is done inside
|
|
113
|
+
const scalar = hasEven(p.y) ? d_ : Fn.neg(d_);
|
|
114
|
+
return { scalar, bytes: pointToBytes(p) };
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* lift_x from BIP340. Convert 32-byte x coordinate to elliptic curve point.
|
|
118
|
+
* @returns valid point checked for being on-curve
|
|
119
|
+
*/
|
|
120
|
+
function lift_x(x) {
|
|
121
|
+
const Fp = Fpk1;
|
|
122
|
+
if (!Fp.isValidNot0(x))
|
|
123
|
+
throw new Error('invalid x: Fail if x ≥ p');
|
|
124
|
+
const xx = Fp.create(x * x);
|
|
125
|
+
const c = Fp.create(xx * x + BigInt(7)); // Let c = x³ + 7 mod p.
|
|
126
|
+
let y = Fp.sqrt(c); // Let y = c^(p+1)/4 mod p. Same as sqrt().
|
|
127
|
+
// Return the unique point P such that x(P) = x and
|
|
128
|
+
// y(P) = y if y mod 2 = 0 or y(P) = p-y otherwise.
|
|
129
|
+
if (!hasEven(y))
|
|
130
|
+
y = Fp.neg(y);
|
|
131
|
+
const p = Pointk1.fromAffine({ x, y });
|
|
132
|
+
p.assertValidity();
|
|
133
|
+
return p;
|
|
134
|
+
}
|
|
135
|
+
// BIP-340 callers still need to supply canonical 32-byte inputs where required; this alias only
|
|
136
|
+
// parses big-endian bytes and does not enforce the fixed-width contract itself.
|
|
137
|
+
const num = bytesToNumberBE;
|
|
138
|
+
/** Create tagged hash, convert it to bigint, reduce modulo-n. */
|
|
139
|
+
function challenge(...args) {
|
|
140
|
+
return Pointk1.Fn.create(num(taggedHash('BIP0340/challenge', ...args)));
|
|
141
|
+
}
|
|
142
|
+
/** Schnorr public key is just `x` coordinate of Point as per BIP340. */
|
|
143
|
+
function schnorrGetPublicKey(secretKey) {
|
|
144
|
+
return schnorrGetExtPubKey(secretKey).bytes; // d'=int(sk). Fail if d'=0 or d'≥n. Ret bytes(d'⋅G)
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Creates Schnorr signature as per BIP340. Verifies itself before returning anything.
|
|
148
|
+
* `auxRand` is optional and is not the sole source of `k` generation: bad CSPRNG output will not
|
|
149
|
+
* be catastrophic, but BIP-340 still recommends fresh auxiliary randomness when available to harden
|
|
150
|
+
* deterministic signing against side-channel and fault-injection attacks.
|
|
151
|
+
*/
|
|
152
|
+
function schnorrSign(message, secretKey, auxRand = randomBytes(32)) {
|
|
153
|
+
const { Fn, BASE } = Pointk1;
|
|
154
|
+
const m = abytes(message, undefined, 'message');
|
|
155
|
+
const { bytes: px, scalar: d } = schnorrGetExtPubKey(secretKey); // checks for isWithinCurveOrder
|
|
156
|
+
const a = abytes(auxRand, 32, 'auxRand'); // Auxiliary random data a: a 32-byte array
|
|
157
|
+
// Let t be the byte-wise xor of bytes(d) and hash/aux(a).
|
|
158
|
+
const t = Fn.toBytes(d ^ num(taggedHash('BIP0340/aux', a)));
|
|
159
|
+
const rand = taggedHash('BIP0340/nonce', t, px, m); // Let rand = hash/nonce(t || bytes(P) || m)
|
|
160
|
+
// BIP340 defines k' = int(rand) mod n. We can't reuse schnorrGetExtPubKey(rand)
|
|
161
|
+
// here: that helper parses canonical secret keys and rejects rand >= n instead
|
|
162
|
+
// of reducing the nonce hash modulo the group order.
|
|
163
|
+
const k_ = Fn.create(num(rand));
|
|
164
|
+
// BIP-340: "Let k' = int(rand) mod n. Fail if k' = 0. Let R = k'⋅G."
|
|
165
|
+
if (k_ === 0n)
|
|
166
|
+
throw new Error('sign failed: k is zero');
|
|
167
|
+
const p = BASE.multiply(k_); // Rejects zero; only the raw nonce hash needs reduction.
|
|
168
|
+
const k = hasEven(p.y) ? k_ : Fn.neg(k_);
|
|
169
|
+
const rx = pointToBytes(p);
|
|
170
|
+
const e = challenge(rx, px, m); // Let e = int(hash/challenge(bytes(R) || bytes(P) || m)) mod n.
|
|
171
|
+
const sig = new Uint8Array(64); // Let sig = bytes(R) || bytes((k + ed) mod n).
|
|
172
|
+
sig.set(rx, 0);
|
|
173
|
+
sig.set(Fn.toBytes(Fn.create(k + e * d)), 32);
|
|
174
|
+
// If Verify(bytes(P), m, sig) (see below) returns failure, abort
|
|
175
|
+
if (!schnorrVerify(sig, m, px))
|
|
176
|
+
throw new Error('sign: Invalid signature produced');
|
|
177
|
+
return sig;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Verifies Schnorr signature.
|
|
181
|
+
* Will swallow errors & return false except for initial type validation of arguments.
|
|
182
|
+
*/
|
|
183
|
+
function schnorrVerify(signature, message, publicKey) {
|
|
184
|
+
const { Fp, Fn, BASE } = Pointk1;
|
|
185
|
+
const sig = abytes(signature, 64, 'signature');
|
|
186
|
+
const m = abytes(message, undefined, 'message');
|
|
187
|
+
const pub = abytes(publicKey, 32, 'publicKey');
|
|
188
|
+
try {
|
|
189
|
+
const P = lift_x(num(pub)); // P = lift_x(int(pk)); fail if that fails
|
|
190
|
+
const r = num(sig.subarray(0, 32)); // Let r = int(sig[0:32]); fail if r ≥ p.
|
|
191
|
+
if (!Fp.isValidNot0(r))
|
|
192
|
+
return false;
|
|
193
|
+
const s = num(sig.subarray(32, 64)); // Let s = int(sig[32:64]); fail if s ≥ n.
|
|
194
|
+
// Stricter than BIP-340/libsecp256k1, which only reject s >= n. Honest signing reaches
|
|
195
|
+
// s = 0 only with negligible probability (k + e*d ≡ 0 mod n), so treat zero-s inputs as
|
|
196
|
+
// crafted edge cases and fail closed instead of carrying that extra verification surface.
|
|
197
|
+
if (!Fn.isValidNot0(s))
|
|
198
|
+
return false;
|
|
199
|
+
// int(challenge(bytes(r) || bytes(P) || m)) % n
|
|
200
|
+
const e = challenge(Fn.toBytes(r), pointToBytes(P), m);
|
|
201
|
+
// R = s⋅G - e⋅P, where -eP == (n-e)P
|
|
202
|
+
const R = BASE.multiplyUnsafe(s).add(P.multiplyUnsafe(Fn.neg(e)));
|
|
203
|
+
const { x, y } = R.toAffine();
|
|
204
|
+
// Fail if is_infinite(R) / not has_even_y(R) / x(R) ≠ r.
|
|
205
|
+
if (R.is0() || !hasEven(y) || x !== r)
|
|
206
|
+
return false;
|
|
207
|
+
return true;
|
|
208
|
+
}
|
|
209
|
+
catch (error) {
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
export const __TEST = /* @__PURE__ */ Object.freeze({ lift_x });
|
|
214
|
+
/**
|
|
215
|
+
* Schnorr signatures over secp256k1.
|
|
216
|
+
* See {@link https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki | BIP 340}.
|
|
217
|
+
* @example
|
|
218
|
+
* Generate one BIP340 Schnorr keypair, sign a message, and verify it.
|
|
219
|
+
*
|
|
220
|
+
* ```js
|
|
221
|
+
* import { schnorr } from './secp256k1';
|
|
222
|
+
* const { secretKey, publicKey } = schnorr.keygen();
|
|
223
|
+
* // const publicKey = schnorr.getPublicKey(secretKey);
|
|
224
|
+
* const msg = new TextEncoder().encode('hello');
|
|
225
|
+
* const sig = schnorr.sign(msg, secretKey);
|
|
226
|
+
* const isValid = schnorr.verify(sig, msg, publicKey);
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
export const schnorr = /* @__PURE__ */ (() => {
|
|
230
|
+
const size = 32;
|
|
231
|
+
const seedLength = 48;
|
|
232
|
+
const randomSecretKey = (seed) => {
|
|
233
|
+
seed = seed === undefined ? randomBytes(seedLength) : seed;
|
|
234
|
+
return mapHashToField(seed, secp256k1_CURVE.n);
|
|
235
|
+
};
|
|
236
|
+
return Object.freeze({
|
|
237
|
+
keygen: createKeygen(randomSecretKey, schnorrGetPublicKey),
|
|
238
|
+
getPublicKey: schnorrGetPublicKey,
|
|
239
|
+
sign: schnorrSign,
|
|
240
|
+
verify: schnorrVerify,
|
|
241
|
+
Point: Pointk1,
|
|
242
|
+
utils: Object.freeze({
|
|
243
|
+
randomSecretKey,
|
|
244
|
+
taggedHash,
|
|
245
|
+
lift_x,
|
|
246
|
+
pointToBytes,
|
|
247
|
+
}),
|
|
248
|
+
lengths: Object.freeze({
|
|
249
|
+
secretKey: size,
|
|
250
|
+
publicKey: size,
|
|
251
|
+
publicKeyHasPrefix: false,
|
|
252
|
+
signature: size * 2,
|
|
253
|
+
seed: seedLength,
|
|
254
|
+
}),
|
|
255
|
+
});
|
|
256
|
+
})();
|
|
257
|
+
// RFC 9380 Appendix E.1 3-isogeny coefficients for secp256k1, stored in ascending degree order.
|
|
258
|
+
// The final `1` in each denominator array is the explicit monic leading term.
|
|
259
|
+
const isoMap = /* @__PURE__ */ (() => isogenyMap(Fpk1, [
|
|
260
|
+
// xNum
|
|
261
|
+
[
|
|
262
|
+
'0x8e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38daaaaa8c7',
|
|
263
|
+
'0x7d3d4c80bc321d5b9f315cea7fd44c5d595d2fc0bf63b92dfff1044f17c6581',
|
|
264
|
+
'0x534c328d23f234e6e2a413deca25caece4506144037c40314ecbd0b53d9dd262',
|
|
265
|
+
'0x8e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38daaaaa88c',
|
|
266
|
+
],
|
|
267
|
+
// xDen
|
|
268
|
+
[
|
|
269
|
+
'0xd35771193d94918a9ca34ccbb7b640dd86cd409542f8487d9fe6b745781eb49b',
|
|
270
|
+
'0xedadc6f64383dc1df7c4b2d51b54225406d36b641f5e41bbc52a56612a8c6d14',
|
|
271
|
+
'0x0000000000000000000000000000000000000000000000000000000000000001', // LAST 1
|
|
272
|
+
],
|
|
273
|
+
// yNum
|
|
274
|
+
[
|
|
275
|
+
'0x4bda12f684bda12f684bda12f684bda12f684bda12f684bda12f684b8e38e23c',
|
|
276
|
+
'0xc75e0c32d5cb7c0fa9d0a54b12a0a6d5647ab046d686da6fdffc90fc201d71a3',
|
|
277
|
+
'0x29a6194691f91a73715209ef6512e576722830a201be2018a765e85a9ecee931',
|
|
278
|
+
'0x2f684bda12f684bda12f684bda12f684bda12f684bda12f684bda12f38e38d84',
|
|
279
|
+
],
|
|
280
|
+
// yDen
|
|
281
|
+
[
|
|
282
|
+
'0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffff93b',
|
|
283
|
+
'0x7a06534bb8bdb49fd5e9e6632722c2989467c1bfc8e8d978dfb425d2685c2573',
|
|
284
|
+
'0x6484aa716545ca2cf3a70c3fa8fe337e0a3d21162f0d6299a7bf8192bfd2a76f',
|
|
285
|
+
'0x0000000000000000000000000000000000000000000000000000000000000001', // LAST 1
|
|
286
|
+
],
|
|
287
|
+
].map((i) => i.map((j) => BigInt(j)))))();
|
|
288
|
+
// RFC 9380 §8.7 secp256k1 E' parameters for the SWU-to-isogeny pipeline below.
|
|
289
|
+
let mapSWU;
|
|
290
|
+
const getMapSWU = () => mapSWU ||
|
|
291
|
+
(mapSWU = mapToCurveSimpleSWU(Fpk1, {
|
|
292
|
+
// Building the SWU sqrt-ratio helper eagerly adds noticeable `secp256k1.js` import cost, so
|
|
293
|
+
// defer it to first use; after that the cached mapper is reused directly.
|
|
294
|
+
A: BigInt('0x3f8731abdd661adca08a5558f0f5d272e953d363cb6f0e5d405447c01a444533'),
|
|
295
|
+
B: BigInt('1771'),
|
|
296
|
+
Z: Fpk1.create(BigInt('-11')),
|
|
297
|
+
}));
|
|
298
|
+
/**
|
|
299
|
+
* Hashing / encoding to secp256k1 points / field. RFC 9380 methods.
|
|
300
|
+
* @example
|
|
301
|
+
* Hash one message onto secp256k1.
|
|
302
|
+
*
|
|
303
|
+
* ```ts
|
|
304
|
+
* const point = secp256k1_hasher.hashToCurve(new TextEncoder().encode('hello noble'));
|
|
305
|
+
* ```
|
|
306
|
+
*/
|
|
307
|
+
export const secp256k1_hasher = /* @__PURE__ */ (() => createHasher(Pointk1, (scalars) => {
|
|
308
|
+
const { x, y } = getMapSWU()(Fpk1.create(scalars[0]));
|
|
309
|
+
return isoMap(x, y);
|
|
310
|
+
}, {
|
|
311
|
+
DST: 'secp256k1_XMD:SHA-256_SSWU_RO_',
|
|
312
|
+
encodeDST: 'secp256k1_XMD:SHA-256_SSWU_NU_',
|
|
313
|
+
p: Fpk1.ORDER,
|
|
314
|
+
m: 1,
|
|
315
|
+
k: 128,
|
|
316
|
+
expand: 'xmd',
|
|
317
|
+
hash: sha256,
|
|
318
|
+
}))();
|
|
319
|
+
/**
|
|
320
|
+
* FROST threshold signatures over secp256k1. RFC 9591.
|
|
321
|
+
* @example
|
|
322
|
+
* Create one trusted-dealer package for 2-of-3 secp256k1 signing.
|
|
323
|
+
*
|
|
324
|
+
* ```ts
|
|
325
|
+
* const alice = secp256k1_FROST.Identifier.derive('alice@example.com');
|
|
326
|
+
* const bob = secp256k1_FROST.Identifier.derive('bob@example.com');
|
|
327
|
+
* const carol = secp256k1_FROST.Identifier.derive('carol@example.com');
|
|
328
|
+
* const deal = secp256k1_FROST.trustedDealer({ min: 2, max: 3 }, [alice, bob, carol]);
|
|
329
|
+
* ```
|
|
330
|
+
*/
|
|
331
|
+
export const secp256k1_FROST = /* @__PURE__ */ (() => createFROST({
|
|
332
|
+
name: 'FROST-secp256k1-SHA256-v1',
|
|
333
|
+
Point: Pointk1,
|
|
334
|
+
hashToScalar: secp256k1_hasher.hashToScalar,
|
|
335
|
+
hash: sha256,
|
|
336
|
+
}))();
|
|
337
|
+
// Taproot utils
|
|
338
|
+
// `undefined` means "disable TapTweak entirely"; callers that want the BIP-341/BIP-386 empty
|
|
339
|
+
// merkle root must pass `new Uint8Array(0)` explicitly.
|
|
340
|
+
function tweak(point, merkleRoot) {
|
|
341
|
+
if (merkleRoot === undefined)
|
|
342
|
+
return _0n;
|
|
343
|
+
const x = pointToBytes(point);
|
|
344
|
+
const t = bytesToNumberBE(taggedHash('TapTweak', x, merkleRoot));
|
|
345
|
+
// BIP-341 taproot_tweak_pubkey/taproot_tweak_seckey: "if t >= SECP256K1_ORDER:
|
|
346
|
+
// raise ValueError". TapTweak must reject overflow instead of reducing modulo n.
|
|
347
|
+
if (!Pointk1.Fn.isValid(t))
|
|
348
|
+
throw new Error('invalid TapTweak hash');
|
|
349
|
+
return t;
|
|
350
|
+
}
|
|
351
|
+
function frostPubToEvenY(pub) {
|
|
352
|
+
const VK = Pointk1.fromBytes(pub.commitments[0]);
|
|
353
|
+
// Keep aliasing on the already-even path so wrapper callers can skip unnecessary cloning.
|
|
354
|
+
if (hasEven(VK.y))
|
|
355
|
+
return pub;
|
|
356
|
+
return {
|
|
357
|
+
signers: { min: pub.signers.min, max: pub.signers.max },
|
|
358
|
+
commitments: pub.commitments.map((i) => Pointk1.fromBytes(i).negate().toBytes()),
|
|
359
|
+
verifyingShares: Object.fromEntries(Object.entries(pub.verifyingShares).map(([k, v]) => [
|
|
360
|
+
k,
|
|
361
|
+
Pointk1.fromBytes(v).negate().toBytes(),
|
|
362
|
+
])),
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
function frostSecretToEvenY(s, pub) {
|
|
366
|
+
const VK = Pointk1.fromBytes(pub.commitments[0]);
|
|
367
|
+
// Keep aliasing on the already-even path so wrapper callers can preserve package identity.
|
|
368
|
+
if (hasEven(VK.y))
|
|
369
|
+
return s;
|
|
370
|
+
const Fn = Pointk1.Fn;
|
|
371
|
+
return {
|
|
372
|
+
...s,
|
|
373
|
+
signingShare: Fn.toBytes(Fn.neg(Fn.fromBytes(s.signingShare))),
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
function frostNoncesToEvenY(PK, nonces) {
|
|
377
|
+
if (hasEven(PK.y))
|
|
378
|
+
return nonces;
|
|
379
|
+
const Fn = Pointk1.Fn;
|
|
380
|
+
return {
|
|
381
|
+
binding: Fn.toBytes(Fn.neg(Fn.fromBytes(nonces.binding))),
|
|
382
|
+
hiding: Fn.toBytes(Fn.neg(Fn.fromBytes(nonces.hiding))),
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
function frostTweakSecret(s, pub, merkleRoot) {
|
|
386
|
+
const Fn = Pointk1.Fn;
|
|
387
|
+
const keyPackage = frostSecretToEvenY(s, pub);
|
|
388
|
+
const evenPub = frostPubToEvenY(pub);
|
|
389
|
+
const t = tweak(Pointk1.fromBytes(evenPub.commitments[0]), merkleRoot);
|
|
390
|
+
const signingShare = Fn.toBytes(Fn.add(Fn.fromBytes(keyPackage.signingShare), t));
|
|
391
|
+
return {
|
|
392
|
+
identifier: keyPackage.identifier,
|
|
393
|
+
signingShare,
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
function frostTweakPublic(pub, merkleRoot) {
|
|
397
|
+
const PKPackage = frostPubToEvenY(pub);
|
|
398
|
+
const t = tweak(Pointk1.fromBytes(PKPackage.commitments[0]), merkleRoot);
|
|
399
|
+
const tp = Pointk1.BASE.multiply(t);
|
|
400
|
+
const commitments = PKPackage.commitments.map((c, i) => (i === 0 ? Pointk1.fromBytes(c).add(tp) : Pointk1.fromBytes(c)).toBytes());
|
|
401
|
+
const verifyingShares = {};
|
|
402
|
+
for (const k in PKPackage.verifyingShares) {
|
|
403
|
+
verifyingShares[k] = Pointk1.fromBytes(PKPackage.verifyingShares[k]).add(tp).toBytes();
|
|
404
|
+
}
|
|
405
|
+
return {
|
|
406
|
+
signers: { min: PKPackage.signers.min, max: PKPackage.signers.max },
|
|
407
|
+
commitments,
|
|
408
|
+
verifyingShares,
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* FROST threshold signatures over secp256k1-schnorr-taproot. RFC 9591.
|
|
413
|
+
* DKG outputs are auto-tweaked with the empty Taproot merkle root for compatibility, while
|
|
414
|
+
* `trustedDealer()` outputs stay untweaked unless callers apply the Taproot tweak themselves.
|
|
415
|
+
* @example
|
|
416
|
+
* Create one trusted-dealer package for Taproot-compatible FROST signing.
|
|
417
|
+
*
|
|
418
|
+
* ```ts
|
|
419
|
+
* const alice = schnorr_FROST.Identifier.derive('alice@example.com');
|
|
420
|
+
* const bob = schnorr_FROST.Identifier.derive('bob@example.com');
|
|
421
|
+
* const carol = schnorr_FROST.Identifier.derive('carol@example.com');
|
|
422
|
+
* const deal = schnorr_FROST.trustedDealer({ min: 2, max: 3 }, [alice, bob, carol]);
|
|
423
|
+
* ```
|
|
424
|
+
*/
|
|
425
|
+
export const schnorr_FROST = /* @__PURE__ */ (() => createFROST({
|
|
426
|
+
name: 'FROST-secp256k1-SHA256-TR-v1',
|
|
427
|
+
Point: Pointk1,
|
|
428
|
+
hashToScalar: secp256k1_hasher.hashToScalar,
|
|
429
|
+
hash: sha256,
|
|
430
|
+
// Taproot related hacks
|
|
431
|
+
parsePublicKey(publicKey) {
|
|
432
|
+
// External Taproot keys are x-only, but local key packages still use compressed points.
|
|
433
|
+
if (publicKey.length === 32)
|
|
434
|
+
return lift_x(bytesToNumberBE(publicKey));
|
|
435
|
+
if (publicKey.length === 33)
|
|
436
|
+
return Pointk1.fromBytes(publicKey);
|
|
437
|
+
throw new Error(`expected x-only or compressed public key, got length=${publicKey.length}`);
|
|
438
|
+
},
|
|
439
|
+
adjustScalar(n) {
|
|
440
|
+
const PK = Pointk1.BASE.multiply(n);
|
|
441
|
+
return hasEven(PK.y) ? n : Pointk1.Fn.neg(n);
|
|
442
|
+
},
|
|
443
|
+
adjustPoint: (p) => (hasEven(p.y) ? p : p.negate()),
|
|
444
|
+
challenge(R, PK, msg) {
|
|
445
|
+
return challenge(pointToBytes(R), pointToBytes(PK), msg);
|
|
446
|
+
},
|
|
447
|
+
adjustNonces: frostNoncesToEvenY,
|
|
448
|
+
adjustGroupCommitmentShare: (GC, GCShare) => (!hasEven(GC.y) ? GCShare.negate() : GCShare),
|
|
449
|
+
adjustPublic: frostPubToEvenY,
|
|
450
|
+
adjustSecret: frostSecretToEvenY,
|
|
451
|
+
adjustTx: {
|
|
452
|
+
// Compat with official implementation
|
|
453
|
+
encode: (tx) => tx.subarray(1),
|
|
454
|
+
decode: (tx) => concatBytes(Uint8Array.of(0x02), tx),
|
|
455
|
+
},
|
|
456
|
+
adjustDKG: (k) => {
|
|
457
|
+
// Compatibility with frost-secp256k1-tr: DKG output is auto-tweaked with the
|
|
458
|
+
// empty Taproot merkle root, while dealer-generated keys stay untweaked.
|
|
459
|
+
const merkleRoot = new Uint8Array(0);
|
|
460
|
+
return {
|
|
461
|
+
public: frostTweakPublic(k.public, merkleRoot),
|
|
462
|
+
secret: frostTweakSecret(k.secret, k.public, merkleRoot),
|
|
463
|
+
};
|
|
464
|
+
},
|
|
465
|
+
}))();
|