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
|
|
|
|
2023-05-10 06:25:40 -04:00
|
|
|
pub use dkg::tests::{key_gen, musig_key_gen, recover_key};
|
2022-05-30 16:37:51 -04:00
|
|
|
|
2022-05-25 00:28:57 -04:00
|
|
|
use crate::{
|
2023-03-01 08:02:45 -05:00
|
|
|
Curve, Participant, ThresholdKeys, FrostError,
|
2023-03-07 05:30:21 -05:00
|
|
|
algorithm::{Algorithm, Hram, IetfSchnorr},
|
2022-10-25 23:17:25 -05:00
|
|
|
sign::{Writable, PreprocessMachine, SignMachine, SignatureMachine, AlgorithmMachine},
|
2022-04-21 21:36:18 -04:00
|
|
|
};
|
|
|
|
|
|
2023-03-01 08:02:45 -05:00
|
|
|
/// Tests for the nonce handling code.
|
|
|
|
|
pub mod nonces;
|
|
|
|
|
use nonces::{test_multi_nonce, test_invalid_commitment, test_invalid_dleq_proof};
|
|
|
|
|
|
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.
|
2023-05-10 06:25:40 -04:00
|
|
|
pub const THRESHOLD: u16 = ((PARTICIPANTS * 2) / 3) + 1;
|
2022-05-25 00:28:57 -04:00
|
|
|
|
2022-09-29 07:08:20 -04:00
|
|
|
/// Clone a map without a specific value.
|
2023-07-08 11:29:05 -04:00
|
|
|
pub fn clone_without<K: Clone + core::cmp::Eq + core::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,
|
2023-12-17 00:01:41 -05:00
|
|
|
algorithm: &A,
|
2023-02-23 06:50:45 -05:00
|
|
|
keys: &HashMap<Participant, ThresholdKeys<C>>,
|
|
|
|
|
) -> HashMap<Participant, AlgorithmMachine<C, A>> {
|
2022-05-25 00:28:57 -04:00
|
|
|
let mut included = vec![];
|
2023-02-23 06:50:45 -05:00
|
|
|
while included.len() < usize::from(keys[&Participant::new(1).unwrap()].params().t()) {
|
|
|
|
|
let n = Participant::new(
|
|
|
|
|
u16::try_from((rng.next_u64() % u64::try_from(keys.len()).unwrap()) + 1).unwrap(),
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2022-05-25 00:28:57 -04:00
|
|
|
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) {
|
2023-03-17 23:43:32 -04:00
|
|
|
Some((*i, AlgorithmMachine::new(algorithm.clone(), keys.clone())))
|
2022-07-15 01:26:07 -04:00
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
2022-05-25 00:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
2023-03-01 08:02:45 -05:00
|
|
|
// Run the preprocess step
|
|
|
|
|
pub(crate) fn preprocess<
|
2022-12-08 19:04:35 -05:00
|
|
|
R: RngCore + CryptoRng,
|
|
|
|
|
M: PreprocessMachine,
|
2023-02-23 06:50:45 -05:00
|
|
|
F: FnMut(&mut R, &mut HashMap<Participant, M::SignMachine>),
|
2022-12-08 19:04:35 -05:00
|
|
|
>(
|
2022-05-25 00:28:57 -04:00
|
|
|
rng: &mut R,
|
2023-02-23 06:50:45 -05:00
|
|
|
mut machines: HashMap<Participant, M>,
|
2022-12-08 19:04:35 -05:00
|
|
|
mut cache: F,
|
2023-03-01 08:02:45 -05:00
|
|
|
) -> (HashMap<Participant, M::SignMachine>, HashMap<Participant, M::Preprocess>) {
|
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);
|
|
|
|
|
|
2023-03-01 08:02:45 -05:00
|
|
|
(machines, commitments)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Run the preprocess and generate signature shares
|
|
|
|
|
#[allow(clippy::type_complexity)]
|
|
|
|
|
pub(crate) fn preprocess_and_shares<
|
|
|
|
|
R: RngCore + CryptoRng,
|
|
|
|
|
M: PreprocessMachine,
|
|
|
|
|
F: FnMut(&mut R, &mut HashMap<Participant, M::SignMachine>),
|
|
|
|
|
>(
|
|
|
|
|
rng: &mut R,
|
|
|
|
|
machines: HashMap<Participant, M>,
|
|
|
|
|
cache: F,
|
|
|
|
|
msg: &[u8],
|
|
|
|
|
) -> (
|
|
|
|
|
HashMap<Participant, <M::SignMachine as SignMachine<M::Signature>>::SignatureMachine>,
|
|
|
|
|
HashMap<Participant, <M::SignMachine as SignMachine<M::Signature>>::SignatureShare>,
|
|
|
|
|
) {
|
|
|
|
|
let (mut machines, commitments) = preprocess(rng, machines, cache);
|
|
|
|
|
|
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,
|
2023-02-23 06:50:45 -05:00
|
|
|
F: FnMut(&mut R, &mut HashMap<Participant, M::SignMachine>),
|
2023-01-01 01:54:18 -05:00
|
|
|
>(
|
|
|
|
|
rng: &mut R,
|
2023-02-23 06:50:45 -05:00
|
|
|
machines: HashMap<Participant, M>,
|
2023-01-01 01:54:18 -05:00
|
|
|
cache: F,
|
|
|
|
|
msg: &[u8],
|
|
|
|
|
) -> M::Signature {
|
2023-03-01 08:02:45 -05:00
|
|
|
let (mut machines, shares) = preprocess_and_shares(rng, machines, cache, msg);
|
2023-01-01 01:54:18 -05:00
|
|
|
|
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,
|
2023-02-23 06:50:45 -05:00
|
|
|
machines: HashMap<Participant, M>,
|
2022-12-08 19:04:35 -05:00
|
|
|
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,
|
2023-12-17 00:01:41 -05:00
|
|
|
params: &<M::SignMachine as SignMachine<M::Signature>>::Params,
|
2023-02-23 06:50:45 -05:00
|
|
|
mut keys: HashMap<Participant, <M::SignMachine as SignMachine<M::Signature>>::Keys>,
|
|
|
|
|
machines: HashMap<Participant, M>,
|
2022-12-08 19:04:35 -05:00
|
|
|
msg: &[u8],
|
|
|
|
|
) -> M::Signature {
|
|
|
|
|
sign_internal(
|
|
|
|
|
rng,
|
|
|
|
|
machines,
|
|
|
|
|
|rng, machines| {
|
|
|
|
|
// Cache and rebuild half of the machines
|
2023-12-16 20:54:24 -05:00
|
|
|
let included = machines.keys().copied().collect::<Vec<_>>();
|
2023-07-08 11:29:05 -04:00
|
|
|
for i in included {
|
2022-12-08 19:04:35 -05:00
|
|
|
if (rng.next_u64() % 2) == 0 {
|
|
|
|
|
let cache = machines.remove(&i).unwrap().cache();
|
|
|
|
|
machines.insert(
|
|
|
|
|
i,
|
Coordinator Cleanup (#481)
* Move logic for evaluating if a cosign should occur to its own file
Cleans it up and makes it more robust.
* Have expected_next_batch return an error instead of retrying
While convenient to offer an error-free implementation, it potentially caused
very long lived lock acquisitions in handle_processor_message.
* Unify and clean DkgConfirmer and DkgRemoval
Does so via adding a new file for the common code, SigningProtocol.
Modifies from_cache to return the preprocess with the machine, as there's no
reason not to. Also removes an unused Result around the type.
Clarifies the security around deterministic nonces, removing them for
saved-to-disk cached preprocesses. The cached preprocesses are encrypted as the
DB is not a proper secret store.
Moves arguments always present in the protocol from function arguments into the
struct itself.
Removes the horribly ugly code in DkgRemoval, fixing multiple issues present
with it which would cause it to fail on use.
* Set SeraiBlockNumber in cosign.rs as it's used by the cosigning protocol
* Remove unnecessary Clone from lambdas in coordinator
* Remove the EventDb from Tributary scanner
We used per-Transaction DB TXNs so on error, we don't have to rescan the entire
block yet only the rest of it. We prevented scanning multiple transactions by
tracking which we already had.
This is over-engineered and not worth it.
* Implement borsh for HasEvents, removing the manual encoding
* Merge DkgConfirmer and DkgRemoval into signing_protocol.rs
Fixes a bug in DkgConfirmer which would cause it to improperly handle indexes
if any validator had multiple key shares.
* Strictly type DataSpecification's Label
* Correct threshold_i_map_to_keys_and_musig_i_map
It didn't include the participant's own index and accordingly was offset.
* Create TributaryBlockHandler
This struct contains all variables prior passed to handle_block and stops them
from being passed around again and again.
This also ensures fatal_slash is only called while handling a block, as needed
as it expects to operate under perfect consensus.
* Inline accumulate, store confirmation nonces with shares
Inlining accumulate makes sense due to the amount of data accumulate needed to
be passed.
Storing confirmation nonces with shares ensures that both are available or
neither. Prior, one could be yet the other may not have been (requiring an
assert in runtime to ensure we didn't bungle it somehow).
* Create helper functions for handling DkgRemoval/SubstrateSign/Sign Tributary TXs
* Move Label into SignData
All of our transactions which use SignData end up with the same common usage
pattern for Label, justifying this.
Removes 3 transactions, explicitly de-duplicating their handlers.
* Remove CurrentlyCompletingKeyPair for the non-contextual DkgKeyPair
* Remove the manual read/write for TributarySpec for borsh
This struct doesn't have any optimizations booned by the manual impl. Using
borsh reduces our scope.
* Use temporary variables to further minimize LoC in tributary handler
* Remove usage of tuples for non-trivial Tributary transactions
* Remove serde from dkg
serde could be used to deserialize intenrally inconsistent objects which could
lead to panics or faults.
The BorshDeserialize derives have been replaced with a manual implementation
which won't produce inconsistent objects.
* Abstract Future generics using new trait definitions in coordinator
* Move published_signed_transaction to tributary/mod.rs to reduce the size of main.rs
* Split coordinator/src/tributary/mod.rs into spec.rs and transaction.rs
2023-12-10 20:21:44 -05:00
|
|
|
M::SignMachine::from_cache(params.clone(), keys.remove(&i).unwrap(), cache).0,
|
2022-12-08 19:04:35 -05:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
msg,
|
|
|
|
|
)
|
|
|
|
|
}
|
2023-03-01 08:02:45 -05:00
|
|
|
|
2023-05-10 06:25:40 -04:00
|
|
|
/// Test a basic Schnorr signature with the provided keys.
|
|
|
|
|
pub fn test_schnorr_with_keys<R: RngCore + CryptoRng, C: Curve, H: Hram<C>>(
|
|
|
|
|
rng: &mut R,
|
2023-12-17 00:01:41 -05:00
|
|
|
keys: &HashMap<Participant, ThresholdKeys<C>>,
|
2023-05-10 06:25:40 -04:00
|
|
|
) {
|
2023-03-01 08:02:45 -05:00
|
|
|
const MSG: &[u8] = b"Hello, World!";
|
|
|
|
|
|
2023-12-17 00:01:41 -05:00
|
|
|
let machines = algorithm_machines(&mut *rng, &IetfSchnorr::<C, H>::ietf(), keys);
|
|
|
|
|
let sig = sign(&mut *rng, &IetfSchnorr::<C, H>::ietf(), keys.clone(), machines, MSG);
|
2023-03-01 08:02:45 -05:00
|
|
|
let group_key = keys[&Participant::new(1).unwrap()].group_key();
|
|
|
|
|
assert!(sig.verify(group_key, H::hram(&sig.R, &group_key, MSG)));
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-10 06:25:40 -04:00
|
|
|
/// Test a basic Schnorr signature.
|
|
|
|
|
pub fn test_schnorr<R: RngCore + CryptoRng, C: Curve, H: Hram<C>>(rng: &mut R) {
|
|
|
|
|
let keys = key_gen(&mut *rng);
|
2023-12-17 00:01:41 -05:00
|
|
|
test_schnorr_with_keys::<_, _, H>(&mut *rng, &keys)
|
2023-05-10 06:25:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Test a basic Schnorr signature, yet with MuSig.
|
|
|
|
|
pub fn test_musig_schnorr<R: RngCore + CryptoRng, C: Curve, H: Hram<C>>(rng: &mut R) {
|
|
|
|
|
let keys = musig_key_gen(&mut *rng);
|
2023-12-17 00:01:41 -05:00
|
|
|
test_schnorr_with_keys::<_, _, H>(&mut *rng, &keys)
|
2023-05-10 06:25:40 -04:00
|
|
|
}
|
|
|
|
|
|
2023-03-20 20:10:00 -04:00
|
|
|
/// Test an offset Schnorr signature.
|
2023-03-01 08:02:45 -05:00
|
|
|
pub fn test_offset_schnorr<R: RngCore + CryptoRng, C: Curve, H: Hram<C>>(rng: &mut R) {
|
|
|
|
|
const MSG: &[u8] = b"Hello, World!";
|
|
|
|
|
|
|
|
|
|
let mut keys = key_gen(&mut *rng);
|
|
|
|
|
let group_key = keys[&Participant::new(1).unwrap()].group_key();
|
|
|
|
|
|
|
|
|
|
let offset = C::F::from(5);
|
|
|
|
|
let offset_key = group_key + (C::generator() * offset);
|
2023-07-08 11:29:05 -04:00
|
|
|
for keys in keys.values_mut() {
|
2023-03-01 08:02:45 -05:00
|
|
|
*keys = keys.offset(offset);
|
|
|
|
|
assert_eq!(keys.group_key(), offset_key);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-17 00:01:41 -05:00
|
|
|
let machines = algorithm_machines(&mut *rng, &IetfSchnorr::<C, H>::ietf(), &keys);
|
|
|
|
|
let sig = sign(&mut *rng, &IetfSchnorr::<C, H>::ietf(), keys.clone(), machines, MSG);
|
2023-03-01 08:02:45 -05:00
|
|
|
let group_key = keys[&Participant::new(1).unwrap()].group_key();
|
|
|
|
|
assert!(sig.verify(offset_key, H::hram(&sig.R, &group_key, MSG)));
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-20 20:10:00 -04:00
|
|
|
/// Test blame for an invalid Schnorr signature share.
|
2023-03-01 08:02:45 -05:00
|
|
|
pub fn test_schnorr_blame<R: RngCore + CryptoRng, C: Curve, H: Hram<C>>(rng: &mut R) {
|
|
|
|
|
const MSG: &[u8] = b"Hello, World!";
|
|
|
|
|
|
|
|
|
|
let keys = key_gen(&mut *rng);
|
2023-12-17 00:01:41 -05:00
|
|
|
let machines = algorithm_machines(&mut *rng, &IetfSchnorr::<C, H>::ietf(), &keys);
|
2023-03-01 08:02:45 -05:00
|
|
|
|
|
|
|
|
let (mut machines, shares) = preprocess_and_shares(&mut *rng, machines, |_, _| {}, MSG);
|
|
|
|
|
|
|
|
|
|
for (i, machine) in machines.drain() {
|
|
|
|
|
let mut shares = clone_without(&shares, &i);
|
|
|
|
|
|
|
|
|
|
// Select a random participant to give an invalid share
|
|
|
|
|
let participants = shares.keys().collect::<Vec<_>>();
|
|
|
|
|
let faulty = *participants
|
|
|
|
|
[usize::try_from(rng.next_u64() % u64::try_from(participants.len()).unwrap()).unwrap()];
|
|
|
|
|
shares.get_mut(&faulty).unwrap().invalidate();
|
|
|
|
|
|
|
|
|
|
assert_eq!(machine.complete(shares).err(), Some(FrostError::InvalidShare(faulty)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-20 20:10:00 -04:00
|
|
|
/// Run a variety of tests against a ciphersuite.
|
2023-03-01 08:02:45 -05:00
|
|
|
pub fn test_ciphersuite<R: RngCore + CryptoRng, C: Curve, H: Hram<C>>(rng: &mut R) {
|
|
|
|
|
test_schnorr::<R, C, H>(rng);
|
2023-05-10 06:25:40 -04:00
|
|
|
test_musig_schnorr::<R, C, H>(rng);
|
2023-03-01 08:02:45 -05:00
|
|
|
test_offset_schnorr::<R, C, H>(rng);
|
|
|
|
|
test_schnorr_blame::<R, C, H>(rng);
|
|
|
|
|
|
|
|
|
|
test_multi_nonce::<R, C>(rng);
|
|
|
|
|
test_invalid_commitment::<R, C>(rng);
|
|
|
|
|
test_invalid_dleq_proof::<R, C>(rng);
|
|
|
|
|
}
|