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
38 changes: 1 addition & 37 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ members = [
"crates/header-chain",
"crates/commit-chain",
"crates/state-chain",
#"crates/mara-slipstream-client",
"crates/bitcoin-light-client-circuit",
"circuits/header-chain-proof/host",
"circuits/commit-chain-proof/host",
Expand Down Expand Up @@ -70,7 +71,8 @@ secp256k1 = { version = "0.29.1", features = ["global-context"] }
anyhow = "1.0.97"
strum = { version = "0.26", features = ["derive"] }
tempfile = "3.19.1"
reqwest = { version = "0.12.15", features = ["json"] }
reqwest = { version = "0.12.28", default-features = false, features = ["json"] }
http-body-util = "0.1"

axum = "0.8.1"
http = "1.3.1"
Expand Down
39 changes: 22 additions & 17 deletions crates/bitcoin-light-client-circuit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ pub fn verify_proof(
#[cfg(test)]
mod tests {
use super::*;
use bitcoin::{Amount, Transaction};
use bitcoin::Transaction;
const PROOF: &[u8] = include_bytes!("../../../circuits/data/watchtower/output3.bin.proof.bin");
const PUBLIC_INPUTS: &[u8] =
include_bytes!("../../../circuits/data/watchtower/output3.bin.public_inputs.bin");
Expand Down Expand Up @@ -561,22 +561,6 @@ mod tests {
assert_eq!(words, recovered);
}

#[test]
fn test_extract_op_return() {
// Example: construct a fake tx with OP_RETURN
let expected_op_data = [12, 3, 4, 45];
let script = ScriptBuf::new_op_return(&expected_op_data);
let tx = Transaction {
version: bitcoin::transaction::Version::TWO,
lock_time: bitcoin::absolute::LockTime::ZERO,
input: vec![],
output: vec![bitcoin::TxOut { value: Amount::ZERO, script_pubkey: script }],
};

let op_return_data = commit_chain::extract_op_return_data(&tx.output);
assert_eq!(expected_op_data.to_vec(), op_return_data);
}

#[test]
fn test_u256_to_le_bits() {
use std::str::FromStr;
Expand All @@ -595,4 +579,25 @@ mod tests {
let reconstructed_str = reconstructed2.to_string();
assert_eq!(u_str, reconstructed_str);
}

#[test]
fn test_extract_data_from_commitment_outputs() {
use bitcoin::consensus::encode::deserialize;
// Testnet4 tx: 14b586e2e64e7b4b12aca96832d0703b9d218fa81e0ea84c1155a5749b28924b
let bytes = hex::decode(
"02000000000102c1eeed57e622af0fbb57e863a3b6284dc5ce6249804d0b6ce923bf81004188b00000000000fffffffff0679cdb8b16eebfbcd7fb4d582a20247857db4927f0f79388ba6735bdcd98f60b00000000ffffffff0c4a010000000000002200209bf28a9ccba44a0cbdd17ce6bb8262a136a08ac0088b0ec3f5ae484951f590dd4a01000000000000220020f65f87063cd8c00f539cb877d19a672b241066780a185357072e7da517286a1d4a0100000000000022002056dde473377f215234cf46258af2e2a5007a8e34206ecb438cef18906035c7384a01000000000000220020e77df8511565180c187f61ecdaf15fdc0bcd6076084a3a6823eb1e7fbb4e5af74a01000000000000220020ee98e7229e9a7ac2269cf2c493ff09f1c8b64f5116f5c3662be80640f8a11e144a01000000000000220020a2703885ba63f87d5bde5c3f085a7343e09eaef408074390d2081ff325cc6e894a0100000000000022002038e7a277db470a65c1075e58053075fe83944c621e43601f2b77e18e597c1cbe4a01000000000000220020ad89f2340414b9209003047abdf53716103ff6e71bd1f04c17055c33859124e84a010000000000002200207024bc980d3fa0d5715d859f2e036164414bbdcf0000000000000000000000004a01000000000000220020000000000000000000000938e7e6f31c23766897f6d40100307830306562643200000000000000003c6a3a353862363863396134373432343339653636393834306432306265626665346562643236393366636138323463386430623065346531366233368726980000000000220020082778653debae3a77067cc2c165ae2294c4c6984515aabe977dde1d8e39365103412853f869fd7d8a3e97fd095e6dce63be7e13b172f70e6c92b4ad968af671b3b1f72113b8d2a2a3749c02fc1241110081d8b8ba3686fc6e9b157530ba2fc41bef81222076c09522e2614dadf1471e456abf567b1756465a4133cedc7a0780b277f7e954ac41c076c09522e2614dadf1471e456abf567b1756465a4133cedc7a0780b277f7e954d391bf91c2d0eb83acda477b90f5efbfa4e4d1388e31690a6de57ff01c93f2e202473044022006feb2824f0d733e11c98ea52b0c2aaa1e6bc4c4223287959addbe2e33ebb71202203a1d4613db8eadf3e8d65e46b94e4027d3e6435a2abffd8aeefbc8bd4858f7a50123210376c09522e2614dadf1471e456abf567b1756465a4133cedc7a0780b277f7e954ac00000000"
).unwrap();
let tx: Transaction = deserialize(&bytes).unwrap();

let commitment = extract_data_from_commitment_outputs(&tx.output);
let (
parsed_graph_id,
_proof,
_public_values,
_vk,
_watchtower_total_work,
_watchtower_consensus_block_height,
) = parse_watchtower_commitment(&commitment).unwrap();
assert_eq!(hex::encode(parsed_graph_id), "9bf28a9ccba44a0cbdd17ce6bb8262a1");
}
}
114 changes: 1 addition & 113 deletions crates/bitcoin-light-client-circuit/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,7 @@ use bitcoin::absolute::LockTime;
use bitcoin::blockdata::opcodes::all::*;
use bitcoin::blockdata::script::Builder;
use bitcoin::transaction::Version;
use bitcoin::{
Address, Amount, CompressedPublicKey, Network, OutPoint, ScriptBuf, Sequence, Transaction,
TxIn, TxOut, Witness,
};
use hex::FromHex;

use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};

pub fn decode_eth_address(addr: &str) -> Result<[u8; 20], hex::FromHexError> {
// Strip 0x if it exists
let addr = addr.strip_prefix("0x").unwrap_or(addr);
// Decode into Vec<u8>
let bytes = Vec::from_hex(addr)?;

// Ensure it's 20 bytes
let arr: [u8; 20] = bytes.try_into().expect("Ethereum address must be 20 bytes");
Ok(arr)
}
use bitcoin::{Address, Amount, OutPoint, ScriptBuf, Sequence, Transaction, TxIn, TxOut, Witness};

/// Return script length L for a standard m-of-n multisig script with compressed pubkeys.
pub fn multisig_script_len(n: u32) -> u32 {
Expand Down Expand Up @@ -146,98 +129,3 @@ pub fn create_sequencer_update_partial_tx(
};
Ok(tx)
}

pub fn create_dummy_publisher_keys(total: usize, network: Network) -> Vec<(SecretKey, PublicKey)> {
let secp = Secp256k1::new();

let mut keys = Vec::new();

for i in 0..total {
let sk = SecretKey::from_slice(&[i as u8 + 1; 32]).unwrap();
let pk = PublicKey::from_secret_key(&secp, &sk);
keys.push((sk, pk));
}
println!("Publisher private key:");
keys.iter().for_each(|(sk, _)| {
let k = bitcoin::PrivateKey { compressed: true, network: network.into(), inner: *sk };
println!("{:?}\n", k.to_wif())
});
println!("Publisher public key:");
keys.iter().for_each(|(_, pk)| println!("{}\n", CompressedPublicKey(*pk)));
keys
}

#[cfg(test)]
mod tests {
use super::*;
use bitcoin::{
Address, EcdsaSighashType, Network, OutPoint, Sequence, TxIn, absolute::LockTime,
hashes::Hash, transaction::Version,
};
use commit_chain::*;

#[test]
fn test_verify_p2wsh_multisig_witness() {
// === Step 1: generate key pairs ===
let keys = create_dummy_publisher_keys(3, bitcoin::Network::Regtest);
let pubkeys: Vec<PublicKey> = keys.iter().map(|(_, pk)| *pk).collect();

let threshold = 2;

// === Step 2: create redeem_script ===
let redeem_script = create_sequencer_update_script(&pubkeys, threshold);

// === Step 3: create prevout (P2WSH output) ===
let script_pubkey = ScriptBuf::new_p2wsh(&redeem_script.wscript_hash());
let prev_value = Amount::from_sat(100_000);
let prevout = TxOut { value: prev_value, script_pubkey };

// Fake OutPoint
let prev_outpoint =
OutPoint { txid: bitcoin::Txid::from_byte_array([0u8; 32].into()), vout: 0 };

// === Step 4: construct spending tx ===
let mut tx = Transaction {
version: Version::TWO,
lock_time: LockTime::ZERO,
input: vec![TxIn {
previous_output: prev_outpoint,
script_sig: ScriptBuf::new(),
sequence: Sequence::MAX,
witness: Witness::default(),
}],
output: vec![TxOut {
value: Amount::from_sat(99_000),
script_pubkey: {
let btc_pk0 = bitcoin::PublicKey::from(pubkeys[0]);
Address::p2pkh(&btc_pk0, Network::Testnet).script_pubkey()
},
}],
};

// === Step 5: sign by 2 private keys ===
let (sig1, _) =
sign_partial(&mut tx, &keys[0].0, &redeem_script, prev_value, EcdsaSighashType::All)
.unwrap();

let (sig2, _) =
sign_partial(&mut tx, &keys[1].0, &redeem_script, prev_value, EcdsaSighashType::All)
.unwrap();

// === Step 6: finalize witness ===
finalize(&mut tx, vec![sig1, sig2], &redeem_script).unwrap();

// === Step 7: verify ===
let ok = verify_p2wsh_multisig_witness(
&tx,
0,
&prevout,
&redeem_script,
&pubkeys,
threshold as usize,
)
.unwrap();

assert!(ok, "2-of-3 multisig witness should verify");
}
}
2 changes: 1 addition & 1 deletion crates/commit-chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ bitcoin = { workspace = true, features = ["serde", "rand", "secp-recovery"] }
bincode = "1.3.3"
# workspace
guest-executor = { workspace = true }
header-chain = { path = "../header-chain" }
#header-chain = { path = "../header-chain" }

# Ziren
zkm-verifier = { git = "https://github.com/ProjectZKM/Ziren", package = "zkm-verifier" }
Expand Down
26 changes: 21 additions & 5 deletions crates/commit-chain/src/commit_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,17 @@ impl CommitChainState {
let mut commit_block_height: u32 = self.block_height;
for commit in &commits {
let latest_commit_txn_with_wtns = &commit.commit_txn;
println!("commit tx: {:?}", latest_commit_txn_with_wtns.compute_txid());
let latest_sequencers = &commit.sequencers;
let publisher_public_keys = &commit.publisher_public_keys;
let threshold = commit.threshold;

assert_eq!(commit.genesis_txid, self.genesis_txid);

let prev_commit_txid = prev_commit_txn.compute_txid();
println!("prev commit txid: {prev_commit_txid}, {prev_commit_txn:?}");
// calculate the commitment of prev sequencer set and check the equivalent
if !prev_sequencers.is_empty() {
let expected_prev_commit = extract_op_return_data(&prev_commit_txn.output);
if let Hash::Sha256(prev_sequencer_set_hash) = sequencer_hash(prev_sequencers) {
println!(
"expected prev commit: {expected_prev_commit:?}, {prev_sequencer_set_hash:?}"
);
assert_eq!(prev_sequencer_set_hash[..], expected_prev_commit[0..32]);
} else {
panic!("Invalid prev sequencer set hash");
Expand Down Expand Up @@ -244,3 +239,24 @@ pub fn extract_op_return_data(tx_output: &[TxOut]) -> Vec<u8> {
}
results
}

#[cfg(test)]
mod tests {
use super::*;
use bitcoin::{Amount, ScriptBuf};
#[test]
fn test_extract_op_return() {
// Example: construct a fake tx with OP_RETURN
let expected_op_data = [12, 3, 4, 45];
let script = ScriptBuf::new_op_return(&expected_op_data);
let tx = Transaction {
version: bitcoin::transaction::Version::TWO,
lock_time: bitcoin::absolute::LockTime::ZERO,
input: vec![],
output: vec![bitcoin::TxOut { value: Amount::ZERO, script_pubkey: script }],
};

let op_return_data = extract_op_return_data(&tx.output);
assert_eq!(expected_op_data.to_vec(), op_return_data);
}
}
Loading
Loading