Skip to content

Commit

Permalink
feat: reduce network prover (#687)
Browse files Browse the repository at this point in the history
  • Loading branch information
ctian1 authored May 14, 2024
1 parent 933df11 commit e2decbf
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 105 deletions.
17 changes: 11 additions & 6 deletions core/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use std::collections::HashMap;
use std::fs::File;
use std::io::BufWriter;
use std::io::Write;
use std::rc::Rc;
use std::sync::Arc;

use thiserror::Error;
Expand Down Expand Up @@ -76,7 +75,7 @@ pub struct Runtime {

pub(crate) unconstrained_state: ForkState,

pub syscall_map: HashMap<SyscallCode, Rc<dyn Syscall>>,
pub syscall_map: HashMap<SyscallCode, Arc<dyn Syscall>>,

pub max_syscall_cycles: u32,

Expand Down Expand Up @@ -132,7 +131,7 @@ impl Runtime {
program,
memory_accesses: MemoryAccessRecord::default(),
shard_size: shard_size * 4,
shard_batch_size: env::shard_batch_size() as u32 * shard_size,
shard_batch_size: env::shard_batch_size() as u32,
cycle_tracker: HashMap::new(),
io_buf: HashMap::new(),
trace_buf,
Expand Down Expand Up @@ -937,6 +936,12 @@ impl Runtime {
tracing::info!("starting execution");
}

pub fn run_untraced(&mut self) -> Result<(), ExecutionError> {
self.emit_events = false;
while !self.execute()? {}
Ok(())
}

pub fn run(&mut self) -> Result<(), ExecutionError> {
self.emit_events = true;
while !self.execute()? {}
Expand Down Expand Up @@ -965,10 +970,10 @@ impl Runtime {
break;
}

if env::shard_batch_size() > 0 && current_shard != self.state.current_shard {
if self.shard_batch_size > 0 && current_shard != self.state.current_shard {
num_shards_executed += 1;
current_shard = self.state.current_shard;
if num_shards_executed == env::shard_batch_size() {
if num_shards_executed == self.shard_batch_size {
break;
}
}
Expand Down Expand Up @@ -1040,7 +1045,7 @@ impl Runtime {
}
}

fn get_syscall(&mut self, code: SyscallCode) -> Option<&Rc<dyn Syscall>> {
fn get_syscall(&mut self, code: SyscallCode) -> Option<&Arc<dyn Syscall>> {
self.syscall_map.get(&code)
}
}
Expand Down
62 changes: 31 additions & 31 deletions core/src/runtime/syscall.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::Arc;

use strum_macros::EnumIter;

Expand Down Expand Up @@ -149,7 +149,7 @@ impl SyscallCode {
}
}

pub trait Syscall {
pub trait Syscall: Send + Sync {
/// Execute the syscall and return the resulting value of register a0. `arg1` and `arg2` are the
/// values in registers X10 and X11, respectively. While not a hard requirement, the convention
/// is that the return value is only for system calls such as `HALT`. Most precompiles use `arg1`
Expand Down Expand Up @@ -256,86 +256,86 @@ impl<'a> SyscallContext<'a> {
}
}

pub fn default_syscall_map() -> HashMap<SyscallCode, Rc<dyn Syscall>> {
let mut syscall_map = HashMap::<SyscallCode, Rc<dyn Syscall>>::default();
syscall_map.insert(SyscallCode::HALT, Rc::new(SyscallHalt {}));
syscall_map.insert(SyscallCode::SHA_EXTEND, Rc::new(ShaExtendChip::new()));
syscall_map.insert(SyscallCode::SHA_COMPRESS, Rc::new(ShaCompressChip::new()));
pub fn default_syscall_map() -> HashMap<SyscallCode, Arc<dyn Syscall>> {
let mut syscall_map = HashMap::<SyscallCode, Arc<dyn Syscall>>::default();
syscall_map.insert(SyscallCode::HALT, Arc::new(SyscallHalt {}));
syscall_map.insert(SyscallCode::SHA_EXTEND, Arc::new(ShaExtendChip::new()));
syscall_map.insert(SyscallCode::SHA_COMPRESS, Arc::new(ShaCompressChip::new()));
syscall_map.insert(
SyscallCode::ED_ADD,
Rc::new(EdAddAssignChip::<Ed25519>::new()),
Arc::new(EdAddAssignChip::<Ed25519>::new()),
);
syscall_map.insert(
SyscallCode::ED_DECOMPRESS,
Rc::new(EdDecompressChip::<Ed25519Parameters>::new()),
Arc::new(EdDecompressChip::<Ed25519Parameters>::new()),
);
syscall_map.insert(
SyscallCode::KECCAK_PERMUTE,
Rc::new(KeccakPermuteChip::new()),
Arc::new(KeccakPermuteChip::new()),
);
syscall_map.insert(
SyscallCode::SECP256K1_ADD,
Rc::new(WeierstrassAddAssignChip::<Secp256k1>::new()),
Arc::new(WeierstrassAddAssignChip::<Secp256k1>::new()),
);
syscall_map.insert(
SyscallCode::SECP256K1_DOUBLE,
Rc::new(WeierstrassDoubleAssignChip::<Secp256k1>::new()),
Arc::new(WeierstrassDoubleAssignChip::<Secp256k1>::new()),
);
syscall_map.insert(SyscallCode::SHA_COMPRESS, Rc::new(ShaCompressChip::new()));
syscall_map.insert(SyscallCode::SHA_COMPRESS, Arc::new(ShaCompressChip::new()));
syscall_map.insert(
SyscallCode::SECP256K1_DECOMPRESS,
Rc::new(WeierstrassDecompressChip::<Secp256k1>::new()),
Arc::new(WeierstrassDecompressChip::<Secp256k1>::new()),
);
syscall_map.insert(
SyscallCode::BN254_ADD,
Rc::new(WeierstrassAddAssignChip::<Bn254>::new()),
Arc::new(WeierstrassAddAssignChip::<Bn254>::new()),
);
syscall_map.insert(
SyscallCode::BN254_DOUBLE,
Rc::new(WeierstrassDoubleAssignChip::<Bn254>::new()),
Arc::new(WeierstrassDoubleAssignChip::<Bn254>::new()),
);
syscall_map.insert(
SyscallCode::BLAKE3_COMPRESS_INNER,
Rc::new(Blake3CompressInnerChip::new()),
Arc::new(Blake3CompressInnerChip::new()),
);
syscall_map.insert(
SyscallCode::BLS12381_ADD,
Rc::new(WeierstrassAddAssignChip::<Bls12381>::new()),
Arc::new(WeierstrassAddAssignChip::<Bls12381>::new()),
);
syscall_map.insert(
SyscallCode::BLS12381_DOUBLE,
Rc::new(WeierstrassDoubleAssignChip::<Bls12381>::new()),
Arc::new(WeierstrassDoubleAssignChip::<Bls12381>::new()),
);
syscall_map.insert(
SyscallCode::BLAKE3_COMPRESS_INNER,
Rc::new(Blake3CompressInnerChip::new()),
Arc::new(Blake3CompressInnerChip::new()),
);
syscall_map.insert(SyscallCode::UINT256_MUL, Rc::new(Uint256MulChip::new()));
syscall_map.insert(SyscallCode::UINT256_MUL, Arc::new(Uint256MulChip::new()));
syscall_map.insert(
SyscallCode::ENTER_UNCONSTRAINED,
Rc::new(SyscallEnterUnconstrained::new()),
Arc::new(SyscallEnterUnconstrained::new()),
);
syscall_map.insert(
SyscallCode::EXIT_UNCONSTRAINED,
Rc::new(SyscallExitUnconstrained::new()),
Arc::new(SyscallExitUnconstrained::new()),
);
syscall_map.insert(SyscallCode::WRITE, Rc::new(SyscallWrite::new()));
syscall_map.insert(SyscallCode::COMMIT, Rc::new(SyscallCommit::new()));
syscall_map.insert(SyscallCode::WRITE, Arc::new(SyscallWrite::new()));
syscall_map.insert(SyscallCode::COMMIT, Arc::new(SyscallCommit::new()));
syscall_map.insert(
SyscallCode::COMMIT_DEFERRED_PROOFS,
Rc::new(SyscallCommitDeferred::new()),
Arc::new(SyscallCommitDeferred::new()),
);
syscall_map.insert(
SyscallCode::VERIFY_SP1_PROOF,
Rc::new(SyscallVerifySP1Proof::new()),
Arc::new(SyscallVerifySP1Proof::new()),
);
syscall_map.insert(SyscallCode::HINT_LEN, Rc::new(SyscallHintLen::new()));
syscall_map.insert(SyscallCode::HINT_READ, Rc::new(SyscallHintRead::new()));
syscall_map.insert(SyscallCode::HINT_LEN, Arc::new(SyscallHintLen::new()));
syscall_map.insert(SyscallCode::HINT_READ, Arc::new(SyscallHintRead::new()));
syscall_map.insert(
SyscallCode::BLS12381_DECOMPRESS,
Rc::new(WeierstrassDecompressChip::<Bls12381>::new()),
Arc::new(WeierstrassDecompressChip::<Bls12381>::new()),
);
syscall_map.insert(SyscallCode::UINT256_MUL, Rc::new(Uint256MulChip::new()));
syscall_map.insert(SyscallCode::UINT256_MUL, Arc::new(Uint256MulChip::new()));

syscall_map
}
Expand Down
Loading

0 comments on commit e2decbf

Please sign in to comment.