-
Notifications
You must be signed in to change notification settings - Fork 580
fix: enforce deferred proof verification #2455
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
Open
0xernesto
wants to merge
4
commits into
dev
Choose a base branch
from
ernesto/read-deferred-proofs
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -253,6 +253,29 @@ | |
/// The unconstrained cycle limit was exceeded. | ||
#[error("unconstrained cycle limit exceeded")] | ||
UnconstrainedCycleLimitExceeded(u64), | ||
|
||
/// Not all deferred proofs were verified during execution. | ||
#[error("unverified deferred proofs: verified={actual}, expected={expected}")] | ||
UnverifiedDeferredProofs { | ||
/// The total number of deferred proofs that were provided. | ||
expected: usize, | ||
/// The actual number of deferred proofs that were verified. | ||
actual: usize, | ||
}, | ||
|
||
/// Insufficient deferred proofs were provided. | ||
#[error("insufficient deferred proofs: unable to verify proof at index {index}")] | ||
InsufficientDeferredProofs { | ||
/// The index of the proof that was requested. | ||
index: usize, | ||
}, | ||
|
||
/// Deferred proof verification failed. | ||
#[error("deferred proof verification failed: {reason}")] | ||
DeferredProofVerificationFailed { | ||
/// The reason for the verification failure. | ||
reason: String, | ||
}, | ||
} | ||
|
||
impl<'a> Executor<'a> { | ||
|
@@ -503,7 +526,7 @@ | |
) -> MemoryReadRecord { | ||
// Check that the memory address is within the babybear field and not within the registers' | ||
// address space. Also check that the address is aligned. | ||
if addr % 4 != 0 || addr <= Register::X31 as u32 || addr >= BABYBEAR_PRIME { | ||
panic!("Invalid memory access: addr={addr}"); | ||
} | ||
|
||
|
@@ -736,7 +759,7 @@ | |
) -> MemoryWriteRecord { | ||
// Check that the memory address is within the babybear field and not within the registers' | ||
// address space. Also check that the address is aligned. | ||
if addr % 4 != 0 || addr <= Register::X31 as u32 || addr >= BABYBEAR_PRIME { | ||
panic!("Invalid memory access: addr={addr}"); | ||
} | ||
|
||
|
@@ -1614,7 +1637,51 @@ | |
// Executing a syscall optionally returns a value to write to the t0 | ||
// register. If it returns None, we just keep the | ||
// syscall_id in t0. | ||
let res = syscall_impl.execute(&mut precompile_rt, syscall, b, c); | ||
let res = if syscall == SyscallCode::VERIFY_SP1_PROOF { | ||
// Catch panics for VERIFY_SP1_PROOF to provide better error messages. | ||
match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's kind of bad that we need to use this and have syscall-specific branch here, ideally execute fn should just return a Result |
||
syscall_impl.execute(&mut precompile_rt, syscall, b, c) | ||
})) { | ||
Ok(result) => result, | ||
Err(panic_payload) => { | ||
// Extract the panic message. | ||
let msg = panic_payload | ||
.downcast_ref::<String>() | ||
.map(String::as_str) | ||
.or_else(|| panic_payload.downcast_ref::<&str>().copied()); | ||
|
||
if let Some(msg) = msg { | ||
let index = precompile_rt.rt.state.proof_stream_ptr; | ||
let available = precompile_rt.rt.state.proof_stream.len(); | ||
|
||
if msg.contains("Not enough proofs") { | ||
tracing::error!( | ||
"Insufficient deferred proofs: unable to verify proof \ | ||
at index {index} because only {available} proofs were \ | ||
provided. \ | ||
Make sure you're passing the correct number of proofs \ | ||
and that you're calling verify_sp1_proof for all proofs." | ||
); | ||
return Err(ExecutionError::InsufficientDeferredProofs { | ||
index, | ||
}); | ||
} else if msg.contains("Failed to verify proof") { | ||
tracing::error!( | ||
"Failed to verify deferred proof at index {index}." | ||
); | ||
return Err(ExecutionError::DeferredProofVerificationFailed { | ||
reason: msg.to_string(), | ||
}); | ||
} | ||
} | ||
|
||
// Resume default behavior for unknown panics. | ||
std::panic::resume_unwind(panic_payload); | ||
} | ||
} | ||
} else { | ||
syscall_impl.execute(&mut precompile_rt, syscall, b, c) | ||
}; | ||
let a = if let Some(val) = res { val } else { syscall_id }; | ||
|
||
// If the syscall is `HALT` and the exit code is non-zero, return an error. | ||
|
@@ -1718,7 +1785,7 @@ | |
// | ||
// If we're close to not fitting, early stop the shard to ensure we don't OOM. | ||
let mut shape_match_found = true; | ||
if self.state.global_clk % self.shape_check_frequency == 0 { | ||
// Estimate the number of events in the trace. | ||
Self::estimate_riscv_event_counts( | ||
&mut self.event_counts, | ||
|
@@ -2059,7 +2126,7 @@ | |
let public_values = self.record.public_values; | ||
|
||
if done { | ||
self.postprocess(); | ||
self.postprocess()?; | ||
|
||
// Push the remaining execution record with memory initialize & finalize events. | ||
self.bump_record(); | ||
|
@@ -2108,7 +2175,7 @@ | |
Ok(done) | ||
} | ||
|
||
fn postprocess(&mut self) { | ||
fn postprocess(&mut self) -> Result<(), ExecutionError> { | ||
// Flush remaining stdout/stderr | ||
for (fd, buf) in &self.io_buf { | ||
if !buf.is_empty() { | ||
|
@@ -2124,12 +2191,17 @@ | |
} | ||
} | ||
|
||
// Ensure that all proofs and input bytes were read, otherwise warn the user. | ||
// Ensure that all proofs were read, otherwise return an error. | ||
if self.state.proof_stream_ptr != self.state.proof_stream.len() { | ||
tracing::warn!( | ||
"Not all proofs were read. Proving will fail during recursion. Did you pass too | ||
many proofs in or forget to call verify_sp1_proof?" | ||
let expected = self.state.proof_stream.len(); | ||
let actual = self.state.proof_stream_ptr; | ||
tracing::error!( | ||
"Not all proofs were verified. \ | ||
Expected to verify {expected} proofs, but only {actual} were verified. \ | ||
Make sure you're passing the correct number of proofs \ | ||
and that you're calling verify_sp1_proof for all proofs." | ||
); | ||
return Err(ExecutionError::UnverifiedDeferredProofs { expected, actual }); | ||
} | ||
|
||
if !self.state.input_stream.is_empty() { | ||
|
@@ -2221,6 +2293,8 @@ | |
.push(MemoryInitializeFinalizeEvent::finalize_from_record(addr, &record)); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn get_syscall(&mut self, code: SyscallCode) -> Option<&Arc<dyn Syscall>> { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: "unverified" is a bit misleading. Maybe "not enough"?