@noble/curves 1.9.5 → 2.0.0-beta.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 (211) hide show
  1. package/README.md +267 -421
  2. package/abstract/bls.d.ts +49 -111
  3. package/abstract/bls.d.ts.map +1 -1
  4. package/abstract/bls.js +108 -152
  5. package/abstract/bls.js.map +1 -1
  6. package/abstract/curve.d.ts +7 -48
  7. package/abstract/curve.d.ts.map +1 -1
  8. package/abstract/curve.js +22 -47
  9. package/abstract/curve.js.map +1 -1
  10. package/abstract/edwards.d.ts +17 -68
  11. package/abstract/edwards.d.ts.map +1 -1
  12. package/abstract/edwards.js +98 -175
  13. package/abstract/edwards.js.map +1 -1
  14. package/abstract/fft.js +14 -27
  15. package/abstract/fft.js.map +1 -1
  16. package/abstract/hash-to-curve.d.ts +11 -24
  17. package/abstract/hash-to-curve.d.ts.map +1 -1
  18. package/abstract/hash-to-curve.js +30 -35
  19. package/abstract/hash-to-curve.js.map +1 -1
  20. package/abstract/modular.d.ts +5 -17
  21. package/abstract/modular.d.ts.map +1 -1
  22. package/abstract/modular.js +166 -167
  23. package/abstract/modular.js.map +1 -1
  24. package/abstract/montgomery.d.ts +4 -9
  25. package/abstract/montgomery.d.ts.map +1 -1
  26. package/abstract/montgomery.js +17 -20
  27. package/abstract/montgomery.js.map +1 -1
  28. package/abstract/oprf.d.ts +282 -0
  29. package/abstract/oprf.d.ts.map +1 -0
  30. package/abstract/oprf.js +297 -0
  31. package/abstract/oprf.js.map +1 -0
  32. package/abstract/poseidon.js +20 -24
  33. package/abstract/poseidon.js.map +1 -1
  34. package/abstract/tower.d.ts +9 -7
  35. package/abstract/tower.d.ts.map +1 -1
  36. package/abstract/tower.js +600 -364
  37. package/abstract/tower.js.map +1 -1
  38. package/abstract/weierstrass.d.ts +12 -145
  39. package/abstract/weierstrass.d.ts.map +1 -1
  40. package/abstract/weierstrass.js +153 -377
  41. package/abstract/weierstrass.js.map +1 -1
  42. package/bls12-381.d.ts +2 -2
  43. package/bls12-381.d.ts.map +1 -1
  44. package/bls12-381.js +174 -216
  45. package/bls12-381.js.map +1 -1
  46. package/bn254.d.ts +58 -10
  47. package/bn254.d.ts.map +1 -1
  48. package/bn254.js +70 -130
  49. package/bn254.js.map +1 -1
  50. package/ed25519.d.ts +12 -31
  51. package/ed25519.d.ts.map +1 -1
  52. package/ed25519.js +104 -146
  53. package/ed25519.js.map +1 -1
  54. package/ed448.d.ts +14 -33
  55. package/ed448.d.ts.map +1 -1
  56. package/ed448.js +105 -132
  57. package/ed448.js.map +1 -1
  58. package/index.js +1 -1
  59. package/misc.d.ts +10 -14
  60. package/misc.d.ts.map +1 -1
  61. package/misc.js +51 -60
  62. package/misc.js.map +1 -1
  63. package/nist.d.ts +11 -14
  64. package/nist.d.ts.map +1 -1
  65. package/nist.js +46 -55
  66. package/nist.js.map +1 -1
  67. package/package.json +9 -224
  68. package/secp256k1.d.ts +7 -23
  69. package/secp256k1.d.ts.map +1 -1
  70. package/secp256k1.js +72 -83
  71. package/secp256k1.js.map +1 -1
  72. package/src/abstract/bls.ts +197 -344
  73. package/src/abstract/curve.ts +10 -83
  74. package/src/abstract/edwards.ts +96 -223
  75. package/src/abstract/hash-to-curve.ts +32 -45
  76. package/src/abstract/modular.ts +144 -130
  77. package/src/abstract/montgomery.ts +21 -22
  78. package/src/abstract/oprf.ts +600 -0
  79. package/src/abstract/tower.ts +627 -382
  80. package/src/abstract/weierstrass.ts +101 -482
  81. package/src/bls12-381.ts +148 -176
  82. package/src/bn254.ts +67 -122
  83. package/src/ed25519.ts +65 -118
  84. package/src/ed448.ts +63 -113
  85. package/src/index.ts +1 -1
  86. package/src/misc.ts +66 -49
  87. package/src/nist.ts +48 -57
  88. package/src/secp256k1.ts +56 -88
  89. package/src/utils.ts +41 -61
  90. package/src/webcrypto.ts +362 -0
  91. package/utils.d.ts +28 -19
  92. package/utils.d.ts.map +1 -1
  93. package/utils.js +45 -121
  94. package/utils.js.map +1 -1
  95. package/webcrypto.d.ts +47 -0
  96. package/webcrypto.d.ts.map +1 -0
  97. package/webcrypto.js +231 -0
  98. package/webcrypto.js.map +1 -0
  99. package/esm/_shortw_utils.d.ts +0 -19
  100. package/esm/_shortw_utils.d.ts.map +0 -1
  101. package/esm/_shortw_utils.js +0 -16
  102. package/esm/_shortw_utils.js.map +0 -1
  103. package/esm/abstract/bls.d.ts +0 -190
  104. package/esm/abstract/bls.d.ts.map +0 -1
  105. package/esm/abstract/bls.js +0 -408
  106. package/esm/abstract/bls.js.map +0 -1
  107. package/esm/abstract/curve.d.ts +0 -231
  108. package/esm/abstract/curve.d.ts.map +0 -1
  109. package/esm/abstract/curve.js +0 -465
  110. package/esm/abstract/curve.js.map +0 -1
  111. package/esm/abstract/edwards.d.ts +0 -237
  112. package/esm/abstract/edwards.d.ts.map +0 -1
  113. package/esm/abstract/edwards.js +0 -632
  114. package/esm/abstract/edwards.js.map +0 -1
  115. package/esm/abstract/fft.d.ts +0 -122
  116. package/esm/abstract/fft.d.ts.map +0 -1
  117. package/esm/abstract/fft.js +0 -425
  118. package/esm/abstract/fft.js.map +0 -1
  119. package/esm/abstract/hash-to-curve.d.ts +0 -102
  120. package/esm/abstract/hash-to-curve.d.ts.map +0 -1
  121. package/esm/abstract/hash-to-curve.js +0 -203
  122. package/esm/abstract/hash-to-curve.js.map +0 -1
  123. package/esm/abstract/modular.d.ts +0 -171
  124. package/esm/abstract/modular.d.ts.map +0 -1
  125. package/esm/abstract/modular.js +0 -530
  126. package/esm/abstract/modular.js.map +0 -1
  127. package/esm/abstract/montgomery.d.ts +0 -30
  128. package/esm/abstract/montgomery.d.ts.map +0 -1
  129. package/esm/abstract/montgomery.js +0 -157
  130. package/esm/abstract/montgomery.js.map +0 -1
  131. package/esm/abstract/poseidon.d.ts +0 -68
  132. package/esm/abstract/poseidon.d.ts.map +0 -1
  133. package/esm/abstract/poseidon.js +0 -296
  134. package/esm/abstract/poseidon.js.map +0 -1
  135. package/esm/abstract/tower.d.ts +0 -93
  136. package/esm/abstract/tower.d.ts.map +0 -1
  137. package/esm/abstract/tower.js +0 -502
  138. package/esm/abstract/tower.js.map +0 -1
  139. package/esm/abstract/utils.d.ts +0 -5
  140. package/esm/abstract/utils.d.ts.map +0 -1
  141. package/esm/abstract/utils.js +0 -7
  142. package/esm/abstract/utils.js.map +0 -1
  143. package/esm/abstract/weierstrass.d.ts +0 -412
  144. package/esm/abstract/weierstrass.d.ts.map +0 -1
  145. package/esm/abstract/weierstrass.js +0 -1428
  146. package/esm/abstract/weierstrass.js.map +0 -1
  147. package/esm/bls12-381.d.ts +0 -16
  148. package/esm/bls12-381.d.ts.map +0 -1
  149. package/esm/bls12-381.js +0 -738
  150. package/esm/bls12-381.js.map +0 -1
  151. package/esm/bn254.d.ts +0 -18
  152. package/esm/bn254.d.ts.map +0 -1
  153. package/esm/bn254.js +0 -246
  154. package/esm/bn254.js.map +0 -1
  155. package/esm/ed25519.d.ts +0 -106
  156. package/esm/ed25519.d.ts.map +0 -1
  157. package/esm/ed25519.js +0 -467
  158. package/esm/ed25519.js.map +0 -1
  159. package/esm/ed448.d.ts +0 -101
  160. package/esm/ed448.d.ts.map +0 -1
  161. package/esm/ed448.js +0 -448
  162. package/esm/ed448.js.map +0 -1
  163. package/esm/index.d.ts +0 -2
  164. package/esm/index.d.ts.map +0 -1
  165. package/esm/index.js +0 -17
  166. package/esm/index.js.map +0 -1
  167. package/esm/jubjub.d.ts +0 -12
  168. package/esm/jubjub.d.ts.map +0 -1
  169. package/esm/jubjub.js +0 -12
  170. package/esm/jubjub.js.map +0 -1
  171. package/esm/misc.d.ts +0 -19
  172. package/esm/misc.d.ts.map +0 -1
  173. package/esm/misc.js +0 -109
  174. package/esm/misc.js.map +0 -1
  175. package/esm/nist.d.ts +0 -21
  176. package/esm/nist.d.ts.map +0 -1
  177. package/esm/nist.js +0 -132
  178. package/esm/nist.js.map +0 -1
  179. package/esm/p256.d.ts +0 -16
  180. package/esm/p256.d.ts.map +0 -1
  181. package/esm/p256.js +0 -16
  182. package/esm/p256.js.map +0 -1
  183. package/esm/p384.d.ts +0 -16
  184. package/esm/p384.d.ts.map +0 -1
  185. package/esm/p384.js +0 -16
  186. package/esm/p384.js.map +0 -1
  187. package/esm/p521.d.ts +0 -16
  188. package/esm/p521.d.ts.map +0 -1
  189. package/esm/p521.js +0 -16
  190. package/esm/p521.js.map +0 -1
  191. package/esm/package.json +0 -4
  192. package/esm/pasta.d.ts +0 -10
  193. package/esm/pasta.d.ts.map +0 -1
  194. package/esm/pasta.js +0 -10
  195. package/esm/pasta.js.map +0 -1
  196. package/esm/secp256k1.d.ts +0 -89
  197. package/esm/secp256k1.d.ts.map +0 -1
  198. package/esm/secp256k1.js +0 -292
  199. package/esm/secp256k1.js.map +0 -1
  200. package/esm/utils.d.ts +0 -110
  201. package/esm/utils.d.ts.map +0 -1
  202. package/esm/utils.js +0 -322
  203. package/esm/utils.js.map +0 -1
  204. package/src/_shortw_utils.ts +0 -21
  205. package/src/abstract/utils.ts +0 -7
  206. package/src/jubjub.ts +0 -12
  207. package/src/p256.ts +0 -15
  208. package/src/p384.ts +0 -15
  209. package/src/p521.ts +0 -15
  210. package/src/package.json +0 -3
  211. package/src/pasta.ts +0 -9
