mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 12:19:24 +00:00
Move to Arc/RwLock
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
use core::fmt::Debug;
|
use core::fmt::Debug;
|
||||||
use std::{rc::Rc, cell::RefCell};
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
use rand_core::{RngCore, CryptoRng, SeedableRng};
|
use rand_core::{RngCore, CryptoRng, SeedableRng};
|
||||||
use rand_chacha::ChaCha12Rng;
|
use rand_chacha::ChaCha12Rng;
|
||||||
@@ -47,7 +47,7 @@ impl ClsagInput {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct ClsagDetails {
|
pub struct ClsagDetails {
|
||||||
input: ClsagInput,
|
input: ClsagInput,
|
||||||
mask: Scalar
|
mask: Scalar
|
||||||
@@ -70,7 +70,7 @@ struct Interim {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
#[derive(Clone, PartialEq, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct ClsagMultisig {
|
pub struct ClsagMultisig {
|
||||||
transcript: Transcript,
|
transcript: Transcript,
|
||||||
|
|
||||||
@@ -79,7 +79,7 @@ pub struct ClsagMultisig {
|
|||||||
image: EdwardsPoint,
|
image: EdwardsPoint,
|
||||||
AH: (dfg::EdwardsPoint, dfg::EdwardsPoint),
|
AH: (dfg::EdwardsPoint, dfg::EdwardsPoint),
|
||||||
|
|
||||||
details: Rc<RefCell<Option<ClsagDetails>>>,
|
details: Arc<RwLock<Option<ClsagDetails>>>,
|
||||||
|
|
||||||
msg: Option<[u8; 32]>,
|
msg: Option<[u8; 32]>,
|
||||||
interim: Option<Interim>
|
interim: Option<Interim>
|
||||||
@@ -88,7 +88,7 @@ pub struct ClsagMultisig {
|
|||||||
impl ClsagMultisig {
|
impl ClsagMultisig {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
transcript: Transcript,
|
transcript: Transcript,
|
||||||
details: Rc<RefCell<Option<ClsagDetails>>>
|
details: Arc<RwLock<Option<ClsagDetails>>>
|
||||||
) -> Result<ClsagMultisig, MultisigError> {
|
) -> Result<ClsagMultisig, MultisigError> {
|
||||||
Ok(
|
Ok(
|
||||||
ClsagMultisig {
|
ClsagMultisig {
|
||||||
@@ -111,11 +111,11 @@ impl ClsagMultisig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn input(&self) -> ClsagInput {
|
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 {
|
fn mask(&self) -> Scalar {
|
||||||
self.details.borrow().as_ref().unwrap().mask
|
(*self.details.read().unwrap()).as_ref().unwrap().mask
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#[cfg(feature = "multisig")]
|
#[cfg(feature = "multisig")]
|
||||||
use std::{cell::RefCell, rc::Rc};
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
use rand::{RngCore, rngs::OsRng};
|
use rand::{RngCore, rngs::OsRng};
|
||||||
|
|
||||||
@@ -97,7 +97,7 @@ fn clsag_multisig() -> Result<(), MultisigError> {
|
|||||||
&mut OsRng,
|
&mut OsRng,
|
||||||
ClsagMultisig::new(
|
ClsagMultisig::new(
|
||||||
Transcript::new(b"Monero Serai CLSAG Test"),
|
Transcript::new(b"Monero Serai CLSAG Test"),
|
||||||
Rc::new(RefCell::new(Some(
|
Arc::new(RwLock::new(Some(
|
||||||
ClsagDetails::new(
|
ClsagDetails::new(
|
||||||
ClsagInput::new(
|
ClsagInput::new(
|
||||||
Commitment::new(randomness, AMOUNT),
|
Commitment::new(randomness, AMOUNT),
|
||||||
|
|||||||
@@ -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_core::{RngCore, CryptoRng, SeedableRng};
|
||||||
use rand_chacha::ChaCha12Rng;
|
use rand_chacha::ChaCha12Rng;
|
||||||
@@ -28,7 +28,7 @@ pub struct TransactionMachine {
|
|||||||
|
|
||||||
images: Vec<EdwardsPoint>,
|
images: Vec<EdwardsPoint>,
|
||||||
output_masks: Option<Scalar>,
|
output_masks: Option<Scalar>,
|
||||||
inputs: Vec<Rc<RefCell<Option<ClsagDetails>>>>,
|
inputs: Vec<Arc<RwLock<Option<ClsagDetails>>>>,
|
||||||
clsags: Vec<AlgorithmMachine<Ed25519, ClsagMultisig>>,
|
clsags: Vec<AlgorithmMachine<Ed25519, ClsagMultisig>>,
|
||||||
|
|
||||||
tx: Option<Transaction>
|
tx: Option<Transaction>
|
||||||
@@ -49,7 +49,7 @@ impl SignableTransaction {
|
|||||||
let mut inputs = vec![];
|
let mut inputs = vec![];
|
||||||
for _ in 0 .. self.inputs.len() {
|
for _ in 0 .. self.inputs.len() {
|
||||||
// Doesn't resize as that will use a single Rc for the entire Vec
|
// 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![];
|
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
|
// 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
|
// 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
|
// 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(
|
let decoys = Decoys::select(
|
||||||
// Using a seeded RNG with a specific height, committed to above, should make these decoys
|
// 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
|
// committed to. They'll also be committed to later via the TX message as a whole
|
||||||
@@ -107,7 +107,7 @@ impl SignableTransaction {
|
|||||||
transcript.clone(),
|
transcript.clone(),
|
||||||
inputs[i].clone()
|
inputs[i].clone()
|
||||||
).map_err(|e| TransactionError::MultisigError(e))?,
|
).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
|
&included
|
||||||
).map_err(|e| TransactionError::FrostError(e))?
|
).map_err(|e| TransactionError::FrostError(e))?
|
||||||
);
|
);
|
||||||
@@ -270,8 +270,7 @@ impl StateMachine for TransactionMachine {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
value.3.replace(
|
*value.3.write().unwrap() = Some(
|
||||||
Some(
|
|
||||||
ClsagDetails::new(
|
ClsagDetails::new(
|
||||||
ClsagInput::new(
|
ClsagInput::new(
|
||||||
value.0.commitment,
|
value.0.commitment,
|
||||||
@@ -279,7 +278,6 @@ impl StateMachine for TransactionMachine {
|
|||||||
).map_err(|_| panic!("Signing an input which isn't present in the ring we created for it"))?,
|
).map_err(|_| panic!("Signing an input which isn't present in the ring we created for it"))?,
|
||||||
mask
|
mask
|
||||||
)
|
)
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
self.clsags.push(value.4);
|
self.clsags.push(value.4);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use core::fmt;
|
use core::fmt;
|
||||||
use std::{rc::Rc, collections::HashMap};
|
use std::{sync::Arc, collections::HashMap};
|
||||||
|
|
||||||
use rand_core::{RngCore, CryptoRng};
|
use rand_core::{RngCore, CryptoRng};
|
||||||
|
|
||||||
@@ -19,7 +19,7 @@ use crate::{
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Params<C: Curve, A: Algorithm<C>> {
|
pub struct Params<C: Curve, A: Algorithm<C>> {
|
||||||
algorithm: A,
|
algorithm: A,
|
||||||
keys: Rc<MultisigKeys<C>>,
|
keys: Arc<MultisigKeys<C>>,
|
||||||
view: MultisigView<C>,
|
view: MultisigView<C>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,7 +27,7 @@ pub struct Params<C: Curve, A: Algorithm<C>> {
|
|||||||
impl<C: Curve, A: Algorithm<C>> Params<C, A> {
|
impl<C: Curve, A: Algorithm<C>> Params<C, A> {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
algorithm: A,
|
algorithm: A,
|
||||||
keys: Rc<MultisigKeys<C>>,
|
keys: Arc<MultisigKeys<C>>,
|
||||||
included: &[u16],
|
included: &[u16],
|
||||||
) -> Result<Params<C, A>, FrostError> {
|
) -> Result<Params<C, A>, FrostError> {
|
||||||
let mut included = included.to_vec();
|
let mut included = included.to_vec();
|
||||||
@@ -297,7 +297,7 @@ impl<C: Curve, A: Algorithm<C>> AlgorithmMachine<C, A> {
|
|||||||
/// Creates a new machine to generate a key for the specified curve in the specified multisig
|
/// Creates a new machine to generate a key for the specified curve in the specified multisig
|
||||||
pub fn new(
|
pub fn new(
|
||||||
algorithm: A,
|
algorithm: A,
|
||||||
keys: Rc<MultisigKeys<C>>,
|
keys: Arc<MultisigKeys<C>>,
|
||||||
included: &[u16],
|
included: &[u16],
|
||||||
) -> Result<AlgorithmMachine<C, A>, FrostError> {
|
) -> Result<AlgorithmMachine<C, A>, FrostError> {
|
||||||
Ok(
|
Ok(
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use std::{rc::Rc, collections::HashMap};
|
use std::{sync::Arc, collections::HashMap};
|
||||||
|
|
||||||
use rand_core::{RngCore, CryptoRng};
|
use rand_core::{RngCore, CryptoRng};
|
||||||
|
|
||||||
@@ -36,7 +36,7 @@ pub fn clone_without<K: Clone + std::cmp::Eq + std::hash::Hash, V: Clone>(
|
|||||||
|
|
||||||
pub fn key_gen<R: RngCore + CryptoRng, C: Curve>(
|
pub fn key_gen<R: RngCore + CryptoRng, C: Curve>(
|
||||||
rng: &mut R
|
rng: &mut R
|
||||||
) -> HashMap<u16, Rc<MultisigKeys<C>>> {
|
) -> HashMap<u16, Arc<MultisigKeys<C>>> {
|
||||||
let mut params = HashMap::new();
|
let mut params = HashMap::new();
|
||||||
let mut machines = HashMap::new();
|
let mut machines = HashMap::new();
|
||||||
|
|
||||||
@@ -98,7 +98,7 @@ pub fn key_gen<R: RngCore + CryptoRng, C: Curve>(
|
|||||||
}
|
}
|
||||||
assert_eq!(group_key.unwrap(), these_keys.group_key());
|
assert_eq!(group_key.unwrap(), these_keys.group_key());
|
||||||
|
|
||||||
keys.insert(*i, Rc::new(these_keys));
|
keys.insert(*i, Arc::new(these_keys));
|
||||||
}
|
}
|
||||||
|
|
||||||
keys
|
keys
|
||||||
@@ -120,7 +120,7 @@ pub fn recover<C: Curve>(keys: &HashMap<u16, MultisigKeys<C>>) -> C::F {
|
|||||||
pub fn algorithm_machines<R: RngCore, C: Curve, A: Algorithm<C>>(
|
pub fn algorithm_machines<R: RngCore, C: Curve, A: Algorithm<C>>(
|
||||||
rng: &mut R,
|
rng: &mut R,
|
||||||
algorithm: A,
|
algorithm: A,
|
||||||
keys: &HashMap<u16, Rc<MultisigKeys<C>>>,
|
keys: &HashMap<u16, Arc<MultisigKeys<C>>>,
|
||||||
) -> HashMap<u16, AlgorithmMachine<C, A>> {
|
) -> HashMap<u16, AlgorithmMachine<C, A>> {
|
||||||
let mut included = vec![];
|
let mut included = vec![];
|
||||||
while included.len() < usize::from(keys[&1].params().t()) {
|
while included.len() < usize::from(keys[&1].params().t()) {
|
||||||
|
|||||||
@@ -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};
|
use rand_core::{RngCore, CryptoRng};
|
||||||
|
|
||||||
@@ -80,7 +80,7 @@ pub(crate) fn core_batch_verify<R: RngCore + CryptoRng, C: Curve>(rng: &mut R) {
|
|||||||
fn sign_core<R: RngCore + CryptoRng, C: Curve>(
|
fn sign_core<R: RngCore + CryptoRng, C: Curve>(
|
||||||
rng: &mut R,
|
rng: &mut R,
|
||||||
group_key: C::G,
|
group_key: C::G,
|
||||||
keys: &HashMap<u16, Rc<MultisigKeys<C>>>
|
keys: &HashMap<u16, Arc<MultisigKeys<C>>>
|
||||||
) {
|
) {
|
||||||
const MESSAGE: &'static [u8] = b"Hello, World!";
|
const MESSAGE: &'static [u8] = b"Hello, World!";
|
||||||
|
|
||||||
@@ -111,7 +111,7 @@ fn sign_with_offset<R: RngCore + CryptoRng, C: Curve>(rng: &mut R) {
|
|||||||
|
|
||||||
let offset = C::hash_to_F(b"FROST Test sign_with_offset", b"offset");
|
let offset = C::hash_to_F(b"FROST Test sign_with_offset", b"offset");
|
||||||
for i in 1 ..= u16::try_from(keys.len()).unwrap() {
|
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);
|
let offset_key = group_key + (C::GENERATOR_TABLE * offset);
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use std::{rc::Rc, collections::HashMap};
|
use std::{sync::Arc, collections::HashMap};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Curve, MultisigKeys,
|
Curve, MultisigKeys,
|
||||||
@@ -73,7 +73,7 @@ pub fn vectors<C: Curve, H: Hram<C>>(vectors: Vectors) {
|
|||||||
*i,
|
*i,
|
||||||
AlgorithmMachine::new(
|
AlgorithmMachine::new(
|
||||||
Schnorr::<C, H>::new(),
|
Schnorr::<C, H>::new(),
|
||||||
Rc::new(keys[i].clone()),
|
Arc::new(keys[i].clone()),
|
||||||
vectors.included.clone()
|
vectors.included.clone()
|
||||||
).unwrap()
|
).unwrap()
|
||||||
));
|
));
|
||||||
|
|||||||
Reference in New Issue
Block a user