Skip to content

Commit

Permalink
chore: Remove FIXMEs, add utils/uint.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
wwared committed Sep 30, 2024
1 parent e198b3f commit e70ff51
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 78 deletions.
29 changes: 5 additions & 24 deletions core/src/syscall/precompiles/sha512/compress/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<u32>) -> u64 {
u64::from_le_bytes(
val.into_iter()
.flat_map(|x| x.to_le_bytes())
.collect::<Vec<_>>()
.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
Expand All @@ -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];
Expand Down Expand Up @@ -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<u32> = v.into_iter().flat_map(u64_to_u32x2).collect();
let v: Vec<u32> = 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);
Expand Down
13 changes: 2 additions & 11 deletions core/src/syscall/precompiles/sha512/compress/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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),
Expand Down
13 changes: 2 additions & 11 deletions core/src/syscall/precompiles/sha512/compress/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F: PrimeField32> MachineAir<F> for Sha512CompressChip {
type Record = ExecutionRecord;

Expand Down Expand Up @@ -173,7 +164,7 @@ impl<F: PrimeField32> MachineAir<F> 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]);
}
Expand Down
20 changes: 5 additions & 15 deletions core/src/syscall/precompiles/sha512/extend/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -16,38 +17,27 @@ impl Syscall for Sha512ExtendChip {
assert!(i >= 16);
assert!(i < 80);

// FIXME
fn u32_vec_to_u64(val: Vec<u32>) -> u64 {
u64::from_le_bytes(
val.into_iter()
.flat_map(|x| x.to_le_bytes())
.collect::<Vec<_>>()
.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
Expand Down
23 changes: 6 additions & 17 deletions core/src/syscall/precompiles/sha512/extend/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -104,21 +104,10 @@ impl<F: PrimeField32> MachineAir<F> for Sha512ExtendChip {
&mut new_byte_lookup_events,
);

// FIXME
fn u32_vec_to_u64(val: Vec<u32>) -> u64 {
u64::from_le_bytes(
val.into_iter()
.flat_map(|x| x.to_le_bytes())
.collect::<Vec<_>>()
.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
Expand Down Expand Up @@ -148,7 +137,7 @@ impl<F: PrimeField32> MachineAir<F> 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
Expand Down Expand Up @@ -178,17 +167,17 @@ impl<F: PrimeField32> MachineAir<F> 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(
Expand Down
2 changes: 2 additions & 0 deletions core/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod options;
mod programs;
mod prove;
mod tracer;
mod uint;

use std::borrow::Borrow;

Expand All @@ -20,6 +21,7 @@ pub use options::*;
pub use programs::tests;
pub use prove::*;
pub use tracer::*;
pub use uint::*;

use crate::{
memory::MemoryCols,
Expand Down
21 changes: 21 additions & 0 deletions core/src/utils/uint.rs
Original file line number Diff line number Diff line change
@@ -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::<Vec<_>>()
.try_into()
.unwrap(),
)
}

0 comments on commit e70ff51

Please sign in to comment.