package/bls12-381.js CHANGED
@@ -1,6 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.bls12_381 = exports.bls12_381_Fr = void 0;
4
1
  /**
5
2
  * bls12-381 is pairing-friendly Barreto-Lynn-Scott elliptic curve construction allowing to:
6
3
 
@@ -80,14 +77,14 @@ Filecoin uses little endian byte arrays for secret keys - make sure to reverse b
80
77
  * @module
81
78
  */
82
79
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
83
- const sha2_js_1 = require("@noble/hashes/sha2.js");
84
- const bls_ts_1 = require("./abstract/bls.js");
85
- const modular_ts_1 = require("./abstract/modular.js");
86
- const utils_ts_1 = require("./utils.js");
80
+ import { sha256 } from '@noble/hashes/sha2.js';
81
+ import { bls } from "./abstract/bls.js";
82
+ import { Field } from "./abstract/modular.js";
83
+ import { abytes, bitLen, bitMask, bytesToHex, bytesToNumberBE, concatBytes, copyBytes, hexToBytes, numberToBytesBE, randomBytes, } from "./utils.js";
87
84
  // Types
88
- const hash_to_curve_ts_1 = require("./abstract/hash-to-curve.js");
89
- const tower_ts_1 = require("./abstract/tower.js");
90
- const weierstrass_ts_1 = require("./abstract/weierstrass.js");
85
+ import { isogenyMap } from "./abstract/hash-to-curve.js";
86
+ import { psiFrobenius, tower12 } from "./abstract/tower.js";
87
+ import { mapToCurveSimpleSWU, weierstrass, } from "./abstract/weierstrass.js";
91
88
  // Be friendly to bad ECMAScript parsers by not using bigint literals
