Skip to content

Commit

Permalink
feat: added range checks to RevealSubstring template
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyas-londhe committed Sep 26, 2024
1 parent 21e1e47 commit 800b9a1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/circuits/helpers/reveal-substring.circom
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ template RevealSubstring(maxLength, maxSubstringLength, shouldCheckUniqueness) {

signal output substring[maxSubstringLength];

// Substring start index should be less than maxLength
signal isSubstringStartIndexValid <== LessThan(log2Ceil(maxLength))([substringStartIndex, maxLength]);
isSubstringStartIndexValid === 1;

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

// substring index + substring length should be less than maxLength + 1
signal sum <== substringStartIndex + substringLength;
signal isSumValid <== LessThan(log2Ceil(maxLength + 1))([sum, maxLength + 1]);
isSumValid === 1;

// Extract the substring
component selectSubArray = SelectSubArray(maxLength, maxSubstringLength);
selectSubArray.in <== in;
Expand Down
45 changes: 45 additions & 0 deletions packages/circuits/tests/reveal-substring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,49 @@ describe("RevealSubstring Circuit", () => {
...Array(13).fill(0n),
]);
});

it("should fail when substringStartIndex is equal to maxLength", async () => {
const input = {
in: Array(256).fill(1),
substringStartIndex: 256, // Equal to maxLength
substringLength: 1,
};
await expect(circuit.calculateWitness(input)).rejects.toThrow();
});

it("should fail when substringStartIndex is greater than maxLength", async () => {
const input = {
in: Array(256).fill(1),
substringStartIndex: 257, // Greater than maxLength
substringLength: 1,
};
await expect(circuit.calculateWitness(input)).rejects.toThrow();
});

it("should fail when substringLength is equal to maxSubstringLength", async () => {
const input = {
in: Array(256).fill(1),
substringStartIndex: 0,
substringLength: 16, // Equal to maxSubstringLength
};
await expect(circuit.calculateWitness(input)).rejects.toThrow();
});

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

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

0 comments on commit 800b9a1

Please sign in to comment.