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: Blake2s round precompile #176

Open
wants to merge 30 commits into
base: dev
Choose a base branch
from
Open

feat: Blake2s round precompile #176

wants to merge 30 commits into from

Conversation

storojs72
Copy link
Member

@storojs72 storojs72 commented Sep 23, 2024

This PR implements gadget and syscall for Blake2s round function.

The integrated code that allows using this syscall in external sphinx programs is located in zkvm branch of RustCrypto.

The following is a comparison of single Blake2s hashing inside Sphinx program without / with precompile:

summary: cycles=5989, e2e=2.196652208, khz=2726.42, proofSize=1789916 (without precompile)
summary: cycles=4401, e2e=2.104103333, khz=2091.63, proofSize=3188917 (with precompile)

TODO:

  • Explore if it makes sense to extend the precompile to round_x10 or any other possible imprevements from performance perspective.

Copy link
Member

@huitseeker huitseeker left a comment

Choose a reason for hiding this comment

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

This looks pretty good at first glance! I'll come back to this.

Cargo.toml Outdated
@@ -78,7 +78,7 @@ getrandom = "=0.2.14" # 0.2.15 depends on yanked libc 0.2.154
hashbrown = { version = "0.14.5", features = ["serde"] }
hex = "0.4.3"
home = "0.5.9"
hybrid-array = "0.2.0-rc"
hybrid-array = "0.2.0-rc.9"
Copy link
Member

Choose a reason for hiding this comment

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

the resolution to 0.2.0-rc.9 should be automatic without this change

Copy link
Member Author

@storojs72 storojs72 Sep 24, 2024

Choose a reason for hiding this comment

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

For reference: https://github.com/argumentcomputer/zk-light-clients/pull/241>

The hybrid-array dependency has been recently updated to 0.2.0-rc.10 version which breaks compiling the sphinx programs (also see this broken CI job). My attempt was to hardcode version to 0.2.0-rc.9 (which allows successful compiling of sphinx programs) - but it actually still doesn't help - we need either downgrade version manually:

cargo update hybrid-array@0.2.0-rc.10 --precise 0.2.0-rc.9

or, as @wwared suggested - reinstall the whole toolchain from scratch.

Anyway, you are right, we can leave it as is.

let record_clone = record.clone();
machine.debug_constraints(pk, record_clone);
}
//#[cfg(debug_assertions)]
Copy link
Member

Choose a reason for hiding this comment

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

Nit: remove before merge.
FYI, we have a cargo-profile called dev-ci, which will be as efficient to run as --release, but has debug assertions!

);
}

self.constrain_shuffled_indices(builder, &local.shuffled_indices, local.is_real);
Copy link
Member

Choose a reason for hiding this comment

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

It seems like local.shuffled_indices is only used here, and this is just enforcing that the first 4 elements are 0 while the rest are 1.

I believe you can remove the shuffled_indices column and its related constraints without any impact. The shuffling is still properly constrained by the constant values of the vX_shuffle_lookup vectors offsetting the memory reads.

Comment on lines 52 to 58
pub fn syscall_blake2s_add_2(left: *mut u32, right: *const u32);
pub fn syscall_blake2s_add_3(left: *mut u32, right: *const u32);
pub fn syscall_blake2s_xor_rotate_right_16(left: *mut u32, right: *const u32);
pub fn syscall_blake2s_xor_rotate_right_12(left: *mut u32, right: *const u32);
pub fn syscall_blake2s_xor_rotate_right_8(left: *mut u32, right: *const u32);
pub fn syscall_blake2s_xor_rotate_right_7(left: *mut u32, right: *const u32);
pub fn syscall_blake2s_quarter_round(left: *mut u32, right: *const u32);
Copy link
Member

Choose a reason for hiding this comment

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

I believe these can be removed since they are no longer being used/defined

Comment on lines 187 to 188
let blake_2s_round = Blake2sRoundChip::new();
chips.push(RiscvAir::Blake2sRound(blake_2s_round));
Copy link
Member

Choose a reason for hiding this comment

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

This should be moved up to after bls12381_g1_decompress above I believe

@storojs72 storojs72 force-pushed the artem/blake2s branch 2 times, most recently from 61fa060 to 4bcefbd Compare September 24, 2024 15:25
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