@noble/curves 0.6.4 → 0.7.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 (172) hide show
  1. package/README.md +486 -298
  2. package/{lib/_shortw_utils.d.ts → _shortw_utils.d.ts} +1 -1
  3. package/_shortw_utils.d.ts.map +1 -0
  4. package/{lib/_shortw_utils.js → _shortw_utils.js} +2 -0
  5. package/_shortw_utils.js.map +1 -0
  6. package/{lib/abstract → abstract}/bls.d.ts +4 -9
  7. package/abstract/bls.d.ts.map +1 -0
  8. package/{lib/abstract → abstract}/bls.js +13 -26
  9. package/abstract/bls.js.map +1 -0
  10. package/{lib/abstract → abstract}/curve.d.ts +1 -0
  11. package/abstract/curve.d.ts.map +1 -0
  12. package/{lib/abstract → abstract}/curve.js +1 -0
  13. package/abstract/curve.js.map +1 -0
  14. package/{lib/abstract → abstract}/edwards.d.ts +1 -0
  15. package/abstract/edwards.d.ts.map +1 -0
  16. package/{lib/abstract → abstract}/edwards.js +9 -15
  17. package/abstract/edwards.js.map +1 -0
  18. package/{lib/abstract → abstract}/hash-to-curve.d.ts +5 -5
  19. package/abstract/hash-to-curve.d.ts.map +1 -0
  20. package/{lib/abstract → abstract}/hash-to-curve.js +41 -38
  21. package/abstract/hash-to-curve.js.map +1 -0
  22. package/{lib/abstract → abstract}/modular.d.ts +1 -0
  23. package/abstract/modular.d.ts.map +1 -0
  24. package/{lib/abstract → abstract}/modular.js +2 -1
  25. package/abstract/modular.js.map +1 -0
  26. package/{lib/abstract → abstract}/montgomery.d.ts +4 -3
  27. package/abstract/montgomery.d.ts.map +1 -0
  28. package/{lib/abstract → abstract}/montgomery.js +12 -9
  29. package/abstract/montgomery.js.map +1 -0
  30. package/{lib/abstract → abstract}/poseidon.d.ts +1 -0
  31. package/abstract/poseidon.d.ts.map +1 -0
  32. package/{lib/abstract → abstract}/poseidon.js +1 -0
  33. package/abstract/poseidon.js.map +1 -0
  34. package/{lib/abstract → abstract}/utils.d.ts +12 -1
  35. package/abstract/utils.d.ts.map +1 -0
  36. package/{lib/abstract → abstract}/utils.js +96 -10
  37. package/abstract/utils.js.map +1 -0
  38. package/{lib/abstract → abstract}/weierstrass.d.ts +6 -6
  39. package/abstract/weierstrass.d.ts.map +1 -0
  40. package/{lib/abstract → abstract}/weierstrass.js +74 -115
  41. package/abstract/weierstrass.js.map +1 -0
  42. package/{lib/bls12-381.d.ts → bls12-381.d.ts} +1 -0
  43. package/bls12-381.d.ts.map +1 -0
  44. package/{lib/bls12-381.js → bls12-381.js} +41 -7
  45. package/bls12-381.js.map +1 -0
  46. package/{lib/bn.d.ts → bn.d.ts} +1 -0
  47. package/bn.d.ts.map +1 -0
  48. package/{lib/bn.js → bn.js} +1 -0
  49. package/bn.js.map +1 -0
  50. package/{lib/ed25519.d.ts → ed25519.d.ts} +2 -1
  51. package/ed25519.d.ts.map +1 -0
  52. package/{lib/ed25519.js → ed25519.js} +6 -5
  53. package/ed25519.js.map +1 -0
  54. package/{lib/ed448.d.ts → ed448.d.ts} +2 -1
  55. package/ed448.d.ts.map +1 -0
  56. package/{lib/ed448.js → ed448.js} +4 -3
  57. package/ed448.js.map +1 -0
  58. package/{lib/esm → esm}/_shortw_utils.js +2 -0
  59. package/esm/_shortw_utils.js.map +1 -0
  60. package/{lib/esm → esm}/abstract/bls.js +14 -27
  61. package/esm/abstract/bls.js.map +1 -0
  62. package/{lib/esm → esm}/abstract/curve.js +1 -0
  63. package/esm/abstract/curve.js.map +1 -0
  64. package/{lib/esm → esm}/abstract/edwards.js +9 -15
  65. package/esm/abstract/edwards.js.map +1 -0
  66. package/{lib/esm → esm}/abstract/hash-to-curve.js +40 -36
  67. package/esm/abstract/hash-to-curve.js.map +1 -0
  68. package/{lib/esm → esm}/abstract/modular.js +2 -1
  69. package/esm/abstract/modular.js.map +1 -0
  70. package/{lib/esm → esm}/abstract/montgomery.js +12 -9
  71. package/esm/abstract/montgomery.js.map +1 -0
  72. package/{lib/esm → esm}/abstract/poseidon.js +1 -0
  73. package/esm/abstract/poseidon.js.map +1 -0
  74. package/{lib/esm → esm}/abstract/utils.js +93 -9
  75. package/esm/abstract/utils.js.map +1 -0
  76. package/{lib/esm → esm}/abstract/weierstrass.js +74 -115
  77. package/esm/abstract/weierstrass.js.map +1 -0
  78. package/{lib/esm → esm}/bls12-381.js +41 -7
  79. package/esm/bls12-381.js.map +1 -0
  80. package/{lib/esm → esm}/bn.js +1 -0
  81. package/esm/bn.js.map +1 -0
  82. package/{lib/esm → esm}/ed25519.js +7 -6
  83. package/esm/ed25519.js.map +1 -0
  84. package/{lib/esm → esm}/ed448.js +4 -3
  85. package/esm/ed448.js.map +1 -0
  86. package/{lib → esm}/index.js +1 -0
  87. package/esm/index.js.map +1 -0
  88. package/{lib/esm → esm}/jubjub.js +1 -0
  89. package/esm/jubjub.js.map +1 -0
  90. package/{lib/esm → esm}/p192.js +1 -0
  91. package/esm/p192.js.map +1 -0
  92. package/{lib/esm → esm}/p224.js +1 -0
  93. package/esm/p224.js.map +1 -0
  94. package/{lib/esm → esm}/p256.js +2 -1
  95. package/esm/p256.js.map +1 -0
  96. package/{lib/esm → esm}/p384.js +2 -1
  97. package/esm/p384.js.map +1 -0
  98. package/{lib/esm → esm}/p521.js +2 -1
  99. package/esm/p521.js.map +1 -0
  100. package/{lib/esm → esm}/package.json +0 -0
  101. package/{lib/esm → esm}/pasta.js +1 -0
  102. package/esm/pasta.js.map +1 -0
  103. package/{lib/esm → esm}/secp256k1.js +51 -50
  104. package/esm/secp256k1.js.map +1 -0
  105. package/{lib/esm → esm}/stark.js +5 -4
  106. package/esm/stark.js.map +1 -0
  107. package/index.d.ts +1 -0
  108. package/index.d.ts.map +1 -0
  109. package/index.js +3 -0
  110. package/index.js.map +1 -0
  111. package/{lib/jubjub.d.ts → jubjub.d.ts} +1 -0
  112. package/jubjub.d.ts.map +1 -0
  113. package/{lib/jubjub.js → jubjub.js} +1 -0
  114. package/jubjub.js.map +1 -0
  115. package/{lib/p192.d.ts → p192.d.ts} +1 -2
  116. package/p192.d.ts.map +1 -0
  117. package/{lib/p192.js → p192.js} +1 -0
  118. package/p192.js.map +1 -0
  119. package/{lib/p224.d.ts → p224.d.ts} +1 -2
  120. package/p224.d.ts.map +1 -0
  121. package/{lib/p224.js → p224.js} +1 -0
  122. package/p224.js.map +1 -0
  123. package/{lib/p256.d.ts → p256.d.ts} +2 -3
  124. package/p256.d.ts.map +1 -0
  125. package/{lib/p256.js → p256.js} +2 -1
  126. package/p256.js.map +1 -0
  127. package/{lib/p384.d.ts → p384.d.ts} +2 -3
  128. package/p384.d.ts.map +1 -0
  129. package/{lib/p384.js → p384.js} +2 -1
  130. package/p384.js.map +1 -0
  131. package/{lib/p521.d.ts → p521.d.ts} +2 -3
  132. package/p521.d.ts.map +1 -0
  133. package/{lib/p521.js → p521.js} +2 -1
  134. package/p521.js.map +1 -0
  135. package/package.json +84 -79
  136. package/{lib/pasta.d.ts → pasta.d.ts} +1 -0
  137. package/pasta.d.ts.map +1 -0
  138. package/{lib/pasta.js → pasta.js} +1 -0
  139. package/pasta.js.map +1 -0
  140. package/{lib/secp256k1.d.ts → secp256k1.d.ts} +20 -6
  141. package/secp256k1.d.ts.map +1 -0
  142. package/{lib/secp256k1.js → secp256k1.js} +48 -47
  143. package/secp256k1.js.map +1 -0
  144. package/src/_shortw_utils.ts +20 -0
  145. package/src/abstract/bls.ts +376 -0
  146. package/src/abstract/curve.ts +199 -0
  147. package/src/abstract/edwards.ts +479 -0
  148. package/src/abstract/hash-to-curve.ts +220 -0
  149. package/src/abstract/modular.ts +417 -0
  150. package/src/abstract/montgomery.ts +186 -0
  151. package/src/abstract/poseidon.ts +119 -0
  152. package/src/abstract/utils.ts +246 -0
  153. package/src/abstract/weierstrass.ts +1177 -0
  154. package/src/bls12-381.ts +1274 -0
  155. package/src/bn.ts +21 -0
  156. package/src/ed25519.ts +428 -0
  157. package/src/ed448.ts +241 -0
  158. package/{lib/esm/index.js → src/index.ts} +0 -1
  159. package/src/jubjub.ts +58 -0
  160. package/src/p192.ts +25 -0
  161. package/src/p224.ts +25 -0
  162. package/src/p256.ts +53 -0
  163. package/src/p384.ts +57 -0
  164. package/src/p521.ts +57 -0
  165. package/src/pasta.ts +31 -0
  166. package/src/secp256k1.ts +270 -0
  167. package/src/stark.ts +356 -0
  168. package/{lib/stark.d.ts → stark.d.ts} +1 -1
  169. package/stark.d.ts.map +1 -0
  170. package/{lib/stark.js → stark.js} +5 -4
  171. package/stark.js.map +1 -0
  172. package/lib/index.d.ts +0 -0
