Skip to content

Commit

Permalink
include calldata in generator plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Aug 15, 2023
1 parent 0546df7 commit 25a2e90
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 225 deletions.
37 changes: 17 additions & 20 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,17 +242,18 @@ const commands = [
action: zkeyExportJson
},
{
cmd: "zkey export solidityverifier [circuit_final.zkey] [verifier.sol]",
description: "Creates a verifier in solidity",
cmd: "zkey export verifier [circuit_final.zkey] [verifier.sol]",
description: "Creates a verifier",
alias: ["zkesv", "generateverifier -vk|verificationkey -v|verifier"],
options: "-plugin",
action: zkeyExportSolidityVerifier
action: zkeyExportVerifier
},
{
cmd: "zkey export soliditycalldata [public.json] [proof.json]",
cmd: "zkey export calldata [public.json] [proof.json]",
description: "Generates call parameters ready to be called.",
alias: ["zkesc", "generatecall -pub|public -p|proof"],
action: zkeyExportSolidityCalldata
options: "-plugin",
action: zkeyExportCalldata
},
{
cmd: "groth16 setup [circuit.r1cs] [powersoftau.ptau] [circuit_0000.zkey]",
Expand Down Expand Up @@ -608,8 +609,8 @@ async function zkeyExportJson(params, options) {
await bfj.write(zkeyJsonName, zKeyJson, {space: 1});
}

// solidity genverifier [circuit_final.zkey] [verifier.sol]
async function zkeyExportSolidityVerifier(params, options) {
// zkey export verifier [circuit_final.zkey] [verifier.sol]
async function zkeyExportVerifier(params, options) {
let zkeyName;
let verifierName;

Expand All @@ -627,7 +628,9 @@ async function zkeyExportSolidityVerifier(params, options) {

if (options.verbose) Logger.setLogLevel("DEBUG");

const plugin = options.plugin || "snarkjs-generate-solidity";
const pluginName = options.plugin || "snarkjs-generate-solidity";
const plugin = await import(pluginName);

const verifierCode = await zkey.exportVerifier(zkeyName, plugin, logger);

fs.writeFileSync(verifierName, verifierCode, "utf-8");
Expand All @@ -636,8 +639,8 @@ async function zkeyExportSolidityVerifier(params, options) {
}


// solidity gencall <public.json> <proof.json>
async function zkeyExportSolidityCalldata(params, options) {
// zkey export calldata <public.json> <proof.json>
async function zkeyExportCalldata(params, options) {
let publicName;
let proofName;

Expand All @@ -658,16 +661,10 @@ async function zkeyExportSolidityCalldata(params, options) {
const pub = JSON.parse(fs.readFileSync(publicName, "utf8"));
const proof = JSON.parse(fs.readFileSync(proofName, "utf8"));

let res;
if (proof.protocol == "groth16") {
res = await groth16.exportSolidityCallData(proof, pub);
} else if (proof.protocol == "plonk") {
res = await plonk.exportSolidityCallData(proof, pub);
} else if (proof.protocol === "fflonk") {
res = await fflonk.exportSolidityCallData(pub, proof);
} else {
throw new Error("Invalid Protocol");
}
const pluginName = options.plugin || "snarkjs-generate-solidity";
const plugin = await import(pluginName);

const res = await zkey.exportCalldata(proof, pub, plugin, logger);
console.log(res);

return 0;
Expand Down
2 changes: 1 addition & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export * as r1cs from "./src/r1cs.js";
export * as wtns from "./src/wtns.js";
export * as zKey from "./src/zkey.js";
export * as plonk from "./src/plonk.js";
export * as fflonk from "./src/fflonk.js";
export * as fflonk from "./src/fflonk.js";
95 changes: 91 additions & 4 deletions snarkjs-generate-solidity/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ async function getCurveFromName(name) {
}


async function groth16(verificationKey, _logger) {
async function groth16SolidityVerifier(verificationKey, logger) {
const template = await fs.promises.readFile(path.join(__dirname, "templates", "verifier_groth16.sol.ejs"), "utf8");
return ejs.render(template, verificationKey);
}

async function plonk(verificationKey, _logger) {
async function plonkSolidityVerifier(verificationKey, logger) {
const template = await fs.promises.readFile(path.join(__dirname, "templates", "verifier_plonk.sol.ejs"), "utf8");
return ejs.render(template, verificationKey);
}

async function fflonk(vk, logger) {
async function fflonkSolidityVerifier(vk, logger) {

if (logger) logger.info("FFLONK EXPORT SOLIDITY VERIFIER STARTED");

Expand Down Expand Up @@ -72,4 +72,91 @@ async function fflonk(vk, logger) {
}
}

export const verifiers = { groth16, plonk, fflonk };
export const verifiers = {
groth16: groth16SolidityVerifier,
plonk: plonkSolidityVerifier,
fflonk: fflonkSolidityVerifier,
};

function p256(n) {
let nstr = n.toString(16);
while (nstr.length < 64) nstr = "0"+nstr;
nstr = `"0x${nstr}"`;
return nstr;
}

async function groth16SolidityCallData(_proof, _pub, logger) {
const proof = unstringifyBigInts(_proof);
const pub = unstringifyBigInts(_pub);

let inputs = "";
for (let i=0; i<pub.length; i++) {
if (inputs != "") inputs = inputs + ",";
inputs = inputs + p256(pub[i]);
}

let S;
S=`[${p256(proof.pi_a[0])}, ${p256(proof.pi_a[1])}],` +
`[[${p256(proof.pi_b[0][1])}, ${p256(proof.pi_b[0][0])}],[${p256(proof.pi_b[1][1])}, ${p256(proof.pi_b[1][0])}]],` +
`[${p256(proof.pi_c[0])}, ${p256(proof.pi_c[1])}],` +
`[${inputs}]`;

return S;
}

async function plonkSolidityCallData(_proof, _pub, logger) {
const proof = unstringifyBigInts(_proof);
const pub = unstringifyBigInts(_pub);

let inputs = "";
for (let i=0; i<pub.length; i++) {
if (inputs != "") inputs = inputs + ",";
inputs = inputs + p256(pub[i]);
}

return `[${p256(proof.A[0])}, ${p256(proof.A[1])},` +
`${p256(proof.B[0])},${p256(proof.B[1])},` +
`${p256(proof.C[0])},${p256(proof.C[1])},` +
`${p256(proof.Z[0])},${p256(proof.Z[1])},` +
`${p256(proof.T1[0])},${p256(proof.T1[1])},` +
`${p256(proof.T2[0])},${p256(proof.T2[1])},` +
`${p256(proof.T3[0])},${p256(proof.T3[1])},` +
`${p256(proof.Wxi[0])},${p256(proof.Wxi[1])},` +
`${p256(proof.Wxiw[0])},${p256(proof.Wxiw[1])},` +
`${p256(proof.eval_a)},` +
`${p256(proof.eval_b)},` +
`${p256(proof.eval_c)},` +
`${p256(proof.eval_s1)},` +
`${p256(proof.eval_s2)},` +
`${p256(proof.eval_zw)}]` +
`[${inputs}]`;
}

async function fflonkSolidityCallData(_pub, _proof, logger) {
const proof = unstringifyBigInts(_proof);
const pub = unstringifyBigInts(_pub);

let inputs = "";
for (let i = 0; i < pub.length; i++) {
if (inputs !== "") inputs = inputs + ",";
inputs = inputs + p256(pub[i]);
}

return `[${p256(proof.polynomials.C1[0])}, ${p256(proof.polynomials.C1[1])},` +
`${p256(proof.polynomials.C2[0])},${p256(proof.polynomials.C2[1])},` +
`${p256(proof.polynomials.W1[0])},${p256(proof.polynomials.W1[1])},` +
`${p256(proof.polynomials.W2[0])},${p256(proof.polynomials.W2[1])},` +
`${p256(proof.evaluations.ql)},${p256(proof.evaluations.qr)},${p256(proof.evaluations.qm)},` +
`${p256(proof.evaluations.qo)},${p256(proof.evaluations.qc)},${p256(proof.evaluations.s1)},` +
`${p256(proof.evaluations.s2)},${p256(proof.evaluations.s3)},${p256(proof.evaluations.a)},` +
`${p256(proof.evaluations.b)},${p256(proof.evaluations.c)},${p256(proof.evaluations.z)},` +
`${p256(proof.evaluations.zw)},${p256(proof.evaluations.t1w)},${p256(proof.evaluations.t2w)},` +
`${p256(proof.evaluations.inv)}],` +
`[${inputs}]`;
}

export const calldata = {
groth16: groth16SolidityCallData,
plonk: plonkSolidityCallData,
fflonk: fflonkSolidityCallData,
};
1 change: 0 additions & 1 deletion src/fflonk.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ export { default as setup } from "./fflonk_setup.js";
export { default as prove } from "./fflonk_prove.js";
export { default as fullProve } from "./fflonk_full_prove.js";
export { default as verify } from "./fflonk_verify.js";
export { default as exportSolidityCallData } from "./fflonk_export_calldata.js";
61 changes: 0 additions & 61 deletions src/fflonk_export_calldata.js

This file was deleted.

1 change: 0 additions & 1 deletion src/groth16.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@
export {default as fullProve} from "./groth16_fullprove.js";
export {default as prove} from "./groth16_prove.js";
export {default as verify} from "./groth16_verify.js";
export {default as exportSolidityCallData} from "./groth16_exportsoliditycalldata.js";
46 changes: 0 additions & 46 deletions src/groth16_exportsoliditycalldata.js

This file was deleted.

1 change: 0 additions & 1 deletion src/plonk.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,3 @@ export {default as setup} from "./plonk_setup.js";
export {default as fullProve} from "./plonk_fullprove.js";
export {default as prove} from "./plonk_prove.js";
export {default as verify} from "./plonk_verify.js";
export {default as exportSolidityCallData} from "./plonk_exportsoliditycalldata.js";
65 changes: 0 additions & 65 deletions src/plonk_exportsoliditycalldata.js

This file was deleted.

Loading

0 comments on commit 25a2e90

Please sign in to comment.