Skip to content

Commit

Permalink
test: added RevealSubstring tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyas-londhe committed Sep 13, 2024
1 parent 6200617 commit 2219b57
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 104 deletions.
4 changes: 2 additions & 2 deletions packages/circuits/helpers/reveal-substring.circom
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ template RevealSubstring(maxLength, maxSubstringLength) {

// substringLength should be less than maxSubstringLength
signal lengthCheck;
lengthCheck <== LessThan(log2Ceil(maxSubstringLength))([substringLength, maxSubstringLength]);
lengthCheck <== LessThan(log2Ceil(maxSubstringLength))([substringLength, maxSubstringLength + 1]);
lengthCheck === 1;

// substringStartIndex + substringLength should be less than maxLength
signal startIndexPlusLengthCheck;
startIndexPlusLengthCheck <== LessThan(log2Ceil(maxLength))([substringStartIndex + substringLength, maxLength]);
startIndexPlusLengthCheck <== LessThan(log2Ceil(maxLength))([substringStartIndex + substringLength, maxLength + 1]);
startIndexPlusLengthCheck === 1;

// Extract the substring
Expand Down
179 changes: 179 additions & 0 deletions packages/circuits/tests/reveal-substring.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import { wasm as wasm_tester } from "circom_tester";
import path from "path";

describe("RevealSubstring Circuit", () => {
let circuit: any;

beforeAll(async () => {
circuit = await wasm_tester(
path.join(
__dirname,
"./test-circuits/reveal-substring-test.circom"
),
{
recompile: true,
include: path.join(__dirname, "../../../node_modules"),
output: path.join(__dirname, "./compiled-test-circuits"),
}
);
});

it("should correctly reveal a substring in the middle of the input", async () => {
const input = {
in: [
...Array(100)
.fill(0)
.map((_, i) => (i % 255) + 1),
...Array(156).fill(0),
],
substringStartIndex: 50,
substringLength: 5,
};
const witness = await circuit.calculateWitness(input);
await circuit.checkConstraints(witness);
expect(witness.slice(1, 17)).toEqual([
51n,
52n,
53n,
54n,
55n,
...Array(11).fill(0n),
]);
});

it("should correctly reveal a substring at the start of the input", async () => {
const input = {
in: [
...Array(100)
.fill(0)
.map((_, i) => (i % 255) + 1),
...Array(156).fill(0),
],
substringStartIndex: 0,
substringLength: 5,
};
const witness = await circuit.calculateWitness(input);
await circuit.checkConstraints(witness);
expect(witness.slice(1, 17)).toEqual([
1n,
2n,
3n,
4n,
5n,
...Array(11).fill(0n),
]);
});

it("should correctly reveal a substring at the end of the non-zero input", async () => {
const input = {
in: [
...Array(100)
.fill(0)
.map((_, i) => (i % 255) + 1),
...Array(156).fill(0),
],
substringStartIndex: 95,
substringLength: 5,
};
const witness = await circuit.calculateWitness(input);
await circuit.checkConstraints(witness);
expect(witness.slice(1, 17)).toEqual([
96n,
97n,
98n,
99n,
100n,
...Array(11).fill(0n),
]);
});

it("should fail when substringStartIndex is out of bounds", async () => {
const input = {
in: Array(256).fill(1),
substringStartIndex: 256,
substringLength: 5,
};
await expect(circuit.calculateWitness(input)).rejects.toThrow(
"Assert Failed"
);
});

it("should fail when substringLength is greater than maxSubstringLength", async () => {
const input = {
in: Array(256).fill(1),
substringStartIndex: 0,
substringLength: 17,
};
await expect(circuit.calculateWitness(input)).rejects.toThrow(
"Assert Failed"
);
});

it("should fail when substringStartIndex + substringLength exceeds maxLength", async () => {
const input = {
in: Array(256).fill(1),
substringStartIndex: 250,
substringLength: 7,
};
await expect(circuit.calculateWitness(input)).rejects.toThrow(
"Assert Failed"
);
});

it("should correctly reveal a substring of length 1", async () => {
const input = {
in: [
...Array(100)
.fill(0)
.map((_, i) => (i % 255) + 1),
...Array(156).fill(0),
],
substringStartIndex: 50,
substringLength: 1,
};
const witness = await circuit.calculateWitness(input);
await circuit.checkConstraints(witness);
expect(witness.slice(1, 17)).toEqual([51n, ...Array(15).fill(0n)]);
});

it("should correctly reveal the maximum length substring", async () => {
const input = {
in: [
...Array(100)
.fill(0)
.map((_, i) => (i % 255) + 1),
...Array(156).fill(0),
],
substringStartIndex: 0,
substringLength: 16,
};
const witness = await circuit.calculateWitness(input);
await circuit.checkConstraints(witness);
expect(witness.slice(1, 17)).toEqual(
Array(16)
.fill(0)
.map((_, i) => BigInt(i + 1))
);
});

it("should pad with zeros when substringLength is less than maxSubstringLength", async () => {
const input = {
in: [
...Array(100)
.fill(0)
.map((_, i) => (i % 255) + 1),
...Array(156).fill(0),
],
substringStartIndex: 0,
substringLength: 3,
};
const witness = await circuit.calculateWitness(input);
await circuit.checkConstraints(witness);
expect(witness.slice(1, 17)).toEqual([
1n,
2n,
3n,
...Array(13).fill(0n),
]);
});
});
97 changes: 0 additions & 97 deletions packages/circuits/tests/substring-match.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma circom 2.1.6;

include "../../helpers/reveal-substring.circom";

component main = RevealSubstring(256, 16);

This file was deleted.

0 comments on commit 2219b57

Please sign in to comment.