@@ -0,0 +1,270 @@
1
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2
+ import { sha256 } from '@noble/hashes/sha256';
3
+ import { randomBytes } from '@noble/hashes/utils';
4
+ import { Fp as Field, mod, pow2 } from './abstract/modular.js';
5
+ import { ProjPointType as PointType, mapToCurveSimpleSWU } from './abstract/weierstrass.js';
6
+ import type { Hex, PrivKey } from './abstract/utils.js';
7
+ import { bytesToNumberBE, concatBytes, ensureBytes, numberToBytesBE } from './abstract/utils.js';
8
+ import * as htf from './abstract/hash-to-curve.js';
9
+ import { createCurve } from './_shortw_utils.js';
10
+
11
+ const secp256k1P = BigInt('0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f');
12
+ const secp256k1N = BigInt('0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141');
13
+ const _1n = BigInt(1);
14
+ const _2n = BigInt(2);
15
+ const divNearest = (a: bigint, b: bigint) => (a + b / _2n) / b;
16
+
17
+ /**
18
+ * √n = n^((p+1)/4) for fields p = 3 mod 4. We unwrap the loop and multiply bit-by-bit.
19
+ * (P+1n/4n).toString(2) would produce bits [223x 1, 0, 22x 1, 4x 0, 11, 00]
20
+ */
21
+ function sqrtMod(y: bigint): bigint {
22
+ const P = secp256k1P;
23
+ // prettier-ignore
24
+ const _3n = BigInt(3), _6n = BigInt(6), _11n = BigInt(11), _22n = BigInt(22);
25
+ // prettier-ignore
26
+ const _23n = BigInt(23), _44n = BigInt(44), _88n = BigInt(88);
27
+ const b2 = (y * y * y) % P; // x^3, 11
28
+ const b3 = (b2 * b2 * y) % P; // x^7
29
+ const b6 = (pow2(b3, _3n, P) * b3) % P;
30
+ const b9 = (pow2(b6, _3n, P) * b3) % P;
31
+ const b11 = (pow2(b9, _2n, P) * b2) % P;
32
+ const b22 = (pow2(b11, _11n, P) * b11) % P;
33
+ const b44 = (pow2(b22, _22n, P) * b22) % P;
34
+ const b88 = (pow2(b44, _44n, P) * b44) % P;
35
+ const b176 = (pow2(b88, _88n, P) * b88) % P;
36
+ const b220 = (pow2(b176, _44n, P) * b44) % P;
37
+ const b223 = (pow2(b220, _3n, P) * b3) % P;
38
+ const t1 = (pow2(b223, _23n, P) * b22) % P;
39
+ const t2 = (pow2(t1, _6n, P) * b2) % P;
40
+ const root = pow2(t2, _2n, P);
41
+ if (!Fp.eql(Fp.sqr(root), y)) throw new Error('Cannot find square root');
42
+ return root;
43
+ }
44
+
45
+ const Fp = Field(secp256k1P, undefined, undefined, { sqrt: sqrtMod });
46
+ type Fp = bigint;
47
+
48
+ export const secp256k1 = createCurve(
49
+ {
50
+ a: BigInt(0), // equation params: a, b
51
+ b: BigInt(7), // Seem to be rigid: bitcointalk.org/index.php?topic=289795.msg3183975#msg3183975
52
+ Fp, // Field's prime: 2n**256n - 2n**32n - 2n**9n - 2n**8n - 2n**7n - 2n**6n - 2n**4n - 1n
53
+ n: secp256k1N, // Curve order, total count of valid points in the field
54
+ // Base point (x, y) aka generator point
55
+ Gx: BigInt('55066263022277343669578718895168534326250603453777594175500187360389116729240'),
56
+ Gy: BigInt('32670510020758816978083085130507043184471273380659243275938904335757337482424'),
57
+ h: BigInt(1), // Cofactor
58
+ lowS: true, // Allow only low-S signatures by default in sign() and verify()
59
+ /**
60
+ * secp256k1 belongs to Koblitz curves: it has efficiently computable endomorphism.
61
+ * Endomorphism uses 2x less RAM, speeds up precomputation by 2x and ECDH / key recovery by 20%.
62
+ * For precomputed wNAF it trades off 1/2 init time & 1/3 ram for 20% perf hit.
63
+ * Explanation: https://gist.github.com/paulmillr/eb670806793e84df628a7c434a873066
64
+ */
65
+ endo: {
66
+ beta: BigInt('0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee'),
67
+ splitScalar: (k: bigint) => {
68
+ const n = secp256k1N;
69
+ const a1 = BigInt('0x3086d221a7d46bcde86c90e49284eb15');
70
+ const b1 = -_1n * BigInt('0xe4437ed6010e88286f547fa90abfe4c3');
71
+ const a2 = BigInt('0x114ca50f7a8e2f3f657c1108d9d44cfd8');
72
+ const b2 = a1;
73
+ const POW_2_128 = BigInt('0x100000000000000000000000000000000'); // (2n**128n).toString(16)
74
+
75
+ const c1 = divNearest(b2 * k, n);
76
+ const c2 = divNearest(-b1 * k, n);
77
+ let k1 = mod(k - c1 * a1 - c2 * a2, n);
78
+ let k2 = mod(-c1 * b1 - c2 * b2, n);
79
+ const k1neg = k1 > POW_2_128;
80
+ const k2neg = k2 > POW_2_128;
81
+ if (k1neg) k1 = n - k1;
82
+ if (k2neg) k2 = n - k2;
83
+ if (k1 > POW_2_128 || k2 > POW_2_128) {
84
+ throw new Error('splitScalar: Endomorphism failed, k=' + k);
85
+ }
86
+ return { k1neg, k1, k2neg, k2 };
87
+ },
88
+ },
89
+ },
90
+ sha256
91
+ );
92
+
93
+ // Schnorr signatures are superior to ECDSA from above. Below is Schnorr-specific BIP0340 code.
94
+ // https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki
95
+ const _0n = BigInt(0);
96
+ const fe = (x: bigint) => typeof x === 'bigint' && _0n < x && x < secp256k1P;
97
+ const ge = (x: bigint) => typeof x === 'bigint' && _0n < x && x < secp256k1N;
98
+ /** An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)] */
99
+ const TAGGED_HASH_PREFIXES: { [tag: string]: Uint8Array } = {};
100
+ function taggedHash(tag: string, ...messages: Uint8Array[]): Uint8Array {
101
+ let tagP = TAGGED_HASH_PREFIXES[tag];
102
+ if (tagP === undefined) {
103
+ const tagH = sha256(Uint8Array.from(tag, (c) => c.charCodeAt(0)));
104
+ tagP = concatBytes(tagH, tagH);
105
+ TAGGED_HASH_PREFIXES[tag] = tagP;
106
+ }
107
+ return sha256(concatBytes(tagP, ...messages));
108
+ }
109
+
110
+ // ECDSA compact points are 33-byte. Schnorr is 32: we strip first byte 0x02 or 0x03
111
+ const pointToBytes = (point: PointType<bigint>) => point.toRawBytes(true).slice(1);
112
+ const numTo32b = (n: bigint) => numberToBytesBE(n, 32);
113
+ const modP = (x: bigint) => mod(x, secp256k1P);
114
+ const modN = (x: bigint) => mod(x, secp256k1N);
115
+ const Point = secp256k1.ProjectivePoint;
116
+ const GmulAdd = (Q: PointType<bigint>, a: bigint, b: bigint) =>
117
+ Point.BASE.multiplyAndAddUnsafe(Q, a, b);
118
+ // Calculate point, scalar and bytes
119
+ function schnorrGetExtPubKey(priv: PrivKey) {
120
+ const d = secp256k1.utils.normPrivateKeyToScalar(priv); // same method executed in fromPrivateKey
121
+ const point = Point.fromPrivateKey(d); // P = d'⋅G; 0 < d' < n check is done inside
122
+ const scalar = point.hasEvenY() ? d : modN(-d); // d = d' if has_even_y(P), otherwise d = n-d'
123
+ return { point, scalar, bytes: pointToBytes(point) };
124
+ }
125
+ /**
126
+ * lift_x from BIP340. Convert 32-byte x coordinate to elliptic curve point.
127
+ * @returns valid point checked for being on-curve
128
+ */
129
+ function lift_x(x: bigint): PointType<bigint> {
130
+ if (!fe(x)) throw new Error('bad x: need 0 < x < p'); // Fail if x ≥ p.
131
+ const xx = modP(x * x);
132
+ const c = modP(xx * x + BigInt(7)); // Let c = x³ + 7 mod p.
133
+ let y = sqrtMod(c); // Let y = c^(p+1)/4 mod p.
134
+ if (y % 2n !== 0n) y = modP(-y); // Return the unique point P such that x(P) = x and
135
+ const p = new Point(x, y, _1n); // y(P) = y if y mod 2 = 0 or y(P) = p-y otherwise.
136
+ p.assertValidity();
137
+ return p;
138
+ }
139
+ /**
140
+ * Create tagged hash, convert it to bigint, reduce modulo-n.
141
+ */
142
+ function challenge(...args: Uint8Array[]): bigint {
143
+ return modN(bytesToNumberBE(taggedHash('BIP0340/challenge', ...args)));
144
+ }
145
+
146
+ /**
147
+ * Schnorr public key is just `x` coordinate of Point as per BIP340.
148
+ */
149
+ function schnorrGetPublicKey(privateKey: Hex): Uint8Array {
150
+ return schnorrGetExtPubKey(privateKey).bytes; // d'=int(sk). Fail if d'=0 or d'≥n. Ret bytes(d'⋅G)
151
+ }
152
+
153
+ /**
154
+ * Creates Schnorr signature as per BIP340. Verifies itself before returning anything.
155
+ * auxRand is optional and is not the sole source of k generation: bad CSPRNG won't be dangerous.
156
+ */
157
+ function schnorrSign(
158
+ message: Hex,
159
+ privateKey: PrivKey,
160
+ auxRand: Hex = randomBytes(32)
161
+ ): Uint8Array {
162
+ const m = ensureBytes('message', message);
163
+ const { bytes: px, scalar: d } = schnorrGetExtPubKey(privateKey); // checks for isWithinCurveOrder
164
+ const a = ensureBytes('auxRand', auxRand, 32); // Auxiliary random data a: a 32-byte array
165
+ const t = numTo32b(d ^ bytesToNumberBE(taggedHash('BIP0340/aux', a))); // Let t be the byte-wise xor of bytes(d) and hash/aux(a)
166
+ const rand = taggedHash('BIP0340/nonce', t, px, m); // Let rand = hash/nonce(t || bytes(P) || m)
167
+ const k_ = modN(bytesToNumberBE(rand)); // Let k' = int(rand) mod n
168
+ if (k_ === _0n) throw new Error('sign failed: k is zero'); // Fail if k' = 0.
169
+ const { point: R, bytes: rx, scalar: k } = schnorrGetExtPubKey(k_); // Let R = k'⋅G.
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(numTo32b(R.px), 0);
173
+ sig.set(numTo32b(modN(k + e * d)), 32);
174
+ // If Verify(bytes(P), m, sig) (see below) returns failure, abort
175
+ if (!schnorrVerify(sig, m, px)) throw new Error('sign: Invalid signature produced');
176
+ return sig;
177
+ }
178
+
179
+ /**
180
+ * Verifies Schnorr signature.
181
+ * Will swallow errors & return false except for initial type validation of arguments.
182
+ */
183
+ function schnorrVerify(signature: Hex, message: Hex, publicKey: Hex): boolean {
184
+ const sig = ensureBytes('signature', signature, 64);
185
+ const m = ensureBytes('message', message);
186
+ const pub = ensureBytes('publicKey', publicKey, 32);
187
+ try {
188
+ const P = lift_x(bytesToNumberBE(pub)); // P = lift_x(int(pk)); fail if that fails
189
+ const r = bytesToNumberBE(sig.subarray(0, 32)); // Let r = int(sig[0:32]); fail if r ≥ p.
190
+ if (!fe(r)) return false;
191
+ const s = bytesToNumberBE(sig.subarray(32, 64)); // Let s = int(sig[32:64]); fail if s ≥ n.
192
+ if (!ge(s)) return false;
193
+ const e = challenge(numTo32b(r), pointToBytes(P), m); // int(challenge(bytes(r)||bytes(P)||m))%n
194
+ const R = GmulAdd(P, s, modN(-e)); // R = s⋅G - e⋅P
195
+ if (!R || !R.hasEvenY() || R.toAffine().x !== r) return false; // -eP == (n-e)P
196
+ return true; // Fail if is_infinite(R) / not has_even_y(R) / x(R) ≠ r.
197
+ } catch (error) {
198
+ return false;
199
+ }
200
+ }
201
+
202
+ export const schnorr = {
203
+ getPublicKey: schnorrGetPublicKey,
204
+ sign: schnorrSign,
205
+ verify: schnorrVerify,
206
+ utils: {
207
+ randomPrivateKey: secp256k1.utils.randomPrivateKey,
208
+ getExtendedPublicKey: schnorrGetExtPubKey,
209
+ lift_x,
210
+ pointToBytes,
211
+ numberToBytesBE,
212
+ bytesToNumberBE,
213
+ taggedHash,
214
+ mod,
215
+ },
216
+ };
217
+
218
+ const isoMap = htf.isogenyMap(
219
+ Fp,
220
+ [
221
+ // xNum
222
+ [
223
+ '0x8e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38daaaaa8c7',
224
+ '0x7d3d4c80bc321d5b9f315cea7fd44c5d595d2fc0bf63b92dfff1044f17c6581',
225
+ '0x534c328d23f234e6e2a413deca25caece4506144037c40314ecbd0b53d9dd262',
226
+ '0x8e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38daaaaa88c',
227
+ ],
228
+ // xDen
229
+ [
230
+ '0xd35771193d94918a9ca34ccbb7b640dd86cd409542f8487d9fe6b745781eb49b',
231
+ '0xedadc6f64383dc1df7c4b2d51b54225406d36b641f5e41bbc52a56612a8c6d14',
232
+ '0x0000000000000000000000000000000000000000000000000000000000000001', // LAST 1
233
+ ],
234
+ // yNum
235
+ [
236
+ '0x4bda12f684bda12f684bda12f684bda12f684bda12f684bda12f684b8e38e23c',
237
+ '0xc75e0c32d5cb7c0fa9d0a54b12a0a6d5647ab046d686da6fdffc90fc201d71a3',
238
+ '0x29a6194691f91a73715209ef6512e576722830a201be2018a765e85a9ecee931',
239
+ '0x2f684bda12f684bda12f684bda12f684bda12f684bda12f684bda12f38e38d84',
240
+ ],
241
+ // yDen
242
+ [
243
+ '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffff93b',
244
+ '0x7a06534bb8bdb49fd5e9e6632722c2989467c1bfc8e8d978dfb425d2685c2573',
245
+ '0x6484aa716545ca2cf3a70c3fa8fe337e0a3d21162f0d6299a7bf8192bfd2a76f',
246
+ '0x0000000000000000000000000000000000000000000000000000000000000001', // LAST 1
247
+ ],
248
+ ].map((i) => i.map((j) => BigInt(j))) as [Fp[], Fp[], Fp[], Fp[]]
249
+ );
250
+ const mapSWU = mapToCurveSimpleSWU(Fp, {
251
+ A: BigInt('0x3f8731abdd661adca08a5558f0f5d272e953d363cb6f0e5d405447c01a444533'),
252
+ B: BigInt('1771'),
253
+ Z: Fp.create(BigInt('-11')),
254
+ });
255
+ export const { hashToCurve, encodeToCurve } = htf.createHasher(
256
+ secp256k1.ProjectivePoint,
257
+ (scalars: bigint[]) => {
258
+ const { x, y } = mapSWU(Fp.create(scalars[0]));
259
+ return isoMap(x, y);
260
+ },
261
+ {
262
+ DST: 'secp256k1_XMD:SHA-256_SSWU_RO_',
263
+ encodeDST: 'secp256k1_XMD:SHA-256_SSWU_NU_',
264
+ p: Fp.ORDER,
265
+ m: 1,
266
+ k: 128,
267
+ expand: 'xmd',
268
+ hash: sha256,
269
+ }
270
+ );
package/src/stark.ts ADDED
@@ -0,0 +1,356 @@
1
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2
+ import { keccak_256 } from '@noble/hashes/sha3';
3
+ import { sha256 } from '@noble/hashes/sha256';
4
+ import { weierstrass, ProjPointType } from './abstract/weierstrass.js';
5
+ import * as cutils from './abstract/utils.js';
6
+ import { Fp, mod, Field, validateField } from './abstract/modular.js';
7
+ import { getHash } from './_shortw_utils.js';
8
+ import * as poseidon from './abstract/poseidon.js';
9
+ import { utf8ToBytes } from '@noble/hashes/utils';
10
+
11
+ type ProjectivePoint = ProjPointType<bigint>;
12
+ // Stark-friendly elliptic curve
13
+ // https://docs.starkware.co/starkex/stark-curve.html
14
+
15
+ const CURVE_N = BigInt(
16
+ '3618502788666131213697322783095070105526743751716087489154079457884512865583'
17
+ );
18
+ const nBitLength = 252;
19
+ // Copy-pasted from weierstrass.ts
20
+ function bits2int(bytes: Uint8Array): bigint {
21
+ const delta = bytes.length * 8 - nBitLength;
22
+ const num = cutils.bytesToNumberBE(bytes);
23
+ return delta > 0 ? num >> BigInt(delta) : num;
24
+ }
25
+ function bits2int_modN(bytes: Uint8Array): bigint {
26
+ return mod(bits2int(bytes), CURVE_N);
27
+ }
28
+ export const starkCurve = weierstrass({
29
+ // Params: a, b
30
+ a: BigInt(1),
31
+ b: BigInt('3141592653589793238462643383279502884197169399375105820974944592307816406665'),
32
+ // Field over which we'll do calculations; 2n**251n + 17n * 2n**192n + 1n
33
+ // There is no efficient sqrt for field (P%4==1)
34
+ Fp: Fp(BigInt('0x800000000000011000000000000000000000000000000000000000000000001')),
35
+ // Curve order, total count of valid points in the field.
36
+ n: CURVE_N,
37
+ nBitLength: nBitLength, // len(bin(N).replace('0b',''))
38
+ // Base point (x, y) aka generator point
39
+ Gx: BigInt('874739451078007766457464989774322083649278607533249481151382481072868806602'),
40
+ Gy: BigInt('152666792071518830868575557812948353041420400780739481342941381225525861407'),
41
+ h: BigInt(1),
42
+ // Default options
43
+ lowS: false,
44
+ ...getHash(sha256),
45
+ // Custom truncation routines for stark curve
46
+ bits2int: (bytes: Uint8Array): bigint => {
47
+ while (bytes[0] === 0) bytes = bytes.subarray(1);
48
+ return bits2int(bytes);
49
+ },
50
+ bits2int_modN: (bytes: Uint8Array): bigint => {
51
+ let hashS = cutils.bytesToNumberBE(bytes).toString(16);
52
+ if (hashS.length === 63) {
53
+ hashS += '0';
54
+ bytes = hexToBytes0x(hashS);
55
+ }
56
+ // Truncate zero bytes on left (compat with elliptic)
57
+ while (bytes[0] === 0) bytes = bytes.subarray(1);
58
+ return bits2int_modN(bytes);
59
+ },
60
+ });
61
+
62
+ // Custom Starknet type conversion functions that can handle 0x and unpadded hex
63
+ function hexToBytes0x(hex: string): Uint8Array {
64
+ if (typeof hex !== 'string') {
65
+ throw new Error('hexToBytes: expected string, got ' + typeof hex);
66
+ }
67
+ hex = strip0x(hex);
68
+ if (hex.length & 1) hex = '0' + hex; // padding
69
+ if (hex.length % 2) throw new Error('hexToBytes: received invalid unpadded hex ' + hex.length);
70
+ const array = new Uint8Array(hex.length / 2);
71
+ for (let i = 0; i < array.length; i++) {
72
+ const j = i * 2;
73
+ const hexByte = hex.slice(j, j + 2);
74
+ const byte = Number.parseInt(hexByte, 16);
75
+ if (Number.isNaN(byte) || byte < 0) throw new Error('Invalid byte sequence');
76
+ array[i] = byte;
77
+ }
78
+ return array;
79
+ }
80
+ function hexToNumber0x(hex: string): bigint {
81
+ if (typeof hex !== 'string') {
82
+ throw new Error('hexToNumber: expected string, got ' + typeof hex);
83
+ }
84
+ // Big Endian
85
+ // TODO: strip vs no strip?
86
+ return BigInt(`0x${strip0x(hex)}`);
87
+ }
88
+ function bytesToNumber0x(bytes: Uint8Array): bigint {
89
+ return hexToNumber0x(cutils.bytesToHex(bytes));
90
+ }
91
+ function ensureBytes0x(hex: Hex): Uint8Array {
92
+ // Uint8Array.from() instead of hash.slice() because node.js Buffer
93
+ // is instance of Uint8Array, and its slice() creates **mutable** copy
94
+ return hex instanceof Uint8Array ? Uint8Array.from(hex) : hexToBytes0x(hex);
95
+ }
96
+
97
+ function normPrivKey(privKey: Hex) {
98
+ return cutils.bytesToHex(ensureBytes0x(privKey)).padStart(64, '0');
99
+ }
100
+ function getPublicKey0x(privKey: Hex, isCompressed = false) {
101
+ return starkCurve.getPublicKey(normPrivKey(privKey), isCompressed);
102
+ }
103
+ function getSharedSecret0x(privKeyA: Hex, pubKeyB: Hex) {
104
+ return starkCurve.getSharedSecret(normPrivKey(privKeyA), pubKeyB);
105
+ }
106
+
107
+ function sign0x(msgHash: Hex, privKey: Hex, opts?: any) {
108
+ if (typeof privKey === 'string') privKey = strip0x(privKey).padStart(64, '0');
109
+ return starkCurve.sign(ensureBytes0x(msgHash), normPrivKey(privKey), opts);
110
+ }
111
+ function verify0x(signature: Hex, msgHash: Hex, pubKey: Hex) {
112
+ const sig = signature instanceof Signature ? signature : ensureBytes0x(signature);
113
+ return starkCurve.verify(sig, ensureBytes0x(msgHash), ensureBytes0x(pubKey));
114
+ }
115
+
116
+ const { CURVE, ProjectivePoint, Signature } = starkCurve;
117
+ export const utils = starkCurve.utils;
118
+ export {
119
+ CURVE,
120
+ Signature,
121
+ ProjectivePoint,
122
+ getPublicKey0x as getPublicKey,
123
+ getSharedSecret0x as getSharedSecret,
124
+ sign0x as sign,
125
+ verify0x as verify,
126
+ };
127
+
128
+ const stripLeadingZeros = (s: string) => s.replace(/^0+/gm, '');
129
+ export const bytesToHexEth = (uint8a: Uint8Array): string =>
130
+ `0x${stripLeadingZeros(cutils.bytesToHex(uint8a))}`;
131
+ export const strip0x = (hex: string) => hex.replace(/^0x/i, '');
132
+ export const numberToHexEth = (num: bigint | number) => `0x${num.toString(16)}`;
133
+
134
+ // We accept hex strings besides Uint8Array for simplicity
135
+ type Hex = Uint8Array | string;
136
+
137
+ // 1. seed generation
138
+ function hashKeyWithIndex(key: Uint8Array, index: number) {
139
+ let indexHex = cutils.numberToHexUnpadded(index);
140
+ if (indexHex.length & 1) indexHex = '0' + indexHex;
141
+ return sha256Num(cutils.concatBytes(key, hexToBytes0x(indexHex)));
142
+ }
143
+
144
+ export function grindKey(seed: Hex) {
145
+ const _seed = ensureBytes0x(seed);
146
+ const sha256mask = 2n ** 256n;
147
+
148
+ const limit = sha256mask - mod(sha256mask, CURVE_N);
149
+ for (let i = 0; ; i++) {
150
+ const key = hashKeyWithIndex(_seed, i);
151
+ // key should be in [0, limit)
152
+ if (key < limit) return mod(key, CURVE_N).toString(16);
153
+ }
154
+ }
155
+
156
+ export function getStarkKey(privateKey: Hex) {
157
+ return bytesToHexEth(getPublicKey0x(privateKey, true).slice(1));
158
+ }
159
+
160
+ export function ethSigToPrivate(signature: string) {
161
+ signature = strip0x(signature.replace(/^0x/, ''));
162
+ if (signature.length !== 130) throw new Error('Wrong ethereum signature');
163
+ return grindKey(signature.substring(0, 64));
164
+ }
165
+
166
+ const MASK_31 = 2n ** 31n - 1n;
167
+ const int31 = (n: bigint) => Number(n & MASK_31);
168
+ export function getAccountPath(
169
+ layer: string,
170
+ application: string,
171
+ ethereumAddress: string,
172
+ index: number
173
+ ) {
174
+ const layerNum = int31(sha256Num(layer));
175
+ const applicationNum = int31(sha256Num(application));
176
+ const eth = hexToNumber0x(ethereumAddress);
177
+ return `m/2645'/${layerNum}'/${applicationNum}'/${int31(eth)}'/${int31(eth >> 31n)}'/${index}`;
178
+ }
179
+
180
+ // https://docs.starkware.co/starkex/pedersen-hash-function.html
181
+ const PEDERSEN_POINTS_AFFINE = [
182
+ new ProjectivePoint(
183
+ 2089986280348253421170679821480865132823066470938446095505822317253594081284n,
184
+ 1713931329540660377023406109199410414810705867260802078187082345529207694986n,
185
+ 1n
186
+ ),
187
+ new ProjectivePoint(
188
+ 996781205833008774514500082376783249102396023663454813447423147977397232763n,
189
+ 1668503676786377725805489344771023921079126552019160156920634619255970485781n,
190
+ 1n
191
+ ),
192
+ new ProjectivePoint(
193
+ 2251563274489750535117886426533222435294046428347329203627021249169616184184n,
194
+ 1798716007562728905295480679789526322175868328062420237419143593021674992973n,
195
+ 1n
196
+ ),
197
+ new ProjectivePoint(
198
+ 2138414695194151160943305727036575959195309218611738193261179310511854807447n,
199
+ 113410276730064486255102093846540133784865286929052426931474106396135072156n,
200
+ 1n
201
+ ),
202
+ new ProjectivePoint(
203
+ 2379962749567351885752724891227938183011949129833673362440656643086021394946n,
204
+ 776496453633298175483985398648758586525933812536653089401905292063708816422n,
205
+ 1n
206
+ ),
207
+ ];
208
+ // for (const p of PEDERSEN_POINTS) p._setWindowSize(8);
209
+ const PEDERSEN_POINTS = PEDERSEN_POINTS_AFFINE;
210
+
211
+ function pedersenPrecompute(p1: ProjectivePoint, p2: ProjectivePoint): ProjectivePoint[] {
212
+ const out: ProjectivePoint[] = [];
213
+ let p = p1;
214
+ for (let i = 0; i < 248; i++) {
215
+ out.push(p);
216
+ p = p.double();
217
+ }
218
+ // NOTE: we cannot use wNAF here, because last 4 bits will require full 248 bits multiplication
219
+ // We can add support for this to wNAF, but it will complicate wNAF.
220
+ p = p2;
221
+ for (let i = 0; i < 4; i++) {
222
+ out.push(p);
223
+ p = p.double();
224
+ }
225
+ return out;
226
+ }
227
+ const PEDERSEN_POINTS1 = pedersenPrecompute(PEDERSEN_POINTS[1], PEDERSEN_POINTS[2]);
228
+ const PEDERSEN_POINTS2 = pedersenPrecompute(PEDERSEN_POINTS[3], PEDERSEN_POINTS[4]);
229
+
230
+ type PedersenArg = Hex | bigint | number;
231
+ function pedersenArg(arg: PedersenArg): bigint {
232
+ let value: bigint;
233
+ if (typeof arg === 'bigint') value = arg;
234
+ else if (typeof arg === 'number') {
235
+ if (!Number.isSafeInteger(arg)) throw new Error(`Invalid pedersenArg: ${arg}`);
236
+ value = BigInt(arg);
237
+ } else value = bytesToNumber0x(ensureBytes0x(arg));
238
+ // [0..Fp)
239
+ if (!(0n <= value && value < starkCurve.CURVE.Fp.ORDER))
240
+ throw new Error(`PedersenArg should be 0 <= value < CURVE.P: ${value}`);
241
+ return value;
242
+ }
243
+
244
+ function pedersenSingle(point: ProjectivePoint, value: PedersenArg, constants: ProjectivePoint[]) {
245
+ let x = pedersenArg(value);
246
+ for (let j = 0; j < 252; j++) {
247
+ const pt = constants[j];
248
+ if (pt.px === point.px) throw new Error('Same point');
249
+ if ((x & 1n) !== 0n) point = point.add(pt);
250
+ x >>= 1n;
251
+ }
252
+ return point;
253
+ }
254
+
255
+ // shift_point + x_low * P_0 + x_high * P1 + y_low * P2 + y_high * P3
256
+ export function pedersen(x: PedersenArg, y: PedersenArg) {
257
+ let point: ProjectivePoint = PEDERSEN_POINTS[0];
258
+ point = pedersenSingle(point, x, PEDERSEN_POINTS1);
259
+ point = pedersenSingle(point, y, PEDERSEN_POINTS2);
260
+ return bytesToHexEth(point.toRawBytes(true).slice(1));
261
+ }
262
+
263
+ export function hashChain(data: PedersenArg[], fn = pedersen) {
264
+ if (!Array.isArray(data) || data.length < 1)
265
+ throw new Error('data should be array of at least 1 element');
266
+ if (data.length === 1) return numberToHexEth(pedersenArg(data[0]));
267
+ return Array.from(data)
268
+ .reverse()
269
+ .reduce((acc, i) => fn(i, acc));
270
+ }
271
+ // Same as hashChain, but computes hash even for single element and order is not revesed
272
+ export const computeHashOnElements = (data: PedersenArg[], fn = pedersen) =>
273
+ [0, ...data, data.length].reduce((x, y) => fn(x, y));
274
+
275
+ const MASK_250 = cutils.bitMask(250);
276
+ export const keccak = (data: Uint8Array): bigint => bytesToNumber0x(keccak_256(data)) & MASK_250;
277
+ const sha256Num = (data: Uint8Array | string): bigint => cutils.bytesToNumberBE(sha256(data));
278
+
279
+ // Poseidon hash
280
+ export const Fp253 = Fp(
281
+ BigInt('14474011154664525231415395255581126252639794253786371766033694892385558855681')
282
+ ); // 2^253 + 2^199 + 1
283
+ export const Fp251 = Fp(
284
+ BigInt('3618502788666131213697322783095070105623107215331596699973092056135872020481')
285
+ ); // 2^251 + 17 * 2^192 + 1
286
+
287
+ function poseidonRoundConstant(Fp: Field<bigint>, name: string, idx: number) {
288
+ const val = Fp.fromBytes(sha256(utf8ToBytes(`${name}${idx}`)));
289
+ return Fp.create(val);
290
+ }
291
+
292
+ // NOTE: doesn't check eiginvalues and possible can create unsafe matrix. But any filtration here will break compatibility with starknet
293
+ // Please use only if you really know what you doing.
294
+ // https://eprint.iacr.org/2019/458.pdf Section 2.3 (Avoiding Insecure Matrices)
295
+ export function _poseidonMDS(Fp: Field<bigint>, name: string, m: number, attempt = 0) {
296
+ const x_values: bigint[] = [];
297
+ const y_values: bigint[] = [];
298
+ for (let i = 0; i < m; i++) {
299
+ x_values.push(poseidonRoundConstant(Fp, `${name}x`, attempt * m + i));
300
+ y_values.push(poseidonRoundConstant(Fp, `${name}y`, attempt * m + i));
301
+ }
302
+ if (new Set([...x_values, ...y_values]).size !== 2 * m)
303
+ throw new Error('X and Y values are not distinct');
304
+ return x_values.map((x) => y_values.map((y) => Fp.inv(Fp.sub(x, y))));
305
+ }
306
+
307
+ const MDS_SMALL = [
308
+ [3, 1, 1],
309
+ [1, -1, 1],
310
+ [1, 1, -2],
311
+ ].map((i) => i.map(BigInt));
312
+
313
+ export type PoseidonOpts = {
314
+ Fp: Field<bigint>;
315
+ rate: number;
316
+ capacity: number;
317
+ roundsFull: number;
318
+ roundsPartial: number;
319
+ };
320
+
321
+ export function poseidonBasic(opts: PoseidonOpts, mds: bigint[][]) {
322
+ validateField(opts.Fp);
323
+ if (!Number.isSafeInteger(opts.rate) || !Number.isSafeInteger(opts.capacity))
324
+ throw new Error(`Wrong poseidon opts: ${opts}`);
325
+ const m = opts.rate + opts.capacity;
326
+ const rounds = opts.roundsFull + opts.roundsPartial;
327
+ const roundConstants = [];
328
+ for (let i = 0; i < rounds; i++) {
329
+ const row = [];
330
+ for (let j = 0; j < m; j++) row.push(poseidonRoundConstant(opts.Fp, 'Hades', m * i + j));
331
+ roundConstants.push(row);
332
+ }
333
+ return poseidon.poseidon({
334
+ ...opts,
335
+ t: m,
336
+ sboxPower: 3,
337
+ reversePartialPowIdx: true, // Why?!
338
+ mds,
339
+ roundConstants,
340
+ });
341
+ }
342
+
343
+ export function poseidonCreate(opts: PoseidonOpts, mdsAttempt = 0) {
344
+ const m = opts.rate + opts.capacity;
345
+ if (!Number.isSafeInteger(mdsAttempt)) throw new Error(`Wrong mdsAttempt=${mdsAttempt}`);
346
+ return poseidonBasic(opts, _poseidonMDS(opts.Fp, 'HadesMDS', m, mdsAttempt));
347
+ }
348
+
349
+ export const poseidonSmall = poseidonBasic(
350
+ { Fp: Fp251, rate: 2, capacity: 1, roundsFull: 8, roundsPartial: 83 },
351
+ MDS_SMALL
352
+ );
353
+
354
+ export function poseidonHash(x: bigint, y: bigint, fn = poseidonSmall) {
355
+ return fn([x, y, 2n])[0];
356
+ }
@@ -42,7 +42,6 @@ declare const CURVE: Readonly<{
42
42
  export declare const utils: {
43
43
  normPrivateKeyToScalar: (key: cutils.PrivKey) => bigint;
44
44
  isValidPrivateKey(privateKey: cutils.PrivKey): boolean;
45
- hashToPrivateKey: (hash: cutils.Hex) => Uint8Array;
46
45
  randomPrivateKey: () => Uint8Array;
47
46
  precompute: (windowSize?: number | undefined, point?: ProjPointType<bigint> | undefined) => ProjPointType<bigint>;
48
47
  };
@@ -86,3 +85,4 @@ export declare function poseidonHash(x: bigint, y: bigint, fn?: {
86
85
  (values: bigint[]): bigint[];
87
86
  roundConstants: bigint[][];
88
87
  }): bigint;
88
+ //# sourceMappingURL=stark.d.ts.map
package/stark.d.ts.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stark.d.ts","sourceRoot":"","sources":["src/stark.ts"],"names":[],"mappings":"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"}
@@ -95,21 +95,21 @@ function ensureBytes0x(hex) {
95
95
  // is instance of Uint8Array, and its slice() creates **mutable** copy
96
96
  return hex instanceof Uint8Array ? Uint8Array.from(hex) : hexToBytes0x(hex);
97
97
  }
98
- function normalizePrivateKey(privKey) {
98
+ function normPrivKey(privKey) {
99
99
  return cutils.bytesToHex(ensureBytes0x(privKey)).padStart(64, '0');
100
100
  }
101
101
  function getPublicKey0x(privKey, isCompressed = false) {
102
- return exports.starkCurve.getPublicKey(normalizePrivateKey(privKey), isCompressed);
102
+ return exports.starkCurve.getPublicKey(normPrivKey(privKey), isCompressed);
103
103
  }
104
104
  exports.getPublicKey = getPublicKey0x;
105
105
  function getSharedSecret0x(privKeyA, pubKeyB) {
106
- return exports.starkCurve.getSharedSecret(normalizePrivateKey(privKeyA), pubKeyB);
106
+ return exports.starkCurve.getSharedSecret(normPrivKey(privKeyA), pubKeyB);
107
107
  }
108
108
  exports.getSharedSecret = getSharedSecret0x;
109
109
  function sign0x(msgHash, privKey, opts) {
110
110
  if (typeof privKey === 'string')
111
111
  privKey = (0, exports.strip0x)(privKey).padStart(64, '0');
112
- return exports.starkCurve.sign(ensureBytes0x(msgHash), normalizePrivateKey(privKey), opts);
112
+ return exports.starkCurve.sign(ensureBytes0x(msgHash), normPrivKey(privKey), opts);
113
113
  }
114
114
  exports.sign = sign0x;
115
115
  function verify0x(signature, msgHash, pubKey) {
@@ -311,3 +311,4 @@ function poseidonHash(x, y, fn = exports.poseidonSmall) {
311
311
  return fn([x, y, 2n])[0];
312
312
  }
313
313
  exports.poseidonHash = poseidonHash;
314
+ //# sourceMappingURL=stark.js.map