bls12-381.js 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.bls12_381 = void 0;
  4. /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
  5. const sha256_1 = require("@noble/hashes/sha256");
  6. const utils_1 = require("@noble/hashes/utils");
  7. const bls_js_1 = require("./abstract/bls.js");
  8. const mod = require("./abstract/modular.js");
  9. const utils_js_1 = require("./abstract/utils.js");
  10. // Types
  11. const hash_to_curve_js_1 = require("./abstract/hash-to-curve.js");
  12. const weierstrass_js_1 = require("./abstract/weierstrass.js");
  13. /*
  14. bls12-381 is pairing-friendly Barreto-Lynn-Scott elliptic curve construction allowing to:
  15. - Construct zk-SNARKs at the 120-bit security
  16. - Efficiently verify N aggregate signatures with 1 pairing and N ec additions:
  17. the Boneh-Lynn-Shacham signature scheme is orders of magnitude more efficient than Schnorr
  18. ### Summary
  19. 1. BLS Relies on Bilinear Pairing (expensive)
  20. 2. Private Keys: 32 bytes
  21. 3. Public Keys: 48 bytes: 381 bit affine x coordinate, encoded into 48 big-endian bytes.
  22. 4. Signatures: 96 bytes: two 381 bit integers (affine x coordinate), encoded into two 48 big-endian byte arrays.
  23. - The signature is a point on the G2 subgroup, which is defined over a finite field
  24. with elements twice as big as the G1 curve (G2 is over Fp2 rather than Fp. Fp2 is analogous to the complex numbers).
  25. 5. The 12 stands for the Embedding degree.
  26. ### Formulas
  27. - `P = pk x G` - public keys
  28. - `S = pk x H(m)` - signing
  29. - `e(P, H(m)) == e(G, S)` - verification using pairings
  30. - `e(G, S) = e(G, SUM(n)(Si)) = MUL(n)(e(G, Si))` - signature aggregation
  31. ### Compatibility and notes
  32. 1. It is compatible with Algorand, Chia, Dfinity, Ethereum, Filecoin, ZEC
  33. Filecoin uses little endian byte arrays for private keys - make sure to reverse byte order.
  34. 2. Some projects use G2 for public keys and G1 for signatures. It's called "short signature"
  35. 3. Curve security level is about 120 bits as per Barbulescu-Duquesne 2017
  36. https://hal.science/hal-01534101/file/main.pdf
  37. 4. Compatible with specs:
  38. [cfrg-pairing-friendly-curves-11](https://tools.ietf.org/html/draft-irtf-cfrg-pairing-friendly-curves-11),
  39. [cfrg-bls-signature-05](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-bls-signature-05),
  40. [RFC 9380](https://www.rfc-editor.org/rfc/rfc9380).
  41. */
  42. // Be friendly to bad ECMAScript parsers by not using bigint literals
  43. // prettier-ignore
  44. const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3), _4n = BigInt(4);
  45. // prettier-ignore
  46. const _8n = BigInt(8), _16n = BigInt(16);
  47. // CURVE FIELDS
  48. // Finite field over p.
  49. const Fp_raw = BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab');
  50. const Fp = mod.Field(Fp_raw);
  51. // Finite field over r.
  52. // This particular field is not used anywhere in bls12-381, but it is still useful.
  53. const Fr = mod.Field(BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001'));
  54. const Fp2Add = ({ c0, c1 }, { c0: r0, c1: r1 }) => ({
  55. c0: Fp.add(c0, r0),
  56. c1: Fp.add(c1, r1),
  57. });
  58. const Fp2Subtract = ({ c0, c1 }, { c0: r0, c1: r1 }) => ({
  59. c0: Fp.sub(c0, r0),
  60. c1: Fp.sub(c1, r1),
  61. });
  62. const Fp2Multiply = ({ c0, c1 }, rhs) => {
  63. if (typeof rhs === 'bigint')
  64. return { c0: Fp.mul(c0, rhs), c1: Fp.mul(c1, rhs) };
  65. // (a+bi)(c+di) = (ac−bd) + (ad+bc)i
  66. const { c0: r0, c1: r1 } = rhs;
  67. let t1 = Fp.mul(c0, r0); // c0 * o0
  68. let t2 = Fp.mul(c1, r1); // c1 * o1
  69. // (T1 - T2) + ((c0 + c1) * (r0 + r1) - (T1 + T2))*i
  70. const o0 = Fp.sub(t1, t2);
  71. const o1 = Fp.sub(Fp.mul(Fp.add(c0, c1), Fp.add(r0, r1)), Fp.add(t1, t2));
  72. return { c0: o0, c1: o1 };
  73. };
  74. const Fp2Square = ({ c0, c1 }) => {
  75. const a = Fp.add(c0, c1);
  76. const b = Fp.sub(c0, c1);
  77. const c = Fp.add(c0, c0);
  78. return { c0: Fp.mul(a, b), c1: Fp.mul(c, c1) };
  79. };
  80. // G2 is the order-q subgroup of E2(Fp²) : y² = x³+4(1+√−1),
  81. // where Fp2 is Fp[√−1]/(x2+1). #E2(Fp2 ) = h2q, where
  82. // G² - 1
  83. // h2q
  84. // NOTE: ORDER was wrong!
  85. const FP2_ORDER = Fp_raw * Fp_raw;
  86. const Fp2 = {
  87. ORDER: FP2_ORDER,
  88. BITS: (0, utils_js_1.bitLen)(FP2_ORDER),
  89. BYTES: Math.ceil((0, utils_js_1.bitLen)(FP2_ORDER) / 8),
  90. MASK: (0, utils_js_1.bitMask)((0, utils_js_1.bitLen)(FP2_ORDER)),
  91. ZERO: { c0: Fp.ZERO, c1: Fp.ZERO },
  92. ONE: { c0: Fp.ONE, c1: Fp.ZERO },
  93. create: (num) => num,
  94. isValid: ({ c0, c1 }) => typeof c0 === 'bigint' && typeof c1 === 'bigint',
  95. is0: ({ c0, c1 }) => Fp.is0(c0) && Fp.is0(c1),
  96. eql: ({ c0, c1 }, { c0: r0, c1: r1 }) => Fp.eql(c0, r0) && Fp.eql(c1, r1),
  97. neg: ({ c0, c1 }) => ({ c0: Fp.neg(c0), c1: Fp.neg(c1) }),
  98. pow: (num, power) => mod.FpPow(Fp2, num, power),
  99. invertBatch: (nums) => mod.FpInvertBatch(Fp2, nums),
  100. // Normalized
  101. add: Fp2Add,
  102. sub: Fp2Subtract,
  103. mul: Fp2Multiply,
  104. sqr: Fp2Square,
  105. // NonNormalized stuff
  106. addN: Fp2Add,
  107. subN: Fp2Subtract,
  108. mulN: Fp2Multiply,
  109. sqrN: Fp2Square,
  110. // Why inversion for bigint inside Fp instead of Fp2? it is even used in that context?
  111. div: (lhs, rhs) => Fp2.mul(lhs, typeof rhs === 'bigint' ? Fp.inv(Fp.create(rhs)) : Fp2.inv(rhs)),
  112. inv: ({ c0: a, c1: b }) => {
  113. // We wish to find the multiplicative inverse of a nonzero
  114. // element a + bu in Fp2. We leverage an identity
  115. //
  116. // (a + bu)(a - bu) = a² + b²
  117. //
  118. // which holds because u² = -1. This can be rewritten as
  119. //
  120. // (a + bu)(a - bu)/(a² + b²) = 1
  121. //
  122. // because a² + b² = 0 has no nonzero solutions for (a, b).
  123. // This gives that (a - bu)/(a² + b²) is the inverse
  124. // of (a + bu). Importantly, this can be computing using
  125. // only a single inversion in Fp.
  126. const factor = Fp.inv(Fp.create(a * a + b * b));
  127. return { c0: Fp.mul(factor, Fp.create(a)), c1: Fp.mul(factor, Fp.create(-b)) };
  128. },
  129. sqrt: (num) => {
  130. if (Fp2.eql(num, Fp2.ZERO))
  131. return Fp2.ZERO; // Algo doesn't handles this case
  132. // TODO: Optimize this line. It's extremely slow.
  133. // Speeding this up would boost aggregateSignatures.
  134. // https://eprint.iacr.org/2012/685.pdf applicable?
  135. // https://github.com/zkcrypto/bls12_381/blob/080eaa74ec0e394377caa1ba302c8c121df08b07/src/fp2.rs#L250
  136. // https://github.com/supranational/blst/blob/aae0c7d70b799ac269ff5edf29d8191dbd357876/src/exp2.c#L1
  137. // Inspired by https://github.com/dalek-cryptography/curve25519-dalek/blob/17698df9d4c834204f83a3574143abacb4fc81a5/src/field.rs#L99
  138. const candidateSqrt = Fp2.pow(num, (Fp2.ORDER + _8n) / _16n);
  139. const check = Fp2.div(Fp2.sqr(candidateSqrt), num); // candidateSqrt.square().div(this);
  140. const R = FP2_ROOTS_OF_UNITY;
  141. const divisor = [R[0], R[2], R[4], R[6]].find((r) => Fp2.eql(r, check));
  142. if (!divisor)
  143. throw new Error('No root');
  144. const index = R.indexOf(divisor);
  145. const root = R[index / 2];
  146. if (!root)
  147. throw new Error('Invalid root');
  148. const x1 = Fp2.div(candidateSqrt, root);
  149. const x2 = Fp2.neg(x1);
  150. const { re: re1, im: im1 } = Fp2.reim(x1);
  151. const { re: re2, im: im2 } = Fp2.reim(x2);
  152. if (im1 > im2 || (im1 === im2 && re1 > re2))
  153. return x1;
  154. return x2;
  155. },
  156. // Same as sgn0_m_eq_2 in RFC 9380
  157. isOdd: (x) => {
  158. const { re: x0, im: x1 } = Fp2.reim(x);
  159. const sign_0 = x0 % _2n;
  160. const zero_0 = x0 === _0n;
  161. const sign_1 = x1 % _2n;
  162. return BigInt(sign_0 || (zero_0 && sign_1)) == _1n;
  163. },
  164. // Bytes util
  165. fromBytes(b) {
  166. if (b.length !== Fp2.BYTES)
  167. throw new Error(`fromBytes wrong length=${b.length}`);
  168. return { c0: Fp.fromBytes(b.subarray(0, Fp.BYTES)), c1: Fp.fromBytes(b.subarray(Fp.BYTES)) };
  169. },
  170. toBytes: ({ c0, c1 }) => (0, utils_js_1.concatBytes)(Fp.toBytes(c0), Fp.toBytes(c1)),
  171. cmov: ({ c0, c1 }, { c0: r0, c1: r1 }, c) => ({
  172. c0: Fp.cmov(c0, r0, c),
  173. c1: Fp.cmov(c1, r1, c),
  174. }),
  175. // Specific utils
  176. // toString() {
  177. // return `Fp2(${this.c0} + ${this.c1}×i)`;
  178. // }
  179. reim: ({ c0, c1 }) => ({ re: c0, im: c1 }),
  180. // multiply by u + 1
  181. mulByNonresidue: ({ c0, c1 }) => ({ c0: Fp.sub(c0, c1), c1: Fp.add(c0, c1) }),
  182. multiplyByB: ({ c0, c1 }) => {
  183. let t0 = Fp.mul(c0, _4n); // 4 * c0
  184. let t1 = Fp.mul(c1, _4n); // 4 * c1
  185. // (T0-T1) + (T0+T1)*i
  186. return { c0: Fp.sub(t0, t1), c1: Fp.add(t0, t1) };
  187. },
  188. fromBigTuple: (tuple) => {
  189. if (tuple.length !== 2)
  190. throw new Error('Invalid tuple');
  191. const fps = tuple.map((n) => Fp.create(n));
  192. return { c0: fps[0], c1: fps[1] };
  193. },
  194. frobeniusMap: ({ c0, c1 }, power) => ({
  195. c0,
  196. c1: Fp.mul(c1, FP2_FROBENIUS_COEFFICIENTS[power % 2]),
  197. }),
  198. };
  199. // Finite extension field over irreducible polynominal.
  200. // Fp(u) / (u² - β) where β = -1
  201. const FP2_FROBENIUS_COEFFICIENTS = [
  202. BigInt('0x1'),
  203. BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaa'),
  204. ].map((item) => Fp.create(item));
  205. // For Fp2 roots of unity.
  206. const rv1 = BigInt('0x6af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09');
  207. // const ev1 =
  208. // BigInt('0x699be3b8c6870965e5bf892ad5d2cc7b0e85a117402dfd83b7f4a947e02d978498255a2aaec0ac627b5afbdf1bf1c90');
  209. // const ev2 =
  210. // BigInt('0x8157cd83046453f5dd0972b6e3949e4288020b5b8a9cc99ca07e27089a2ce2436d965026adad3ef7baba37f2183e9b5');
  211. // const ev3 =
  212. // BigInt('0xab1c2ffdd6c253ca155231eb3e71ba044fd562f6f72bc5bad5ec46a0b7a3b0247cf08ce6c6317f40edbc653a72dee17');
  213. // const ev4 =
  214. // BigInt('0xaa404866706722864480885d68ad0ccac1967c7544b447873cc37e0181271e006df72162a3d3e0287bf597fbf7f8fc1');
  215. // Eighth roots of unity, used for computing square roots in Fp2.
  216. // To verify or re-calculate:
  217. // Array(8).fill(new Fp2([1n, 1n])).map((fp2, k) => fp2.pow(Fp2.ORDER * BigInt(k) / 8n))
  218. const FP2_ROOTS_OF_UNITY = [
  219. [_1n, _0n],
  220. [rv1, -rv1],
  221. [_0n, _1n],
  222. [rv1, rv1],
  223. [-_1n, _0n],
  224. [-rv1, rv1],
  225. [_0n, -_1n],
  226. [-rv1, -rv1],
  227. ].map((pair) => Fp2.fromBigTuple(pair));
  228. const Fp6Add = ({ c0, c1, c2 }, { c0: r0, c1: r1, c2: r2 }) => ({
  229. c0: Fp2.add(c0, r0),
  230. c1: Fp2.add(c1, r1),
  231. c2: Fp2.add(c2, r2),
  232. });
  233. const Fp6Subtract = ({ c0, c1, c2 }, { c0: r0, c1: r1, c2: r2 }) => ({
  234. c0: Fp2.sub(c0, r0),
  235. c1: Fp2.sub(c1, r1),
  236. c2: Fp2.sub(c2, r2),
  237. });
  238. const Fp6Multiply = ({ c0, c1, c2 }, rhs) => {
  239. if (typeof rhs === 'bigint') {
  240. return {
  241. c0: Fp2.mul(c0, rhs),
  242. c1: Fp2.mul(c1, rhs),
  243. c2: Fp2.mul(c2, rhs),
  244. };
  245. }
  246. const { c0: r0, c1: r1, c2: r2 } = rhs;
  247. const t0 = Fp2.mul(c0, r0); // c0 * o0
  248. const t1 = Fp2.mul(c1, r1); // c1 * o1
  249. const t2 = Fp2.mul(c2, r2); // c2 * o2
  250. return {
  251. // t0 + (c1 + c2) * (r1 * r2) - (T1 + T2) * (u + 1)
  252. c0: Fp2.add(t0, Fp2.mulByNonresidue(Fp2.sub(Fp2.mul(Fp2.add(c1, c2), Fp2.add(r1, r2)), Fp2.add(t1, t2)))),
  253. // (c0 + c1) * (r0 + r1) - (T0 + T1) + T2 * (u + 1)
  254. c1: Fp2.add(Fp2.sub(Fp2.mul(Fp2.add(c0, c1), Fp2.add(r0, r1)), Fp2.add(t0, t1)), Fp2.mulByNonresidue(t2)),
  255. // T1 + (c0 + c2) * (r0 + r2) - T0 + T2
  256. c2: Fp2.sub(Fp2.add(t1, Fp2.mul(Fp2.add(c0, c2), Fp2.add(r0, r2))), Fp2.add(t0, t2)),
  257. };
  258. };
  259. const Fp6Square = ({ c0, c1, c2 }) => {
  260. let t0 = Fp2.sqr(c0); // c0²
  261. let t1 = Fp2.mul(Fp2.mul(c0, c1), _2n); // 2 * c0 * c1
  262. let t3 = Fp2.mul(Fp2.mul(c1, c2), _2n); // 2 * c1 * c2
  263. let t4 = Fp2.sqr(c2); // c2²
  264. return {
  265. c0: Fp2.add(Fp2.mulByNonresidue(t3), t0), // T3 * (u + 1) + T0
  266. c1: Fp2.add(Fp2.mulByNonresidue(t4), t1), // T4 * (u + 1) + T1
  267. // T1 + (c0 - c1 + c2)² + T3 - T0 - T4
  268. c2: Fp2.sub(Fp2.sub(Fp2.add(Fp2.add(t1, Fp2.sqr(Fp2.add(Fp2.sub(c0, c1), c2))), t3), t0), t4),
  269. };
  270. };
  271. const Fp6 = {
  272. ORDER: Fp2.ORDER, // TODO: unused, but need to verify
  273. BITS: 3 * Fp2.BITS,
  274. BYTES: 3 * Fp2.BYTES,
  275. MASK: (0, utils_js_1.bitMask)(3 * Fp2.BITS),
  276. ZERO: { c0: Fp2.ZERO, c1: Fp2.ZERO, c2: Fp2.ZERO },
  277. ONE: { c0: Fp2.ONE, c1: Fp2.ZERO, c2: Fp2.ZERO },
  278. create: (num) => num,
  279. isValid: ({ c0, c1, c2 }) => Fp2.isValid(c0) && Fp2.isValid(c1) && Fp2.isValid(c2),
  280. is0: ({ c0, c1, c2 }) => Fp2.is0(c0) && Fp2.is0(c1) && Fp2.is0(c2),
  281. neg: ({ c0, c1, c2 }) => ({ c0: Fp2.neg(c0), c1: Fp2.neg(c1), c2: Fp2.neg(c2) }),
  282. eql: ({ c0, c1, c2 }, { c0: r0, c1: r1, c2: r2 }) => Fp2.eql(c0, r0) && Fp2.eql(c1, r1) && Fp2.eql(c2, r2),
  283. sqrt: () => {
  284. throw new Error('Not implemented');
  285. },
  286. // Do we need division by bigint at all? Should be done via order:
  287. div: (lhs, rhs) => Fp6.mul(lhs, typeof rhs === 'bigint' ? Fp.inv(Fp.create(rhs)) : Fp6.inv(rhs)),
  288. pow: (num, power) => mod.FpPow(Fp6, num, power),
  289. invertBatch: (nums) => mod.FpInvertBatch(Fp6, nums),
  290. // Normalized
  291. add: Fp6Add,
  292. sub: Fp6Subtract,
  293. mul: Fp6Multiply,
  294. sqr: Fp6Square,
  295. // NonNormalized stuff
  296. addN: Fp6Add,
  297. subN: Fp6Subtract,
  298. mulN: Fp6Multiply,
  299. sqrN: Fp6Square,
  300. inv: ({ c0, c1, c2 }) => {
  301. let t0 = Fp2.sub(Fp2.sqr(c0), Fp2.mulByNonresidue(Fp2.mul(c2, c1))); // c0² - c2 * c1 * (u + 1)
  302. let t1 = Fp2.sub(Fp2.mulByNonresidue(Fp2.sqr(c2)), Fp2.mul(c0, c1)); // c2² * (u + 1) - c0 * c1
  303. let t2 = Fp2.sub(Fp2.sqr(c1), Fp2.mul(c0, c2)); // c1² - c0 * c2
  304. // 1/(((c2 * T1 + c1 * T2) * v) + c0 * T0)
  305. let t4 = Fp2.inv(Fp2.add(Fp2.mulByNonresidue(Fp2.add(Fp2.mul(c2, t1), Fp2.mul(c1, t2))), Fp2.mul(c0, t0)));
  306. return { c0: Fp2.mul(t4, t0), c1: Fp2.mul(t4, t1), c2: Fp2.mul(t4, t2) };
  307. },
  308. // Bytes utils
  309. fromBytes: (b) => {
  310. if (b.length !== Fp6.BYTES)
  311. throw new Error(`fromBytes wrong length=${b.length}`);
  312. return {
  313. c0: Fp2.fromBytes(b.subarray(0, Fp2.BYTES)),
  314. c1: Fp2.fromBytes(b.subarray(Fp2.BYTES, 2 * Fp2.BYTES)),
  315. c2: Fp2.fromBytes(b.subarray(2 * Fp2.BYTES)),
  316. };
  317. },
  318. toBytes: ({ c0, c1, c2 }) => (0, utils_js_1.concatBytes)(Fp2.toBytes(c0), Fp2.toBytes(c1), Fp2.toBytes(c2)),
  319. cmov: ({ c0, c1, c2 }, { c0: r0, c1: r1, c2: r2 }, c) => ({
  320. c0: Fp2.cmov(c0, r0, c),
  321. c1: Fp2.cmov(c1, r1, c),
  322. c2: Fp2.cmov(c2, r2, c),
  323. }),
  324. // Utils
  325. // fromTriple(triple: [Fp2, Fp2, Fp2]) {
  326. // return new Fp6(...triple);
  327. // }
  328. // toString() {
  329. // return `Fp6(${this.c0} + ${this.c1} * v, ${this.c2} * v^2)`;
  330. // }
  331. fromBigSix: (t) => {
  332. if (!Array.isArray(t) || t.length !== 6)
  333. throw new Error('Invalid Fp6 usage');
  334. return {
  335. c0: Fp2.fromBigTuple(t.slice(0, 2)),
  336. c1: Fp2.fromBigTuple(t.slice(2, 4)),
  337. c2: Fp2.fromBigTuple(t.slice(4, 6)),
  338. };
  339. },
  340. frobeniusMap: ({ c0, c1, c2 }, power) => ({
  341. c0: Fp2.frobeniusMap(c0, power),
  342. c1: Fp2.mul(Fp2.frobeniusMap(c1, power), FP6_FROBENIUS_COEFFICIENTS_1[power % 6]),
  343. c2: Fp2.mul(Fp2.frobeniusMap(c2, power), FP6_FROBENIUS_COEFFICIENTS_2[power % 6]),
  344. }),
  345. mulByNonresidue: ({ c0, c1, c2 }) => ({ c0: Fp2.mulByNonresidue(c2), c1: c0, c2: c1 }),
  346. // Sparse multiplication
  347. multiplyBy1: ({ c0, c1, c2 }, b1) => ({
  348. c0: Fp2.mulByNonresidue(Fp2.mul(c2, b1)),
  349. c1: Fp2.mul(c0, b1),
  350. c2: Fp2.mul(c1, b1),
  351. }),
  352. // Sparse multiplication
  353. multiplyBy01({ c0, c1, c2 }, b0, b1) {
  354. let t0 = Fp2.mul(c0, b0); // c0 * b0
  355. let t1 = Fp2.mul(c1, b1); // c1 * b1
  356. return {
  357. // ((c1 + c2) * b1 - T1) * (u + 1) + T0
  358. c0: Fp2.add(Fp2.mulByNonresidue(Fp2.sub(Fp2.mul(Fp2.add(c1, c2), b1), t1)), t0),
  359. // (b0 + b1) * (c0 + c1) - T0 - T1
  360. c1: Fp2.sub(Fp2.sub(Fp2.mul(Fp2.add(b0, b1), Fp2.add(c0, c1)), t0), t1),
  361. // (c0 + c2) * b0 - T0 + T1
  362. c2: Fp2.add(Fp2.sub(Fp2.mul(Fp2.add(c0, c2), b0), t0), t1),
  363. };
  364. },
  365. multiplyByFp2: ({ c0, c1, c2 }, rhs) => ({
  366. c0: Fp2.mul(c0, rhs),
  367. c1: Fp2.mul(c1, rhs),
  368. c2: Fp2.mul(c2, rhs),
  369. }),
  370. };
  371. const FP6_FROBENIUS_COEFFICIENTS_1 = [
  372. [BigInt('0x1'), BigInt('0x0')],
  373. [
  374. BigInt('0x0'),
  375. BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'),
  376. ],
  377. [
  378. BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'),
  379. BigInt('0x0'),
  380. ],
  381. [BigInt('0x0'), BigInt('0x1')],
  382. [
  383. BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'),
  384. BigInt('0x0'),
  385. ],
  386. [
  387. BigInt('0x0'),
  388. BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'),
  389. ],
  390. ].map((pair) => Fp2.fromBigTuple(pair));
  391. const FP6_FROBENIUS_COEFFICIENTS_2 = [
  392. [BigInt('0x1'), BigInt('0x0')],
  393. [
  394. BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaad'),
  395. BigInt('0x0'),
  396. ],
  397. [
  398. BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'),
  399. BigInt('0x0'),
  400. ],
  401. [
  402. BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaa'),
  403. BigInt('0x0'),
  404. ],
  405. [
  406. BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'),
  407. BigInt('0x0'),
  408. ],
  409. [
  410. BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffeffff'),
  411. BigInt('0x0'),
  412. ],
  413. ].map((pair) => Fp2.fromBigTuple(pair));
  414. // The BLS parameter x for BLS12-381
  415. const BLS_X = BigInt('0xd201000000010000');
  416. const BLS_X_LEN = (0, utils_js_1.bitLen)(BLS_X);
  417. const Fp12Add = ({ c0, c1 }, { c0: r0, c1: r1 }) => ({
  418. c0: Fp6.add(c0, r0),
  419. c1: Fp6.add(c1, r1),
  420. });
  421. const Fp12Subtract = ({ c0, c1 }, { c0: r0, c1: r1 }) => ({
  422. c0: Fp6.sub(c0, r0),
  423. c1: Fp6.sub(c1, r1),
  424. });
  425. const Fp12Multiply = ({ c0, c1 }, rhs) => {
  426. if (typeof rhs === 'bigint')
  427. return { c0: Fp6.mul(c0, rhs), c1: Fp6.mul(c1, rhs) };
  428. let { c0: r0, c1: r1 } = rhs;
  429. let t1 = Fp6.mul(c0, r0); // c0 * r0
  430. let t2 = Fp6.mul(c1, r1); // c1 * r1
  431. return {
  432. c0: Fp6.add(t1, Fp6.mulByNonresidue(t2)), // T1 + T2 * v
  433. // (c0 + c1) * (r0 + r1) - (T1 + T2)
  434. c1: Fp6.sub(Fp6.mul(Fp6.add(c0, c1), Fp6.add(r0, r1)), Fp6.add(t1, t2)),
  435. };
  436. };
  437. const Fp12Square = ({ c0, c1 }) => {
  438. let ab = Fp6.mul(c0, c1); // c0 * c1
  439. return {
  440. // (c1 * v + c0) * (c0 + c1) - AB - AB * v
  441. c0: Fp6.sub(Fp6.sub(Fp6.mul(Fp6.add(Fp6.mulByNonresidue(c1), c0), Fp6.add(c0, c1)), ab), Fp6.mulByNonresidue(ab)),
  442. c1: Fp6.add(ab, ab),
  443. }; // AB + AB
  444. };
  445. function Fp4Square(a, b) {
  446. const a2 = Fp2.sqr(a);
  447. const b2 = Fp2.sqr(b);
  448. return {
  449. first: Fp2.add(Fp2.mulByNonresidue(b2), a2), // b² * Nonresidue + a²
  450. second: Fp2.sub(Fp2.sub(Fp2.sqr(Fp2.add(a, b)), a2), b2), // (a + b)² - a² - b²
  451. };
  452. }
  453. const Fp12 = {
  454. ORDER: Fp2.ORDER, // TODO: unused, but need to verify
  455. BITS: 2 * Fp2.BITS,
  456. BYTES: 2 * Fp2.BYTES,
  457. MASK: (0, utils_js_1.bitMask)(2 * Fp2.BITS),
  458. ZERO: { c0: Fp6.ZERO, c1: Fp6.ZERO },
  459. ONE: { c0: Fp6.ONE, c1: Fp6.ZERO },
  460. create: (num) => num,
  461. isValid: ({ c0, c1 }) => Fp6.isValid(c0) && Fp6.isValid(c1),
  462. is0: ({ c0, c1 }) => Fp6.is0(c0) && Fp6.is0(c1),
  463. neg: ({ c0, c1 }) => ({ c0: Fp6.neg(c0), c1: Fp6.neg(c1) }),
  464. eql: ({ c0, c1 }, { c0: r0, c1: r1 }) => Fp6.eql(c0, r0) && Fp6.eql(c1, r1),
  465. sqrt: () => {
  466. throw new Error('Not implemented');
  467. },
  468. inv: ({ c0, c1 }) => {
  469. let t = Fp6.inv(Fp6.sub(Fp6.sqr(c0), Fp6.mulByNonresidue(Fp6.sqr(c1)))); // 1 / (c0² - c1² * v)
  470. return { c0: Fp6.mul(c0, t), c1: Fp6.neg(Fp6.mul(c1, t)) }; // ((C0 * T) * T) + (-C1 * T) * w
  471. },
  472. div: (lhs, rhs) => Fp12.mul(lhs, typeof rhs === 'bigint' ? Fp.inv(Fp.create(rhs)) : Fp12.inv(rhs)),
  473. pow: (num, power) => mod.FpPow(Fp12, num, power),
  474. invertBatch: (nums) => mod.FpInvertBatch(Fp12, nums),
  475. // Normalized
  476. add: Fp12Add,
  477. sub: Fp12Subtract,
  478. mul: Fp12Multiply,
  479. sqr: Fp12Square,
  480. // NonNormalized stuff
  481. addN: Fp12Add,
  482. subN: Fp12Subtract,
  483. mulN: Fp12Multiply,
  484. sqrN: Fp12Square,
  485. // Bytes utils
  486. fromBytes: (b) => {
  487. if (b.length !== Fp12.BYTES)
  488. throw new Error(`fromBytes wrong length=${b.length}`);
  489. return {
  490. c0: Fp6.fromBytes(b.subarray(0, Fp6.BYTES)),
  491. c1: Fp6.fromBytes(b.subarray(Fp6.BYTES)),
  492. };
  493. },
  494. toBytes: ({ c0, c1 }) => (0, utils_js_1.concatBytes)(Fp6.toBytes(c0), Fp6.toBytes(c1)),
  495. cmov: ({ c0, c1 }, { c0: r0, c1: r1 }, c) => ({
  496. c0: Fp6.cmov(c0, r0, c),
  497. c1: Fp6.cmov(c1, r1, c),
  498. }),
  499. // Utils
  500. // toString() {
  501. // return `Fp12(${this.c0} + ${this.c1} * w)`;
  502. // },
  503. // fromTuple(c: [Fp6, Fp6]) {
  504. // return new Fp12(...c);
  505. // }
  506. fromBigTwelve: (t) => ({
  507. c0: Fp6.fromBigSix(t.slice(0, 6)),
  508. c1: Fp6.fromBigSix(t.slice(6, 12)),
  509. }),
  510. // Raises to q**i -th power
  511. frobeniusMap(lhs, power) {
  512. const r0 = Fp6.frobeniusMap(lhs.c0, power);
  513. const { c0, c1, c2 } = Fp6.frobeniusMap(lhs.c1, power);
  514. const coeff = FP12_FROBENIUS_COEFFICIENTS[power % 12];
  515. return {
  516. c0: r0,
  517. c1: Fp6.create({
  518. c0: Fp2.mul(c0, coeff),
  519. c1: Fp2.mul(c1, coeff),
  520. c2: Fp2.mul(c2, coeff),
  521. }),
  522. };
  523. },
  524. // Sparse multiplication
  525. multiplyBy014: ({ c0, c1 }, o0, o1, o4) => {
  526. let t0 = Fp6.multiplyBy01(c0, o0, o1);
  527. let t1 = Fp6.multiplyBy1(c1, o4);
  528. return {
  529. c0: Fp6.add(Fp6.mulByNonresidue(t1), t0), // T1 * v + T0
  530. // (c1 + c0) * [o0, o1+o4] - T0 - T1
  531. c1: Fp6.sub(Fp6.sub(Fp6.multiplyBy01(Fp6.add(c1, c0), o0, Fp2.add(o1, o4)), t0), t1),
  532. };
  533. },
  534. multiplyByFp2: ({ c0, c1 }, rhs) => ({
  535. c0: Fp6.multiplyByFp2(c0, rhs),
  536. c1: Fp6.multiplyByFp2(c1, rhs),
  537. }),
  538. conjugate: ({ c0, c1 }) => ({ c0, c1: Fp6.neg(c1) }),
  539. // A cyclotomic group is a subgroup of Fp^n defined by
  540. // GΦₙ(p) = {α ∈ Fpⁿ : α^Φₙ(p) = 1}
  541. // The result of any pairing is in a cyclotomic subgroup
  542. // https://eprint.iacr.org/2009/565.pdf
  543. _cyclotomicSquare: ({ c0, c1 }) => {
  544. const { c0: c0c0, c1: c0c1, c2: c0c2 } = c0;
  545. const { c0: c1c0, c1: c1c1, c2: c1c2 } = c1;
  546. const { first: t3, second: t4 } = Fp4Square(c0c0, c1c1);
  547. const { first: t5, second: t6 } = Fp4Square(c1c0, c0c2);
  548. const { first: t7, second: t8 } = Fp4Square(c0c1, c1c2);
  549. let t9 = Fp2.mulByNonresidue(t8); // T8 * (u + 1)
  550. return {
  551. c0: Fp6.create({
  552. c0: Fp2.add(Fp2.mul(Fp2.sub(t3, c0c0), _2n), t3), // 2 * (T3 - c0c0) + T3
  553. c1: Fp2.add(Fp2.mul(Fp2.sub(t5, c0c1), _2n), t5), // 2 * (T5 - c0c1) + T5
  554. c2: Fp2.add(Fp2.mul(Fp2.sub(t7, c0c2), _2n), t7),
  555. }), // 2 * (T7 - c0c2) + T7
  556. c1: Fp6.create({
  557. c0: Fp2.add(Fp2.mul(Fp2.add(t9, c1c0), _2n), t9), // 2 * (T9 + c1c0) + T9
  558. c1: Fp2.add(Fp2.mul(Fp2.add(t4, c1c1), _2n), t4), // 2 * (T4 + c1c1) + T4
  559. c2: Fp2.add(Fp2.mul(Fp2.add(t6, c1c2), _2n), t6),
  560. }),
  561. }; // 2 * (T6 + c1c2) + T6
  562. },
  563. _cyclotomicExp(num, n) {
  564. let z = Fp12.ONE;
  565. for (let i = BLS_X_LEN - 1; i >= 0; i--) {
  566. z = Fp12._cyclotomicSquare(z);
  567. if ((0, utils_js_1.bitGet)(n, i))
  568. z = Fp12.mul(z, num);
  569. }
  570. return z;
  571. },
  572. // https://eprint.iacr.org/2010/354.pdf
  573. // https://eprint.iacr.org/2009/565.pdf
  574. finalExponentiate: (num) => {
  575. const x = BLS_X;
  576. // this^(q⁶) / this
  577. const t0 = Fp12.div(Fp12.frobeniusMap(num, 6), num);
  578. // t0^(q²) * t0
  579. const t1 = Fp12.mul(Fp12.frobeniusMap(t0, 2), t0);
  580. const t2 = Fp12.conjugate(Fp12._cyclotomicExp(t1, x));
  581. const t3 = Fp12.mul(Fp12.conjugate(Fp12._cyclotomicSquare(t1)), t2);
  582. const t4 = Fp12.conjugate(Fp12._cyclotomicExp(t3, x));
  583. const t5 = Fp12.conjugate(Fp12._cyclotomicExp(t4, x));
  584. const t6 = Fp12.mul(Fp12.conjugate(Fp12._cyclotomicExp(t5, x)), Fp12._cyclotomicSquare(t2));
  585. const t7 = Fp12.conjugate(Fp12._cyclotomicExp(t6, x));
  586. const t2_t5_pow_q2 = Fp12.frobeniusMap(Fp12.mul(t2, t5), 2);
  587. const t4_t1_pow_q3 = Fp12.frobeniusMap(Fp12.mul(t4, t1), 3);
  588. const t6_t1c_pow_q1 = Fp12.frobeniusMap(Fp12.mul(t6, Fp12.conjugate(t1)), 1);
  589. const t7_t3c_t1 = Fp12.mul(Fp12.mul(t7, Fp12.conjugate(t3)), t1);
  590. // (t2 * t5)^(q²) * (t4 * t1)^(q³) * (t6 * t1.conj)^(q^1) * t7 * t3.conj * t1
  591. return Fp12.mul(Fp12.mul(Fp12.mul(t2_t5_pow_q2, t4_t1_pow_q3), t6_t1c_pow_q1), t7_t3c_t1);
  592. },
  593. };
  594. const FP12_FROBENIUS_COEFFICIENTS = [
  595. [BigInt('0x1'), BigInt('0x0')],
  596. [
  597. BigInt('0x1904d3bf02bb0667c231beb4202c0d1f0fd603fd3cbd5f4f7b2443d784bab9c4f67ea53d63e7813d8d0775ed92235fb8'),
  598. BigInt('0x00fc3e2b36c4e03288e9e902231f9fb854a14787b6c7b36fec0c8ec971f63c5f282d5ac14d6c7ec22cf78a126ddc4af3'),
  599. ],
  600. [
  601. BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffeffff'),
  602. BigInt('0x0'),
  603. ],
  604. [
  605. BigInt('0x135203e60180a68ee2e9c448d77a2cd91c3dedd930b1cf60ef396489f61eb45e304466cf3e67fa0af1ee7b04121bdea2'),
  606. BigInt('0x06af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09'),
  607. ],
  608. [
  609. BigInt('0x00000000000000005f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe'),
  610. BigInt('0x0'),
  611. ],
  612. [
  613. BigInt('0x144e4211384586c16bd3ad4afa99cc9170df3560e77982d0db45f3536814f0bd5871c1908bd478cd1ee605167ff82995'),
  614. BigInt('0x05b2cfd9013a5fd8df47fa6b48b1e045f39816240c0b8fee8beadf4d8e9c0566c63a3e6e257f87329b18fae980078116'),
  615. ],
  616. [
  617. BigInt('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaaa'),
  618. BigInt('0x0'),
  619. ],
  620. [
  621. BigInt('0x00fc3e2b36c4e03288e9e902231f9fb854a14787b6c7b36fec0c8ec971f63c5f282d5ac14d6c7ec22cf78a126ddc4af3'),
  622. BigInt('0x1904d3bf02bb0667c231beb4202c0d1f0fd603fd3cbd5f4f7b2443d784bab9c4f67ea53d63e7813d8d0775ed92235fb8'),
  623. ],
  624. [
  625. BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac'),
  626. BigInt('0x0'),
  627. ],
  628. [
  629. BigInt('0x06af0e0437ff400b6831e36d6bd17ffe48395dabc2d3435e77f76e17009241c5ee67992f72ec05f4c81084fbede3cc09'),
  630. BigInt('0x135203e60180a68ee2e9c448d77a2cd91c3dedd930b1cf60ef396489f61eb45e304466cf3e67fa0af1ee7b04121bdea2'),
  631. ],
  632. [
  633. BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaad'),
  634. BigInt('0x0'),
  635. ],
  636. [
  637. BigInt('0x05b2cfd9013a5fd8df47fa6b48b1e045f39816240c0b8fee8beadf4d8e9c0566c63a3e6e257f87329b18fae980078116'),
  638. BigInt('0x144e4211384586c16bd3ad4afa99cc9170df3560e77982d0db45f3536814f0bd5871c1908bd478cd1ee605167ff82995'),
  639. ],
  640. ].map((n) => Fp2.fromBigTuple(n));
  641. // END OF CURVE FIELDS
  642. // HashToCurve
  643. // 3-isogeny map from E' to E https://www.rfc-editor.org/rfc/rfc9380#appendix-E.3
  644. const isogenyMapG2 = (0, hash_to_curve_js_1.isogenyMap)(Fp2, [
  645. // xNum
  646. [
  647. [
  648. '0x5c759507e8e333ebb5b7a9a47d7ed8532c52d39fd3a042a88b58423c50ae15d5c2638e343d9c71c6238aaaaaaaa97d6',
  649. '0x5c759507e8e333ebb5b7a9a47d7ed8532c52d39fd3a042a88b58423c50ae15d5c2638e343d9c71c6238aaaaaaaa97d6',
  650. ],
  651. [
  652. '0x0',
  653. '0x11560bf17baa99bc32126fced787c88f984f87adf7ae0c7f9a208c6b4f20a4181472aaa9cb8d555526a9ffffffffc71a',
  654. ],
  655. [
  656. '0x11560bf17baa99bc32126fced787c88f984f87adf7ae0c7f9a208c6b4f20a4181472aaa9cb8d555526a9ffffffffc71e',
  657. '0x8ab05f8bdd54cde190937e76bc3e447cc27c3d6fbd7063fcd104635a790520c0a395554e5c6aaaa9354ffffffffe38d',
  658. ],
  659. [
  660. '0x171d6541fa38ccfaed6dea691f5fb614cb14b4e7f4e810aa22d6108f142b85757098e38d0f671c7188e2aaaaaaaa5ed1',
  661. '0x0',
  662. ],
  663. ],
  664. // xDen
  665. [
  666. [
  667. '0x0',
  668. '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaa63',
  669. ],
  670. [
  671. '0xc',
  672. '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaa9f',
  673. ],
  674. ['0x1', '0x0'], // LAST 1
  675. ],
  676. // yNum
  677. [
  678. [
  679. '0x1530477c7ab4113b59a4c18b076d11930f7da5d4a07f649bf54439d87d27e500fc8c25ebf8c92f6812cfc71c71c6d706',
  680. '0x1530477c7ab4113b59a4c18b076d11930f7da5d4a07f649bf54439d87d27e500fc8c25ebf8c92f6812cfc71c71c6d706',
  681. ],
  682. [
  683. '0x0',
  684. '0x5c759507e8e333ebb5b7a9a47d7ed8532c52d39fd3a042a88b58423c50ae15d5c2638e343d9c71c6238aaaaaaaa97be',
  685. ],
  686. [
  687. '0x11560bf17baa99bc32126fced787c88f984f87adf7ae0c7f9a208c6b4f20a4181472aaa9cb8d555526a9ffffffffc71c',
  688. '0x8ab05f8bdd54cde190937e76bc3e447cc27c3d6fbd7063fcd104635a790520c0a395554e5c6aaaa9354ffffffffe38f',
  689. ],
  690. [
  691. '0x124c9ad43b6cf79bfbf7043de3811ad0761b0f37a1e26286b0e977c69aa274524e79097a56dc4bd9e1b371c71c718b10',
  692. '0x0',
  693. ],
  694. ],
  695. // yDen
  696. [
  697. [
  698. '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffa8fb',
  699. '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffa8fb',
  700. ],
  701. [
  702. '0x0',
  703. '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffa9d3',
  704. ],
  705. [
  706. '0x12',
  707. '0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaa99',
  708. ],
  709. ['0x1', '0x0'], // LAST 1
  710. ],
  711. ].map((i) => i.map((pair) => Fp2.fromBigTuple(pair.map(BigInt)))));
  712. // 11-isogeny map from E' to E
  713. const isogenyMapG1 = (0, hash_to_curve_js_1.isogenyMap)(Fp, [
  714. // xNum
  715. [
  716. '0x11a05f2b1e833340b809101dd99815856b303e88a2d7005ff2627b56cdb4e2c85610c2d5f2e62d6eaeac1662734649b7',
  717. '0x17294ed3e943ab2f0588bab22147a81c7c17e75b2f6a8417f565e33c70d1e86b4838f2a6f318c356e834eef1b3cb83bb',
  718. '0xd54005db97678ec1d1048c5d10a9a1bce032473295983e56878e501ec68e25c958c3e3d2a09729fe0179f9dac9edcb0',
  719. '0x1778e7166fcc6db74e0609d307e55412d7f5e4656a8dbf25f1b33289f1b330835336e25ce3107193c5b388641d9b6861',
  720. '0xe99726a3199f4436642b4b3e4118e5499db995a1257fb3f086eeb65982fac18985a286f301e77c451154ce9ac8895d9',
  721. '0x1630c3250d7313ff01d1201bf7a74ab5db3cb17dd952799b9ed3ab9097e68f90a0870d2dcae73d19cd13c1c66f652983',
  722. '0xd6ed6553fe44d296a3726c38ae652bfb11586264f0f8ce19008e218f9c86b2a8da25128c1052ecaddd7f225a139ed84',
  723. '0x17b81e7701abdbe2e8743884d1117e53356de5ab275b4db1a682c62ef0f2753339b7c8f8c8f475af9ccb5618e3f0c88e',
  724. '0x80d3cf1f9a78fc47b90b33563be990dc43b756ce79f5574a2c596c928c5d1de4fa295f296b74e956d71986a8497e317',
  725. '0x169b1f8e1bcfa7c42e0c37515d138f22dd2ecb803a0c5c99676314baf4bb1b7fa3190b2edc0327797f241067be390c9e',
  726. '0x10321da079ce07e272d8ec09d2565b0dfa7dccdde6787f96d50af36003b14866f69b771f8c285decca67df3f1605fb7b',
  727. '0x6e08c248e260e70bd1e962381edee3d31d79d7e22c837bc23c0bf1bc24c6b68c24b1b80b64d391fa9c8ba2e8ba2d229',
  728. ],
  729. // xDen
  730. [
  731. '0x8ca8d548cff19ae18b2e62f4bd3fa6f01d5ef4ba35b48ba9c9588617fc8ac62b558d681be343df8993cf9fa40d21b1c',
  732. '0x12561a5deb559c4348b4711298e536367041e8ca0cf0800c0126c2588c48bf5713daa8846cb026e9e5c8276ec82b3bff',
  733. '0xb2962fe57a3225e8137e629bff2991f6f89416f5a718cd1fca64e00b11aceacd6a3d0967c94fedcfcc239ba5cb83e19',
  734. '0x3425581a58ae2fec83aafef7c40eb545b08243f16b1655154cca8abc28d6fd04976d5243eecf5c4130de8938dc62cd8',
  735. '0x13a8e162022914a80a6f1d5f43e7a07dffdfc759a12062bb8d6b44e833b306da9bd29ba81f35781d539d395b3532a21e',
  736. '0xe7355f8e4e667b955390f7f0506c6e9395735e9ce9cad4d0a43bcef24b8982f7400d24bc4228f11c02df9a29f6304a5',
  737. '0x772caacf16936190f3e0c63e0596721570f5799af53a1894e2e073062aede9cea73b3538f0de06cec2574496ee84a3a',
  738. '0x14a7ac2a9d64a8b230b3f5b074cf01996e7f63c21bca68a81996e1cdf9822c580fa5b9489d11e2d311f7d99bbdcc5a5e',
  739. '0xa10ecf6ada54f825e920b3dafc7a3cce07f8d1d7161366b74100da67f39883503826692abba43704776ec3a79a1d641',
  740. '0x95fc13ab9e92ad4476d6e3eb3a56680f682b4ee96f7d03776df533978f31c1593174e4b4b7865002d6384d168ecdd0a',
  741. '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001', // LAST 1
  742. ],
  743. // yNum
  744. [
  745. '0x90d97c81ba24ee0259d1f094980dcfa11ad138e48a869522b52af6c956543d3cd0c7aee9b3ba3c2be9845719707bb33',
  746. '0x134996a104ee5811d51036d776fb46831223e96c254f383d0f906343eb67ad34d6c56711962fa8bfe097e75a2e41c696',
  747. '0xcc786baa966e66f4a384c86a3b49942552e2d658a31ce2c344be4b91400da7d26d521628b00523b8dfe240c72de1f6',
  748. '0x1f86376e8981c217898751ad8746757d42aa7b90eeb791c09e4a3ec03251cf9de405aba9ec61deca6355c77b0e5f4cb',
  749. '0x8cc03fdefe0ff135caf4fe2a21529c4195536fbe3ce50b879833fd221351adc2ee7f8dc099040a841b6daecf2e8fedb',
  750. '0x16603fca40634b6a2211e11db8f0a6a074a7d0d4afadb7bd76505c3d3ad5544e203f6326c95a807299b23ab13633a5f0',
  751. '0x4ab0b9bcfac1bbcb2c977d027796b3ce75bb8ca2be184cb5231413c4d634f3747a87ac2460f415ec961f8855fe9d6f2',
  752. '0x987c8d5333ab86fde9926bd2ca6c674170a05bfe3bdd81ffd038da6c26c842642f64550fedfe935a15e4ca31870fb29',
  753. '0x9fc4018bd96684be88c9e221e4da1bb8f3abd16679dc26c1e8b6e6a1f20cabe69d65201c78607a360370e577bdba587',
  754. '0xe1bba7a1186bdb5223abde7ada14a23c42a0ca7915af6fe06985e7ed1e4d43b9b3f7055dd4eba6f2bafaaebca731c30',
  755. '0x19713e47937cd1be0dfd0b8f1d43fb93cd2fcbcb6caf493fd1183e416389e61031bf3a5cce3fbafce813711ad011c132',
  756. '0x18b46a908f36f6deb918c143fed2edcc523559b8aaf0c2462e6bfe7f911f643249d9cdf41b44d606ce07c8a4d0074d8e',
  757. '0xb182cac101b9399d155096004f53f447aa7b12a3426b08ec02710e807b4633f06c851c1919211f20d4c04f00b971ef8',
  758. '0x245a394ad1eca9b72fc00ae7be315dc757b3b080d4c158013e6632d3c40659cc6cf90ad1c232a6442d9d3f5db980133',
  759. '0x5c129645e44cf1102a159f748c4a3fc5e673d81d7e86568d9ab0f5d396a7ce46ba1049b6579afb7866b1e715475224b',
  760. '0x15e6be4e990f03ce4ea50b3b42df2eb5cb181d8f84965a3957add4fa95af01b2b665027efec01c7704b456be69c8b604',
  761. ],
  762. // yDen
  763. [
  764. '0x16112c4c3a9c98b252181140fad0eae9601a6de578980be6eec3232b5be72e7a07f3688ef60c206d01479253b03663c1',
  765. '0x1962d75c2381201e1a0cbd6c43c348b885c84ff731c4d59ca4a10356f453e01f78a4260763529e3532f6102c2e49a03d',
  766. '0x58df3306640da276faaae7d6e8eb15778c4855551ae7f310c35a5dd279cd2eca6757cd636f96f891e2538b53dbf67f2',
  767. '0x16b7d288798e5395f20d23bf89edb4d1d115c5dbddbcd30e123da489e726af41727364f2c28297ada8d26d98445f5416',
  768. '0xbe0e079545f43e4b00cc912f8228ddcc6d19c9f0f69bbb0542eda0fc9dec916a20b15dc0fd2ededda39142311a5001d',
  769. '0x8d9e5297186db2d9fb266eaac783182b70152c65550d881c5ecd87b6f0f5a6449f38db9dfa9cce202c6477faaf9b7ac',
  770. '0x166007c08a99db2fc3ba8734ace9824b5eecfdfa8d0cf8ef5dd365bc400a0051d5fa9c01a58b1fb93d1a1399126a775c',
  771. '0x16a3ef08be3ea7ea03bcddfabba6ff6ee5a4375efa1f4fd7feb34fd206357132b920f5b00801dee460ee415a15812ed9',
  772. '0x1866c8ed336c61231a1be54fd1d74cc4f9fb0ce4c6af5920abc5750c4bf39b4852cfe2f7bb9248836b233d9d55535d4a',
  773. '0x167a55cda70a6e1cea820597d94a84903216f763e13d87bb5308592e7ea7d4fbc7385ea3d529b35e346ef48bb8913f55',
  774. '0x4d2f259eea405bd48f010a01ad2911d9c6dd039bb61a6290e591b36e636a5c871a5c29f4f83060400f8b49cba8f6aa8',
  775. '0xaccbb67481d033ff5852c1e48c50c477f94ff8aefce42d28c0f9a88cea7913516f968986f7ebbea9684b529e2561092',
  776. '0xad6b9514c767fe3c3613144b45f1496543346d98adf02267d5ceef9a00d9b8693000763e3b90ac11e99b138573345cc',
  777. '0x2660400eb2e4f3b628bdd0d53cd76f2bf565b94e72927c1cb748df27942480e420517bd8714cc80d1fadc1326ed06f7',
  778. '0xe0fa1d816ddc03e6b24255e0d7819c171c40f65e273b853324efcd6356caa205ca2f570f13497804415473a1d634b8f',
  779. '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001', // LAST 1
  780. ],
  781. ].map((i) => i.map((j) => BigInt(j))));
  782. // SWU Map - Fp2 to G2': y² = x³ + 240i * x + 1012 + 1012i
  783. const G2_SWU = (0, weierstrass_js_1.mapToCurveSimpleSWU)(Fp2, {
  784. A: Fp2.create({ c0: Fp.create(_0n), c1: Fp.create(BigInt(240)) }), // A' = 240 * I
  785. B: Fp2.create({ c0: Fp.create(BigInt(1012)), c1: Fp.create(BigInt(1012)) }), // B' = 1012 * (1 + I)
  786. Z: Fp2.create({ c0: Fp.create(BigInt(-2)), c1: Fp.create(BigInt(-1)) }), // Z: -(2 + I)
  787. });
  788. // Optimized SWU Map - Fp to G1
  789. const G1_SWU = (0, weierstrass_js_1.mapToCurveSimpleSWU)(Fp, {
  790. A: Fp.create(BigInt('0x144698a3b8e9433d693a02c96d4982b0ea985383ee66a8d8e8981aefd881ac98936f8da0e0f97f5cf428082d584c1d')),
  791. B: Fp.create(BigInt('0x12e2908d11688030018b12e8753eee3b2016c1f0f24f4070a0b9c14fcef35ef55a23215a316ceaa5d1cc48e98e172be0')),
  792. Z: Fp.create(BigInt(11)),
  793. });
  794. // Endomorphisms (for fast cofactor clearing)
  795. // Ψ(P) endomorphism
  796. const ut_root = Fp6.create({ c0: Fp2.ZERO, c1: Fp2.ONE, c2: Fp2.ZERO });
  797. const wsq = Fp12.create({ c0: ut_root, c1: Fp6.ZERO });
  798. const wcu = Fp12.create({ c0: Fp6.ZERO, c1: ut_root });
  799. const [wsq_inv, wcu_inv] = Fp12.invertBatch([wsq, wcu]);
  800. function psi(x, y) {
  801. // Untwist Fp2->Fp12 && frobenius(1) && twist back
  802. const x2 = Fp12.mul(Fp12.frobeniusMap(Fp12.multiplyByFp2(wsq_inv, x), 1), wsq).c0.c0;
  803. const y2 = Fp12.mul(Fp12.frobeniusMap(Fp12.multiplyByFp2(wcu_inv, y), 1), wcu).c0.c0;
  804. return [x2, y2];
  805. }
  806. // Ψ endomorphism
  807. function G2psi(c, P) {
  808. const affine = P.toAffine();
  809. const p = psi(affine.x, affine.y);
  810. return new c(p[0], p[1], Fp2.ONE);
  811. }
  812. // Ψ²(P) endomorphism
  813. // 1 / F2(2)^((p-1)/3) in GF(p²)
  814. const PSI2_C1 = BigInt('0x1a0111ea397fe699ec02408663d4de85aa0d857d89759ad4897d29650fb85f9b409427eb4f49fffd8bfd00000000aaac');
  815. function psi2(x, y) {
  816. return [Fp2.mul(x, PSI2_C1), Fp2.neg(y)];
  817. }
  818. function G2psi2(c, P) {
  819. const affine = P.toAffine();
  820. const p = psi2(affine.x, affine.y);
  821. return new c(p[0], p[1], Fp2.ONE);
  822. }
  823. // Default hash_to_field options are for hash to G2.
  824. //
  825. // Parameter definitions are in section 5.3 of the spec unless otherwise noted.
  826. // Parameter values come from section 8.8.2 of the spec.
  827. // https://www.rfc-editor.org/rfc/rfc9380#section-8.8.2
  828. //
  829. // Base field F is GF(p^m)
  830. // p = 0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab
  831. // m = 2 (or 1 for G1 see section 8.8.1)
  832. // k = 128
  833. const htfDefaults = Object.freeze({
  834. // DST: a domain separation tag
  835. // defined in section 2.2.5
  836. // Use utils.getDSTLabel(), utils.setDSTLabel(value)
  837. DST: 'BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_',
  838. encodeDST: 'BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_',
  839. // p: the characteristic of F
  840. // where F is a finite field of characteristic p and order q = p^m
  841. p: Fp.ORDER,
  842. // m: the extension degree of F, m >= 1
  843. // where F is a finite field of characteristic p and order q = p^m
  844. m: 2,
  845. // k: the target security level for the suite in bits
  846. // defined in section 5.1
  847. k: 128,
  848. // option to use a message that has already been processed by
  849. // expand_message_xmd
  850. expand: 'xmd',
  851. // Hash functions for: expand_message_xmd is appropriate for use with a
  852. // wide range of hash functions, including SHA-2, SHA-3, BLAKE2, and others.
  853. // BBS+ uses blake2: https://github.com/hyperledger/aries-framework-go/issues/2247
  854. hash: sha256_1.sha256,
  855. });
  856. // Encoding utils
  857. // Point on G1 curve: (x, y)
  858. // Compressed point of infinity
  859. const COMPRESSED_ZERO = setMask(Fp.toBytes(_0n), { infinity: true, compressed: true }); // set compressed & point-at-infinity bits
  860. function parseMask(bytes) {
  861. // Copy, so we can remove mask data. It will be removed also later, when Fp.create will call modulo.
  862. bytes = bytes.slice();
  863. const mask = bytes[0] & 224;
  864. const compressed = !!((mask >> 7) & 1); // compression bit (0b1000_0000)
  865. const infinity = !!((mask >> 6) & 1); // point at infinity bit (0b0100_0000)
  866. const sort = !!((mask >> 5) & 1); // sort bit (0b0010_0000)
  867. bytes[0] &= 31; // clear mask (zero first 3 bits)
  868. return { compressed, infinity, sort, value: bytes };
  869. }
  870. function setMask(bytes, mask) {
  871. if (bytes[0] & 224)
  872. throw new Error('setMask: non-empty mask');
  873. if (mask.compressed)
  874. bytes[0] |= 128;
  875. if (mask.infinity)
  876. bytes[0] |= 64;
  877. if (mask.sort)
  878. bytes[0] |= 32;
  879. return bytes;
  880. }
  881. function signatureG1ToRawBytes(point) {
  882. point.assertValidity();
  883. const isZero = point.equals(exports.bls12_381.G1.ProjectivePoint.ZERO);
  884. const { x, y } = point.toAffine();
  885. if (isZero)
  886. return COMPRESSED_ZERO.slice();
  887. const P = Fp.ORDER;
  888. const sort = Boolean((y * _2n) / P);
  889. return setMask((0, utils_js_1.numberToBytesBE)(x, Fp.BYTES), { compressed: true, sort });
  890. }
  891. function signatureG2ToRawBytes(point) {
  892. // NOTE: by some reasons it was missed in bls12-381, looks like bug
  893. point.assertValidity();
  894. const len = Fp.BYTES;
  895. if (point.equals(exports.bls12_381.G2.ProjectivePoint.ZERO))
  896. return (0, utils_js_1.concatBytes)(COMPRESSED_ZERO, (0, utils_js_1.numberToBytesBE)(_0n, len));
  897. const { x, y } = point.toAffine();
  898. const { re: x0, im: x1 } = Fp2.reim(x);
  899. const { re: y0, im: y1 } = Fp2.reim(y);
  900. const tmp = y1 > _0n ? y1 * _2n : y0 * _2n;
  901. const sort = Boolean((tmp / Fp.ORDER) & _1n);
  902. const z2 = x0;
  903. return (0, utils_js_1.concatBytes)(setMask((0, utils_js_1.numberToBytesBE)(x1, len), { sort, compressed: true }), (0, utils_js_1.numberToBytesBE)(z2, len));
  904. }
  905. // To verify curve parameters, see pairing-friendly-curves spec:
  906. // https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-pairing-friendly-curves-11
  907. // Basic math is done over finite fields over p.
  908. // More complicated math is done over polynominal extension fields.
  909. // To simplify calculations in Fp12, we construct extension tower:
  910. // Fp₁₂ = Fp₆² => Fp₂³
  911. // Fp(u) / (u² - β) where β = -1
  912. // Fp₂(v) / (v³ - ξ) where ξ = u + 1
  913. // Fp₆(w) / (w² - γ) where γ = v
  914. // Here goes constants && point encoding format
  915. exports.bls12_381 = (0, bls_js_1.bls)({
  916. // Fields
  917. fields: {
  918. Fp,
  919. Fp2,
  920. Fp6,
  921. Fp12,
  922. Fr,
  923. },
  924. // G1 is the order-q subgroup of E1(Fp) : y² = x³ + 4, #E1(Fp) = h1q, where
  925. // characteristic; z + (z⁴ - z² + 1)(z - 1)²/3
  926. G1: {
  927. Fp,
  928. // cofactor; (z - 1)²/3
  929. h: BigInt('0x396c8c005555e1568c00aaab0000aaab'),
  930. // generator's coordinates
  931. // x = 3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507
  932. // y = 1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569
  933. Gx: BigInt('0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb'),
  934. Gy: BigInt('0x08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1'),
  935. a: Fp.ZERO,
  936. b: _4n,
  937. htfDefaults: { ...htfDefaults, m: 1, DST: 'BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_NUL_' },
  938. wrapPrivateKey: true,
  939. allowInfinityPoint: true,
  940. // Checks is the point resides in prime-order subgroup.
  941. // point.isTorsionFree() should return true for valid points
  942. // It returns false for shitty points.
  943. // https://eprint.iacr.org/2021/1130.pdf
  944. isTorsionFree: (c, point) => {
  945. // φ endomorphism
  946. const cubicRootOfUnityModP = BigInt('0x5f19672fdf76ce51ba69c6076a0f77eaddb3a93be6f89688de17d813620a00022e01fffffffefffe');
  947. const phi = new c(Fp.mul(point.px, cubicRootOfUnityModP), point.py, point.pz);
  948. // todo: unroll
  949. const xP = point.multiplyUnsafe(exports.bls12_381.params.x).negate(); // [x]P
  950. const u2P = xP.multiplyUnsafe(exports.bls12_381.params.x); // [u2]P
  951. return u2P.equals(phi);
  952. // https://eprint.iacr.org/2019/814.pdf
  953. // (z² − 1)/3
  954. // const c1 = BigInt('0x396c8c005555e1560000000055555555');
  955. // const P = this;
  956. // const S = P.sigma();
  957. // const Q = S.double();
  958. // const S2 = S.sigma();
  959. // // [(z² − 1)/3](2σ(P) − P − σ²(P)) − σ²(P) = O
  960. // const left = Q.subtract(P).subtract(S2).multiplyUnsafe(c1);
  961. // const C = left.subtract(S2);
  962. // return C.isZero();
  963. },
  964. // Clear cofactor of G1
  965. // https://eprint.iacr.org/2019/403
  966. clearCofactor: (_c, point) => {
  967. // return this.multiplyUnsafe(CURVE.h);
  968. return point.multiplyUnsafe(exports.bls12_381.params.x).add(point); // x*P + P
  969. },
  970. mapToCurve: (scalars) => {
  971. const { x, y } = G1_SWU(Fp.create(scalars[0]));
  972. return isogenyMapG1(x, y);
  973. },
  974. fromBytes: (bytes) => {
  975. const { compressed, infinity, sort, value } = parseMask(bytes);
  976. if (value.length === 48 && compressed) {
  977. // TODO: Fp.bytes
  978. const P = Fp.ORDER;
  979. const compressedValue = (0, utils_js_1.bytesToNumberBE)(value);
  980. // Zero
  981. const x = Fp.create(compressedValue & Fp.MASK);
  982. if (infinity) {
  983. if (x !== _0n)
  984. throw new Error('G1: non-empty compressed point at infinity');
  985. return { x: _0n, y: _0n };
  986. }
  987. const right = Fp.add(Fp.pow(x, _3n), Fp.create(exports.bls12_381.params.G1b)); // y² = x³ + b
  988. let y = Fp.sqrt(right);
  989. if (!y)
  990. throw new Error('Invalid compressed G1 point');
  991. if ((y * _2n) / P !== BigInt(sort))
  992. y = Fp.neg(y);
  993. return { x: Fp.create(x), y: Fp.create(y) };
  994. }
  995. else if (value.length === 96 && !compressed) {
  996. // Check if the infinity flag is set
  997. const x = (0, utils_js_1.bytesToNumberBE)(value.subarray(0, Fp.BYTES));
  998. const y = (0, utils_js_1.bytesToNumberBE)(value.subarray(Fp.BYTES));
  999. if (infinity) {
  1000. if (x !== _0n || y !== _0n)
  1001. throw new Error('G1: non-empty point at infinity');
  1002. return exports.bls12_381.G1.ProjectivePoint.ZERO.toAffine();
  1003. }
  1004. return { x: Fp.create(x), y: Fp.create(y) };
  1005. }
  1006. else {
  1007. throw new Error('Invalid point G1, expected 48/96 bytes');
  1008. }
  1009. },
  1010. toBytes: (c, point, isCompressed) => {
  1011. const isZero = point.equals(c.ZERO);
  1012. const { x, y } = point.toAffine();
  1013. if (isCompressed) {
  1014. if (isZero)
  1015. return COMPRESSED_ZERO.slice();
  1016. const P = Fp.ORDER;
  1017. const sort = Boolean((y * _2n) / P);
  1018. return setMask((0, utils_js_1.numberToBytesBE)(x, Fp.BYTES), { compressed: true, sort });
  1019. }
  1020. else {
  1021. if (isZero) {
  1022. // 2x PUBLIC_KEY_LENGTH
  1023. const x = (0, utils_js_1.concatBytes)(new Uint8Array([0x40]), new Uint8Array(2 * Fp.BYTES - 1));
  1024. return x;
  1025. }
  1026. else {
  1027. return (0, utils_js_1.concatBytes)((0, utils_js_1.numberToBytesBE)(x, Fp.BYTES), (0, utils_js_1.numberToBytesBE)(y, Fp.BYTES));
  1028. }
  1029. }
  1030. },
  1031. ShortSignature: {
  1032. fromHex(hex) {
  1033. const { infinity, sort, value } = parseMask((0, utils_js_1.ensureBytes)('signatureHex', hex, 48));
  1034. const P = Fp.ORDER;
  1035. const compressedValue = (0, utils_js_1.bytesToNumberBE)(value);
  1036. // Zero
  1037. if (infinity)
  1038. return exports.bls12_381.G1.ProjectivePoint.ZERO;
  1039. const x = Fp.create(compressedValue & Fp.MASK);
  1040. const right = Fp.add(Fp.pow(x, _3n), Fp.create(exports.bls12_381.params.G1b)); // y² = x³ + b
  1041. let y = Fp.sqrt(right);
  1042. if (!y)
  1043. throw new Error('Invalid compressed G1 point');
  1044. const aflag = BigInt(sort);
  1045. if ((y * _2n) / P !== aflag)
  1046. y = Fp.neg(y);
  1047. const point = exports.bls12_381.G1.ProjectivePoint.fromAffine({ x, y });
  1048. point.assertValidity();
  1049. return point;
  1050. },
  1051. toRawBytes(point) {
  1052. return signatureG1ToRawBytes(point);
  1053. },
  1054. toHex(point) {
  1055. return (0, utils_js_1.bytesToHex)(signatureG1ToRawBytes(point));
  1056. },
  1057. },
  1058. },
  1059. // G2 is the order-q subgroup of E2(Fp²) : y² = x³+4(1+√−1),
  1060. // where Fp2 is Fp[√−1]/(x2+1). #E2(Fp2 ) = h2q, where
  1061. // G² - 1
  1062. // h2q
  1063. G2: {
  1064. Fp: Fp2,
  1065. // cofactor
  1066. h: BigInt('0x5d543a95414e7f1091d50792876a202cd91de4547085abaa68a205b2e5a7ddfa628f1cb4d9e82ef21537e293a6691ae1616ec6e786f0c70cf1c38e31c7238e5'),
  1067. Gx: Fp2.fromBigTuple([
  1068. BigInt('0x024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8'),
  1069. BigInt('0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e'),
  1070. ]),
  1071. // y =
  1072. // 927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582,
  1073. // 1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905
  1074. Gy: Fp2.fromBigTuple([
  1075. BigInt('0x0ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801'),
  1076. BigInt('0x0606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be'),
  1077. ]),
  1078. a: Fp2.ZERO,
  1079. b: Fp2.fromBigTuple([_4n, _4n]),
  1080. hEff: BigInt('0xbc69f08f2ee75b3584c6a0ea91b352888e2a8e9145ad7689986ff031508ffe1329c2f178731db956d82bf015d1212b02ec0ec69d7477c1ae954cbc06689f6a359894c0adebbf6b4e8020005aaa95551'),
  1081. htfDefaults: { ...htfDefaults },
  1082. wrapPrivateKey: true,
  1083. allowInfinityPoint: true,
  1084. mapToCurve: (scalars) => {
  1085. const { x, y } = G2_SWU(Fp2.fromBigTuple(scalars));
  1086. return isogenyMapG2(x, y);
  1087. },
  1088. // Checks is the point resides in prime-order subgroup.
  1089. // point.isTorsionFree() should return true for valid points
  1090. // It returns false for shitty points.
  1091. // https://eprint.iacr.org/2021/1130.pdf
  1092. isTorsionFree: (c, P) => {
  1093. return P.multiplyUnsafe(exports.bls12_381.params.x).negate().equals(G2psi(c, P)); // ψ(P) == [u](P)
  1094. // Older version: https://eprint.iacr.org/2019/814.pdf
  1095. // Ψ²(P) => Ψ³(P) => [z]Ψ³(P) where z = -x => [z]Ψ³(P) - Ψ²(P) + P == O
  1096. // return P.psi2().psi().mulNegX().subtract(psi2).add(P).isZero();
  1097. },
  1098. // Maps the point into the prime-order subgroup G2.
  1099. // clear_cofactor_bls12381_g2 from cfrg-hash-to-curve-11
  1100. // https://eprint.iacr.org/2017/419.pdf
  1101. // prettier-ignore
  1102. clearCofactor: (c, P) => {
  1103. const x = exports.bls12_381.params.x;
  1104. let t1 = P.multiplyUnsafe(x).negate(); // [-x]P
  1105. let t2 = G2psi(c, P); // Ψ(P)
  1106. let t3 = P.double(); // 2P
  1107. t3 = G2psi2(c, t3); // Ψ²(2P)
  1108. t3 = t3.subtract(t2); // Ψ²(2P) - Ψ(P)
  1109. t2 = t1.add(t2); // [-x]P + Ψ(P)
  1110. t2 = t2.multiplyUnsafe(x).negate(); // [x²]P - [x]Ψ(P)
  1111. t3 = t3.add(t2); // Ψ²(2P) - Ψ(P) + [x²]P - [x]Ψ(P)
  1112. t3 = t3.subtract(t1); // Ψ²(2P) - Ψ(P) + [x²]P - [x]Ψ(P) + [x]P
  1113. const Q = t3.subtract(P); // Ψ²(2P) - Ψ(P) + [x²]P - [x]Ψ(P) + [x]P - 1P
  1114. return Q; // [x²-x-1]P + [x-1]Ψ(P) + Ψ²(2P)
  1115. },
  1116. fromBytes: (bytes) => {
  1117. const { compressed, infinity, sort, value } = parseMask(bytes);
  1118. if ((!compressed && !infinity && sort) || // 00100000
  1119. (!compressed && infinity && sort) || // 01100000
  1120. (sort && infinity && compressed) // 11100000
  1121. ) {
  1122. throw new Error('Invalid encoding flag: ' + (bytes[0] & 224));
  1123. }
  1124. const L = Fp.BYTES;
  1125. const slc = (b, from, to) => (0, utils_js_1.bytesToNumberBE)(b.slice(from, to));
  1126. if (value.length === 96 && compressed) {
  1127. const b = exports.bls12_381.params.G2b;
  1128. const P = Fp.ORDER;
  1129. if (infinity) {
  1130. // check that all bytes are 0
  1131. if (value.reduce((p, c) => (p !== 0 ? c + 1 : c), 0) > 0) {
  1132. throw new Error('Invalid compressed G2 point');
  1133. }
  1134. return { x: Fp2.ZERO, y: Fp2.ZERO };
  1135. }
  1136. const x_1 = slc(value, 0, L);
  1137. const x_0 = slc(value, L, 2 * L);
  1138. const x = Fp2.create({ c0: Fp.create(x_0), c1: Fp.create(x_1) });
  1139. const right = Fp2.add(Fp2.pow(x, _3n), b); // y² = x³ + 4 * (u+1) = x³ + b
  1140. let y = Fp2.sqrt(right);
  1141. const Y_bit = y.c1 === _0n ? (y.c0 * _2n) / P : (y.c1 * _2n) / P ? _1n : _0n;
  1142. y = sort && Y_bit > 0 ? y : Fp2.neg(y);
  1143. return { x, y };
  1144. }
  1145. else if (value.length === 192 && !compressed) {
  1146. if (infinity) {
  1147. if (value.reduce((p, c) => (p !== 0 ? c + 1 : c), 0) > 0) {
  1148. throw new Error('Invalid uncompressed G2 point');
  1149. }
  1150. return { x: Fp2.ZERO, y: Fp2.ZERO };
  1151. }
  1152. const x1 = slc(value, 0, L);
  1153. const x0 = slc(value, L, 2 * L);
  1154. const y1 = slc(value, 2 * L, 3 * L);
  1155. const y0 = slc(value, 3 * L, 4 * L);
  1156. return { x: Fp2.fromBigTuple([x0, x1]), y: Fp2.fromBigTuple([y0, y1]) };
  1157. }
  1158. else {
  1159. throw new Error('Invalid point G2, expected 96/192 bytes');
  1160. }
  1161. },
  1162. toBytes: (c, point, isCompressed) => {
  1163. const { BYTES: len, ORDER: P } = Fp;
  1164. const isZero = point.equals(c.ZERO);
  1165. const { x, y } = point.toAffine();
  1166. if (isCompressed) {
  1167. if (isZero)
  1168. return (0, utils_js_1.concatBytes)(COMPRESSED_ZERO, (0, utils_js_1.numberToBytesBE)(_0n, len));
  1169. const flag = Boolean(y.c1 === _0n ? (y.c0 * _2n) / P : (y.c1 * _2n) / P);
  1170. return (0, utils_js_1.concatBytes)(setMask((0, utils_js_1.numberToBytesBE)(x.c1, len), { compressed: true, sort: flag }), (0, utils_js_1.numberToBytesBE)(x.c0, len));
  1171. }
  1172. else {
  1173. if (isZero)
  1174. return (0, utils_js_1.concatBytes)(new Uint8Array([0x40]), new Uint8Array(4 * len - 1)); // bytes[0] |= 1 << 6;
  1175. const { re: x0, im: x1 } = Fp2.reim(x);
  1176. const { re: y0, im: y1 } = Fp2.reim(y);
  1177. return (0, utils_js_1.concatBytes)((0, utils_js_1.numberToBytesBE)(x1, len), (0, utils_js_1.numberToBytesBE)(x0, len), (0, utils_js_1.numberToBytesBE)(y1, len), (0, utils_js_1.numberToBytesBE)(y0, len));
  1178. }
  1179. },
  1180. Signature: {
  1181. // TODO: Optimize, it's very slow because of sqrt.
  1182. fromHex(hex) {
  1183. const { infinity, sort, value } = parseMask((0, utils_js_1.ensureBytes)('signatureHex', hex));
  1184. const P = Fp.ORDER;
  1185. const half = value.length / 2;
  1186. if (half !== 48 && half !== 96)
  1187. throw new Error('Invalid compressed signature length, must be 96 or 192');
  1188. const z1 = (0, utils_js_1.bytesToNumberBE)(value.slice(0, half));
  1189. const z2 = (0, utils_js_1.bytesToNumberBE)(value.slice(half));
  1190. // Indicates the infinity point
  1191. if (infinity)
  1192. return exports.bls12_381.G2.ProjectivePoint.ZERO;
  1193. const x1 = Fp.create(z1 & Fp.MASK);
  1194. const x2 = Fp.create(z2);
  1195. const x = Fp2.create({ c0: x2, c1: x1 });
  1196. const y2 = Fp2.add(Fp2.pow(x, _3n), exports.bls12_381.params.G2b); // y² = x³ + 4
  1197. // The slow part
  1198. let y = Fp2.sqrt(y2);
  1199. if (!y)
  1200. throw new Error('Failed to find a square root');
  1201. // Choose the y whose leftmost bit of the imaginary part is equal to the a_flag1
  1202. // If y1 happens to be zero, then use the bit of y0
  1203. const { re: y0, im: y1 } = Fp2.reim(y);
  1204. const aflag1 = BigInt(sort);
  1205. const isGreater = y1 > _0n && (y1 * _2n) / P !== aflag1;
  1206. const isZero = y1 === _0n && (y0 * _2n) / P !== aflag1;
  1207. if (isGreater || isZero)
  1208. y = Fp2.neg(y);
  1209. const point = exports.bls12_381.G2.ProjectivePoint.fromAffine({ x, y });
  1210. point.assertValidity();
  1211. return point;
  1212. },
  1213. toRawBytes(point) {
  1214. return signatureG2ToRawBytes(point);
  1215. },
  1216. toHex(point) {
  1217. return (0, utils_js_1.bytesToHex)(signatureG2ToRawBytes(point));
  1218. },
  1219. },
  1220. },
  1221. params: {
  1222. x: BLS_X, // The BLS parameter x for BLS12-381
  1223. r: Fr.ORDER, // order; z⁴ − z² + 1; CURVE.n from other curves
  1224. },
  1225. htfDefaults,
  1226. hash: sha256_1.sha256,
  1227. randomBytes: utils_1.randomBytes,
  1228. });
  1229. //# sourceMappingURL=bls12-381.js.map