Skip to content

Commit

Permalink
Merge pull request #100 from numtel/optimization_default_skip_argument
Browse files Browse the repository at this point in the history
Optimization level null uses Circom default
  • Loading branch information
erhant authored Oct 25, 2024
2 parents 37c16af + e3426e6 commit 7e02f54
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
9 changes: 5 additions & 4 deletions src/configs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: 2, >=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. */
Expand Down Expand Up @@ -66,7 +67,7 @@ export const DEFAULT = Object.seal<Readonly<CircomkitConfig>>({
dirBuild: './build',
circomPath: 'circom',
// compiler-specific
optimization: 2,
optimization: undefined,
inspect: true,
include: ['./node_modules'],
cWitness: false,
Expand Down
7 changes: 5 additions & 2 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 8 additions & 6 deletions src/functions/circuit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <value>
flags += ` --O2round ${config.optimization}`;
} else {
// --O0, --O1 or --O2
flags += ` --O${config.optimization}`;
if (typeof config.optimization === 'number') {
if (config.optimization > 2) {
// --O2round <value>
flags += ` --O2round ${config.optimization}`;
} else {
// --O0, --O1 or --O2
flags += ` --O${config.optimization}`;
}
}

// call `circom` as a sub-process
Expand Down
1 change: 1 addition & 0 deletions tests/witnessTester.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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});
});
Expand Down

0 comments on commit 7e02f54

Please sign in to comment.