Skip to content

Commit

Permalink
circuit: add range check for usernameIndex and fromIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
saleel committed Sep 8, 2024
1 parent d52b5a0 commit dd039d9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/circuits/src/twitter.circom
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ template TwitterVerifier(maxHeadersLength, maxBodyLength, n, k, exposeFrom) {
if (exposeFrom) {
signal input fromEmailIndex;

// Assert fromEmailIndex < emailHeaderLength
signal isFromIndexValid <== LessThan(log2Ceil(maxHeadersLength))([fromEmailIndex, emailHeaderLength]);
isFromIndexValid === 1;

signal (fromEmailFound, fromEmailReveal[maxHeadersLength]) <== FromAddrRegex(maxHeadersLength)(emailHeader);
fromEmailFound === 1;

Expand All @@ -75,6 +79,10 @@ template TwitterVerifier(maxHeadersLength, maxBodyLength, n, k, exposeFrom) {
signal (twitterFound, twitterReveal[maxBodyLength]) <== TwitterResetRegex(maxBodyLength)(emailBody);
twitterFound === 1;

// Assert twitterUsernameIndex < emailBodyLength
signal isTwitterIndexValid <== LessThan(log2Ceil(maxBodyLength))([twitterUsernameIndex, emailBodyLength]);
isTwitterIndexValid === 1;

// Pack the username to int
var maxTwitterUsernameLength = 21;
signal twitterUsernamePacks[1] <== PackRegexReveal(maxBodyLength, maxTwitterUsernameLength)(twitterReveal, twitterUsernameIndex);
Expand Down
15 changes: 15 additions & 0 deletions packages/circuits/tests/twitter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ describe("Twitter email test", function () {
twitterVerifierInputs.twitterUsernameIndex = (Number((await twitterVerifierInputs).twitterUsernameIndex) + 1).toString();

expect.assertions(1);

try {
const witness = await circuit.calculateWitness(twitterVerifierInputs);
await circuit.checkConstraints(witness);
} catch (error) {
expect((error as Error).message).toMatch("Assert Failed");
}
})

it("should fail if the twitterUsernameIndex is out of bounds", async function () {
const twitterVerifierInputs = await generateTwitterVerifierCircuitInputs(rawEmail, ethAddress);
twitterVerifierInputs.twitterUsernameIndex = (twitterVerifierInputs.emailBodyLength! + 1).toString();

expect.assertions(1);

try {
const witness = await circuit.calculateWitness(twitterVerifierInputs);
await circuit.checkConstraints(witness);
Expand Down

0 comments on commit dd039d9

Please sign in to comment.