@noble/curves 0.6.3 → 0.7.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.
Files changed (172) hide show
  1. package/README.md +429 -281
  2. package/{lib/_shortw_utils.d.ts → _shortw_utils.d.ts} +3 -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 +12 -25
  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 +1 -0
  27. package/abstract/montgomery.d.ts.map +1 -0
  28. package/{lib/abstract → abstract}/montgomery.js +3 -2
  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 -4
  39. package/abstract/weierstrass.d.ts.map +1 -0
  40. package/{lib/abstract → abstract}/weierstrass.js +55 -93
  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} +4 -3
  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} +2 -1
  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 +13 -26
  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 +3 -2
  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 +55 -93
  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 +5 -4
  83. package/esm/ed25519.js.map +1 -0
  84. package/{lib/esm → esm}/ed448.js +2 -1
  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 +41 -50
  104. package/esm/secp256k1.js.map +1 -0
  105. package/{lib/esm → esm}/stark.js +1 -0
  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} +5 -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} +5 -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} +6 -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} +6 -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} +6 -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} +17 -6
  141. package/secp256k1.d.ts.map +1 -0
  142. package/{lib/secp256k1.js → secp256k1.js} +38 -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 +184 -0
  151. package/src/abstract/poseidon.ts +119 -0
  152. package/src/abstract/utils.ts +246 -0
  153. package/src/abstract/weierstrass.ts +1175 -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 +260 -0
  167. package/src/stark.ts +356 -0
  168. package/{lib/stark.d.ts → stark.d.ts} +3 -1
  169. package/stark.d.ts.map +1 -0
  170. package/{lib/stark.js → stark.js} +1 -0
  171. package/stark.js.map +1 -0
  172. package/lib/index.d.ts +0 -0
@@ -1,21 +1,15 @@
1
1
  "use strict";
2
+ var _a;
2
3
  Object.defineProperty(exports, "__esModule", { value: true });
3
4
  exports.encodeToCurve = exports.hashToCurve = exports.schnorr = exports.secp256k1 = void 0;
4
5
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
5
6
  const sha256_1 = require("@noble/hashes/sha256");
7
+ const utils_1 = require("@noble/hashes/utils");
6
8
  const modular_js_1 = require("./abstract/modular.js");
7
- const _shortw_utils_js_1 = require("./_shortw_utils.js");
8
9
  const weierstrass_js_1 = require("./abstract/weierstrass.js");
9
10
  const utils_js_1 = require("./abstract/utils.js");
10
- const utils_1 = require("@noble/hashes/utils");
11
11
  const htf = require("./abstract/hash-to-curve.js");
12
- /**
13
- * secp256k1 belongs to Koblitz curves: it has efficiently computable endomorphism.
14
- * Endomorphism uses 2x less RAM, speeds up precomputation by 2x and ECDH / key recovery by 20%.
15
- * Should always be used for Projective's double-and-add multiplication.
16
- * For affines cached multiplication, it trades off 1/2 init time & 1/3 ram for 20% perf hit.
17
- * https://gist.github.com/paulmillr/eb670806793e84df628a7c434a873066
18
- */
12
+ const _shortw_utils_js_1 = require("./_shortw_utils.js");
19
13
  const secp256k1P = BigInt('0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f');
20
14
  const secp256k1N = BigInt('0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141');
21
15
  const _1n = BigInt(1);
@@ -51,23 +45,22 @@ function sqrtMod(y) {
51
45
  }
52
46
  const Fp = (0, modular_js_1.Fp)(secp256k1P, undefined, undefined, { sqrt: sqrtMod });
