Skip to content

Commit

Permalink
AssertionError not working, revert to original
Browse files Browse the repository at this point in the history
  • Loading branch information
erhant committed Jul 1, 2023
1 parent d8fabde commit b527615
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
23 changes: 21 additions & 2 deletions circuits/multiplier.circom
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
pragma circom 2.0.0;

template IsZero() {
signal input in;
signal output out;

signal inv;
inv <-- in != 0 ? (1 / in) : 0;

out <== (-in * inv) + 1;
in * out === 0;
}

template Multiplier(n) {
assert(n > 1);
signal input in[n];
signal output out;

signal inner[n-1];
// assert that all numbers are != 1
component isZero[n];
for (var i = 0; i < n; i++) {
isZero[i] = IsZero();
isZero[i].in <== in[i] - 1;
isZero[i].out === 0;
}

// multiply
signal inner[n-1];
inner[0] <== in[0] * in[1];
for(var i = 2; i < n; i++) {
for (var i = 2; i < n; i++) {
inner[i-1] <== inner[i-2] * in[i];
}

Expand Down
19 changes: 10 additions & 9 deletions src/testers/witnessTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ export default class WitnessTester<IN extends readonly string[] = [], OUT extend
this.circomWasmTester = circomWasmTester;
}

/** Assert that constraints are valid for a given witness.
* @param witness witness
*/
/** Assert that constraints are valid for a given witness. */
async expectConstraintPass(witness: WitnessType): Promise<void> {
return this.circomWasmTester.checkConstraints(witness);
}
Expand All @@ -27,18 +25,18 @@ export default class WitnessTester<IN extends readonly string[] = [], OUT extend
* This is useful to test if a fake witness (a witness from a
* dishonest prover) can still be valid, which would indicate
* that there are soundness errors in the circuit.
* @param witness witness
*/
async expectConstraintFail(witness: WitnessType): Promise<void> {
await this.expectConstraintPass(witness).then(
() => assert.fail('Expected constraints to not match.'),
err => expect(err).to.be.instanceOf(AssertionError)
err => {
// console.log(err.message);
expect(err.message).to.eq("Constraint doesn't match");
}
);
}

/** Compute witness given the input signals.
* @param input all signals, private and public
*/
/** Compute witness given the input signals. */
async calculateWitness(input: CircuitSignals<IN>): Promise<WitnessType> {
return this.circomWasmTester.calculateWitness(input, true);
}
Expand Down Expand Up @@ -74,7 +72,10 @@ export default class WitnessTester<IN extends readonly string[] = [], OUT extend
async expectFail(input: CircuitSignals<IN>) {
await this.calculateWitness(input).then(
() => assert.fail('Expected witness calculation to fail.'),
err => expect(err).to.be.instanceOf(AssertionError)
err => {
// console.log(err.message);
expect(err.message.startsWith('Error: Assert Failed.')).to.be.true;
}
);
}

Expand Down
5 changes: 5 additions & 0 deletions tests/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ export const N = 3;

const numbers = Array.from({length: N}, () => Math.floor(Math.random() * 100 * N));
const product = numbers.reduce((prev, acc) => acc * prev);
const malicious = Array.from({length: N}, () => 1);
malicious[0] = product;

export const CIRCUIT_NAME = `multiplier_${N}`;
export const CIRCUIT_CONFIG = {
Expand All @@ -16,6 +18,9 @@ export const PTAU_PATH = './ptau/powersOfTau28_hez_final_08.ptau';
export const INPUT = {
in: numbers,
};
export const BAD_INPUT = {
in: malicious,
};
export const OUTPUT = {
out: product,
};
14 changes: 11 additions & 3 deletions tests/testers.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Circomkit, ProofTester, WitnessTester} from '../src';
import {CIRCUIT_CONFIG, CIRCUIT_NAME, INPUT, N, OUTPUT} from './common';
import {BAD_INPUT, CIRCUIT_CONFIG, CIRCUIT_NAME, INPUT, N, OUTPUT} from './common';
import {expect} from 'chai';

describe('witness tester', () => {
Expand All @@ -14,13 +14,21 @@ describe('witness tester', () => {
});

it('should have correct number of constraints', async () => {
await circuit.expectConstraintCount(N, true);
// N - 1 constraints for each multiplication
// 1 constraint for the output
// 4 * N constraints to check if each input is different than 1
// TOTAL: 5 * N
await circuit.expectConstraintCount(5 * N, true);
});

it('should assert correctly', async () => {
await circuit.expectPass(INPUT, OUTPUT);
});

it('should fail for bad inupt', async () => {
await circuit.expectFail(BAD_INPUT);
});

it('should compute correctly', async () => {
const output = await circuit.compute(INPUT, ['out']);
expect(output).to.haveOwnProperty('out');
Expand All @@ -35,7 +43,7 @@ describe('witness tester', () => {
it('should NOT assert for bad witness', async () => {
const witness = await circuit.calculateWitness(INPUT);
const badWitness = await circuit.editWitness(witness, {
'main.inner[0]': 99999n,
'main.inner[0]': 1n,
});
await circuit.expectConstraintFail(badWitness);
});
Expand Down

0 comments on commit b527615

Please sign in to comment.