Skip to content

Commit

Permalink
feat: Add skip_deferred_proof_verification (#1760)
Browse files Browse the repository at this point in the history
  • Loading branch information
ratankaliani committed Nov 20, 2024
1 parent ff8f482 commit 516fdfe
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 12 deletions.
20 changes: 18 additions & 2 deletions crates/core/executor/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ pub struct SP1Context<'a> {

/// The maximum number of cpu cycles to use for execution.
pub max_cycles: Option<u64>,

/// Skip deferred proof verification.
pub skip_deferred_proof_verification: bool,
}

/// A builder for [`SP1Context`].
Expand All @@ -30,6 +33,7 @@ pub struct SP1ContextBuilder<'a> {
hook_registry_entries: Vec<(u32, BoxedHook<'a>)>,
subproof_verifier: Option<Arc<dyn SubproofVerifier + 'a>>,
max_cycles: Option<u64>,
skip_deferred_proof_verification: bool,
}

impl<'a> SP1Context<'a> {
Expand Down Expand Up @@ -68,7 +72,13 @@ impl<'a> SP1ContextBuilder<'a> {
});
let subproof_verifier = take(&mut self.subproof_verifier);
let cycle_limit = take(&mut self.max_cycles);
SP1Context { hook_registry, subproof_verifier, max_cycles: cycle_limit }
let skip_deferred_proof_verification = take(&mut self.skip_deferred_proof_verification);
SP1Context {
hook_registry,
subproof_verifier,
max_cycles: cycle_limit,
skip_deferred_proof_verification,
}
}

/// Add a runtime [Hook](super::Hook) into the context.
Expand Down Expand Up @@ -110,6 +120,12 @@ impl<'a> SP1ContextBuilder<'a> {
self.max_cycles = Some(max_cycles);
self
}

/// Set the skip deferred proof verification flag.
pub fn set_skip_deferred_proof_verification(&mut self, skip: bool) -> &mut Self {
self.skip_deferred_proof_verification = skip;
self
}
}

#[cfg(test)]
Expand All @@ -120,7 +136,7 @@ mod tests {

#[test]
fn defaults() {
let SP1Context { hook_registry, subproof_verifier, max_cycles: cycle_limit } =
let SP1Context { hook_registry, subproof_verifier, max_cycles: cycle_limit, .. } =
SP1Context::builder().build();
assert!(hook_registry.is_none());
assert!(subproof_verifier.is_none());
Expand Down
17 changes: 17 additions & 0 deletions crates/core/executor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ use crate::{
Instruction, Opcode, Program, Register,
};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// Whether to verify deferred proofs during execution.
pub enum DeferredProofVerification {
/// Verify deferred proofs during execution.
Enabled,
/// Skip verification of deferred proofs
Disabled,
}

/// An executor for the SP1 RISC-V zkVM.
///
/// The exeuctor is responsible for executing a user program and tracing important events which
Expand Down Expand Up @@ -78,6 +87,9 @@ pub struct Executor<'a> {
/// The maximum number of cpu cycles to use for execution.
pub max_cycles: Option<u64>,

/// Skip deferred proof verification.
pub deferred_proof_verification: DeferredProofVerification,

/// The state of the execution.
pub state: ExecutionState,

Expand Down Expand Up @@ -228,6 +240,11 @@ impl<'a> Executor<'a> {
hook_registry,
opts,
max_cycles: context.max_cycles,
deferred_proof_verification: if context.skip_deferred_proof_verification {
DeferredProofVerification::Disabled
} else {
DeferredProofVerification::Enabled
},
memory_checkpoint: PagedMemory::new_preallocated(),
uninitialized_memory_checkpoint: PagedMemory::new_preallocated(),
local_memory_access: HashMap::new(),
Expand Down
28 changes: 18 additions & 10 deletions crates/core/executor/src/syscalls/verify.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::DeferredProofVerification;

use super::{Syscall, SyscallCode, SyscallContext};

pub(crate) struct VerifySyscall;
Expand Down Expand Up @@ -32,16 +34,22 @@ impl Syscall for VerifySyscall {
let vkey_bytes: [u32; 8] = vkey.try_into().unwrap();
let pv_digest_bytes: [u32; 8] = pv_digest.try_into().unwrap();

ctx.rt
.subproof_verifier
.verify_deferred_proof(proof, proof_vk, vkey_bytes, pv_digest_bytes)
.unwrap_or_else(|e| {
panic!(
"Failed to verify proof {proof_index} with digest {}: {}",
hex::encode(bytemuck::cast_slice(&pv_digest_bytes)),
e
)
});
// Skip deferred proof verification if the corresponding runtime flag is set.
match ctx.rt.deferred_proof_verification {
DeferredProofVerification::Enabled => {
ctx.rt
.subproof_verifier
.verify_deferred_proof(proof, proof_vk, vkey_bytes, pv_digest_bytes)
.unwrap_or_else(|e| {
panic!(
"Failed to verify proof {proof_index} with digest {}: {}",
hex::encode(bytemuck::cast_slice(&pv_digest_bytes)),
e
)
});
}
DeferredProofVerification::Disabled => {}
}

None
}
Expand Down
12 changes: 12 additions & 0 deletions crates/sdk/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ impl<'a> Execute<'a> {
self.context_builder.max_cycles(max_cycles);
self
}

/// Skip deferred proof verification.
pub fn set_skip_deferred_proof_verification(mut self, value: bool) -> Self {
self.context_builder.set_skip_deferred_proof_verification(value);
self
}
}

/// Builder to prepare and configure proving execution of a program on an input.
Expand Down Expand Up @@ -217,4 +223,10 @@ impl<'a> Prove<'a> {
self.timeout = Some(timeout);
self
}

/// Set the skip deferred proof verification flag.
pub fn set_skip_deferred_proof_verification(mut self, value: bool) -> Self {
self.context_builder.set_skip_deferred_proof_verification(value);
self
}
}

0 comments on commit 516fdfe

Please sign in to comment.