Skip to content

Commit

Permalink
circuits: Add function comments for header_chain_proof.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyhunsen committed Oct 25, 2024
1 parent a388b46 commit 98e147c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
26 changes: 25 additions & 1 deletion circuits/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,32 @@ fn read_header_except_prev_blockhash<E: Environment>() -> HeaderWithoutPrevBlock
(version, merkle_root, time, bits, nonce)
}

/// Runs header chain prover on a Risczero zkVM.
///
/// TODO check env and returns
///
/// # Environment
///
/// - `[u8; 32]`: Genesis block's hash.
/// - `u32`: 0 to specify current block is not genesis, bigger than 0 to specify it is
/// - `[u32; 8]`: Previous method ID
/// - `u32`: Previous offset
/// - `[u8; 32]`: Previous block's hash
/// - `[u8; 32]`: Previous block's total work
///
/// # Returns
///
/// A tuple with 5 members:
///
/// - `[u32; 8]`: Method ID
/// - `[u32; 8]`: Genesis block's hash
/// - `u32`: Offset?
/// - `[u32; 8]`: Block hash?
/// - `[u32; 8]`: Total work
pub fn header_chain_proof<E: Environment>() -> ([u32; 8], [u8; 32], u32, [u8; 32], [u8; 32]) {
let genesis_block_hash = E::read_32bytes();
let is_genesis = E::read_u32();

let (mut curr_prev_block_hash, method_id, mut total_work) = if is_genesis == 0 {
let prev_method_id = E::read_u32x8();
let prev_offset = E::read_u32();
Expand Down Expand Up @@ -126,11 +149,11 @@ pub fn header_chain_proof<E: Environment>() -> ([u32; 8], [u8; 32], u32, [u8; 32
let method_id = E::read_u32x8();
(genesis_block_hash, method_id, U256::ZERO)
};

let return_offset = E::read_u32();
let batch_size = E::read_u32();

let mut to_return_block_hash: [u8; 32] = [0; 32];

for i in 0..batch_size {
let header_without_prev_blockhash = read_header_except_prev_blockhash::<E>();
curr_prev_block_hash =
Expand All @@ -144,6 +167,7 @@ pub fn header_chain_proof<E: Environment>() -> ([u32; 8], [u8; 32], u32, [u8; 32
to_return_block_hash = curr_prev_block_hash;
}
}

(
method_id,
genesis_block_hash,
Expand Down
41 changes: 34 additions & 7 deletions core/src/chain_prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ enum BlockFetchStatus {
}

/// Input data for a proof.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ProofData {
pub genesis_block_hash: BlockHash,
pub is_genesis: bool,
Expand Down Expand Up @@ -192,8 +193,11 @@ where
///
/// # Returns
///
/// - [`Vec<u8>`]: Raw proof data.
async fn prove_block(&self, proof_data: ProofData) -> Result<Vec<u8>, BridgeError> {
/// - TODO
async fn prove_block(
&self,
proof_data: ProofData,
) -> Result<([u32; 8], [u8; 32], u32, [u8; 32], [u8; 32]), BridgeError> {
let env = ExecutorEnv::builder()
.write(&proof_data.genesis_block_hash.as_raw_hash().as_byte_array())
.unwrap()
Expand All @@ -215,12 +219,12 @@ where
.prove(env, verifier_circuit::GUEST_ELF)
.unwrap()
.receipt;
let output: u32 = receipt.journal.decode().unwrap();
let output: ([u32; 8], [u8; 32], u32, [u8; 32], [u8; 32]) =
receipt.journal.decode().unwrap();

println!("Hello, world! I generated a proof of guest execution! {} is a public output from journal ", output);
println!("receipt: {:?} ", receipt);
tracing::debug!("Decoded journal output: {:?}", output);

Ok(receipt.journal.bytes)
Ok(output)
}

/// Returns active blockchain tip.
Expand Down Expand Up @@ -249,6 +253,7 @@ mod tests {
extended_rpc::ExtendedRpc,
mock::database::create_test_config_with_thread_name,
};
use bitcoin::{hashes::Hash, BlockHash, Work};
use bitcoincore_rpc::RpcApi;

#[tokio::test]
Expand Down Expand Up @@ -344,6 +349,28 @@ mod tests {
let rpc = create_extended_rpc!(config);
let prover = ChainProver::new(&config, rpc.clone()).await.unwrap();

prover.prove_block(ProofData::default()).await.unwrap();
// Genesis block proving.
let proof_data = ProofData::default();
let output = prover.prove_block(proof_data).await.unwrap();
assert_eq!(output.0, proof_data.prev_method_id);
assert_eq!(
output.1,
proof_data.genesis_block_hash.as_raw_hash().to_byte_array()
);

// Current block proving.
let current_tip = prover.get_active_tip().unwrap();
let current_block = rpc.client.get_block(&current_tip.hash).unwrap();
let prev_block_hash = current_block.header.prev_blockhash;
let proof_data = ProofData {
genesis_block_hash: BlockHash::all_zeros(),
is_genesis: false,
prev_method_id: [0; 8],
prev_offset: 0,
prev_block_hash: prev_block_hash,
prev_total_work: Work::from_hex("0x0").unwrap(),
};
let output = prover.prove_block(proof_data).await.unwrap();
assert_eq!(output.0, proof_data.prev_method_id);
}
}

0 comments on commit 98e147c

Please sign in to comment.