Skip to content

Commit

Permalink
fixes & new version (#77)
Browse files Browse the repository at this point in the history
* fixes & new version

* comments
  • Loading branch information
erhant authored Apr 26, 2024
1 parent 18266fb commit c9f951a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 25 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "circomkit",
"version": "0.0.25",
"description": "A Circom development environment",
"version": "0.1.0",
"description": "A Circom testing & development environment",
"author": "erhant",
"license": "MIT",
"engines": {
Expand Down
20 changes: 11 additions & 9 deletions src/circomkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class Circomkit {
}

/** Computes a path that requires a circuit name. */
private path(circuit: string, type: CircuitPathBuilders): string {
path(circuit: string, type: CircuitPathBuilders): string {
const dir = `${this.config.dirBuild}/${circuit}`;
switch (type) {
case 'dir':
Expand All @@ -117,17 +117,17 @@ export class Circomkit {
}

/** Computes a path that requires a circuit and an input name. */
private pathWithInput(circuit: string, input: string, type: CircuitInputPathBuilders): string {
pathWithInput(circuit: string, input: string, type: CircuitInputPathBuilders): string {
const dir = `${this.config.dirBuild}/${circuit}/${input}`;
switch (type) {
case 'dir':
return dir;
case 'wtns':
return `${dir}/witness.wtns`;
case 'pubs':
return `${dir}/public.json`;
case 'proof':
return `${dir}/proof.json`;
case 'wtns':
return `${dir}/witness.wtns`;
return `${dir}/${this.config.protocol}_proof.json`;
case 'in':
return `${this.config.dirInputs}/${circuit}/${input}.json`;
default:
Expand All @@ -136,13 +136,13 @@ export class Circomkit {
}

/** Given a PTAU name, returns the relative path. */
private pathPtau(ptauName: string): string {
pathPtau(ptauName: string): string {
return `${this.config.dirPtau}/${ptauName}`;
}

/** Given a circuit & id name, returns the relative path of the phase-2 PTAU.
* This is used in particular by Groth16's circuit-specific setup phase. */
private pathZkey(circuit: string, id: number): string {
pathZkey(circuit: string, id: number): string {
return `${this.config.dirBuild}/${circuit}/${circuit}_${id}.zkey`;
}

Expand Down Expand Up @@ -185,8 +185,10 @@ export class Circomkit {
}

/** Read the information about the circuit by extracting it from the R1CS file.
* This implementation follows the specs at
* https://github.com/iden3/r1csfile/blob/master/doc/r1cs_bin_format.md.
*
* This implementation follows the specs at [iden3/r1csfile](https://github.com/iden3/r1csfile/blob/master/doc/r1cs_bin_format.md)
* and is inspired from the work by [PSE's `p0tion`](https://github.com/privacy-scaling-explorations/p0tion/blob/f88bcee5d499dce975d0592ed10b21aa8d73bbd2/packages/actions/src/helpers/utils.ts#L413)
* and by [Weijiekoh's `circom-helper`](https://github.com/weijiekoh/circom-helper/blob/master/ts/read_num_inputs.ts#L5).
*/
async info(circuit: string): Promise<R1CSInfoType> {
let pointer = 0;
Expand Down
26 changes: 16 additions & 10 deletions src/testers/witnessTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,29 @@ export class WitnessTester<IN extends readonly string[] = [], OUT extends readon
* See [here](https://github.com/iden3/circom/blob/master/code_producers/src/wasm_elements/common/witness_calculator.js#L21)
* for the list of errors that may occur during witness calculation.
* Most of the time, you will be expecting an assertion error.
*
* @returns the error message.
*/
async expectFail(input: CircuitSignals<IN>) {
await this.calculateWitness(input).then(
async expectFail(input: CircuitSignals<IN>): Promise<string> {
return await this.calculateWitness(input).then(
() => assert.fail('Expected witness calculation to fail.'),
err => {
const errorMessage = (err as Error).message;

const isExpectedError = [
'Error: Assert Failed.', // a constraint failure (most common)
'Not enough values for input signal', // few inputs than expected for a signal
'Too many values for input signal', // more inputs than expected for a signal
].some(msg => (err as Error).message.startsWith(msg));
if (isExpectedError) {
// we expected this failure, register it as an expect call
expect(isExpectedError).to.be.true;
} else {
// we did not expect this failure, throw it anyways
throw err;
}
'Not all inputs have been set.', // few inputs than expected for many signals
].some(msg => errorMessage.startsWith(msg));

// we did not expect this failure, throw it anyways
if (!isExpectedError) throw err;

// we expected this failure, register it as an expect call
expect(isExpectedError).to.be.true;

return errorMessage;
}
);
}
Expand Down
3 changes: 1 addition & 2 deletions src/utils/r1cs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {readSync, ReadPosition} from 'fs';

/** Reads a specified number of bytes from a file and converts them to a BigInt.
*/
/** Reads a specified number of bytes from a file and converts them to a `BigInt`. */
export function readBytesFromFile(fd: number, offset: number, length: number, position: ReadPosition): BigInt {
const buffer = Buffer.alloc(length);

Expand Down
9 changes: 7 additions & 2 deletions tests/errors.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Circomkit, WitnessTester} from '../src';

describe('error catching', () => {
describe('errors', () => {
let circuit: WitnessTester<['in', 'inin'], ['out']>;

before(async () => {
Expand All @@ -10,25 +10,30 @@ describe('error catching', () => {

it('should fail for fewer inputs than expected', async () => {
await circuit.expectFail({in: 0, inin: [1]});
// Not enough values for input signal inin
});

it('should fail for more inputs than expected', async () => {
await circuit.expectFail({in: 0, inin: [1, 2, 3]});
// Too many values for input signal inin
});

it('should fail due to false-assert', async () => {
await circuit.expectFail({in: 1, inin: [1, 2]});
// Error: Assert Failed.
});

it('should fail due to missing signal', async () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
await circuit.expectFail({inin: [1, 2, 3]});
await circuit.expectFail({inin: [1, 2]});
// Not all inputs have been set. Only 2 out of 3.
});

it('should fail due to extra signal', async () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
await circuit.expectFail({inin: [1, 2, 3], idontexist: 1});
// Too many values for input signal inin
});
});

0 comments on commit c9f951a

Please sign in to comment.