53
47
  exports.secp256k1 = (0, _shortw_utils_js_1.createCurve)({
54
- // Params: a, b
55
- // Seem to be rigid https://bitcointalk.org/index.php?topic=289795.msg3183975#msg3183975
56
48
  a: BigInt(0),
57
49
  b: BigInt(7),
58
- // Field over which we'll do calculations;
59
- // 2n**256n - 2n**32n - 2n**9n - 2n**8n - 2n**7n - 2n**6n - 2n**4n - 1n
60
50
  Fp,
61
- // Curve order, total count of valid points in the field
62
51
  n: secp256k1N,
63
52
  // Base point (x, y) aka generator point
64
53
  Gx: BigInt('55066263022277343669578718895168534326250603453777594175500187360389116729240'),
65
54
  Gy: BigInt('32670510020758816978083085130507043184471273380659243275938904335757337482424'),
66
55
  h: BigInt(1),
67
- // Alllow only low-S signatures by default in sign() and verify()
68
56
  lowS: true,
57
+ /**
58
+ * secp256k1 belongs to Koblitz curves: it has efficiently computable endomorphism.
59
+ * Endomorphism uses 2x less RAM, speeds up precomputation by 2x and ECDH / key recovery by 20%.
60
+ * For precomputed wNAF it trades off 1/2 init time & 1/3 ram for 20% perf hit.
61
+ * Explanation: https://gist.github.com/paulmillr/eb670806793e84df628a7c434a873066
62
+ */
69
63
  endo: {
70
- // Params taken from https://gist.github.com/paulmillr/eb670806793e84df628a7c434a873066
71
64
  beta: BigInt('0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee'),
72
65
  splitScalar: (k) => {
73
66
  const n = secp256k1N;
@@ -93,17 +86,11 @@ exports.secp256k1 = (0, _shortw_utils_js_1.createCurve)({
93
86
  },
94
87
  },
95
88
  }, sha256_1.sha256);
96
- // Schnorr signatures are superior to ECDSA from above.
97
- // Below is Schnorr-specific code as per BIP0340.
89
+ // Schnorr signatures are superior to ECDSA from above. Below is Schnorr-specific BIP0340 code.
98
90
  // https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki
99
91
  const _0n = BigInt(0);
100
92
  const fe = (x) => typeof x === 'bigint' && _0n < x && x < secp256k1P;
101
93
  const ge = (x) => typeof x === 'bigint' && _0n < x && x < secp256k1N;
102
- const TAGS = {
103
- challenge: 'BIP0340/challenge',
104
- aux: 'BIP0340/aux',
105
- nonce: 'BIP0340/nonce',
106
- };
107
94
  /** An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)] */
108
95
  const TAGGED_HASH_PREFIXES = {};
109
96
  function taggedHash(tag, ...messages) {
@@ -117,12 +104,12 @@ function taggedHash(tag, ...messages) {
117
104
  }
118
105
  const pointToBytes = (point) => point.toRawBytes(true).slice(1);
119
106
  const numTo32b = (n) => (0, utils_js_1.numberToBytesBE)(n, 32);
107
+ const modP = (x) => (0, modular_js_1.mod)(x, secp256k1P);
120
108
  const modN = (x) => (0, modular_js_1.mod)(x, secp256k1N);
121
109
  const Point = exports.secp256k1.ProjectivePoint;
122
110
  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
111
  function schnorrGetExtPubKey(priv) {
125
- let d = typeof priv === 'bigint' ? priv : hex32ToInt(priv);
112
+ const d = exports.secp256k1.utils.normPrivateKeyToScalar(priv);
126
113
  const point = Point.fromPrivateKey(d); // P = d'⋅G; 0 < d' < n check is done inside
127
114
  const scalar = point.hasEvenY() ? d : modN(-d); // d = d' if has_even_y(P), otherwise d = n-d'
128
115
  return { point, scalar, bytes: pointToBytes(point) };
@@ -130,31 +117,34 @@ function schnorrGetExtPubKey(priv) {
130
117
  function lift_x(x) {
131
118
  if (!fe(x))
132
119
  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.
120
+ const xx = modP(x * x);
121
+ const c = modP(xx * x + BigInt(7)); // Let c = x³ + 7 mod p.
134
122
  let y = sqrtMod(c); // Let y = c^(p+1)/4 mod p.
135
123
  if (y % 2n !== 0n)
136
- y = (0, modular_js_1.mod)(-y, secp256k1P); // Return the unique point P such that x(P) = x and
124
+ y = modP(-y); // Return the unique point P such that x(P) = x and
137
125
  const p = new Point(x, y, _1n); // y(P) = y if y mod 2 = 0 or y(P) = p-y otherwise.
138
126
  p.assertValidity();
139
127
  return p;
140
128
  }
141
129
  function challenge(...args) {
142
- return modN((0, utils_js_1.bytesToNumberBE)(taggedHash(TAGS.challenge, ...args)));
130
+ return modN((0, utils_js_1.bytesToNumberBE)(taggedHash('BIP0340/challenge', ...args)));
143
131
  }
144
- // Schnorr's pubkey is just `x` of Point (BIP340)
132
+ /**
133
+ * Schnorr public key is just `x` coordinate of Point as per BIP340.
134
+ */
145
135
  function schnorrGetPublicKey(privateKey) {
146
136
  return schnorrGetExtPubKey(privateKey).bytes; // d'=int(sk). Fail if d'=0 or d'≥n. Ret bytes(d'⋅G)
147
137
  }
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
138
+ /**
139
+ * Creates Schnorr signature as per BIP340. Verifies itself before returning anything.
140
+ * auxRand is optional and is not the sole source of k generation: bad CSPRNG won't be dangerous.
141
+ */
150
142
  function schnorrSign(message, privateKey, auxRand = (0, utils_1.randomBytes)(32)) {
151
- if (message == null)
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)
143
+ const m = (0, utils_js_1.ensureBytes)('message', message);
144
+ const { bytes: px, scalar: d } = schnorrGetExtPubKey(privateKey); // checks for isWithinCurveOrder
145
+ const a = (0, utils_js_1.ensureBytes)('auxRand', auxRand, 32); // Auxiliary random data a: a 32-byte array
146
+ const t = numTo32b(d ^ (0, utils_js_1.bytesToNumberBE)(taggedHash('BIP0340/aux', a))); // Let t be the byte-wise xor of bytes(d) and hash/aux(a)
147
+ const rand = taggedHash('BIP0340/nonce', t, px, m); // Let rand = hash/nonce(t || bytes(P) || m)
158
148
  const k_ = modN((0, utils_js_1.bytesToNumberBE)(rand)); // Let k' = int(rand) mod n
159
149
  if (k_ === _0n)
160
150
  throw new Error('sign failed: k is zero'); // Fail if k' = 0.
@@ -169,20 +159,21 @@ function schnorrSign(message, privateKey, auxRand = (0, utils_1.randomBytes)(32)
169
159
  return sig;
170
160
  }
171
161
  /**
172
- * Verifies Schnorr signature synchronously.
162
+ * Verifies Schnorr signature.
173
163
  */
174
164
  function schnorrVerify(signature, message, publicKey) {
165
+ const sig = (0, utils_js_1.ensureBytes)('signature', signature, 64);
166
+ const m = (0, utils_js_1.ensureBytes)('message', message);
167
+ const pub = (0, utils_js_1.ensureBytes)('publicKey', publicKey, 32);
175
168
  try {
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);
169
+ const P = lift_x((0, utils_js_1.bytesToNumberBE)(pub)); // P = lift_x(int(pk)); fail if that fails
178
170
  const r = (0, utils_js_1.bytesToNumberBE)(sig.subarray(0, 32)); // Let r = int(sig[0:32]); fail if r ≥ p.
179
171
  if (!fe(r))
180
172
  return false;
181
173
  const s = (0, utils_js_1.bytesToNumberBE)(sig.subarray(32, 64)); // Let s = int(sig[32:64]); fail if s ≥ n.
182
174
  if (!ge(s))
183
175
  return false;
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
176
+ const e = challenge(numTo32b(r), pointToBytes(P), m); // int(challenge(bytes(r)||bytes(P)||m))%n
186
177
  const R = GmulAdd(P, s, modN(-e)); // R = s⋅G - e⋅P
187
178
  if (!R || !R.hasEvenY() || R.toAffine().x !== r)
188
179
  return false; // -eP == (n-e)P
@@ -197,6 +188,7 @@ exports.schnorr = {
197
188
  sign: schnorrSign,
198
189
  verify: schnorrVerify,
199
190
  utils: {
191
+ randomPrivateKey: exports.secp256k1.utils.randomPrivateKey,
200
192
  getExtendedPublicKey: schnorrGetExtPubKey,
201
193
  lift_x,
202
194
  pointToBytes,
@@ -240,7 +232,7 @@ const mapSWU = (0, weierstrass_js_1.mapToCurveSimpleSWU)(Fp, {
240
232
  B: BigInt('1771'),
241
233
  Z: Fp.create(BigInt('-11')),
242
234
  });
243
- const { hashToCurve, encodeToCurve } = htf.hashToCurve(exports.secp256k1.ProjectivePoint, (scalars) => {
235
+ _a = htf.createHasher(exports.secp256k1.ProjectivePoint, (scalars) => {
244
236
  const { x, y } = mapSWU(Fp.create(scalars[0]));
245
237
  return isoMap(x, y);
246
238
  }, {
@@ -251,6 +243,5 @@ const { hashToCurve, encodeToCurve } = htf.hashToCurve(exports.secp256k1.Project
251
243
  k: 128,
252
244
  expand: 'xmd',
253
245
  hash: sha256_1.sha256,
254
- });
255
- exports.hashToCurve = hashToCurve;
256
- exports.encodeToCurve = encodeToCurve;
246
+ }), exports.hashToCurve = _a.hashToCurve, exports.encodeToCurve = _a.encodeToCurve;
247
+ //# sourceMappingURL=secp256k1.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"secp256k1.js","sourceRoot":"","sources":["src/secp256k1.ts"],"names":[],"mappings":";;;;AAAA,sEAAsE;AACtE,iDAA8C;AAC9C,+CAAkD;AAClD,sDAA+D;AAC/D,8DAA4F;AAE5F,kDAAiG;AACjG,mDAAmD;AACnD,yDAAiD;AAEjD,MAAM,UAAU,GAAG,MAAM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,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC;IACjB,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;CAC5B,CAAC,CAAC;AACU,KAAiC,GAAG,CAAC,YAAY,CAC5D,iBAAS,CAAC,eAAe,EACzB,CAAC,OAAiB,EAAE,EAAE;IACpB,MAAM,EAAE,CAAC,EAAE,CAAC,EAAE,GAAG,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC/C,OAAO,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AACtB,CAAC,EACD;IACE,GAAG,EAAE,gCAAgC;IACrC,SAAS,EAAE,gCAAgC;IAC3C,CAAC,EAAE,EAAE,CAAC,KAAK;IACX,CAAC,EAAE,CAAC;IACJ,CAAC,EAAE,GAAG;IACN,MAAM,EAAE,KAAK;IACb,IAAI,EAAE,eAAM;CACb,CACF,EAfc,mBAAW,mBAAE,qBAAa,oBAevC"}
@@ -0,0 +1,20 @@
1
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2
+ import { hmac } from '@noble/hashes/hmac';
3
+ import { concatBytes, randomBytes } from '@noble/hashes/utils';
4
+ import { weierstrass, CurveType } from './abstract/weierstrass.js';
5
+ import { CHash } from './abstract/utils.js';
6
+
7
+ // connects noble-curves to noble-hashes
8
+ export function getHash(hash: CHash) {
9
+ return {
10
+ hash,
11
+ hmac: (key: Uint8Array, ...msgs: Uint8Array[]) => hmac(hash, key, concatBytes(...msgs)),
12
+ randomBytes,
13
+ };
14
+ }
15
+ // Same API as @noble/hashes, with ability to create curve with custom hash
16
+ type CurveDef = Readonly<Omit<CurveType, 'hash' | 'hmac' | 'randomBytes'>>;
17
+ export function createCurve(curveDef: CurveDef, defHash: CHash) {
18
+ const create = (hash: CHash) => weierstrass({ ...curveDef, ...getHash(hash) });
19
+ return Object.freeze({ ...create(defHash), create });
20
+ }
@@ -0,0 +1,376 @@
1
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2
+ /**
3
+ * BLS (Barreto-Lynn-Scott) family of pairing-friendly curves.
4
+ * Implements BLS (Boneh-Lynn-Shacham) signatures.
5
+ * Consists of two curves: G1 and G2:
6
+ * - G1 is a subgroup of (x, y) E(Fq) over y² = x³ + 4.
7
+ * - G2 is a subgroup of ((x₁, x₂+i), (y₁, y₂+i)) E(Fq²) over y² = x³ + 4(1 + i) where i is √-1
8
+ * - Gt, created by bilinear (ate) pairing e(G1, G2), consists of p-th roots of unity in
9
+ * Fq^k where k is embedding degree. Only degree 12 is currently supported, 24 is not.
10
+ * Pairing is used to aggregate and verify signatures.
11
+ * We are using Fp for private keys (shorter) and Fp₂ for signatures (longer).
12
+ * Some projects may prefer to swap this relation, it is not supported for now.
13
+ */
14
+ import { AffinePoint } from './curve.js';
15
+ import { Field, hashToPrivateScalar } from './modular.js';
16
+ import { Hex, PrivKey, CHash, bitLen, bitGet, ensureBytes } from './utils.js';
17
+ import * as htf from './hash-to-curve.js';
18
+ import {
19
+ CurvePointsType,
20
+ ProjPointType as ProjPointType,
21
+ CurvePointsRes,
22
+ weierstrassPoints,
23
+ } from './weierstrass.js';
24
+
25
+ type Fp = bigint; // Can be different field?
26
+
27
+ export type SignatureCoder<Fp2> = {
28
+ decode(hex: Hex): ProjPointType<Fp2>;
29
+ encode(point: ProjPointType<Fp2>): Uint8Array;
30
+ };
31
+
32
+ export type CurveType<Fp, Fp2, Fp6, Fp12> = {
33
+ r: bigint;
34
+ G1: Omit<CurvePointsType<Fp>, 'n'> & {
35
+ mapToCurve: htf.MapToCurve<Fp>;
36
+ htfDefaults: htf.Opts;
37
+ };
38
+ G2: Omit<CurvePointsType<Fp2>, 'n'> & {
39
+ Signature: SignatureCoder<Fp2>;
40
+ mapToCurve: htf.MapToCurve<Fp2>;
41
+ htfDefaults: htf.Opts;
42
+ };
43
+ x: bigint;
44
+ Fp: Field<Fp>;
45
+ Fr: Field<bigint>;
46
+ Fp2: Field<Fp2> & {
47
+ reim: (num: Fp2) => { re: bigint; im: bigint };
48
+ multiplyByB: (num: Fp2) => Fp2;
49
+ frobeniusMap(num: Fp2, power: number): Fp2;
50
+ };
51
+ Fp6: Field<Fp6>;
52
+ Fp12: Field<Fp12> & {
53
+ frobeniusMap(num: Fp12, power: number): Fp12;
54
+ multiplyBy014(num: Fp12, o0: Fp2, o1: Fp2, o4: Fp2): Fp12;
55
+ conjugate(num: Fp12): Fp12;
56
+ finalExponentiate(num: Fp12): Fp12;
57
+ };
58
+ htfDefaults: htf.Opts;
59
+ hash: CHash; // Because we need outputLen for DRBG
60
+ randomBytes: (bytesLength?: number) => Uint8Array;
61
+ };
62
+
63
+ export type CurveFn<Fp, Fp2, Fp6, Fp12> = {
64
+ CURVE: CurveType<Fp, Fp2, Fp6, Fp12>;
65
+ Fr: Field<bigint>;
66
+ Fp: Field<Fp>;
67
+ Fp2: Field<Fp2>;
68
+ Fp6: Field<Fp6>;
69
+ Fp12: Field<Fp12>;
70
+ G1: CurvePointsRes<Fp> & ReturnType<typeof htf.createHasher<Fp>>;
71
+ G2: CurvePointsRes<Fp2> & ReturnType<typeof htf.createHasher<Fp2>>;
72
+ Signature: SignatureCoder<Fp2>;
73
+ millerLoop: (ell: [Fp2, Fp2, Fp2][], g1: [Fp, Fp]) => Fp12;
74
+ calcPairingPrecomputes: (p: AffinePoint<Fp2>) => [Fp2, Fp2, Fp2][];
75
+ pairing: (P: ProjPointType<Fp>, Q: ProjPointType<Fp2>, withFinalExponent?: boolean) => Fp12;
76
+ getPublicKey: (privateKey: PrivKey) => Uint8Array;
77
+ sign: {
78
+ (message: Hex, privateKey: PrivKey): Uint8Array;
79
+ (message: ProjPointType<Fp2>, privateKey: PrivKey): ProjPointType<Fp2>;
80
+ };
81
+ verify: (
82
+ signature: Hex | ProjPointType<Fp2>,
83
+ message: Hex | ProjPointType<Fp2>,
84
+ publicKey: Hex | ProjPointType<Fp>
85
+ ) => boolean;
86
+ aggregatePublicKeys: {
87
+ (publicKeys: Hex[]): Uint8Array;
88
+ (publicKeys: ProjPointType<Fp>[]): ProjPointType<Fp>;
89
+ };
90
+ aggregateSignatures: {
91
+ (signatures: Hex[]): Uint8Array;
92
+ (signatures: ProjPointType<Fp2>[]): ProjPointType<Fp2>;
93
+ };
94
+ verifyBatch: (
95
+ signature: Hex | ProjPointType<Fp2>,
96
+ messages: (Hex | ProjPointType<Fp2>)[],
97
+ publicKeys: (Hex | ProjPointType<Fp>)[]
98
+ ) => boolean;
99
+ utils: {
100
+ randomPrivateKey: () => Uint8Array;
101
+ };
102
+ };
103
+
104
+ export function bls<Fp2, Fp6, Fp12>(
105
+ CURVE: CurveType<Fp, Fp2, Fp6, Fp12>
106
+ ): CurveFn<Fp, Fp2, Fp6, Fp12> {
107
+ // Fields looks pretty specific for curve, so for now we need to pass them with opts
108
+ const { Fp, Fr, Fp2, Fp6, Fp12 } = CURVE;
109
+ const BLS_X_LEN = bitLen(CURVE.x);
110
+ const groupLen = 32; // TODO: calculate; hardcoded for now
111
+
112
+ // Pre-compute coefficients for sparse multiplication
113
+ // Point addition and point double calculations is reused for coefficients
114
+ function calcPairingPrecomputes(p: AffinePoint<Fp2>) {
115
+ const { x, y } = p;
116
+ // prettier-ignore
117
+ const Qx = x, Qy = y, Qz = Fp2.ONE;
118
+ // prettier-ignore
119
+ let Rx = Qx, Ry = Qy, Rz = Qz;
120
+ let ell_coeff: [Fp2, Fp2, Fp2][] = [];
121
+ for (let i = BLS_X_LEN - 2; i >= 0; i--) {
122
+ // Double
123
+ let t0 = Fp2.sqr(Ry); // Ry²
124
+ let t1 = Fp2.sqr(Rz); // Rz²
125
+ let t2 = Fp2.multiplyByB(Fp2.mul(t1, 3n)); // 3 * T1 * B
126
+ let t3 = Fp2.mul(t2, 3n); // 3 * T2
127
+ let t4 = Fp2.sub(Fp2.sub(Fp2.sqr(Fp2.add(Ry, Rz)), t1), t0); // (Ry + Rz)² - T1 - T0
128
+ ell_coeff.push([
129
+ Fp2.sub(t2, t0), // T2 - T0
130
+ Fp2.mul(Fp2.sqr(Rx), 3n), // 3 * Rx²
131
+ Fp2.neg(t4), // -T4
132
+ ]);
133
+ Rx = Fp2.div(Fp2.mul(Fp2.mul(Fp2.sub(t0, t3), Rx), Ry), 2n); // ((T0 - T3) * Rx * Ry) / 2
134
+ Ry = Fp2.sub(Fp2.sqr(Fp2.div(Fp2.add(t0, t3), 2n)), Fp2.mul(Fp2.sqr(t2), 3n)); // ((T0 + T3) / 2)² - 3 * T2²
135
+ Rz = Fp2.mul(t0, t4); // T0 * T4
136
+ if (bitGet(CURVE.x, i)) {
137
+ // Addition
138
+ let t0 = Fp2.sub(Ry, Fp2.mul(Qy, Rz)); // Ry - Qy * Rz
139
+ let t1 = Fp2.sub(Rx, Fp2.mul(Qx, Rz)); // Rx - Qx * Rz
140
+ ell_coeff.push([
141
+ Fp2.sub(Fp2.mul(t0, Qx), Fp2.mul(t1, Qy)), // T0 * Qx - T1 * Qy
142
+ Fp2.neg(t0), // -T0
143
+ t1, // T1
144
+ ]);
145
+ let t2 = Fp2.sqr(t1); // T1²
146
+ let t3 = Fp2.mul(t2, t1); // T2 * T1
147
+ let t4 = Fp2.mul(t2, Rx); // T2 * Rx
148
+ let t5 = Fp2.add(Fp2.sub(t3, Fp2.mul(t4, 2n)), Fp2.mul(Fp2.sqr(t0), Rz)); // T3 - 2 * T4 + T0² * Rz
149
+ Rx = Fp2.mul(t1, t5); // T1 * T5
150
+ Ry = Fp2.sub(Fp2.mul(Fp2.sub(t4, t5), t0), Fp2.mul(t3, Ry)); // (T4 - T5) * T0 - T3 * Ry
151
+ Rz = Fp2.mul(Rz, t3); // Rz * T3
152
+ }
153
+ }
154
+ return ell_coeff;
155
+ }
156
+
157
+ function millerLoop(ell: [Fp2, Fp2, Fp2][], g1: [Fp, Fp]): Fp12 {
158
+ const { x } = CURVE;
159
+ const Px = g1[0];
160
+ const Py = g1[1];
161
+ let f12 = Fp12.ONE;
162
+ for (let j = 0, i = BLS_X_LEN - 2; i >= 0; i--, j++) {
163
+ const E = ell[j];
164
+ f12 = Fp12.multiplyBy014(f12, E[0], Fp2.mul(E[1], Px), Fp2.mul(E[2], Py));
165
+ if (bitGet(x, i)) {
166
+ j += 1;
167
+ const F = ell[j];
168
+ f12 = Fp12.multiplyBy014(f12, F[0], Fp2.mul(F[1], Px), Fp2.mul(F[2], Py));
169
+ }
170
+ if (i !== 0) f12 = Fp12.sqr(f12);
171
+ }
172
+ return Fp12.conjugate(f12);
173
+ }
174
+
175
+ const utils = {
176
+ randomPrivateKey: (): Uint8Array => {
177
+ return Fr.toBytes(hashToPrivateScalar(CURVE.randomBytes(groupLen + 8), CURVE.r));
178
+ },
179
+ };
180
+
181
+ // Point on G1 curve: (x, y)
182
+ const G1_ = weierstrassPoints({ n: Fr.ORDER, ...CURVE.G1 });
183
+ const G1 = Object.assign(
184
+ G1_,
185
+ htf.createHasher(G1_.ProjectivePoint, CURVE.G1.mapToCurve, {
186
+ ...CURVE.htfDefaults,
187
+ ...CURVE.G1.htfDefaults,
188
+ })
189
+ );
190
+
191
+ // Sparse multiplication against precomputed coefficients
192
+ // TODO: replace with weakmap?
193
+ type withPairingPrecomputes = { _PPRECOMPUTES: [Fp2, Fp2, Fp2][] | undefined };
194
+ function pairingPrecomputes(point: G2): [Fp2, Fp2, Fp2][] {
195
+ const p = point as G2 & withPairingPrecomputes;
196
+ if (p._PPRECOMPUTES) return p._PPRECOMPUTES;
197
+ p._PPRECOMPUTES = calcPairingPrecomputes(point.toAffine());
198
+ return p._PPRECOMPUTES;
199
+ }
200
+
201
+ // TODO: export
202
+ // function clearPairingPrecomputes(point: G2) {
203
+ // const p = point as G2 & withPairingPrecomputes;
204
+ // p._PPRECOMPUTES = undefined;
205
+ // }
206
+
207
+ // Point on G2 curve (complex numbers): (x₁, x₂+i), (y₁, y₂+i)
208
+ const G2_ = weierstrassPoints({ n: Fr.ORDER, ...CURVE.G2 });
209
+ const G2 = Object.assign(
210
+ G2_,
211
+ htf.createHasher(G2_.ProjectivePoint as htf.H2CPointConstructor<Fp2>, CURVE.G2.mapToCurve, {
212
+ ...CURVE.htfDefaults,
213
+ ...CURVE.G2.htfDefaults,
214
+ })
215
+ );
216
+
217
+ const { Signature } = CURVE.G2;
218
+
219
+ // Calculates bilinear pairing
220
+ function pairing(Q: G1, P: G2, withFinalExponent: boolean = true): Fp12 {
221
+ if (Q.equals(G1.ProjectivePoint.ZERO) || P.equals(G2.ProjectivePoint.ZERO))
222
+ throw new Error('pairing is not available for ZERO point');
223
+ Q.assertValidity();
224
+ P.assertValidity();
225
+ // Performance: 9ms for millerLoop and ~14ms for exp.
226
+ const Qa = Q.toAffine();
227
+ const looped = millerLoop(pairingPrecomputes(P), [Qa.x, Qa.y]);
228
+ return withFinalExponent ? Fp12.finalExponentiate(looped) : looped;
229
+ }
230
+ type G1 = typeof G1.ProjectivePoint.BASE;
231
+ type G2 = typeof G2.ProjectivePoint.BASE;
232
+
233
+ type G1Hex = Hex | G1;
234
+ type G2Hex = Hex | G2;
235
+ function normP1(point: G1Hex): G1 {
236
+ return point instanceof G1.ProjectivePoint ? (point as G1) : G1.ProjectivePoint.fromHex(point);
237
+ }
238
+ function normP2(point: G2Hex): G2 {
239
+ return point instanceof G2.ProjectivePoint ? point : Signature.decode(point);
240
+ }
241
+ function normP2Hash(point: G2Hex, htfOpts?: htf.htfBasicOpts): G2 {
242
+ return point instanceof G2.ProjectivePoint
243
+ ? point
244
+ : (G2.hashToCurve(ensureBytes('point', point), htfOpts) as G2);
245
+ }
246
+
247
+ // Multiplies generator by private key.
248
+ // P = pk x G
249
+ function getPublicKey(privateKey: PrivKey): Uint8Array {
250
+ return G1.ProjectivePoint.fromPrivateKey(privateKey).toRawBytes(true);
251
+ }
252
+
253
+ // Executes `hashToCurve` on the message and then multiplies the result by private key.
254
+ // S = pk x H(m)
255
+ function sign(message: Hex, privateKey: PrivKey, htfOpts?: htf.htfBasicOpts): Uint8Array;
256
+ function sign(message: G2, privateKey: PrivKey, htfOpts?: htf.htfBasicOpts): G2;
257
+ function sign(message: G2Hex, privateKey: PrivKey, htfOpts?: htf.htfBasicOpts): Uint8Array | G2 {
258
+ const msgPoint = normP2Hash(message, htfOpts);
259
+ msgPoint.assertValidity();
260
+ const sigPoint = msgPoint.multiply(G1.normalizePrivateKey(privateKey));
261
+ if (message instanceof G2.ProjectivePoint) return sigPoint;
262
+ return Signature.encode(sigPoint);
263
+ }
264
+
265
+ // Checks if pairing of public key & hash is equal to pairing of generator & signature.
266
+ // e(P, H(m)) == e(G, S)
267
+ function verify(
268
+ signature: G2Hex,
269
+ message: G2Hex,
270
+ publicKey: G1Hex,
271
+ htfOpts?: htf.htfBasicOpts
272
+ ): boolean {
273
+ const P = normP1(publicKey);
274
+ const Hm = normP2Hash(message, htfOpts);
275
+ const G = G1.ProjectivePoint.BASE;
276
+ const S = normP2(signature);
277
+ // Instead of doing 2 exponentiations, we use property of billinear maps
278
+ // and do one exp after multiplying 2 points.
279
+ const ePHm = pairing(P.negate(), Hm, false);
280
+ const eGS = pairing(G, S, false);
281
+ const exp = Fp12.finalExponentiate(Fp12.mul(eGS, ePHm));
282
+ return Fp12.eql(exp, Fp12.ONE);
283
+ }
284
+
285
+ // Adds a bunch of public key points together.
286
+ // pk1 + pk2 + pk3 = pkA
287
+ function aggregatePublicKeys(publicKeys: Hex[]): Uint8Array;
288
+ function aggregatePublicKeys(publicKeys: G1[]): G1;
289
+ function aggregatePublicKeys(publicKeys: G1Hex[]): Uint8Array | G1 {
290
+ if (!publicKeys.length) throw new Error('Expected non-empty array');
291
+ const agg = publicKeys.map(normP1).reduce((sum, p) => sum.add(p), G1.ProjectivePoint.ZERO);
292
+ const aggAffine = agg; //.toAffine();
293
+ if (publicKeys[0] instanceof G1.ProjectivePoint) {
294
+ aggAffine.assertValidity();
295
+ return aggAffine;
296
+ }
297
+ // toRawBytes ensures point validity
298
+ return aggAffine.toRawBytes(true);
299
+ }
300
+
301
+ // Adds a bunch of signature points together.
302
+ function aggregateSignatures(signatures: Hex[]): Uint8Array;
303
+ function aggregateSignatures(signatures: G2[]): G2;
304
+ function aggregateSignatures(signatures: G2Hex[]): Uint8Array | G2 {
305
+ if (!signatures.length) throw new Error('Expected non-empty array');
306
+ const agg = signatures.map(normP2).reduce((sum, s) => sum.add(s), G2.ProjectivePoint.ZERO);
307
+ const aggAffine = agg; //.toAffine();
308
+ if (signatures[0] instanceof G2.ProjectivePoint) {
309
+ aggAffine.assertValidity();
310
+ return aggAffine;
311
+ }
312
+ return Signature.encode(aggAffine);
313
+ }
314
+
315
+ // https://ethresear.ch/t/fast-verification-of-multiple-bls-signatures/5407
316
+ // e(G, S) = e(G, SUM(n)(Si)) = MUL(n)(e(G, Si))
317
+ function verifyBatch(
318
+ signature: G2Hex,
319
+ messages: G2Hex[],
320
+ publicKeys: G1Hex[],
321
+ htfOpts?: htf.htfBasicOpts
322
+ ): boolean {
323
+ // @ts-ignore
324
+ // console.log('verifyBatch', bytesToHex(signature as any), messages, publicKeys.map(bytesToHex));
325
+
326
+ if (!messages.length) throw new Error('Expected non-empty messages array');
327
+ if (publicKeys.length !== messages.length)
328
+ throw new Error('Pubkey count should equal msg count');
329
+ const sig = normP2(signature);
330
+ const nMessages = messages.map((i) => normP2Hash(i, htfOpts));
331
+ const nPublicKeys = publicKeys.map(normP1);
332
+ try {
333
+ const paired = [];
334
+ for (const message of new Set(nMessages)) {
335
+ const groupPublicKey = nMessages.reduce(
336
+ (groupPublicKey, subMessage, i) =>
337
+ subMessage === message ? groupPublicKey.add(nPublicKeys[i]) : groupPublicKey,
338
+ G1.ProjectivePoint.ZERO
339
+ );
340
+ // const msg = message instanceof PointG2 ? message : await PointG2.hashToCurve(message);
341
+ // Possible to batch pairing for same msg with different groupPublicKey here
342
+ paired.push(pairing(groupPublicKey, message, false));
343
+ }
344
+ paired.push(pairing(G1.ProjectivePoint.BASE.negate(), sig, false));
345
+ const product = paired.reduce((a, b) => Fp12.mul(a, b), Fp12.ONE);
346
+ const exp = Fp12.finalExponentiate(product);
347
+ return Fp12.eql(exp, Fp12.ONE);
348
+ } catch {
349
+ return false;
350
+ }
351
+ }
352
+
353
+ G1.ProjectivePoint.BASE._setWindowSize(4);
354
+
355
+ return {
356
+ CURVE,
357
+ Fr,
358
+ Fp,
359
+ Fp2,
360
+ Fp6,
361
+ Fp12,
362
+ G1,
363
+ G2,
364
+ Signature,
365
+ millerLoop,
366
+ calcPairingPrecomputes,
367
+ pairing,
368
+ getPublicKey,
369
+ sign,
370
+ verify,
371
+ aggregatePublicKeys,
372
+ aggregateSignatures,
373
+ verifyBatch,
374
+ utils,
375
+ };
376
+ }