From 9856d30741820aefe9daec30461909469193e193 Mon Sep 17 00:00:00 2001 From: leo1987820 Date: Sat, 27 Jun 2026 00:37:48 +0800 Subject: [PATCH] fix reveal substring LessThan bit widths --- .../circuits/helpers/reveal-substring.circom | 8 +++---- .../tests/reveal-substring-bit-width.test.ts | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 packages/circuits/tests/reveal-substring-bit-width.test.ts diff --git a/packages/circuits/helpers/reveal-substring.circom b/packages/circuits/helpers/reveal-substring.circom index f3197e71..f6fd9d8c 100644 --- a/packages/circuits/helpers/reveal-substring.circom +++ b/packages/circuits/helpers/reveal-substring.circom @@ -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 @@ -47,4 +47,4 @@ template RevealSubstring(maxLength, maxSubstringLength, shouldCheckUniqueness) { } substring <== selectSubArray.out; -} \ No newline at end of file +} diff --git a/packages/circuits/tests/reveal-substring-bit-width.test.ts b/packages/circuits/tests/reveal-substring-bit-width.test.ts new file mode 100644 index 00000000..30a38bac --- /dev/null +++ b/packages/circuits/tests/reveal-substring-bit-width.test.ts @@ -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])", + ); + }); +});