92
89
  // prettier-ignore
93
90
  const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3), _4n = BigInt(4);
@@ -98,7 +95,7 @@ const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3), _4n =
98
95
  const BLS_X = BigInt('0xd201000000010000');
99
96
  // t = x (called differently in different places)
100
97
  // const t = -BLS_X;
101
- const BLS_X_LEN = (0, utils_ts_1.bitLen)(BLS_X);
98
+ const BLS_X_LEN = bitLen(BLS_X);
102
99
  // a=0, b=4
103
100
  // P is characteristic of field Fp, in which curve calculations are done.
104
101
  // p = (t-1)² * (t⁴-t²+1)/3 + t
@@ -121,10 +118,13 @@ const bls12_381_CURVE_G1 = {
121
118
  Gy: BigInt('0x08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1'),
122
119
  };
123
120
  // CURVE FIELDS
124
- exports.bls12_381_Fr = (0, modular_ts_1.Field)(bls12_381_CURVE_G1.n, { modFromBytes: true });
125
- const { Fp, Fp2, Fp6, Fp4Square, Fp12 } = (0, tower_ts_1.tower12)({
126
- // Order of Fp
121
+ // r = z⁴ − z² + 1; CURVE.n from other curves
122
+ export const bls12_381_Fr = Field(bls12_381_CURVE_G1.n, {
123
+ modFromBytes: true,
124
+ });
125
+ const { Fp, Fp2, Fp6, Fp12 } = tower12({
127
126
  ORDER: bls12_381_CURVE_G1.p,
127
+ X_LEN: BLS_X_LEN,
128
128
  // Finite extension field over irreducible polynominal.
129
129
  // Fp(u) / (u² - β) where β = -1
130
130
  FP2_NONRESIDUE: [_1n, _1n],
@@ -134,42 +134,6 @@ const { Fp, Fp2, Fp6, Fp4Square, Fp12 } = (0, tower_ts_1.tower12)({
134
134
  // (T0-T1) + (T0+T1)*i
135
135
  return { c0: Fp.sub(t0, t1), c1: Fp.add(t0, t1) };
136
136
  },
137
- // Fp12
138
- // A cyclotomic group is a subgroup of Fp^n defined by
139
- // GΦₙ(p) = {α ∈ Fpⁿ : α^Φₙ(p) = 1}
140
- // The result of any pairing is in a cyclotomic subgroup
141
- // https://eprint.iacr.org/2009/565.pdf
142
- Fp12cyclotomicSquare: ({ c0, c1 }) => {
143
- const { c0: c0c0, c1: c0c1, c2: c0c2 } = c0;
144
- const { c0: c1c0, c1: c1c1, c2: c1c2 } = c1;
145
- const { first: t3, second: t4 } = Fp4Square(c0c0, c1c1);
146
- const { first: t5, second: t6 } = Fp4Square(c1c0, c0c2);
147
- const { first: t7, second: t8 } = Fp4Square(c0c1, c1c2);
148
- const t9 = Fp2.mulByNonresidue(t8); // T8 * (u + 1)
149
- return {
150
- c0: Fp6.create({
151
- c0: Fp2.add(Fp2.mul(Fp2.sub(t3, c0c0), _2n), t3), // 2 * (T3 - c0c0) + T3
152
- c1: Fp2.add(Fp2.mul(Fp2.sub(t5, c0c1), _2n), t5), // 2 * (T5 - c0c1) + T5
153
- c2: Fp2.add(Fp2.mul(Fp2.sub(t7, c0c2), _2n), t7),
154
- }), // 2 * (T7 - c0c2) + T7
155
- c1: Fp6.create({
156
- c0: Fp2.add(Fp2.mul(Fp2.add(t9, c1c0), _2n), t9), // 2 * (T9 + c1c0) + T9
157
- c1: Fp2.add(Fp2.mul(Fp2.add(t4, c1c1), _2n), t4), // 2 * (T4 + c1c1) + T4
158
- c2: Fp2.add(Fp2.mul(Fp2.add(t6, c1c2), _2n), t6),
159
- }),
160
- }; // 2 * (T6 + c1c2) + T6
161
- },
162
- Fp12cyclotomicExp(num, n) {
163
- let z = Fp12.ONE;
164
- for (let i = BLS_X_LEN - 1; i >= 0; i--) {
165
- z = Fp12._cyclotomicSquare(z);
166
- if ((0, utils_ts_1.bitGet)(n, i))
167
- z = Fp12.mul(z, num);
168
- }
169
- return z;
170
- },
171
- // https://eprint.iacr.org/2010/354.pdf
172
- // https://eprint.iacr.org/2009/565.pdf
173
137
  Fp12finalExponentiate: (num) => {
174
138
  const x = BLS_X;
175
139
  // this^(q⁶) / this
@@ -191,7 +155,7 @@ const { Fp, Fp2, Fp6, Fp4Square, Fp12 } = (0, tower_ts_1.tower12)({
191
155
  },
192
156
  });
193
157
  // GLV endomorphism Ψ(P), for fast cofactor clearing
194
- const { G2psi, G2psi2 } = (0, tower_ts_1.psiFrobenius)(Fp, Fp2, Fp2.div(Fp2.ONE, Fp2.NONRESIDUE)); // 1/(u+1)
158
+ const { G2psi, G2psi2 } = psiFrobenius(Fp, Fp2, Fp2.div(Fp2.ONE, Fp2.NONRESIDUE)); // 1/(u+1)
195
159
  /**
196
160
  * Default hash_to_field / hash-to-curve for BLS.
197
161
  * m: 1 for G1, 2 for G2
@@ -199,14 +163,14 @@ const { G2psi, G2psi2 } = (0, tower_ts_1.psiFrobenius)(Fp, Fp2, Fp2.div(Fp2.ONE,
199
163
  * hash: any function, e.g. BBS+ uses BLAKE2: see [github](https://github.com/hyperledger/aries-framework-go/issues/2247).
200
164
  * Parameter values come from [section 8.8.2 of RFC 9380](https://www.rfc-editor.org/rfc/rfc9380#section-8.8.2).
201
165
  */
202
- const htfDefaults = Object.freeze({
166
+ const hasher_opts = Object.freeze({
203
167
  DST: 'BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_',
204
168
  encodeDST: 'BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_',
205
169
  p: Fp.ORDER,
206
170
  m: 2,
207
171
  k: 128,
208
172
  expand: 'xmd',
209
- hash: sha2_js_1.sha256,
173
+ hash: sha256,
210
174
  });
211
175
  // a=0, b=4
212
176
  // cofactor h of G2
@@ -234,24 +198,25 @@ const bls12_381_CURVE_G2 = {
234
198
  // Set compressed & point-at-infinity bits
235
199
  const COMPZERO = setMask(Fp.toBytes(_0n), { infinity: true, compressed: true });
236
200
  function parseMask(bytes) {
237
- // Copy, so we can remove mask data. It will be removed also later, when Fp.create will call modulo.
238
- bytes = bytes.slice();
239
- const mask = bytes[0] & 224;
201
+ // Copy, so we can remove mask data.
202
+ // It will be removed also later, when Fp.create will call modulo.
203
+ bytes = copyBytes(bytes);
204
+ const mask = bytes[0] & 0b1110_0000;
240
205
  const compressed = !!((mask >> 7) & 1); // compression bit (0b1000_0000)
241
206
  const infinity = !!((mask >> 6) & 1); // point at infinity bit (0b0100_0000)
242
207
  const sort = !!((mask >> 5) & 1); // sort bit (0b0010_0000)
243
- bytes[0] &= 31; // clear mask (zero first 3 bits)
208
+ bytes[0] &= 0b0001_1111; // clear mask (zero first 3 bits)
244
209
  return { compressed, infinity, sort, value: bytes };
245
210
  }
246
211
  function setMask(bytes, mask) {
247
- if (bytes[0] & 224)
212
+ if (bytes[0] & 0b1110_0000)
248
213
  throw new Error('setMask: non-empty mask');
249
214
  if (mask.compressed)
250
- bytes[0] |= 128;
215
+ bytes[0] |= 0b1000_0000;
251
216
  if (mask.infinity)
252
- bytes[0] |= 64;
217
+ bytes[0] |= 0b0100_0000;
253
218
  if (mask.sort)
254
- bytes[0] |= 32;
219
+ bytes[0] |= 0b0010_0000;
255
220
  return bytes;
256
221
  }
257
222
  function pointG1ToBytes(_c, point, isComp) {
@@ -262,14 +227,14 @@ function pointG1ToBytes(_c, point, isComp) {
262
227
  if (is0)
263
228
  return COMPZERO.slice();
264
229
  const sort = Boolean((y * _2n) / P);
265
- return setMask((0, utils_ts_1.numberToBytesBE)(x, L), { compressed: true, sort });
230
+ return setMask(numberToBytesBE(x, L), { compressed: true, sort });
266
231
  }
267
232
  else {
268
233
  if (is0) {
269
- return (0, utils_ts_1.concatBytes)(Uint8Array.of(0x40), new Uint8Array(2 * L - 1));
234
+ return concatBytes(Uint8Array.of(0x40), new Uint8Array(2 * L - 1));
270
235
  }
271
236
  else {
272
- return (0, utils_ts_1.concatBytes)((0, utils_ts_1.numberToBytesBE)(x, L), (0, utils_ts_1.numberToBytesBE)(y, L));
237
+ return concatBytes(numberToBytesBE(x, L), numberToBytesBE(y, L));
273
238
  }
274
239
  }
275
240
  }
@@ -280,15 +245,15 @@ function signatureG1ToBytes(point) {
280
245
  if (point.is0())
281
246
  return COMPZERO.slice();
282
247
  const sort = Boolean((y * _2n) / P);
283
- return setMask((0, utils_ts_1.numberToBytesBE)(x, L), { compressed: true, sort });
248
+ return setMask(numberToBytesBE(x, L), { compressed: true, sort });
284
249
  }
285
250
  function pointG1FromBytes(bytes) {
286
251
  const { compressed, infinity, sort, value } = parseMask(bytes);
287
252
  const { BYTES: L, ORDER: P } = Fp;
288
253
  if (value.length === 48 && compressed) {
289
- const compressedValue = (0, utils_ts_1.bytesToNumberBE)(value);
254
+ const compressedValue = bytesToNumberBE(value);
290
255
  // Zero
291
- const x = Fp.create(compressedValue & (0, utils_ts_1.bitMask)(Fp.BITS));
256
+ const x = Fp.create(compressedValue & bitMask(Fp.BITS));
292
257
  if (infinity) {
293
258
  if (x !== _0n)
294
259
  throw new Error('invalid G1 point: non-empty, at infinity, with compression');
@@ -304,12 +269,12 @@ function pointG1FromBytes(bytes) {
304
269
  }
305
270
  else if (value.length === 96 && !compressed) {
306
271
  // Check if the infinity flag is set
307
- const x = (0, utils_ts_1.bytesToNumberBE)(value.subarray(0, L));
308
- const y = (0, utils_ts_1.bytesToNumberBE)(value.subarray(L));
272
+ const x = bytesToNumberBE(value.subarray(0, L));
273
+ const y = bytesToNumberBE(value.subarray(L));
309
274
  if (infinity) {
310
275
  if (x !== _0n || y !== _0n)
311
276
  throw new Error('G1: non-empty point at infinity');
312
- return exports.bls12_381.G1.Point.ZERO.toAffine();
277
+ return bls12_381.G1.Point.ZERO.toAffine();
313
278
  }
314
279
  return { x: Fp.create(x), y: Fp.create(y) };
315
280
  }
@@ -317,15 +282,15 @@ function pointG1FromBytes(bytes) {
317
282
  throw new Error('invalid G1 point: expected 48/96 bytes');
318
283
  }
319
284
  }
320
- function signatureG1FromBytes(hex) {
321
- const { infinity, sort, value } = parseMask((0, utils_ts_1.ensureBytes)('signatureHex', hex, 48));
285
+ function signatureG1FromBytes(bytes) {
286
+ const { infinity, sort, value } = parseMask(abytes(bytes, 48, 'signature'));
322
287
  const P = Fp.ORDER;
323
- const Point = exports.bls12_381.G1.Point;
324
- const compressedValue = (0, utils_ts_1.bytesToNumberBE)(value);
288
+ const Point = bls12_381.G1.Point;
289
+ const compressedValue = bytesToNumberBE(value);
325
290
  // Zero
326
291
  if (infinity)
327
292
  return Point.ZERO;
328
- const x = Fp.create(compressedValue & (0, utils_ts_1.bitMask)(Fp.BITS));
293
+ const x = Fp.create(compressedValue & bitMask(Fp.BITS));
329
294
  const right = Fp.add(Fp.pow(x, _3n), Fp.create(bls12_381_CURVE_G1.b)); // y² = x³ + b
330
295
  let y = Fp.sqrt(right);
331
296
  if (!y)
@@ -343,30 +308,30 @@ function pointG2ToBytes(_c, point, isComp) {
343
308
  const { x, y } = point.toAffine();
344
309
  if (isComp) {
345
310
  if (is0)
346
- return (0, utils_ts_1.concatBytes)(COMPZERO, (0, utils_ts_1.numberToBytesBE)(_0n, L));
311
+ return concatBytes(COMPZERO, numberToBytesBE(_0n, L));
347
312
  const flag = Boolean(y.c1 === _0n ? (y.c0 * _2n) / P : (y.c1 * _2n) / P);
348
- return (0, utils_ts_1.concatBytes)(setMask((0, utils_ts_1.numberToBytesBE)(x.c1, L), { compressed: true, sort: flag }), (0, utils_ts_1.numberToBytesBE)(x.c0, L));
313
+ return concatBytes(setMask(numberToBytesBE(x.c1, L), { compressed: true, sort: flag }), numberToBytesBE(x.c0, L));
349
314
  }
350
315
  else {
351
316
  if (is0)
352
- return (0, utils_ts_1.concatBytes)(Uint8Array.of(0x40), new Uint8Array(4 * L - 1));
317
+ return concatBytes(Uint8Array.of(0x40), new Uint8Array(4 * L - 1));
353
318
  const { re: x0, im: x1 } = Fp2.reim(x);
354
319
  const { re: y0, im: y1 } = Fp2.reim(y);
355
- return (0, utils_ts_1.concatBytes)((0, utils_ts_1.numberToBytesBE)(x1, L), (0, utils_ts_1.numberToBytesBE)(x0, L), (0, utils_ts_1.numberToBytesBE)(y1, L), (0, utils_ts_1.numberToBytesBE)(y0, L));
320
+ return concatBytes(numberToBytesBE(x1, L), numberToBytesBE(x0, L), numberToBytesBE(y1, L), numberToBytesBE(y0, L));
356
321
  }
357
322
  }
358
323
  function signatureG2ToBytes(point) {
359
324
  point.assertValidity();
360
325
  const { BYTES: L } = Fp;
361
326
  if (point.is0())
362
- return (0, utils_ts_1.concatBytes)(COMPZERO, (0, utils_ts_1.numberToBytesBE)(_0n, L));
327
+ return concatBytes(COMPZERO, numberToBytesBE(_0n, L));
363
328
  const { x, y } = point.toAffine();
364
329
  const { re: x0, im: x1 } = Fp2.reim(x);
365
330
  const { re: y0, im: y1 } = Fp2.reim(y);
366
331
  const tmp = y1 > _0n ? y1 * _2n : y0 * _2n;
367
332
  const sort = Boolean((tmp / Fp.ORDER) & _1n);
368
333
  const z2 = x0;
369
- return (0, utils_ts_1.concatBytes)(setMask((0, utils_ts_1.numberToBytesBE)(x1, L), { sort, compressed: true }), (0, utils_ts_1.numberToBytesBE)(z2, L));
334
+ return concatBytes(setMask(numberToBytesBE(x1, L), { sort, compressed: true }), numberToBytesBE(z2, L));
370
335
  }
371
336
  function pointG2FromBytes(bytes) {
372
337
  const { BYTES: L, ORDER: P } = Fp;
@@ -375,9 +340,9 @@ function pointG2FromBytes(bytes) {
375
340
  (!compressed && infinity && sort) || // 01100000
376
341
  (sort && infinity && compressed) // 11100000
377
342
  ) {
378
- throw new Error('invalid encoding flag: ' + (bytes[0] & 224));
343
+ throw new Error('invalid encoding flag: ' + (bytes[0] & 0b1110_0000));
379
344
  }
380
- const slc = (b, from, to) => (0, utils_ts_1.bytesToNumberBE)(b.slice(from, to));
345
+ const slc = (b, from, to) => bytesToNumberBE(b.slice(from, to));
381
346
  if (value.length === 96 && compressed) {
382
347
  if (infinity) {
383
348
  // check that all bytes are 0
@@ -412,20 +377,20 @@ function pointG2FromBytes(bytes) {
412
377
  throw new Error('invalid G2 point: expected 96/192 bytes');
413
378
  }
414
379
  }
415
- function signatureG2FromBytes(hex) {
380
+ function signatureG2FromBytes(bytes) {
416
381
  const { ORDER: P } = Fp;
417
382
  // TODO: Optimize, it's very slow because of sqrt.
418
- const { infinity, sort, value } = parseMask((0, utils_ts_1.ensureBytes)('signatureHex', hex));
419
- const Point = exports.bls12_381.G2.Point;
383
+ const { infinity, sort, value } = parseMask(abytes(bytes));
384
+ const Point = bls12_381.G2.Point;
420
385
  const half = value.length / 2;
421
386
  if (half !== 48 && half !== 96)
422
387
  throw new Error('invalid compressed signature length, expected 96/192 bytes');
423
- const z1 = (0, utils_ts_1.bytesToNumberBE)(value.slice(0, half));
424
- const z2 = (0, utils_ts_1.bytesToNumberBE)(value.slice(half));
388
+ const z1 = bytesToNumberBE(value.slice(0, half));
389
+ const z2 = bytesToNumberBE(value.slice(half));
425
390
  // Indicates the infinity point
426
391
  if (infinity)
427
392
  return Point.ZERO;
428
- const x1 = Fp.create(z1 & (0, utils_ts_1.bitMask)(Fp.BITS));
393
+ const x1 = Fp.create(z1 & bitMask(Fp.BITS));
429
394
  const x2 = Fp.create(z2);
430
395
  const x = Fp2.create({ c0: x2, c1: x1 });
431
396
  const y2 = Fp2.add(Fp2.pow(x, _3n), bls12_381_CURVE_G2.b); // y² = x³ + 4
@@ -445,6 +410,121 @@ function signatureG2FromBytes(hex) {
445
410
  point.assertValidity();
446
411
  return point;
447
412
  }
413
+ const signatureCoders = {
414
+ ShortSignature: {
415
+ fromBytes(bytes) {
416
+ return signatureG1FromBytes(abytes(bytes));
417
+ },
418
+ fromHex(hex) {
419
+ return signatureG1FromBytes(hexToBytes(hex));
420
+ },
421
+ toBytes(point) {
422
+ return signatureG1ToBytes(point);
423
+ },
424
+ toRawBytes(point) {
425
+ return signatureG1ToBytes(point);
426
+ },
427
+ toHex(point) {
428
+ return bytesToHex(signatureG1ToBytes(point));
429
+ },
430
+ },
431
+ LongSignature: {
432
+ fromBytes(bytes) {
433
+ return signatureG2FromBytes(abytes(bytes));
434
+ },
435
+ fromHex(hex) {
436
+ return signatureG2FromBytes(hexToBytes(hex));
437
+ },
438
+ toBytes(point) {
439
+ return signatureG2ToBytes(point);
440
+ },
441
+ toRawBytes(point) {
442
+ return signatureG2ToBytes(point);
443
+ },
444
+ toHex(point) {
445
+ return bytesToHex(signatureG2ToBytes(point));
446
+ },
447
+ },
448
+ };
449
+ const fields = {
450
+ Fp,
451
+ Fp2,
452
+ Fp6,
453
+ Fp12,
454
+ Fr: bls12_381_Fr,
455
+ };
456
+ const G1_Point = weierstrass(bls12_381_CURVE_G1, {
457
+ allowInfinityPoint: true,
458
+ Fn: bls12_381_Fr,
459
+ fromBytes: pointG1FromBytes,
460
+ toBytes: pointG1ToBytes,
461
+ // Checks is the point resides in prime-order subgroup.
462
+ // point.isTorsionFree() should return true for valid points
463
+ // It returns false for shitty points.
464
+ // https://eprint.iacr.org/2021/1130.pdf
465
+ isTorsionFree: (c, point) => {
466
+ // GLV endomorphism ψ(P)
467
+ const beta = BigInt('0x5f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe');
468
+ const phi = new c(Fp.mul(point.X, beta), point.Y, point.Z);
469
+ // TODO: unroll
470
+ const xP = point.multiplyUnsafe(BLS_X).negate(); // [x]P
471
+ const u2P = xP.multiplyUnsafe(BLS_X); // [u2]P
472
+ return u2P.equals(phi);
473
+ },
474
+ // Clear cofactor of G1
475
+ // https://eprint.iacr.org/2019/403
476
+ clearCofactor: (_c, point) => {
477
+ // return this.multiplyUnsafe(CURVE.h);
478
+ return point.multiplyUnsafe(BLS_X).add(point); // x*P + P
479
+ },
480
+ });
481
+ const G2_Point = weierstrass(bls12_381_CURVE_G2, {
482
+ Fp: Fp2,
483
+ allowInfinityPoint: true,
484
+ Fn: bls12_381_Fr,
485
+ fromBytes: pointG2FromBytes,
486
+ toBytes: pointG2ToBytes,
487
+ // https://eprint.iacr.org/2021/1130.pdf
488
+ // Older version: https://eprint.iacr.org/2019/814.pdf
489
+ isTorsionFree: (c, P) => {
490
+ return P.multiplyUnsafe(BLS_X).negate().equals(G2psi(c, P)); // ψ(P) == [u](P)
491
+ },
492
+ // clear_cofactor_bls12381_g2 from RFC 9380.
493
+ // https://eprint.iacr.org/2017/419.pdf
494
+ // prettier-ignore
495
+ clearCofactor: (c, P) => {
496
+ const x = BLS_X;
497
+ let t1 = P.multiplyUnsafe(x).negate(); // [-x]P
498
+ let t2 = G2psi(c, P); // Ψ(P)
499
+ let t3 = P.double(); // 2P
500
+ t3 = G2psi2(c, t3); // Ψ²(2P)
501
+ t3 = t3.subtract(t2); // Ψ²(2P) - Ψ(P)
502
+ t2 = t1.add(t2); // [-x]P + Ψ(P)
503
+ t2 = t2.multiplyUnsafe(x).negate(); // [x²]P - [x]Ψ(P)
504
+ t3 = t3.add(t2); // Ψ²(2P) - Ψ(P) + [x²]P - [x]Ψ(P)
505
+ t3 = t3.subtract(t1); // Ψ²(2P) - Ψ(P) + [x²]P - [x]Ψ(P) + [x]P
506
+ const Q = t3.subtract(P); // Ψ²(2P) - Ψ(P) + [x²]P - [x]Ψ(P) + [x]P - 1P
507
+ return Q; // [x²-x-1]P + [x-1]Ψ(P) + Ψ²(2P)
508
+ },
509
+ });
510
+ const bls12_hasher_opts = {
511
+ mapToG1: mapToG1,
512
+ mapToG2: mapToG2,
513
+ hasherOpts: hasher_opts,
514
+ hasherOptsG1: { ...hasher_opts, m: 1, DST: 'BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_NUL_' },
515
+ hasherOptsG2: { ...hasher_opts },
516
+ };
517
+ const bls12_params = {
518
+ ateLoopSize: BLS_X, // The BLS parameter x for BLS12-381
519
+ xNegative: true,
520
+ twistType: 'multiplicative',
521
+ randomBytes: randomBytes,
522
+ // https://datatracker.ietf.org/doc/html/rfc9380#name-clearing-the-cofactor
523
+ // https://datatracker.ietf.org/doc/html/rfc9380#name-cofactor-clearing-for-bls12
524
+ // G2hEff: BigInt(
525
+ // '0xbc69f08f2ee75b3584c6a0ea91b352888e2a8e9145ad7689986ff031508ffe1329c2f178731db956d82bf015d1212b02ec0ec69d7477c1ae954cbc06689f6a359894c0adebbf6b4e8020005aaa95551'
526
+ // ),
527
+ };
448
528
  /**
449
529
  * bls12-381 pairing-friendly curve.
450
530
  * @example
@@ -456,131 +536,9 @@ function signatureG2FromBytes(hex) {
456
536
  * const signature = bls.sign(message, privateKey);
457
537
  * const isValid = bls.verify(signature, message, publicKey);
458
538
  */
459
- exports.bls12_381 = (0, bls_ts_1.bls)({
460
- // Fields
461
- fields: {
462
- Fp,
463
- Fp2,
464
- Fp6,
465
- Fp12,
466
- Fr: exports.bls12_381_Fr,
467
- },
468
- // G1: y² = x³ + 4
469
- G1: {
470
- ...bls12_381_CURVE_G1,
471
- Fp,
472
- htfDefaults: { ...htfDefaults, m: 1, DST: 'BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_NUL_' },
473
- wrapPrivateKey: true,
474
- allowInfinityPoint: true,
475
- // Checks is the point resides in prime-order subgroup.
476
- // point.isTorsionFree() should return true for valid points
477
- // It returns false for shitty points.
478
- // https://eprint.iacr.org/2021/1130.pdf
479
- isTorsionFree: (c, point) => {
480
- // GLV endomorphism ψ(P)
481
- const beta = BigInt('0x5f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe');
482
- const phi = new c(Fp.mul(point.X, beta), point.Y, point.Z);
483
- // TODO: unroll
484
- const xP = point.multiplyUnsafe(BLS_X).negate(); // [x]P
485
- const u2P = xP.multiplyUnsafe(BLS_X); // [u2]P
486
- return u2P.equals(phi);
487
- },
488
- // Clear cofactor of G1
489
- // https://eprint.iacr.org/2019/403
490
- clearCofactor: (_c, point) => {
491
- // return this.multiplyUnsafe(CURVE.h);
492
- return point.multiplyUnsafe(BLS_X).add(point); // x*P + P
493
- },
494
- mapToCurve: mapToG1,
495
- fromBytes: pointG1FromBytes,
496
- toBytes: pointG1ToBytes,
497
- ShortSignature: {
498
- fromBytes(bytes) {
499
- (0, utils_ts_1.abytes)(bytes);
500
- return signatureG1FromBytes(bytes);
501
- },
502
- fromHex(hex) {
503
- return signatureG1FromBytes(hex);
504
- },
505
- toBytes(point) {
506
- return signatureG1ToBytes(point);
507
- },
508
- toRawBytes(point) {
509
- return signatureG1ToBytes(point);
510
- },
511
- toHex(point) {
512
- return (0, utils_ts_1.bytesToHex)(signatureG1ToBytes(point));
513
- },
514
- },
515
- },
516
- G2: {
517
- ...bls12_381_CURVE_G2,
518
- Fp: Fp2,
519
- // https://datatracker.ietf.org/doc/html/rfc9380#name-clearing-the-cofactor
520
- // https://datatracker.ietf.org/doc/html/rfc9380#name-cofactor-clearing-for-bls12
521
- hEff: BigInt('0xbc69f08f2ee75b3584c6a0ea91b352888e2a8e9145ad7689986ff031508ffe1329c2f178731db956d82bf015d1212b02ec0ec69d7477c1ae954cbc06689f6a359894c0adebbf6b4e8020005aaa95551'),
522
- htfDefaults: { ...htfDefaults },
523
- wrapPrivateKey: true,
524
- allowInfinityPoint: true,
525
- mapToCurve: mapToG2,
526
- // Checks is the point resides in prime-order subgroup.
527
- // point.isTorsionFree() should return true for valid points
528
- // It returns false for shitty points.
529
- // https://eprint.iacr.org/2021/1130.pdf
530
- // Older version: https://eprint.iacr.org/2019/814.pdf
531
- isTorsionFree: (c, P) => {
532
- return P.multiplyUnsafe(BLS_X).negate().equals(G2psi(c, P)); // ψ(P) == [u](P)
533
- },
534
- // Maps the point into the prime-order subgroup G2.
535
- // clear_cofactor_bls12381_g2 from RFC 9380.
536
- // https://eprint.iacr.org/2017/419.pdf
537
- // prettier-ignore
538
- clearCofactor: (c, P) => {
539
- const x = BLS_X;
540
- let t1 = P.multiplyUnsafe(x).negate(); // [-x]P
541
- let t2 = G2psi(c, P); // Ψ(P)
542
- let t3 = P.double(); // 2P
543
- t3 = G2psi2(c, t3); // Ψ²(2P)
544
- t3 = t3.subtract(t2); // Ψ²(2P) - Ψ(P)
545
- t2 = t1.add(t2); // [-x]P + Ψ(P)
546
- t2 = t2.multiplyUnsafe(x).negate(); // [x²]P - [x]Ψ(P)
547
- t3 = t3.add(t2); // Ψ²(2P) - Ψ(P) + [x²]P - [x]Ψ(P)
548
- t3 = t3.subtract(t1); // Ψ²(2P) - Ψ(P) + [x²]P - [x]Ψ(P) + [x]P
549
- const Q = t3.subtract(P); // Ψ²(2P) - Ψ(P) + [x²]P - [x]Ψ(P) + [x]P - 1P
550
- return Q; // [x²-x-1]P + [x-1]Ψ(P) + Ψ²(2P)
551
- },
552
- fromBytes: pointG2FromBytes,
553
- toBytes: pointG2ToBytes,
554
- Signature: {
555
- fromBytes(bytes) {
556
- (0, utils_ts_1.abytes)(bytes);
557
- return signatureG2FromBytes(bytes);
558
- },
559
- fromHex(hex) {
560
- return signatureG2FromBytes(hex);
561
- },
562
- toBytes(point) {
563
- return signatureG2ToBytes(point);
564
- },
565
- toRawBytes(point) {
566
- return signatureG2ToBytes(point);
567
- },
568
- toHex(point) {
569
- return (0, utils_ts_1.bytesToHex)(signatureG2ToBytes(point));
570
- },
571
- },
572
- },
573
- params: {
574
- ateLoopSize: BLS_X, // The BLS parameter x for BLS12-381
575
- r: bls12_381_CURVE_G1.n, // order; z⁴ − z² + 1; CURVE.n from other curves
576
- xNegative: true,
577
- twistType: 'multiplicative',
578
- },
579
- htfDefaults,
580
- hash: sha2_js_1.sha256,
581
- });
539
+ export const bls12_381 = bls(fields, G1_Point, G2_Point, bls12_params, bls12_hasher_opts, signatureCoders);
582
540
  // 3-isogeny map from E' to E https://www.rfc-editor.org/rfc/rfc9380#appendix-E.3
583
- const isogenyMapG2 = (0, hash_to_curve_ts_1.isogenyMap)(Fp2, [
541
+ const isogenyMapG2 = isogenyMap(Fp2, [
584
542
  // xNum
585
543
  [
586
544
  [
@@ -649,7 +607,7 @@ const isogenyMapG2 = (0, hash_to_curve_ts_1.isogenyMap)(Fp2, [
649
607
  ],
650
608
  ].map((i) => i.map((pair) => Fp2.fromBigTuple(pair.map(BigInt)))));
651
609
  // 11-isogeny map from E' to E
652
- const isogenyMapG1 = (0, hash_to_curve_ts_1.isogenyMap)(Fp, [
610
+ const isogenyMapG1 = isogenyMap(Fp, [
653
611
  // xNum
654
612
  [
655
613
  '0x11a05f2b1e833340b809101dd99815856b303e88a2d7005ff2627b56cdb4e2c85610c2d5f2e62d6eaeac1662734649b7',
@@ -719,13 +677,13 @@ const isogenyMapG1 = (0, hash_to_curve_ts_1.isogenyMap)(Fp, [
719
677
  ],
720
678
  ].map((i) => i.map((j) => BigInt(j))));
721
679
  // Optimized SWU Map - Fp to G1
722
- const G1_SWU = (0, weierstrass_ts_1.mapToCurveSimpleSWU)(Fp, {
680
+ const G1_SWU = mapToCurveSimpleSWU(Fp, {
723
681
  A: Fp.create(BigInt('0x144698a3b8e9433d693a02c96d4982b0ea985383ee66a8d8e8981aefd881ac98936f8da0e0f97f5cf428082d584c1d')),
724
682
  B: Fp.create(BigInt('0x12e2908d11688030018b12e8753eee3b2016c1f0f24f4070a0b9c14fcef35ef55a23215a316ceaa5d1cc48e98e172be0')),
725
683
  Z: Fp.create(BigInt(11)),
726
684
  });
727
685
  // SWU Map - Fp2 to G2': y² = x³ + 240i * x + 1012 + 1012i
728
- const G2_SWU = (0, weierstrass_ts_1.mapToCurveSimpleSWU)(Fp2, {
686
+ const G2_SWU = mapToCurveSimpleSWU(Fp2, {
729
687
  A: Fp2.create({ c0: Fp.create(_0n), c1: Fp.create(BigInt(240)) }), // A' = 240 * I
730
688
  B: Fp2.create({ c0: Fp.create(BigInt(1012)), c1: Fp.create(BigInt(1012)) }), // B' = 1012 * (1 + I)
731
689
  Z: Fp2.create({ c0: Fp.create(BigInt(-2)), c1: Fp.create(BigInt(-1)) }), // Z: -(2 + I)