Skip to content

Commit

Permalink
Simplify ram permutation circuit witness
Browse files Browse the repository at this point in the history
  • Loading branch information
0xVolosnikov committed Sep 30, 2024
1 parent 53e6cec commit f341c38
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 39 deletions.
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
11 changes: 9 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,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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions crates/zkevm_test_harness/src/tests/complex_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ pub(crate) fn generate_base_layer(
let mut sorted_memory_queue_witnesses = sorted_memory_queue_witnesses.into_iter();

Check warning on line 302 in crates/zkevm_test_harness/src/tests/complex_tests/mod.rs

View workflow job for this annotation

GitHub Actions / cargo build

unused variable: `sorted_memory_queue_witnesses`

Check warning on line 302 in crates/zkevm_test_harness/src/tests/complex_tests/mod.rs

View workflow job for this annotation

GitHub Actions / cargo build

variable does not need to be mutable

Check warning on line 302 in crates/zkevm_test_harness/src/tests/complex_tests/mod.rs

View workflow job for this annotation

GitHub Actions / cargo test

unused variable: `sorted_memory_queue_witnesses`

Check warning on line 302 in crates/zkevm_test_harness/src/tests/complex_tests/mod.rs

View workflow job for this annotation

GitHub Actions / cargo test

variable does not need to be mutable
for el in basic_block_circuits.iter_mut() {

Check warning on line 303 in crates/zkevm_test_harness/src/tests/complex_tests/mod.rs

View workflow job for this annotation

GitHub Actions / cargo build

unused variable: `el`

Check warning on line 303 in crates/zkevm_test_harness/src/tests/complex_tests/mod.rs

View workflow job for this annotation

GitHub Actions / cargo test

unused variable: `el`
use circuit_definitions::boojum::field::Field;
match &el {
/*match &el {
ZkSyncBaseLayerCircuit::RAMPermutation(inner) => {
let mut witness = inner.witness.take().unwrap();
let zero_state = [GoldilocksField::ZERO; FULL_SPONGE_QUEUE_STATE_WIDTH];
Expand All @@ -326,7 +326,7 @@ pub(crate) fn generate_base_layer(
inner.witness.store(Some(witness));
}
_ => {}
}
}*/
}

(
Expand Down
4 changes: 2 additions & 2 deletions crates/zkevm_test_harness/src/tests/run_manually.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ pub(crate) fn run_with_options(entry_point_bytecode: Vec<[u8; 32]>, options: Opt
for el in basic_block_circuits {
println!("Doing {} circuit", el.short_description());
use circuit_definitions::boojum::field::Field;
match &el {
/*match &el {
ZkSyncBaseLayerCircuit::RAMPermutation(inner) => {
let mut witness = inner.witness.take().unwrap();
let zero_state = [GoldilocksField::ZERO; FULL_SPONGE_QUEUE_STATE_WIDTH];
Expand All @@ -348,7 +348,7 @@ pub(crate) fn run_with_options(entry_point_bytecode: Vec<[u8; 32]>, options: Opt
inner.witness.store(Some(witness));
}
_ => {}
}
}*/
base_test_circuit(el);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ 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 +142,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 +350,8 @@ 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

0 comments on commit f341c38

Please sign in to comment.