From e82383bcf6048a71aeb3eb33fc5cea02f9238d57 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Wed, 3 Feb 2021 15:25:05 +1100 Subject: [PATCH] Avoid carrying rng --- swap/src/protocol/alice.rs | 1 + swap/src/protocol/alice/state.rs | 20 ++++++++++++-------- swap/src/protocol/alice/steps.rs | 3 +-- swap/src/protocol/bob/state.rs | 9 +++++---- swap/src/protocol/bob/swap.rs | 30 +++++------------------------- 5 files changed, 24 insertions(+), 39 deletions(-) diff --git a/swap/src/protocol/alice.rs b/swap/src/protocol/alice.rs index 20bf002f..6f5b02d4 100644 --- a/swap/src/protocol/alice.rs +++ b/swap/src/protocol/alice.rs @@ -198,6 +198,7 @@ impl Builder { self.execution_params.bitcoin_punish_timelock, redeem_address, punish_address, + rng, ); Ok(AliceState::Started { amounts, state0 }) diff --git a/swap/src/protocol/alice/state.rs b/swap/src/protocol/alice/state.rs index 6fcee3b6..394493a5 100644 --- a/swap/src/protocol/alice/state.rs +++ b/swap/src/protocol/alice/state.rs @@ -16,7 +16,6 @@ use rand::{CryptoRng, RngCore}; use serde::{Deserialize, Serialize}; use sha2::Sha256; use std::fmt; -use tracing::info; #[derive(Debug)] pub enum AliceState { @@ -87,6 +86,7 @@ pub struct State0 { pub a: bitcoin::SecretKey, pub s_a: cross_curve_dleq::Scalar, pub v_a: monero::PrivateViewKey, + pub dleq_proof_s_a: cross_curve_dleq::Proof, #[serde(with = "::bitcoin::util::amount::serde::as_sat")] pub btc: bitcoin::Amount, pub xmr: monero::Amount, @@ -98,7 +98,7 @@ pub struct State0 { impl State0 { #[allow(clippy::too_many_arguments)] - pub fn new( + pub fn new( a: bitcoin::SecretKey, s_a: cross_curve_dleq::Scalar, v_a: monero::PrivateViewKey, @@ -108,11 +108,18 @@ impl State0 { punish_timelock: Timelock, redeem_address: bitcoin::Address, punish_address: bitcoin::Address, - ) -> Self { + rng: &mut R, + ) -> Self + where + R: RngCore + CryptoRng, + { + let dleq_proof_s_a = cross_curve_dleq::Proof::new(rng, &s_a); + Self { a, s_a, v_a, + dleq_proof_s_a, redeem_address, punish_address, btc, @@ -122,17 +129,14 @@ impl State0 { } } - pub fn next_message(&self, rng: &mut R) -> alice::Message0 { - info!("Producing first message"); - let dleq_proof_s_a = cross_curve_dleq::Proof::new(rng, &self.s_a); - + pub fn next_message(&self) -> alice::Message0 { alice::Message0 { A: self.a.public(), S_a_monero: monero::PublicKey::from_private_key(&monero::PrivateKey { scalar: self.s_a.into_ed25519(), }), S_a_bitcoin: self.s_a.into_secp256k1().into(), - dleq_proof_s_a, + dleq_proof_s_a: self.dleq_proof_s_a.clone(), v_a: self.v_a, redeem_address: self.redeem_address.clone(), punish_address: self.punish_address.clone(), diff --git a/swap/src/protocol/alice/steps.rs b/swap/src/protocol/alice/steps.rs index 86b96b5c..3272d616 100644 --- a/swap/src/protocol/alice/steps.rs +++ b/swap/src/protocol/alice/steps.rs @@ -23,7 +23,6 @@ use futures::{ pin_mut, }; use libp2p::PeerId; -use rand::rngs::OsRng; use sha2::Sha256; use std::sync::Arc; use tokio::time::timeout; @@ -62,7 +61,7 @@ pub async fn negotiate( ) .await??; - let alice_message0 = state0.next_message(&mut OsRng); + let alice_message0 = state0.next_message(); event_loop_handle .send_message0(channel, alice_message0) .await?; diff --git a/swap/src/protocol/bob/state.rs b/swap/src/protocol/bob/state.rs index f727291a..a5902956 100644 --- a/swap/src/protocol/bob/state.rs +++ b/swap/src/protocol/bob/state.rs @@ -74,6 +74,7 @@ pub struct State0 { b: bitcoin::SecretKey, s_b: cross_curve_dleq::Scalar, v_b: monero::PrivateViewKey, + dleq_proof_s_b: cross_curve_dleq::Proof, #[serde(with = "::bitcoin::util::amount::serde::as_sat")] btc: bitcoin::Amount, xmr: monero::Amount, @@ -97,6 +98,7 @@ impl State0 { let s_b = cross_curve_dleq::Scalar::random(rng); let v_b = monero::PrivateViewKey::new_random(rng); + let dleq_proof_s_b = cross_curve_dleq::Proof::new(rng, &s_b); Self { b, @@ -104,6 +106,7 @@ impl State0 { v_b, btc, xmr, + dleq_proof_s_b, cancel_timelock, punish_timelock, refund_address, @@ -111,16 +114,14 @@ impl State0 { } } - pub fn next_message(&self, rng: &mut R) -> bob::Message0 { - let dleq_proof_s_b = cross_curve_dleq::Proof::new(rng, &self.s_b); - + pub fn next_message(&self) -> bob::Message0 { bob::Message0 { B: self.b.public(), S_b_monero: monero::PublicKey::from_private_key(&monero::PrivateKey { scalar: self.s_b.into_ed25519(), }), S_b_bitcoin: self.s_b.into_secp256k1().into(), - dleq_proof_s_b, + dleq_proof_s_b: self.dleq_proof_s_b.clone(), v_b: self.v_b, refund_address: self.refund_address.clone(), } diff --git a/swap/src/protocol/bob/swap.rs b/swap/src/protocol/bob/swap.rs index ced72266..e8614686 100644 --- a/swap/src/protocol/bob/swap.rs +++ b/swap/src/protocol/bob/swap.rs @@ -11,7 +11,6 @@ use crate::{ }; use anyhow::{bail, Result}; use async_recursion::async_recursion; -use rand::{rngs::OsRng, CryptoRng, RngCore}; use std::sync::Arc; use tokio::select; use tracing::info; @@ -44,7 +43,6 @@ pub async fn run_until( swap.db, swap.bitcoin_wallet, swap.monero_wallet, - OsRng, swap.swap_id, swap.execution_params, ) @@ -54,20 +52,16 @@ pub async fn run_until( // State machine driver for swap execution #[allow(clippy::too_many_arguments)] #[async_recursion] -async fn run_until_internal( +async fn run_until_internal( state: BobState, is_target_state: fn(&BobState) -> bool, mut event_loop_handle: EventLoopHandle, db: Database, bitcoin_wallet: Arc, monero_wallet: Arc, - mut rng: R, swap_id: Uuid, execution_params: ExecutionParams, -) -> Result -where - R: RngCore + CryptoRng + Send, -{ +) -> Result { info!("Current state: {}", state); if is_target_state(&state) { Ok(state) @@ -80,7 +74,6 @@ where state0, amounts, &mut event_loop_handle, - &mut rng, bitcoin_wallet.clone(), ) .await?; @@ -95,7 +88,6 @@ where db, bitcoin_wallet, monero_wallet, - rng, swap_id, execution_params, ) @@ -117,7 +109,6 @@ where db, bitcoin_wallet, monero_wallet, - rng, swap_id, execution_params, ) @@ -170,7 +161,6 @@ where db, bitcoin_wallet, monero_wallet, - rng, swap_id, execution_params, ) @@ -217,7 +207,6 @@ where db, bitcoin_wallet, monero_wallet, - rng, swap_id, execution_params, ) @@ -260,7 +249,6 @@ where db, bitcoin_wallet, monero_wallet, - rng, swap_id, execution_params, ) @@ -296,7 +284,6 @@ where db, bitcoin_wallet.clone(), monero_wallet, - rng, swap_id, execution_params, ) @@ -318,7 +305,6 @@ where db, bitcoin_wallet, monero_wallet, - rng, swap_id, execution_params, ) @@ -344,7 +330,6 @@ where db, bitcoin_wallet, monero_wallet, - rng, swap_id, execution_params, ) @@ -376,7 +361,6 @@ where db, bitcoin_wallet, monero_wallet, - rng, swap_id, execution_params, ) @@ -390,16 +374,12 @@ where } } -pub async fn negotiate( +pub async fn negotiate( state0: crate::protocol::bob::state::State0, amounts: SwapAmounts, swarm: &mut EventLoopHandle, - mut rng: R, bitcoin_wallet: Arc, -) -> Result -where - R: RngCore + CryptoRng + Send, -{ +) -> Result { tracing::trace!("Starting negotiate"); swarm .send_swap_request(SwapRequest { @@ -411,7 +391,7 @@ where // argument. let _swap_response = swarm.recv_swap_response().await?; - swarm.send_message0(state0.next_message(&mut rng)).await?; + swarm.send_message0(state0.next_message()).await?; let msg0 = swarm.recv_message0().await?; let state1 = state0.receive(bitcoin_wallet.as_ref(), msg0).await?;