@noble/curves 0.5.2 → 0.6.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/README.md +49 -5
- package/lib/_shortw_utils.d.ts +10 -21
- package/lib/abstract/bls.d.ts +39 -32
- package/lib/abstract/bls.js +74 -73
- package/lib/abstract/{group.d.ts → curve.d.ts} +31 -1
- package/lib/abstract/{group.js → curve.js} +39 -2
- package/lib/abstract/edwards.d.ts +30 -72
- package/lib/abstract/edwards.js +197 -375
- package/lib/abstract/hash-to-curve.d.ts +25 -6
- package/lib/abstract/hash-to-curve.js +40 -12
- package/lib/abstract/modular.d.ts +20 -7
- package/lib/abstract/modular.js +61 -35
- package/lib/abstract/montgomery.js +4 -5
- package/lib/abstract/poseidon.d.ts +29 -0
- package/lib/abstract/poseidon.js +115 -0
- package/lib/abstract/utils.d.ts +5 -36
- package/lib/abstract/utils.js +23 -71
- package/lib/abstract/weierstrass.d.ts +51 -74
- package/lib/abstract/weierstrass.js +455 -628
- package/lib/bls12-381.js +63 -58
- package/lib/bn.js +1 -1
- package/lib/ed25519.d.ts +7 -5
- package/lib/ed25519.js +82 -79
- package/lib/ed448.d.ts +3 -0
- package/lib/ed448.js +86 -83
- package/lib/esm/abstract/bls.js +75 -74
- package/lib/esm/abstract/{group.js → curve.js} +37 -1
- package/lib/esm/abstract/edwards.js +196 -374
- package/lib/esm/abstract/hash-to-curve.js +38 -11
- package/lib/esm/abstract/modular.js +58 -34
- package/lib/esm/abstract/montgomery.js +5 -6
- package/lib/esm/abstract/poseidon.js +109 -0
- package/lib/esm/abstract/utils.js +21 -66
- package/lib/esm/abstract/weierstrass.js +454 -627
- package/lib/esm/bls12-381.js +75 -70
- package/lib/esm/bn.js +1 -1
- package/lib/esm/ed25519.js +80 -78
- package/lib/esm/ed448.js +84 -82
- package/lib/esm/jubjub.js +1 -1
- package/lib/esm/p256.js +11 -9
- package/lib/esm/p384.js +11 -9
- package/lib/esm/p521.js +13 -12
- package/lib/esm/secp256k1.js +115 -151
- package/lib/esm/stark.js +104 -40
- package/lib/jubjub.d.ts +2 -2
- package/lib/jubjub.js +1 -1
- package/lib/p192.d.ts +20 -42
- package/lib/p224.d.ts +20 -42
- package/lib/p256.d.ts +23 -42
- package/lib/p256.js +13 -10
- package/lib/p384.d.ts +23 -42
- package/lib/p384.js +13 -10
- package/lib/p521.d.ts +23 -42
- package/lib/p521.js +15 -13
- package/lib/secp256k1.d.ts +25 -37
- package/lib/secp256k1.js +115 -151
- package/lib/stark.d.ts +36 -19
- package/lib/stark.js +107 -40
- package/package.json +13 -8
package/lib/secp256k1.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.encodeToCurve = exports.hashToCurve = exports.schnorr = exports.secp256k1 = void 0;
|
|
4
4
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
5
5
|
const sha256_1 = require("@noble/hashes/sha256");
|
|
6
6
|
const modular_js_1 = require("./abstract/modular.js");
|
|
@@ -8,7 +8,7 @@ const _shortw_utils_js_1 = require("./_shortw_utils.js");
|
|
|
8
8
|
const weierstrass_js_1 = require("./abstract/weierstrass.js");
|
|
9
9
|
const utils_js_1 = require("./abstract/utils.js");
|
|
10
10
|
const utils_1 = require("@noble/hashes/utils");
|
|
11
|
-
const
|
|
11
|
+
const htf = require("./abstract/hash-to-curve.js");
|
|
12
12
|
/**
|
|
13
13
|
* secp256k1 belongs to Koblitz curves: it has efficiently computable endomorphism.
|
|
14
14
|
* Endomorphism uses 2x less RAM, speeds up precomputation by 2x and ECDH / key recovery by 20%.
|
|
@@ -22,10 +22,7 @@ const _1n = BigInt(1);
|
|
|
22
22
|
const _2n = BigInt(2);
|
|
23
23
|
const divNearest = (a, b) => (a + b / _2n) / b;
|
|
24
24
|
/**
|
|
25
|
-
*
|
|
26
|
-
* To calculate √y, we need to exponentiate it to a very big number:
|
|
27
|
-
* `y² = x³ + ax + b; y = y² ^ (p+1)/4`
|
|
28
|
-
* We are unwrapping the loop and multiplying it bit-by-bit.
|
|
25
|
+
* √n = n^((p+1)/4) for fields p = 3 mod 4. We unwrap the loop and multiply bit-by-bit.
|
|
29
26
|
* (P+1n/4n).toString(2) would produce bits [223x 1, 0, 22x 1, 4x 0, 11, 00]
|
|
30
27
|
*/
|
|
31
28
|
function sqrtMod(y) {
|
|
@@ -48,45 +45,11 @@ function sqrtMod(y) {
|
|
|
48
45
|
const t1 = ((0, modular_js_1.pow2)(b223, _23n, P) * b22) % P;
|
|
49
46
|
const t2 = ((0, modular_js_1.pow2)(t1, _6n, P) * b2) % P;
|
|
50
47
|
const root = (0, modular_js_1.pow2)(t2, _2n, P);
|
|
51
|
-
if (!Fp.
|
|
48
|
+
if (!Fp.eql(Fp.sqr(root), y))
|
|
52
49
|
throw new Error('Cannot find square root');
|
|
53
50
|
return root;
|
|
54
51
|
}
|
|
55
52
|
const Fp = (0, modular_js_1.Fp)(secp256k1P, undefined, undefined, { sqrt: sqrtMod });
|
|
56
|
-
const isoMap = (0, hash_to_curve_js_1.isogenyMap)(Fp, [
|
|
57
|
-
// xNum
|
|
58
|
-
[
|
|
59
|
-
'0x8e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38daaaaa8c7',
|
|
60
|
-
'0x7d3d4c80bc321d5b9f315cea7fd44c5d595d2fc0bf63b92dfff1044f17c6581',
|
|
61
|
-
'0x534c328d23f234e6e2a413deca25caece4506144037c40314ecbd0b53d9dd262',
|
|
62
|
-
'0x8e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38daaaaa88c',
|
|
63
|
-
],
|
|
64
|
-
// xDen
|
|
65
|
-
[
|
|
66
|
-
'0xd35771193d94918a9ca34ccbb7b640dd86cd409542f8487d9fe6b745781eb49b',
|
|
67
|
-
'0xedadc6f64383dc1df7c4b2d51b54225406d36b641f5e41bbc52a56612a8c6d14',
|
|
68
|
-
'0x0000000000000000000000000000000000000000000000000000000000000001', // LAST 1
|
|
69
|
-
],
|
|
70
|
-
// yNum
|
|
71
|
-
[
|
|
72
|
-
'0x4bda12f684bda12f684bda12f684bda12f684bda12f684bda12f684b8e38e23c',
|
|
73
|
-
'0xc75e0c32d5cb7c0fa9d0a54b12a0a6d5647ab046d686da6fdffc90fc201d71a3',
|
|
74
|
-
'0x29a6194691f91a73715209ef6512e576722830a201be2018a765e85a9ecee931',
|
|
75
|
-
'0x2f684bda12f684bda12f684bda12f684bda12f684bda12f684bda12f38e38d84',
|
|
76
|
-
],
|
|
77
|
-
// yDen
|
|
78
|
-
[
|
|
79
|
-
'0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffff93b',
|
|
80
|
-
'0x7a06534bb8bdb49fd5e9e6632722c2989467c1bfc8e8d978dfb425d2685c2573',
|
|
81
|
-
'0x6484aa716545ca2cf3a70c3fa8fe337e0a3d21162f0d6299a7bf8192bfd2a76f',
|
|
82
|
-
'0x0000000000000000000000000000000000000000000000000000000000000001', // LAST 1
|
|
83
|
-
],
|
|
84
|
-
].map((i) => i.map((j) => BigInt(j))));
|
|
85
|
-
const mapSWU = (0, weierstrass_js_1.mapToCurveSimpleSWU)(Fp, {
|
|
86
|
-
A: BigInt('0x3f8731abdd661adca08a5558f0f5d272e953d363cb6f0e5d405447c01a444533'),
|
|
87
|
-
B: BigInt('1771'),
|
|
88
|
-
Z: Fp.create(BigInt('-11')),
|
|
89
|
-
});
|
|
90
53
|
exports.secp256k1 = (0, _shortw_utils_js_1.createCurve)({
|
|
91
54
|
// Params: a, b
|
|
92
55
|
// Seem to be rigid https://bitcointalk.org/index.php?topic=289795.msg3183975#msg3183975
|
|
@@ -129,51 +92,13 @@ exports.secp256k1 = (0, _shortw_utils_js_1.createCurve)({
|
|
|
129
92
|
return { k1neg, k1, k2neg, k2 };
|
|
130
93
|
},
|
|
131
94
|
},
|
|
132
|
-
mapToCurve: (scalars) => {
|
|
133
|
-
const { x, y } = mapSWU(Fp.create(scalars[0]));
|
|
134
|
-
return isoMap(x, y);
|
|
135
|
-
},
|
|
136
|
-
htfDefaults: {
|
|
137
|
-
DST: 'secp256k1_XMD:SHA-256_SSWU_RO_',
|
|
138
|
-
p: Fp.ORDER,
|
|
139
|
-
m: 1,
|
|
140
|
-
k: 128,
|
|
141
|
-
expand: 'xmd',
|
|
142
|
-
hash: sha256_1.sha256,
|
|
143
|
-
},
|
|
144
95
|
}, sha256_1.sha256);
|
|
145
|
-
// Schnorr
|
|
96
|
+
// Schnorr signatures are superior to ECDSA from above.
|
|
97
|
+
// Below is Schnorr-specific code as per BIP0340.
|
|
98
|
+
// https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki
|
|
146
99
|
const _0n = BigInt(0);
|
|
147
|
-
const
|
|
148
|
-
const
|
|
149
|
-
const normalizePrivateKey = exports.secp256k1.utils._normalizePrivateKey;
|
|
150
|
-
// TODO: export?
|
|
151
|
-
function normalizePublicKey(publicKey) {
|
|
152
|
-
if (publicKey instanceof exports.secp256k1.Point) {
|
|
153
|
-
publicKey.assertValidity();
|
|
154
|
-
return publicKey;
|
|
155
|
-
}
|
|
156
|
-
else {
|
|
157
|
-
const bytes = (0, utils_js_1.ensureBytes)(publicKey);
|
|
158
|
-
// Schnorr is 32 bytes
|
|
159
|
-
if (bytes.length !== 32)
|
|
160
|
-
throw new Error('Schnorr pubkeys must be 32 bytes');
|
|
161
|
-
const x = (0, utils_js_1.bytesToNumberBE)(bytes);
|
|
162
|
-
if (!isValidFieldElement(x))
|
|
163
|
-
throw new Error('Point is not on curve');
|
|
164
|
-
const y2 = exports.secp256k1.utils._weierstrassEquation(x); // y² = x³ + ax + b
|
|
165
|
-
let y = sqrtMod(y2); // y = y² ^ (p+1)/4
|
|
166
|
-
const isYOdd = (y & _1n) === _1n;
|
|
167
|
-
// Schnorr
|
|
168
|
-
if (isYOdd)
|
|
169
|
-
y = exports.secp256k1.CURVE.Fp.negate(y);
|
|
170
|
-
const point = new exports.secp256k1.Point(x, y);
|
|
171
|
-
point.assertValidity();
|
|
172
|
-
return point;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
const isWithinCurveOrder = exports.secp256k1.utils._isWithinCurveOrder;
|
|
176
|
-
const isValidFieldElement = exports.secp256k1.utils._isValidFieldElement;
|
|
100
|
+
const fe = (x) => typeof x === 'bigint' && _0n < x && x < secp256k1P;
|
|
101
|
+
const ge = (x) => typeof x === 'bigint' && _0n < x && x < secp256k1N;
|
|
177
102
|
const TAGS = {
|
|
178
103
|
challenge: 'BIP0340/challenge',
|
|
179
104
|
aux: 'BIP0340/aux',
|
|
@@ -190,46 +115,38 @@ function taggedHash(tag, ...messages) {
|
|
|
190
115
|
}
|
|
191
116
|
return (0, sha256_1.sha256)((0, utils_js_1.concatBytes)(tagP, ...messages));
|
|
192
117
|
}
|
|
193
|
-
exports.taggedHash = taggedHash;
|
|
194
118
|
const toRawX = (point) => point.toRawBytes(true).slice(1);
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
// Do we need this at all for Schnorr?
|
|
201
|
-
class SchnorrSignature {
|
|
202
|
-
constructor(r, s) {
|
|
203
|
-
this.r = r;
|
|
204
|
-
this.s = s;
|
|
205
|
-
this.assertValidity();
|
|
206
|
-
}
|
|
207
|
-
static fromHex(hex) {
|
|
208
|
-
const bytes = (0, utils_js_1.ensureBytes)(hex);
|
|
209
|
-
const len = 32; // group length
|
|
210
|
-
if (bytes.length !== 2 * len)
|
|
211
|
-
throw new TypeError(`SchnorrSignature.fromHex: expected ${2 * len} bytes, not ${bytes.length}`);
|
|
212
|
-
const r = (0, utils_js_1.bytesToNumberBE)(bytes.subarray(0, len));
|
|
213
|
-
const s = (0, utils_js_1.bytesToNumberBE)(bytes.subarray(len, 2 * len));
|
|
214
|
-
return new SchnorrSignature(r, s);
|
|
215
|
-
}
|
|
216
|
-
assertValidity() {
|
|
217
|
-
const { r, s } = this;
|
|
218
|
-
if (!isValidFieldElement(r) || !isWithinCurveOrder(s))
|
|
219
|
-
throw new Error('Invalid signature');
|
|
220
|
-
}
|
|
221
|
-
toHex() {
|
|
222
|
-
return numTo32bStr(this.r) + numTo32bStr(this.s);
|
|
223
|
-
}
|
|
224
|
-
toRawBytes() {
|
|
225
|
-
return (0, utils_js_1.hexToBytes)(this.toHex());
|
|
226
|
-
}
|
|
227
|
-
}
|
|
119
|
+
const numTo32b = (n) => (0, utils_js_1.numberToBytesBE)(n, 32);
|
|
120
|
+
const modN = (x) => (0, modular_js_1.mod)(x, secp256k1N);
|
|
121
|
+
const _Point = exports.secp256k1.ProjectivePoint;
|
|
122
|
+
const Gmul = (priv) => _Point.fromPrivateKey(priv);
|
|
123
|
+
const GmulAdd = (Q, a, b) => _Point.BASE.multiplyAndAddUnsafe(Q, a, b);
|
|
228
124
|
function schnorrGetScalar(priv) {
|
|
229
|
-
|
|
230
|
-
|
|
125
|
+
// Let d' = int(sk)
|
|
126
|
+
// Fail if d' = 0 or d' ≥ n
|
|
127
|
+
// Let P = d'⋅G
|
|
128
|
+
// Let d = d' if has_even_y(P), otherwise let d = n - d' .
|
|
129
|
+
const point = Gmul(priv);
|
|
130
|
+
const scalar = point.hasEvenY() ? priv : modN(-priv);
|
|
231
131
|
return { point, scalar, x: toRawX(point) };
|
|
232
132
|
}
|
|
133
|
+
function lift_x(x) {
|
|
134
|
+
if (!fe(x))
|
|
135
|
+
throw new Error('bad x: need 0 < x < p'); // Fail if x ≥ p.
|
|
136
|
+
const c = (0, modular_js_1.mod)(x * x * x + BigInt(7), secp256k1P); // Let c = x³ + 7 mod p.
|
|
137
|
+
let y = sqrtMod(c); // Let y = c^(p+1)/4 mod p.
|
|
138
|
+
if (y % 2n !== 0n)
|
|
139
|
+
y = (0, modular_js_1.mod)(-y, secp256k1P); // Return the unique point P such that x(P) = x and
|
|
140
|
+
const p = new _Point(x, y, _1n); // y(P) = y if y mod 2 = 0 or y(P) = p-y otherwise.
|
|
141
|
+
p.assertValidity();
|
|
142
|
+
return p;
|
|
143
|
+
}
|
|
144
|
+
function challenge(...args) {
|
|
145
|
+
return modN((0, utils_js_1.bytesToNumberBE)(taggedHash(TAGS.challenge, ...args)));
|
|
146
|
+
}
|
|
147
|
+
function schnorrGetPublicKey(privateKey) {
|
|
148
|
+
return toRawX(Gmul(privateKey)); // Let d' = int(sk). Fail if d' = 0 or d' ≥ n. Return bytes(d'⋅G)
|
|
149
|
+
}
|
|
233
150
|
/**
|
|
234
151
|
* Synchronously creates Schnorr signature. Improved security: verifies itself before
|
|
235
152
|
* producing an output.
|
|
@@ -239,23 +156,23 @@ function schnorrGetScalar(priv) {
|
|
|
239
156
|
*/
|
|
240
157
|
function schnorrSign(message, privateKey, auxRand = (0, utils_1.randomBytes)(32)) {
|
|
241
158
|
if (message == null)
|
|
242
|
-
throw new
|
|
159
|
+
throw new Error(`sign: Expected valid message, not "${message}"`);
|
|
243
160
|
const m = (0, utils_js_1.ensureBytes)(message);
|
|
244
161
|
// checks for isWithinCurveOrder
|
|
245
|
-
const { x: px, scalar: d } = schnorrGetScalar(
|
|
246
|
-
const
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
const
|
|
250
|
-
const
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
const
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
162
|
+
const { x: px, scalar: d } = schnorrGetScalar((0, utils_js_1.bytesToNumberBE)((0, utils_js_1.ensureBytes)(privateKey, 32)));
|
|
163
|
+
const a = (0, utils_js_1.ensureBytes)(auxRand, 32); // Auxiliary random data a: a 32-byte array
|
|
164
|
+
// TODO: replace with proper xor?
|
|
165
|
+
const t = numTo32b(d ^ (0, utils_js_1.bytesToNumberBE)(taggedHash(TAGS.aux, a))); // Let t be the byte-wise xor of bytes(d) and hash/aux(a)
|
|
166
|
+
const rand = taggedHash(TAGS.nonce, t, px, m); // Let rand = hash/nonce(t || bytes(P) || m)
|
|
167
|
+
const k_ = modN((0, utils_js_1.bytesToNumberBE)(rand)); // Let k' = int(rand) mod n
|
|
168
|
+
if (k_ === _0n)
|
|
169
|
+
throw new Error('sign failed: k is zero'); // Fail if k' = 0.
|
|
170
|
+
const { point: R, x: rx, scalar: k } = schnorrGetScalar(k_); // Let R = k'⋅G.
|
|
171
|
+
const e = challenge(rx, px, m); // Let e = int(hash/challenge(bytes(R) || bytes(P) || m)) mod n.
|
|
172
|
+
const sig = new Uint8Array(64); // Let sig = bytes(R) || bytes((k + ed) mod n).
|
|
173
|
+
sig.set(numTo32b(R.px), 0);
|
|
174
|
+
sig.set(numTo32b(modN(k + e * d)), 32);
|
|
175
|
+
// If Verify(bytes(P), m, sig) (see below) returns failure, abort
|
|
259
176
|
if (!schnorrVerify(sig, m, px))
|
|
260
177
|
throw new Error('sign: Invalid signature produced');
|
|
261
178
|
return sig;
|
|
@@ -265,30 +182,77 @@ function schnorrSign(message, privateKey, auxRand = (0, utils_1.randomBytes)(32)
|
|
|
265
182
|
*/
|
|
266
183
|
function schnorrVerify(signature, message, publicKey) {
|
|
267
184
|
try {
|
|
268
|
-
const
|
|
269
|
-
const sig =
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
const
|
|
274
|
-
|
|
275
|
-
const e = schnorrChallengeFinalize(taggedHash(TAGS.challenge, numTo32b(r), toRawX(P), m));
|
|
276
|
-
// Finalize
|
|
277
|
-
// R = s⋅G - e⋅P
|
|
278
|
-
// -eP == (n-e)P
|
|
279
|
-
const R = exports.secp256k1.Point.BASE.multiplyAndAddUnsafe(P, normalizePrivateKey(s), (0, modular_js_1.mod)(-e, exports.secp256k1.CURVE.n));
|
|
280
|
-
if (!R || !R.hasEvenY() || R.x !== r)
|
|
185
|
+
const P = lift_x((0, utils_js_1.bytesToNumberBE)((0, utils_js_1.ensureBytes)(publicKey, 32))); // P = lift_x(int(pk)); fail if that fails
|
|
186
|
+
const sig = (0, utils_js_1.ensureBytes)(signature, 64);
|
|
187
|
+
const r = (0, utils_js_1.bytesToNumberBE)(sig.subarray(0, 32)); // Let r = int(sig[0:32]); fail if r ≥ p.
|
|
188
|
+
if (!fe(r))
|
|
189
|
+
return false;
|
|
190
|
+
const s = (0, utils_js_1.bytesToNumberBE)(sig.subarray(32, 64)); // Let s = int(sig[32:64]); fail if s ≥ n.
|
|
191
|
+
if (!ge(s))
|
|
281
192
|
return false;
|
|
282
|
-
|
|
193
|
+
const m = (0, utils_js_1.ensureBytes)(message);
|
|
194
|
+
const e = challenge(numTo32b(r), toRawX(P), m); // int(challenge(bytes(r)||bytes(P)||m)) mod n
|
|
195
|
+
const R = GmulAdd(P, s, modN(-e)); // R = s⋅G - e⋅P
|
|
196
|
+
if (!R || !R.hasEvenY() || R.toAffine().x !== r)
|
|
197
|
+
return false; // -eP == (n-e)P
|
|
198
|
+
return true; // Fail if is_infinite(R) / not has_even_y(R) / x(R) ≠ r.
|
|
283
199
|
}
|
|
284
200
|
catch (error) {
|
|
285
201
|
return false;
|
|
286
202
|
}
|
|
287
203
|
}
|
|
288
204
|
exports.schnorr = {
|
|
289
|
-
Signature: SchnorrSignature,
|
|
290
205
|
// Schnorr's pubkey is just `x` of Point (BIP340)
|
|
291
|
-
getPublicKey:
|
|
206
|
+
getPublicKey: schnorrGetPublicKey,
|
|
292
207
|
sign: schnorrSign,
|
|
293
208
|
verify: schnorrVerify,
|
|
209
|
+
utils: { lift_x, int: utils_js_1.bytesToNumberBE, taggedHash },
|
|
294
210
|
};
|
|
211
|
+
const isoMap = htf.isogenyMap(Fp, [
|
|
212
|
+
// xNum
|
|
213
|
+
[
|
|
214
|
+
'0x8e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38daaaaa8c7',
|
|
215
|
+
'0x7d3d4c80bc321d5b9f315cea7fd44c5d595d2fc0bf63b92dfff1044f17c6581',
|
|
216
|
+
'0x534c328d23f234e6e2a413deca25caece4506144037c40314ecbd0b53d9dd262',
|
|
217
|
+
'0x8e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38daaaaa88c',
|
|
218
|
+
],
|
|
219
|
+
// xDen
|
|
220
|
+
[
|
|
221
|
+
'0xd35771193d94918a9ca34ccbb7b640dd86cd409542f8487d9fe6b745781eb49b',
|
|
222
|
+
'0xedadc6f64383dc1df7c4b2d51b54225406d36b641f5e41bbc52a56612a8c6d14',
|
|
223
|
+
'0x0000000000000000000000000000000000000000000000000000000000000001', // LAST 1
|
|
224
|
+
],
|
|
225
|
+
// yNum
|
|
226
|
+
[
|
|
227
|
+
'0x4bda12f684bda12f684bda12f684bda12f684bda12f684bda12f684b8e38e23c',
|
|
228
|
+
'0xc75e0c32d5cb7c0fa9d0a54b12a0a6d5647ab046d686da6fdffc90fc201d71a3',
|
|
229
|
+
'0x29a6194691f91a73715209ef6512e576722830a201be2018a765e85a9ecee931',
|
|
230
|
+
'0x2f684bda12f684bda12f684bda12f684bda12f684bda12f684bda12f38e38d84',
|
|
231
|
+
],
|
|
232
|
+
// yDen
|
|
233
|
+
[
|
|
234
|
+
'0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffff93b',
|
|
235
|
+
'0x7a06534bb8bdb49fd5e9e6632722c2989467c1bfc8e8d978dfb425d2685c2573',
|
|
236
|
+
'0x6484aa716545ca2cf3a70c3fa8fe337e0a3d21162f0d6299a7bf8192bfd2a76f',
|
|
237
|
+
'0x0000000000000000000000000000000000000000000000000000000000000001', // LAST 1
|
|
238
|
+
],
|
|
239
|
+
].map((i) => i.map((j) => BigInt(j))));
|
|
240
|
+
const mapSWU = (0, weierstrass_js_1.mapToCurveSimpleSWU)(Fp, {
|
|
241
|
+
A: BigInt('0x3f8731abdd661adca08a5558f0f5d272e953d363cb6f0e5d405447c01a444533'),
|
|
242
|
+
B: BigInt('1771'),
|
|
243
|
+
Z: Fp.create(BigInt('-11')),
|
|
244
|
+
});
|
|
245
|
+
const { hashToCurve, encodeToCurve } = htf.hashToCurve(exports.secp256k1.ProjectivePoint, (scalars) => {
|
|
246
|
+
const { x, y } = mapSWU(Fp.create(scalars[0]));
|
|
247
|
+
return isoMap(x, y);
|
|
248
|
+
}, {
|
|
249
|
+
DST: 'secp256k1_XMD:SHA-256_SSWU_RO_',
|
|
250
|
+
encodeDST: 'secp256k1_XMD:SHA-256_SSWU_NU_',
|
|
251
|
+
p: Fp.ORDER,
|
|
252
|
+
m: 1,
|
|
253
|
+
k: 128,
|
|
254
|
+
expand: 'xmd',
|
|
255
|
+
hash: sha256_1.sha256,
|
|
256
|
+
});
|
|
257
|
+
exports.hashToCurve = hashToCurve;
|
|
258
|
+
exports.encodeToCurve = encodeToCurve;
|
package/lib/stark.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ProjPointType } from './abstract/weierstrass.js';
|
|
2
2
|
import * as cutils from './abstract/utils.js';
|
|
3
|
-
|
|
3
|
+
import { Field } from './abstract/modular.js';
|
|
4
|
+
declare type ProjectivePoint = ProjPointType<bigint>;
|
|
4
5
|
export declare const starkCurve: import("./abstract/weierstrass.js").CurveFn;
|
|
5
6
|
declare function getPublicKey0x(privKey: Hex, isCompressed?: boolean): Uint8Array;
|
|
6
7
|
declare function getSharedSecret0x(privKeyA: Hex, pubKeyB: Hex): Uint8Array;
|
|
@@ -9,7 +10,7 @@ declare function verify0x(signature: Hex, msgHash: Hex, pubKey: Hex): boolean;
|
|
|
9
10
|
declare const CURVE: Readonly<{
|
|
10
11
|
readonly nBitLength: number;
|
|
11
12
|
readonly nByteLength: number;
|
|
12
|
-
readonly Fp:
|
|
13
|
+
readonly Fp: Field<bigint>;
|
|
13
14
|
readonly n: bigint;
|
|
14
15
|
readonly h: bigint;
|
|
15
16
|
readonly hEff?: bigint | undefined;
|
|
@@ -29,32 +30,22 @@ declare const CURVE: Readonly<{
|
|
|
29
30
|
k2: bigint;
|
|
30
31
|
};
|
|
31
32
|
} | undefined;
|
|
32
|
-
readonly isTorsionFree?: ((c: import("./abstract/weierstrass.js").
|
|
33
|
-
readonly clearCofactor?: ((c: import("./abstract/weierstrass.js").
|
|
34
|
-
readonly htfDefaults?: import("./abstract/hash-to-curve.js").htfOpts | undefined;
|
|
35
|
-
readonly mapToCurve?: ((scalar: bigint[]) => {
|
|
36
|
-
x: bigint;
|
|
37
|
-
y: bigint;
|
|
38
|
-
}) | undefined;
|
|
33
|
+
readonly isTorsionFree?: ((c: import("./abstract/weierstrass.js").ProjConstructor<bigint>, point: ProjPointType<bigint>) => boolean) | undefined;
|
|
34
|
+
readonly clearCofactor?: ((c: import("./abstract/weierstrass.js").ProjConstructor<bigint>, point: ProjPointType<bigint>) => ProjPointType<bigint>) | undefined;
|
|
39
35
|
lowS: boolean;
|
|
40
36
|
readonly hash: cutils.CHash;
|
|
41
37
|
readonly hmac: (key: Uint8Array, ...messages: Uint8Array[]) => Uint8Array;
|
|
42
38
|
readonly randomBytes: (bytesLength?: number | undefined) => Uint8Array;
|
|
43
|
-
readonly
|
|
44
|
-
|
|
39
|
+
readonly bits2int?: ((bytes: Uint8Array) => bigint) | undefined;
|
|
40
|
+
readonly bits2int_modN?: ((bytes: Uint8Array) => bigint) | undefined;
|
|
41
|
+
}>, ProjectivePoint: import("./abstract/weierstrass.js").ProjConstructor<bigint>, Signature: import("./abstract/weierstrass.js").SignatureConstructor;
|
|
45
42
|
export declare const utils: {
|
|
46
|
-
_bigintToBytes: (num: bigint) => Uint8Array;
|
|
47
|
-
_bigintToString: (num: bigint) => string;
|
|
48
43
|
_normalizePrivateKey: (key: cutils.PrivKey) => bigint;
|
|
49
|
-
_normalizePublicKey: (publicKey: import("./abstract/weierstrass.js").PubKey) => import("./abstract/weierstrass.js").PointType<bigint>;
|
|
50
|
-
_isWithinCurveOrder: (num: bigint) => boolean;
|
|
51
|
-
_isValidFieldElement: (num: bigint) => boolean;
|
|
52
|
-
_weierstrassEquation: (x: bigint) => bigint;
|
|
53
44
|
isValidPrivateKey(privateKey: cutils.PrivKey): boolean;
|
|
54
45
|
hashToPrivateKey: (hash: cutils.Hex) => Uint8Array;
|
|
55
46
|
randomPrivateKey: () => Uint8Array;
|
|
56
47
|
};
|
|
57
|
-
export { CURVE,
|
|
48
|
+
export { CURVE, Signature, ProjectivePoint, getPublicKey0x as getPublicKey, getSharedSecret0x as getSharedSecret, sign0x as sign, verify0x as verify, };
|
|
58
49
|
export declare const bytesToHexEth: (uint8a: Uint8Array) => string;
|
|
59
50
|
export declare const strip0x: (hex: string) => string;
|
|
60
51
|
export declare const numberToHexEth: (num: bigint | number) => string;
|
|
@@ -68,3 +59,29 @@ export declare function pedersen(x: PedersenArg, y: PedersenArg): string;
|
|
|
68
59
|
export declare function hashChain(data: PedersenArg[], fn?: typeof pedersen): PedersenArg;
|
|
69
60
|
export declare const computeHashOnElements: (data: PedersenArg[], fn?: typeof pedersen) => PedersenArg;
|
|
70
61
|
export declare const keccak: (data: Uint8Array) => bigint;
|
|
62
|
+
export declare const Fp253: Readonly<Field<bigint> & Required<Pick<Field<bigint>, "isOdd">>>;
|
|
63
|
+
export declare const Fp251: Readonly<Field<bigint> & Required<Pick<Field<bigint>, "isOdd">>>;
|
|
64
|
+
export declare function _poseidonMDS(Fp: Field<bigint>, name: string, m: number, attempt?: number): bigint[][];
|
|
65
|
+
export declare type PoseidonOpts = {
|
|
66
|
+
Fp: Field<bigint>;
|
|
67
|
+
rate: number;
|
|
68
|
+
capacity: number;
|
|
69
|
+
roundsFull: number;
|
|
70
|
+
roundsPartial: number;
|
|
71
|
+
};
|
|
72
|
+
export declare function poseidonBasic(opts: PoseidonOpts, mds: bigint[][]): {
|
|
73
|
+
(values: bigint[]): bigint[];
|
|
74
|
+
roundConstants: bigint[][];
|
|
75
|
+
};
|
|
76
|
+
export declare function poseidonCreate(opts: PoseidonOpts, mdsAttempt?: number): {
|
|
77
|
+
(values: bigint[]): bigint[];
|
|
78
|
+
roundConstants: bigint[][];
|
|
79
|
+
};
|
|
80
|
+
export declare const poseidonSmall: {
|
|
81
|
+
(values: bigint[]): bigint[];
|
|
82
|
+
roundConstants: bigint[][];
|
|
83
|
+
};
|
|
84
|
+
export declare function poseidonHash(x: bigint, y: bigint, fn?: {
|
|
85
|
+
(values: bigint[]): bigint[];
|
|
86
|
+
roundConstants: bigint[][];
|
|
87
|
+
}): bigint;
|