2022-10-25 23:17:25 -05:00
|
|
|
use std::collections::HashMap;
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-12-13 20:25:32 -05:00
|
|
|
use rand_core::{RngCore, CryptoRng};
|
2022-04-23 03:49:30 -04:00
|
|
|
|
2022-10-29 03:54:42 -05:00
|
|
|
pub use dkg::tests::{key_gen, recover_key};
|
2022-05-30 16:37:51 -04:00
|
|
|
|
2022-05-25 00:28:57 -04:00
|
|
|
use crate::{
|
2022-10-29 03:54:42 -05:00
|
|
|
Curve, ThresholdKeys,
|
2022-05-25 00:28:57 -04:00
|
|
|
algorithm::Algorithm,
|
2022-10-25 23:17:25 -05:00
|
|
|
sign::{Writable, PreprocessMachine, SignMachine, SignatureMachine, AlgorithmMachine},
|
2022-04-21 21:36:18 -04:00
|
|
|
};
|
|
|
|
|
|
2022-09-29 06:02:43 -04:00
|
|
|
/// Vectorized test suite to ensure consistency.
|
2022-06-03 01:25:46 -04:00
|
|
|
pub mod vectors;
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-05-25 00:28:57 -04:00
|
|
|
// Literal test definitions to run during `cargo test`
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod literal;
|
|
|
|
|
|
2022-09-29 07:08:20 -04:00
|
|
|
/// Constant amount of participants to use when testing.
|
2022-05-25 00:28:57 -04:00
|
|
|
pub const PARTICIPANTS: u16 = 5;
|
2022-09-29 07:08:20 -04:00
|
|
|
/// Constant threshold of participants to use when signing.
|
2022-05-25 00:28:57 -04:00
|
|
|
pub const THRESHOLD: u16 = ((PARTICIPANTS / 3) * 2) + 1;
|
|
|
|
|
|
2022-09-29 07:08:20 -04:00
|
|
|
/// Clone a map without a specific value.
|
2022-05-25 00:28:57 -04:00
|
|
|
pub fn clone_without<K: Clone + std::cmp::Eq + std::hash::Hash, V: Clone>(
|
2022-05-24 21:41:14 -04:00
|
|
|
map: &HashMap<K, V>,
|
2022-07-15 01:26:07 -04:00
|
|
|
without: &K,
|
2022-05-24 21:41:14 -04:00
|
|
|
) -> HashMap<K, V> {
|
|
|
|
|
let mut res = map.clone();
|
|
|
|
|
res.remove(without).unwrap();
|
|
|
|
|
res
|
2022-04-21 21:36:18 -04:00
|
|
|
}
|
|
|
|
|
|
2022-09-29 07:08:20 -04:00
|
|
|
/// Spawn algorithm machines for a random selection of signers, each executing the given algorithm.
|
2022-05-25 00:28:57 -04:00
|
|
|
pub fn algorithm_machines<R: RngCore, C: Curve, A: Algorithm<C>>(
|
|
|
|
|
rng: &mut R,
|
2022-05-24 21:41:14 -04:00
|
|
|
algorithm: A,
|
2022-10-29 03:54:42 -05:00
|
|
|
keys: &HashMap<u16, ThresholdKeys<C>>,
|
2022-05-25 00:28:57 -04:00
|
|
|
) -> HashMap<u16, AlgorithmMachine<C, A>> {
|
|
|
|
|
let mut included = vec![];
|
|
|
|
|
while included.len() < usize::from(keys[&1].params().t()) {
|
|
|
|
|
let n = u16::try_from((rng.next_u64() % u64::try_from(keys.len()).unwrap()) + 1).unwrap();
|
|
|
|
|
if included.contains(&n) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
included.push(n);
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
keys
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|(i, keys)| {
|
2022-07-22 02:34:36 -04:00
|
|
|
if included.contains(i) {
|
2022-12-08 19:04:35 -05:00
|
|
|
Some((*i, AlgorithmMachine::new(algorithm.clone(), keys.clone()).unwrap()))
|
2022-07-15 01:26:07 -04:00
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
2022-05-25 00:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
2023-01-01 01:54:18 -05:00
|
|
|
// Run the commit step and generate signature shares
|
|
|
|
|
#[allow(clippy::type_complexity)]
|
|
|
|
|
pub(crate) fn commit_and_shares<
|
2022-12-08 19:04:35 -05:00
|
|
|
R: RngCore + CryptoRng,
|
|
|
|
|
M: PreprocessMachine,
|
|
|
|
|
F: FnMut(&mut R, &mut HashMap<u16, M::SignMachine>),
|
|
|
|
|
>(
|
2022-05-25 00:28:57 -04:00
|
|
|
rng: &mut R,
|
|
|
|
|
mut machines: HashMap<u16, M>,
|
2022-12-08 19:04:35 -05:00
|
|
|
mut cache: F,
|
2022-07-15 01:26:07 -04:00
|
|
|
msg: &[u8],
|
2023-01-01 01:54:18 -05:00
|
|
|
) -> (
|
|
|
|
|
HashMap<u16, <M::SignMachine as SignMachine<M::Signature>>::SignatureMachine>,
|
|
|
|
|
HashMap<u16, <M::SignMachine as SignMachine<M::Signature>>::SignatureShare>,
|
|
|
|
|
) {
|
2022-05-24 21:41:14 -04:00
|
|
|
let mut commitments = HashMap::new();
|
2022-07-15 01:26:07 -04:00
|
|
|
let mut machines = machines
|
|
|
|
|
.drain()
|
|
|
|
|
.map(|(i, machine)| {
|
|
|
|
|
let (machine, preprocess) = machine.preprocess(rng);
|
2022-10-25 23:17:25 -05:00
|
|
|
commitments.insert(i, {
|
|
|
|
|
let mut buf = vec![];
|
|
|
|
|
preprocess.write(&mut buf).unwrap();
|
|
|
|
|
machine.read_preprocess::<&[u8]>(&mut buf.as_ref()).unwrap()
|
|
|
|
|
});
|
2022-07-15 01:26:07 -04:00
|
|
|
(i, machine)
|
|
|
|
|
})
|
|
|
|
|
.collect::<HashMap<_, _>>();
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2022-12-08 19:04:35 -05:00
|
|
|
cache(rng, &mut machines);
|
|
|
|
|
|
2022-05-24 21:41:14 -04:00
|
|
|
let mut shares = HashMap::new();
|
2023-01-01 01:54:18 -05:00
|
|
|
let machines = machines
|
2022-07-15 01:26:07 -04:00
|
|
|
.drain()
|
|
|
|
|
.map(|(i, machine)| {
|
|
|
|
|
let (machine, share) = machine.sign(clone_without(&commitments, &i), msg).unwrap();
|
2022-10-25 23:17:25 -05:00
|
|
|
shares.insert(i, {
|
|
|
|
|
let mut buf = vec![];
|
|
|
|
|
share.write(&mut buf).unwrap();
|
|
|
|
|
machine.read_share::<&[u8]>(&mut buf.as_ref()).unwrap()
|
|
|
|
|
});
|
2022-07-15 01:26:07 -04:00
|
|
|
(i, machine)
|
|
|
|
|
})
|
|
|
|
|
.collect::<HashMap<_, _>>();
|
2022-04-21 21:36:18 -04:00
|
|
|
|
2023-01-01 01:54:18 -05:00
|
|
|
(machines, shares)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn sign_internal<
|
|
|
|
|
R: RngCore + CryptoRng,
|
|
|
|
|
M: PreprocessMachine,
|
|
|
|
|
F: FnMut(&mut R, &mut HashMap<u16, M::SignMachine>),
|
|
|
|
|
>(
|
|
|
|
|
rng: &mut R,
|
|
|
|
|
machines: HashMap<u16, M>,
|
|
|
|
|
cache: F,
|
|
|
|
|
msg: &[u8],
|
|
|
|
|
) -> M::Signature {
|
|
|
|
|
let (mut machines, shares) = commit_and_shares(rng, machines, cache, msg);
|
|
|
|
|
|
2022-05-24 21:41:14 -04:00
|
|
|
let mut signature = None;
|
2022-06-24 08:40:14 -04:00
|
|
|
for (i, machine) in machines.drain() {
|
|
|
|
|
let sig = machine.complete(clone_without(&shares, &i)).unwrap();
|
2022-05-24 21:41:14 -04:00
|
|
|
if signature.is_none() {
|
2022-05-25 00:28:57 -04:00
|
|
|
signature = Some(sig.clone());
|
2022-05-24 21:41:14 -04:00
|
|
|
}
|
2022-05-25 00:28:57 -04:00
|
|
|
assert_eq!(&sig, signature.as_ref().unwrap());
|
2022-05-24 21:41:14 -04:00
|
|
|
}
|
2022-05-25 00:28:57 -04:00
|
|
|
signature.unwrap()
|
2022-04-21 21:36:18 -04:00
|
|
|
}
|
2022-12-08 19:04:35 -05:00
|
|
|
|
|
|
|
|
/// Execute the signing protocol, without caching any machines. This isn't as comprehensive at
|
|
|
|
|
/// testing as sign, and accordingly isn't preferred, yet is usable for machines not supporting
|
|
|
|
|
/// caching.
|
|
|
|
|
pub fn sign_without_caching<R: RngCore + CryptoRng, M: PreprocessMachine>(
|
|
|
|
|
rng: &mut R,
|
|
|
|
|
machines: HashMap<u16, M>,
|
|
|
|
|
msg: &[u8],
|
|
|
|
|
) -> M::Signature {
|
|
|
|
|
sign_internal(rng, machines, |_, _| {}, msg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Execute the signing protocol, randomly caching various machines to ensure they can cache
|
|
|
|
|
/// successfully.
|
|
|
|
|
pub fn sign<R: RngCore + CryptoRng, M: PreprocessMachine>(
|
|
|
|
|
rng: &mut R,
|
|
|
|
|
params: <M::SignMachine as SignMachine<M::Signature>>::Params,
|
|
|
|
|
mut keys: HashMap<u16, <M::SignMachine as SignMachine<M::Signature>>::Keys>,
|
|
|
|
|
machines: HashMap<u16, M>,
|
|
|
|
|
msg: &[u8],
|
|
|
|
|
) -> M::Signature {
|
|
|
|
|
sign_internal(
|
|
|
|
|
rng,
|
|
|
|
|
machines,
|
|
|
|
|
|rng, machines| {
|
|
|
|
|
// Cache and rebuild half of the machines
|
2023-01-01 04:18:23 -05:00
|
|
|
let mut included = machines.keys().cloned().collect::<Vec<_>>();
|
2022-12-08 19:04:35 -05:00
|
|
|
for i in included.drain(..) {
|
|
|
|
|
if (rng.next_u64() % 2) == 0 {
|
|
|
|
|
let cache = machines.remove(&i).unwrap().cache();
|
|
|
|
|
machines.insert(
|
|
|
|
|
i,
|
|
|
|
|
M::SignMachine::from_cache(params.clone(), keys.remove(&i).unwrap(), cache).unwrap(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
msg,
|
|
|
|
|
)
|
|
|
|
|
}
|