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

Express Maj with 2 XORs instead of 5 #1299

Merged
merged 5 commits into from
Dec 15, 2023
Merged

Conversation

mitschabaude
Copy link
Member

@mitschabaude mitschabaude commented Dec 6, 2023

constraints for 3 hashes: 18.8k -> 15.8k

speeds up Maj with the formula:

Maj(x, y, z) = (x & y) ^ (x & z) ^ (y & z) = 1/2 * (x + y + z - (x ^ y ^ z))

the first version is what we originally used, but needs 5 XORs, the second/new version only needs 2 XORs and also fewer generic gates.

also, speeds up Ch thanks to:

Ch(x, y, z) = (x & y) ^ (~x & z) = (x & y) + (~x & z)

the insight here was that we can use the second formula (+ instead of XOR) because the terms have no overlapping 1 bits.

third speed-up comes from using faster range checks for the quotient in divMod32 when used for addition mod 32. the existing 32 bit check (which uses 2.5 rows) is a good default because it supports all inputs up to 64 bits, for example resulting from 32x32 bit multiplication. But addition can only overflow by 1 bit, so we can use the boolean check which only uses 0.5 rows. For longer sums we can use the 16-bit check which uses 1 row.

in general, it seems reasonable to allow specifying the quotient bits as an extra argument in divMod32

@mitschabaude mitschabaude requested a review from a team as a code owner December 6, 2023 08:25
@mitschabaude mitschabaude changed the title Express Maj with 3 XORs instead of 5 Express Maj with 2 XORs instead of 5 Dec 6, 2023
Base automatically changed from perf/sha2-multi-rot to dog-food-sha256 December 6, 2023 19:04
@Trivo25 Trivo25 merged commit 7d0bbea into dog-food-sha256 Dec 15, 2023
10 checks passed
@Trivo25 Trivo25 deleted the perf/sha2-maj-trick branch December 15, 2023 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants