@noble/curves 0.5.2 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/README.md +115 -41
  2. package/lib/_shortw_utils.d.ts +13 -24
  3. package/lib/abstract/bls.d.ts +39 -32
  4. package/lib/abstract/bls.js +74 -73
  5. package/lib/abstract/{group.d.ts → curve.d.ts} +30 -1
  6. package/lib/abstract/{group.js → curve.js} +33 -2
  7. package/lib/abstract/edwards.d.ts +30 -72
  8. package/lib/abstract/edwards.js +206 -389
  9. package/lib/abstract/hash-to-curve.d.ts +25 -6
  10. package/lib/abstract/hash-to-curve.js +40 -12
  11. package/lib/abstract/modular.d.ts +21 -8
  12. package/lib/abstract/modular.js +72 -48
  13. package/lib/abstract/montgomery.js +23 -68
  14. package/lib/abstract/poseidon.d.ts +29 -0
  15. package/lib/abstract/poseidon.js +115 -0
  16. package/lib/abstract/utils.d.ts +9 -37
  17. package/lib/abstract/utils.js +61 -87
  18. package/lib/abstract/weierstrass.d.ts +58 -81
  19. package/lib/abstract/weierstrass.js +485 -679
  20. package/lib/bls12-381.js +63 -58
  21. package/lib/bn.js +1 -1
  22. package/lib/ed25519.d.ts +7 -5
  23. package/lib/ed25519.js +82 -79
  24. package/lib/ed448.d.ts +3 -0
  25. package/lib/ed448.js +86 -83
  26. package/lib/esm/abstract/bls.js +75 -74
  27. package/lib/esm/abstract/{group.js → curve.js} +31 -1
  28. package/lib/esm/abstract/edwards.js +204 -387
  29. package/lib/esm/abstract/hash-to-curve.js +38 -11
  30. package/lib/esm/abstract/modular.js +69 -47
  31. package/lib/esm/abstract/montgomery.js +24 -69
  32. package/lib/esm/abstract/poseidon.js +109 -0
  33. package/lib/esm/abstract/utils.js +58 -82
  34. package/lib/esm/abstract/weierstrass.js +484 -678
  35. package/lib/esm/bls12-381.js +75 -70
  36. package/lib/esm/bn.js +1 -1
  37. package/lib/esm/ed25519.js +80 -78
  38. package/lib/esm/ed448.js +84 -82
  39. package/lib/esm/jubjub.js +1 -1
  40. package/lib/esm/p224.js +1 -1
  41. package/lib/esm/p256.js +11 -9
  42. package/lib/esm/p384.js +11 -9
  43. package/lib/esm/p521.js +12 -23
  44. package/lib/esm/secp256k1.js +124 -162
  45. package/lib/esm/stark.js +105 -41
  46. package/lib/jubjub.d.ts +2 -2
  47. package/lib/jubjub.js +1 -1
  48. package/lib/p192.d.ts +26 -48
  49. package/lib/p224.d.ts +26 -48
  50. package/lib/p224.js +1 -1
  51. package/lib/p256.d.ts +29 -48
  52. package/lib/p256.js +13 -10
  53. package/lib/p384.d.ts +29 -48
  54. package/lib/p384.js +13 -10
  55. package/lib/p521.d.ts +37 -57
  56. package/lib/p521.js +14 -24
  57. package/lib/secp256k1.d.ts +37 -46
  58. package/lib/secp256k1.js +124 -162
  59. package/lib/stark.d.ts +39 -22
  60. package/lib/stark.js +108 -41
  61. package/package.json +15 -10
