From fdb804a96f7b70bd10c5f319f93a01a57d28f590 Mon Sep 17 00:00:00 2001 From: Vladislav Volosnikov Date: Wed, 4 Sep 2024 17:51:39 +0200 Subject: [PATCH 1/3] Simplify ram permutation circuit witness --- .../src/ram_permutation/input.rs | 14 ++-------- .../zkevm_circuits/src/ram_permutation/mod.rs | 11 ++++++-- .../memory_related/ram_permutation.rs | 27 +++++-------------- 3 files changed, 17 insertions(+), 35 deletions(-) diff --git a/crates/zkevm_circuits/src/ram_permutation/input.rs b/crates/zkevm_circuits/src/ram_permutation/input.rs index c1d1ee1..d000cf0 100644 --- a/crates/zkevm_circuits/src/ram_permutation/input.rs +++ b/crates/zkevm_circuits/src/ram_permutation/input.rs @@ -116,18 +116,8 @@ pub type RamPermutationCycleInputOutputWitness = crate::fsm_input_output::Clo pub struct RamPermutationCircuitInstanceWitness { pub closed_form_input: RamPermutationCycleInputOutputWitness, - pub unsorted_queue_witness: FullStateCircuitQueueRawWitness< - F, - MemoryQuery, - FULL_SPONGE_QUEUE_STATE_WIDTH, - MEMORY_QUERY_PACKED_WIDTH, - >, - pub sorted_queue_witness: FullStateCircuitQueueRawWitness< - F, - MemoryQuery, - FULL_SPONGE_QUEUE_STATE_WIDTH, - MEMORY_QUERY_PACKED_WIDTH, - >, + pub unsorted_queue_witness: Vec< as CSAllocatable>::Witness>, + pub sorted_queue_witness: Vec< as CSAllocatable>::Witness>, } pub type MemoryQueriesQueue = diff --git a/crates/zkevm_circuits/src/ram_permutation/mod.rs b/crates/zkevm_circuits/src/ram_permutation/mod.rs index 249b77d..bcc3f1d 100644 --- a/crates/zkevm_circuits/src/ram_permutation/mod.rs +++ b/crates/zkevm_circuits/src/ram_permutation/mod.rs @@ -1,9 +1,12 @@ use super::*; +use base_structures::vm_state::FULL_SPONGE_QUEUE_STATE_WIDTH; use boojum::cs::traits::cs::ConstraintSystem; +use boojum::field::Field; use boojum::field::SmallField; use boojum::gadgets::boolean::Boolean; use boojum::gadgets::num::Num; +use boojum::gadgets::queue::full_state_queue::FullStateCircuitQueueRawWitness; use boojum::gadgets::traits::selectable::Selectable; use boojum::gadgets::u256::UInt256; use boojum::gadgets::u32::UInt32; @@ -78,7 +81,9 @@ where > = MemoryQueriesQueue::from_state(cs, unsorted_queue_state); unsorted_queue.witness = Arc::new(FullStateCircuitQueueWitness::from_inner_witness( - unsorted_queue_witness, + FullStateCircuitQueueRawWitness { + elements: unsorted_queue_witness.into_iter().map(|x| (x, [Field::ZERO; FULL_SPONGE_QUEUE_STATE_WIDTH])).collect() + }, )); // passthrought must be trivial @@ -104,7 +109,9 @@ where > = MemoryQueriesQueue::from_state(cs, sorted_queue_state); sorted_queue.witness = Arc::new(FullStateCircuitQueueWitness::from_inner_witness( - sorted_queue_witness, + FullStateCircuitQueueRawWitness { + elements: sorted_queue_witness.into_iter().map(|x| (x, [Field::ZERO; FULL_SPONGE_QUEUE_STATE_WIDTH])).collect() + }, )); // get challenges for permutation argument diff --git a/crates/zkevm_test_harness/src/witness/individual_circuits/memory_related/ram_permutation.rs b/crates/zkevm_test_harness/src/witness/individual_circuits/memory_related/ram_permutation.rs index 2f25f2d..8483532 100644 --- a/crates/zkevm_test_harness/src/witness/individual_circuits/memory_related/ram_permutation.rs +++ b/crates/zkevm_test_harness/src/witness/individual_circuits/memory_related/ram_permutation.rs @@ -124,6 +124,10 @@ pub(crate) fn compute_ram_circuit_snapshots = all_memory_queries.par_iter().map(|x| x.encoding_witness()).collect(); + let rhs_contributions: Vec<_> = all_memory_queries_sorted.par_iter().map(|x| x.encoding_witness()).collect(); + let challenges = produce_fs_challenges::< Field, RoundFunction, @@ -138,15 +142,6 @@ pub(crate) fn compute_ram_circuit_snapshots = all_memory_queries - .iter() - .map(|x| x.encoding_witness()) - .collect(); - let rhs_contributions: Vec<_> = all_memory_queries_sorted - .iter() - .map(|x| x.encoding_witness()) - .collect(); - for idx in 0..DEFAULT_NUM_PERMUTATION_ARGUMENT_REPETITIONS { let (lhs_grand_product_chain, rhs_grand_product_chain) = compute_grand_product_chains( &lhs_contributions.iter().collect(), @@ -355,18 +350,8 @@ pub(crate) fn compute_ram_circuit_snapshots Date: Mon, 30 Sep 2024 18:33:58 +0200 Subject: [PATCH 2/3] Cargo fmt in circuits --- crates/zkevm_circuits/src/ram_permutation/mod.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/zkevm_circuits/src/ram_permutation/mod.rs b/crates/zkevm_circuits/src/ram_permutation/mod.rs index bcc3f1d..b546524 100644 --- a/crates/zkevm_circuits/src/ram_permutation/mod.rs +++ b/crates/zkevm_circuits/src/ram_permutation/mod.rs @@ -82,7 +82,10 @@ where unsorted_queue.witness = Arc::new(FullStateCircuitQueueWitness::from_inner_witness( FullStateCircuitQueueRawWitness { - elements: unsorted_queue_witness.into_iter().map(|x| (x, [Field::ZERO; FULL_SPONGE_QUEUE_STATE_WIDTH])).collect() + elements: unsorted_queue_witness + .into_iter() + .map(|x| (x, [Field::ZERO; FULL_SPONGE_QUEUE_STATE_WIDTH])) + .collect(), }, )); @@ -110,7 +113,10 @@ where sorted_queue.witness = Arc::new(FullStateCircuitQueueWitness::from_inner_witness( FullStateCircuitQueueRawWitness { - elements: sorted_queue_witness.into_iter().map(|x| (x, [Field::ZERO; FULL_SPONGE_QUEUE_STATE_WIDTH])).collect() + elements: sorted_queue_witness + .into_iter() + .map(|x| (x, [Field::ZERO; FULL_SPONGE_QUEUE_STATE_WIDTH])) + .collect(), }, )); From c62d19967c86bac77bb6b508f40c45ccf22339df Mon Sep 17 00:00:00 2001 From: Vladislav Volosnikov Date: Wed, 2 Oct 2024 11:53:22 +0200 Subject: [PATCH 3/3] Cargo fmt --- .../memory_related/ram_permutation.rs | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/crates/zkevm_test_harness/src/witness/individual_circuits/memory_related/ram_permutation.rs b/crates/zkevm_test_harness/src/witness/individual_circuits/memory_related/ram_permutation.rs index 8483532..aa2c38d 100644 --- a/crates/zkevm_test_harness/src/witness/individual_circuits/memory_related/ram_permutation.rs +++ b/crates/zkevm_test_harness/src/witness/individual_circuits/memory_related/ram_permutation.rs @@ -124,9 +124,14 @@ pub(crate) fn compute_ram_circuit_snapshots = all_memory_queries.par_iter().map(|x| x.encoding_witness()).collect(); - let rhs_contributions: Vec<_> = all_memory_queries_sorted.par_iter().map(|x| x.encoding_witness()).collect(); + let lhs_contributions: Vec<_> = all_memory_queries + .par_iter() + .map(|x| x.encoding_witness()) + .collect(); + let rhs_contributions: Vec<_> = all_memory_queries_sorted + .par_iter() + .map(|x| x.encoding_witness()) + .collect(); let challenges = produce_fs_challenges::< Field, @@ -350,8 +355,14 @@ pub(crate) fn compute_ram_circuit_snapshots