Skip to content

Commit

Permalink
Merge pull request #27 from chainwayxyz/hakan/tx-address-functions
Browse files Browse the repository at this point in the history
Hakan/tx address functions
  • Loading branch information
ekrembal authored Dec 8, 2023
2 parents 02ad470 + aa78b79 commit e630b4f
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 310 deletions.
22 changes: 1 addition & 21 deletions src/actor.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use bitcoin::hashes::sha256;
use bitcoin::opcodes::all::*;
use bitcoin::TapNodeHash;
use bitcoin::{
hashes::Hash,
script::Builder,
secp256k1::{
rand, schnorr::Signature, All, Keypair, Message, Secp256k1, SecretKey, XOnlyPublicKey,
},
Address, ScriptBuf, TapSighash, TapTweakHash,
Address, TapSighash, TapTweakHash,
};
use rand::Rng;

Expand Down Expand Up @@ -47,15 +45,6 @@ impl Actor {
}
}

pub fn generate_timelock_script(&self, block_count: u32) -> ScriptBuf {
Builder::new()
.push_int(block_count as i64)
.push_opcode(OP_CSV)
.push_x_only_key(&self.public_key)
.push_opcode(OP_CHECKSIG)
.into_script()
}

pub fn sign_with_tweak(
&self,
sighash: TapSighash,
Expand Down Expand Up @@ -94,15 +83,6 @@ impl Actor {
self.challenge_preimages.push(preimages);
challenge_hashes
}

pub fn generate_challenge_script(&self, challenge_hash: &HashValue) -> ScriptBuf {
Builder::new()
.push_slice(challenge_hash)
.push_opcode(OP_EQUALVERIFY)
.push_x_only_key(&self.public_key)
.push_opcode(OP_CHECKSIG)
.into_script()
}
}

#[cfg(test)]
Expand Down
105 changes: 25 additions & 80 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@ use std::collections::BTreeMap;
use std::iter::zip;
use std::rc::Rc;

use bitcoin::secp256k1::All;
use bitcoin::secp256k1::Secp256k1;
use bitcoin::taproot::TaprootSpendInfo;
use bitcoin::{Address, ScriptBuf};

use crate::actor::Actor;
use crate::utils::taproot_address_from_script_leaves;
use crate::wire::HashTuple;
use crate::wire::HashValue;

