Interpolate Now Takes Any IntoIterator (#9)

* interpolate now takes an IntoIterator

* simplify generic types, update docs, revert interpolate parameter types

* update version strings

Co-authored-by: Aitor Ruano <45633475+c0dearm@users.noreply.github.com>
This commit is contained in:
zer0x64 2020-02-27 04:52:04 -05:00 committed by GitHub
parent 09586ab512
commit 391edbd7da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 7 deletions

View File

@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [0.3.1] - 2020-01-23
### Changed
- Sharks recover method now accepts any iterable collection
## [0.3.0] - 2020-01-22 ## [0.3.0] - 2020-01-22
### Added ### Added
- Share struct which allows to convert from/to byte vectors - Share struct which allows to convert from/to byte vectors

View File

@ -1,6 +1,6 @@
[package] [package]
name = "sharks" name = "sharks"
version = "0.3.0" version = "0.3.1"
authors = ["Aitor Ruano <codearm@pm.me>"] authors = ["Aitor Ruano <codearm@pm.me>"]
description = "Fast, small and secure Shamir's Secret Sharing library crate" description = "Fast, small and secure Shamir's Secret Sharing library crate"
homepage = "https://github.com/c0dearm/sharks" homepage = "https://github.com/c0dearm/sharks"

View File

@ -58,7 +58,7 @@ const EXP_TABLE: [u8; 512] = [
0x58, 0xb0, 0x7d, 0xfa, 0xe9, 0xcf, 0x83, 0x1b, 0x36, 0x6c, 0xd8, 0xad, 0x47, 0x8e, 0x01, 0x02, 0x58, 0xb0, 0x7d, 0xfa, 0xe9, 0xcf, 0x83, 0x1b, 0x36, 0x6c, 0xd8, 0xad, 0x47, 0x8e, 0x01, 0x02,
]; ];
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] #[derive(Debug, PartialEq, Copy, Clone)]
pub struct GF256(pub u8); pub struct GF256(pub u8);
#[allow(clippy::suspicious_arithmetic_impl)] #[allow(clippy::suspicious_arithmetic_impl)]

View File

@ -64,7 +64,7 @@ impl Sharks {
math::get_evaluator(polys) math::get_evaluator(polys)
} }
/// Given a slice of shares, recovers the original secret. /// Given an iterable collection of shares, recovers the original secret.
/// If the number of distinct shares is less than the minimum threshold an `Err` is returned, /// If the number of distinct shares is less than the minimum threshold an `Err` is returned,
/// otherwise an `Ok` containing the secret. /// otherwise an `Ok` containing the secret.
/// ///
@ -82,13 +82,28 @@ impl Sharks {
/// secret = sharks.recover(&shares); /// secret = sharks.recover(&shares);
/// // Not enough shares to recover secret /// // Not enough shares to recover secret
/// assert!(secret.is_err()); /// assert!(secret.is_err());
pub fn recover(&self, shares: &[Share]) -> Result<Vec<u8>, &str> { pub fn recover<'a, T>(&self, shares: T) -> Result<Vec<u8>, &str>
let shares_x: HashSet<u8> = shares.iter().map(|s| s.x.0).collect(); where
T: IntoIterator<Item = &'a Share>,
T::IntoIter: Iterator<Item = &'a Share>,
{
let (keys, shares) = shares
.into_iter()
.map(|s| {
(
s.x.0,
Share {
x: s.x,
y: s.y.clone(),
},
)
})
.unzip::<u8, Share, HashSet<u8>, Vec<Share>>();
if shares_x.len() < self.0 as usize { if keys.len() < self.0 as usize {
Err("Not enough shares to recover original secret") Err("Not enough shares to recover original secret")
} else { } else {
Ok(math::interpolate(shares)) Ok(math::interpolate(shares.as_slice()))
} }
} }
} }