-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from chainwayxyz/ekrem/presign
Ekrem/presign
- Loading branch information
Showing
11 changed files
with
231 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// communication.rs | ||
use futures_util::{SinkExt, StreamExt}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::error::Error; | ||
use tokio::io::{AsyncRead, AsyncWrite}; | ||
use tokio_tungstenite::tungstenite::protocol::Message; | ||
use tokio_tungstenite::WebSocketStream; | ||
|
||
pub async fn send_message<T, M>( | ||
ws_stream: &mut WebSocketStream<T>, | ||
message: &M, | ||
) -> Result<(), Box<dyn Error>> | ||
where | ||
T: AsyncRead + AsyncWrite + Unpin, | ||
M: Serialize, | ||
{ | ||
let serialized = serde_json::to_string(message)?; | ||
ws_stream.send(Message::Text(serialized)).await?; | ||
Ok(()) | ||
} | ||
|
||
pub async fn receive_message<T, M>(ws_stream: &mut WebSocketStream<T>) -> Result<M, Box<dyn Error>> | ||
where | ||
T: AsyncRead + AsyncWrite + Unpin, | ||
M: for<'de> Deserialize<'de>, | ||
{ | ||
if let Some(msg) = ws_stream.next().await { | ||
let msg = msg?; | ||
if let Message::Text(text) = msg { | ||
let deserialized: M = serde_json::from_str(&text)?; | ||
return Ok(deserialized); | ||
} | ||
} | ||
Err("Failed to receive message".into()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod actor; | ||
pub mod circuit; | ||
pub mod communication; | ||
pub mod gates; | ||
pub mod traits; | ||
pub mod utils; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use bitcoin::XOnlyPublicKey; | ||
|
||
// prover.rs | ||
use bitvm::{ | ||
circuit::Circuit, | ||
communication::{receive_message, send_message}, | ||
traits::circuit::CircuitTrait, | ||
}; | ||
|
||
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"; | ||
let (mut ws_stream, _) = connect_async(url).await.expect("Failed to connect"); | ||
println!("WebSocket handshake has been successfully completed"); | ||
|
||
send_message(&mut ws_stream, &"bristol/add.txt".to_string()) | ||
.await | ||
.unwrap(); | ||
|
||
let verifier_publickey_str: String = receive_message(&mut ws_stream).await.unwrap(); | ||
println!("Verifier public key: {}", verifier_publickey_str); | ||
let verifier_publickey: XOnlyPublicKey = verifier_publickey_str.parse().unwrap(); | ||
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(); | ||
|
||
send_message(&mut ws_stream, &wire_hashes).await.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
use bitcoin::{script::Builder, ScriptBuf, XOnlyPublicKey}; | ||
|
||
pub trait WireTrait { | ||
fn get_hash_pair(&self) -> [[u8; 32]; 2]; | ||
fn generate_anti_contradiction_script(&self, verifier_pk: XOnlyPublicKey) -> ScriptBuf; | ||
fn add_bit_commitment_script(&self, builder: Builder) -> Builder; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// verifier.rs | ||
use bitvm::{ | ||
actor::Actor, | ||
communication::{receive_message, send_message}, | ||
}; | ||
use tokio::net::{TcpListener, TcpStream}; | ||
use tokio_tungstenite::accept_async; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let listener = TcpListener::bind("127.0.0.1:9000").await.unwrap(); | ||
println!("Listening on: 127.0.0.1:9000"); | ||
|
||
while let Ok((stream, _)) = listener.accept().await { | ||
tokio::spawn(handle_connection(stream)); | ||
} | ||
} | ||
|
||
async fn handle_connection(stream: TcpStream) { | ||
let mut ws_stream = accept_async(stream) | ||
.await | ||
.expect("Error during the websocket handshake occurred"); | ||
|
||
let message: String = receive_message(&mut ws_stream).await.unwrap(); | ||
println!("Received: {}", message); | ||
|
||
let verifier = Actor::new(); | ||
|
||
// send our public key to the prover | ||
send_message(&mut ws_stream, &verifier.public_key.to_string()) | ||
.await | ||
.unwrap(); | ||
|
||
let wire_hashes: Vec<[[u8; 32]; 2]> = receive_message(&mut ws_stream).await.unwrap(); | ||
|
||
println!("Wire hashes: {:?}", wire_hashes); | ||
} |
Oops, something went wrong.