Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Simplify ram circuit input #48

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions crates/zkevm_circuits/src/ram_permutation/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,8 @@ pub type RamPermutationCycleInputOutputWitness<F> = crate::fsm_input_output::Clo
pub struct RamPermutationCircuitInstanceWitness<F: SmallField> {
pub closed_form_input: RamPermutationCycleInputOutputWitness<F>,

pub unsorted_queue_witness: FullStateCircuitQueueRawWitness<
F,
MemoryQuery<F>,
FULL_SPONGE_QUEUE_STATE_WIDTH,
MEMORY_QUERY_PACKED_WIDTH,
>,
pub sorted_queue_witness: FullStateCircuitQueueRawWitness<
F,
MemoryQuery<F>,
FULL_SPONGE_QUEUE_STATE_WIDTH,
MEMORY_QUERY_PACKED_WIDTH,
>,
pub unsorted_queue_witness: Vec<<MemoryQuery<F> as CSAllocatable<F>>::Witness>,
pub sorted_queue_witness: Vec<<MemoryQuery<F> as CSAllocatable<F>>::Witness>,
}

pub type MemoryQueriesQueue<F, R> =
Expand Down
17 changes: 15 additions & 2 deletions crates/zkevm_circuits/src/ram_permutation/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -78,7 +81,12 @@ 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
Expand All @@ -104,7 +112,12 @@ 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ pub(crate) fn compute_ram_circuit_snapshots<CB: FnMut(WitnessGenerationArtifact)
let mut rhs_grand_product_chains =
Vec::with_capacity(DEFAULT_NUM_PERMUTATION_ARGUMENT_REPETITIONS);
{
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,
RoundFunction,
Expand All @@ -138,15 +147,6 @@ pub(crate) fn compute_ram_circuit_snapshots<CB: FnMut(WitnessGenerationArtifact)
round_function,
);

let lhs_contributions: Vec<_> = 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(),
Expand Down Expand Up @@ -355,18 +355,14 @@ pub(crate) fn compute_ram_circuit_snapshots<CB: FnMut(WitnessGenerationArtifact)
},
},
// we will need witnesses to pop elements from the front of the queue
unsorted_queue_witness: FullStateCircuitQueueRawWitness {
elements: unsorted_queries_in_chunk
.into_iter()
.map(|x| (x.reflect(), [Field::ZERO; FULL_SPONGE_QUEUE_STATE_WIDTH]))
.collect(),
},
sorted_queue_witness: FullStateCircuitQueueRawWitness {
elements: sorted_queries_in_chunk
.into_iter()
.map(|x| (x.reflect(), [Field::ZERO; FULL_SPONGE_QUEUE_STATE_WIDTH]))
.collect(),
},
unsorted_queue_witness: unsorted_queries_in_chunk
.into_par_iter()
.map(|x| x.reflect())
.collect(),
sorted_queue_witness: sorted_queries_in_chunk
.into_par_iter()
.map(|x| x.reflect())
.collect(),
};

if sorted_states_len % per_circuit_capacity != 0 {
Expand Down
Loading