From 21e1e47a19a74deddd6d8b0e24f3f0e07d593a23 Mon Sep 17 00:00:00 2001 From: shreyas-londhe Date: Sat, 14 Sep 2024 23:49:35 +0530 Subject: [PATCH] fix: made uniqueness check optional for RevealSubstring --- .../circuits/helpers/reveal-substring.circom | 14 ++++---- .../circuits/tests/reveal-substring.test.ts | 33 ------------------- .../reveal-substring-test.circom | 2 +- 3 files changed, 9 insertions(+), 40 deletions(-) diff --git a/packages/circuits/helpers/reveal-substring.circom b/packages/circuits/helpers/reveal-substring.circom index 064a023f..e585d1d1 100644 --- a/packages/circuits/helpers/reveal-substring.circom +++ b/packages/circuits/helpers/reveal-substring.circom @@ -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]; @@ -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; } \ No newline at end of file diff --git a/packages/circuits/tests/reveal-substring.test.ts b/packages/circuits/tests/reveal-substring.test.ts index 6ce639b9..ea035184 100644 --- a/packages/circuits/tests/reveal-substring.test.ts +++ b/packages/circuits/tests/reveal-substring.test.ts @@ -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: [ diff --git a/packages/circuits/tests/test-circuits/reveal-substring-test.circom b/packages/circuits/tests/test-circuits/reveal-substring-test.circom index 940cc983..92025107 100644 --- a/packages/circuits/tests/test-circuits/reveal-substring-test.circom +++ b/packages/circuits/tests/test-circuits/reveal-substring-test.circom @@ -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);