Change signatures of share validation fns

* Pass a ref to `Vec<Shares>` instead of recreating and moving the object
  through several functions.
* Return `slen`/ `data_len`, since we'll be using it anyway in `recover_secrets`
This commit is contained in:
Noah Vesely
2018-03-28 18:51:10 -06:00
committed by Romain Ruetschi
parent d098033f6f
commit 840f5cce81
4 changed files with 17 additions and 16 deletions

View File

@ -247,7 +247,8 @@ impl SS1 {
&self, &self,
shares: &[Share], shares: &[Share],
) -> Result<(Vec<u8>, AccessStructure, Option<MetaData>)> { ) -> Result<(Vec<u8>, AccessStructure, Option<MetaData>)> {
let (_, shares) = validate_shares(shares.to_vec())?; let shares = shares.to_vec();
validate_shares(&shares)?;
let underlying_shares = shares let underlying_shares = shares
.iter() .iter()

View File

@ -89,9 +89,8 @@ impl ThSS {
&self, &self,
shares: &[Share], shares: &[Share],
) -> Result<(Vec<u8>, AccessStructure, Option<MetaData>)> { ) -> Result<(Vec<u8>, AccessStructure, Option<MetaData>)> {
let (threshold, shares) = validate_shares(shares.to_vec())?; let shares = shares.to_vec();
let (threshold, cypher_len) = validate_shares(&shares)?;
let cypher_len = shares[0].data.len();
let polys = (0..cypher_len) let polys = (0..cypher_len)
.map(|i| { .map(|i| {

View File

@ -11,27 +11,27 @@ use share::{IsShare, IsSignedShare};
/// TODO: Doc /// TODO: Doc
pub(crate) fn validate_signed_shares<S: IsSignedShare>( pub(crate) fn validate_signed_shares<S: IsSignedShare>(
shares: Vec<S>, shares: &Vec<S>,
verify_signatures: bool, verify_signatures: bool,
) -> Result<(u8, Vec<S>)> { ) -> Result<(u8, usize)> {
let (threshold, shares) = validate_shares(shares)?; let result = validate_shares(shares)?;
if verify_signatures { if verify_signatures {
S::verify_signatures(&shares)?; S::verify_signatures(&shares)?;
} }
Ok((threshold, shares)) Ok(result)
} }
/// TODO: Doc /// TODO: Doc
pub(crate) fn validate_shares<S: IsShare>(shares: Vec<S>) -> Result<(u8, Vec<S>)> { pub(crate) fn validate_shares<S: IsShare>(shares: &Vec<S>) -> Result<(u8, usize)> {
if shares.is_empty() { if shares.is_empty() {
bail!(ErrorKind::EmptyShares); bail!(ErrorKind::EmptyShares);
} }
let shares_count = shares.len(); let shares_count = shares.len();
let mut result: Vec<S> = Vec::with_capacity(shares_count);
let mut ids = Vec::with_capacity(shares_count);
let mut k_compatibility_sets = HashMap::new(); let mut k_compatibility_sets = HashMap::new();
let mut data_len_compatibility_sets = HashMap::new(); let mut data_len_compatibility_sets = HashMap::new();
@ -52,7 +52,7 @@ pub(crate) fn validate_shares<S: IsShare>(shares: Vec<S>) -> Result<(u8, Vec<S>)
let k_set = k_compatibility_sets.get_mut(&threshold).unwrap(); let k_set = k_compatibility_sets.get_mut(&threshold).unwrap();
k_set.insert(id); k_set.insert(id);
if result.iter().any(|s| s.get_id() == id) { if ids.iter().any(|&x| x == id) {
bail!(ErrorKind::DuplicateShareId(id)); bail!(ErrorKind::DuplicateShareId(id));
} }
@ -62,8 +62,7 @@ pub(crate) fn validate_shares<S: IsShare>(shares: Vec<S>) -> Result<(u8, Vec<S>)
let data_len_set = data_len_compatibility_sets.get_mut(&data_len).unwrap(); let data_len_set = data_len_compatibility_sets.get_mut(&data_len).unwrap();
data_len_set.insert(id); data_len_set.insert(id);
ids.push(id);
result.push(share);
} }
// Validate threshold // Validate threshold
@ -107,7 +106,10 @@ pub(crate) fn validate_shares<S: IsShare>(shares: Vec<S>) -> Result<(u8, Vec<S>)
} }
} }
Ok((threshold, result)) // It is safe to unwrap because data_len_sets == 1
let slen = data_len_compatibility_sets.keys().last().unwrap().to_owned();
Ok((threshold, data_len))
} }
pub(crate) fn validate_share_count(threshold: u8, shares_count: u8) -> Result<(u8, u8)> { pub(crate) fn validate_share_count(threshold: u8, shares_count: u8) -> Result<(u8, u8)> {

View File

@ -93,9 +93,8 @@ impl SSS {
/// ///
/// At least `k` distinct shares need to be provided to recover the share. /// At least `k` distinct shares need to be provided to recover the share.
pub fn recover_secret(shares: Vec<Share>, verify_signatures: bool) -> Result<Vec<u8>> { pub fn recover_secret(shares: Vec<Share>, verify_signatures: bool) -> Result<Vec<u8>> {
let (threshold, shares) = validate_signed_shares(shares, verify_signatures)?; let (threshold, slen) = validate_signed_shares(&shares, verify_signatures)?;
let slen = shares[0].data.len();
let mut col_in = Vec::with_capacity(threshold as usize); let mut col_in = Vec::with_capacity(threshold as usize);
let mut secret = Vec::with_capacity(slen); let mut secret = Vec::with_capacity(slen);
for byteindex in 0..slen { for byteindex in 0..slen {