From 3a21c8dee43c77604350fdf33c1615e25bf1dacd Mon Sep 17 00:00:00 2001 From: Shahar Kaminsky Date: Tue, 26 Sep 2023 17:27:09 +0300 Subject: [PATCH] Mirrors branch v1.3.3 from private at 61b1b630862aa4134233a045f9dd4e8509263333 (#12) --- .gitignore | 1 - cs_derive/src/utils.rs | 2 +- src/glue/code_unpacker_sha256/mod.rs | 2 +- src/glue/memory_queries_validity/mod.rs | 1 - src/glue/merkleize_l1_messages/merkleize.rs | 2 - src/glue/optimizable_queue/simple_impl.rs | 1 - src/recursion/recursion_tree/utils.rs | 1 - src/scheduler/queues/memory_access.rs | 206 ------------------ src/scheduler/queues/mod.rs | 2 - src/vm/primitives/memory_view/mod.rs | 4 - src/vm/primitives/memory_view/read_view.rs | 127 ----------- src/vm/primitives/memory_view/write_query.rs | 86 -------- src/vm/primitives/mod.rs | 1 - src/vm/vm_cycle/memory_view/write_query.rs | 1 + .../opcode_execution/special_call_ret.rs | 2 - 15 files changed, 3 insertions(+), 436 deletions(-) delete mode 100644 src/scheduler/queues/memory_access.rs delete mode 100644 src/vm/primitives/memory_view/mod.rs delete mode 100644 src/vm/primitives/memory_view/read_view.rs delete mode 100644 src/vm/primitives/memory_view/write_query.rs diff --git a/.gitignore b/.gitignore index 04fa3b6..77147e2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ /target Cargo.lock .idea/ - diff --git a/cs_derive/src/utils.rs b/cs_derive/src/utils.rs index bfa7577..46c39ef 100644 --- a/cs_derive/src/utils.rs +++ b/cs_derive/src/utils.rs @@ -77,7 +77,7 @@ fn find_in_equivalent_types(ty: &TypePath) -> (TypePath, Option) { let second_ty: TypePath = syn::parse_str(b).unwrap(); let type_param = if let Some(ref ty_param) = c { Some(GenericParam::Type( - syn::parse_str::(ty_param.clone()).unwrap(), + syn::parse_str::(ty_param).unwrap(), )) } else { None diff --git a/src/glue/code_unpacker_sha256/mod.rs b/src/glue/code_unpacker_sha256/mod.rs index 1e7a57d..faefcc0 100644 --- a/src/glue/code_unpacker_sha256/mod.rs +++ b/src/glue/code_unpacker_sha256/mod.rs @@ -250,7 +250,7 @@ use crate::glue::optimizable_queue::{ }; use crate::glue::traits::get_vec_vec_witness_raw_with_hint_on_more_in_subset; use crate::glue::traits::CircuitFixedLengthEncodableExt; -use crate::scheduler::queues::memory_access::*; +// use crate::scheduler::queues::memory_access::*; use crate::scheduler::queues::*; use crate::vm::optimizer::sponge_set::SpongeOptimizer; use crate::vm::primitives::UInt64; diff --git a/src/glue/memory_queries_validity/mod.rs b/src/glue/memory_queries_validity/mod.rs index 7651c01..65eff69 100644 --- a/src/glue/memory_queries_validity/mod.rs +++ b/src/glue/memory_queries_validity/mod.rs @@ -5,7 +5,6 @@ use crate::glue::optimizable_queue::{ }; use crate::glue::traits::get_vec_witness_raw_with_hint; use crate::glue::traits::CircuitFixedLengthEncodableExt; -use crate::scheduler::queues::memory_access::*; use crate::scheduler::queues::*; use crate::vm::optimizer::sponge_set::SpongeOptimizer; use crate::vm::primitives::UInt64; diff --git a/src/glue/merkleize_l1_messages/merkleize.rs b/src/glue/merkleize_l1_messages/merkleize.rs index fc65333..8ff8803 100644 --- a/src/glue/merkleize_l1_messages/merkleize.rs +++ b/src/glue/merkleize_l1_messages/merkleize.rs @@ -606,8 +606,6 @@ pub(crate) fn byte_encodable_queue_binary_hash< chunk.copy_from_slice(&encoding); } - drop(encoding_area); - let binary_hash = binary_hasher.hash(cs, &bytes)?; Ok(binary_hash) diff --git a/src/glue/optimizable_queue/simple_impl.rs b/src/glue/optimizable_queue/simple_impl.rs index 0c80ff1..5151ae3 100644 --- a/src/glue/optimizable_queue/simple_impl.rs +++ b/src/glue/optimizable_queue/simple_impl.rs @@ -1,5 +1,4 @@ use super::super::traits::*; -use super::utils::*; use super::*; use crate::bellman::SynthesisError; use crate::circuit_structures::traits::*; diff --git a/src/recursion/recursion_tree/utils.rs b/src/recursion/recursion_tree/utils.rs index ad11a04..d709baf 100644 --- a/src/recursion/recursion_tree/utils.rs +++ b/src/recursion/recursion_tree/utils.rs @@ -46,7 +46,6 @@ impl< layer_elements.push(node_hash); } assert!(layer_elements.len().is_power_of_two()); - drop(src); node_layers.push(layer_elements); } assert_eq!(node_layers.last().unwrap().len(), 1); diff --git a/src/scheduler/queues/memory_access.rs b/src/scheduler/queues/memory_access.rs deleted file mode 100644 index d5067ba..0000000 --- a/src/scheduler/queues/memory_access.rs +++ /dev/null @@ -1,206 +0,0 @@ -use super::*; -use crate::glue::traits::*; -use crate::vm::primitives::{UInt16, UInt32}; -use cs_derive::*; -use num_traits::Zero; -// We accumulate memory queries and then use a standard validity argument - -#[derive( - Derivative, - CSWitnessable, - CSAllocatable, - CSEqual, - CSSelectable, - FixedLengthEncodableExt, - FixedLengthDecodableExt, -)] -#[EncodingLength = "2"] -#[PackWithCS = "true"] -#[derivative(Clone, Debug)] -pub struct PackedMemoryQuery { - pub timestamp_meta: Boolean, - pub timestamp: UInt32, - pub memory_key: UInt64, - pub rw_flag: Boolean, - pub value_residual: UInt64, - pub value: Num, -} - -impl PackedMemoryQuery { - pub fn pack>( - &self, - cs: &mut CS, - ) -> Result<[Num; 2], SynthesisError> { - let shifts = compute_shifts::(); - let el0 = self.value; - - let mut shift = 0; - let mut lc = LinearCombination::zero(); - lc.add_assign_number_with_coeff(&self.value_residual.inner, shifts[shift]); - shift += 64; - lc.add_assign_boolean_with_coeff(&self.rw_flag, shifts[shift]); - shift += 1; - lc.add_assign_number_with_coeff(&self.memory_key.inner, shifts[shift]); - shift += 64; - lc.add_assign_number_with_coeff(&self.timestamp.inner, shifts[shift]); - shift += 32; - lc.add_assign_boolean_with_coeff(&self.timestamp_meta, shifts[shift]); - shift += 1; - - assert!(shift <= E::Fr::CAPACITY as usize); - - let el1 = lc.into_num(cs)?; - - Ok([el0, el1]) - } -} - -#[derive( - Derivative, - CSWitnessable, - CSAllocatable, - CSEqual, - CSSelectable, - FixedLengthEncodableExt, - FixedLengthDecodableExt, -)] -#[EncodingLength = "2"] -#[PackWithCS = "true"] -#[derivative(Clone, Debug)] -pub struct RawMemoryQuery { - pub timestamp_meta: Boolean, - pub timestamp: UInt32, - pub memory_markers: [Boolean; 2], - pub memory_page: UInt32, - pub memory_index: UInt16, - pub rw_flag: Boolean, - pub value_residual: UInt64, - pub value: Num, -} - -impl RawMemoryQuery { - pub fn pack>( - &self, - cs: &mut CS, - ) -> Result<[Num; 2], SynthesisError> { - let shifts = compute_shifts::(); - let el0 = self.value; - - let mut shift = 0; - let mut lc = LinearCombination::zero(); - lc.add_assign_number_with_coeff(&self.value_residual.inner, shifts[shift]); - shift += 64; - lc.add_assign_boolean_with_coeff(&self.rw_flag, shifts[shift]); - shift += 1; - // NOTE: we pack is as it would be compatible with PackedMemoryQuery later on - lc.add_assign_number_with_coeff(&self.memory_index.inner, shifts[shift]); - shift += 16; - lc.add_assign_number_with_coeff(&self.memory_page.inner, shifts[shift]); - shift += 32; - lc.add_assign_boolean_with_coeff(&self.memory_markers[0], shifts[shift]); - shift += 1; - lc.add_assign_boolean_with_coeff(&self.memory_markers[1], shifts[shift]); - shift += 1; - - shift += 14; - // ------------ - lc.add_assign_number_with_coeff(&self.timestamp.inner, shifts[shift]); - shift += 32; - lc.add_assign_boolean_with_coeff(&self.timestamp_meta, shifts[shift]); - shift += 1; - - assert!(shift <= E::Fr::CAPACITY as usize); - - let el1 = lc.into_num(cs)?; - - Ok([el0, el1]) - } -} - -#[derive(Derivative, CSWitnessable, CSAllocatable, CSEqual, CSSelectable)] -#[derivative(Clone, Debug)] -pub struct MemoryQuery { - pub timestamp_meta: Boolean, - pub timestamp: UInt32, - pub memory_markers: [Boolean; 2], - pub memory_page: UInt32, - pub memory_index: UInt16, - pub rw_flag: Boolean, - pub value: UInt256, -} - -impl MemoryQuery { - pub fn empty() -> Self { - Self { - timestamp_meta: Boolean::constant(false), - timestamp: UInt32::zero(), - memory_markers: [Boolean::constant(false); 2], - memory_page: UInt32::zero(), - memory_index: UInt16::zero(), - rw_flag: Boolean::constant(false), - value: UInt256::zero(), - } - } - pub fn into_raw_query>( - &self, - cs: &mut CS, - ) -> Result, SynthesisError> { - let shifts = compute_shifts::(); - let residual = self.value.inner[3]; - - let mut shift = 0; - let mut lc = LinearCombination::zero(); - lc.add_assign_number_with_coeff(&self.value.inner[0].inner, shifts[shift]); - shift += 64; - lc.add_assign_number_with_coeff(&self.value.inner[1].inner, shifts[shift]); - shift += 64; - lc.add_assign_number_with_coeff(&self.value.inner[2].inner, shifts[shift]); - shift += 64; - assert!(shift <= E::Fr::CAPACITY as usize); - let value = lc.into_num(cs)?; - - let query = RawMemoryQuery { - timestamp_meta: self.timestamp_meta, - timestamp: self.timestamp, - memory_markers: self.memory_markers, - memory_page: self.memory_page, - memory_index: self.memory_index, - rw_flag: self.rw_flag, - value_residual: residual, - value, - }; - - Ok(query) - } -} - -#[derive(Derivative, CSWitnessable, CSAllocatable, CSEqual, CSSelectable)] -#[derivative(Clone, Debug)] -pub struct CodeQuery { - pub timestamp_meta: Boolean, - pub timestamp: UInt32, - pub memory_page: UInt32, - pub memory_index: UInt16, - pub rw_flag: Boolean, - pub value: Num, -} - -impl CodeQuery { - pub fn into_raw_query(&self) -> RawMemoryQuery { - let query = RawMemoryQuery { - timestamp_meta: self.timestamp_meta, - timestamp: self.timestamp, - memory_markers: [Boolean::constant(false); 2], - memory_page: self.memory_page, - memory_index: self.memory_index, - rw_flag: self.rw_flag, - value_residual: UInt64::zero(), - value: self.value, - }; - - query - } -} - -pub type MemoryQueriesQueue = - FixedWidthEncodingSpongeLikeQueue, 2, SW, AW>; diff --git a/src/scheduler/queues/mod.rs b/src/scheduler/queues/mod.rs index 3808f18..e6d8a1a 100644 --- a/src/scheduler/queues/mod.rs +++ b/src/scheduler/queues/mod.rs @@ -8,7 +8,6 @@ pub mod delegated_memory_write; pub mod ecdsa_request; pub mod full_sponge_like_queue_state; pub mod l1_authorizations; -pub mod memory_access; pub mod pubdata_chunk; pub mod queue_state; pub mod recursive_request; @@ -24,7 +23,6 @@ pub use self::delegated_memory_write::*; pub use self::ecdsa_request::*; pub use self::full_sponge_like_queue_state::*; pub use self::l1_authorizations::*; -pub use self::memory_access::*; pub use self::pubdata_chunk::*; pub use self::queue_state::*; pub use self::recursive_request::*; diff --git a/src/vm/primitives/memory_view/mod.rs b/src/vm/primitives/memory_view/mod.rs deleted file mode 100644 index 889f7b5..0000000 --- a/src/vm/primitives/memory_view/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -use super::*; - -pub mod read_view; -pub mod write_query; \ No newline at end of file diff --git a/src/vm/primitives/memory_view/read_view.rs b/src/vm/primitives/memory_view/read_view.rs deleted file mode 100644 index 70ded04..0000000 --- a/src/vm/primitives/memory_view/read_view.rs +++ /dev/null @@ -1,127 +0,0 @@ -use super::*; -use crate::scheduler::queues::RawMemoryQuery; -use super::super::register_view::*; - - -// Important note on this structure: we can make sure that -// - this query will not go into the queue if it's not executed (so if opcode doesn't touch a memory) -// - if it's executed then it will be a feeded all the way into the function that decomposes a selected -// value into the "view", thus range checks all the uint128 parts that we use unchecked here -// So we only need to ensure that all parts are range checked if all the above actually executes. It automatically -// will guarantee us that lowest128 is indeed 128 bits, and that u64_word_2 + (u64_word_3 << 64) is also 128 bits, -// so we only need to ensure that e.g. u64_word_3 is 128 bits - -#[derive(Derivative)] -#[derivative(Clone, Debug)] -pub struct MemoryKey { - pub timestamp: UInt32, - pub memory_markers: [Boolean; 2], - pub memory_page: UInt32, - pub memory_index: UInt16, -} - -impl MemoryKey { - pub fn uninitialized() -> Self { - MemoryKey { - timestamp: UInt32::::zero(), - memory_markers: [Boolean::constant(false); 2], - memory_page: UInt32::::zero(), - memory_index: UInt16::::zero(), - } - } -} - -// DO NOT make pub to avoid breaking invariants - -#[derive(Derivative)] -#[derivative(Clone, Debug)] -pub struct MemoryReadQuery { - timestamp: UInt32, - memory_markers: [Boolean; 2], - memory_page: UInt32, - memory_index: UInt16, - lowest_128: UInt128, - u64_word_2: UInt64, - u64_word_3: UInt64, -} - -impl MemoryReadQuery { - pub(crate) fn into_raw_query>(&self, cs: &mut CS) -> Result, SynthesisError> { - let shifts = compute_shifts::(); - let mut lc = LinearCombination::zero(); - lc.add_assign_number_with_coeff(&self.lowest_128.inner, shifts[0]); - lc.add_assign_number_with_coeff(&self.u64_word_2.inner, shifts[128]); - let value = lc.into_num(cs)?; - let new = RawMemoryQuery { - timestamp_meta: Boolean::constant(true), - timestamp: self.timestamp, - memory_markers: self.memory_markers, - memory_page: self.memory_page, - memory_index: self.memory_index, - rw_flag: Boolean::constant(false), - value_residual: self.u64_word_3, - value, - }; - - Ok(new) - } - - pub(crate) fn from_key_and_value_witness>(cs: &mut CS, key: MemoryKey, value: Option) -> Result { - use num_traits::ToPrimitive; - - // decompose witness if we know it - let mut words = split_some_into_fixed_number_of_limbs(value, 64, 4); - let u64_word_3 = words.pop().unwrap().map(|el| el.to_u64().unwrap()); - let u64_word_2 = words.pop().unwrap().map(|el| el.to_u64().unwrap()); - let u64_word_1 = words.pop().unwrap(); - let u64_word_0 = words.pop().unwrap(); - - // only range check the word 3, and make a wittness for the rest - let lowest_128 = match (u64_word_0, u64_word_1) { - (Some(u64_word_0), Some(u64_word_1)) => { - let tmp = u64_word_0 + (u64_word_1 << 64u64); - - Some(tmp.to_u128().unwrap()) - }, - _ => None - }; - - let lowest_128 = UInt128::allocate_unchecked(cs, lowest_128)?; - let u64_word_2 = UInt64::allocate(cs, u64_word_2)?; - let u64_word_3 = UInt64::allocate(cs, u64_word_3)?; - - let MemoryKey { - timestamp, - memory_markers, - memory_page, - memory_index, - } = key; - - let new = Self { - timestamp, - memory_markers, - memory_page, - memory_index, - lowest_128, - u64_word_2, - u64_word_3 - }; - - Ok(new) - } - - pub(crate) fn into_register_value>(&self, cs: &mut CS) -> Result, SynthesisError> { - let shifts = compute_shifts::(); - let mut lc = LinearCombination::zero(); - lc.add_assign_number_with_coeff(&self.u64_word_2.inner, shifts[0]); - lc.add_assign_number_with_coeff(&self.u64_word_3.inner, shifts[64]); - let highest_128 = UInt128::from_num_unchecked(lc.into_num(cs)?); - - let result = Register { - inner: [self.lowest_128, highest_128] - }; - - Ok(result) - } -} - diff --git a/src/vm/primitives/memory_view/write_query.rs b/src/vm/primitives/memory_view/write_query.rs deleted file mode 100644 index 5f87800..0000000 --- a/src/vm/primitives/memory_view/write_query.rs +++ /dev/null @@ -1,86 +0,0 @@ -use super::*; -use crate::scheduler::queues::RawMemoryQuery; -use super::read_view::MemoryKey; -use super::super::register_view::*; - -// DO NOT make pub to avoid breaking invariants - -#[derive(Derivative)] -#[derivative(Clone, Debug)] -pub struct MemoryWriteQuery { - timestamp: UInt32, - memory_markers: [Boolean; 2], - memory_page: UInt32, - memory_index: UInt16, - lowest_128: UInt128, - u64_word_2: UInt64, - u64_word_3: UInt64, -} - -impl MemoryWriteQuery { - pub(crate) fn into_raw_query>(&self, cs: &mut CS) -> Result, SynthesisError> { - let shifts = compute_shifts::(); - let mut lc = LinearCombination::zero(); - lc.add_assign_number_with_coeff(&self.lowest_128.inner, shifts[0]); - lc.add_assign_number_with_coeff(&self.u64_word_2.inner, shifts[128]); - let value = lc.into_num(cs)?; - let new = RawMemoryQuery { - timestamp_meta: Boolean::constant(true), - timestamp: self.timestamp, - memory_markers: self.memory_markers, - memory_page: self.memory_page, - memory_index: self.memory_index, - rw_flag: Boolean::constant(false), - value_residual: self.u64_word_3, - value, - }; - - Ok(new) - } - - pub(crate) fn from_key_and_value_witness>(cs: &mut CS, key: MemoryKey, register_output: Register) -> Result { - use num_traits::ToPrimitive; - - let [lowest_128, highest_128] = register_output.inner; - - let tmp = highest_128.get_value().map(|el| (el as u64, (el >> 64) as u64)); - let (u64_word_2, u64_word_3) = match tmp { - Some((a, b)) => (Some(a), Some(b)), - _ => (None, None) - }; - - // we do not need to range check everything, only N-1 ut of N elements in LC - - let u64_word_2 = UInt64::allocate_unchecked(cs, u64_word_2)?; - let u64_word_3 = UInt64::allocate(cs, u64_word_3)?; - - let shifts = compute_shifts::(); - let mut minus_one = E::Fr::one(); - minus_one.negate(); - - let mut lc = LinearCombination::zero(); - lc.add_assign_number_with_coeff(&u64_word_2.inner, shifts[0]); - lc.add_assign_number_with_coeff(&u64_word_3.inner, shifts[64]); - lc.add_assign_number_with_coeff(&highest_128.inner, minus_one); - - let MemoryKey { - timestamp, - memory_markers, - memory_page, - memory_index, - } = key; - - let new = Self { - timestamp, - memory_markers, - memory_page, - memory_index, - lowest_128, - u64_word_2, - u64_word_3 - }; - - Ok(new) - } -} - diff --git a/src/vm/primitives/mod.rs b/src/vm/primitives/mod.rs index 7e48e14..118010f 100644 --- a/src/vm/primitives/mod.rs +++ b/src/vm/primitives/mod.rs @@ -5,7 +5,6 @@ pub mod rescue_sponge; pub mod small_uints; pub mod uint256; pub mod utils; -// pub mod memory_view; pub use small_uints::u160; pub use small_uints::{UInt128, UInt16, UInt160, UInt32, UInt64, UInt8}; diff --git a/src/vm/vm_cycle/memory_view/write_query.rs b/src/vm/vm_cycle/memory_view/write_query.rs index cbb4172..b7b5c28 100644 --- a/src/vm/vm_cycle/memory_view/write_query.rs +++ b/src/vm/vm_cycle/memory_view/write_query.rs @@ -73,6 +73,7 @@ impl MemoryWriteQuery { lc.add_assign_number_with_coeff(&u64_word_2.inner, shifts[0]); lc.add_assign_number_with_coeff(&u64_word_3.inner, shifts[64]); lc.add_assign_number_with_coeff(&highest_128.inner, minus_one); + lc.enforce_zero(cs)?; let MemoryKey { timestamp, diff --git a/src/vm/vm_cycle/opcode_execution/special_call_ret.rs b/src/vm/vm_cycle/opcode_execution/special_call_ret.rs index a03c1b8..94b48d6 100644 --- a/src/vm/vm_cycle/opcode_execution/special_call_ret.rs +++ b/src/vm/vm_cycle/opcode_execution/special_call_ret.rs @@ -1127,8 +1127,6 @@ fn callstack_candidate_for_far_call< let call_now_in_construction_kernel = smart_and(cs, &[can_call_code.not(), target_is_kernel])?; - drop(bytecode_hash); - // at the end of the day all our exceptions will lead to memory page being 0 code_hash_length_in_words = code_hash_length_in_words.mask(cs, &code_format_exception.not())?;