UNPKG

export-ton-verifier

Version:

Tool for generating Groth16 and PLONK verifier code for the TON blockchain from SnarkJS .zkey or verification key JSON files.

93 lines (90 loc) 2.88 kB
import { createLogger, loadPlonkTemplateVerificationKey, loadVerificationKey } from "./chunk-LXOLCHR7.js"; import { defaultTemplatesDir } from "./chunk-6TDHKTNH.js"; import { normalizeContractName, resolveTemplatePath } from "./chunk-TB4DFXWQ.js"; import { g1Compressed, g2Compressed, getCurveFromName } from "./chunk-I7S4EEHA.js"; import { assertTonBlsCurve } from "./chunk-4I2ZWZKN.js"; // src/generateVerifiers.js import fs from "fs/promises"; import ejs from "ejs"; import { utils } from "ffjavascript"; var { unstringifyBigInts } = utils; async function generateVerifier(inputPath, outputPath, { lang = "tolk", templatesDir = defaultTemplatesDir(), contractName = null, logger = null, quiet = false } = {}) { const log = createLogger({ logger, quiet }); const verifierInput = await loadVerificationKey(inputPath, { logger: log }); const vkRaw = verifierInput.verificationKey; assertTonBlsCurve(vkRaw.curve, "verification_key.curve"); const normalizedContractName = typeof contractName === "string" && contractName.trim() ? normalizeContractName(contractName) : null; const templatePath = await resolveTemplatePath( templatesDir, lang, vkRaw.protocol ); log.log(`Using template for: ${lang} ${vkRaw.protocol}`); let curve; try { if (vkRaw.protocol === "groth16") { const vk = unstringifyBigInts(vkRaw); curve = await getCurveFromName(vkRaw.curve); log.log("Compressing points..."); const data = { protocol: "groth16", curve: vkRaw.curve, contractName: normalizedContractName, vk_alpha_1: g1Compressed(curve, vk.vk_alpha_1), vk_beta_2: g2Compressed(curve, vk.vk_beta_2), vk_gamma_2: g2Compressed(curve, vk.vk_gamma_2), vk_delta_2: g2Compressed(curve, vk.vk_delta_2), IC: vk.IC.map((x) => g1Compressed(curve, x)), nPublic: vk.IC.length - 1, publicInputKeyLen: 32, _raw: vkRaw }; log.log("Rendering template..."); const template2 = await fs.readFile(templatePath, "utf8"); const rendered2 = ejs.render(template2, data); log.log(`Saving file: ${outputPath}`); await fs.writeFile(outputPath, rendered2, "utf8"); log.log("Done."); return "groth16"; } log.log("Rendering template for PLONK..."); const template = await fs.readFile(templatePath, "utf8"); const verificationKey = { ...await loadPlonkTemplateVerificationKey(verifierInput), contractName: normalizedContractName }; const rendered = ejs.render(template, verificationKey); log.log(`Saving file: ${outputPath}`); await fs.writeFile(outputPath, rendered, "utf8"); log.log("Done."); return "plonk"; } finally { if (curve && typeof curve.terminate === "function") { await curve.terminate(); } } } export { generateVerifier };