UNPKG

export-ton-verifier

Version:

Tool for generating a Groth16 verifier code for the TON blockchain from SnarkJS .zkey files.

113 lines (107 loc) 3.63 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { dictFromInputList: () => dictFromInputList, g1Compressed: () => g1Compressed, g2Compressed: () => g2Compressed, getCurveFromName: () => getCurveFromName, groth16CompressProof: () => groth16CompressProof }); module.exports = __toCommonJS(index_exports); // src/groth16CompressProof.js var import_ffjavascript2 = require("ffjavascript"); // src/utils.js var import_ffjavascript = require("ffjavascript"); async function getCurveFromName(name, options) { let curve; let singleThread = options && options.singleThread; const normName = normalizeName(name); if (["BN128", "BN254", "ALTBN128"].indexOf(normName) >= 0) { curve = await (0, import_ffjavascript.buildBn128)(singleThread); } else if (["BLS12381"].indexOf(normName) >= 0) { curve = await (0, import_ffjavascript.buildBls12381)(singleThread); } else { throw new Error(`Curve not supported: ${name}`); } return curve; function normalizeName(n) { return n.toUpperCase().match(/[A-Za-z0-9]+/g).join(""); } } function toHexString(byteArray) { return Array.from( byteArray, (byte) => ("0" + (byte & 255).toString(16)).slice(-2) ).join(""); } function g1Compressed(curve, p1Raw) { const p1 = curve.G1.fromObject(p1Raw); const buff = new Uint8Array(48); curve.G1.toRprCompressed(buff, 0, p1); if (buff[0] & 128) buff[0] |= 32; buff[0] |= 128; return toHexString(buff); } function g2Compressed(curve, p2Raw) { const p2 = curve.G2.fromObject(p2Raw); const buff = new Uint8Array(96); curve.G2.toRprCompressed(buff, 0, p2); if (buff[0] & 128) buff[0] |= 32; buff[0] |= 128; return toHexString(buff); } // src/groth16CompressProof.js async function groth16CompressProof(proof, publicSignals) { const curve = await (0, import_ffjavascript2.buildBls12381)(); const proofProc = import_ffjavascript2.utils.unstringifyBigInts(proof); const pi_aS = g1Compressed(curve, proofProc.pi_a); const pi_bS = g2Compressed(curve, proofProc.pi_b); const pi_cS = g1Compressed(curve, proofProc.pi_c); const pi_a = Buffer.from(pi_aS, "hex"); const pi_b = Buffer.from(pi_bS, "hex"); const pi_c = Buffer.from(pi_cS, "hex"); const pubInputs = publicSignals.map((s) => BigInt(s)); return { pi_a, pi_b, pi_c, pubInputs }; } // src/dictFromInputList.ts var import_core = require("@ton/core"); function dictFromInputList(list) { const dict = import_core.Dictionary.empty( import_core.Dictionary.Keys.Int(32), import_core.Dictionary.Values.BigInt(256) ); for (let i = 0; i < list.length; i++) { dict.set(i, list[i]); } return dict; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { dictFromInputList, g1Compressed, g2Compressed, getCurveFromName, groth16CompressProof });