1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use super::{KeyedSig, SigShare};
use crate::messaging::node::Proposal;
use crate::routing::{
core::{AggregatorError, SignatureAggregator},
error::Result,
};
use serde::{Serialize, Serializer};
use thiserror::Error;
pub(crate) trait ProposalUtils {
fn prove(
&self,
public_key_set: bls::PublicKeySet,
index: usize,
secret_key_share: &bls::SecretKeyShare,
) -> Result<SigShare>;
fn as_signable(&self) -> SignableView;
}
impl ProposalUtils for Proposal {
fn prove(
&self,
public_key_set: bls::PublicKeySet,
index: usize,
secret_key_share: &bls::SecretKeyShare,
) -> Result<SigShare> {
Ok(SigShare::new(
public_key_set,
index,
secret_key_share,
&bincode::serialize(&self.as_signable()).map_err(|_| ProposalError::Invalid)?,
))
}
fn as_signable(&self) -> SignableView {
SignableView(self)
}
}
pub(crate) struct SignableView<'a>(pub(crate) &'a Proposal);
impl<'a> Serialize for SignableView<'a> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self.0 {
Proposal::Online { node_state, .. } => node_state.serialize(serializer),
Proposal::Offline(node_state) => node_state.serialize(serializer),
Proposal::SectionInfo(info) => info.serialize(serializer),
Proposal::OurElders(info) => info.sig.public_key.serialize(serializer),
Proposal::JoinsAllowed(joins_allowed) => joins_allowed.serialize(serializer),
}
}
}
#[derive(Default)]
pub(crate) struct ProposalAggregator(SignatureAggregator);
impl ProposalAggregator {
pub(crate) fn add(
&mut self,
proposal: Proposal,
sig_share: SigShare,
) -> Result<(Proposal, KeyedSig), ProposalError> {
let bytes =
bincode::serialize(&SignableView(&proposal)).map_err(|_| ProposalError::Invalid)?;
let sig = self.0.add(&bytes, sig_share)?;
Ok((proposal, sig))
}
}
#[derive(Debug, Error)]
pub enum ProposalError {
#[error("failed to aggregate signature shares: {0}")]
Aggregation(#[from] AggregatorError),
#[error("invalid proposal")]
Invalid,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::routing::{dkg, section};
use anyhow::Result;
use std::fmt::Debug;
use xor_name::Prefix;
#[test]
fn serialize_for_signing() -> Result<()> {
let (section_auth, _, _) =
section::test_utils::gen_section_authority_provider(Prefix::default(), 4);
let proposal = Proposal::SectionInfo(section_auth.clone());
verify_serialize_for_signing(&proposal, §ion_auth)?;
let new_sk = bls::SecretKey::random();
let new_pk = new_sk.public_key();
let section_signed_auth = dkg::test_utils::section_signed(&new_sk, section_auth)?;
let proposal = Proposal::OurElders(section_signed_auth);
verify_serialize_for_signing(&proposal, &new_pk)?;
Ok(())
}
fn verify_serialize_for_signing<T>(proposal: &Proposal, should_serialize_as: &T) -> Result<()>
where
T: Serialize + Debug,
{
let actual = bincode::serialize(&SignableView(proposal))?;
let expected = bincode::serialize(should_serialize_as)?;
assert_eq!(
actual, expected,
"expected SignableView({:?}) to serialize same as {:?}, but didn't",
proposal, should_serialize_as
);
Ok(())
}
}