Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce UnindexedValues #735

Merged
merged 3 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fastcrypto-tbls/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl<G: GroupElement + Serialize> Nodes<G> {
}
}

/// Get the share ids of a node.
/// Get the share ids of a node (ordered).
pub fn share_ids_of(&self, id: PartyId) -> Vec<ShareIndex> {
// TODO: [perf opt] Cache this
self.share_ids_iter()
Expand Down
3 changes: 2 additions & 1 deletion fastcrypto-tbls/src/tbls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ use std::borrow::Borrow;

use crate::dl_verification::{batch_coefficients, get_random_scalars};
use crate::polynomial::Poly;
use crate::types::IndexedValue;
use crate::types::{IndexedValue, UnindexedValues};
use fastcrypto::error::{FastCryptoError, FastCryptoResult};
use fastcrypto::groups::{GroupElement, HashToGroupElement, MultiScalarMul, Scalar};
use fastcrypto::traits::AllowedRng;
use itertools::Itertools;

pub type Share<S> = IndexedValue<S>;
pub type PartialSignature<S> = IndexedValue<S>;
pub type CompactPartialSignatures<S> = UnindexedValues<S>;
benr-ml marked this conversation as resolved.
Show resolved Hide resolved

/// Trait [ThresholdBls] provides sign & verify functions for standard and partial BLS signatures.
pub trait ThresholdBls {
Expand Down
24 changes: 23 additions & 1 deletion fastcrypto-tbls/src/tests/tbls_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

use crate::dl_verification::verify_poly_evals;
use crate::polynomial::Poly;
use crate::tbls::Share;
use crate::tbls::{CompactPartialSignatures, Share};
use crate::types::ShareIndex;
use crate::{tbls::ThresholdBls, types::ThresholdBls12381MinSig};
use fastcrypto::groups::bls12381::{G1Element, G2Element, Scalar};
use fastcrypto::groups::{bls12381, GroupElement};
Expand Down Expand Up @@ -193,3 +194,24 @@ fn test_verify_poly_evals() {
shares[1].value -= Scalar::generator();
assert!(verify_poly_evals(&shares, &public_poly, &mut thread_rng()).is_err());
}

#[test]
fn test_compact() {
let private_poly = Poly::<bls12381::Scalar>::rand(99, &mut thread_rng());
let share_ids = (1..=1)
.map(|i| ShareIndex::new(i).unwrap())
.collect::<Vec<_>>();
let shares = share_ids
.iter()
.map(|i| private_poly.eval(*i))
.collect::<Vec<_>>();
let msg = b"test";
let sigs = shares
.iter()
.map(|s| ThresholdBls12381MinSig::partial_sign(s, msg))
.collect::<Vec<_>>();

let compact: CompactPartialSignatures<G1Element> = sigs.clone().into();
let sigs2 = compact.add_indexes(&share_ids).unwrap();
assert_eq!(sigs, sigs2);
}
35 changes: 35 additions & 0 deletions fastcrypto-tbls/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,41 @@
pub value: A,
}

/// Basic wrapper of a set of values that are not associated with indexes, assuming the indexes are known to all
/// parties. Used to reduce the size of the messages in the protocol.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]

Check warning on line 55 in fastcrypto-tbls/src/types.rs

View check run for this annotation

Codecov / codecov/patch

fastcrypto-tbls/src/types.rs#L55

Added line #L55 was not covered by tests
pub struct UnindexedValues<A> {
pub values: Vec<A>,
}

impl<A> From<Vec<IndexedValue<A>>> for UnindexedValues<A> {
fn from(index_values: Vec<IndexedValue<A>>) -> Self {
let mut values: Vec<A> = Vec::new();
benr-ml marked this conversation as resolved.
Show resolved Hide resolved
for v in index_values {
values.push(v.value);
}
Self { values }
}
}

impl<A> UnindexedValues<A> {
pub fn add_indexes(self, indexes: &[ShareIndex]) -> FastCryptoResult<Vec<IndexedValue<A>>> {
if self.values.len() != indexes.len() {
return Err(FastCryptoError::InvalidInput);

Check warning on line 73 in fastcrypto-tbls/src/types.rs

View check run for this annotation

Codecov / codecov/patch

fastcrypto-tbls/src/types.rs#L73

Added line #L73 was not covered by tests
}
let values = self
.values
.into_iter()
.zip(indexes)
.map(|(value, index)| IndexedValue {
index: *index,
value,
})
.collect();
Ok(values)
}
}

/// ECIES related types with Ristretto points.
///
pub type PrivateEciesKey = ecies::PrivateKey<RistrettoPoint>;
Expand Down
Loading