From 842941c79aa97ff79dc14950dd50b1d7a2ad9251 Mon Sep 17 00:00:00 2001 From: Elichai Turkel Date: Mon, 20 Dec 2021 13:34:31 +0200 Subject: [PATCH] Add a cautionary assert to `share_at_indices` to prevent user from misusing the api and passing 0 as index (#157) * Add an assert to make sure that no one passes 0 as an index to secret sharing * Add a comment warning users on share_at_indices * Change share_at_indices to recieve an iterator of NonZeroU16 to enforce non zero at the type level --- .../secret_sharing/feldman_vss.rs | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/cryptographic_primitives/secret_sharing/feldman_vss.rs b/src/cryptographic_primitives/secret_sharing/feldman_vss.rs index 014a13b0..f9320f12 100644 --- a/src/cryptographic_primitives/secret_sharing/feldman_vss.rs +++ b/src/cryptographic_primitives/secret_sharing/feldman_vss.rs @@ -7,6 +7,7 @@ */ use std::convert::{TryFrom, TryInto}; +use std::num::NonZeroU16; use std::{fmt, ops}; use serde::{Deserialize, Serialize}; @@ -102,18 +103,24 @@ impl VerifiableSS { ) } - // generate VerifiableSS from a secret and user defined x values (in case user wants to distribute point f(1), f(4), f(6) and not f(1),f(2),f(3)) - pub fn share_at_indices( + /// generate VerifiableSS from a secret and user defined x values (in case user wants to distribute point f(1), f(4), f(6) and not f(1),f(2),f(3)) + /// NOTE: The caller should make sure that `t`, `n` and the contents of `index_vec` can't be controlled by a malicious party. + pub fn share_at_indices( t: u16, n: u16, secret: &Scalar, - index_vec: &[u16], - ) -> (VerifiableSS, SecretShares) { - assert_eq!(usize::from(n), index_vec.len()); + indicies: I, + ) -> (VerifiableSS, SecretShares) + where + I: IntoIterator, + I::IntoIter: ExactSizeIterator, + { + let indicies = indicies.into_iter(); + assert_eq!(usize::from(n), indicies.len()); let polynomial = Polynomial::::sample_exact_with_fixed_const_term(t, secret.clone()); let shares = polynomial - .evaluate_many_bigint(index_vec.iter().cloned()) + .evaluate_many_bigint(indicies.map(NonZeroU16::get)) .collect(); let g = Point::::generator(); @@ -286,8 +293,12 @@ mod tests { fn test_secret_sharing_3_out_of_5_at_indices() { let secret = Scalar::random(); let parties = [1, 2, 4, 5, 6]; - let (vss_scheme, secret_shares) = - VerifiableSS::::share_at_indices(3, 5, &secret, &parties); + let (vss_scheme, secret_shares) = VerifiableSS::::share_at_indices( + 3, + 5, + &secret, + parties.iter().map(|&v| NonZeroU16::new(v).unwrap()), + ); let shares_vec = vec![ secret_shares[0].clone(),