fix: Remove bfj & fs calls from core logic (#356)

This commit is contained in:
Blaine Bublitz
2023-05-07 00:02:26 +01:00
committed by GitHub
parent 40f51368df
commit 570cb07805
28 changed files with 11401 additions and 5415 deletions

View File

@@ -97,6 +97,7 @@ jobs:
{"a": 3, "b": 11}
EOT
snarkjs wtns calculate circuit.wasm input.json witness.wtns
snarkjs wtns check circuit.r1cs witness.wtns
- name: 23. Debug the final witness calculation
run: snarkjs wtns debug circuit.wasm input.json witness.wtns circuit.sym --trigger --get --set
- name: 24. Create the proof

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

41
cli.js
View File

@@ -38,8 +38,7 @@ const {stringifyBigInts} = utils;
import * as zkey from "./src/zkey.js";
import * as groth16 from "./src/groth16.js";
import * as plonk from "./src/plonk.js";
import * as fflonkCmd from "./src/cmds/fflonk_cmds.js";
import * as wtnsCmd from "./src/cmds/wtns_cmds.js";
import * as fflonk from "./src/fflonk.js";
import * as wtns from "./src/wtns.js";
import * as curves from "./src/curves.js";
import path from "path";
@@ -487,7 +486,13 @@ async function wtnsCheck(params, options) {
if (options.verbose) Logger.setLogLevel("DEBUG");
return await wtnsCmd.wtnsCheckCmd(r1csFilename, wtnsFilename, logger);
const isValid = await wtns.check(r1csFilename, wtnsFilename, logger);
if (isValid) {
return 0;
} else {
return 1;
}
}
@@ -675,7 +680,7 @@ async function zkeyExportSolidityCalldata(params, options) {
} else if (proof.protocol == "plonk") {
res = await plonk.exportSolidityCallData(proof, pub);
} else if (proof.protocol === "fflonk") {
res = await fflonkCmd.fflonkExportCallDataCmd(pub, proof, logger);
res = await fflonk.exportSolidityCallData(pub, proof);
} else {
throw new Error("Invalid Protocol");
}
@@ -1161,7 +1166,7 @@ async function fflonkSetup(params, options) {
if (options.verbose) Logger.setLogLevel("DEBUG");
return await fflonkCmd.fflonkSetupCmd(r1csFilename, ptauFilename, zkeyFilename, logger);
return await fflonk.setup(r1csFilename, ptauFilename, zkeyFilename, logger);
}
@@ -1173,7 +1178,15 @@ async function fflonkProve(params, options) {
if (options.verbose) Logger.setLogLevel("DEBUG");
return await fflonkCmd.fflonkProveCmd(zkeyFilename, witnessFilename, publicInputsFilename, proofFilename, logger);
const {proof, publicSignals} = await fflonk.prove(zkeyFilename, witnessFilename, logger);
if(undefined !== proofFilename && undefined !== publicInputsFilename) {
// Write the proof and the publig signals in each file
await bfj.write(proofFilename, stringifyBigInts(proof), {space: 1});
await bfj.write(publicInputsFilename, stringifyBigInts(publicSignals), {space: 1});
}
return 0;
}
async function fflonkFullProve(params, options) {
@@ -1186,7 +1199,15 @@ async function fflonkFullProve(params, options) {
if (options.verbose) Logger.setLogLevel("DEBUG");
return await fflonkCmd.fflonkFullProveCmd(zkeyFilename, witnessInputsFilename, wasmFilename, publicInputsFilename, proofFilename, logger);
const input = JSON.parse(await fs.promises.readFile(witnessInputsFilename, "utf8"));
const {proof, publicSignals} = await fflonk.fullProve(input, wasmFilename, zkeyFilename, logger);
// Write the proof and the publig signals in each file
await bfj.write(proofFilename, stringifyBigInts(proof), {space: 1});
await bfj.write(publicInputsFilename, stringifyBigInts(publicSignals), {space: 1});
return 0;
}
async function fflonkVerify(params, options) {
@@ -1196,7 +1217,11 @@ async function fflonkVerify(params, options) {
if (options.verbose) Logger.setLogLevel("DEBUG");
const isValid = await fflonkCmd.fflonkVerifyCmd(vkeyFilename, publicInputsFilename, proofFilename, logger);
const vkey = JSON.parse(fs.readFileSync(vkeyFilename, "utf8"));
const publicInputs = JSON.parse(fs.readFileSync(publicInputsFilename, "utf8"));
const proof = JSON.parse(fs.readFileSync(proofFilename, "utf8"));
const isValid = await fflonk.verify(vkey, publicInputs, proof, logger);
return isValid ? 0 : 1;
}

View File

@@ -1,10 +1,7 @@
export * as groth16 from "./src/groth16.js";
export * as powersOfTau from "./src/powersoftau.js";
export * as r1cs from "./src/r1cs.js";
export * as wtns from "./src/wtns.js";
export * as wtnsCmds from "./src/cmds/wtns_cmds.js";
export * as zKey from "./src/zkey.js";
export * as plonk from "./src/plonk.js";
export * as fflonk from "./src/cmds/fflonk_cmds.js";
export * as fflonk from "./src/fflonk.js";

View File

@@ -126,8 +126,8 @@ describe("Smart contracts test suite", function () {
const wtnsFilename = path.join("../test", "fflonk", "witness.wtns");
const zkeyFilename = { type: "mem" };
await snarkjs.fflonk.fflonkSetupCmd(r1csFilename, ptauFilename, zkeyFilename);
const { proof: proofJson, publicSignals: publicInputs } = await snarkjs.fflonk.fflonkProveCmd(zkeyFilename, wtnsFilename);
await snarkjs.fflonk.setup(r1csFilename, ptauFilename, zkeyFilename);
const { proof: proofJson, publicSignals: publicInputs } = await snarkjs.fflonk.prove(zkeyFilename, wtnsFilename);
// Generate fflonk verifier solidity file from fflonk template + zkey
const verifierCode = await snarkjs.zKey.exportSolidityVerifier(zkeyFilename, templates);

View File

@@ -1,25 +0,0 @@
/*
Copyright 2022 iden3 association.
This file is part of snarkJS.
snarkJS is a free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
snarkJS is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU General Public License
along with snarkJS. If not, see <https://www.gnu.org/licenses/>.
*/
export {fflonkSetupCmd} from "./fflonk_setup_cmd.js";
export {fflonkProveCmd} from "./fflonk_prove_cmd.js";
export {fflonkFullProveCmd} from "./fflonk_full_prove_cmd.js";
export {fflonkVerifyCmd} from "./fflonk_verify_cmd.js";
export {fflonkExportSolidityVerifierCmd} from "./fflonk_export_solidity_verifier_cmd.js";
export {fflonkExportCallDataCmd} from "./fflonk_export_calldata_cmd.js";

View File

@@ -1,22 +0,0 @@
/*
This file is part of snarkjs.
snarkjs is a free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your option)
any later version.
snarkjs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
snarkjs. If not, see <https://www.gnu.org/licenses/>.
*/
import fflonkExportCallData from "../fflonk_export_calldata.js";
export async function fflonkExportCallDataCmd(publicInputs, proof, logger) {
return await fflonkExportCallData(publicInputs, proof, logger);
}

View File

@@ -1,22 +0,0 @@
/*
This file is part of snarkjs.
snarkjs is a free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your option)
any later version.
snarkjs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
snarkjs. If not, see <https://www.gnu.org/licenses/>.
*/
import fflonkExportSolidityVerifier from "../fflonk_export_solidity_verifier.js";
export async function fflonkExportSolidityVerifierCmd(vk, templates, logger) {
return fflonkExportSolidityVerifier(vk, templates, logger);
}

View File

@@ -1,43 +0,0 @@
/*
This file is part of snarkjs.
snarkjs is a free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your option)
any later version.
snarkjs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
snarkjs. If not, see <https://www.gnu.org/licenses/>.
*/
import fflonkProve from "../fflonk_prove.js";
import wtns_calculate from "../wtns_calculate.js";
import {utils} from "ffjavascript";
const {unstringifyBigInts, stringifyBigInts} = utils;
import fs from "fs";
import bfj from "bfj";
export async function fflonkFullProveCmd(zkeyFilename, witnessInputsFilename, wasmFilename, publicInputsFilename, proofFilename, logger) {
let input = JSON.parse(await fs.promises.readFile(witnessInputsFilename, "utf8"));
input = unstringifyBigInts(input);
const wtns= {type: "mem"};
// Compute the witness
await wtns_calculate(input, wasmFilename, wtns);
// Compute the proof
const {proof, publicSignals} = await fflonkProve(zkeyFilename, wtns, logger);
// Write the proof and the publig signals in each file
await bfj.write(proofFilename, stringifyBigInts(proof), {space: 1});
await bfj.write(publicInputsFilename, stringifyBigInts(publicSignals), {space: 1});
return 0;
}

View File

@@ -1,33 +0,0 @@
/*
This file is part of snarkjs.
snarkjs is a free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your option)
any later version.
snarkjs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
snarkjs. If not, see <https://www.gnu.org/licenses/>.
*/
import bfj from "bfj";
import {utils} from "ffjavascript";
import fflonkProve from "../fflonk_prove.js";
const {stringifyBigInts} = utils;
export async function fflonkProveCmd(zkeyFilename, witnessFilename, publicInputsFilename, proofFilename, logger) {
const {proof, publicSignals} = await fflonkProve(zkeyFilename, witnessFilename, logger);
if(undefined !== proofFilename && undefined !== publicInputsFilename) {
await bfj.write(proofFilename, stringifyBigInts(proof), {space: 1});
await bfj.write(publicInputsFilename, stringifyBigInts(publicSignals), {space: 1});
}
return {proof, publicSignals};
}

View File

@@ -1,22 +0,0 @@
/*
This file is part of snarkjs.
snarkjs is a free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your option)
any later version.
snarkjs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
snarkjs. If not, see <https://www.gnu.org/licenses/>.
*/
import fflonkSetup from "../fflonk_setup.js";
export async function fflonkSetupCmd(r1csFilename, ptauFilename, zkeyFilename, logger) {
return fflonkSetup(r1csFilename, ptauFilename, zkeyFilename, logger);
}

View File

@@ -1,22 +0,0 @@
/*
This file is part of snarkjs.
snarkjs is a free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your option)
any later version.
snarkjs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
snarkjs. If not, see <https://www.gnu.org/licenses/>.
*/
import wtnsCheck from "../wtns_check.js";
export async function wtnsCheckCmd(r1csFilename, wtnsFilename, logger) {
return await wtnsCheck(r1csFilename, wtnsFilename, logger);
}

View File

@@ -1,20 +0,0 @@
/*
Copyright 2022 iden3 association.
This file is part of snarkJS.
snarkJS is a free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
snarkJS is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU General Public License
along with snarkJS. If not, see <https://www.gnu.org/licenses/>.
*/
export {wtnsCheckCmd} from "./wtns_check_cmd.js";

View File

@@ -17,28 +17,9 @@
snarkjs. If not, see <https://www.gnu.org/licenses/>.
*/
// FFlonk constants
export const FF_T_POL_DEG_MIN = 3;
// FFlonk A
export const A = 12;
// ZKEY constants
export const ZKEY_FF_NSECTIONS = 17;
export const ZKEY_FF_HEADER_SECTION = 2;
export const ZKEY_FF_ADDITIONS_SECTION = 3;
export const ZKEY_FF_A_MAP_SECTION = 4;
export const ZKEY_FF_B_MAP_SECTION = 5;
export const ZKEY_FF_C_MAP_SECTION = 6;
export const ZKEY_FF_QL_SECTION = 7;
export const ZKEY_FF_QR_SECTION = 8;
export const ZKEY_FF_QM_SECTION = 9;
export const ZKEY_FF_QO_SECTION = 10;
export const ZKEY_FF_QC_SECTION = 11;
export const ZKEY_FF_SIGMA1_SECTION = 12;
export const ZKEY_FF_SIGMA2_SECTION = 13;
export const ZKEY_FF_SIGMA3_SECTION = 14;
export const ZKEY_FF_LAGRANGE_SECTION = 15;
export const ZKEY_FF_PTAU_SECTION = 16;
export const ZKEY_FF_C0_SECTION = 17;
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 exportSolidityVerifier } from "./fflonk_export_solidity_verifier.js";
export { default as exportSolidityCallData } from "./fflonk_export_calldata.js";

44
src/fflonk_constants.js Normal file
View File

@@ -0,0 +1,44 @@
/*
Copyright 2022 iden3 association.
This file is part of snarkjs.
snarkjs is a free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your option)
any later version.
snarkjs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
snarkjs. If not, see <https://www.gnu.org/licenses/>.
*/
// FFlonk constants
export const FF_T_POL_DEG_MIN = 3;
// FFlonk A
export const A = 12;
// ZKEY constants
export const ZKEY_FF_NSECTIONS = 17;
export const ZKEY_FF_HEADER_SECTION = 2;
export const ZKEY_FF_ADDITIONS_SECTION = 3;
export const ZKEY_FF_A_MAP_SECTION = 4;
export const ZKEY_FF_B_MAP_SECTION = 5;
export const ZKEY_FF_C_MAP_SECTION = 6;
export const ZKEY_FF_QL_SECTION = 7;
export const ZKEY_FF_QR_SECTION = 8;
export const ZKEY_FF_QM_SECTION = 9;
export const ZKEY_FF_QO_SECTION = 10;
export const ZKEY_FF_QC_SECTION = 11;
export const ZKEY_FF_SIGMA1_SECTION = 12;
export const ZKEY_FF_SIGMA2_SECTION = 13;
export const ZKEY_FF_SIGMA3_SECTION = 14;
export const ZKEY_FF_LAGRANGE_SECTION = 15;
export const ZKEY_FF_PTAU_SECTION = 16;
export const ZKEY_FF_C0_SECTION = 17;

View File

@@ -33,7 +33,7 @@ function p256(n) {
return nstr;
}
export default async function fflonkExportCallData(_pub, _proof, logger) {
export default async function fflonkExportCallData(_pub, _proof) {
const proof = unstringifyBigInts(_proof);
const pub = unstringifyBigInts(_pub);

View File

@@ -15,13 +15,19 @@
snarkjs. If not, see <https://www.gnu.org/licenses/>.
*/
import fs from "fs";
import fflonkVerify from "../fflonk_verify.js";
import fflonkProve from "./fflonk_prove.js";
import wtns_calculate from "./wtns_calculate.js";
import {utils} from "ffjavascript";
const {unstringifyBigInts} = utils;
export async function fflonkVerifyCmd(vkeyFilename, publicInputsFilename, proofFilename, logger) {
const vkey = JSON.parse(fs.readFileSync(vkeyFilename, "utf8"));
const publicInputs = JSON.parse(fs.readFileSync(publicInputsFilename, "utf8"));
const proof = JSON.parse(fs.readFileSync(proofFilename, "utf8"));
export default async function fflonkFullProve(_input, wasmFilename, zkeyFilename, logger) {
const input = unstringifyBigInts(_input);
return await fflonkVerify(vkey, publicInputs, proof, logger);
const wtns= {type: "mem"};
// Compute the witness
await wtns_calculate(input, wasmFilename, wtns);
// Compute the proof
return await fflonkProve(zkeyFilename, wtns, logger);
}

View File

@@ -38,7 +38,7 @@ import {
ZKEY_FF_SIGMA1_SECTION,
ZKEY_FF_SIGMA2_SECTION,
ZKEY_FF_SIGMA3_SECTION,
} from "./fflonk.js";
} from "./fflonk_constants.js";
import { Keccak256Transcript } from "./Keccak256Transcript.js";
import { Proof } from "./proof.js";
import { Polynomial } from "./polynomial/polynomial.js";

View File

@@ -42,7 +42,7 @@ import {
FF_T_POL_DEG_MIN,
ZKEY_FF_NSECTIONS,
ZKEY_FF_C0_SECTION,
} from "./fflonk.js";
} from "./fflonk_constants.js";
import {FFLONK_PROTOCOL_ID, HEADER_ZKEY_SECTION} from "./zkey_constants.js";
import {
getFFlonkAdditionConstraint,

View File

@@ -19,7 +19,7 @@
import plonk_prove from "./plonk_prove.js";
import wtns_calculate from "./wtns_calculate.js";
import {utils} from 'ffjavascript';
import {utils} from "ffjavascript";
const {unstringifyBigInts} = utils;
export default async function plonkFullProve(_input, wasmFile, zkeyFileName, logger) {

View File

@@ -20,3 +20,4 @@
export {default as calculate} from "./wtns_calculate.js";
export {default as debug} from "./wtns_debug.js";
export {default as exportJson} from "./wtns_export_json.js";
export {default as check} from "./wtns_check.js";

View File

@@ -1,8 +1,7 @@
import * as fastFile from "fastfile";
import ejs from "ejs";
import exportVerificationKey from "./zkey_export_verificationkey.js";
import {fflonkExportSolidityVerifierCmd} from "./cmds/fflonk_cmds.js";
import fflonkExportSolidityVerifierCmd from "./fflonk_export_solidity_verifier.js";
// Not ready yet
// module.exports.generateVerifier_kimleeoh = generateVerifier_kimleeoh;

View File

@@ -50,7 +50,7 @@ import * as binFileUtils from "@iden3/binfileutils";
import { getCurveFromQ as getCurve } from "./curves.js";
import { log2 } from "./misc.js";
import {FFLONK_PROTOCOL_ID, GROTH16_PROTOCOL_ID, PLONK_PROTOCOL_ID} from "./zkey_constants.js";
import {ZKEY_FF_HEADER_SECTION} from "./fflonk.js";
import {ZKEY_FF_HEADER_SECTION} from "./fflonk_constants.js";
export async function writeHeader(fd, zkey) {

View File

@@ -1,4 +1,4 @@
import * as fflonk from "../src/cmds/fflonk_cmds.js";
import * as fflonk from "../src/fflonk.js";
import zkeyExportVerificationKey from "../src/zkey_export_verificationkey.js";
import { getCurveFromName } from "../src/curves.js";
import path from "path";
@@ -32,17 +32,17 @@ describe("Fflonk test suite", function () {
it("fflonk full prove", async () => {
// fflonk setup
await fflonk.fflonkSetupCmd(r1csFilename, ptauFilename, zkeyFilename);
await fflonk.setup(r1csFilename, ptauFilename, zkeyFilename);
// flonk prove
await fflonk.fflonkProveCmd(zkeyFilename, wtnsFilename, publicInputsFilename, proofFilename);
const {proof, publicSignals} = await fflonk.prove(zkeyFilename, wtnsFilename);
// export verification key
const vKey = await zkeyExportVerificationKey(zkeyFilename);
await bfj.write(vkeyFilename, stringifyBigInts(vKey), { space: 1 });
// Verify the proof
const isValid = await fflonk.fflonkVerifyCmd(vkeyFilename, publicInputsFilename, proofFilename);
const isValid = await fflonk.verify(vKey, publicSignals, proof);
assert(isValid);
});

View File

@@ -113,7 +113,7 @@ describe("Full process", function () {
});
it ("checks witness complies with r1cs", async () => {
await snarkjs.wtnsCmds.wtnsCheckCmd(path.join("test", "circuit", "circuit.r1cs"), wtns);
await snarkjs.wtns.check(path.join("test", "circuit", "circuit.r1cs"), wtns);
});
it ("groth16 proof", async () => {