Skip to content

Commit

Permalink
feat: added RevealSubstring template
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyas-londhe committed Sep 13, 2024
1 parent 1f02068 commit 6200617
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 134 deletions.
48 changes: 48 additions & 0 deletions packages/circuits/helpers/reveal-substring.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
pragma circom 2.1.6;

include "circomlib/circuits/comparators.circom";
include "../utils/array.circom";

/// @title RevealSubstring
/// @notice This circuit reveals a substring from an input array and verifies its uniqueness
/// @dev Ensures the revealed substring occurs exactly once in the input
/// @param maxLength The maximum length of the input array
/// @param maxSubstringLength The maximum length of the substring to be revealed
template RevealSubstring(maxLength, maxSubstringLength) {
assert(maxSubstringLength < maxLength);

signal input in[maxLength];
signal input substringStartIndex;
signal input substringLength;

signal output substring[maxSubstringLength];

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

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

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

// Extract the substring
component selectSubArray = SelectSubArray(maxLength, maxSubstringLength);
selectSubArray.in <== in;
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;

substring <== selectSubArray.out;
}
134 changes: 0 additions & 134 deletions packages/circuits/helpers/substring-match.circom

This file was deleted.

0 comments on commit 6200617

Please sign in to comment.