One Round DKG (#589)

* Upstream GBP, divisor, circuit abstraction, and EC gadgets from FCMP++

* Initial eVRF implementation

Not quite done yet. It needs to communicate the resulting points and proofs to
extract them from the Pedersen Commitments in order to return those, and then
be tested.

* Add the openings of the PCs to the eVRF as necessary

* Add implementation of secq256k1

* Make DKG Encryption a bit more flexible

No longer requires the use of an EncryptionKeyMessage, and allows pre-defined
keys for encryption.

* Make NUM_BITS an argument for the field macro

* Have the eVRF take a Zeroizing private key

* Initial eVRF-based DKG

* Add embedwards25519 curve

* Inline the eVRF into the DKG library

Due to how we're handling share encryption, we'd either need two circuits or to
dedicate this circuit to the DKG. The latter makes sense at this time.

* Add documentation to the eVRF-based DKG

* Add paragraph claiming robustness

* Update to the new eVRF proof

* Finish routing the eVRF functionality

Still needs errors and serialization, along with a few other TODOs.

* Add initial eVRF DKG test

* Improve eVRF DKG

Updates how we calculcate verification shares, improves performance when
extracting multiple sets of keys, and adds more to the test for it.

* Start using a proper error for the eVRF DKG

* Resolve various TODOs

Supports recovering multiple key shares from the eVRF DKG.

Inlines two loops to save 2**16 iterations.

Adds support for creating a constant time representation of scalars < NUM_BITS.

* Ban zero ECDH keys, document non-zero requirements

* Implement eVRF traits, all the way up to the DKG, for secp256k1/ed25519

* Add Ristretto eVRF trait impls

* Support participating multiple times in the eVRF DKG

* Only participate once per key, not once per key share

* Rewrite processor key-gen around the eVRF DKG

Still a WIP.

* Finish routing the new key gen in the processor

Doesn't touch the tests, coordinator, nor Substrate yet.
`cargo +nightly fmt && cargo +nightly-2024-07-01 clippy --all-features -p serai-processor`
does pass.

* Deduplicate and better document in processor key_gen

* Update serai-processor tests to the new key gen

* Correct amount of yx coefficients, get processor key gen test to pass

* Add embedded elliptic curve keys to Substrate

* Update processor key gen tests to the eVRF DKG

* Have set_keys take signature_participants, not removed_participants

Now no one is removed from the DKG. Only `t` people publish the key however.

Uses a BitVec for an efficient encoding of the participants.

* Update the coordinator binary for the new DKG

This does not yet update any tests.

* Add sensible Debug to key_gen::[Processor, Coordinator]Message

* Have the DKG explicitly declare how to interpolate its shares

Removes the hack for MuSig where we multiply keys by the inverse of their
lagrange interpolation factor.

* Replace Interpolation::None with Interpolation::Constant

Allows the MuSig DKG to keep the secret share as the original private key,
enabling deriving FROST nonces consistently regardless of the MuSig context.

* Get coordinator tests to pass

* Update spec to the new DKG

* Get clippy to pass across the repo

* cargo machete

* Add an extra sleep to ensure expected ordering of `Participation`s

* Update orchestration

* Remove bad panic in coordinator

It expected ConfirmationShare to be n-of-n, not t-of-n.

* Improve documentation on  functions

* Update TX size limit

We now no longer have to support the ridiculous case of having 49 DKG
participations within a 101-of-150 DKG. It does remain quite high due to
needing to _sign_ so many times. It'd may be optimal for parties with multiple
key shares to independently send their preprocesses/shares (despite the
overhead that'll cause with signatures and the transaction structure).

* Correct error in the Processor spec document

* Update a few comments in the validator-sets pallet

* Send/Recv Participation one at a time

Sending all, then attempting to receive all in an expected order, wasn't working
even with notable delays between sending messages. This points to the mempool
not working as expected...

* Correct ThresholdKeys serialization in modular-frost test

* Updating existing TX size limit test for the new DKG parameters

* Increase time allowed for the DKG on the GH CI

* Correct construction of signature_participants in serai-client tests

Fault identified by akil.

* Further contextualize DkgConfirmer by ValidatorSet

Caught by a safety check we wouldn't reuse preprocesses across messages. That
raises the question of we were prior reusing preprocesses (reusing keys)?
Except that'd have caused a variety of signing failures (suggesting we had some
staggered timing avoiding it in practice but yes, this was possible in theory).

* Add necessary calls to set_embedded_elliptic_curve_key in coordinator set rotation tests

* Correct shimmed setting of a secq256k1 key

* cargo fmt

* Don't use `[0; 32]` for the embedded keys in the coordinator rotation test

The key_gen function expects the random values already decided.

* Big-endian secq256k1 scalars

Also restores the prior, safer, Encryption::register function.
This commit is contained in:
Luke Parker
2024-08-16 11:26:07 -07:00
parent 669b2fef72
commit e4e4245ee3
121 changed files with 10388 additions and 2480 deletions

View File

@@ -3,11 +3,14 @@
use std::sync::{OnceLock, Mutex};
use zeroize::Zeroizing;
use rand_core::{RngCore, OsRng};
use ciphersuite::{group::ff::PrimeField, Ciphersuite, Ristretto};
use ciphersuite::{
group::{ff::PrimeField, GroupEncoding},
Ciphersuite, Secp256k1, Ed25519, Ristretto,
};
use dkg::evrf::*;
use serai_client::primitives::NetworkId;
use serai_client::primitives::{NetworkId, insecure_arbitrary_key_from_name};
use messages::{ProcessorMessage, CoordinatorMessage};
use serai_message_queue::{Service, Metadata, client::MessageQueue};
@@ -24,13 +27,42 @@ mod tests;
static UNIQUE_ID: OnceLock<Mutex<u16>> = OnceLock::new();
#[allow(dead_code)]
#[derive(Clone)]
pub struct EvrfPublicKeys {
substrate: [u8; 32],
network: Vec<u8>,
}
pub fn processor_instance(
name: &str,
network: NetworkId,
port: u32,
message_queue_key: <Ristretto as Ciphersuite>::F,
) -> Vec<TestBodySpecification> {
let mut entropy = [0; 32];
OsRng.fill_bytes(&mut entropy);
) -> (Vec<TestBodySpecification>, EvrfPublicKeys) {
let substrate_evrf_key =
insecure_arbitrary_key_from_name::<<Ristretto as EvrfCurve>::EmbeddedCurve>(name);
let substrate_evrf_pub_key =
(<Ristretto as EvrfCurve>::EmbeddedCurve::generator() * substrate_evrf_key).to_bytes();
let substrate_evrf_key = substrate_evrf_key.to_repr();
let (network_evrf_key, network_evrf_pub_key) = match network {
NetworkId::Serai => panic!("starting a processor for Serai"),
NetworkId::Bitcoin | NetworkId::Ethereum => {
let evrf_key =
insecure_arbitrary_key_from_name::<<Secp256k1 as EvrfCurve>::EmbeddedCurve>(name);
let pub_key =
(<Secp256k1 as EvrfCurve>::EmbeddedCurve::generator() * evrf_key).to_bytes().to_vec();
(evrf_key.to_repr(), pub_key)
}
NetworkId::Monero => {
let evrf_key =
insecure_arbitrary_key_from_name::<<Ed25519 as EvrfCurve>::EmbeddedCurve>(name);
let pub_key =
(<Ed25519 as EvrfCurve>::EmbeddedCurve::generator() * evrf_key).to_bytes().to_vec();
(evrf_key.to_repr(), pub_key)
}
};
let network_str = match network {
NetworkId::Serai => panic!("starting a processor for Serai"),
@@ -47,7 +79,8 @@ pub fn processor_instance(
.replace_env(
[
("MESSAGE_QUEUE_KEY".to_string(), hex::encode(message_queue_key.to_repr())),
("ENTROPY".to_string(), hex::encode(entropy)),
("SUBSTRATE_EVRF_KEY".to_string(), hex::encode(substrate_evrf_key)),
("NETWORK_EVRF_KEY".to_string(), hex::encode(network_evrf_key)),
("NETWORK".to_string(), network_str.to_string()),
("NETWORK_RPC_LOGIN".to_string(), format!("{RPC_USER}:{RPC_PASS}")),
("NETWORK_RPC_PORT".to_string(), port.to_string()),
@@ -75,21 +108,27 @@ pub fn processor_instance(
);
}
res
(res, EvrfPublicKeys { substrate: substrate_evrf_pub_key, network: network_evrf_pub_key })
}
pub struct ProcessorKeys {
coordinator: <Ristretto as Ciphersuite>::F,
evrf: EvrfPublicKeys,
}
pub type Handles = (String, String, String, String);
pub fn processor_stack(
name: &str,
network: NetworkId,
network_hostname_override: Option<String>,
) -> (Handles, <Ristretto as Ciphersuite>::F, Vec<TestBodySpecification>) {
) -> (Handles, ProcessorKeys, Vec<TestBodySpecification>) {
let (network_composition, network_rpc_port) = network_instance(network);
let (coord_key, message_queue_keys, message_queue_composition) =
serai_message_queue_tests::instance();
let mut processor_compositions =
processor_instance(network, network_rpc_port, message_queue_keys[&network]);
let (mut processor_compositions, evrf_keys) =
processor_instance(name, network, network_rpc_port, message_queue_keys[&network]);
// Give every item in this stack a unique ID
// Uses a Mutex as we can't generate a 8-byte random ID without hitting hostname length limits
@@ -155,7 +194,7 @@ pub fn processor_stack(
handles[2].clone(),
handles.get(3).cloned().unwrap_or(String::new()),
),
coord_key,
ProcessorKeys { coordinator: coord_key, evrf: evrf_keys },
compositions,
)
}
@@ -170,6 +209,8 @@ pub struct Coordinator {
processor_handle: String,
relayer_handle: String,
evrf_keys: EvrfPublicKeys,
next_send_id: u64,
next_recv_id: u64,
queue: MessageQueue,
@@ -180,7 +221,7 @@ impl Coordinator {
network: NetworkId,
ops: &DockerOperations,
handles: Handles,
coord_key: <Ristretto as Ciphersuite>::F,
keys: ProcessorKeys,
) -> Coordinator {
let rpc = ops.handle(&handles.1).host_port(2287).unwrap();
let rpc = rpc.0.to_string() + ":" + &rpc.1.to_string();
@@ -193,9 +234,11 @@ impl Coordinator {
processor_handle: handles.2,
relayer_handle: handles.3,
evrf_keys: keys.evrf,
next_send_id: 0,
next_recv_id: 0,
queue: MessageQueue::new(Service::Coordinator, rpc, Zeroizing::new(coord_key)),
queue: MessageQueue::new(Service::Coordinator, rpc, Zeroizing::new(keys.coordinator)),
};
// Sleep for up to a minute in case the external network's RPC has yet to start
@@ -302,6 +345,11 @@ impl Coordinator {
res
}
/// Get the eVRF keys for the associated processor.
pub fn evrf_keys(&self) -> EvrfPublicKeys {
self.evrf_keys.clone()
}
/// Send a message to a processor as its coordinator.
pub async fn send_message(&mut self, msg: impl Into<CoordinatorMessage>) {
let msg: CoordinatorMessage = msg.into();

View File

@@ -451,7 +451,7 @@ impl Wallet {
);
}
let to_spend_key = decompress_point(<[u8; 32]>::try_from(to.as_ref()).unwrap()).unwrap();
let to_spend_key = decompress_point(<[u8; 32]>::try_from(to.as_slice()).unwrap()).unwrap();
let to_view_key = additional_key::<Monero>(0);
let to_addr = Address::new(
Network::Mainnet,

View File

@@ -3,6 +3,8 @@ use std::{
time::{SystemTime, Duration},
};
use rand_core::{RngCore, OsRng};
use dkg::{Participant, tests::clone_without};
use messages::{coordinator::*, SubstrateContext};

View File

@@ -1,30 +1,24 @@
use std::{collections::HashMap, time::SystemTime};
use std::time::SystemTime;
use dkg::{Participant, ThresholdParams, tests::clone_without};
use dkg::Participant;
use serai_client::{
primitives::{NetworkId, BlockHash, PublicKey},
validator_sets::primitives::{Session, KeyPair},
};
use messages::{SubstrateContext, key_gen::KeyGenId, CoordinatorMessage, ProcessorMessage};
use messages::{SubstrateContext, CoordinatorMessage, ProcessorMessage};
use crate::{*, tests::*};
pub(crate) async fn key_gen(coordinators: &mut [Coordinator]) -> KeyPair {
// Perform an interaction with all processors via their coordinators
async fn interact_with_all<
FS: Fn(Participant) -> messages::key_gen::CoordinatorMessage,
FR: FnMut(Participant, messages::key_gen::ProcessorMessage),
>(
async fn interact_with_all<FR: FnMut(Participant, messages::key_gen::ProcessorMessage)>(
coordinators: &mut [Coordinator],
message: FS,
mut recv: FR,
) {
for (i, coordinator) in coordinators.iter_mut().enumerate() {
let participant = Participant::new(u16::try_from(i + 1).unwrap()).unwrap();
coordinator.send_message(CoordinatorMessage::KeyGen(message(participant))).await;
match coordinator.recv_message().await {
ProcessorMessage::KeyGen(msg) => recv(participant, msg),
_ => panic!("processor didn't return KeyGen message"),
@@ -33,85 +27,69 @@ pub(crate) async fn key_gen(coordinators: &mut [Coordinator]) -> KeyPair {
}
// Order a key gen
let id = KeyGenId { session: Session(0), attempt: 0 };
let session = Session(0);
let mut commitments = HashMap::new();
interact_with_all(
coordinators,
|participant| messages::key_gen::CoordinatorMessage::GenerateKey {
id,
params: ThresholdParams::new(
u16::try_from(THRESHOLD).unwrap(),
u16::try_from(COORDINATORS).unwrap(),
let mut evrf_public_keys = vec![];
for coordinator in &*coordinators {
let keys = coordinator.evrf_keys();
evrf_public_keys.push((keys.substrate, keys.network));
}
let mut participations = vec![];
for coordinator in &mut *coordinators {
coordinator
.send_message(CoordinatorMessage::KeyGen(
messages::key_gen::CoordinatorMessage::GenerateKey {
session,
threshold: u16::try_from(THRESHOLD).unwrap(),
evrf_public_keys: evrf_public_keys.clone(),
},
))
.await;
}
// This takes forever on debug, as we use in these tests
let ci_scaling_factor =
1 + u64::from(u8::from(std::env::var("GITHUB_CI") == Ok("true".to_string())));
tokio::time::sleep(core::time::Duration::from_secs(600 * ci_scaling_factor)).await;
interact_with_all(coordinators, |participant, msg| match msg {
messages::key_gen::ProcessorMessage::Participation { session: this_session, participation } => {
assert_eq!(this_session, session);
participations.push(messages::key_gen::CoordinatorMessage::Participation {
session,
participant,
)
.unwrap(),
shares: 1,
},
|participant, msg| match msg {
messages::key_gen::ProcessorMessage::Commitments {
id: this_id,
commitments: mut these_commitments,
} => {
assert_eq!(this_id, id);
assert_eq!(these_commitments.len(), 1);
commitments.insert(participant, these_commitments.swap_remove(0));
}
_ => panic!("processor didn't return Commitments in response to GenerateKey"),
},
)
participation,
});
}
_ => panic!("processor didn't return Participation in response to GenerateKey"),
})
.await;
// Send the commitments to all parties
let mut shares = HashMap::new();
interact_with_all(
coordinators,
|participant| messages::key_gen::CoordinatorMessage::Commitments {
id,
commitments: clone_without(&commitments, &participant),
},
|participant, msg| match msg {
messages::key_gen::ProcessorMessage::Shares { id: this_id, shares: mut these_shares } => {
assert_eq!(this_id, id);
assert_eq!(these_shares.len(), 1);
shares.insert(participant, these_shares.swap_remove(0));
}
_ => panic!("processor didn't return Shares in response to GenerateKey"),
},
)
.await;
// Send the shares
// Send the participations
let mut substrate_key = None;
let mut network_key = None;
interact_with_all(
coordinators,
|participant| messages::key_gen::CoordinatorMessage::Shares {
id,
shares: vec![shares
.iter()
.filter_map(|(this_participant, shares)| {
shares.get(&participant).cloned().map(|share| (*this_participant, share))
})
.collect()],
},
|_, msg| match msg {
messages::key_gen::ProcessorMessage::GeneratedKeyPair {
id: this_id,
substrate_key: this_substrate_key,
network_key: this_network_key,
} => {
assert_eq!(this_id, id);
if substrate_key.is_none() {
substrate_key = Some(this_substrate_key);
network_key = Some(this_network_key.clone());
}
assert_eq!(substrate_key.unwrap(), this_substrate_key);
assert_eq!(network_key.as_ref().unwrap(), &this_network_key);
for participation in participations {
for coordinator in &mut *coordinators {
coordinator.send_message(participation.clone()).await;
}
}
// This also takes a while on debug
tokio::time::sleep(core::time::Duration::from_secs(240 * ci_scaling_factor)).await;
interact_with_all(coordinators, |_, msg| match msg {
messages::key_gen::ProcessorMessage::GeneratedKeyPair {
session: this_session,
substrate_key: this_substrate_key,
network_key: this_network_key,
} => {
assert_eq!(this_session, session);
if substrate_key.is_none() {
substrate_key = Some(this_substrate_key);
network_key = Some(this_network_key.clone());
}
_ => panic!("processor didn't return GeneratedKeyPair in response to GenerateKey"),
},
)
assert_eq!(substrate_key.unwrap(), this_substrate_key);
assert_eq!(network_key.as_ref().unwrap(), &this_network_key);
}
_ => panic!("processor didn't return GeneratedKeyPair in response to all Participations"),
})
.await;
// Confirm the key pair
@@ -132,7 +110,7 @@ pub(crate) async fn key_gen(coordinators: &mut [Coordinator]) -> KeyPair {
.send_message(CoordinatorMessage::Substrate(
messages::substrate::CoordinatorMessage::ConfirmKeyPair {
context,
session: id.session,
session,
key_pair: key_pair.clone(),
},
))

View File

@@ -1,5 +1,3 @@
use ciphersuite::{Ciphersuite, Ristretto};
use serai_client::primitives::NetworkId;
use dockertest::DockerTest;
@@ -17,18 +15,21 @@ mod send;
pub(crate) const COORDINATORS: usize = 4;
pub(crate) const THRESHOLD: usize = ((COORDINATORS * 2) / 3) + 1;
fn new_test(network: NetworkId) -> (Vec<(Handles, <Ristretto as Ciphersuite>::F)>, DockerTest) {
fn new_test(network: NetworkId) -> (Vec<(Handles, ProcessorKeys)>, DockerTest) {
let mut coordinators = vec![];
let mut test = DockerTest::new().with_network(dockertest::Network::Isolated);
let mut eth_handle = None;
for _ in 0 .. COORDINATORS {
let (handles, coord_key, compositions) = processor_stack(network, eth_handle.clone());
for i in 0 .. COORDINATORS {
// Uses the counter `i` as this has no relation to any other system, and while Substrate has
// hard-coded names for itself, these tests down't spawn any Substrate node
let (handles, keys, compositions) =
processor_stack(&i.to_string(), network, eth_handle.clone());
// TODO: Remove this once https://github.com/foundry-rs/foundry/issues/7955
// This has all processors share an Ethereum node until we can sync controlled nodes
if network == NetworkId::Ethereum {
eth_handle = eth_handle.or_else(|| Some(handles.0.clone()));
}
coordinators.push((handles, coord_key));
coordinators.push((handles, keys));
for composition in compositions {
test.provide_container(composition);
}

View File

@@ -3,6 +3,8 @@ use std::{
time::{SystemTime, Duration},
};
use rand_core::{RngCore, OsRng};
use dkg::{Participant, tests::clone_without};
use messages::{sign::SignId, SubstrateContext};