Skip to content

Commit

Permalink
feat: computing r like fiat-shamir
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyas-londhe committed Aug 3, 2024
1 parent b9008c4 commit d4980c1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
17 changes: 15 additions & 2 deletions packages/circuits/helpers/substring-match.circom
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pragma circom 2.1.6;

include "circomlib/circuits/comparators.circom";
include "circomlib/circuits/mux1.circom";
include "../utils/hash.circom";

/// @title SubstringMatch
/// @notice This template verifies if a given substring exists within a larger string at a specified index
Expand All @@ -15,9 +16,21 @@ include "circomlib/circuits/mux1.circom";
/// @output isValid A signal that is 1 if the substring matches at the given index, 0 otherwise
template SubstringMatch(maxLength, maxSubstringLength) {
signal input in[maxLength];
signal input startIndex;
signal input revealedString[maxSubstringLength];
signal input r;
signal input startIndex;

// Derive r from the inputs
signal r;
component rHasher;
rHasher = PoseidonModular(maxLength + maxSubstringLength + 1);
rHasher.in[0] <== startIndex;
for (var i = 0; i < maxSubstringLength; i++) {
rHasher.in[i + 1] <== revealedString[i];
}
for (var i = 0; i < maxLength; i++) {
rHasher.in[i + maxSubstringLength + 1] <== in[i];
}
r <== rHasher.out;

// Check if each character in the revealed string is non-zero
signal isNonZero[maxSubstringLength];
Expand Down
7 changes: 1 addition & 6 deletions packages/circuits/tests/substring-match.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ describe('SubstringMatch', () => {
const input = {
in: padArray([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100], 32), // "hello world"
startIndex: 6,
revealedString: padArray([119, 111, 114, 108, 100], 16), // "world"
r: 69, // A prime number for the random linear combination
revealedString: padArray([119, 111, 114, 108, 100], 16), // "world"
};

const witness = await circuit.calculateWitness(input);
Expand All @@ -41,7 +40,6 @@ describe('SubstringMatch', () => {
in: padArray([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100], 32), // "hello world"
startIndex: 6,
revealedString: padArray([119, 111, 114, 108, 107], 16), // "worlk" (last character different)
r: 69,
};

const witness = await circuit.calculateWitness(input);
Expand All @@ -57,7 +55,6 @@ describe('SubstringMatch', () => {
in: padArray([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100], 32), // "hello world"
startIndex: 0,
revealedString: padArray([104, 101, 108, 108, 111], 16), // "hello"
r: 69,
};

const witness = await circuit.calculateWitness(input);
Expand All @@ -73,7 +70,6 @@ describe('SubstringMatch', () => {
in: padArray([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100], 32), // "hello world"
startIndex: 7,
revealedString: padArray([111, 114, 108, 100], 16), // "orld"
r: 69,
};

const witness = await circuit.calculateWitness(input);
Expand All @@ -89,7 +85,6 @@ describe('SubstringMatch', () => {
in: padArray([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100], 32), // "hello world"
startIndex: 32, // Out of bounds (valid indices are 0-31)
revealedString: padArray([100], 16), // "d"
r: 69,
};

const witness = await circuit.calculateWitness(input);
Expand Down

0 comments on commit d4980c1

Please sign in to comment.