Do the minimal amount of work for dkg to compile under no-std

The Substrate runtime requires access to the MuSig key aggregation function.

\#279 related.
This commit is contained in:
Luke Parker 2023-05-12 23:24:47 -04:00
parent 4d50b6892c
commit 84c2d73093
No known key found for this signature in database
6 changed files with 462 additions and 429 deletions

2
Cargo.lock generated
View file

@ -2032,6 +2032,7 @@ dependencies = [
"rand_core 0.6.4",
"schnorr-signatures",
"serde",
"std-shims",
"thiserror",
"zeroize",
]
@ -8775,6 +8776,7 @@ version = "0.1.0"
dependencies = [
"ciphersuite",
"dalek-ff-group",
"dkg",
"flexible-transcript",
"minimal-ed448",
"monero-generators",

View file

@ -13,26 +13,30 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]
[dependencies]
thiserror = "1"
thiserror = { version = "1", default-features = false, optional = true }
rand_core = "0.6"
rand_core = { version = "0.6", default-features = false }
zeroize = { version = "^1.5", features = ["zeroize_derive"] }
zeroize = { version = "^1.5", default-features = false, features = ["zeroize_derive"] }
std-shims = { version = "0.1", path = "../../common/std-shims", default-features = false }
serde = { version = "1", features = ["derive"], optional = true }
transcript = { package = "flexible-transcript", path = "../transcript", version = "0.3", features = ["recommended"] }
chacha20 = { version = "0.9", features = ["zeroize"] }
transcript = { package = "flexible-transcript", path = "../transcript", version = "0.3", default-features = false, features = ["recommended"] }
chacha20 = { version = "0.9", default-features = false, features = ["zeroize"] }
ciphersuite = { path = "../ciphersuite", version = "0.3", features = ["std"] }
multiexp = { path = "../multiexp", version = "0.3", features = ["batch"] }
ciphersuite = { path = "../ciphersuite", version = "0.3", default-features = false }
multiexp = { path = "../multiexp", version = "0.3", default-features = false, features = ["batch"] }
schnorr = { package = "schnorr-signatures", path = "../schnorr", version = "0.4" }
dleq = { path = "../dleq", version = "0.3", features = ["serialize"] }
dleq = { path = "../dleq", version = "0.3", default-features = false, features = ["serialize"] }
[dev-dependencies]
ciphersuite = { path = "../ciphersuite", version = "0.3", features = ["ristretto"] }
ciphersuite = { path = "../ciphersuite", version = "0.3", default-features = false, features = ["ristretto"] }
[features]
std = ["thiserror", "std-shims/std", "ciphersuite/std", "multiexp/std", "schnorr/std", "dleq/std"]
serde = ["dep:serde"]
tests = ["rand_core/getrandom"]
default = ["std"]

View file