use crate::{
gates::{AndGate, NotGate, XorGate},
traits::{circuit::CircuitTrait, gate::GateTrait, wire::WireTrait},
traits::gate::GateTrait,
utils::read_lines,
wire::Wire,
};
Expand All @@ -28,27 +21,16 @@ pub struct Circuit {

impl Default for Circuit {
fn default() -> Self {
Self::new()
Self::from_bristol("bristol/test.txt")
}
}

impl Circuit {
pub fn new() -> Self {
Circuit {
input_sizes: vec![32, 32],
output_sizes: vec![32],
gates: vec![Box::new(NotGate::new(vec![], vec![]))],
wires: vec![],
}
}
}

impl CircuitTrait for Circuit {
fn num_gates(&self) -> usize {
pub fn num_gates(&self) -> usize {
self.gates.len()
}

fn evaluate(&mut self, inputs: Vec<Vec<bool>>) -> Vec<Vec<bool>> {
pub fn evaluate(&mut self, inputs: Vec<Vec<bool>>) -> Vec<Vec<bool>> {
assert_eq!(
inputs.len(),
self.input_sizes.len(),
Expand Down Expand Up @@ -84,7 +66,7 @@ impl CircuitTrait for Circuit {
output
}

fn get_wire_hashes(&self) -> Vec<HashTuple> {
pub fn get_wire_hashes(&self) -> Vec<HashTuple> {
self.wires
.iter()
.map(|wire_rcref| {
Expand All @@ -94,7 +76,7 @@ impl CircuitTrait for Circuit {
.collect::<Vec<HashTuple>>()
}

fn from_bristol(file: &str) -> Self {
pub fn from_bristol(file: &str) -> Self {
let mut nog: usize = 0; // number of gates
let mut now: usize = 0; // number of wires
let mut input_sizes = Vec::<usize>::new();
Expand Down Expand Up @@ -195,69 +177,26 @@ impl CircuitTrait for Circuit {
.collect::<Vec<Rc<RefCell<Wire>>>>(),
};
}

fn generate_response_tree(
&self,
secp: &Secp256k1<All>,
_prover: &Actor,
verifier: &Actor,
challenge_hashes: Vec<HashValue>,
) -> (Address, TaprootSpendInfo) {
assert_eq!(
challenge_hashes.len(),
self.gates.len(),
"wrong number of challenge hashes"
);
let mut scripts = self
.gates
.iter()
.zip(challenge_hashes.iter())
.map(|(gate, hash)| gate.create_response_script(*hash))
.collect::<Vec<ScriptBuf>>();
scripts.push(verifier.generate_timelock_script(10));
taproot_address_from_script_leaves(secp, scripts)
}

fn generate_challenge_tree(
&self,
secp: &Secp256k1<All>,
prover: &Actor,
verifier: &Actor,
challenge_hashes: Vec<HashValue>,
) -> (Address, TaprootSpendInfo) {
assert_eq!(
challenge_hashes.len(),
self.gates.len(),
"wrong number of challenge hashes"
);
let mut scripts = challenge_hashes
.iter()
.map(|x| verifier.generate_challenge_script(x))
.collect::<Vec<ScriptBuf>>();
// let mut reveal_challenge_scripts =
scripts.extend(self.wires.iter().map(|wire_rcref| {
wire_rcref
.try_borrow_mut()
.unwrap()
.generate_anti_contradiction_script(verifier.public_key)
}));
scripts.push(prover.generate_timelock_script(10));
taproot_address_from_script_leaves(secp, scripts)
}
}

#[cfg(test)]
mod tests {
use bitcoin::secp256k1::Secp256k1;
use bitcoin::taproot::LeafVersion;

use super::*;
use crate::actor::Actor;
use crate::utils::{bool_array_to_number, number_to_bool_array};

use crate::transactions::{
generate_anti_contradiction_script, generate_challenge_address_and_info,
generate_timelock_script,
};

#[test]
fn test_circuit() {
let circuit = Circuit::new();
assert!(circuit.output_sizes[0] == 32);
let circuit = Circuit::default();
assert!(circuit.output_sizes[0] == 1);
}

#[test]
Expand Down Expand Up @@ -288,11 +227,17 @@ mod tests {

let challenge_hashes = verifier.generate_challenge_hashes(circuit.num_gates());

let (_address, tree_info) =
circuit.generate_challenge_tree(&secp, &prover, &verifier, challenge_hashes);
let (_address, tree_info) = generate_challenge_address_and_info(
&secp,
&circuit,
prover.public_key,
verifier.public_key,
challenge_hashes,
);
for wire_rcref in circuit.wires.iter() {
let wire = wire_rcref.try_borrow_mut().unwrap();
let script = wire.generate_anti_contradiction_script(verifier.public_key);
let script =
generate_anti_contradiction_script(wire.get_hash_pair(), verifier.public_key);
let ctrl_block = tree_info
.control_block(&(script.clone(), LeafVersion::TapScript))
.unwrap();
Expand All @@ -303,7 +248,7 @@ mod tests {
));
}
// TODO: add tests for reveral challenge scripts
let p10_script = prover.generate_timelock_script(10);
let p10_script = generate_timelock_script(prover.public_key, 10);
let p10_ctrl_block = tree_info
.control_block(&(p10_script.clone(), LeafVersion::TapScript))
.unwrap();
Expand Down
76 changes: 38 additions & 38 deletions src/gates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bitcoin::opcodes::all::{
use bitcoin::script::Builder;
use bitcoin::ScriptBuf;

use crate::traits::wire::WireTrait;
use crate::transactions::add_bit_commitment_script;
use crate::wire::HashValue;
use crate::{traits::gate::GateTrait, wire::Wire};
use std::cell::RefCell;
Expand Down Expand Up @@ -40,15 +40,15 @@ impl GateTrait for NotGate {
.push_opcode(OP_SHA256)
.push_slice(lock_hash)
.push_opcode(OP_EQUALVERIFY);
let builder = self.output_wires[0]
.try_borrow()
.unwrap()
.add_bit_commitment_script(builder)
.push_opcode(OP_TOALTSTACK);
let builder = self.input_wires[0]
.try_borrow()
.unwrap()
.add_bit_commitment_script(builder);
let builder = add_bit_commitment_script(
self.output_wires[0].try_borrow().unwrap().get_hash_pair(),
builder,
)
.push_opcode(OP_TOALTSTACK);
let builder = add_bit_commitment_script(
self.input_wires[0].try_borrow().unwrap().get_hash_pair(),
builder,
);
builder
.push_opcode(OP_NOT)
.push_opcode(OP_FROMALTSTACK)
Expand Down Expand Up @@ -87,20 +87,20 @@ impl GateTrait for AndGate {
.push_opcode(OP_SHA256)
.push_slice(lock_hash)
.push_opcode(OP_EQUALVERIFY);
let builder = self.output_wires[0]
.try_borrow()
.unwrap()
.add_bit_commitment_script(builder)
.push_opcode(OP_TOALTSTACK);
let builder = self.input_wires[0]
.try_borrow()
.unwrap()
.add_bit_commitment_script(builder)
.push_opcode(OP_TOALTSTACK);
let builder = self.input_wires[1]
.try_borrow()
.unwrap()
.add_bit_commitment_script(builder);
let builder = add_bit_commitment_script(
self.output_wires[0].try_borrow().unwrap().get_hash_pair(),
builder,
)
.push_opcode(OP_TOALTSTACK);
let builder = add_bit_commitment_script(
self.input_wires[0].try_borrow().unwrap().get_hash_pair(),
builder,
)
.push_opcode(OP_TOALTSTACK);
let builder = add_bit_commitment_script(
self.input_wires[0].try_borrow().unwrap().get_hash_pair(),
builder,
);
builder
.push_opcode(OP_FROMALTSTACK)
.push_opcode(OP_AND)
Expand Down Expand Up @@ -140,20 +140,20 @@ impl GateTrait for XorGate {
.push_opcode(OP_SHA256)
.push_slice(lock_hash)
.push_opcode(OP_EQUALVERIFY);
let builder = self.output_wires[0]
.try_borrow()
.unwrap()
.add_bit_commitment_script(builder)
.push_opcode(OP_TOALTSTACK);
let builder = self.input_wires[0]
.try_borrow()
.unwrap()
.add_bit_commitment_script(builder)
.push_opcode(OP_TOALTSTACK);
let builder = self.input_wires[1]
.try_borrow()
.unwrap()
.add_bit_commitment_script(builder);
let builder = add_bit_commitment_script(
self.output_wires[0].try_borrow().unwrap().get_hash_pair(),
builder,
)
.push_opcode(OP_TOALTSTACK);
let builder = add_bit_commitment_script(
self.input_wires[0].try_borrow().unwrap().get_hash_pair(),
builder,
)
.push_opcode(OP_TOALTSTACK);
let builder = add_bit_commitment_script(
self.input_wires[0].try_borrow().unwrap().get_hash_pair(),
builder,
);
builder
.push_opcode(OP_FROMALTSTACK)
.push_opcode(OP_XOR)
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ pub mod circuit;
pub mod communication;
pub mod gates;
pub mod traits;
pub mod transactions;
pub mod utils;
pub mod wire;
17 changes: 12 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ use bitcoin::{Amount, OutPoint, ScriptBuf, TapLeafHash, Transaction, TxIn, TxOut

use bitcoincore_rpc::{Auth, Client, RpcApi};
use bitvm::actor::Actor;
use bitvm::traits::wire::WireTrait;

use bitvm::{circuit::Circuit, traits::circuit::CircuitTrait};
use bitvm::circuit::Circuit;
use bitvm::transactions::{
generate_anti_contradiction_script, generate_challenge_address_and_info,
};

use std::borrow::BorrowMut;

Expand Down Expand Up @@ -71,8 +73,13 @@ fn main() {

let challenge_hashes = vicky.generate_challenge_hashes(circuit.num_gates());

let (address, kickoff_taproot_info) =
circuit.generate_challenge_tree(&secp, &paul, &vicky, challenge_hashes);
let (address, kickoff_taproot_info) = generate_challenge_address_and_info(
&secp,
&circuit,
paul.public_key,
vicky.public_key,
challenge_hashes,
);

let mut tx = Transaction {
version: bitcoin::transaction::Version::TWO,
Expand Down Expand Up @@ -128,7 +135,7 @@ fn main() {

let vout: u32 = 0;

let script = wire.generate_anti_contradiction_script(vicky.public_key);
let script = generate_anti_contradiction_script(wire.get_hash_pair(), vicky.public_key);

let mut tx = Transaction {
version: bitcoin::transaction::Version::TWO,
Expand Down
1 change: 0 additions & 1 deletion src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use bitcoin::XOnlyPublicKey;
use bitvm::{
circuit::Circuit,
communication::{receive_message, send_message},
traits::circuit::CircuitTrait,
wire::HashTuple,
};

Expand Down
1 change: 0 additions & 1 deletion src/traits/bit_commitment.rs

This file was deleted.

Loading

0 comments on commit e630b4f

Please sign in to comment.