Skip to content

Commit

Permalink
Merge branch 'master' into fix/program-size
Browse files Browse the repository at this point in the history
  • Loading branch information
naure authored Nov 21, 2024
2 parents 401b83d + 6ea3bee commit 1f68a1f
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ jobs:
env:
RAYON_NUM_THREADS: 8
RUST_LOG: debug
run: cargo run --release --package ceno_zkvm --example fibonacci_elf --target ${{ matrix.target }} --
run: cargo run --release --package ceno_zkvm --bin e2e --target ${{ matrix.target }} -- --platform=sp1 ceno_zkvm/examples/fibonacci.elf
18 changes: 9 additions & 9 deletions ceno_emul/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct Platform {
pub rom_end: Addr,
pub ram_start: Addr,
pub ram_end: Addr,
pub stack_top: Addr,
/// If true, ecall instructions are no-op instead of trap. Testing only.
pub unsafe_ecall_nop: bool,
}
Expand All @@ -19,7 +20,8 @@ pub const CENO_PLATFORM: Platform = Platform {
rom_start: 0x2000_0000,
rom_end: 0x3000_0000 - 1,
ram_start: 0x8000_0000,
ram_end: 0xFFFF_FFFF,
ram_end: 0xFFFF_0000 - 1,
stack_top: 0xC0000000,
unsafe_ecall_nop: false,
};

Expand Down Expand Up @@ -58,15 +60,13 @@ impl Platform {
}

pub const fn ram_end(&self) -> Addr {
if cfg!(feature = "forbid_overflow") {
// (1<<11) - 1 == 0x7ff is the largest positive 'immediate'
// offset we can have in memory instructions.
// So if we stay away from it, we are safe.
assert!(self.ram_end < -(1_i32 << 11) as u32)
}
self.ram_end
- if cfg!(feature = "forbid_overflow") {
// (1<<11) - 1 == 0x7ff is the largest positive 'immediate'
// offset we can have in memory instructions.
// So if we stay away from it, we are safe.
0x7FF
} else {
0
}
}

pub fn is_ram(&self, addr: Addr) -> bool {
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 @@ -182,7 +182,7 @@ fn main() {

let mut vm = VMState::new(CENO_PLATFORM, program.clone());

// init mmio
// init memory mapped IO
for record in &public_io_init {
vm.init_memory(record.addr.into(), record.value);
}
Expand Down
54 changes: 39 additions & 15 deletions ceno_zkvm/examples/fibonacci_elf.rs → ceno_zkvm/src/bin/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ use ceno_zkvm::{
structs::{ZKVMConstraintSystem, ZKVMFixedTraces, ZKVMWitnesses},
tables::{MemFinalRecord, MemInitRecord, ProgramTableCircuit},
};
use clap::Parser;
use clap::{Parser, ValueEnum};
use ff_ext::ff::Field;
use goldilocks::GoldilocksExt2;
use itertools::{Itertools, MinMaxResult, chain, enumerate};
use mpcs::{Basefold, BasefoldRSParams, PolynomialCommitmentScheme};
use std::{
collections::{HashMap, HashSet},
panic,
fs, panic,
time::Instant,
};
use tracing::level_filters::LevelFilter;
use tracing_flame::FlameLayer;
use tracing_subscriber::{EnvFilter, Registry, fmt, layer::SubscriberExt};
use transcript::Transcript;
Expand All @@ -30,9 +31,22 @@ use transcript::Transcript;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// The path to the ELF file to execute.
elf: String,

/// The maximum number of steps to execute the program.
#[arg(short, long)]
max_steps: Option<usize>,

/// The preset configuration to use.
#[arg(short, long, value_enum, default_value_t = Preset::Ceno)]
platform: Preset,
}

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

fn main() {
Expand All @@ -52,24 +66,34 @@ fn main() {
.with_thread_ids(false)
.with_thread_names(false),
)
.with(EnvFilter::from_default_env())
.with(
EnvFilter::builder()
.with_default_directive(LevelFilter::DEBUG.into())
.from_env_lossy(),
)
.with(flame_layer.with_threads_collapsed(true));
tracing::subscriber::set_global_default(subscriber).unwrap();

let sp1_platform = Platform {
rom_start: 0x0020_0800,
rom_end: 0x003f_ffff,
ram_start: 0x0020_0000,
ram_end: 0xffff_ffff,
unsafe_ecall_nop: true,
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_start: 0x0020_0800,
rom_end: 0x003f_ffff,
ram_start: 0x0020_0000,
ram_end: 0xFFFF_0000 - 1,
unsafe_ecall_nop: true,
},
};
// The stack section is not mentioned in ELF headers, so we repeat the constant STACK_TOP here.
const STACK_TOP: u32 = 0x0020_0400;
tracing::info!("Running on platform {:?}", args.platform);

const STACK_SIZE: u32 = 256;
let mut mem_padder = MemPadder::new(sp1_platform.ram_start()..=sp1_platform.ram_end());
let mut mem_padder = MemPadder::new(platform.ram_start()..=platform.ram_end());

let elf_bytes = include_bytes!(r"fibonacci.elf");
let mut vm = VMState::new_from_elf(sp1_platform, elf_bytes).unwrap();
tracing::info!("Loading ELF file: {}", args.elf);
let elf_bytes = fs::read(&args.elf).expect("read elf file");
let mut vm = VMState::new_from_elf(platform, &elf_bytes).unwrap();

// keygen
let pcs_param = Pcs::setup(1 << MAX_NUM_VARIABLES).expect("Basefold PCS setup");
Expand Down Expand Up @@ -101,7 +125,7 @@ fn main() {
});

