Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 12 additions & 11 deletions circuits/commit-chain-proof/host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ pub async fn fetch_commit_chain(
commits_file: &str,
network: Network,
) -> anyhow::Result<Vec<CircuitCommit>> {
let btc_client = BTCClient::new(network, Some(&esplora_url));
let btc_client = BTCClient::new(network, Some(esplora_url));

tracing::info!(
"Fetching commit chain, commit_info_file: {}, commits_file: {}",
commit_info_file,
commits_file
);
let rdr = std::fs::File::open(commit_info_file).context(&("read error"))?;
let rdr = std::fs::File::open(commit_info_file).context("read error")?;
let ci: CommitInfo = serde_json::from_reader(rdr)?;
let mut commits: Vec<CircuitCommit> = vec![];
let txid = Txid::from_str(&ci.txid)?;
Expand Down Expand Up @@ -131,8 +131,8 @@ pub async fn fetch_commit_chain(
block_height,
};
commits.push(commit);
std::fs::write(&commits_file, serde_json::to_vec(&commits)?)
.expect(&format!("write {commits_file} error"));
std::fs::write(commits_file, serde_json::to_vec(&commits)?)
.with_context(|| format!("write {commits_file} error"))?;
Ok(commits)
}

Expand All @@ -144,6 +144,7 @@ pub struct CommitChainProofBuilder {
}

