Avoid carrying rng

This commit is contained in:
Franck Royer 2021-02-03 15:25:05 +11:00
parent 25bf5df122
commit e82383bcf6
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
5 changed files with 24 additions and 39 deletions

View File

@ -198,6 +198,7 @@ impl Builder {
self.execution_params.bitcoin_punish_timelock, self.execution_params.bitcoin_punish_timelock,
redeem_address, redeem_address,
punish_address, punish_address,
rng,
); );
Ok(AliceState::Started { amounts, state0 }) Ok(AliceState::Started { amounts, state0 })

View File

@ -16,7 +16,6 @@ use rand::{CryptoRng, RngCore};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sha2::Sha256; use sha2::Sha256;
use std::fmt; use std::fmt;
use tracing::info;
#[derive(Debug)] #[derive(Debug)]
pub enum AliceState { pub enum AliceState {
@ -87,6 +86,7 @@ pub struct State0 {
pub a: bitcoin::SecretKey, pub a: bitcoin::SecretKey,
pub s_a: cross_curve_dleq::Scalar, pub s_a: cross_curve_dleq::Scalar,
pub v_a: monero::PrivateViewKey, pub v_a: monero::PrivateViewKey,
pub dleq_proof_s_a: cross_curve_dleq::Proof,
#[serde(with = "::bitcoin::util::amount::serde::as_sat")] #[serde(with = "::bitcoin::util::amount::serde::as_sat")]
pub btc: bitcoin::Amount, pub btc: bitcoin::Amount,
pub xmr: monero::Amount, pub xmr: monero::Amount,
@ -98,7 +98,7 @@ pub struct State0 {
impl State0 { impl State0 {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn new( pub fn new<R>(
a: bitcoin::SecretKey, a: bitcoin::SecretKey,
s_a: cross_curve_dleq::Scalar, s_a: cross_curve_dleq::Scalar,
v_a: monero::PrivateViewKey, v_a: monero::PrivateViewKey,
@ -108,11 +108,18 @@ impl State0 {
punish_timelock: Timelock, punish_timelock: Timelock,
redeem_address: bitcoin::Address, redeem_address: bitcoin::Address,
punish_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 { Self {
a, a,
s_a, s_a,
v_a, v_a,
dleq_proof_s_a,
redeem_address, redeem_address,
punish_address, punish_address,
btc, btc,
@ -122,17 +129,14 @@ impl State0 {
} }
} }
pub fn next_message<R: RngCore + CryptoRng>(&self, rng: &mut R) -> alice::Message0 { pub fn next_message(&self) -> alice::Message0 {
info!("Producing first message");
let dleq_proof_s_a = cross_curve_dleq::Proof::new(rng, &self.s_a);
alice::Message0 { alice::Message0 {
A: self.a.public(), A: self.a.public(),
S_a_monero: monero::PublicKey::from_private_key(&monero::PrivateKey { S_a_monero: monero::PublicKey::from_private_key(&monero::PrivateKey {
scalar: self.s_a.into_ed25519(), scalar: self.s_a.into_ed25519(),
}), }),
S_a_bitcoin: self.s_a.into_secp256k1().into(), 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, v_a: self.v_a,
redeem_address: self.redeem_address.clone(), redeem_address: self.redeem_address.clone(),
punish_address: self.punish_address.clone(), punish_address: self.punish_address.clone(),

View File

@ -23,7 +23,6 @@ use futures::{
pin_mut, pin_mut,
}; };
use libp2p::PeerId; use libp2p::PeerId;
use rand::rngs::OsRng;
use sha2::Sha256; use sha2::Sha256;
use std::sync::Arc; use std::sync::Arc;
use tokio::time::timeout; use tokio::time::timeout;
@ -62,7 +61,7 @@ pub async fn negotiate(
) )
.await??; .await??;
let alice_message0 = state0.next_message(&mut OsRng); let alice_message0 = state0.next_message();
event_loop_handle event_loop_handle
.send_message0(channel, alice_message0) .send_message0(channel, alice_message0)
.await?; .await?;

View File

@ -74,6 +74,7 @@ pub struct State0 {
b: bitcoin::SecretKey, b: bitcoin::SecretKey,
s_b: cross_curve_dleq::Scalar, s_b: cross_curve_dleq::Scalar,
v_b: monero::PrivateViewKey, v_b: monero::PrivateViewKey,
dleq_proof_s_b: cross_curve_dleq::Proof,
#[serde(with = "::bitcoin::util::amount::serde::as_sat")] #[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: bitcoin::Amount, btc: bitcoin::Amount,
xmr: monero::Amount, xmr: monero::Amount,
@ -97,6 +98,7 @@ impl State0 {
let s_b = cross_curve_dleq::Scalar::random(rng); let s_b = cross_curve_dleq::Scalar::random(rng);
let v_b = monero::PrivateViewKey::new_random(rng); let v_b = monero::PrivateViewKey::new_random(rng);
let dleq_proof_s_b = cross_curve_dleq::Proof::new(rng, &s_b);
Self { Self {
b, b,
@ -104,6 +106,7 @@ impl State0 {
v_b, v_b,
btc, btc,
xmr, xmr,
dleq_proof_s_b,
cancel_timelock, cancel_timelock,
punish_timelock, punish_timelock,
refund_address, refund_address,
@ -111,16 +114,14 @@ impl State0 {
} }
} }
pub fn next_message<R: RngCore + CryptoRng>(&self, rng: &mut R) -> bob::Message0 { pub fn next_message(&self) -> bob::Message0 {
let dleq_proof_s_b = cross_curve_dleq::Proof::new(rng, &self.s_b);
bob::Message0 { bob::Message0 {
B: self.b.public(), B: self.b.public(),
S_b_monero: monero::PublicKey::from_private_key(&monero::PrivateKey { S_b_monero: monero::PublicKey::from_private_key(&monero::PrivateKey {
scalar: self.s_b.into_ed25519(), scalar: self.s_b.into_ed25519(),
}), }),
S_b_bitcoin: self.s_b.into_secp256k1().into(), 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, v_b: self.v_b,
refund_address: self.refund_address.clone(), refund_address: self.refund_address.clone(),
} }

View File

@ -11,7 +11,6 @@ use crate::{
}; };
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use async_recursion::async_recursion; use async_recursion::async_recursion;
use rand::{rngs::OsRng, CryptoRng, RngCore};
use std::sync::Arc; use std::sync::Arc;
use tokio::select; use tokio::select;
use tracing::info; use tracing::info;
@ -44,7 +43,6 @@ pub async fn run_until(
swap.db, swap.db,
swap.bitcoin_wallet, swap.bitcoin_wallet,
swap.monero_wallet, swap.monero_wallet,
OsRng,
swap.swap_id, swap.swap_id,
swap.execution_params, swap.execution_params,
) )
@ -54,20 +52,16 @@ pub async fn run_until(
// State machine driver for swap execution // State machine driver for swap execution
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
#[async_recursion] #[async_recursion]
async fn run_until_internal<R>( async fn run_until_internal(
state: BobState, state: BobState,
is_target_state: fn(&BobState) -> bool, is_target_state: fn(&BobState) -> bool,
mut event_loop_handle: EventLoopHandle, mut event_loop_handle: EventLoopHandle,
db: Database, db: Database,
bitcoin_wallet: Arc<bitcoin::Wallet>, bitcoin_wallet: Arc<bitcoin::Wallet>,
monero_wallet: Arc<monero::Wallet>, monero_wallet: Arc<monero::Wallet>,
mut rng: R,
swap_id: Uuid, swap_id: Uuid,
execution_params: ExecutionParams, execution_params: ExecutionParams,
) -> Result<BobState> ) -> Result<BobState> {
where
R: RngCore + CryptoRng + Send,
{
info!("Current state: {}", state); info!("Current state: {}", state);
if is_target_state(&state) { if is_target_state(&state) {
Ok(state) Ok(state)
@ -80,7 +74,6 @@ where
state0, state0,
amounts, amounts,
&mut event_loop_handle, &mut event_loop_handle,
&mut rng,
bitcoin_wallet.clone(), bitcoin_wallet.clone(),
) )
.await?; .await?;
@ -95,7 +88,6 @@ where
db, db,
bitcoin_wallet, bitcoin_wallet,
monero_wallet, monero_wallet,
rng,
swap_id, swap_id,
execution_params, execution_params,
) )
@ -117,7 +109,6 @@ where
db, db,
bitcoin_wallet, bitcoin_wallet,
monero_wallet, monero_wallet,
rng,
swap_id, swap_id,
execution_params, execution_params,
) )
@ -170,7 +161,6 @@ where
db, db,
bitcoin_wallet, bitcoin_wallet,
monero_wallet, monero_wallet,
rng,
swap_id, swap_id,
execution_params, execution_params,
) )
@ -217,7 +207,6 @@ where
db, db,
bitcoin_wallet, bitcoin_wallet,
monero_wallet, monero_wallet,
rng,
swap_id, swap_id,
execution_params, execution_params,
) )
@ -260,7 +249,6 @@ where
db, db,
bitcoin_wallet, bitcoin_wallet,
monero_wallet, monero_wallet,
rng,
swap_id, swap_id,
execution_params, execution_params,
) )
@ -296,7 +284,6 @@ where
db, db,
bitcoin_wallet.clone(), bitcoin_wallet.clone(),
monero_wallet, monero_wallet,
rng,
swap_id, swap_id,
execution_params, execution_params,
) )
@ -318,7 +305,6 @@ where
db, db,
bitcoin_wallet, bitcoin_wallet,
monero_wallet, monero_wallet,
rng,
swap_id, swap_id,
execution_params, execution_params,
) )
@ -344,7 +330,6 @@ where
db, db,
bitcoin_wallet, bitcoin_wallet,
monero_wallet, monero_wallet,
rng,
swap_id, swap_id,
execution_params, execution_params,
) )
@ -376,7 +361,6 @@ where
db, db,
bitcoin_wallet, bitcoin_wallet,
monero_wallet, monero_wallet,
rng,
swap_id, swap_id,
execution_params, execution_params,
) )
@ -390,16 +374,12 @@ where
} }
} }
pub async fn negotiate<R>( pub async fn negotiate(
state0: crate::protocol::bob::state::State0, state0: crate::protocol::bob::state::State0,
amounts: SwapAmounts, amounts: SwapAmounts,
swarm: &mut EventLoopHandle, swarm: &mut EventLoopHandle,
mut rng: R,
bitcoin_wallet: Arc<crate::bitcoin::Wallet>, bitcoin_wallet: Arc<crate::bitcoin::Wallet>,
) -> Result<bob::state::State2> ) -> Result<bob::state::State2> {
where
R: RngCore + CryptoRng + Send,
{
tracing::trace!("Starting negotiate"); tracing::trace!("Starting negotiate");
swarm swarm
.send_swap_request(SwapRequest { .send_swap_request(SwapRequest {
@ -411,7 +391,7 @@ where
// argument. // argument.
let _swap_response = swarm.recv_swap_response().await?; 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 msg0 = swarm.recv_message0().await?;
let state1 = state0.receive(bitcoin_wallet.as_ref(), msg0).await?; let state1 = state0.receive(bitcoin_wallet.as_ref(), msg0).await?;