Skip to content

Commit

Permalink
Merge pull request #25 from chainwayxyz/ekrem/presign
Browse files Browse the repository at this point in the history
Ekrem/presign
  • Loading branch information
ekrembal authored Dec 8, 2023
2 parents 6d9ee0b + 80e776d commit 409dcb1
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 32 deletions.
5 changes: 3 additions & 2 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use bitcoin::{Address, ScriptBuf};

use crate::actor::Actor;
use crate::utils::taproot_address_from_script_leaves;
use crate::wire::HashTuple;
use crate::{
gates::{AndGate, NotGate, XorGate},
traits::{circuit::CircuitTrait, gate::GateTrait, wire::WireTrait},
Expand Down Expand Up @@ -82,14 +83,14 @@ impl CircuitTrait for Circuit {
output
}

fn get_wire_hashes(&self) -> Vec<[[u8; 32]; 2]> {
fn get_wire_hashes(&self) -> Vec<HashTuple> {
self.wires
.iter()
.map(|wire_rcref| {
let wire = wire_rcref.try_borrow_mut().unwrap();
wire.get_hash_pair()
})
.collect::<Vec<[[u8; 32]; 2]>>()
.collect::<Vec<HashTuple>>()
}

fn from_bristol(file: &str) -> Self {
Expand Down
8 changes: 4 additions & 4 deletions src/gates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ mod tests {
let script = not_gate.create_response_script(lock_hash);

let solution_01_preimages = vec![
input_wire_0_preimages[0].clone().to_vec(),
output_wire_0_preimages[1].clone().to_vec(),
input_wire_0_preimages.zero.clone().to_vec(),
output_wire_0_preimages.one.clone().to_vec(),
lock_preimage.to_vec(),
];
let mut exec_01 = Exec::new(
Expand Down Expand Up @@ -230,8 +230,8 @@ mod tests {
assert_eq!(res.error, None);

let solution_01_preimages = vec![
input_wire_0_preimages[0].clone().to_vec(),
output_wire_0_preimages[0].clone().to_vec(),
input_wire_0_preimages.zero.clone().to_vec(),
output_wire_0_preimages.zero.clone().to_vec(),
lock_preimage.to_vec(),
];
let mut exec_00 = Exec::new(
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ fn main() {

let witness = sighash_cache.witness_mut(0).unwrap();
witness.push(sig.as_ref());
witness.push(wire.preimages.unwrap()[1]);
witness.push(wire.preimages.unwrap()[0]);
witness.push(wire.preimages.unwrap().one);
witness.push(wire.preimages.unwrap().zero);
witness.push(script);
witness.push(&control_block.serialize());

Expand Down
13 changes: 2 additions & 11 deletions src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,11 @@ use bitvm::{
circuit::Circuit,
communication::{receive_message, send_message},
traits::circuit::CircuitTrait,
wire::HashTuple,
};

use tokio_tungstenite::connect_async;

// #[derive(Serialize, Deserialize, Debug)]
// struct WireHash {
// zero: [u8; 32],
// one: [u8; 32],
// }
// #[derive(Serialize, Deserialize, Debug)]
// struct WireHashes {
// wire_hashes: Vec<WireHash>,
// }

#[tokio::main]
async fn main() {
let url = "ws://127.0.0.1:9000";
Expand All @@ -35,7 +26,7 @@ async fn main() {
println!("Verifier public key: {}", verifier_publickey);

let circuit = Circuit::from_bristol("bristol/add.txt");
let wire_hashes: Vec<[[u8; 32]; 2]> = circuit.get_wire_hashes();
let wire_hashes: Vec<HashTuple> = circuit.get_wire_hashes();

send_message(&mut ws_stream, &wire_hashes).await.unwrap();
}
4 changes: 2 additions & 2 deletions src/traits/circuit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::actor::Actor;
use crate::{actor::Actor, wire::HashTuple};
use bitcoin::{
secp256k1::{All, Secp256k1},
taproot::TaprootSpendInfo,
Expand All @@ -11,7 +11,7 @@ pub trait CircuitTrait {

fn evaluate(&mut self, inputs: Vec<Vec<bool>>) -> Vec<Vec<bool>>;

fn get_wire_hashes(&self) -> Vec<[[u8; 32]; 2]>;
fn get_wire_hashes(&self) -> Vec<HashTuple>;

fn from_bristol(file: &str) -> Self;

Expand Down
4 changes: 3 additions & 1 deletion src/traits/wire.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use bitcoin::{script::Builder, ScriptBuf, XOnlyPublicKey};

use crate::wire::HashTuple;

pub trait WireTrait {
fn get_hash_pair(&self) -> [[u8; 32]; 2];
fn get_hash_pair(&self) -> HashTuple;
fn generate_anti_contradiction_script(&self, verifier_pk: XOnlyPublicKey) -> ScriptBuf;
fn add_bit_commitment_script(&self, builder: Builder) -> Builder;
}
40 changes: 30 additions & 10 deletions src/wire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,25 @@ use bitcoin::opcodes::all::*;
use bitcoin::ScriptBuf;
use bitcoin::XOnlyPublicKey;
use rand::Rng;
use serde::Deserialize;
use serde::Serialize;

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct HashTuple {
pub zero: [u8; 32],
pub one: [u8; 32],
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct PreimageTuple {
pub zero: [u8; 32],
pub one: [u8; 32],
}

#[derive(Clone)]
pub struct Wire {
pub preimages: Option<[[u8; 32]; 2]>,
pub hashes: [[u8; 32]; 2],
pub preimages: Option<PreimageTuple>,
pub hashes: HashTuple,
pub selector: Option<bool>,
pub index: Option<usize>,
}
Expand Down Expand Up @@ -40,26 +54,32 @@ impl Wire {
let hash2 = sha256::Hash::hash(&preimage2).to_byte_array();

Wire {
preimages: Some([preimage1, preimage2]),
hashes: [hash1, hash2],
preimages: Some(PreimageTuple {
zero: preimage1,
one: preimage2,
}),
hashes: HashTuple {
zero: hash1,
one: hash2,
},
selector: None,
index: Some(index),
}
}
}

impl WireTrait for Wire {
fn get_hash_pair(&self) -> [[u8; 32]; 2] {
fn get_hash_pair(&self) -> HashTuple {
self.hashes
}

fn generate_anti_contradiction_script(&self, verifier_pk: XOnlyPublicKey) -> ScriptBuf {
Builder::new()
.push_opcode(OP_SHA256)
.push_slice(self.hashes[0])
.push_slice(self.hashes.zero)
.push_opcode(OP_EQUALVERIFY)
.push_opcode(OP_SHA256)
.push_slice(self.hashes[1])
.push_slice(self.hashes.one)
.push_opcode(OP_EQUALVERIFY)
.push_x_only_key(&verifier_pk)
.push_opcode(OP_CHECKSIG)
Expand All @@ -70,11 +90,11 @@ impl WireTrait for Wire {
builder
.push_opcode(OP_SHA256)
.push_opcode(OP_DUP)
.push_slice(self.hashes[1])
.push_slice(self.hashes.one)
.push_opcode(OP_EQUAL)
.push_opcode(OP_DUP)
.push_opcode(OP_ROT)
.push_slice(self.hashes[0])
.push_slice(self.hashes.zero)
.push_opcode(OP_EQUAL)
.push_opcode(OP_BOOLOR)
.push_opcode(OP_VERIFY)
Expand Down Expand Up @@ -107,7 +127,7 @@ mod tests {
let script = wire.generate_anti_contradiction_script(verifier_pk);

let preimages_vec = if let Some(preimages) = wire.preimages {
vec![preimages[1].to_vec(), preimages[0].to_vec()]
vec![preimages.one.to_vec(), preimages.zero.to_vec()]
} else {
panic!("wire preimages are None")
};
Expand Down

0 comments on commit 409dcb1

Please sign in to comment.