impl CommitChainProofBuilder {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
let client = ProverClient::new();
let (proving_key, verifying_key) = client.setup(COMMIT_CHAIN);
Expand Down Expand Up @@ -183,7 +184,7 @@ impl ProofBuilder for CommitChainProofBuilder {
let prev_receipt = if *init_input {
None
} else {
let public_inputs = fs::read(&format!("{}.public_inputs.bin", input_proof))
let public_inputs = fs::read(format!("{}.public_inputs.bin", input_proof))
.context("Read public input")?;
//let prev: CommitChainCircuitOutput = serde_json::from_slice(&public_inputs).unwrap();
Some(public_inputs)
Expand All @@ -193,8 +194,8 @@ impl ProofBuilder for CommitChainProofBuilder {
Some(public_inputs) => {
let proof_bytes =
fs::read(input_proof).context("Failed to read input proof file")?;
let zkm_vk_hash = fs::read(&format!("{}.vk_hash.bin", input_proof))
.context("Read vk hash")?;
let zkm_vk_hash =
fs::read(format!("{}.vk_hash.bin", input_proof)).context("Read vk hash")?;
let version_path = format!("{input_proof}.zkm_version.bin");
let zkm_version = fs::read(&version_path)
.with_context(|| {
Expand Down Expand Up @@ -290,16 +291,16 @@ impl ProofBuilder for CommitChainProofBuilder {
//tracing::info!("Generate proof successfully, proof: {:?}", proof);
//Ok((public_value_hex, proof_size))

std::fs::write(&format!("{}", output_proof), proof.bytes())?;
std::fs::write(output_proof, proof.bytes())?;
let public_value_hex = hex::encode(proof.public_values.to_vec());
let proof_size = proof.bytes().len();
let zkm_version = proof.zkm_version.clone();
std::fs::write(
&format!("{}.public_inputs.bin", output_proof),
format!("{}.public_inputs.bin", output_proof),
proof.public_values.to_vec(),
)?;
std::fs::write(&format!("{}.vk_hash.bin", output_proof), self.verifying_key.bytes32())?;
std::fs::write(&format!("{}.zkm_version.bin", output_proof), zkm_version)?;
std::fs::write(format!("{}.vk_hash.bin", output_proof), self.verifying_key.bytes32())?;
std::fs::write(format!("{}.zkm_version.bin", output_proof), zkm_version)?;
Ok((public_value_hex, proof_size))
}
}
Expand Down
23 changes: 13 additions & 10 deletions circuits/header-chain-proof/host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ pub async fn fetch_header_chain(
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(block_header_file)
.expect(&format!("Open {block_header_file} error"));
.with_context(|| format!("Open {block_header_file} error"))?;

let mut headers: Vec<u8> = Vec::new();
writer.read_to_end(&mut headers)?;
Expand Down Expand Up @@ -151,6 +152,7 @@ pub struct HeaderChainProofBuilder {
}

impl HeaderChainProofBuilder {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
let client = ProverClient::new();
let (proving_key, verifying_key) = client.setup(HEADER_CHAIN);
Expand Down Expand Up @@ -197,9 +199,10 @@ impl ProofBuilder for HeaderChainProofBuilder {
let prev_receipt = if *init_input {
None
} else {
let public_inputs = fs::read(&format!("{}.public_inputs.bin", input_proof)).expect(
&format!("Failed to read public inputs from {}.public_inputs.bin", input_proof),
);
let public_inputs = fs::read(format!("{}.public_inputs.bin", input_proof))
.with_context(|| {
format!("Failed to read public inputs from {}.public_inputs.bin", input_proof)
})?;
Some(public_inputs)
};

Expand All @@ -208,7 +211,7 @@ impl ProofBuilder for HeaderChainProofBuilder {
Some(public_inputs) => {
let proof_bytes =
fs::read(input_proof).context("Failed to read input proof file").unwrap();
let zkm_vk_hash = fs::read(&format!("{}.vk_hash.bin", input_proof)).unwrap();
let zkm_vk_hash = fs::read(format!("{}.vk_hash.bin", input_proof)).unwrap();
let version_path = format!("{input_proof}.zkm_version.bin");
let zkm_version = fs::read(&version_path)
.with_context(|| {
Expand Down Expand Up @@ -244,7 +247,7 @@ impl ProofBuilder for HeaderChainProofBuilder {
batch_size
);

let block_headers = (&total_block_headers[*start..*start + *batch_size]).to_vec();
let block_headers = total_block_headers[*start..*start + *batch_size].to_vec();
let input: HeaderChainCircuitInput = HeaderChainCircuitInput {
prev_proof,
zkm_proof,
Expand Down Expand Up @@ -306,16 +309,16 @@ impl ProofBuilder for HeaderChainProofBuilder {
//fs::write(&format!("{}.vk", output_proof), bincode::serialize(&self.verifying_key)?)?;
//fs::write(&format!("{}.in", output_proof), input)?;

std::fs::write(&format!("{}", output_proof), proof.bytes())?;
std::fs::write(output_proof, proof.bytes())?;
let public_value_hex = hex::encode(proof.public_values.to_vec());
let proof_size = proof.bytes().len();
let zkm_version = proof.zkm_version.clone();
std::fs::write(
&format!("{}.public_inputs.bin", output_proof),
format!("{}.public_inputs.bin", output_proof),
proof.public_values.to_vec(),
)?;
std::fs::write(&format!("{}.vk_hash.bin", output_proof), self.verifying_key.bytes32())?;
std::fs::write(&format!("{}.zkm_version.bin", output_proof), zkm_version)?;
std::fs::write(format!("{}.vk_hash.bin", output_proof), self.verifying_key.bytes32())?;
std::fs::write(format!("{}.zkm_version.bin", output_proof), zkm_version)?;

tracing::info!("Generate proof successfully, proof: {:?}", proof);
Ok((public_value_hex, proof_size))
Expand Down
42 changes: 22 additions & 20 deletions circuits/operator-proof/host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,14 @@ use sha2::{Digest, Sha256};
use std::sync::OnceLock;
static ELF_ID: OnceLock<String> = OnceLock::new();

type IndexedWatchtowerInputs = Vec<(u16, Txid, PublicKey)>;
type GraphWatchtowerXOnlyPublicKeys = Vec<[u8; 32]>;

/// Parses the full graph key list and keeps each included challenge's original graph index.
fn parse_indexed_watchtower_inputs(
watchtower_challenge_txids: &str,
watchtower_public_keys: &str,
) -> anyhow::Result<(Vec<(u16, Txid, PublicKey)>, Vec<[u8; 32]>)> {
) -> anyhow::Result<(IndexedWatchtowerInputs, GraphWatchtowerXOnlyPublicKeys)> {
let public_keys = watchtower_public_keys
.split(',')
.map(PublicKey::from_str)
Expand Down Expand Up @@ -143,9 +146,9 @@ pub async fn fetch_target_block_and_watchtower_tx(
)> {
let (indexed_watchtower_inputs, graph_watchtower_xonly_public_keys) =
parse_indexed_watchtower_inputs(watchtower_challenge_txids, watchtower_public_keys)?;
let btc_client = BTCClient::new(bitcoin_network, Some(&esplora_url));
let btc_client = BTCClient::new(bitcoin_network, Some(esplora_url));

let latest_sequencer_commit_txid = Txid::from_str(&latest_sequencer_commit_txid)?;
let latest_sequencer_commit_txid = Txid::from_str(latest_sequencer_commit_txid)?;
let operator_latest_sequencer_commit_txn =
match btc_client.get_tx(&latest_sequencer_commit_txid).await? {
Some(tx) => tx,
Expand All @@ -156,7 +159,7 @@ pub async fn fetch_target_block_and_watchtower_tx(
};
let tx_status = btc_client.get_tx_status(&latest_sequencer_commit_txid).await?;
let block_pos_ss_commit = match tx_status.block_height {
Some(height) => height as u32,
Some(height) => height,
None => anyhow::bail!(
"Latest sequencer commit txn is not confirmed yet: {}",
latest_sequencer_commit_txid
Expand Down Expand Up @@ -258,6 +261,7 @@ pub struct OperatorProofBuilder {
}

impl OperatorProofBuilder {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
let client = ProverClient::new();
let (proving_key, verifying_key) = client.setup(OPERATOR);
Expand Down Expand Up @@ -316,12 +320,12 @@ impl ProofBuilder for OperatorProofBuilder {
// --- header chain --- //
let header_chain_input = {
let zkm_public_values =
fs::read(&format!("{}.public_inputs.bin", header_chain_input_proof)).unwrap();
fs::read(format!("{}.public_inputs.bin", header_chain_input_proof)).unwrap();
let zkm_proof = fs::read(header_chain_input_proof)
.context("Failed to read input proof file")
.unwrap();
let zkm_vk_hash =
fs::read(&format!("{}.vk_hash.bin", header_chain_input_proof)).unwrap();
fs::read(format!("{}.vk_hash.bin", header_chain_input_proof)).unwrap();
let version_path = format!("{header_chain_input_proof}.zkm_version.bin");
let zkm_version = fs::read(&version_path)
.with_context(|| format!("failed to read zkm_version file '{version_path}'"))
Expand All @@ -343,12 +347,12 @@ impl ProofBuilder for OperatorProofBuilder {
// --- commit chain --- //
let commit_chain_input = {
let zkm_public_values =
fs::read(&format!("{}.public_inputs.bin", commit_chain_input_proof)).unwrap();
fs::read(format!("{}.public_inputs.bin", commit_chain_input_proof)).unwrap();
let zkm_proof = fs::read(commit_chain_input_proof)
.context("Failed to read input proof file")
.unwrap();
let zkm_vk_hash =
fs::read(&format!("{}.vk_hash.bin", commit_chain_input_proof)).unwrap();
fs::read(format!("{}.vk_hash.bin", commit_chain_input_proof)).unwrap();
let version_path = format!("{commit_chain_input_proof}.zkm_version.bin");
let zkm_version = fs::read(&version_path)
.with_context(|| format!("failed to read zkm_version file '{version_path}'"))
Expand All @@ -373,9 +377,8 @@ impl ProofBuilder for OperatorProofBuilder {
.context("Failed to read input proof file")
.unwrap();
let zkm_public_values =
fs::read(&format!("{}.public_inputs.bin", state_chain_input_proof)).unwrap();
let zkm_vk_hash =
fs::read(&format!("{}.vk_hash.bin", state_chain_input_proof)).unwrap();
fs::read(format!("{}.public_inputs.bin", state_chain_input_proof)).unwrap();
let zkm_vk_hash = fs::read(format!("{}.vk_hash.bin", state_chain_input_proof)).unwrap();
let version_path = format!("{state_chain_input_proof}.zkm_version.bin");
let zkm_version = fs::read(&version_path)
.with_context(|| format!("failed to read zkm_version file '{version_path}'"))
Expand All @@ -398,11 +401,10 @@ impl ProofBuilder for OperatorProofBuilder {
// --- spv --- //
//let latest_sequencer_commit_txid = Txid::from_str(&latest_sequencer_commit_txid).unwrap();

let operator_genesis_sequencer_commit_txid =
Txid::from_str(&genesis_sequencer_commit_txid)?;
let operator_genesis_sequencer_commit_txid = Txid::from_str(genesis_sequencer_commit_txid)?;

let bitcoin_block_headers = {
let headers: Vec<u8> = std::fs::read(&format!("{header_chain_input_proof}.blocks"))
let headers: Vec<u8> = std::fs::read(format!("{header_chain_input_proof}.blocks"))
.context("read header chain blocks error")?;
headers
.chunks(80)
Expand All @@ -426,7 +428,7 @@ impl ProofBuilder for OperatorProofBuilder {
operator_latest_sequencer_commit_txn.compute_txid()
);
let spv_ss_commit = build_spv(
&operator_latest_sequencer_commit_txn,
operator_latest_sequencer_commit_txn,
*block_pos_ss_commit,
target_block_ss_commit.clone(),
&bitcoin_block_headers,
Expand All @@ -437,7 +439,7 @@ impl ProofBuilder for OperatorProofBuilder {
|| -> anyhow::Result<(ZKMProofWithPublicValues, u64, f32)> {
let mut stdin = ZKMStdin::new();

let included_watchtowers: U256 = U256::from_str(&included_watchtowers).unwrap();
let included_watchtowers: U256 = U256::from_str(included_watchtowers).unwrap();
stdin.write(&included_watchtowers);

stdin.write(&graph_id);
Expand Down Expand Up @@ -495,11 +497,11 @@ impl ProofBuilder for OperatorProofBuilder {
let public_value_hex = hex::encode(proof.public_values.to_vec());
let proof_size = proof.bytes().len();
let zkm_version = proof.zkm_version.clone();
std::fs::write(&format!("{}.public_inputs.bin", output), proof.public_values.to_vec())?;
std::fs::write(&format!("{}.vk_hash.bin", output), self.verifying_key.bytes32())?;
std::fs::write(&format!("{}.zkm_version.bin", output), zkm_version)?;
std::fs::write(format!("{}.public_inputs.bin", output), proof.public_values.to_vec())?;
std::fs::write(format!("{}.vk_hash.bin", output), self.verifying_key.bytes32())?;
std::fs::write(format!("{}.zkm_version.bin", output), zkm_version)?;
let proof = bincode::serialize(&proof)?;
std::fs::write(&format!("{}", output), proof)?;
std::fs::write(output, proof)?;
Ok((public_value_hex, proof_size))
}
}
Expand Down
Loading