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

refactor: export sha_256_pad_single #90

Merged
merged 6 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion plonky2x/src/frontend/ecc/ed25519/gadgets/eddsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::fmt::Debug;
use curta::chip::ec::edwards::ed25519::Ed25519 as CurtaEd25519;
use curta::chip::ec::edwards::scalar_mul::generator::ScalarMulEd25519Gadget;
use curta::chip::ec::edwards::EdwardsParameters;
use curta::math::extension::CubicParameters;
use curta::math::extension::cubic::parameters::CubicParameters;
use plonky2::field::extension::Extendable;
use plonky2::hash::hash_types::RichField;
use plonky2::iop::target::{BoolTarget, Target};
Expand Down
22 changes: 9 additions & 13 deletions plonky2x/src/frontend/hash/sha/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ pub fn sha256_variable_length_single_chunk<F: RichField + Extendable<D>, const D
}

// Pad a variable length, single SHA256 chunk from a message
fn pad_single_sha256_chunk<F: RichField + Extendable<D>, const D: usize>(
pub fn pad_single_sha256_chunk<F: RichField + Extendable<D>, const D: usize>(
builder: &mut CircuitBuilder<F, D>,
message: &[BoolTarget],
// Length in bits (assumes less than SINGLE_CHUNK_MAX_MESSAGE_BYTES * 8)
length: Target,
) -> Vec<BoolTarget> {
) -> [BoolTarget; CHUNK_64_BYTES * 8] {
assert!(message.len() <= SINGLE_CHUNK_MAX_MESSAGE_BYTES * 8);
// 1) Adds all message bits before idx = length
// 2) Adds padding bit when idx = length
Expand Down Expand Up @@ -127,7 +127,11 @@ fn pad_single_sha256_chunk<F: RichField + Extendable<D>, const D: usize>(
msg_input.push(length_bits[i]);
}

msg_input
let mut padded_msg = [builder._false(); CHUNK_64_BYTES * 8];

padded_msg[..(CHUNK_64_BYTES * 8)].copy_from_slice(&msg_input[..(CHUNK_64_BYTES * 8)]);

padded_msg
}

// Process SHA256 on padded chunks
Expand Down Expand Up @@ -497,18 +501,10 @@ mod tests {

let msg_hash = sha256_variable_length_single_chunk(&mut builder, &targets, length);

for i in 0..digest_bits.len() {
if digest_bits[i] {
builder.assert_one(msg_hash[i].target);
} else {
builder.assert_zero(msg_hash[i].target);
}
}

let mut pw = PartialWitness::new();

for i in 0..msg_bits.len() {
pw.set_bool_target(targets[i], msg_bits[i]);
for i in 0..msg_hash.len() {
pw.set_bool_target(msg_hash[i], digest_bits[i]);
}

dbg!(builder.num_gates());
Expand Down