Skip to content

Commit

Permalink
circuit: use SubArraySelector in ByteSubArrayToInts
Browse files Browse the repository at this point in the history
  • Loading branch information
saleel committed Apr 1, 2024
1 parent 66bf305 commit b1d2052
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
28 changes: 11 additions & 17 deletions packages/circuits/utils/bytes.circom
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ template BytesToInts(maxBytes) {

/// @title ByteSubArrayToInts
/// @notice Select sub array from a byte array and pack to a 31-byte integer array
/// @notice This is not used in the main circuits
/// @param maxBytes: the maximum number of bytes in the input array
/// @notice This is not used in ZK-Email circuits anywhere
/// @param maxArrayLen: the maximum number of elements in the input array
/// @param maxSubArrayLen: the maximum number of elements in the sub array
/// @input in: the input byte array
/// @input startIndex: the start index of the sub array
/// @input length: the length of the sub array
/// @output out: the output integer array
template ByteSubArrayToInts(maxArrayLen, maxSubArrayLen) {
assert(maxSubArrayLen < maxArrayLen);
Expand All @@ -76,24 +79,15 @@ template ByteSubArrayToInts(maxArrayLen, maxSubArrayLen) {

signal output out[chunkLength];

assert(length < maxSubArrayLen);
assert(length <= maxSubArrayLen);

component shifter = ArrayShiftLeft(maxArrayLen, maxSubArrayLen);
shifter.in <== in;
shifter.shift <== startIndex;
component subarraySelector = SubArraySelector(maxArrayLen, maxSubArrayLen);
subarraySelector.in <== in;
subarraySelector.startIndex <== startIndex;
subarraySelector.length <== length;

component packer = BytesToInts(maxSubArrayLen);

// Set value after length to zero
component gts[maxSubArrayLen];
for (var i = 0; i < maxSubArrayLen; i++) {
gts[i] = GreaterThan(log2Ceil(maxSubArrayLen));
gts[i].in[0] <== length;
gts[i].in[1] <== i;

packer.in[i] <== gts[i].out * shifter.out[i];
}

packer.in <== subarraySelector.out;

out <== packer.out;
}
Expand Down
9 changes: 6 additions & 3 deletions packages/circuits/utils/extract.circom
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ include "./bytes.circom";

/// @title ExtractRegexReveal
/// @notice Extracts reveal part from a regex match
/// @notice Verifies data before and after (maxRevealLen) reveal part is zero
/// @param maxArrayLen Maximum length of the input array
/// @param maxRevealLen Maximum length of the reveal part
/// @input in Input array
/// @input startIndex Index of the start of the reveal part
/// @output out Revealed data array
template ExtractRegexReveal(maxArrayLen, maxRevealLen) {
var bitLength = log2Ceil(maxArrayLen);

signal input in[maxArrayLen];
signal input startIndex;

signal output out[maxRevealLen];


var bitLength = log2Ceil(maxArrayLen);

signal isStartIndex[maxArrayLen];
signal isZero[maxArrayLen];
signal isPreviousZero[maxArrayLen];
Expand Down Expand Up @@ -68,6 +69,8 @@ template PackRegexReveal(maxArrayLen, maxRevealLen) {
extractor.in <== in;
extractor.startIndex <== startIndex;

// Items after reveal part and before maxRevealLen are already asserted to zero
// So we can safely pack without an additional `length` input
component packer = BytesToInts(maxRevealLen);
packer.in <== extractor.out;
out <== packer.out;
Expand Down

0 comments on commit b1d2052

Please sign in to comment.