let stack_addrs = (1..=STACK_SIZE)
.map(|i| STACK_TOP - i * WORD_SIZE as u32)
.map(|i| platform.stack_top - i * WORD_SIZE as u32)
.map(|addr| MemInitRecord { addr, value: 0 });

let mem_init = chain!(program_addrs, stack_addrs).collect_vec();
Expand Down
8 changes: 4 additions & 4 deletions ceno_zkvm/src/tables/ram/ram_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl<E: ExtensionField, NVRAM: NonVolatileTable + Send + Sync + Clone> TableCirc
num_fixed: usize,
io_addrs: &[Addr],
) -> RowMajorMatrix<E::BaseField> {
// assume returned table is well-formed include padding
// assume returned table is well-formed including padding
config.gen_init_state(num_fixed, io_addrs)
}

Expand All @@ -127,7 +127,7 @@ impl<E: ExtensionField, NVRAM: NonVolatileTable + Send + Sync + Clone> TableCirc
_multiplicity: &[HashMap<u64, usize>],
final_cycles: &[Cycle],
) -> Result<RowMajorMatrix<E::BaseField>, ZKVMError> {
// assume returned table is well-formed include padding
// assume returned table is well-formed including padding
config.assign_instances(num_witin, final_cycles)
}
}
Expand All @@ -153,8 +153,8 @@ pub trait DynVolatileRamTable {
}
}

/// DynVolatileRamCircuit initializes and finalizes memory with
/// - at witnessed addresses, in a contiguous range chosen by the prover.
/// DynVolatileRamCircuit initializes and finalizes memory
/// - at witnessed addresses, in a contiguous range chosen by the prover,
/// - with zeros as initial content,
/// - with witnessed final content that the program wrote.
pub struct DynVolatileRamCircuit<E, R>(PhantomData<(E, R)>);
Expand Down
56 changes: 31 additions & 25 deletions ceno_zkvm/src/tables/ram/ram_impl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, marker::PhantomData, mem::MaybeUninit};
use std::{marker::PhantomData, mem::MaybeUninit};

use ceno_emul::{Addr, Cycle};
use ff_ext::ExtensionField;
Expand Down Expand Up @@ -353,41 +353,47 @@ impl<DVRAM: DynVolatileRamTable + Send + Sync + Clone> DynVolatileRamTableConfig
num_witness: usize,
final_mem: &[MemFinalRecord],
) -> Result<RowMajorMatrix<F>, ZKVMError> {
let accessed_addrs = final_mem
.iter()
.cloned()
.map(|record| (record.addr, record))
.collect::<HashMap<_, _>>();
assert!(final_mem.len() <= DVRAM::max_len());
assert!(DVRAM::max_len().is_power_of_two());
let mut final_table = RowMajorMatrix::<F>::new(DVRAM::max_len(), num_witness);
let mut final_table =
RowMajorMatrix::<F>::new(final_mem.len().next_power_of_two(), num_witness);

final_table
.par_iter_mut()
.with_min_len(MIN_PAR_SIZE)
.enumerate()
.for_each(|(i, row)| {
let addr = DVRAM::addr(i);
set_val!(row, self.addr, addr as u64);
if let Some(rec) = accessed_addrs.get(&addr) {
if self.final_v.len() == 1 {
// Assign value directly.
set_val!(row, self.final_v[0], rec.value as u64);
} else {
// Assign value limbs.
self.final_v.iter().enumerate().for_each(|(l, limb)| {
let val = (rec.value >> (l * LIMB_BITS)) & LIMB_MASK;
set_val!(row, limb, val as u64);
});
}
set_val!(row, self.final_cycle, rec.cycle);
.zip(final_mem.into_par_iter())
.for_each(|(row, rec)| {
set_val!(row, self.addr, rec.addr as u64);
if self.final_v.len() == 1 {
// Assign value directly.
set_val!(row, self.final_v[0], rec.value as u64);
} else {
// Assign value limbs.
self.final_v.iter().enumerate().for_each(|(l, limb)| {
let val = (rec.value >> (l * LIMB_BITS)) & LIMB_MASK;
set_val!(row, limb, val as u64);
});
}
set_val!(row, self.final_cycle, rec.cycle);
});

// set padding with well-form address
if final_mem.len().next_power_of_two() - final_mem.len() > 0 {
let paddin_entry_start = final_mem.len();
final_table
.par_iter_mut()
.skip(final_mem.len())
.enumerate()
.with_min_len(MIN_PAR_SIZE)
.for_each(|(i, row)| {
// Assign value limbs.
self.final_v.iter().for_each(|limb| {
set_val!(row, limb, 0u64);
});
set_val!(row, self.addr, DVRAM::addr(paddin_entry_start + i) as u64);
set_val!(row, self.final_cycle, 0_u64);
}
});
});
}

Ok(final_table)
}
Expand Down

0 comments on commit 1f68a1f

Please sign in to comment.