Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add range check for usernameIndex and fromIndex #33

Merged
merged 1 commit into from
Sep 9, 2024
Merged
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: 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
Loading