From f0ed56e9f2a2f2939793954269d3e2f3df500c25 Mon Sep 17 00:00:00 2001 From: Shivam Saxena Date: Tue, 21 Mar 2023 14:54:23 +0530 Subject: [PATCH] Added support for compilation mode 'c' --- src/circuit.ts | 18 +++++++++++++----- src/configParser.ts | 1 + src/index.ts | 2 +- src/types.ts | 1 + src/vendors/circom_tester/c/tester.js | 9 ++++----- tests/circomJS.test.ts | 2 +- tests/circuit.test.ts | 2 +- tests/configParser.test.ts | 2 +- tests/data/circuit.config.json | 2 +- 9 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/circuit.ts b/src/circuit.ts index 9690975..6960421 100644 --- a/src/circuit.ts +++ b/src/circuit.ts @@ -2,6 +2,7 @@ import * as path from "path"; import log from "log" const {wasm: wasmTester} = require("./vendors/circom_tester") +const {c: cTester} = require("./vendors/circom_tester") import {CircuitConfig, Networks, Witness, ZK_PROOF} from "./types"; import {genGrothZKey, genPlonkZKey, genVerificationKey} from "./utils/zKey"; @@ -28,11 +29,18 @@ export class Circuit { fs.mkdirSync(this._circuitConfig.outputDir, {recursive:true}) } - this._wasmTester = await wasmTester(this._circuitConfig.inputFilePath, { - output: this._circuitConfig.outputDir, - ...this._circuitConfig.compileOptions - }) - + if(this._circuitConfig.compilationMode =='wasm'){ + this._wasmTester = await wasmTester(this._circuitConfig.inputFilePath, { + output: this._circuitConfig.outputDir, + ...this._circuitConfig.compileOptions + }) + } else if (this._circuitConfig.compilationMode =='c'){ + this._wasmTester = await cTester(this._circuitConfig.inputFilePath, { + output: this._circuitConfig.outputDir, + ...this._circuitConfig.compileOptions + }) + } + } diff --git a/src/configParser.ts b/src/configParser.ts index 37e7769..59ef8a4 100644 --- a/src/configParser.ts +++ b/src/configParser.ts @@ -144,6 +144,7 @@ export class ConfigParser { zKeyPath: path.join(cktOutputDir, "circuit_final.zkey"), // DO NOT change the names vKeyPath: path.join(cktOutputDir, "verification_key.json"), // DO NOT change the names r1csPath: path.join(cktOutputDir, `${cktName}.r1cs`), + compilationMode: c.compilationMode? c.compilationMode : "wasm", compileOptions: { include: [], snarkType: c.proofType ? c.proofType : "groth16", diff --git a/src/index.ts b/src/index.ts index a90985e..9b98345 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1 @@ -export { default as CircomJS } from "./circomJs" \ No newline at end of file +export { default as CircomJS } from "./circomJs" diff --git a/src/types.ts b/src/types.ts index 4993e13..f71f31d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -65,6 +65,7 @@ export type CircuitConfig = { zKeyPath: string; vKeyPath: string; compileOptions: CompileOptions; + compilationMode: CompilationMode; r1csPath:string } diff --git a/src/vendors/circom_tester/c/tester.js b/src/vendors/circom_tester/c/tester.js index 048f7cb..a1b421b 100644 --- a/src/vendors/circom_tester/c/tester.js +++ b/src/vendors/circom_tester/c/tester.js @@ -55,7 +55,7 @@ async function c_tester(circomInput, _options) { assert(false,"Cannot set recompile to false if the "+jsPath+" folder does not exist"); } } - return new CTester(options.output, baseName, run); + return new CTester(options.output, baseName); } async function compile (baseName, fileName, options) { @@ -76,9 +76,9 @@ async function compile (baseName, fileName, options) { if (options.O === 0) flags += "--O0 "; if (options.O === 1) flags += "--O1 "; if (options.verbose) flags += "--verbose "; - + let b; try { - let b = await exec("circom " + flags + fileName); + b = await exec("circom " + flags + fileName); if (options.verbose) { console.log(b.stdout); } @@ -95,10 +95,9 @@ async function compile (baseName, fileName, options) { class CTester { - constructor(dir, baseName, witnessCalculator) { + constructor(dir, baseName) { this.dir=dir; this.baseName = baseName; - this.witnessCalculator = witnessCalculator; } async release() { diff --git a/tests/circomJS.test.ts b/tests/circomJS.test.ts index c90fc80..ab268dc 100644 --- a/tests/circomJS.test.ts +++ b/tests/circomJS.test.ts @@ -22,7 +22,7 @@ const jsonConfig = ` "cID": "circ1000constraints", "fileName": "circ1000constraints.circom", "proofType": "plonk", - "compilationMode": "wasm" + "compilationMode": "c" } ] } diff --git a/tests/circuit.test.ts b/tests/circuit.test.ts index f8039f8..9a0d6a8 100644 --- a/tests/circuit.test.ts +++ b/tests/circuit.test.ts @@ -24,7 +24,7 @@ const jsonConfig = ` "cID": "circ1000constraints", "fileName": "circ1000constraints.circom", "proofType": "plonk", - "compilationMode": "wasm" + "compilationMode": "c" } ] } diff --git a/tests/configParser.test.ts b/tests/configParser.test.ts index 8eade87..bb52a09 100644 --- a/tests/configParser.test.ts +++ b/tests/configParser.test.ts @@ -26,7 +26,7 @@ describe("ConfigParser test", () => { "cID": "circ1000constraints", "fileName": "circ1000constraints.circom", "proofType": "groth16", - "compilationMode": "wasm" + "compilationMode": "c" } ] } diff --git a/tests/data/circuit.config.json b/tests/data/circuit.config.json index bab67fd..1446b68 100644 --- a/tests/data/circuit.config.json +++ b/tests/data/circuit.config.json @@ -16,7 +16,7 @@ "cID": "circ1000constraints", "fileName": "circ1000constraints.circom", "proofType": "plonk", - "compilationMode": "wasm" + "compilationMode": "c" } ] },