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

Optimize Regex Circuit Gen #25

Open
Divide-By-0 opened this issue May 9, 2023 · 1 comment
Open

Optimize Regex Circuit Gen #25

Divide-By-0 opened this issue May 9, 2023 · 1 comment
Labels
enhancement New feature or request help wanted Extra attention is needed medium

Comments

@Divide-By-0
Copy link
Member

Divide-By-0 commented May 9, 2023

Decrease repeated computation - 33% savings

Right now, checking if a specific character is in a range is repeated many times in the code. For instance, body hash check has

      lt[7][i] = LessThan(8);
      lt[7][i].in[0] <== in[i];
      lt[7][i].in[1] <== 58;

and

      lt[25][i] = LessThan(8);
      lt[25][i].in[0] <== in[i];
      lt[25][i].in[1] <== 58;

and 2 other occurrences of this check. Thus, if we just did it once and stored it in a shared register referred to by all of these checks, we could probably reduce the remaining lessThan checks by a factor of 4. This would be about 200K constraints, or about a 33% drop in regex.


This second suggestion I'm about to say does NOT work since circom considers (p/2, p) as negative numbers. It had intially proposed to decrease double less than checks for 16% savings.

Rn, to check if 47 < x < 52, we do two lessthan checks. We could instead do is x - 47 < 5, which is the same thing right, and we change 2b constraints to b + 1 constraints where b is the bit count (8 in this case). For instance, the body hash check has 13 less than pairs (26 total) for 1024 size header, so we'd save 13 * 7 * 1024 ~= 100K constraints roughly, which out of 600K is about 16%. However this will only work if you can change circoms presets to remove negative numbers.

@Divide-By-0 Divide-By-0 added enhancement New feature or request help wanted Extra attention is needed medium labels May 9, 2023
@Divide-By-0
Copy link
Member Author

This would also allow the option for forwards-backwards regexes to 2x regex cost at the tradeoff of increasing the # of regexes that are allowed, and keep circuits feasible.

@Divide-By-0 Divide-By-0 transferred this issue from zkemail/zk-email-verify Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed medium
Projects
Status: No status
Development

No branches or pull requests

1 participant