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: optimize constraints in sha256 #6145

Merged
merged 4 commits into from
Sep 24, 2024
Merged
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
15 changes: 7 additions & 8 deletions noir_stdlib/src/hash/sha256.nr
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,18 @@ pub fn sha256_var<let N: u32>(msg: [u8; N], message_size: u64) -> [u8; 32] {

if !crate::runtime::is_unconstrained() {
for i in 0..56 {
if i < msg_byte_ptr {
assert_eq(msg_block[i], last_block[i]);
} else {
assert_eq(msg_block[i], zero);
}
let predicate = (i < msg_byte_ptr) as u8;
let expected_byte = predicate * last_block[i];
assert_eq(msg_block[i], expected_byte);
}

// We verify the message length was inserted correctly by reversing the byte decomposition.
let len = 8 * message_size;
let len_bytes: [u8; 8] = (len as Field).to_be_bytes();
let mut reconstructed_len: Field = 0;
for i in 56..64 {
assert_eq(msg_block[i], len_bytes[i - 56]);
reconstructed_len = 256 * reconstructed_len + msg_block[i] as Field;
}
assert_eq(reconstructed_len, len as Field);
}

hash_final_block(msg_block, h)
Expand Down Expand Up @@ -254,4 +254,3 @@ fn hash_final_block(msg_block: [u8; 64], mut state: [u32; 8]) -> [u8; 32] {

out_h
}

Loading