export-ton-verifier
Version:
Tool for generating Groth16 and PLONK verifier code for the TON blockchain from SnarkJS .zkey or verification key JSON files.
55 lines (53 loc) • 1.64 kB
JavaScript
// src/utils.js
import { buildBn128, buildBls12381 } from "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 buildBn128(singleThread);
} else if (["BLS12381"].indexOf(normName) >= 0) {
curve = await 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 assertBls12381Curve(curve) {
const normalizedName = String((curve == null ? void 0 : curve.name) ?? "").toLowerCase().replace(/[^a-z0-9]/g, "");
if (normalizedName !== "bls12381") {
throw new Error("Compression helpers support only BLS12-381 curves.");
}
}
function g1Compressed(curve, p1Raw) {
assertBls12381Curve(curve);
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) {
assertBls12381Curve(curve);
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);
}
export {
getCurveFromName,
g1Compressed,
g2Compressed
};