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

sync-platform: Configure the platform / emulator to match circuits #735

Merged
merged 7 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
47 changes: 28 additions & 19 deletions ceno_emul/src/platform.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::Range;
use std::{collections::HashSet, ops::Range};

use crate::addr::{Addr, RegIdx};

Expand All @@ -9,33 +9,42 @@ use crate::addr::{Addr, RegIdx};
/// - codes of environment calls.
#[derive(Clone, Debug)]
pub struct Platform {
pub rom: Range<Addr>,
pub ram: Range<Addr>,
pub prog_code: Range<Addr>,
pub prog_data: Option<HashSet<Addr>>,
naure marked this conversation as resolved.
Show resolved Hide resolved
pub stack: Range<Addr>,
naure marked this conversation as resolved.
Show resolved Hide resolved
pub heap: Range<Addr>,
naure marked this conversation as resolved.
Show resolved Hide resolved
pub public_io: Range<Addr>,
pub hints: Range<Addr>,
pub stack_top: Addr,
/// If true, ecall instructions are no-op instead of trap. Testing only.
pub unsafe_ecall_nop: bool,
}

pub const CENO_PLATFORM: Platform = Platform {
rom: 0x2000_0000..0x3000_0000,
ram: 0x8000_0000..0xFFFF_0000,
prog_code: 0x2000_0000..0x3000_0000,
prog_data: None, // This is an `Option` to allow `const` here.
stack: 0xB0000000..0xC0000000,
naure marked this conversation as resolved.
Show resolved Hide resolved
heap: 0x8000_0000..0xFFFF_0000,
public_io: 0x3000_1000..0x3000_2000,
hints: 0x4000_0000..0x5000_0000,
stack_top: 0xC0000000,
unsafe_ecall_nop: false,
};

