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

feat: Add sha512 precompiles #179

Merged
merged 10 commits into from
Sep 30, 2024
Merged

feat: Add sha512 precompiles #179

merged 10 commits into from
Sep 30, 2024

Conversation

wwared
Copy link
Contributor

@wwared wwared commented Sep 27, 2024

Companion PR: argumentcomputer/RustCrypto-hashes#1 and argumentcomputer/zk-light-clients#256

This PR adds two new syscalls, for SHA-512 extend and compress operations.

Due to the large amount of byte lookups performed per instruction, these instructions all perform a single iteration of the main loop of its corresponding iteration. This means the library code must call these in a loop to perform the entire operation (see the companion PR for how that is done).

Additionally, this PR adds the following types and operations geared towards 64-bit words:

  • Word64<T>, the 64-bit variant of Word<T>
  • Add64Operation, Xor64Operation, And64Operation, Not64Operation: trivial 64-bit variants of their 32-bit counterparts
  • FixedRotateRight64Operation and FixedShiftRight64Operation: the 64-bit counterpart to the 32-bit operations. Required minor changes compared to the 32-bit version

It should be possible to make the above types more generic in the future (for example, making Word<T> generic over the word width), but this would make the PR modify much more of the codebase than it does, just for minor type changes around these operations. Making new operations based on the existing ones is the simpler option that makes it clear where and how these operations are used.

These versions are essentially just copies of their 32-bit counterparts,
made to work with a Word64 type. It's not as nice as a fully generic
version
Also add Xor64 operation, fix typo in Add64, add simple test
adr1anh
adr1anh previously approved these changes Sep 30, 2024
Copy link
Contributor

@adr1anh adr1anh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few things that could be optimized, but that can be done at a later time. Nice work!

core/src/air/word_64.rs Outdated Show resolved Hide resolved
local.is_real,
);
let reduced_prev_i = local.i_mem.prev_value().reduce::<AB>();
builder
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to trust i since it's coming from the Rust code that calls compress

);

// Calculate temp1 := h + S1 + ch + k[i] + w[i].
Add64Operation::<AB::F>::eval(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just making a note, but the Add operation does not need to range check the inputs since we can assume they are bytes (either as an XOR result or because they come from the ZKVM)

);

// Calculate temp1 := h + S1 + ch + k[i] + w[i].
Add64Operation::<AB::F>::eval(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also use an Add4 gadget

Not very useful considering it will almost always overflow. Can be added
back if necessary.
@wwared wwared mentioned this pull request Sep 30, 2024
@wwared wwared marked this pull request as ready for review September 30, 2024 13:42
Copy link
Member

@storojs72 storojs72 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@wwared wwared merged commit 75d6e85 into dev Sep 30, 2024
9 checks passed
@wwared wwared deleted the sha512_gadgets branch September 30, 2024 16:20
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.

3 participants