From ae0a89c269acf86b99c5eebab0dd607433dad829 Mon Sep 17 00:00:00 2001 From: numtel Date: Wed, 16 Oct 2024 17:05:28 -0700 Subject: [PATCH 1/3] Optimization level null uses Circom default --- src/configs/index.ts | 9 +++++---- src/core/index.ts | 4 ++-- src/functions/circuit.ts | 14 ++++++++------ tests/witnessTester.test.ts | 1 + 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/configs/index.ts b/src/configs/index.ts index f5804f0..725661d 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). + * - `null`: 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 | null; /** 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: null, inspect: true, include: ['./node_modules'], cWitness: false, diff --git a/src/core/index.ts b/src/core/index.ts index 8fa55db..1ad86f4 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -65,7 +65,7 @@ export class Circomkit { if (!PROTOCOLS.includes(this.config.protocol)) { throw new Error('Invalid protocol in configuration.'); } - if (this.config.optimization < 0) { + 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 +499,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}); }); From f5611ff31590b3e1a8084064f591d434d6f8f2e6 Mon Sep 17 00:00:00 2001 From: numtel Date: Thu, 17 Oct 2024 15:09:54 -0700 Subject: [PATCH 2/3] Error when optimization is not null or number --- src/core/index.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/core/index.ts b/src/core/index.ts index 1ad86f4..1cb85b4 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -65,6 +65,9 @@ export class Circomkit { if (!PROTOCOLS.includes(this.config.protocol)) { throw new Error('Invalid protocol in configuration.'); } + if (this.config.optimization !== null && 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; From e3426e68691cba4715ea5916aa02c622e9b31665 Mon Sep 17 00:00:00 2001 From: numtel Date: Thu, 17 Oct 2024 15:16:44 -0700 Subject: [PATCH 3/3] Use undefined instead of null as default --- src/configs/index.ts | 6 +++--- src/core/index.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/configs/index.ts b/src/configs/index.ts index 725661d..81bc76d 100644 --- a/src/configs/index.ts +++ b/src/configs/index.ts @@ -32,13 +32,13 @@ export type CircomkitConfig = { /** * [Optimization level](https://docs.circom.io/getting-started/compilation-options/#flags-and-options-related-to-the-r1cs-optimization). * See [`circom/src/input_user.rs`](https://github.com/iden3/circom/blob/master/circom/src/input_user.rs#L249). - * - `null`: Follow Circom default. (=v2.2.0: 1) + * - `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. * - `>2`: Any number higher than 2 will use `--O2round` with the number as simplification rounds. */ - optimization: number | null; + optimization: number | undefined; /** Does an additional check over the constraints produced. */ inspect: boolean; /** Include paths as libraries during compilation. */ @@ -67,7 +67,7 @@ export const DEFAULT = Object.seal>({ dirBuild: './build', circomPath: 'circom', // compiler-specific - optimization: null, + optimization: undefined, inspect: true, include: ['./node_modules'], cWitness: false, diff --git a/src/core/index.ts b/src/core/index.ts index 1cb85b4..81e74bd 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -65,7 +65,7 @@ export class Circomkit { if (!PROTOCOLS.includes(this.config.protocol)) { throw new Error('Invalid protocol in configuration.'); } - if (this.config.optimization !== null && typeof this.config.optimization !== 'number') { + 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) {