Skip to content

Commit

Permalink
refactor: proper naming for input and outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
aoengin committed Mar 3, 2025
1 parent 5fe2cb3 commit d92723d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 56 deletions.
2 changes: 1 addition & 1 deletion bridge-circuit-host/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use alloy::{
primitives::keccak256,
providers::{Provider, ProviderBuilder, RootProvider},
providers::{Provider, RootProvider},
transports::http::{Http, Client},
};
use alloy_primitives::U256;
Expand Down
6 changes: 3 additions & 3 deletions bridge-circuit-host/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use borsh::{self, BorshDeserialize};
use bridge_circuit_host::config::PARAMETERS;
use bridge_circuit_host::{fetch_light_client_proof, fetch_storage_proof};
use circuits_lib::bridge_circuit_core::groth16::CircuitGroth16Proof;
use circuits_lib::bridge_circuit_core::structs::WorkOnlyCircuitInput;
use circuits_lib::bridge_circuit_core::structs::{WorkOnlyCircuitInput, BridgeCircuitInput};
use circuits_lib::bridge_circuit_core::winternitz::{
generate_public_key, sign_digits, Parameters, WinternitzCircuitInput, WinternitzHandler,
generate_public_key, sign_digits, Parameters, WinternitzHandler,
};
use rand::{rngs::SmallRng, Rng, SeedableRng};
use risc0_to_bitvm2_core::header_chain::{BlockHeaderCircuitOutput, CircuitBlockHeader};
Expand Down Expand Up @@ -128,7 +128,7 @@ async fn main() {
message: Some(compressed_proof_and_total_work),
};

