diff --git a/src/configs/index.ts b/src/configs/index.ts index f5804f0..81bc76d 100644 --- a/src/configs/index.ts +++ b/src/configs/index.ts @@ -31,13 +31,14 @@ export type CircomkitConfig = { version: `${number}.${number}.${number}`; /** * [Optimization level](https://docs.circom.io/getting-started/compilation-options/#flags-and-options-related-to-the-r1cs-optimization). - * Defaults to `2` as per the Circom defaults, see [`circom/src/input_user.rs`](https://github.com/iden3/circom/blob/master/circom/src/input_user.rs#L249). + * See [`circom/src/input_user.rs`](https://github.com/iden3/circom/blob/master/circom/src/input_user.rs#L249). + * - `undefined`: Follow Circom default. (=v2.2.0: 1) * - `0`: No simplification is applied. * - `1`: Only applies `var` to `var` and `var` to `constant` simplification. - * - `2`: Full constraint simplificiation via Gaussian eliminations. (Default) + * - `2`: Full constraint simplificiation via Gaussian eliminations. * - `>2`: Any number higher than 2 will use `--O2round` with the number as simplification rounds. */ - optimization: number; + optimization: number | undefined; /** Does an additional check over the constraints produced. */ inspect: boolean; /** Include paths as libraries during compilation. */ @@ -66,7 +67,7 @@ export const DEFAULT = Object.seal>({ dirBuild: './build', circomPath: 'circom', // compiler-specific - optimization: 2, + optimization: undefined, inspect: true, include: ['./node_modules'], cWitness: false, diff --git a/src/core/index.ts b/src/core/index.ts index 8fa55db..81e74bd 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -65,7 +65,10 @@ export class Circomkit { if (!PROTOCOLS.includes(this.config.protocol)) { throw new Error('Invalid protocol in configuration.'); } - if (this.config.optimization < 0) { + if (this.config.optimization !== undefined && typeof this.config.optimization !== 'number') { + throw new Error('Invalid optimization level.'); + } + if (typeof this.config.optimization === 'number' && this.config.optimization < 0) { this.log.warn('Optimization level must be at least 0, setting it to 0.'); this.config.optimization = 0; } @@ -499,7 +502,7 @@ export class Circomkit { output: undefined, // this makes tests to be created under /tmp prime: this.config.prime, verbose: this.config.verbose, - O: Math.min(this.config.optimization, 1), // tester doesnt have O2 + O: typeof this.config.optimization === 'number' ? Math.min(this.config.optimization, 1) : undefined, // tester doesnt have O2 json: false, include: this.config.include, wasm: true, diff --git a/src/functions/circuit.ts b/src/functions/circuit.ts index 24322d0..3f349af 100644 --- a/src/functions/circuit.ts +++ b/src/functions/circuit.ts @@ -21,12 +21,14 @@ export async function compileCircuit(config: CircomkitConfig, targetPath: string if (config.verbose) flags += ' --verbose'; if (config.inspect) flags += ' --inspect'; if (config.cWitness) flags += ' --c'; - if (config.optimization > 2) { - // --O2round - flags += ` --O2round ${config.optimization}`; - } else { - // --O0, --O1 or --O2 - flags += ` --O${config.optimization}`; + if (typeof config.optimization === 'number') { + if (config.optimization > 2) { + // --O2round + flags += ` --O2round ${config.optimization}`; + } else { + // --O0, --O1 or --O2 + flags += ` --O${config.optimization}`; + } } // call `circom` as a sub-process diff --git a/tests/witnessTester.test.ts b/tests/witnessTester.test.ts index 31676bc..02b2442 100644 --- a/tests/witnessTester.test.ts +++ b/tests/witnessTester.test.ts @@ -19,6 +19,7 @@ describe('witness tester', () => { dirCircuits: './tests/circuits', dirInputs: './tests/inputs', dirBuild: './tests/build', + optimization: 2, }); circuit = await circomkit.WitnessTester(name, {...config, recompile: true}); });