impl Platform {
// Virtual memory layout.

pub fn is_rom(&self, addr: Addr) -> bool {
self.rom.contains(&addr)
pub fn is_prog_code(&self, addr: Addr) -> bool {
self.prog_code.contains(&addr)
naure marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn is_prog_data(&self, addr: Addr) -> bool {
self.prog_data
.as_ref()
.map(|set| set.contains(&(addr & !0x3)))
.unwrap_or(false)
}

pub fn is_ram(&self, addr: Addr) -> bool {
self.ram.contains(&addr)
self.stack.contains(&addr) || self.heap.contains(&addr) || self.is_prog_data(addr)
}

pub fn is_pub_io(&self, addr: Addr) -> bool {
Expand All @@ -60,21 +69,21 @@ impl Platform {
// Startup.

pub const fn pc_base(&self) -> Addr {
self.rom.start
self.prog_code.start
}

// Permissions.

pub fn can_read(&self, addr: Addr) -> bool {
self.is_rom(addr) || self.is_ram(addr) || self.is_pub_io(addr) || self.is_hints(addr)
self.can_write(addr)
}
naure marked this conversation as resolved.
Show resolved Hide resolved

pub fn can_write(&self, addr: Addr) -> bool {
self.is_ram(addr)
self.is_ram(addr) || self.is_pub_io(addr) || self.is_hints(addr)
}
naure marked this conversation as resolved.
Show resolved Hide resolved

pub fn can_execute(&self, addr: Addr) -> bool {
self.is_rom(addr)
naure marked this conversation as resolved.
Show resolved Hide resolved
self.is_prog_code(addr)
}

// Environment calls.
Expand Down Expand Up @@ -115,16 +124,16 @@ mod tests {
let p = CENO_PLATFORM;
assert!(p.can_execute(p.pc_base()));
// ROM and RAM do not overlap.
assert!(!p.is_rom(p.ram.start));
assert!(!p.is_rom(p.ram.end - WORD_SIZE as Addr));
assert!(!p.is_ram(p.rom.start));
assert!(!p.is_ram(p.rom.end - WORD_SIZE as Addr));
assert!(!p.is_prog_code(p.heap.start));
assert!(!p.is_prog_code(p.heap.end - WORD_SIZE as Addr));
assert!(!p.is_ram(p.prog_code.start));
assert!(!p.is_ram(p.prog_code.end - WORD_SIZE as Addr));
// Registers do not overlap with ROM or RAM.
for reg in [
Platform::register_vma(0),
Platform::register_vma(VMState::REG_COUNT - 1),
] {
assert!(!p.is_rom(reg));
assert!(!p.is_prog_code(reg));
assert!(!p.is_ram(reg));
}
}
Expand Down
2 changes: 1 addition & 1 deletion ceno_emul/src/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl StepRecord {
Some(value),
Some(Change::new(value, value)),
Some(WriteOp {
addr: CENO_PLATFORM.ram.start.into(),
addr: CENO_PLATFORM.heap.start.into(),
value: Change {
before: value,
after: value,
Expand Down
2 changes: 1 addition & 1 deletion ceno_emul/src/vm_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl EmuContext for VMState {
tracing::warn!("ecall ignored: syscall_id={}", function);
self.store_register(DecodedInstruction::RD_NULL as RegIdx, 0)?;
// Example ecall effect - any writable address will do.
let addr = (self.platform.stack_top - WORD_SIZE as u32).into();
let addr = (self.platform.stack.end - WORD_SIZE as u32).into();
self.store_memory(addr, self.peek_memory(addr))?;
self.set_pc(ByteAddr(self.pc) + PC_STEP_SIZE);
Ok(true)
Expand Down
15 changes: 12 additions & 3 deletions ceno_emul/tests/test_elf.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::{collections::HashSet, sync::Arc};

use anyhow::Result;
use ceno_emul::{ByteAddr, CENO_PLATFORM, EmuContext, InsnKind, Platform, StepRecord, VMState};
use ceno_emul::{
ByteAddr, CENO_PLATFORM, EmuContext, InsnKind, Platform, Program, StepRecord, VMState,
};

#[test]
fn test_ceno_rt_mini() -> Result<()> {
Expand Down Expand Up @@ -27,7 +31,7 @@ fn test_ceno_rt_mem() -> Result<()> {
let mut state = VMState::new_from_elf(CENO_PLATFORM, program_elf)?;
let _steps = run(&mut state)?;

let value = state.peek_memory(CENO_PLATFORM.ram.start.into());
let value = state.peek_memory(CENO_PLATFORM.heap.start.into());
assert_eq!(value, 6765, "Expected Fibonacci 20, got {}", value);
Ok(())
}
Expand Down Expand Up @@ -60,7 +64,12 @@ fn test_ceno_rt_alloc() -> Result<()> {
#[test]
fn test_ceno_rt_io() -> Result<()> {
let program_elf = ceno_examples::ceno_rt_io;
let mut state = VMState::new_from_elf(CENO_PLATFORM, program_elf)?;
let program = Program::load_elf(program_elf, u32::MAX)?;
let platform = Platform {
prog_data: Some(program.image.keys().copied().collect::<HashSet<u32>>()),
..CENO_PLATFORM
};
let mut state = VMState::new(platform, Arc::new(program));
let _steps = run(&mut state)?;

let all_messages = read_all_messages(&state);
Expand Down
25 changes: 7 additions & 18 deletions ceno_zkvm/benches/fibonacci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use std::{
time::{Duration, Instant},
};

use ceno_emul::{CENO_PLATFORM, Platform, Program, WORD_SIZE};
use ceno_emul::{Platform, Program};
use ceno_zkvm::{
self,
e2e::{Checkpoint, run_e2e_with_checkpoint},
e2e::{Checkpoint, Preset, run_e2e_with_checkpoint, setup_platform},
};
use criterion::*;

Expand All @@ -28,29 +28,20 @@ type Pcs = BasefoldDefault<E>;
type E = GoldilocksExt2;

// Relevant init data for fibonacci run
fn setup() -> (Program, Platform, u32, u32) {
fn setup() -> (Program, Platform) {
let mut file_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
file_path.push("examples/fibonacci.elf");
let stack_size = 32768;
let heap_size = 2097152;
let pub_io_size = 16;
let elf_bytes = fs::read(&file_path).expect("read elf file");
let program = Program::load_elf(&elf_bytes, u32::MAX).unwrap();

let platform = Platform {
// The stack section is not mentioned in ELF headers, so we repeat the constant STACK_TOP here.
stack_top: 0x0020_0400,
rom: program.base_address
..program.base_address + (program.instructions.len() * WORD_SIZE) as u32,
ram: 0x0010_0000..0xFFFF_0000,
unsafe_ecall_nop: true,
..CENO_PLATFORM
};

(program, platform, stack_size, heap_size)
let platform = setup_platform(Preset::Sp1, &program, stack_size, heap_size, pub_io_size);
(program, platform)
}

fn fibonacci_prove(c: &mut Criterion) {
let (program, platform, stack_size, heap_size) = setup();
let (program, platform) = setup();
for max_steps in [1usize << 20, 1usize << 21, 1usize << 22] {
// expand more input size once runtime is acceptable
let mut group = c.benchmark_group(format!("fibonacci_max_steps_{}", max_steps));
Expand All @@ -68,8 +59,6 @@ fn fibonacci_prove(c: &mut Criterion) {
run_e2e_with_checkpoint::<E, Pcs>(
program.clone(),
platform.clone(),
stack_size,
heap_size,
vec![],
max_steps,
Checkpoint::PrepE2EProving,
Expand Down
25 changes: 7 additions & 18 deletions ceno_zkvm/benches/fibonacci_witness.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{fs, path::PathBuf, time::Duration};

use ceno_emul::{CENO_PLATFORM, Platform, Program, WORD_SIZE};
use ceno_emul::{Platform, Program};
use ceno_zkvm::{
self,
e2e::{Checkpoint, run_e2e_with_checkpoint},
e2e::{Checkpoint, Preset, run_e2e_with_checkpoint, setup_platform},
};
use criterion::*;

Expand All @@ -23,29 +23,20 @@ type Pcs = BasefoldDefault<E>;
type E = GoldilocksExt2;

// Relevant init data for fibonacci run
fn setup() -> (Program, Platform, u32, u32) {
fn setup() -> (Program, Platform) {
let mut file_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
file_path.push("examples/fibonacci.elf");
let stack_size = 32768;
let heap_size = 2097152;
let pub_io_size = 16;
let elf_bytes = fs::read(&file_path).expect("read elf file");
let program = Program::load_elf(&elf_bytes, u32::MAX).unwrap();

let platform = Platform {
// The stack section is not mentioned in ELF headers, so we repeat the constant STACK_TOP here.
stack_top: 0x0020_0400,
rom: program.base_address
..program.base_address + (program.instructions.len() * WORD_SIZE) as u32,
ram: 0x0010_0000..0xFFFF_0000,
unsafe_ecall_nop: true,
..CENO_PLATFORM
};

(program, platform, stack_size, heap_size)
let platform = setup_platform(Preset::Sp1, &program, stack_size, heap_size, pub_io_size);
(program, platform)
}

fn fibonacci_witness(c: &mut Criterion) {
let (program, platform, stack_size, heap_size) = setup();
let (program, platform) = setup();

let max_steps = usize::MAX;
let mut group = c.benchmark_group(format!("fib_wit_max_steps_{}", max_steps));
Expand All @@ -63,8 +54,6 @@ fn fibonacci_witness(c: &mut Criterion) {
run_e2e_with_checkpoint::<E, Pcs>(
program.clone(),
platform.clone(),
stack_size,
heap_size,
vec![],
max_steps,
Checkpoint::PrepWitnessGen,
Expand Down
2 changes: 1 addition & 1 deletion ceno_zkvm/examples/riscv_opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fn main() {
})
.collect(),
);
let mem_addresses = CENO_PLATFORM.ram.clone();
let mem_addresses = CENO_PLATFORM.heap.clone();
let io_addresses = CENO_PLATFORM.public_io.clone();

let mut fmt_layer = fmt::layer()
Expand Down
42 changes: 11 additions & 31 deletions ceno_zkvm/src/bin/e2e.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use ceno_emul::{CENO_PLATFORM, IterAddresses, Platform, Program, WORD_SIZE, Word};
use ceno_emul::{IterAddresses, Program, WORD_SIZE, Word};
use ceno_zkvm::{
e2e::{Checkpoint, run_e2e_with_checkpoint},
e2e::{Checkpoint, Preset, run_e2e_with_checkpoint, setup_platform},
with_panic_hook,
};
use clap::{Parser, ValueEnum};
use clap::Parser;
use ff_ext::ff::Field;
use goldilocks::{Goldilocks, GoldilocksExt2};
use itertools::Itertools;
Expand Down Expand Up @@ -51,19 +51,14 @@ struct Args {
heap_size: u32,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
enum Preset {
Ceno,
Sp1,
}

fn main() {
let args = {
let mut args = Args::parse();
args.stack_size = args.stack_size.next_multiple_of(WORD_SIZE as u32);
args.heap_size = args.heap_size.next_multiple_of(WORD_SIZE as u32);
args
};
let pub_io_size = 16; // TODO: configure.

// default filter
let default_filter = EnvFilter::builder()
Expand Down Expand Up @@ -99,29 +94,16 @@ fn main() {
.with(args.profiling.is_none().then_some(default_filter))
.init();

let args = {
let mut args = Args::parse();
args.stack_size = args.stack_size.next_multiple_of(WORD_SIZE as u32);
args.heap_size = args.heap_size.next_multiple_of(WORD_SIZE as u32);
args
};

tracing::info!("Loading ELF file: {}", &args.elf);
let elf_bytes = fs::read(&args.elf).expect("read elf file");
let program = Program::load_elf(&elf_bytes, u32::MAX).unwrap();

let platform = match args.platform {
Preset::Ceno => CENO_PLATFORM,
Preset::Sp1 => Platform {
// The stack section is not mentioned in ELF headers, so we repeat the constant STACK_TOP here.
stack_top: 0x0020_0400,
rom: program.base_address
..program.base_address + (program.instructions.len() * WORD_SIZE) as u32,
ram: 0x0010_0000..0xFFFF_0000,
unsafe_ecall_nop: true,
..CENO_PLATFORM
},
};
let platform = setup_platform(
args.platform,
&program,
args.stack_size,
args.heap_size,
pub_io_size,
);
tracing::info!("Running on platform {:?} {:?}", args.platform, platform);
tracing::info!(
"Stack: {} bytes. Heap: {} bytes.",
Expand All @@ -146,8 +128,6 @@ fn main() {
let (state, _) = run_e2e_with_checkpoint::<E, Pcs>(
program,
platform,
args.stack_size,
args.heap_size,
hints,
max_steps,
Checkpoint::PrepSanityCheck,
Expand Down
Loading
Loading