2018-06-08 11:43:27 -07:00
|
|
|
//! Common coin from a given set of keys based on a `pairing` threshold signature scheme.
|
2018-06-04 14:00:38 -07:00
|
|
|
|
2018-06-08 11:43:27 -07:00
|
|
|
use std::collections::{BTreeMap, VecDeque};
|
2018-06-04 14:00:38 -07:00
|
|
|
use std::fmt::Debug;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
use pairing::bls12_381::Bls12;
|
|
|
|
|
2018-06-08 11:43:27 -07:00
|
|
|
use crypto::error as cerror;
|
|
|
|
use crypto::Signature;
|
2018-06-04 14:00:38 -07:00
|
|
|
use messaging::{DistAlgorithm, NetworkInfo, Target, TargetedMessage};
|
|
|
|
|
|
|
|
error_chain! {
|
2018-06-08 11:43:27 -07:00
|
|
|
links {
|
|
|
|
Crypto(cerror::Error, cerror::ErrorKind);
|
|
|
|
}
|
|
|
|
|
2018-06-04 14:00:38 -07:00
|
|
|
errors {
|
2018-06-08 11:43:27 -07:00
|
|
|
UnknownSender {
|
|
|
|
description("unknown sender")
|
|
|
|
}
|
2018-06-09 02:03:38 -07:00
|
|
|
VerificationFailed {
|
|
|
|
description("signature verification failed")
|
2018-06-04 14:00:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-20 01:21:52 -07:00
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
|
2018-06-09 02:03:38 -07:00
|
|
|
pub struct CommonCoinMessage(Signature<Bls12>);
|
2018-06-04 14:00:38 -07:00
|
|
|
|
2018-06-09 13:50:36 -07:00
|
|
|
impl CommonCoinMessage {
|
|
|
|
pub fn new(sig: Signature<Bls12>) -> Self {
|
|
|
|
CommonCoinMessage(sig)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_sig(&self) -> &Signature<Bls12> {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-08 11:43:27 -07:00
|
|
|
/// A common coin algorithm instance. On input, broadcasts our threshold signature share. Upon
|
|
|
|
/// receiving at least `num_faulty + 1` shares, attempts to combine them into a signature. If that
|
|
|
|
/// signature is valid, the instance outputs it and terminates; otherwise the instance aborts.
|
2018-06-04 14:00:38 -07:00
|
|
|
#[derive(Debug)]
|
2018-06-18 07:37:07 -07:00
|
|
|
pub struct CommonCoin<NodeUid, T> {
|
2018-06-18 07:14:17 -07:00
|
|
|
netinfo: Rc<NetworkInfo<NodeUid>>,
|
2018-06-08 11:43:27 -07:00
|
|
|
/// The name of this common coin. It is required to be unique for each common coin round.
|
|
|
|
nonce: T,
|
|
|
|
/// The result of combination of at least `num_faulty + 1` threshold signature shares.
|
|
|
|
output: Option<bool>,
|
|
|
|
/// Outgoing message queue.
|
|
|
|
messages: VecDeque<CommonCoinMessage>,
|
|
|
|
/// All received threshold signature shares.
|
2018-06-18 07:14:17 -07:00
|
|
|
received_shares: BTreeMap<NodeUid, Signature<Bls12>>,
|
2018-06-10 03:36:13 -07:00
|
|
|
/// Whether we provided input to the common coin.
|
|
|
|
had_input: bool,
|
2018-06-08 11:43:27 -07:00
|
|
|
/// Termination flag.
|
|
|
|
terminated: bool,
|
2018-06-04 14:00:38 -07:00
|
|
|
}
|
|
|
|
|
2018-06-18 07:14:17 -07:00
|
|
|
impl<NodeUid, T> DistAlgorithm for CommonCoin<NodeUid, T>
|
2018-06-04 14:00:38 -07:00
|
|
|
where
|
2018-06-18 07:37:07 -07:00
|
|
|
NodeUid: Clone + Debug + Ord,
|
2018-06-08 11:43:27 -07:00
|
|
|
T: Clone + AsRef<[u8]>,
|
2018-06-04 14:00:38 -07:00
|
|
|
{
|
2018-06-18 07:14:17 -07:00
|
|
|
type NodeUid = NodeUid;
|
2018-06-04 14:00:38 -07:00
|
|
|
type Input = ();
|
2018-06-08 11:43:27 -07:00
|
|
|
type Output = bool;
|
2018-06-04 14:00:38 -07:00
|
|
|
type Message = CommonCoinMessage;
|
|
|
|
type Error = Error;
|
|
|
|
|
2018-06-08 11:43:27 -07:00
|
|
|
/// Sends our threshold signature share if not yet sent.
|
|
|
|
fn input(&mut self, _input: Self::Input) -> Result<()> {
|
2018-06-10 03:36:13 -07:00
|
|
|
if !self.had_input {
|
|
|
|
self.had_input = true;
|
2018-06-08 11:43:27 -07:00
|
|
|
self.get_coin()
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-06-04 14:00:38 -07:00
|
|
|
}
|
|
|
|
|
2018-06-08 11:43:27 -07:00
|
|
|
/// Receives input from a remote node.
|
2018-06-04 14:00:38 -07:00
|
|
|
fn handle_message(&mut self, sender_id: &Self::NodeUid, message: Self::Message) -> Result<()> {
|
2018-06-11 09:00:23 -07:00
|
|
|
if self.terminated {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2018-06-09 02:03:38 -07:00
|
|
|
let CommonCoinMessage(share) = message;
|
2018-06-08 11:43:27 -07:00
|
|
|
self.handle_share(sender_id, share)
|
2018-06-04 14:00:38 -07:00
|
|
|
}
|
|
|
|
|
2018-06-08 11:43:27 -07:00
|
|
|
/// Takes the next share of a threshold signature message for multicasting to all other nodes.
|
2018-06-04 14:00:38 -07:00
|
|
|
fn next_message(&mut self) -> Option<TargetedMessage<Self::Message, Self::NodeUid>> {
|
2018-06-08 11:43:27 -07:00
|
|
|
self.messages
|
|
|
|
.pop_front()
|
|
|
|
.map(|msg| Target::All.message(msg))
|
2018-06-04 14:00:38 -07:00
|
|
|
}
|
|
|
|
|
2018-06-08 11:43:27 -07:00
|
|
|
/// Consumes the output. Once consumed, the output stays `None` forever.
|
2018-06-04 14:00:38 -07:00
|
|
|
fn next_output(&mut self) -> Option<Self::Output> {
|
|
|
|
self.output.take()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Whether the algorithm has terminated.
|
|
|
|
fn terminated(&self) -> bool {
|
2018-06-08 11:43:27 -07:00
|
|
|
self.terminated
|
2018-06-04 14:00:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn our_id(&self) -> &Self::NodeUid {
|
|
|
|
self.netinfo.our_uid()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 07:14:17 -07:00
|
|
|
impl<NodeUid, T> CommonCoin<NodeUid, T>
|
2018-06-04 14:00:38 -07:00
|
|
|
where
|
2018-06-18 07:37:07 -07:00
|
|
|
NodeUid: Clone + Debug + Ord,
|
2018-06-08 11:43:27 -07:00
|
|
|
T: Clone + AsRef<[u8]>,
|
2018-06-04 14:00:38 -07:00
|
|
|
{
|
2018-06-18 07:14:17 -07:00
|
|
|
pub fn new(netinfo: Rc<NetworkInfo<NodeUid>>, nonce: T) -> Self {
|
2018-06-04 14:00:38 -07:00
|
|
|
CommonCoin {
|
2018-06-14 02:05:05 -07:00
|
|
|
netinfo,
|
2018-06-08 11:43:27 -07:00
|
|
|
nonce,
|
2018-06-04 14:00:38 -07:00
|
|
|
output: None,
|
|
|
|
messages: VecDeque::new(),
|
2018-06-08 11:43:27 -07:00
|
|
|
received_shares: BTreeMap::new(),
|
2018-06-10 03:36:13 -07:00
|
|
|
had_input: false,
|
2018-06-08 11:43:27 -07:00
|
|
|
terminated: false,
|
2018-06-04 14:00:38 -07:00
|
|
|
}
|
|
|
|
}
|
2018-06-07 09:38:27 -07:00
|
|
|
|
2018-06-08 11:43:27 -07:00
|
|
|
fn get_coin(&mut self) -> Result<()> {
|
|
|
|
let share = self.netinfo.secret_key().sign(&self.nonce);
|
2018-06-09 13:50:36 -07:00
|
|
|
self.messages.push_back(CommonCoinMessage(share.clone()));
|
2018-06-08 11:43:27 -07:00
|
|
|
let id = self.netinfo.our_uid().clone();
|
|
|
|
self.handle_share(&id, share)
|
|
|
|
}
|
|
|
|
|
2018-06-18 07:14:17 -07:00
|
|
|
fn handle_share(&mut self, sender_id: &NodeUid, share: Signature<Bls12>) -> Result<()> {
|
2018-06-14 04:28:38 -07:00
|
|
|
if let Some(i) = self.netinfo.node_index(sender_id) {
|
|
|
|
let pk_i = self.netinfo.public_key_set().public_key_share(*i as u64);
|
2018-06-14 02:05:05 -07:00
|
|
|
if !pk_i.verify(&share, &self.nonce) {
|
|
|
|
// Silently ignore the invalid share.
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
self.received_shares.insert(sender_id.clone(), share);
|
|
|
|
let received_shares = &self.received_shares;
|
2018-06-14 04:28:38 -07:00
|
|
|
if self.had_input && received_shares.len() > self.netinfo.num_faulty() {
|
|
|
|
let sig = self.combine_and_verify_sig()?;
|
2018-06-14 02:05:05 -07:00
|
|
|
// Output the parity of the verified signature.
|
|
|
|
let parity = sig.parity();
|
|
|
|
self.output = Some(parity);
|
|
|
|
self.terminated = true;
|
|
|
|
}
|
|
|
|
Ok(())
|
2018-06-08 11:43:27 -07:00
|
|
|
} else {
|
|
|
|
Err(ErrorKind::UnknownSender.into())
|
|
|
|
}
|
2018-06-07 09:38:27 -07:00
|
|
|
}
|
2018-06-14 04:28:38 -07:00
|
|
|
|
|
|
|
fn combine_and_verify_sig(&self) -> Result<Signature<Bls12>> {
|
|
|
|
// Pass the indices of sender nodes to `combine_signatures`.
|
2018-06-18 07:14:17 -07:00
|
|
|
let ids_shares: BTreeMap<&NodeUid, &Signature<Bls12>> =
|
|
|
|
self.received_shares.iter().collect();
|
|
|
|
let ids_u64: BTreeMap<&NodeUid, u64> = ids_shares
|
2018-06-14 04:28:38 -07:00
|
|
|
.keys()
|
|
|
|
.map(|&id| (id, *self.netinfo.node_index(id).unwrap() as u64))
|
|
|
|
.collect();
|
|
|
|
// Convert indices to `u64` which is an interface type for `pairing`.
|
|
|
|
let shares: BTreeMap<&u64, &Signature<Bls12>> = ids_shares
|
|
|
|
.iter()
|
|
|
|
.map(|(id, &share)| (&ids_u64[id], share))
|
|
|
|
.collect();
|
|
|
|
let sig = self.netinfo.public_key_set().combine_signatures(shares)?;
|
|
|
|
if !self
|
|
|
|
.netinfo
|
|
|
|
.public_key_set()
|
|
|
|
.public_key()
|
|
|
|
.verify(&sig, &self.nonce)
|
|
|
|
{
|
|
|
|
// Abort
|
|
|
|
error!(
|
|
|
|
"{:?} main public key verification failed",
|
|
|
|
self.netinfo.our_uid()
|
|
|
|
);
|
|
|
|
Err(ErrorKind::VerificationFailed.into())
|
|
|
|
} else {
|
|
|
|
Ok(sig)
|
|
|
|
}
|
|
|
|
}
|
2018-06-04 14:00:38 -07:00
|
|
|
}
|