use std::collections::{BTreeMap, BTreeSet};
use std::fmt::{self, Debug, Formatter};
use crate::crypto::{
error::Error as CryptoError,
poly::{BivarCommitment, BivarPoly, Poly},
serde_impl::FieldWrap,
Ciphertext, Fr, G1Affine, PublicKey, PublicKeySet, SecretKey, SecretKeyShare,
};
use crate::pairing::{CurveAffine, Field};
use bincode;
use failure::Fail;
use rand;
use serde::{Deserialize, Serialize};
use crate::{NetworkInfo, NodeIdT};
#[derive(Clone, Eq, PartialEq, Debug, Fail)]
pub enum Error {
#[fail(display = "Error creating SyncKeyGen: {}", _0)]
Creation(CryptoError),
#[fail(display = "Error generating keys: {}", _0)]
Generation(CryptoError),
#[fail(display = "Unknown sender")]
UnknownSender,
#[fail(display = "Serialization error: {}", _0)]
Serialize(String),
}
impl From<bincode::Error> for Error {
fn from(err: bincode::Error) -> Error {
Error::Serialize(format!("{:?}", err))
}
}
#[derive(Deserialize, Serialize, Clone, Hash, Eq, PartialEq)]
pub struct Part(BivarCommitment, Vec<Ciphertext>);
impl Debug for Part {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_tuple("Part")
.field(&format!("<degree {}>", self.0.degree()))
.field(&format!("<{} rows>", self.1.len()))
.finish()
}
}
#[derive(Deserialize, Serialize, Clone, Hash, Eq, PartialEq)]
pub struct Ack(u64, Vec<Ciphertext>);
impl Debug for Ack {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_tuple("Ack")
.field(&self.0)
.field(&format!("<{} values>", self.1.len()))
.finish()
}
}
#[derive(Debug, PartialEq, Eq)]
struct ProposalState {
commit: BivarCommitment,
values: BTreeMap<u64, Fr>,
acks: BTreeSet<u64>,
}
impl ProposalState {
fn new(commit: BivarCommitment) -> ProposalState {
ProposalState {
commit,
values: BTreeMap::new(),
acks: BTreeSet::new(),
}
}
fn is_complete(&self, threshold: usize) -> bool {
self.acks.len() > 2 * threshold
}
}
pub enum PartOutcome {
Valid(Option<Ack>),
Invalid(PartFault),
}
pub enum AckOutcome {
Valid,
Invalid(AckFault),
}
#[derive(Debug)]
pub struct SyncKeyGen<N> {
our_id: N,
our_idx: Option<u64>,
sec_key: SecretKey,
pub_keys: BTreeMap<N, PublicKey>,
parts: BTreeMap<u64, ProposalState>,
threshold: usize,
}
impl<N: NodeIdT> SyncKeyGen<N> {
pub fn new<R: rand::Rng>(
our_id: N,
sec_key: SecretKey,
pub_keys: BTreeMap<N, PublicKey>,
threshold: usize,
rng: &mut R,
) -> Result<(SyncKeyGen<N>, Option<Part>), Error> {
let our_idx = pub_keys
.keys()
.position(|id| *id == our_id)
.map(|idx| idx as u64);
let key_gen = SyncKeyGen {
our_id,
our_idx,
sec_key,
pub_keys,
parts: BTreeMap::new(),
threshold,
};
if our_idx.is_none() {
return Ok((key_gen, None)); }
let our_part = BivarPoly::random(threshold, rng);
let commit = our_part.commitment();
let encrypt = |(i, pk): (usize, &PublicKey)| {
let row = our_part.row(i + 1);
Ok(pk.encrypt_with_rng(rng, &bincode::serialize(&row)?))
};
let rows = key_gen
.pub_keys
.values()
.enumerate()
.map(encrypt)
.collect::<Result<Vec<_>, Error>>()?;
Ok((key_gen, Some(Part(commit, rows))))
}
pub fn public_keys(&self) -> &BTreeMap<N, PublicKey> {
&self.pub_keys
}
pub fn handle_part<R: rand::Rng>(
&mut self,
sender_id: &N,
part: Part,
rng: &mut R,
) -> Result<PartOutcome, Error> {
let sender_idx = self.node_index(sender_id).ok_or(Error::UnknownSender)?;
let row = match self.handle_part_or_fault(sender_idx, part) {
Ok(Some(row)) => row,
Ok(None) => return Ok(PartOutcome::Valid(None)),
Err(fault) => return Ok(PartOutcome::Invalid(fault)),
};
let mut values = Vec::new();
for (idx, pk) in self.pub_keys.values().enumerate() {
let val = row.evaluate(idx + 1);
let ser_val = bincode::serialize(&FieldWrap(val))?;
values.push(pk.encrypt_with_rng(rng, ser_val));
}
Ok(PartOutcome::Valid(Some(Ack(sender_idx, values))))
}
pub fn handle_ack(&mut self, sender_id: &N, ack: Ack) -> Result<AckOutcome, Error> {
let sender_idx = self.node_index(sender_id).ok_or(Error::UnknownSender)?;
Ok(match self.handle_ack_or_fault(sender_idx, ack) {
Ok(()) => AckOutcome::Valid,
Err(fault) => AckOutcome::Invalid(fault),
})
}
fn node_index(&self, node_id: &N) -> Option<u64> {
self.pub_keys
.keys()
.position(|id| id == node_id)
.map(|idx| idx as u64)
}
pub fn count_complete(&self) -> usize {
self.parts
.values()
.filter(|part| part.is_complete(self.threshold))
.count()
}
pub fn is_node_ready(&self, proposer_id: &N) -> bool {
self.node_index(proposer_id)
.and_then(|proposer_idx| self.parts.get(&proposer_idx))
.map_or(false, |part| part.is_complete(self.threshold))
}
pub fn is_ready(&self) -> bool {
self.count_complete() > self.threshold
}
pub fn generate(&self) -> Result<(PublicKeySet, Option<SecretKeyShare>), Error> {
let mut pk_commit = Poly::zero().commitment();
let mut opt_sk_val = self.our_idx.map(|_| Fr::zero());
let is_complete = |part: &&ProposalState| part.is_complete(self.threshold);
for part in self.parts.values().filter(is_complete) {
pk_commit += part.commit.row(0);
if let Some(sk_val) = opt_sk_val.as_mut() {
let row = Poly::interpolate(part.values.iter().take(self.threshold + 1));
sk_val.add_assign(&row.evaluate(0));
}
}
let opt_sk = if let Some(mut fr) = opt_sk_val {
let sk = SecretKeyShare::from_mut(&mut fr);
Some(sk)
} else {
None
};
Ok((pk_commit.into(), opt_sk))
}
pub fn into_network_info(self) -> Result<NetworkInfo<N>, Error> {
let (pk_set, sk_share) = self.generate()?;
let netinfo = NetworkInfo::new(self.our_id, sk_share, pk_set, self.sec_key, self.pub_keys);
Ok(netinfo)
}
pub fn num_nodes(&self) -> usize {
self.pub_keys.len()
}
fn handle_part_or_fault(
&mut self,
sender_idx: u64,
Part(commit, rows): Part,
) -> Result<Option<Poly>, PartFault> {
if rows.len() != self.pub_keys.len() {
return Err(PartFault::RowCount);
}
if let Some(state) = self.parts.get(&sender_idx) {
if state.commit != commit {
return Err(PartFault::MultipleParts);
}
return Ok(None); }
let opt_idx_commit_row = self.our_idx.map(|idx| (idx, commit.row(idx + 1)));
self.parts.insert(sender_idx, ProposalState::new(commit));
let (our_idx, commit_row) = match opt_idx_commit_row {
Some((idx, row)) => (idx, row),
None => return Ok(None), };
let ser_row = self
.sec_key
.decrypt(&rows[our_idx as usize])
.ok_or(PartFault::DecryptRow)?;
let row: Poly = bincode::deserialize(&ser_row).map_err(|_| PartFault::DeserializeRow)?;
if row.commitment() != commit_row {
return Err(PartFault::RowCommitment);
}
Ok(Some(row))
}
fn handle_ack_or_fault(
&mut self,
sender_idx: u64,
Ack(proposer_idx, values): Ack,
) -> Result<(), AckFault> {
if values.len() != self.pub_keys.len() {
return Err(AckFault::ValueCount);
}
let part = self
.parts
.get_mut(&proposer_idx)
.ok_or(AckFault::MissingPart)?;
if !part.acks.insert(sender_idx) {
return Ok(()); }
let our_idx = match self.our_idx {
Some(our_idx) => our_idx,
None => return Ok(()), };
let ser_val = self
.sec_key
.decrypt(&values[our_idx as usize])
.ok_or(AckFault::DecryptValue)?;
let val = bincode::deserialize::<FieldWrap<Fr>>(&ser_val)
.map_err(|_| AckFault::DeserializeValue)?
.into_inner();
if part.commit.evaluate(our_idx + 1, sender_idx + 1) != G1Affine::one().mul(val) {
return Err(AckFault::ValueCommitment);
}
part.values.insert(sender_idx + 1, val);
Ok(())
}
}
#[derive(Clone, Copy, Eq, PartialEq, Debug, Fail)]
pub enum AckFault {
#[fail(display = "The number of values differs from the number of nodes")]
ValueCount,
#[fail(display = "No corresponding Part received")]
MissingPart,
#[fail(display = "Value decryption failed")]
DecryptValue,
#[fail(display = "Value deserialization failed")]
DeserializeValue,
#[fail(display = "Value doesn't match the commitment")]
ValueCommitment,
}
#[derive(Clone, Copy, Eq, PartialEq, Debug, Fail)]
pub enum PartFault {
#[fail(display = "The number of rows differs from the number of nodes")]
RowCount,
#[fail(display = "Received multiple different Part messages from the same sender")]
MultipleParts,
#[fail(display = "Could not decrypt our row in the Part message")]
DecryptRow,
#[fail(display = "Could not deserialize our row in the Part message")]
DeserializeRow,
#[fail(display = "Row does not match the commitment")]
RowCommitment,
}