Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

Commit

Permalink
tests/chain: add some tests for the new functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
fizyk20 committed Jul 17, 2019
1 parent 03cc37d commit a92e10b
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 7 deletions.
76 changes: 70 additions & 6 deletions src/chain/bls_emu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
use super::{delivery_group_size, NetworkEvent, ProofSet, SectionInfo};
use crate::id::{FullId, PublicId};
use parsec;
use std::{collections::BTreeMap, fmt};
use std::{
collections::{BTreeMap, BTreeSet},
fmt,
};

#[derive(Clone, PartialEq, Eq)]
pub struct PublicKeySet {
Expand Down Expand Up @@ -61,6 +64,19 @@ impl PublicKeyShare {
}

impl PublicKeySet {
#[allow(unused)]
pub fn new(threshold: usize, keys: BTreeSet<PublicId>) -> Self {
let sec_info = SectionInfo::new(keys, Default::default(), None).unwrap();
Self::from_section_info(threshold, sec_info)
}

pub fn from_section_info(threshold: usize, sec_info: SectionInfo) -> Self {
Self {
threshold,
sec_info,
}
}

#[allow(unused)]
pub fn threshold(&self) -> usize {
self.threshold
Expand Down Expand Up @@ -91,11 +107,8 @@ impl PublicKeySet {

impl PublicKey {
pub fn from_section_info(sec_info: &SectionInfo) -> Self {
let threshold = delivery_group_size(sec_info.members().len());
PublicKey(PublicKeySet {
sec_info: sec_info.clone(),
threshold,
})
let threshold = delivery_group_size(sec_info.members().len()) - 1;
PublicKey(PublicKeySet::from_section_info(threshold, sec_info.clone()))
}

pub fn verify<M: AsRef<[u8]>>(&self, sig: &Signature, msg: M) -> bool {
Expand All @@ -119,3 +132,54 @@ impl fmt::Debug for PublicKey {
write!(formatter, "BLS-PublicKey({:?})", self.0.sec_info)
}
}

#[cfg(test)]
mod test {
use super::*;
use crate::{chain::delivery_group_size, id::FullId};
use safe_crypto;
use unwrap::unwrap;

fn gen_section(size: usize) -> (PublicKeySet, Vec<SecretKeyShare>) {
unwrap!(safe_crypto::init());

let threshold = delivery_group_size(size) - 1;

let ids: Vec<_> = (0..size).map(|_| FullId::new()).collect();
let pub_ids = ids.iter().map(|full_id| *full_id.public_id()).collect();
let pk_set = PublicKeySet::new(threshold, pub_ids);

(pk_set, ids.into_iter().map(SecretKeyShare).collect())
}

#[test]
fn test_signature() {
let section_size = 10;
let min_sigs = delivery_group_size(section_size);

let (pk_set, sk_shares) = gen_section(section_size);

let data = [1u8, 2, 3, 4, 5, 6];

let mut sigs: Vec<_> = sk_shares
.iter()
.take(min_sigs - 1)
.map(|sk| (sk.public_key_share(), sk.sign(&data)))
.collect();

assert!(sigs.iter().all(|(pks, sig)| pks.verify(sig, &data)));

assert!(pk_set
.combine_signatures(sigs.iter().map(|(pk, sig)| (*pk, sig)))
.is_none());

sigs.push((
sk_shares[min_sigs - 1].public_key_share(),
sk_shares[min_sigs - 1].sign(&data),
));

let sig = unwrap!(pk_set.combine_signatures(sigs.iter().map(|(pk, sig)| (*pk, sig))));

assert!(pk_set.public_key().verify(&sig, &data));
}
}
8 changes: 8 additions & 0 deletions src/chain/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,13 @@ impl Chain {
}
}

#[cfg(test)]
impl Chain {
pub fn validate_our_history(&self) -> bool {
self.state.our_history.validate()
}
}

#[cfg(test)]
mod tests {
use super::super::{GenesisPfxInfo, Proof, ProofSet, SectionInfo};
Expand Down Expand Up @@ -1517,6 +1524,7 @@ mod tests {
full_ids.extend(new_ids);
let proofs = gen_proofs(&full_ids, chain.our_info().members(), &new_info);
unwrap!(chain.add_section_info(new_info, proofs));
assert!(chain.validate_our_history());
check_infos_for_duplication(&chain);
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/chain/shared_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ impl SectionProofBlock {
SectionProofBlock { key, sig }
}

#[allow(unused)]
pub fn verify_with_pk(&self, pk: &BlsPublicKey) -> bool {
let to_verify = self.key.as_event();
match serialisation::serialise(&to_verify) {
Expand All @@ -270,6 +269,18 @@ impl SectionProofChain {
pub fn push(&mut self, block: SectionProofBlock) {
self.blocks.push(block);
}

#[allow(unused)]
pub fn validate(&self) -> bool {
let mut current_pk = &self.genesis_pk;
for block in &self.blocks {
if !block.verify_with_pk(current_pk) {
return false;
}
current_pk = &block.key;
}
true
}
}

impl Debug for SectionProofChain {
Expand Down

0 comments on commit a92e10b

Please sign in to comment.