diff --git a/src/riscv/lib/src/machine_state/block_cache.rs b/src/riscv/lib/src/machine_state/block_cache.rs index 7f89f8411e3116057c197de07e6c8a4a93a375f8..1731f854aac2845bf9d01fa7b26a5aabeb7b6e16 100644 --- a/src/riscv/lib/src/machine_state/block_cache.rs +++ b/src/riscv/lib/src/machine_state/block_cache.rs @@ -846,9 +846,8 @@ fn run_instr( instr: &EnrichedCell, M>, core: &mut MachineCoreState, ) -> Result { - let instr = instr.read_ref(); - let args = &instr.0.args; - let icall = &instr.1; + let args = &instr.read_ref_stored().args; + let icall = instr.read_derived(); icall.run(args, core) } diff --git a/src/riscv/lib/src/state_backend.rs b/src/riscv/lib/src/state_backend.rs index 6f04d2dce1cb758b62ad6fbfc6dc4ab1bd088812..ce1c55f174346fde59eee26a47e704c8ba0db8ee 100644 --- a/src/riscv/lib/src/state_backend.rs +++ b/src/riscv/lib/src/state_backend.rs @@ -166,23 +166,22 @@ pub trait ManagerRead: ManagerBase { values: &mut [E], ); - /// Read the value, and derived value, contained in the enriched cell. - fn enriched_cell_read(cell: &Self::EnrichedCell) -> (V::E, V::D) + /// Read the value contained in the enriched cell. + fn enriched_cell_read_stored(cell: &Self::EnrichedCell) -> V::E where - V: EnrichedValueLinked, - V::E: Copy, - V::D: Copy; + V: EnrichedValue, + V::E: Copy; - /// Obtain a refernce to the value, and derived value, contained in the enriched cell. - fn enriched_cell_ref(cell: &Self::EnrichedCell) -> (&V::E, &V::D) + /// Read the derived value of the enriched cell. + fn enriched_cell_read_derived(cell: &Self::EnrichedCell) -> V::D where - V: EnrichedValueLinked; + V: EnrichedValueLinked, + V::D: Copy; - /// Obtain a refernce to the value, and derived value, contained in the enriched cell. - fn enriched_cell_read_stored(cell: &Self::EnrichedCell) -> V::E + /// Obtain a refernce to the value contained in the enriched cell. + fn enriched_cell_ref_stored(cell: &Self::EnrichedCell) -> &V::E where - V: EnrichedValue, - V::E: Copy; + V: EnrichedValue; } /// Manager with write capabilities @@ -363,29 +362,28 @@ impl ManagerRead for Ref<'_, M> { M::dyn_region_read_all(region, address, values) } - fn enriched_cell_read>( - cell: &Self::EnrichedCell, - ) -> (V::E, V::D) + fn enriched_cell_read_stored(cell: &Self::EnrichedCell) -> V::E where - V::D: Copy, + V: EnrichedValue, V::E: Copy, { - M::enriched_cell_read(cell) + M::enriched_cell_read_stored(cell) } - fn enriched_cell_ref>( - cell: &Self::EnrichedCell, - ) -> (&V::E, &V::D) -where { - M::enriched_cell_ref(cell) + fn enriched_cell_ref_stored(cell: &Self::EnrichedCell) -> &V::E + where + V: EnrichedValue, + { + M::enriched_cell_ref_stored(cell) } - fn enriched_cell_read_stored(cell: &Self::EnrichedCell) -> V::E + fn enriched_cell_read_derived>( + cell: &Self::EnrichedCell, + ) -> V::D where - V: EnrichedValue, - V::E: Copy, + V::D: Copy, { - M::enriched_cell_read_stored(cell) + M::enriched_cell_read_derived(cell) } } diff --git a/src/riscv/lib/src/state_backend/owned_backend.rs b/src/riscv/lib/src/state_backend/owned_backend.rs index 5838853428f780a1784fa1f4892cf6fdd9dcb55b..a4aa78e6a19117eca6d9f42d38315de1b9349dff 100644 --- a/src/riscv/lib/src/state_backend/owned_backend.rs +++ b/src/riscv/lib/src/state_backend/owned_backend.rs @@ -112,24 +112,26 @@ impl ManagerRead for Owned { } } - fn enriched_cell_read(cell: &Self::EnrichedCell) -> (V::E, V::D) + fn enriched_cell_read_stored(cell: &Self::EnrichedCell) -> V::E where + V: EnrichedValue, V::E: Copy, - V::D: Copy, { - *cell + cell.0 } - fn enriched_cell_ref(cell: &Self::EnrichedCell) -> (&V::E, &V::D) { - (&cell.0, &cell.1) + fn enriched_cell_read_derived(cell: &Self::EnrichedCell) -> V::D + where + V::D: Copy, + { + cell.1 } - fn enriched_cell_read_stored(cell: &Self::EnrichedCell) -> V::E + fn enriched_cell_ref_stored(cell: &Self::EnrichedCell) -> &V::E where V: EnrichedValue, - V::E: Copy, { - cell.0 + &cell.0 } } @@ -427,38 +429,39 @@ pub mod test_helpers { #[test] fn enriched_cell_serialise() { pub struct Enriching; - pub struct Fun(Box u64>); impl EnrichedValue for Enriching { type E = u64; - type D = Fun; + type D = T; } - impl<'a> From<&'a u64> for Fun { + #[derive(Clone, Copy)] + pub struct T(u64); + + impl<'a> From<&'a u64> for T { fn from(value: &'a u64) -> Self { - let value = *value; - Self(Box::new(move |x| value.wrapping_add(x))) + T(value.wrapping_add(1)) } } proptest::proptest!(|(value: u64)| { - let mut cell: EnrichedCell = EnrichedCell::bind((0u64, Fun::from(&0))); + let mut cell: EnrichedCell = EnrichedCell::bind((0u64, T::from(&0))); cell.write(value); - assert_eq!(value, *cell.read_ref().0); + let read_value = cell.read_ref_stored(); + + assert_eq!(value, *read_value); let bytes = bincode::serialize(&cell).unwrap(); let cell_after: EnrichedCell = bincode::deserialize(&bytes).unwrap(); - for i in 0..5 { - assert_eq!(cell.read_ref().0, cell_after.read_ref().0); + assert_eq!(*cell.read_ref_stored(), *cell_after.read_ref_stored()); - let fun = cell.read_ref().1; - let fun_after = cell_after.read_ref().1; + let derived = cell.read_derived(); + let derived_after = cell_after.read_derived(); - assert_eq!(value.wrapping_add(i), (fun.0)(i)); - assert_eq!((fun.0)(i), (fun_after.0)(i)); - } + assert_eq!(T::from(read_value).0, derived.0); + assert_eq!(derived.0, derived_after.0); }); } diff --git a/src/riscv/lib/src/state_backend/proof_backend.rs b/src/riscv/lib/src/state_backend/proof_backend.rs index f31fb2289ad04b3113f07c846604358004e07885..98da3a33c61f1ce26bf0a715430165d178902f0c 100644 --- a/src/riscv/lib/src/state_backend/proof_backend.rs +++ b/src/riscv/lib/src/state_backend/proof_backend.rs @@ -113,28 +113,27 @@ impl ManagerRead for ProofGen { } } - fn enriched_cell_read(_cell: &Self::EnrichedCell) -> (V::E, V::D) + fn enriched_cell_read_stored(_cell: &Self::EnrichedCell) -> V::E where - V: EnrichedValueLinked, + V: EnrichedValue, V::E: Copy, - V::D: Copy, { // TODO: RV-325 Support for `EnrichedCell` in the proof-generating backend todo!() } - fn enriched_cell_ref(_cell: &Self::EnrichedCell) -> (&V::E, &V::D) + fn enriched_cell_read_derived(_cell: &Self::EnrichedCell) -> V::D where V: EnrichedValueLinked, + V::D: Copy, { // TODO: RV-325 Support for `EnrichedCell` in the proof-generating backend todo!() } - fn enriched_cell_read_stored(_cell: &Self::EnrichedCell) -> V::E + fn enriched_cell_ref_stored(_cell: &Self::EnrichedCell) -> &V::E where V: EnrichedValue, - V::E: Copy, { // TODO: RV-325 Support for `EnrichedCell` in the proof-generating backend todo!() diff --git a/src/riscv/lib/src/state_backend/region.rs b/src/riscv/lib/src/state_backend/region.rs index 54ccb7da86eecd880af62536e81aa50708d3dc9c..605c073df2c946882d82e7cadfcbfebd52d5b403 100644 --- a/src/riscv/lib/src/state_backend/region.rs +++ b/src/riscv/lib/src/state_backend/region.rs @@ -50,34 +50,31 @@ impl EnrichedCell { M::enriched_cell_write(&mut self.cell, value) } - /// Read the value & derived value from the enriched cell. - pub fn read(&self) -> (V::E, V::D) + /// Read the stored value from the enriched cell. + pub fn read_stored(&self) -> V::E where M: ManagerRead, - V: EnrichedValueLinked, V::E: Copy, - V::D: Copy, { - M::enriched_cell_read(&self.cell) + M::enriched_cell_read_stored(&self.cell) } - /// Obtain references to the value & derived value contained - /// within the cell. - pub fn read_ref(&self) -> (&V::E, &V::D) + /// Read the derived value from the enriched cell. + pub fn read_derived(&self) -> V::D where M: ManagerRead, V: EnrichedValueLinked, + V::D: Copy, { - M::enriched_cell_ref(&self.cell) + M::enriched_cell_read_derived(&self.cell) } - /// Read only the stored value from the enriched cell. - pub fn read_stored(&self) -> V::E + /// Obtain a reference to the value contained within the cell. + pub fn read_ref_stored(&self) -> &V::E where M: ManagerRead, - V::E: Copy, { - M::enriched_cell_read_stored(&self.cell) + M::enriched_cell_ref_stored(&self.cell) } } @@ -99,7 +96,7 @@ where V::E: PartialEq, { fn eq(&self, other: &Self) -> bool { - M::enriched_cell_ref(&self.cell).0 == M::enriched_cell_ref(&other.cell).0 + M::enriched_cell_ref_stored(&self.cell) == M::enriched_cell_ref_stored(&other.cell) } }