Skip to content

Commit

Permalink
added local memory chips to the risc-v machine
Browse files Browse the repository at this point in the history
  • Loading branch information
kevjue committed Aug 9, 2024
1 parent 1a2a6e9 commit c1451e1
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 32 deletions.
20 changes: 16 additions & 4 deletions core/src/memory/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ impl<F: PrimeField32> MachineAir<F> for MemoryChip {

fn name(&self) -> String {
match self.kind {
MemoryChipType::Initialize => "MemoryInit".to_string(),
MemoryChipType::Finalize => "MemoryFinalize".to_string(),
MemoryChipType::Initialize => "MemoryGlobalInit".to_string(),
MemoryChipType::Finalize => "MemoryGlobalFinalize".to_string(),
}
}

Expand All @@ -56,8 +56,20 @@ impl<F: PrimeField32> MachineAir<F> for MemoryChip {
_output: &mut ExecutionRecord,
) -> RowMajorMatrix<F> {
let mut memory_events = match self.kind {
MemoryChipType::Initialize => input.global_memory_initialize_events.clone(),
MemoryChipType::Finalize => input.global_memory_finalize_events.clone(),
MemoryChipType::Initialize => {
println!(
"Global memory initialize table: {:?}",
input.global_memory_initialize_events.len()
);
input.global_memory_initialize_events.clone()
}
MemoryChipType::Finalize => {
println!(
"Global memory finalize table: {:?}",
input.global_memory_finalize_events.len()
);
input.global_memory_finalize_events.clone()
}
};

let previous_addr_bits = match self.kind {
Expand Down
16 changes: 14 additions & 2 deletions core/src/memory/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,20 @@ impl<F: PrimeField32> MachineAir<F> for MemoryLocalChip {
_output: &mut ExecutionRecord,
) -> RowMajorMatrix<F> {
let local_memory_accesses = match self.kind {
MemoryChipType::Initialize => &input.local_memory_initialize_access,
MemoryChipType::Finalize => &input.local_memory_finalize_access,
MemoryChipType::Initialize => {
println!(
"Local memory initialize table: {:?}",
input.local_memory_initialize_access.len()
);
&input.local_memory_initialize_access
}
MemoryChipType::Finalize => {
println!(
"Local memory finalize table: {:?}",
input.local_memory_finalize_access.len()
);
&input.local_memory_finalize_access
}
};

let mut rows =
Expand Down
1 change: 1 addition & 0 deletions core/src/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod trace;

pub use columns::*;
pub use global::*;
pub use local::*;
pub use program::*;

use serde::{Deserialize, Serialize};
Expand Down
22 changes: 15 additions & 7 deletions core/src/stark/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub(crate) mod riscv_chips {
pub use crate::alu::ShiftRightChip;
pub use crate::bytes::ByteChip;
pub use crate::cpu::CpuChip;
pub use crate::memory::MemoryChip;
pub use crate::memory::{MemoryChip, MemoryLocalChip};
pub use crate::program::ProgramChip;
pub use crate::syscall::precompiles::edwards::EdAddAssignChip;
pub use crate::syscall::precompiles::edwards::EdDecompressChip;
Expand Down Expand Up @@ -66,9 +66,13 @@ pub enum RiscvAir<F: PrimeField32> {
/// A lookup table for byte operations.
ByteLookup(ByteChip<F>),
/// A table for initializing the memory state.
MemoryInit(MemoryChip),
MemoryGlobalInit(MemoryChip),
/// A table for finalizing the memory state.
MemoryFinal(MemoryChip),
MemoryGlobalFinal(MemoryChip),
/// A table for initializing the memory state.
MemoryLocalInit(MemoryLocalChip),
/// A table for finalizing the memory state.
MemoryLocalFinal(MemoryLocalChip),
/// A table for initializing the program memory.
ProgramMemory(MemoryProgramChip),
/// A precompile for sha256 extend.
Expand Down Expand Up @@ -165,10 +169,14 @@ impl<F: PrimeField32> RiscvAir<F> {
chips.push(RiscvAir::ShiftLeft(shift_left));
let lt = LtChip::default();
chips.push(RiscvAir::Lt(lt));
let memory_init = MemoryChip::new(MemoryChipType::Initialize);
chips.push(RiscvAir::MemoryInit(memory_init));
let memory_finalize = MemoryChip::new(MemoryChipType::Finalize);
chips.push(RiscvAir::MemoryFinal(memory_finalize));
let memory_global_init = MemoryChip::new(MemoryChipType::Initialize);
chips.push(RiscvAir::MemoryGlobalInit(memory_global_init));
let memory_global_finalize = MemoryChip::new(MemoryChipType::Finalize);
chips.push(RiscvAir::MemoryGlobalFinal(memory_global_finalize));
let memory_local_init = MemoryLocalChip::new(MemoryChipType::Initialize);
chips.push(RiscvAir::MemoryLocalInit(memory_local_init));
let memory_local_finalize = MemoryLocalChip::new(MemoryChipType::Finalize);
chips.push(RiscvAir::MemoryLocalFinal(memory_local_finalize));
let program_memory_init = MemoryProgramChip::new();
chips.push(RiscvAir::ProgramMemory(program_memory_init));
let byte = ByteChip::default();
Expand Down
20 changes: 18 additions & 2 deletions core/src/utils/prove.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use p3_matrix::Matrix;
use std::collections::VecDeque;
use std::fs::File;
use std::io::Seek;
Expand Down Expand Up @@ -349,10 +350,25 @@ where

// Commit to each shard.
let commitments = records
.into_par_iter()
.zip(traces.into_par_iter())
.into_iter()
.zip(traces.into_iter())
.map(|(record, traces)| {
let _span = span.enter();

// let chips = prover.machine().shard_chips(&record).collect::<Vec<_>>();
for (name, trace) in traces.clone() {
let trace_width = trace.width();
let trace_height = trace.height();
tracing::debug!(
"Phase 1 area: {:<15} | Main Cols = {:<5} | Rows = {:<5} | Cells = {:<10}",
name,
trace_width,
trace_height,
trace_width * trace_height,
);

}

let data = prover.commit(record, traces);
let main_commit = data.main_commit.clone();
drop(data);
Expand Down
26 changes: 13 additions & 13 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/tendermint/program/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified examples/tendermint/program/elf/riscv32im-succinct-zkvm-elf
Binary file not shown.
4 changes: 2 additions & 2 deletions examples/tendermint/script/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ async fn get_light_blocks() -> (LightBlock, LightBlock) {
let latest_commit = fetch_latest_commit(&client, &url).await.unwrap();
let block: u64 = latest_commit.result.signed_header.header.height.into();
println!("Latest block: {}", block);
let light_block_1 = fetch_light_block(2279100, peer_id, BASE_URL)
let light_block_1 = fetch_light_block(2439100, peer_id, BASE_URL)
.await
.expect("Failed to generate light block 1");
let light_block_2 = fetch_light_block(2279130, peer_id, BASE_URL)
let light_block_2 = fetch_light_block(2439130, peer_id, BASE_URL)
.await
.expect("Failed to generate light block 2");
(light_block_1, light_block_2)
Expand Down

0 comments on commit c1451e1

Please sign in to comment.