Skip to content
Merged
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion ceno_cli/example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ fn main() {
panic!();
}

ceno_rt::commit(&cnt_primes);
ceno_rt::commit(&cnt_primes.to_le_bytes());
}
43 changes: 7 additions & 36 deletions ceno_cli/src/commands/common_args/ceno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ pub struct CenoOptions {
#[arg(long, value_parser, num_args = 1.., value_delimiter = ',')]
public_io: Option<Vec<Word>>,

/// pub io size in byte
#[arg(long, default_value = "1k", value_parser = parse_size)]
public_io_size: u32,

/// The preset configuration to use.
#[arg(short, long, value_enum, default_value_t = SecurityLevel::default())]
security_level: SecurityLevel,
Expand Down Expand Up @@ -169,25 +165,9 @@ impl CenoOptions {
self.heap_size.next_multiple_of(WORD_SIZE as u32)
}

/// Read the public io into ceno stdin
pub fn read_public_io(&self) -> anyhow::Result<Vec<u32>> {
if let Some(public_io) = &self.public_io {
// if vector contains only one element, write it as a raw `u32`
// otherwise, write the entire vector
// in both cases, convert the resulting `CenoStdin` into a `Vec<u32>`
if public_io.len() == 1 {
CenoStdin::default()
.write(&public_io[0])
.map(|stdin| Into::<Vec<u32>>::into(&*stdin))
} else {
CenoStdin::default()
.write(public_io)
.map(|stdin| Into::<Vec<u32>>::into(&*stdin))
}
.context("failed to get public_io".to_string())
} else {
Ok(vec![])
}
/// Read raw public-io words; digesting happens later in the zkVM pipeline.
pub fn read_public_io(&self) -> Vec<u32> {
self.public_io.clone().unwrap_or_default()
}

/// Read the hints
Expand Down Expand Up @@ -367,32 +347,23 @@ fn run_elf_inner<
options.max_cycle_per_shard,
);

let public_io = options
.read_public_io()
.context("failed to read public io")?;
let public_io_size = options.public_io_size;
assert!(
public_io.len() <= public_io_size as usize / WORD_SIZE,
"require pub io length {} < max public_io_size {}",
public_io.len(),
public_io_size as usize / WORD_SIZE
);
let public_io_digest_input = options.read_public_io();
let public_io_digest = public_io_words_to_digest_words(&public_io_digest_input);
tracing::debug!("public io digest words: {:?}", public_io_digest);

let platform = if compilation_options.release {
setup_platform(
options.platform,
&program,
options.stack_size(),
options.heap_size(),
public_io_size,
)
} else {
setup_platform_debug(
options.platform,
&program,
options.stack_size(),
options.heap_size(),
public_io_size,
)
};
tracing::info!("Running on platform {:?} {}", options.platform, platform);
Expand All @@ -416,7 +387,7 @@ fn run_elf_inner<
platform,
multi_prover,
&hints,
&public_io,
&public_io_digest_input,
options.max_steps,
checkpoint,
options.shard_id.map(|v| v as usize),
Expand Down
13 changes: 10 additions & 3 deletions ceno_cli/src/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,20 @@ where
pub fn generate_base_proof(
&self,
hints: CenoStdin,
pub_io: CenoStdin,
pub_io_digest: &[u32],
max_steps: usize,
shard_id: Option<usize>,
) -> Vec<ZKVMProof<E, PCS>> {
if let Some(zkvm_prover) = self.zkvm_prover.as_ref() {
let init_full_mem = zkvm_prover.setup_init_mem(&Vec::from(&hints), &Vec::from(&pub_io));
run_e2e_proof::<E, PCS, PB, PD>(zkvm_prover, &init_full_mem, max_steps, false, shard_id)
let init_full_mem = zkvm_prover.setup_init_mem(&Vec::from(&hints));
run_e2e_proof::<E, PCS, PB, PD>(
zkvm_prover,
&init_full_mem,
pub_io_digest,
max_steps,
false,
shard_id,
)
} else {
panic!("ZKVMProver is not initialized")
}
Expand Down
2 changes: 1 addition & 1 deletion ceno_emul/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub mod disassemble;
mod syscalls;
pub use syscalls::{
BLS12381_ADD, BLS12381_DECOMPRESS, BLS12381_DOUBLE, BN254_ADD, BN254_DOUBLE, BN254_FP_ADD,
BN254_FP_MUL, BN254_FP2_ADD, BN254_FP2_MUL, KECCAK_PERMUTE, SECP256K1_ADD,
BN254_FP_MUL, BN254_FP2_ADD, BN254_FP2_MUL, KECCAK_PERMUTE, PubIoCommitSpec, SECP256K1_ADD,
SECP256K1_DECOMPRESS, SECP256K1_DOUBLE, SECP256K1_SCALAR_INVERT, SECP256R1_ADD,
SECP256R1_DECOMPRESS, SECP256R1_DOUBLE, SECP256R1_SCALAR_INVERT, SHA_EXTEND,
STATE_CONTINUATION, SyscallSpec, UINT256_MUL,
Expand Down
33 changes: 10 additions & 23 deletions ceno_emul/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::addr::{Addr, RegIdx};
pub struct Platform {
pub rom: Range<Addr>,
pub prog_data: Arc<BTreeSet<Addr>>,
pub public_io: Range<Addr>,

pub stack: Range<Addr>,
pub heap: Range<Addr>,
Expand All @@ -34,7 +33,7 @@ impl Display for Platform {
write!(
f,
"Platform {{ rom: {:#x}..{:#x}, prog_data: {:#x}..{:#x}, stack: {:#x}..{:#x}, heap: {:#x}..{:#x}, \
public_io: {:#x}..{:#x}, hints: {:#x}..{:#x}, unsafe_ecall_nop: {} }}",
hints: {:#x}..{:#x}, unsafe_ecall_nop: {} }}",
self.rom.start,
self.rom.end,
prog_data
Expand All @@ -49,8 +48,6 @@ impl Display for Platform {
self.stack.end,
self.heap.start,
self.heap.end,
self.public_io.start,
self.public_io.end,
self.hints.start,
self.hints.end,
self.unsafe_ecall_nop
Expand Down Expand Up @@ -81,21 +78,21 @@ impl Display for Platform {
// │ STACK (≈128 MB, grows downward)
// │ 0x1800_0000 .. 0x2000_0000
// │
// ├───────────────────────────── 0x1800_0000 (stack base / pubio end)
// ├───────────────────────────── 0x1800_0000 (stack base)
// │
// │ PUBLIC I/O (128 MB)
// │ 0x1000_0000 .. 0x1800_0000
// │ STACK (stack-only memory window, includes reserved low-address area
// │ previously used for PUBLIC I/O)
// │ 0x1000_0000 .. 0x2000_0000
// │
// ├───────────────────────────── 0x1000_0000 (pubio base / rom end)
// ├───────────────────────────── 0x1000_0000 (stack base / rom end)
// │
// │ ROM / TEXT / RODATA (128 MB)
// │ 0x0800_0000 .. 0x1000_0000
// │
// └───────────────────────────── 0x8000_0000 (rom base)
pub static CENO_PLATFORM: Lazy<Platform> = Lazy::new(|| Platform {
rom: 0x0800_0000..0x1000_0000, // 128 MB
public_io: 0x1000_0000..0x1800_0000, // 128 MB
stack: 0x1800_0000..0x2000_4000, // stack grows downward 128MB, 0x4000 reserved for debug io.
rom: 0x0800_0000..0x1000_0000, // 128 MB
stack: 0x1000_0000..0x2000_4000, // stack grows downward, 0x4000 reserved for debug io.
// we make hints start from 0x2800_0000 thus reserve a 128MB gap for debug io
// at the end of stack
hints: 0x2800_0000..0x3000_0000, // 128 MB
Expand Down Expand Up @@ -123,10 +120,6 @@ impl Platform {
self.stack.contains(&addr) || self.heap.contains(&addr) || self.is_prog_data(addr)
}

pub fn is_pub_io(&self, addr: Addr) -> bool {
self.public_io.contains(&addr)
}

pub fn is_hints(&self, addr: Addr) -> bool {
self.hints.contains(&addr)
}
Expand Down Expand Up @@ -155,7 +148,7 @@ impl Platform {
}

pub fn can_write(&self, addr: Addr) -> bool {
self.is_ram(addr) || self.is_pub_io(addr) || self.is_hints(addr)
self.is_ram(addr) || self.is_hints(addr)
}

// Environment calls.
Expand Down Expand Up @@ -187,13 +180,7 @@ impl Platform {

/// Validate the platform configuration, range shall not overlap.
pub fn validate(&self) -> bool {
let mut ranges = [
&self.rom,
&self.stack,
&self.heap,
&self.public_io,
&self.hints,
];
let mut ranges = [&self.rom, &self.stack, &self.heap, &self.hints];
ranges.sort_by_key(|r| r.start);
for i in 0..ranges.len() - 1 {
if ranges[i].end > ranges[i + 1].start {
Expand Down
7 changes: 5 additions & 2 deletions ceno_emul/src/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::Result;
pub mod bn254;
pub mod keccak_permute;
pub mod phantom;
pub mod pubio_commit;
pub mod secp256k1;
pub(crate) mod secp256r1;
pub mod sha256;
Expand All @@ -14,10 +15,11 @@ pub mod uint256;
pub use ceno_syscall::{
BLS12381_ADD, BLS12381_DECOMPRESS, BLS12381_DOUBLE, BN254_ADD, BN254_DOUBLE, BN254_FP_ADD,
BN254_FP_MUL, BN254_FP2_ADD, BN254_FP2_MUL, KECCAK_PERMUTE, PHANTOM_LOG_PC_CYCLE,
SECP256K1_ADD, SECP256K1_DECOMPRESS, SECP256K1_DOUBLE, SECP256K1_SCALAR_INVERT, SECP256R1_ADD,
SECP256R1_DECOMPRESS, SECP256R1_DOUBLE, SECP256R1_SCALAR_INVERT, SHA_EXTEND,
PUB_IO_COMMIT, SECP256K1_ADD, SECP256K1_DECOMPRESS, SECP256K1_DOUBLE, SECP256K1_SCALAR_INVERT,
SECP256R1_ADD, SECP256R1_DECOMPRESS, SECP256R1_DOUBLE, SECP256R1_SCALAR_INVERT, SHA_EXTEND,
STATE_CONTINUATION, UINT256_MUL,
};
pub use pubio_commit::PubIoCommitSpec;

pub trait SyscallSpec {
const NAME: &'static str;
Expand Down Expand Up @@ -50,6 +52,7 @@ pub fn handle_syscall<T: Tracer>(vm: &VMState<T>, function_code: u32) -> Result<
BN254_FP2_ADD => Ok(bn254::bn254_fp2_add(vm)),
BN254_FP2_MUL => Ok(bn254::bn254_fp2_mul(vm)),
UINT256_MUL => Ok(uint256::uint256_mul(vm)),
PUB_IO_COMMIT => Ok(pubio_commit::pubio_commit(vm)),

// phantom syscall
PHANTOM_LOG_PC_CYCLE => Ok(phantom::log_pc_cycle(vm)),
Expand Down
33 changes: 33 additions & 0 deletions ceno_emul/src/syscalls/pubio_commit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::{Change, EmuContext, Platform, Tracer, VMState, WriteOp, utils::MemoryView};

use super::{SyscallEffects, SyscallSpec, SyscallWitness};

const PUBIO_COMMIT_WORDS: usize = 8;

pub struct PubIoCommitSpec;
impl SyscallSpec for PubIoCommitSpec {
const NAME: &'static str = "PUB_IO_COMMIT";
const REG_OPS_COUNT: usize = 1;
const MEM_OPS_COUNT: usize = PUBIO_COMMIT_WORDS;
const CODE: u32 = ceno_syscall::PUB_IO_COMMIT;
}

/// Trace the PUB_IO_COMMIT syscall by reading 8 digest words from guest memory.
pub fn pubio_commit<T: Tracer>(vm: &VMState<T>) -> SyscallEffects {
let digest_ptr = vm.peek_register(Platform::reg_arg0());

let reg_ops = vec![WriteOp::new_register_op(
Platform::reg_arg0(),
Change::new(digest_ptr, digest_ptr),
0,
)];

let digest_view = MemoryView::<_, PUBIO_COMMIT_WORDS>::new(vm, digest_ptr);
let mem_ops = digest_view.mem_ops().to_vec();

assert_eq!(mem_ops.len(), PubIoCommitSpec::MEM_OPS_COUNT);
SyscallEffects {
witness: SyscallWitness::new(mem_ops, reg_ops),
next_pc: None,
}
}
13 changes: 1 addition & 12 deletions ceno_host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,33 +111,22 @@ impl CenoStdin {
}
}

pub fn run(
platform: Platform,
elf: &[u8],
hints: &CenoStdin,
public_io: Option<&CenoStdin>,
) -> Vec<Vec<u8>> {
pub fn run(platform: Platform, elf: &[u8], hints: &CenoStdin) -> Vec<Vec<u8>> {
let program = Program::load_elf(elf, u32::MAX).unwrap();
let platform = Platform {
prog_data: Arc::new(program.image.keys().copied().collect()),
..platform
};

let hints: Vec<u32> = hints.into();
let pubio: Vec<u32> = public_io.map(|c| c.into()).unwrap_or_default();
let hints_range = platform.hints.clone();
let pubio_range = platform.public_io.clone();

let mut state = VMState::new(platform, Arc::new(program));

for (addr, value) in zip(hints_range.iter_addresses(), hints) {
state.init_memory(addr.into(), value);
}

for (addr, value) in zip(pubio_range.iter_addresses(), pubio) {
state.init_memory(addr.into(), value);
}

state
.iter_until_halt()
.collect::<Result<Vec<_>>>()
Expand Down
Loading
Loading