From fdb1929ba4435ba0ce15c65c30c826ada9b9e81a Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Sun, 5 Jun 2022 07:33:15 -0400 Subject: [PATCH] Move to Arc/RwLock --- coins/monero/src/ringct/clsag/multisig.rs | 14 ++++++------ coins/monero/src/tests/clsag.rs | 4 ++-- coins/monero/src/wallet/send/multisig.rs | 26 +++++++++++------------ crypto/frost/src/sign.rs | 8 +++---- crypto/frost/src/tests/mod.rs | 8 +++---- crypto/frost/src/tests/schnorr.rs | 6 +++--- crypto/frost/src/tests/vectors.rs | 4 ++-- 7 files changed, 34 insertions(+), 36 deletions(-) diff --git a/coins/monero/src/ringct/clsag/multisig.rs b/coins/monero/src/ringct/clsag/multisig.rs index c42ac029..8aaae8f8 100644 --- a/coins/monero/src/ringct/clsag/multisig.rs +++ b/coins/monero/src/ringct/clsag/multisig.rs @@ -1,5 +1,5 @@ use core::fmt::Debug; -use std::{rc::Rc, cell::RefCell}; +use std::sync::{Arc, RwLock}; use rand_core::{RngCore, CryptoRng, SeedableRng}; use rand_chacha::ChaCha12Rng; @@ -47,7 +47,7 @@ impl ClsagInput { } } -#[derive(Clone, PartialEq, Debug)] +#[derive(Clone, Debug)] pub struct ClsagDetails { input: ClsagInput, mask: Scalar @@ -70,7 +70,7 @@ struct Interim { } #[allow(non_snake_case)] -#[derive(Clone, PartialEq, Debug)] +#[derive(Clone, Debug)] pub struct ClsagMultisig { transcript: Transcript, @@ -79,7 +79,7 @@ pub struct ClsagMultisig { image: EdwardsPoint, AH: (dfg::EdwardsPoint, dfg::EdwardsPoint), - details: Rc>>, + details: Arc>>, msg: Option<[u8; 32]>, interim: Option @@ -88,7 +88,7 @@ pub struct ClsagMultisig { impl ClsagMultisig { pub fn new( transcript: Transcript, - details: Rc>> + details: Arc>> ) -> Result { Ok( ClsagMultisig { @@ -111,11 +111,11 @@ impl ClsagMultisig { } fn input(&self) -> ClsagInput { - self.details.borrow().as_ref().unwrap().input.clone() + (*self.details.read().unwrap()).as_ref().unwrap().input.clone() } fn mask(&self) -> Scalar { - self.details.borrow().as_ref().unwrap().mask + (*self.details.read().unwrap()).as_ref().unwrap().mask } } diff --git a/coins/monero/src/tests/clsag.rs b/coins/monero/src/tests/clsag.rs index 102b64be..b5b90d2c 100644 --- a/coins/monero/src/tests/clsag.rs +++ b/coins/monero/src/tests/clsag.rs @@ -1,5 +1,5 @@ #[cfg(feature = "multisig")] -use std::{cell::RefCell, rc::Rc}; +use std::sync::{Arc, RwLock}; use rand::{RngCore, rngs::OsRng}; @@ -97,7 +97,7 @@ fn clsag_multisig() -> Result<(), MultisigError> { &mut OsRng, ClsagMultisig::new( Transcript::new(b"Monero Serai CLSAG Test"), - Rc::new(RefCell::new(Some( + Arc::new(RwLock::new(Some( ClsagDetails::new( ClsagInput::new( Commitment::new(randomness, AMOUNT), diff --git a/coins/monero/src/wallet/send/multisig.rs b/coins/monero/src/wallet/send/multisig.rs index c1305cde..e1ac0f10 100644 --- a/coins/monero/src/wallet/send/multisig.rs +++ b/coins/monero/src/wallet/send/multisig.rs @@ -1,4 +1,4 @@ -use std::{cell::RefCell, rc::Rc, collections::HashMap}; +use std::{sync::{Arc, RwLock}, collections::HashMap}; use rand_core::{RngCore, CryptoRng, SeedableRng}; use rand_chacha::ChaCha12Rng; @@ -28,7 +28,7 @@ pub struct TransactionMachine { images: Vec, output_masks: Option, - inputs: Vec>>>, + inputs: Vec>>>, clsags: Vec>, tx: Option @@ -49,7 +49,7 @@ impl SignableTransaction { let mut inputs = vec![]; for _ in 0 .. self.inputs.len() { // Doesn't resize as that will use a single Rc for the entire Vec - inputs.push(Rc::new(RefCell::new(None))); + inputs.push(Arc::new(RwLock::new(None))); } let mut clsags = vec![]; @@ -87,7 +87,7 @@ impl SignableTransaction { // Ideally, this would be done post entropy, instead of now, yet doing so would require sign // to be async which isn't preferable. This should be suitably competent though // While this inability means we can immediately create the input, moving it out of the - // Rc RefCell, keeping it within an Rc RefCell keeps our options flexible + // Arc RwLock, keeping it within an Arc RwLock keeps our options flexible let decoys = Decoys::select( // Using a seeded RNG with a specific height, committed to above, should make these decoys // committed to. They'll also be committed to later via the TX message as a whole @@ -107,7 +107,7 @@ impl SignableTransaction { transcript.clone(), inputs[i].clone() ).map_err(|e| TransactionError::MultisigError(e))?, - Rc::new(keys.offset(dalek_ff_group::Scalar(input.key_offset))), + Arc::new(keys.offset(dalek_ff_group::Scalar(input.key_offset))), &included ).map_err(|e| TransactionError::FrostError(e))? ); @@ -270,15 +270,13 @@ impl StateMachine for TransactionMachine { } ); - value.3.replace( - Some( - ClsagDetails::new( - ClsagInput::new( - value.0.commitment, - value.1 - ).map_err(|_| panic!("Signing an input which isn't present in the ring we created for it"))?, - mask - ) + *value.3.write().unwrap() = Some( + ClsagDetails::new( + ClsagInput::new( + value.0.commitment, + value.1 + ).map_err(|_| panic!("Signing an input which isn't present in the ring we created for it"))?, + mask ) ); diff --git a/crypto/frost/src/sign.rs b/crypto/frost/src/sign.rs index 11739fc4..49c1c853 100644 --- a/crypto/frost/src/sign.rs +++ b/crypto/frost/src/sign.rs @@ -1,5 +1,5 @@ use core::fmt; -use std::{rc::Rc, collections::HashMap}; +use std::{sync::Arc, collections::HashMap}; use rand_core::{RngCore, CryptoRng}; @@ -19,7 +19,7 @@ use crate::{ #[derive(Clone)] pub struct Params> { algorithm: A, - keys: Rc>, + keys: Arc>, view: MultisigView, } @@ -27,7 +27,7 @@ pub struct Params> { impl> Params { pub fn new( algorithm: A, - keys: Rc>, + keys: Arc>, included: &[u16], ) -> Result, FrostError> { let mut included = included.to_vec(); @@ -297,7 +297,7 @@ impl> AlgorithmMachine { /// Creates a new machine to generate a key for the specified curve in the specified multisig pub fn new( algorithm: A, - keys: Rc>, + keys: Arc>, included: &[u16], ) -> Result, FrostError> { Ok( diff --git a/crypto/frost/src/tests/mod.rs b/crypto/frost/src/tests/mod.rs index d361d3fd..52fbf515 100644 --- a/crypto/frost/src/tests/mod.rs +++ b/crypto/frost/src/tests/mod.rs @@ -1,4 +1,4 @@ -use std::{rc::Rc, collections::HashMap}; +use std::{sync::Arc, collections::HashMap}; use rand_core::{RngCore, CryptoRng}; @@ -36,7 +36,7 @@ pub fn clone_without( pub fn key_gen( rng: &mut R -) -> HashMap>> { +) -> HashMap>> { let mut params = HashMap::new(); let mut machines = HashMap::new(); @@ -98,7 +98,7 @@ pub fn key_gen( } assert_eq!(group_key.unwrap(), these_keys.group_key()); - keys.insert(*i, Rc::new(these_keys)); + keys.insert(*i, Arc::new(these_keys)); } keys @@ -120,7 +120,7 @@ pub fn recover(keys: &HashMap>) -> C::F { pub fn algorithm_machines>( rng: &mut R, algorithm: A, - keys: &HashMap>>, + keys: &HashMap>>, ) -> HashMap> { let mut included = vec![]; while included.len() < usize::from(keys[&1].params().t()) { diff --git a/crypto/frost/src/tests/schnorr.rs b/crypto/frost/src/tests/schnorr.rs index 2c2ea85e..684107a7 100644 --- a/crypto/frost/src/tests/schnorr.rs +++ b/crypto/frost/src/tests/schnorr.rs @@ -1,4 +1,4 @@ -use std::{marker::PhantomData, rc::Rc, collections::HashMap}; +use std::{marker::PhantomData, sync::Arc, collections::HashMap}; use rand_core::{RngCore, CryptoRng}; @@ -80,7 +80,7 @@ pub(crate) fn core_batch_verify(rng: &mut R) { fn sign_core( rng: &mut R, group_key: C::G, - keys: &HashMap>> + keys: &HashMap>> ) { const MESSAGE: &'static [u8] = b"Hello, World!"; @@ -111,7 +111,7 @@ fn sign_with_offset(rng: &mut R) { let offset = C::hash_to_F(b"FROST Test sign_with_offset", b"offset"); for i in 1 ..= u16::try_from(keys.len()).unwrap() { - keys.insert(i, Rc::new(keys[&i].offset(offset))); + keys.insert(i, Arc::new(keys[&i].offset(offset))); } let offset_key = group_key + (C::GENERATOR_TABLE * offset); diff --git a/crypto/frost/src/tests/vectors.rs b/crypto/frost/src/tests/vectors.rs index 590d9efa..c4403d07 100644 --- a/crypto/frost/src/tests/vectors.rs +++ b/crypto/frost/src/tests/vectors.rs @@ -1,4 +1,4 @@ -use std::{rc::Rc, collections::HashMap}; +use std::{sync::Arc, collections::HashMap}; use crate::{ Curve, MultisigKeys, @@ -73,7 +73,7 @@ pub fn vectors>(vectors: Vectors) { *i, AlgorithmMachine::new( Schnorr::::new(), - Rc::new(keys[i].clone()), + Arc::new(keys[i].clone()), vectors.included.clone() ).unwrap() ));