From e70ff51c645584379510f8054e7fae23ba843f0d Mon Sep 17 00:00:00 2001 From: wwared <541936+wwared@users.noreply.github.com> Date: Mon, 30 Sep 2024 10:42:07 -0300 Subject: [PATCH] chore: Remove `FIXME`s, add utils/uint.rs --- .../precompiles/sha512/compress/execute.rs | 29 ++++--------------- .../precompiles/sha512/compress/mod.rs | 13 ++------- .../precompiles/sha512/compress/trace.rs | 13 ++------- .../precompiles/sha512/extend/execute.rs | 20 ++++--------- .../precompiles/sha512/extend/trace.rs | 23 ++++----------- core/src/utils/mod.rs | 2 ++ core/src/utils/uint.rs | 21 ++++++++++++++ 7 files changed, 43 insertions(+), 78 deletions(-) create mode 100644 core/src/utils/uint.rs diff --git a/core/src/syscall/precompiles/sha512/compress/execute.rs b/core/src/syscall/precompiles/sha512/compress/execute.rs index 4fdb95c22..235c42326 100644 --- a/core/src/syscall/precompiles/sha512/compress/execute.rs +++ b/core/src/syscall/precompiles/sha512/compress/execute.rs @@ -5,6 +5,7 @@ use crate::{ sha512::{Sha512CompressEvent, SHA512_COMPRESS_K}, SyscallContext, }, + utils::{u32_pair_to_u64, u64_to_le_u32s}, }; impl Syscall for Sha512CompressChip { @@ -20,22 +21,11 @@ impl Syscall for Sha512CompressChip { let start_clk = rt.clk; let mut h_write_records = Vec::new(); - // FIXME - fn u32_vec_to_u64(val: Vec) -> u64 { - u64::from_le_bytes( - val.into_iter() - .flat_map(|x| x.to_le_bytes()) - .collect::>() - .try_into() - .unwrap(), - ) - } - // Execute the "initialize" phase where we read in the h values. let mut hx = [0u64; 8]; for j in 0..8 { let values = rt.slice_unsafe(h_ptr + j * 8, 2); - hx[j as usize] = u32_vec_to_u64(values); + hx[j as usize] = u32_pair_to_u64(values[0], values[1]); } // The `i` index is at the end of the `h_ptr` state @@ -44,11 +34,11 @@ impl Syscall for Sha512CompressChip { // The constants `k` are copied by the guest to the end of the state pointer let (k_i_read_records, k_i) = rt.mr_slice(h_ptr + (9 * 8) + i * 8, 2); - let k_i = u32_vec_to_u64(k_i); + let k_i = u32_pair_to_u64(k_i[0], k_i[1]); assert_eq!(k_i, SHA512_COMPRESS_K[i as usize]); let (w_i_read_records, w_i) = rt.mr_slice(w_ptr + i * 8, 2); - let w_i = u32_vec_to_u64(w_i); + let w_i = u32_pair_to_u64(w_i[0], w_i[1]); // Execute the "compress" iteration. let mut a = hx[0]; @@ -80,18 +70,9 @@ impl Syscall for Sha512CompressChip { b = a; a = temp1.wrapping_add(temp2); - // FIXME - fn u64_to_u32x2(n: u64) -> [u32; 2] { - let n = n.to_le_bytes(); - [ - u32::from_le_bytes(n[..4].try_into().unwrap()), - u32::from_le_bytes(n[4..].try_into().unwrap()), - ] - } - // Execute the "finalize" phase of updating the memory. let v = [a, b, c, d, e, f, g, h]; - let v: Vec = v.into_iter().flat_map(u64_to_u32x2).collect(); + let v: Vec = v.into_iter().flat_map(u64_to_le_u32s).collect(); for i in 0..16 { let record = rt.mw(h_ptr + i as u32 * 4, v[i]); h_write_records.push(record); diff --git a/core/src/syscall/precompiles/sha512/compress/mod.rs b/core/src/syscall/precompiles/sha512/compress/mod.rs index ac300aaec..83e0e6784 100644 --- a/core/src/syscall/precompiles/sha512/compress/mod.rs +++ b/core/src/syscall/precompiles/sha512/compress/mod.rs @@ -132,20 +132,11 @@ pub mod compress_tests { use crate::{ runtime::{Instruction, Opcode, Program, SyscallCode}, - utils::{run_test, setup_logger, tests::SHA512_COMPRESS_ELF}, + utils::{run_test, setup_logger, tests::SHA512_COMPRESS_ELF, u64_to_le_u32s}, }; use super::SHA512_COMPRESS_K; - // FIXME - fn u64_to_u32x2(n: u64) -> [u32; 2] { - let n = n.to_le_bytes(); - [ - u32::from_le_bytes(n[..4].try_into().unwrap()), - u32::from_le_bytes(n[4..].try_into().unwrap()), - ] - } - pub fn sha512_compress_program() -> Program { let w_ptr = 100; let h_ptr = 100000; @@ -172,7 +163,7 @@ pub mod compress_tests { } // Fill out the constants `k` for i in 0..80 { - let k_i = u64_to_u32x2(SHA512_COMPRESS_K[i]); + let k_i = u64_to_le_u32s(SHA512_COMPRESS_K[i]); instructions.extend(vec![ Instruction::new(Opcode::ADD, 29, 0, k_i[0], false, true), Instruction::new(Opcode::ADD, 28, 0, k_i[1], false, true), diff --git a/core/src/syscall/precompiles/sha512/compress/trace.rs b/core/src/syscall/precompiles/sha512/compress/trace.rs index 1be305d75..46066bac5 100644 --- a/core/src/syscall/precompiles/sha512/compress/trace.rs +++ b/core/src/syscall/precompiles/sha512/compress/trace.rs @@ -12,22 +12,13 @@ use crate::{ air::{EventLens, MachineAir, WithEvents}, bytes::{event::ByteRecord, ByteLookupEvent, ByteOpcode}, runtime::{ExecutionRecord, Program}, - utils::pad_rows, + utils::{pad_rows, u64_to_le_u32s}, }; impl<'a> WithEvents<'a> for Sha512CompressChip { type Events = &'a [Sha512CompressEvent]; } -// FIXME -fn u64_to_u32x2(n: u64) -> [u32; 2] { - let n = n.to_le_bytes(); - [ - u32::from_le_bytes(n[..4].try_into().unwrap()), - u32::from_le_bytes(n[4..].try_into().unwrap()), - ] -} - impl MachineAir for Sha512CompressChip { type Record = ExecutionRecord; @@ -173,7 +164,7 @@ impl MachineAir for Sha512CompressChip { event.h_write_records[2 * j + 1], &mut new_byte_lookup_events, ); - let out = u64_to_u32x2(out_h[j]); + let out = u64_to_le_u32s(out_h[j]); assert_eq!(event.h_write_records[2 * j].value, out[0]); assert_eq!(event.h_write_records[2 * j + 1].value, out[1]); } diff --git a/core/src/syscall/precompiles/sha512/extend/execute.rs b/core/src/syscall/precompiles/sha512/extend/execute.rs index d82a77f47..3de86a2d5 100644 --- a/core/src/syscall/precompiles/sha512/extend/execute.rs +++ b/core/src/syscall/precompiles/sha512/extend/execute.rs @@ -2,6 +2,7 @@ use super::Sha512ExtendChip; use crate::{ runtime::Syscall, syscall::precompiles::{sha512::Sha512ExtendEvent, SyscallContext}, + utils::u32_pair_to_u64, }; impl Syscall for Sha512ExtendChip { @@ -16,38 +17,27 @@ impl Syscall for Sha512ExtendChip { assert!(i >= 16); assert!(i < 80); - // FIXME - fn u32_vec_to_u64(val: Vec) -> u64 { - u64::from_le_bytes( - val.into_iter() - .flat_map(|x| x.to_le_bytes()) - .collect::>() - .try_into() - .unwrap(), - ) - } - // Read w[i-15]. let (w_i_minus_15_reads, w_i_minus_15) = rt.mr_slice(w_ptr + (i - 15) * 8, 2); - let w_i_minus_15 = u32_vec_to_u64(w_i_minus_15); + let w_i_minus_15 = u32_pair_to_u64(w_i_minus_15[0], w_i_minus_15[1]); // Compute `s0`. let s0 = w_i_minus_15.rotate_right(1) ^ w_i_minus_15.rotate_right(8) ^ (w_i_minus_15 >> 7); // Read w[i-2]. let (w_i_minus_2_reads, w_i_minus_2) = rt.mr_slice(w_ptr + (i - 2) * 8, 2); - let w_i_minus_2 = u32_vec_to_u64(w_i_minus_2); + let w_i_minus_2 = u32_pair_to_u64(w_i_minus_2[0], w_i_minus_2[1]); // Compute `s1`. let s1 = w_i_minus_2.rotate_right(19) ^ w_i_minus_2.rotate_right(61) ^ (w_i_minus_2 >> 6); // Read w[i-16]. let (w_i_minus_16_reads, w_i_minus_16) = rt.mr_slice(w_ptr + (i - 16) * 8, 2); - let w_i_minus_16 = u32_vec_to_u64(w_i_minus_16); + let w_i_minus_16 = u32_pair_to_u64(w_i_minus_16[0], w_i_minus_16[1]); // Read w[i-7]. let (w_i_minus_7_reads, w_i_minus_7) = rt.mr_slice(w_ptr + (i - 7) * 8, 2); - let w_i_minus_7 = u32_vec_to_u64(w_i_minus_7); + let w_i_minus_7 = u32_pair_to_u64(w_i_minus_7[0], w_i_minus_7[1]); // Compute `w_i`. let w_i = s1 diff --git a/core/src/syscall/precompiles/sha512/extend/trace.rs b/core/src/syscall/precompiles/sha512/extend/trace.rs index b64d29d61..ef2c8a774 100644 --- a/core/src/syscall/precompiles/sha512/extend/trace.rs +++ b/core/src/syscall/precompiles/sha512/extend/trace.rs @@ -8,7 +8,7 @@ use crate::{ air::{EventLens, MachineAir, WithEvents}, bytes::{event::ByteRecord, ByteLookupEvent, ByteOpcode}, runtime::{ExecutionRecord, Program}, - utils::pad_rows, + utils::{pad_rows, u32_pair_to_u64}, }; impl<'a> WithEvents<'a> for Sha512ExtendChip { @@ -104,21 +104,10 @@ impl MachineAir for Sha512ExtendChip { &mut new_byte_lookup_events, ); - // FIXME - fn u32_vec_to_u64(val: Vec) -> u64 { - u64::from_le_bytes( - val.into_iter() - .flat_map(|x| x.to_le_bytes()) - .collect::>() - .try_into() - .unwrap(), - ) - } - // `s0 := (w[i-15] rightrotate 1) xor (w[i-15] rightrotate 8) xor (w[i-15] rightshift 7)` let w_i_minus_15_lo = event.w_i_minus_15_reads[0].value; let w_i_minus_15_hi = event.w_i_minus_15_reads[1].value; - let w_i_minus_15 = u32_vec_to_u64(vec![w_i_minus_15_lo, w_i_minus_15_hi]); + let w_i_minus_15 = u32_pair_to_u64(w_i_minus_15_lo, w_i_minus_15_hi); let w_i_minus_15_rr_1 = cols.w_i_minus_15_rr_1 @@ -148,7 +137,7 @@ impl MachineAir for Sha512ExtendChip { // `s1 := (w[i-2] rightrotate 19) xor (w[i-2] rightrotate 61) xor (w[i-2] rightshift 6)` let w_i_minus_2_lo = event.w_i_minus_2_reads[0].value; let w_i_minus_2_hi = event.w_i_minus_2_reads[1].value; - let w_i_minus_2 = u32_vec_to_u64(vec![w_i_minus_2_lo, w_i_minus_2_hi]); + let w_i_minus_2 = u32_pair_to_u64(w_i_minus_2_lo, w_i_minus_2_hi); let w_i_minus_2_rr_19 = cols.w_i_minus_2_rr_19 @@ -178,17 +167,17 @@ impl MachineAir for Sha512ExtendChip { // Compute `s2`. let w_i_minus_7_lo = event.w_i_minus_7_reads[0].value; let w_i_minus_7_hi = event.w_i_minus_7_reads[1].value; - let w_i_minus_7 = u32_vec_to_u64(vec![w_i_minus_7_lo, w_i_minus_7_hi]); + let w_i_minus_7 = u32_pair_to_u64(w_i_minus_7_lo, w_i_minus_7_hi); let w_i_minus_16_lo = event.w_i_minus_16_reads[0].value; let w_i_minus_16_hi = event.w_i_minus_16_reads[1].value; - let w_i_minus_16 = u32_vec_to_u64(vec![w_i_minus_16_lo, w_i_minus_16_hi]); + let w_i_minus_16 = u32_pair_to_u64(w_i_minus_16_lo, w_i_minus_16_hi); // `s2 := w[i-16] + s0 + w[i-7] + s1`. let s2_0 = cols.s2[0].populate(output, shard, event.channel, w_i_minus_16, s0); let s2_1 = cols.s2[1].populate(output, shard, event.channel, s2_0, w_i_minus_7); let s2_2 = cols.s2[2].populate(output, shard, event.channel, s2_1, s1); - let w_i = u32_vec_to_u64(vec![event.w_i_writes[0].value, event.w_i_writes[1].value]); + let w_i = u32_pair_to_u64(event.w_i_writes[0].value, event.w_i_writes[1].value); assert_eq!(s2_2, w_i); cols.w_i[0].populate( diff --git a/core/src/utils/mod.rs b/core/src/utils/mod.rs index d2d8fbbff..9f23f3320 100644 --- a/core/src/utils/mod.rs +++ b/core/src/utils/mod.rs @@ -8,6 +8,7 @@ mod options; mod programs; mod prove; mod tracer; +mod uint; use std::borrow::Borrow; @@ -20,6 +21,7 @@ pub use options::*; pub use programs::tests; pub use prove::*; pub use tracer::*; +pub use uint::*; use crate::{ memory::MemoryCols, diff --git a/core/src/utils/uint.rs b/core/src/utils/uint.rs new file mode 100644 index 000000000..58088012b --- /dev/null +++ b/core/src/utils/uint.rs @@ -0,0 +1,21 @@ +/// Utility function for converting u64s into u32 pairs. +pub fn u64_to_le_u32s(n: u64) -> [u32; 2] { + let n = n.to_le_bytes(); + [ + u32::from_le_bytes(n[..4].try_into().unwrap()), + u32::from_le_bytes(n[4..].try_into().unwrap()), + ] +} + +/// Utility function for converting a u32 LE pair into a u64. +pub fn u32_pair_to_u64(lo_word: u32, hi_word: u32) -> u64 { + u64::from_le_bytes( + lo_word + .to_le_bytes() + .into_iter() + .chain(hi_word.to_le_bytes()) + .collect::>() + .try_into() + .unwrap(), + ) +}