export-ton-verifier
Version:
Tool for generating Groth16 and PLONK verifier code for the TON blockchain from SnarkJS .zkey or verification key JSON files.
94 lines (91 loc) • 3.3 kB
JavaScript
import {
getCurveFromQ
} from "./chunk-MCE4ZZVW.js";
// src/helpers.js
import fs from "fs/promises";
import path from "path";
import * as binFileUtils from "@iden3/binfileutils";
async function fileExists(p) {
try {
await fs.access(p);
return true;
} catch {
return false;
}
}
async function resolveTemplatePath(templatesDir, lang, protocol) {
const withProtocol = `${lang}_verifier_${protocol}.ejs`;
const fallback = `${lang}_verifier.ejs`;
const pathWithProtocol = path.join(templatesDir, withProtocol);
const pathFallback = path.join(templatesDir, fallback);
if (await fileExists(pathWithProtocol)) {
return pathWithProtocol;
}
if (protocol === "groth16" && await fileExists(pathFallback)) {
return pathFallback;
}
throw new Error(
`Template not found: '${withProtocol}' (or '${fallback}' for groth16) in ${templatesDir}`
);
}
function normalizeContractName(name = "MyVerifier") {
const raw = String(name ?? "").trim();
if (!raw) return "MyVerifier";
const parts = raw.match(/[A-Za-z0-9]+/g) ?? [];
if (parts.length === 0) return "MyVerifier";
let normalized = parts.map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("");
if (!/^[A-Za-z_]/.test(normalized)) {
normalized = `_${normalized}`;
}
return normalized;
}
function log2(V) {
return ((V & 4294901760) !== 0 ? (V &= 4294901760, 16) : 0) | ((V & 4278255360) !== 0 ? (V &= 4278255360, 8) : 0) | ((V & 4042322160) !== 0 ? (V &= 4042322160, 4) : 0) | ((V & 3435973836) !== 0 ? (V &= 3435973836, 2) : 0) | (V & 2863311530) !== 0;
}
async function readG1(fd, curve, toObject) {
const buff = await fd.read(curve.G1.F.n8 * 2);
const res = curve.G1.fromRprLEM(buff, 0);
return toObject ? curve.G1.toObject(res) : res;
}
async function readG2(fd, curve, toObject) {
const buff = await fd.read(curve.G2.F.n8 * 2);
const res = curve.G2.fromRprLEM(buff, 0);
return toObject ? curve.G2.toObject(res) : res;
}
async function readHeaderPlonk(fd, sections, toObject) {
const zkey = {};
zkey.protocol = "plonk";
await binFileUtils.startReadUniqueSection(fd, sections, 2);
const n8q = await fd.readULE32();
zkey.n8q = n8q;
zkey.q = await binFileUtils.readBigInt(fd, n8q);
const n8r = await fd.readULE32();
zkey.n8r = n8r;
zkey.r = await binFileUtils.readBigInt(fd, n8r);
zkey.curve = await getCurveFromQ(zkey.q);
zkey.nVars = await fd.readULE32();
zkey.nPublic = await fd.readULE32();
zkey.domainSize = await fd.readULE32();
zkey.power = log2(zkey.domainSize);
zkey.nAdditions = await fd.readULE32();
zkey.nConstraints = await fd.readULE32();
zkey.k1 = await fd.read(n8r);
zkey.k2 = await fd.read(n8r);
zkey.Qm = await readG1(fd, zkey.curve, toObject);
zkey.Ql = await readG1(fd, zkey.curve, toObject);
zkey.Qr = await readG1(fd, zkey.curve, toObject);
zkey.Qo = await readG1(fd, zkey.curve, toObject);
zkey.Qc = await readG1(fd, zkey.curve, toObject);
zkey.S1 = await readG1(fd, zkey.curve, toObject);
zkey.S2 = await readG1(fd, zkey.curve, toObject);
zkey.S3 = await readG1(fd, zkey.curve, toObject);
zkey.X_2 = await readG2(fd, zkey.curve, toObject);
await binFileUtils.endReadSection(fd);
return zkey;
}
export {
fileExists,
resolveTemplatePath,
normalizeContractName,
readHeaderPlonk
};