Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/circuits/helpers/reveal-substring.circom
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ template RevealSubstring(maxLength, maxSubstringLength, shouldCheckUniqueness) {
signal output substring[maxSubstringLength];

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

// Substring length should be less than maxSubstringLength + 1
signal isSubstringLengthValid <== LessThan(log2Ceil(maxSubstringLength + 1))([substringLength, maxSubstringLength + 1]);
signal isSubstringLengthValid <== LessThan(log2Ceil(maxSubstringLength + 2))([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]);
signal isSumValid <== LessThan(log2Ceil(maxLength + 2))([sum, maxLength + 1]);
isSumValid === 1;

// Extract the substring
Expand All @@ -47,4 +47,4 @@ template RevealSubstring(maxLength, maxSubstringLength, shouldCheckUniqueness) {
}

substring <== selectSubArray.out;
}
}
22 changes: 22 additions & 0 deletions packages/circuits/tests/reveal-substring-bit-width.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import fs from "fs";
import path from "path";

describe("RevealSubstring LessThan bit widths", () => {
it("allocates one extra value for exclusive upper-bound constants", () => {
const source = fs.readFileSync(
path.join(__dirname, "../helpers/reveal-substring.circom"),
"utf8",
);
const compactSource = source.replace(/\s+/g, "");

expect(compactSource).toContain(
"LessThan(log2Ceil(maxLength+1))([substringStartIndex,maxLength])",
);
expect(compactSource).toContain(
"LessThan(log2Ceil(maxSubstringLength+2))([substringLength,maxSubstringLength+1])",
);
expect(compactSource).toContain(
"LessThan(log2Ceil(maxLength+2))([sum,maxLength+1])",
);
});
});