|
1 | 1 | use std::{env, fmt, sync::Arc}; |
2 | 2 |
|
| 3 | +use async_trait::async_trait; |
3 | 4 | use zkaleido::{ |
4 | 5 | Proof, ProofMetadata, ProofReceipt, ProofReceiptWithMetadata, ProofType, PublicValues, |
5 | 6 | VerifyingKey, VerifyingKeyCommitment, ZkVm, ZkVmError, ZkVmExecutor, ZkVmHost, |
6 | | - ZkVmOutputExtractor, ZkVmProver, ZkVmResult, ZkVmTypedVerifier, ZkVmVkProvider, |
| 7 | + ZkVmOutputExtractor, ZkVmProver, ZkVmRemoteProver, ZkVmResult, ZkVmTypedVerifier, |
| 8 | + ZkVmVkProvider, |
7 | 9 | }; |
8 | 10 |
|
9 | 11 | use crate::{env::NativeMachine, input::NativeMachineInputBuilder, proof::NativeProofReceipt}; |
@@ -97,3 +99,53 @@ impl fmt::Debug for NativeHost { |
97 | 99 | write!(f, "native") |
98 | 100 | } |
99 | 101 | } |
| 102 | + |
| 103 | +/// Implementation of `ZkVmRemoteProver` for `NativeHost`. |
| 104 | +/// |
| 105 | +/// Since `NativeHost` executes proofs synchronously, this implementation: |
| 106 | +/// - Runs the proof immediately in `start_proving` |
| 107 | +/// - Serializes the result and hex-encodes it as the "proof ID" |
| 108 | +/// - Deserializes and returns the proof in `get_proof_if_ready_inner` |
| 109 | +/// |
| 110 | +/// Combined with the blanket impl `impl<T: ZkVmHost + ZkVmRemoteProver> ZkVmRemoteHost for T`, |
| 111 | +/// this automatically gives `NativeHost` the `ZkVmRemoteHost` trait, allowing it to work |
| 112 | +/// seamlessly with async/remote proving interfaces. |
| 113 | +#[async_trait(?Send)] |
| 114 | +impl ZkVmRemoteProver for NativeHost { |
| 115 | + async fn start_proving<'a>( |
| 116 | + &self, |
| 117 | + input: <Self::Input<'a> as zkaleido::ZkVmInputBuilder<'a>>::Input, |
| 118 | + proof_type: ProofType, |
| 119 | + ) -> ZkVmResult<String> { |
| 120 | + // Execute proof synchronously |
| 121 | + let proof_receipt = self.prove(input, proof_type)?; |
| 122 | + |
| 123 | + // Serialize using bincode |
| 124 | + let serialized = bincode::serialize(&proof_receipt) |
| 125 | + .map_err(|e| ZkVmError::InvalidProofReceipt(e.into()))?; |
| 126 | + |
| 127 | + // Encode as hex to use as "proof ID" |
| 128 | + Ok(hex::encode(serialized)) |
| 129 | + } |
| 130 | + |
| 131 | + async fn get_proof_if_ready_inner( |
| 132 | + &self, |
| 133 | + id: String, |
| 134 | + ) -> ZkVmResult<Option<Self::ZkVmProofReceipt>> { |
| 135 | + // Decode the hex-encoded proof |
| 136 | + let decoded = hex::decode(&id).map_err(|_| { |
| 137 | + ZkVmError::InvalidProofReceipt( |
| 138 | + std::io::Error::new(std::io::ErrorKind::InvalidData, "Invalid hex encoding").into(), |
| 139 | + ) |
| 140 | + })?; |
| 141 | + |
| 142 | + // Deserialize the ProofReceiptWithMetadata |
| 143 | + let proof: ProofReceiptWithMetadata = |
| 144 | + bincode::deserialize(&decoded).map_err(|e| ZkVmError::InvalidProofReceipt(e.into()))?; |
| 145 | + |
| 146 | + // Convert to NativeProofReceipt |
| 147 | + let native_receipt = proof.try_into().map_err(ZkVmError::InvalidProofReceipt)?; |
| 148 | + |
| 149 | + Ok(Some(native_receipt)) |
| 150 | + } |
| 151 | +} |
0 commit comments