Skip to content

Commit

Permalink
fix: made uniqueness check optional for RevealSubstring
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyas-londhe committed Sep 14, 2024
1 parent 1b8a128 commit 21e1e47
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 40 deletions.
14 changes: 8 additions & 6 deletions packages/circuits/helpers/reveal-substring.circom
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ include "../utils/array.circom";
/// (e.g., checking that substringStartIndex and substringLength are within valid ranges)
/// @param maxLength The maximum length of the input array
/// @param maxSubstringLength The maximum length of the substring to be revealed
template RevealSubstring(maxLength, maxSubstringLength) {
template RevealSubstring(maxLength, maxSubstringLength, shouldCheckUniqueness) {
assert(maxSubstringLength < maxLength);

signal input in[maxLength];
Expand All @@ -25,11 +25,13 @@ template RevealSubstring(maxLength, maxSubstringLength) {
selectSubArray.startIndex <== substringStartIndex;
selectSubArray.length <== substringLength;

// Check if the substring occurs exactly once in the input
component countSubstringOccurrences = CountSubstringOccurrences(maxLength, maxSubstringLength);
countSubstringOccurrences.in <== in;
countSubstringOccurrences.substring <== selectSubArray.out;
countSubstringOccurrences.count === 1;
if (shouldCheckUniqueness) {
// Check if the substring occurs exactly once in the input
component countSubstringOccurrences = CountSubstringOccurrences(maxLength, maxSubstringLength);
countSubstringOccurrences.in <== in;
countSubstringOccurrences.substring <== selectSubArray.out;
countSubstringOccurrences.count === 1;
}

substring <== selectSubArray.out;
}
33 changes: 0 additions & 33 deletions packages/circuits/tests/reveal-substring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,39 +87,6 @@ describe("RevealSubstring Circuit", () => {
]);
});

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: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pragma circom 2.1.6;

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

component main = RevealSubstring(256, 16);
component main = RevealSubstring(256, 16, 1);

0 comments on commit 21e1e47

Please sign in to comment.