Skip to content

Commit

Permalink
clean prints
Browse files Browse the repository at this point in the history
  • Loading branch information
orkunkilic committed Feb 2, 2024
1 parent bc8872b commit f6e4cfe
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 201 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Chainway Limited

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
# BitVM.rs
# Toy BitVM in Rust

Experimental toy BitVM implementation in Rust.

It is recommended to always use [cargo-crev](https://github.com/crev-dev/cargo-crev)
to verify the trustworthiness of each of your dependencies, including this one.


Run regtest with the following command:
```
bitcoind -regtest -rpcuser=admin -rpcpassword=admin -rpcport=18443 -fallbackfee=0.00001 -wallet=admin
```

Then run the following command to generate blocks continuously:
```
bitcoin-cli -regtest -rpcuser=admin -rpcpassword=admin createwallet "admin"
./regtest-commands.sh
```

Then start the verifier binary with the following command:
```
cargo run --bin verifier
```
bitcoin-cli -regtest -rpcuser=admin -rpcpassword=admin generatetoaddress 101 $(bitcoin-cli -regtest -rpcuser=admin -rpcpassword=admin getnewaddress)

Start the prover binary with the following command:
```
cargo run --bin prover
```

From now on, you can start challenging gates and waiting for the prover to respond.
There is a fraud hardcoded in the code. Challenge `64` for first, then `63` to see the fraud and slash the prover.
179 changes: 0 additions & 179 deletions src/main.rs

This file was deleted.

16 changes: 7 additions & 9 deletions src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ async fn main() {
};

for i in 0..bisection_length as u64 {
println!("Bisection iteration {}", i);
println!("Bisection iteration: {}", i);
let challenge_hashes: Vec<HashValue> = receive_message(&mut ws_stream).await.unwrap();
prover.add_challenge_hashes(challenge_hashes.clone());
let (challenge_address, _) = generate_challenge_address_and_info(
Expand Down Expand Up @@ -259,14 +259,14 @@ async fn main() {
)
.unwrap();
let challenge_sig = prover.sign(sig_hash);
println!("challenge sig: {:?}", challenge_sig);
println!("Challenge Sig: {:?}", challenge_sig);

send_message(&mut ws_stream, &challenge_sig).await.unwrap();

last_output = outputs2;
last_txid = response_tx.txid();
}
println!("Bisection complete");
println!("Bisection completed");
// now we send the funding

let prevouts = vec![TxOut {
Expand All @@ -276,7 +276,6 @@ async fn main() {

// if kickoff_tx uninitialized, then panic

println!("prevout: {:?}", prevouts);
let mut sighash_cache = SighashCache::new(kickoff_tx.borrow_mut());
// TODO: add support for signing with a keypair
let sig_hash = sighash_cache
Expand All @@ -297,7 +296,7 @@ async fn main() {
let kickoff_txid = rpc
.send_raw_transaction(&kickoff_tx)
.unwrap_or_else(|e| panic!("Failed to send raw transaction: {}", e));
println!("initial kickoff txid = {:?}", kickoff_txid);
println!("Initial kickoff txid: {:?}", kickoff_txid);
send_message(&mut ws_stream, &kickoff_txid).await.unwrap();

let a1 = 633;
Expand Down Expand Up @@ -459,11 +458,10 @@ async fn main() {
.send_raw_transaction(&challenge_tx)
.unwrap_or_else(|e| panic!("Failed to send raw transaction: {}", e));

println!("Our response to the challenge");
println!("txid : {:?}", challenge_txid);
println!("Responsing to the challenge, txid: {:?}", challenge_txid);

// let _sig = verifier.sign(sig_hash);
println!("NOW WE GIVE THE RESPONSEEE");
println!("Response given");

let a1 = 32;
let a2 = 70;
Expand Down Expand Up @@ -503,7 +501,7 @@ async fn main() {

// println!("response txid: {:?}", response_tx.txid());
// Prover waits for challenge
println!("Waiting for challenge");
println!("Waiting for a challenge...");
let challenge_tx = watch_transaction(&rpc, &response_tx.txid(), watch_interval).unwrap();
let preimage: &[u8; 32] = challenge_tx.input[0]
.witness
Expand Down
17 changes: 8 additions & 9 deletions src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ async fn handle_connection(stream: TcpStream) {
};

for i in 0..bisection_length as u64 {
println!("Bisection iteration {}", i);
println!("Bisection iteration: {}", i);
let challenge_hashes: Vec<HashValue> =
verifier.generate_challenge_hashes(circuit.num_gates());
send_message(&mut ws_stream, &challenge_hashes)
Expand Down Expand Up @@ -234,7 +234,7 @@ async fn handle_connection(stream: TcpStream) {
// Prover needs to give signature to verifier so that verifier can start a challenge
let challenge_sig: Signature = receive_message(&mut ws_stream).await.unwrap();
verifier.add_signature(challenge_sig);
println!("challenge sig: {:?}", challenge_sig);
println!("Challenge Sig: {:?}", challenge_sig);
// Verify needs to verify the signature
let mut sighash_cache = SighashCache::new(response_tx.borrow_mut());

Expand All @@ -259,7 +259,7 @@ async fn handle_connection(stream: TcpStream) {
last_output = outputs2;
last_txid = response_tx.txid();
}
println!("Bisection complete!");
println!("Bisection completed");
let kickoff_txid: Txid = receive_message(&mut ws_stream).await.unwrap();
if kickoff_tx.txid() != kickoff_txid {
panic!("Kickoff txid mismatch!");
Expand Down Expand Up @@ -351,7 +351,7 @@ async fn handle_connection(stream: TcpStream) {

if i != 0 {
// Verifier needs needs to give signature to prover so that prover can give a response
println!("Waiting for provers response");
println!("Waiting for prover's response...");
let provers_response =
watch_transaction(&rpc, &challenge_tx.txid(), watch_interval).unwrap();
let num_wires = circuit.gates[challenge_gate_num].get_input_size()
Expand Down Expand Up @@ -416,7 +416,7 @@ async fn handle_connection(stream: TcpStream) {
value: Amount::from_sat(amt - (2 * i + 2) * (fee + dust_limit)),
}],
};
println!("CONTRADICTION FOUND");
println!("Contraditcion found! Slashing the prover!");
let mut sighash_cache = SighashCache::new(steal_tx.borrow_mut());

let equivocation_script = generate_anti_contradiction_script(
Expand Down Expand Up @@ -448,7 +448,7 @@ async fn handle_connection(stream: TcpStream) {
let steal_txid = rpc
.send_raw_transaction(&steal_tx)
.unwrap_or_else(|e| panic!("Failed to send raw transaction: {}", e));
println!("VERIFIER STOLE ALL THE MONEY: {:?}", steal_txid);
println!("Verifier slashed the prover: {:?}", steal_txid);
}
// Prover needs to give signature to verifier so that verifier can start a challenge
challenge_gate_num =
Expand Down Expand Up @@ -512,11 +512,10 @@ async fn handle_connection(stream: TcpStream) {
.send_raw_transaction(&response_tx)
.unwrap_or_else(|e| panic!("Failed to send raw transaction: {}", e));

println!("CHALLENGE SENTTTTTTT");
println!("txid : {:?}", response_txid);
println!("Challenge transaction sent! txid: {:?}", response_txid);

last_output = outputs2;
last_txid = response_tx.txid();
}
println!("{:?}", last_output);
println!("Last output: {:?}", last_output);
}

0 comments on commit f6e4cfe

Please sign in to comment.