package/lib/secp256k1.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.schnorr = exports.taggedHash = exports.secp256k1 = void 0;
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 hash_to_curve_js_1 = require("./abstract/hash-to-curve.js");
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
- * Allows to compute square root √y 2x faster.
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.equals(Fp.square(root), y))
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 numTo32b = exports.secp256k1.utils._bigintToBytes;
148
- const numTo32bStr = exports.secp256k1.utils._bigintToString;
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,72 +115,55 @@ 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
- const toRawX = (point) => point.toRawBytes(true).slice(1);
195
- // Schnorr signatures are superior to ECDSA from above.
196
- // Below is Schnorr-specific code as per BIP0340.
197
- function schnorrChallengeFinalize(ch) {
198
- return (0, modular_js_1.mod)((0, utils_js_1.bytesToNumberBE)(ch), exports.secp256k1.CURVE.n);
118
+ const pointToBytes = (point) => point.toRawBytes(true).slice(1);
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 GmulAdd = (Q, a, b) => Point.BASE.multiplyAndAddUnsafe(Q, a, b);
123
+ const hex32ToInt = (key) => (0, utils_js_1.bytesToNumberBE)((0, utils_js_1.ensureBytes)(key, 32));
124
+ function schnorrGetExtPubKey(priv) {
125
+ let d = typeof priv === 'bigint' ? priv : hex32ToInt(priv);
126
+ const point = Point.fromPrivateKey(d); // P = d'⋅G; 0 < d' < n check is done inside
127
+ const scalar = point.hasEvenY() ? d : modN(-d); // d = d' if has_even_y(P), otherwise d = n-d'
128
+ return { point, scalar, bytes: pointToBytes(point) };
199
129
  }
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
- }
130
+ function lift_x(x) {
131
+ if (!fe(x))
132
+ throw new Error('bad x: need 0 < x < p'); // Fail if x ≥ p.
133
+ const c = (0, modular_js_1.mod)(x * x * x + BigInt(7), secp256k1P); // Let c = x³ + 7 mod p.
134
+ let y = sqrtMod(c); // Let y = c^(p+1)/4 mod p.
135
+ if (y % 2n !== 0n)
136
+ y = (0, modular_js_1.mod)(-y, secp256k1P); // Return the unique point P such that x(P) = x and
137
+ const p = new Point(x, y, _1n); // y(P) = y if y mod 2 = 0 or y(P) = p-y otherwise.
138
+ p.assertValidity();
139
+ return p;
227
140
  }