let winternitz_circuit_input: WinternitzCircuitInput = WinternitzCircuitInput {
let winternitz_circuit_input: BridgeCircuitInput = BridgeCircuitInput {
winternitz_details: vec![winternitz_details],
hcp: block_header_circuit_output,
payout_spv: spv,
Expand Down
5 changes: 3 additions & 2 deletions circuits-lib/src/bridge_circuit/bridge_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use crate::common::zkvm::ZkvmGuest;
use bitcoin::hashes::Hash;
use bridge_circuit_core::groth16::CircuitGroth16Proof;
use bridge_circuit_core::winternitz::{
verify_winternitz_signature, WinternitzCircuitInput, WinternitzHandler,
verify_winternitz_signature, WinternitzHandler,
};
use bridge_circuit_core::structs::BridgeCircuitInput;
use lc_proof::lc_proof_verifier;
use risc0_zkvm::guest::env;
use sha2::{Digest, Sha256};
Expand Down Expand Up @@ -50,7 +51,7 @@ pub fn convert_to_groth16_and_verify(message: &Vec<u8>, pre_state: &[u8; 32]) ->

pub fn bridge_circuit(guest: &impl ZkvmGuest, pre_state: [u8; 32]) {
let start = env::cycle_count();
let input: WinternitzCircuitInput = guest.read_from_host();
let input: BridgeCircuitInput = guest.read_from_host();

let mut watchtower_flags: Vec<bool> = vec![];
let mut wt_messages_with_idxs: Vec<(usize, Vec<u8>)> = vec![];
Expand Down
49 changes: 49 additions & 0 deletions circuits-lib/src/bridge_circuit_core/structs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use borsh::{BorshDeserialize, BorshSerialize};
use risc0_to_bitvm2_core::header_chain::BlockHeaderCircuitOutput;
use serde::{Deserialize, Serialize};
use risc0_to_bitvm2_core::spv::SPV;

use super::winternitz::WinternitzHandler;

#[derive(Serialize, Deserialize, Eq, PartialEq, Clone, Debug, BorshDeserialize, BorshSerialize)]
pub struct WorkOnlyCircuitInput {
Expand All @@ -25,3 +28,49 @@ pub struct StorageProof {
pub index: u32, // For now this is 18, for a specifix withdrawal
pub txid_hex: [u8; 32], // Move txid
}

#[derive(Clone, Debug, BorshDeserialize, BorshSerialize)]
pub struct BridgeCircuitInput {
pub winternitz_details: Vec<WinternitzHandler>,
pub hcp: BlockHeaderCircuitOutput, // This will be removed once the LightClientProof includes the MMRGuest of the Bitcoin blockhashes
pub payout_spv: SPV,
pub lcp: LightClientProof,
pub operator_id: u32,
pub sp: StorageProof,
pub num_watchtowers: u32,
}

impl BridgeCircuitInput {
pub fn new(
winternitz_details: Vec<WinternitzHandler>,
hcp: BlockHeaderCircuitOutput,
payout_spv: SPV,
lcp: LightClientProof,
operator_id: u32,
sp: StorageProof,
num_watchtowers: u32,
) -> Result<Self, &'static str> {
if num_watchtowers > (1 << 20) - 1 {
return Err("num_watchtowers exceeds u20 limit");
}
Ok(Self {
winternitz_details,
hcp,
payout_spv,
lcp,
operator_id,
sp,
num_watchtowers,
})
}
}

#[derive(Serialize, Deserialize, Eq, PartialEq, Clone, Debug, BorshDeserialize, BorshSerialize)]
pub struct BridgeCircuitOutput {
pub winternitz_pubkeys_digest: [u8; 20],
pub correct_watchtowers: Vec<bool>,
pub payout_tx_blockhash: [u8; 32],
pub last_blockhash: [u8; 32],
pub deposit_txid: [u8; 32],
pub operator_id: [u8; 32],
}
51 changes: 1 addition & 50 deletions circuits-lib/src/bridge_circuit_core/winternitz.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use super::{structs, utils};
use super::utils;
use borsh::{BorshDeserialize, BorshSerialize};
use risc0_to_bitvm2_core::header_chain::BlockHeaderCircuitOutput;
use risc0_to_bitvm2_core::spv::SPV;
use serde::{Deserialize, Serialize};
pub type HashOut = [u8; 20];
pub type PublicKey = Vec<HashOut>;
pub type SecretKey = Vec<u8>;
use bitcoin::hashes::{self, Hash};
use structs::{LightClientProof, StorageProof};
use utils::hash160;

#[derive(Eq, PartialEq, Clone, Debug, BorshDeserialize, BorshSerialize)]
Expand All @@ -18,52 +15,6 @@ pub struct WinternitzHandler {
pub message: Option<Vec<u8>>,
}

#[derive(Clone, Debug, BorshDeserialize, BorshSerialize)]
pub struct WinternitzCircuitInput {
pub winternitz_details: Vec<WinternitzHandler>,
pub hcp: BlockHeaderCircuitOutput, // This will be removed once the LightClientProof includes the MMRGuest of the Bitcoin blockhashes
pub payout_spv: SPV,
pub lcp: LightClientProof,
pub operator_id: u32,
pub sp: StorageProof,
pub num_watchtowers: u32,
}

impl WinternitzCircuitInput {
pub fn new(
winternitz_details: Vec<WinternitzHandler>,
hcp: BlockHeaderCircuitOutput,
payout_spv: SPV,
lcp: LightClientProof,
operator_id: u32,
sp: StorageProof,
num_watchtowers: u32,
) -> Result<Self, &'static str> {
if num_watchtowers > (1 << 20) - 1 {
return Err("num_watchtowers exceeds u20 limit");
}
Ok(Self {
winternitz_details,
hcp,
payout_spv,
lcp,
operator_id,
sp,
num_watchtowers,
})
}
}

#[derive(Serialize, Deserialize, Eq, PartialEq, Clone, Debug, BorshDeserialize, BorshSerialize)]
pub struct WinternitzCircuitOutput {
pub winternitz_pubkeys_digest: [u8; 20],
pub correct_watchtowers: Vec<bool>,
pub payout_tx_blockhash: [u8; 32],
pub last_blockhash: [u8; 32],
pub deposit_txid: [u8; 32],
pub operator_id: [u8; 32],
}

pub fn verify_winternitz_signature(input: &WinternitzHandler) -> bool {
let message = input.message.as_ref().unwrap();
let signature = input.signature.as_ref().unwrap();
Expand Down

0 comments on commit d92723d

Please sign in to comment.