@ -1,35 +1,27 @@
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![doc = include_str!("../README.md")]
use core::{
fmt::{self, Debug},
ops::Deref,
};
use std::{io, sync::Arc, collections::HashMap};
use core::fmt::{self, Debug};
#[cfg(feature = "std")]
use thiserror::Error;
use zeroize::{Zeroize, Zeroizing};
use ciphersuite::{
group::{
ff::{Field, PrimeField},
GroupEncoding,
},
Ciphersuite,
};
/// Encryption types and utilities used to secure DKG messages.
pub mod encryption;
use zeroize::Zeroize;
/// MuSig-style key aggregation.
pub mod musig;
/// Encryption types and utilities used to secure DKG messages.
#[cfg(feature = "std")]
pub mod encryption;
/// The distributed key generation protocol described in the
/// [FROST paper](https://eprint.iacr.org/2020/852).
#[cfg(feature = "std")]
pub mod frost;
/// Promote keys between ciphersuites.
#[cfg(feature = "std")]
pub mod promote;
/// Tests for application-provided curves and algorithms.
@ -70,39 +62,60 @@ impl fmt::Display for Participant {
}
/// Various errors possible during key generation.
#[derive(Clone, PartialEq, Eq, Debug, Error)]
#[derive(Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "std", derive(Error))]
pub enum DkgError<B: Clone + PartialEq + Eq + Debug> {
/// A parameter was zero.
#[error("a parameter was 0 (threshold {0}, participants {1})")]
#[cfg_attr(feature = "std", error("a parameter was 0 (threshold {0}, participants {1})"))]
ZeroParameter(u16, u16),
/// The threshold exceeded the amount of participants.
#[error("invalid threshold (max {1}, got {0})")]
#[cfg_attr(feature = "std", error("invalid threshold (max {1}, got {0})"))]
InvalidThreshold(u16, u16),
/// Invalid participant identifier.
#[error("invalid participant (0 < participant <= {0}, yet participant is {1})")]
#[cfg_attr(
feature = "std",
error("invalid participant (0 < participant <= {0}, yet participant is {1})")
)]
InvalidParticipant(u16, Participant),
/// Invalid signing set.
#[error("invalid signing set")]
#[cfg_attr(feature = "std", error("invalid signing set"))]
InvalidSigningSet,
/// Invalid amount of participants.
#[error("invalid participant quantity (expected {0}, got {1})")]
#[cfg_attr(feature = "std", error("invalid participant quantity (expected {0}, got {1})"))]
InvalidParticipantQuantity(usize, usize),
/// A participant was duplicated.
#[error("duplicated participant ({0})")]
#[cfg_attr(feature = "std", error("duplicated participant ({0})"))]
DuplicatedParticipant(Participant),
/// A participant was missing.
#[error("missing participant {0}")]
#[cfg_attr(feature = "std", error("missing participant {0}"))]
MissingParticipant(Participant),
/// An invalid proof of knowledge was provided.
#[error("invalid proof of knowledge (participant {0})")]
#[cfg_attr(feature = "std", error("invalid proof of knowledge (participant {0})"))]
InvalidProofOfKnowledge(Participant),
/// An invalid DKG share was provided.
#[error("invalid share (participant {participant}, blame {blame})")]
#[cfg_attr(feature = "std", error("invalid share (participant {participant}, blame {blame})"))]
InvalidShare { participant: Participant, blame: Option<B> },
}
#[cfg(feature = "std")]
mod lib {
pub use super::*;
use core::ops::Deref;
use std::{io, sync::Arc, collections::HashMap};
use zeroize::Zeroizing;
use ciphersuite::{
group::{
ff::{Field, PrimeField},
GroupEncoding,
},
Ciphersuite,
};
// Validate a map of values to have the expected included participants
pub(crate) fn validate_map<T, B: Clone + PartialEq + Eq + Debug>(
map: &HashMap<Participant, T>,
@ -135,11 +148,11 @@ pub(crate) fn validate_map<T, B: Clone + PartialEq + Eq + Debug>(
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ThresholdParams {
/// Participants needed to sign on behalf of the group.
t: u16,
pub(crate) t: u16,
/// Amount of participants.
n: u16,
pub(crate) n: u16,
/// Index of the participant being acted for.
i: Participant,
pub(crate) i: Participant,
}
impl ThresholdParams {
@ -149,8 +162,6 @@ impl ThresholdParams {
Err(DkgError::ZeroParameter(t, n))?;
}
// When t == n, this shouldn't be used (MuSig2 and other variants of MuSig exist for a reason),
// but it's not invalid to do so
if t > n {
Err(DkgError::InvalidThreshold(t, n))?;
}
@ -201,14 +212,14 @@ pub fn lagrange<F: PrimeField>(i: Participant, included: &[Participant]) -> F {
#[derive(Clone, PartialEq, Eq)]
pub struct ThresholdCore<C: Ciphersuite> {
/// Threshold Parameters.
params: ThresholdParams,
pub(crate) params: ThresholdParams,
/// Secret share key.
secret_share: Zeroizing<C::F>,
pub(crate) secret_share: Zeroizing<C::F>,
/// Group key.
group_key: C::G,
pub(crate) group_key: C::G,
/// Verification shares.
verification_shares: HashMap<Participant, C::G>,
pub(crate) verification_shares: HashMap<Participant, C::G>,
}
impl<C: Ciphersuite> fmt::Debug for ThresholdCore<C> {
@ -239,7 +250,7 @@ impl<C: Ciphersuite> ThresholdCore<C> {
secret_share: Zeroizing<C::F>,
verification_shares: HashMap<Participant, C::G>,
) -> ThresholdCore<C> {
let t = (1 ..= params.t).map(Participant).collect::<Vec<_>>();
let t = (1 ..= params.t()).map(Participant).collect::<Vec<_>>();
ThresholdCore {
params,
secret_share,
@ -347,7 +358,7 @@ pub struct ThresholdKeys<C: Ciphersuite> {
// If this is the last reference, the underlying keys will be dropped. When that happens, the
// private key present within it will be zeroed out (as it's within Zeroizing).
#[zeroize(skip)]
core: Arc<ThresholdCore<C>>,
pub(crate) core: Arc<ThresholdCore<C>>,
// Offset applied to these keys.
pub(crate) offset: Option<C::F>,
@ -445,14 +456,16 @@ impl<C: Ciphersuite> ThresholdKeys<C> {
/// Obtain a view of these keys, with any offset applied, interpolated for the specified signing
/// set.
pub fn view(&self, mut included: Vec<Participant>) -> Result<ThresholdView<C>, DkgError<()>> {
if (included.len() < self.params().t.into()) || (usize::from(self.params().n) < included.len())
if (included.len() < self.params().t.into()) ||
(usize::from(self.params().n()) < included.len())
{
Err(DkgError::InvalidSigningSet)?;
}
included.sort();
let mut secret_share =
Zeroizing::new(lagrange::<C::F>(self.params().i, &included) * self.secret_share().deref());
let mut secret_share = Zeroizing::new(
lagrange::<C::F>(self.params().i(), &included) * self.secret_share().deref(),
);
let mut verification_shares = self.verification_shares();
for (i, share) in verification_shares.iter_mut() {
@ -514,3 +527,6 @@ impl<C: Ciphersuite> ThresholdView<C> {
self.verification_shares[&l]
}
}
}
#[cfg(feature = "std")]
pub use lib::*;

View file

@ -1,16 +1,24 @@
#[cfg(feature = "std")]
use core::ops::Deref;
use std::collections::{HashSet, HashMap};
use std_shims::collections::HashSet;
#[cfg(feature = "std")]
use std_shims::collections::HashMap;
#[cfg(feature = "std")]
use zeroize::Zeroizing;
use transcript::{Transcript, RecommendedTranscript};
#[cfg(feature = "std")]
use ciphersuite::group::ff::Field;
use ciphersuite::{
group::{ff::Field, Group, GroupEncoding},
group::{Group, GroupEncoding},
Ciphersuite,
};
use crate::{Participant, DkgError, ThresholdParams, ThresholdCore, lagrange};
use crate::DkgError;
#[cfg(feature = "std")]
use crate::{Participant, ThresholdParams, ThresholdCore, lagrange};
fn check_keys<C: Ciphersuite>(keys: &[C::G]) -> Result<u16, DkgError<()>> {
if keys.is_empty() {
@ -59,6 +67,7 @@ pub fn musig_key<C: Ciphersuite>(keys: &[C::G]) -> Result<C::G, DkgError<()>> {
/// A n-of-n non-interactive DKG which does not guarantee the usability of the resulting key.
///
/// Creating an aggregate key with a list containing duplicated public keys returns an error.
#[cfg(feature = "std")]
pub fn musig<C: Ciphersuite>(
private_key: &Zeroizing<C::F>,
keys: &[C::G],

View file

@ -2,8 +2,10 @@ use std::collections::HashMap;
use rand_core::{RngCore, CryptoRng};
use ciphersuite::Ciphersuite;
use crate::{
Ciphersuite, Participant, ThresholdParams, ThresholdCore,
Participant, ThresholdParams, ThresholdCore,
frost::{KeyGenMachine, SecretShare, KeyMachine},
encryption::{EncryptionKeyMessage, EncryptedMessage},
tests::{THRESHOLD, PARTICIPANTS, clone_without},

View file

@ -26,7 +26,7 @@ multiexp = { path = "../../crypto/multiexp", default-features = false, features
# dleq = { path = "../../crypto/dleq" }
schnorr-signatures = { path = "../../crypto/schnorr", default-features = false }
# dkg = { path = "../../crypto/dkg" }
dkg = { path = "../../crypto/dkg", default-features = false }
# modular-frost = { path = "../../crypto/frost" }
# frost-schnorrkel = { path = "../../crypto/schnorrkel" }