228
- function schnorrGetScalar(priv) {
229
- const point = exports.secp256k1.Point.fromPrivateKey(priv);
230
- const scalar = point.hasEvenY() ? priv : exports.secp256k1.CURVE.n - priv;
231
- return { point, scalar, x: toRawX(point) };
141
+ function challenge(...args) {
142
+ return modN((0, utils_js_1.bytesToNumberBE)(taggedHash(TAGS.challenge, ...args)));
232
143
  }
233
- /**
234
- * Synchronously creates Schnorr signature. Improved security: verifies itself before
235
- * producing an output.
236
- * @param msg message (not message hash)
237
- * @param privateKey private key
238
- * @param auxRand random bytes that would be added to k. Bad RNG won't break it.
239
- */
144
+ // Schnorr's pubkey is just `x` of Point (BIP340)
145
+ function schnorrGetPublicKey(privateKey) {
146
+ return schnorrGetExtPubKey(privateKey).bytes; // d'=int(sk). Fail if d'=0 or d'≥n. Ret bytes(d'⋅G)
147
+ }
148
+ // Creates Schnorr signature as per BIP340. Verifies itself before returning anything.
149
+ // auxRand is optional and is not the sole source of k generation: bad CSPRNG won't be dangerous
240
150
  function schnorrSign(message, privateKey, auxRand = (0, utils_1.randomBytes)(32)) {
241
151
  if (message == null)
242
- throw new TypeError(`sign: Expected valid message, not "${message}"`);
243
- const m = (0, utils_js_1.ensureBytes)(message);
244
- // checks for isWithinCurveOrder
245
- const { x: px, scalar: d } = schnorrGetScalar(normalizePrivateKey(privateKey));
246
- const rand = (0, utils_js_1.ensureBytes)(auxRand);
247
- if (rand.length !== 32)
248
- throw new TypeError('sign: Expected 32 bytes of aux randomness');
249
- const tag = taggedHash;
250
- const t0h = tag(TAGS.aux, rand);
251
- const t = numTo32b(d ^ (0, utils_js_1.bytesToNumberBE)(t0h));
252
- const k0h = tag(TAGS.nonce, t, px, m);
253
- const k0 = (0, modular_js_1.mod)((0, utils_js_1.bytesToNumberBE)(k0h), exports.secp256k1.CURVE.n);
254
- if (k0 === _0n)
255
- throw new Error('sign: Creation of signature failed. k is zero');
256
- const { point: R, x: rx, scalar: k } = schnorrGetScalar(k0);
257
- const e = schnorrChallengeFinalize(tag(TAGS.challenge, rx, px, m));
258
- const sig = new SchnorrSignature(R.x, (0, modular_js_1.mod)(k + e * d, exports.secp256k1.CURVE.n)).toRawBytes();
152
+ throw new Error(`sign: Expected valid message, not "${message}"`);
153
+ const m = (0, utils_js_1.ensureBytes)(message); // checks for isWithinCurveOrder
154
+ const { bytes: px, scalar: d } = schnorrGetExtPubKey(privateKey);
155
+ const a = (0, utils_js_1.ensureBytes)(auxRand, 32); // Auxiliary random data a: a 32-byte array
156
+ 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)
157
+ const rand = taggedHash(TAGS.nonce, t, px, m); // Let rand = hash/nonce(t || bytes(P) || m)
158
+ const k_ = modN((0, utils_js_1.bytesToNumberBE)(rand)); // Let k' = int(rand) mod n
159
+ if (k_ === _0n)
160
+ throw new Error('sign failed: k is zero'); // Fail if k' = 0.
161
+ const { point: R, bytes: rx, scalar: k } = schnorrGetExtPubKey(k_); // Let R = k'⋅G.
162
+ const e = challenge(rx, px, m); // Let e = int(hash/challenge(bytes(R) || bytes(P) || m)) mod n.
163
+ const sig = new Uint8Array(64); // Let sig = bytes(R) || bytes((k + ed) mod n).
164
+ sig.set(numTo32b(R.px), 0);
165
+ sig.set(numTo32b(modN(k + e * d)), 32);
166
+ // If Verify(bytes(P), m, sig) (see below) returns failure, abort
259
167
  if (!schnorrVerify(sig, m, px))
260
168
  throw new Error('sign: Invalid signature produced');
261
169
  return sig;
@@ -265,30 +173,84 @@ function schnorrSign(message, privateKey, auxRand = (0, utils_1.randomBytes)(32)
265
173
  */
266
174
  function schnorrVerify(signature, message, publicKey) {
267
175
  try {
268
- const raw = signature instanceof SchnorrSignature;
269
- const sig = raw ? signature : SchnorrSignature.fromHex(signature);
270
- if (raw)
271
- sig.assertValidity(); // just in case
272
- const { r, s } = sig;
273
- const m = (0, utils_js_1.ensureBytes)(message);
274
- const P = normalizePublicKey(publicKey);
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)
176
+ const P = lift_x(hex32ToInt(publicKey)); // P = lift_x(int(pk)); fail if that fails
177
+ const sig = (0, utils_js_1.ensureBytes)(signature, 64);
178
+ const r = (0, utils_js_1.bytesToNumberBE)(sig.subarray(0, 32)); // Let r = int(sig[0:32]); fail if r ≥ p.
179
+ if (!fe(r))
180
+ return false;
181
+ const s = (0, utils_js_1.bytesToNumberBE)(sig.subarray(32, 64)); // Let s = int(sig[32:64]); fail if s ≥ n.
182
+ if (!ge(s))
281
183
  return false;
282
- return true;
184
+ const m = (0, utils_js_1.ensureBytes)(message);
185
+ const e = challenge(numTo32b(r), pointToBytes(P), m); // int(challenge(bytes(r)||bytes(P)||m)) mod n
186
+ const R = GmulAdd(P, s, modN(-e)); // R = s⋅G - e⋅P
187
+ if (!R || !R.hasEvenY() || R.toAffine().x !== r)
188
+ return false; // -eP == (n-e)P
189
+ return true; // Fail if is_infinite(R) / not has_even_y(R) / x(R) ≠ r.
283
190
  }
284
191
  catch (error) {
285
192
  return false;
286
193
  }
287
194
  }
288
195
  exports.schnorr = {
289
- Signature: SchnorrSignature,
290
- // Schnorr's pubkey is just `x` of Point (BIP340)
291
- getPublicKey: (privateKey) => toRawX(exports.secp256k1.Point.fromPrivateKey(privateKey)),
196
+ getPublicKey: schnorrGetPublicKey,
292
197
  sign: schnorrSign,
293
198
  verify: schnorrVerify,
199
+ utils: {
200
+ getExtendedPublicKey: schnorrGetExtPubKey,
201
+ lift_x,
202
+ pointToBytes,
203
+ numberToBytesBE: utils_js_1.numberToBytesBE,
204
+ bytesToNumberBE: utils_js_1.bytesToNumberBE,
205
+ taggedHash,
206
+ mod: modular_js_1.mod,
207
+ },
294
208
  };
209
+ const isoMap = htf.isogenyMap(Fp, [
210
+ // xNum
211
+ [
212
+ '0x8e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38daaaaa8c7',
213
+ '0x7d3d4c80bc321d5b9f315cea7fd44c5d595d2fc0bf63b92dfff1044f17c6581',
214
+ '0x534c328d23f234e6e2a413deca25caece4506144037c40314ecbd0b53d9dd262',
215
+ '0x8e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38daaaaa88c',
216
+ ],
217
+ // xDen
218
+ [
219
+ '0xd35771193d94918a9ca34ccbb7b640dd86cd409542f8487d9fe6b745781eb49b',
220
+ '0xedadc6f64383dc1df7c4b2d51b54225406d36b641f5e41bbc52a56612a8c6d14',
221
+ '0x0000000000000000000000000000000000000000000000000000000000000001', // LAST 1
222
+ ],
223
+ // yNum
224
+ [
225
+ '0x4bda12f684bda12f684bda12f684bda12f684bda12f684bda12f684b8e38e23c',
226
+ '0xc75e0c32d5cb7c0fa9d0a54b12a0a6d5647ab046d686da6fdffc90fc201d71a3',
227
+ '0x29a6194691f91a73715209ef6512e576722830a201be2018a765e85a9ecee931',
228
+ '0x2f684bda12f684bda12f684bda12f684bda12f684bda12f684bda12f38e38d84',
229
+ ],
230
+ // yDen
231
+ [
232
+ '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffff93b',
233
+ '0x7a06534bb8bdb49fd5e9e6632722c2989467c1bfc8e8d978dfb425d2685c2573',
234
+ '0x6484aa716545ca2cf3a70c3fa8fe337e0a3d21162f0d6299a7bf8192bfd2a76f',
235
+ '0x0000000000000000000000000000000000000000000000000000000000000001', // LAST 1
236
+ ],
237
+ ].map((i) => i.map((j) => BigInt(j))));
238
+ const mapSWU = (0, weierstrass_js_1.mapToCurveSimpleSWU)(Fp, {
239
+ A: BigInt('0x3f8731abdd661adca08a5558f0f5d272e953d363cb6f0e5d405447c01a444533'),
240
+ B: BigInt('1771'),
241
+ Z: Fp.create(BigInt('-11')),
242
+ });
243
+ const { hashToCurve, encodeToCurve } = htf.hashToCurve(exports.secp256k1.ProjectivePoint, (scalars) => {
244
+ const { x, y } = mapSWU(Fp.create(scalars[0]));
245
+ return isoMap(x, y);
246
+ }, {
247
+ DST: 'secp256k1_XMD:SHA-256_SSWU_RO_',
248
+ encodeDST: 'secp256k1_XMD:SHA-256_SSWU_NU_',
249
+ p: Fp.ORDER,
250
+ m: 1,
251
+ k: 128,
252
+ expand: 'xmd',
253
+ hash: sha256_1.sha256,
254
+ });
255
+ exports.hashToCurve = hashToCurve;
256
+ exports.encodeToCurve = encodeToCurve;
package/lib/stark.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- import { ProjectivePointType } from './abstract/weierstrass.js';
1
+ import { ProjPointType } from './abstract/weierstrass.js';
2
2
  import * as cutils from './abstract/utils.js';
3
- declare type ProjectivePoint = ProjectivePointType<bigint>;
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,17 +10,17 @@ 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: import("./abstract/modular.js").Field<bigint>;
13
+ readonly Fp: Field<bigint>;
13
14
  readonly n: bigint;
14
15
  readonly h: bigint;
15
16
  readonly hEff?: bigint | undefined;
16
17
  readonly Gx: bigint;
17
18
  readonly Gy: bigint;
18
- readonly wrapPrivateKey?: boolean | undefined;
19
19
  readonly allowInfinityPoint?: boolean | undefined;
20
20
  readonly a: bigint;
21
21
  readonly b: bigint;
22
- readonly normalizePrivateKey?: ((key: cutils.PrivKey) => cutils.PrivKey) | undefined;
22
+ readonly allowedPrivateKeyLengths?: readonly number[] | undefined;
23
+ readonly wrapPrivateKey?: boolean | undefined;
23
24
  readonly endo?: {
24
25
  beta: bigint;
25
26
  splitScalar: (k: bigint) => {
@@ -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").ProjectiveConstructor<bigint>, point: ProjectivePointType<bigint>) => boolean) | undefined;
33
- readonly clearCofactor?: ((c: import("./abstract/weierstrass.js").ProjectiveConstructor<bigint>, point: ProjectivePointType<bigint>) => ProjectivePointType<bigint>) | undefined;
34
- readonly htfDefaults?: import("./abstract/hash-to-curve.js").htfOpts | undefined;
35
- readonly mapToCurve?: ((scalar: bigint[]) => {
36
- x: bigint;
37
- y: bigint;
38
- }) | undefined;
39
- lowS: boolean;
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;
40
35
  readonly hash: cutils.CHash;
41
36
  readonly hmac: (key: Uint8Array, ...messages: Uint8Array[]) => Uint8Array;
42
37
  readonly randomBytes: (bytesLength?: number | undefined) => Uint8Array;
43
- readonly truncateHash?: ((hash: Uint8Array, truncateOnly?: boolean | undefined) => bigint) | undefined;
44
- }>, Point: import("./abstract/weierstrass.js").PointConstructor<bigint>, ProjectivePoint: import("./abstract/weierstrass.js").ProjectiveConstructor<bigint>, Signature: import("./abstract/weierstrass.js").SignatureConstructor;
38
+ lowS: boolean;
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, Point, Signature, ProjectivePoint, getPublicKey0x as getPublicKey, getSharedSecret0x as getSharedSecret, sign0x as sign, verify0x as verify, };
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;