Skip to content

Commit

Permalink
separate utils circuits and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lonerapier committed Aug 23, 2024
1 parent 4ad1a67 commit 4ac6f77
Show file tree
Hide file tree
Showing 13 changed files with 694 additions and 688 deletions.
2 changes: 1 addition & 1 deletion circuits/extract.circom
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pragma circom 2.1.9;

include "utils.circom";
include "./utils/bytes.circom";
include "parser.circom";

template Extract(DATA_BYTES, MAX_STACK_HEIGHT) {
Expand Down
4 changes: 3 additions & 1 deletion circuits/parser.circom
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ Tests for this module are located in the files: `circuits/test/parser/*.test.ts

pragma circom 2.1.9;

include "utils.circom";
include "./utils/array.circom";
include "./utils/bytes.circom";
include "./utils/operators.circom";
include "language.circom";

/*
Expand Down
3 changes: 2 additions & 1 deletion circuits/search.circom
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
pragma circom 2.1.9;
pragma circom 2.1.9;

include "circomlib/circuits/mux1.circom";
include "./utils/hash.circom";
include "./utils/operators.circom";
include "./utils/array.circom";

/*
SubstringSearch
Expand Down
30 changes: 0 additions & 30 deletions circuits/test/array.test.ts

This file was deleted.

185 changes: 185 additions & 0 deletions circuits/test/utils/array.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import { circomkit, WitnessTester } from "../common";

describe("array", () => {
describe("Slice", () => {
let circuit: WitnessTester<["in"], ["out"]>;
before(async () => {
circuit = await circomkit.WitnessTester(`Slice`, {
file: "circuits/utils/array",
template: "Slice",
params: [10, 2, 4],
});
console.log("#constraints:", await circuit.getConstraintCount());
});

it("witness: [random*10], start: 2, end: 4", async () => {
const input = Array.from({ length: 10 }, () => Math.floor(Math.random() * 256));
await circuit.expectPass(
{ in: input },
{ out: input.slice(2, 4) }
);
});

it("witness: [random*9], start: 2, end: 4", async () => {
const input = Array.from({ length: 9 }, () => Math.floor(Math.random() * 256));
await circuit.expectFail(
{ in: input },
);
});
});
});

describe("IsEqualArray", () => {
let circuit: WitnessTester<["in"], ["out"]>;
before(async () => {
circuit = await circomkit.WitnessTester(`IsEqualArray`, {
file: "circuits/utils/array",
template: "IsEqualArray",
params: [3],
});
console.log("#constraints:", await circuit.getConstraintCount());
});

it("witness: [[0,0,0],[0,0,0]]", async () => {
await circuit.expectPass(
{ in: [[0, 0, 0], [0, 0, 0]] },
{ out: 1 }
);
});

it("witness: [[1,420,69],[1,420,69]]", async () => {
await circuit.expectPass(
{ in: [[1, 420, 69], [1, 420, 69]] },
{ out: 1 },
);
});

it("witness: [[0,0,0],[1,420,69]]", async () => {
await circuit.expectPass(
{ in: [[0, 0, 0], [1, 420, 69]] },
{ out: 0 },
);
});

it("witness: [[1,420,0],[1,420,69]]", async () => {
await circuit.expectPass(
{ in: [[1, 420, 0], [1, 420, 69]] },
{ out: 0 },
);
});

it("witness: [[1,0,69],[1,420,69]]", async () => {
await circuit.expectPass(
{ in: [[1, 0, 69], [1, 420, 69]] },
{ out: 0 },
);
});

it("witness: [[0,420,69],[1,420,69]]", async () => {
await circuit.expectPass(
{ in: [[0, 420, 69], [1, 420, 69]] },
{ out: 0 },
);
});
});

describe("Contains", () => {
let circuit: WitnessTester<["in", "array"], ["out"]>;
before(async () => {
circuit = await circomkit.WitnessTester(`Contains`, {
file: "circuits/utils/array",
template: "Contains",
params: [3],
});
console.log("#constraints:", await circuit.getConstraintCount());
});

it("witness: in = 0, array = [0,1,2]", async () => {
await circuit.expectPass(
{ in: 0, array: [0, 1, 2] },
{ out: 1 }
);
});

it("witness: in = 1, array = [0,1,2]", async () => {
await circuit.expectPass(
{ in: 1, array: [0, 1, 2] },
{ out: 1 }
);
});

it("witness: in = 2, array = [0,1,2]", async () => {
await circuit.expectPass(
{ in: 2, array: [0, 1, 2] },
{ out: 1 }
);
});

it("witness: in = 42069, array = [0,1,2]", async () => {
await circuit.expectPass(
{ in: 42069, array: [0, 1, 2] },
{ out: 0 }
);
});

});

describe("ArrayAdd", () => {
let circuit: WitnessTester<["lhs", "rhs"], ["out"]>;
before(async () => {
circuit = await circomkit.WitnessTester(`ArrayAdd`, {
file: "circuits/utils/array",
template: "ArrayAdd",
params: [3],
});
console.log("#constraints:", await circuit.getConstraintCount());
});

it("witness: lhs = [0,1,2], rhs = [3,5,7]", async () => {
await circuit.expectPass(
{ lhs: [0, 1, 2], rhs: [3, 5, 7] },
{ out: [3, 6, 9] }
);
});

});

describe("ArrayMul", () => {
let circuit: WitnessTester<["lhs", "rhs"], ["out"]>;
before(async () => {
circuit = await circomkit.WitnessTester(`ArrayMul`, {
file: "circuits/utils/array",
template: "ArrayMul",
params: [3],
});
console.log("#constraints:", await circuit.getConstraintCount());
});

it("witness: lhs = [0,1,2], rhs = [3,5,7]", async () => {
await circuit.expectPass(
{ lhs: [0, 1, 2], rhs: [3, 5, 7] },
{ out: [0, 5, 14] }
);
});

});

describe("GenericArrayAdd", () => {
let circuit: WitnessTester<["arrays"], ["out"]>;
before(async () => {
circuit = await circomkit.WitnessTester(`ArrayAdd`, {
file: "circuits/utils/array",
template: "GenericArrayAdd",
params: [3, 2],
});
console.log("#constraints:", await circuit.getConstraintCount());
});

it("witness: arrays = [[0,1,2],[3,5,7]]", async () => {
await circuit.expectPass(
{ arrays: [[0, 1, 2], [3, 5, 7]] },
{ out: [3, 6, 9] }
);
});

});
25 changes: 25 additions & 0 deletions circuits/test/utils/bytes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { circomkit, WitnessTester } from "../common";

describe("ASCII", () => {
let circuit: WitnessTester<["in"], ["out"]>;
before(async () => {
circuit = await circomkit.WitnessTester(`ASCII`, {
file: "circuits/utils/bytes",
template: "ASCII",
params: [13],
});
console.log("#constraints:", await circuit.getConstraintCount());
});

it("(valid) witness: in = b\"Hello, world!\"", async () => {
await circuit.expectPass(
{ in: [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33] },
);
});

it("(invalid) witness: in = [256, ...]", async () => {
await circuit.expectFail(
{ in: [256, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33] }
);
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { circomkit, WitnessTester } from "./common";
import { PoseidonModular } from "./common/poseidon";
import { circomkit, WitnessTester } from "../common";
import { PoseidonModular } from "../common/poseidon";

describe("hash", () => {
describe("PoseidonModular_16", () => {
Expand Down
Loading

0 comments on commit 4ac6f77

Please sign in to comment.