Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
42 changes: 39 additions & 3 deletions crates/aptos-crypto/src/arkworks/shamir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,41 @@ pub trait Reconstructable<SSC: traits::SecretSharingConfig>: Sized {
fn reconstruct(sc: &SSC, shares: &[ShamirShare<Self::ShareValue>]) -> Result<Self>;
}

use crate::weighted_config::WeightedConfigArkworks;


// impl<F: FftField, SK: Reconstructable<ShamirThresholdConfig<F>>> Reconstructable<WeightedConfigArkworks<F>> for SK {
// type ShareValue = Vec<SK::ShareValue>;

// fn reconstruct(
// sc: &WeightedConfigArkworks<F>,
// shares: &[ShamirShare<Self::ShareValue>],
// ) -> anyhow::Result<Self> {
// let mut flattened_shares = Vec::with_capacity(sc.get_total_weight());

// // println!();
// for (player, sub_shares) in shares {
// // println!(
// // "Flattening {} share(s) for player {player}",
// // sub_shares.len()
// // );
// for (pos, share) in sub_shares.iter().enumerate() {
// let virtual_player = sc.get_virtual_player(player, pos);

// // println!(
// // " + Adding share {pos} as virtual player {virtual_player}: {:?}",
// // share
// // );
// // TODO(Performance): Avoiding the cloning here might be nice
// let tuple = (virtual_player, share.clone());
// flattened_shares.push(tuple);
// }
// }

// SK::reconstruct(sc.get_threshold_config(), &flattened_shares)
// }
// }

/// Configuration for a threshold cryptography scheme. Usually one restricts `F` to `Primefield`
/// but any field is theoretically possible.
#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
Expand Down Expand Up @@ -299,15 +334,16 @@ impl<F: FftField> ShamirThresholdConfig<F> {
impl<T: WeightedSum> Reconstructable<ShamirThresholdConfig<T::Scalar>> for T {
type ShareValue = T;

// Can receive more than `sc.t` shares, but will only use the first `sc.t` shares for efficiency
fn reconstruct(
sc: &ShamirThresholdConfig<T::Scalar>,
shares: &[ShamirShare<Self::ShareValue>],
) -> Result<Self> {
if shares.len() != sc.t {
Err(anyhow!("Incorrect number of shares provided"))
if shares.len() < sc.t {
Err(anyhow!("Incorrect number of shares provided, received {} but expected at least {}", shares.len(), sc.t))
} else {
let (roots_of_unity_indices, bases): (Vec<usize>, Vec<Self::ShareValue>) =
shares.iter().map(|(p, g_y)| (p.get_id(), g_y)).collect();
shares[..sc.t].iter().map(|(p, g_y)| (p.get_id(), g_y)).collect();

let lagrange_coeffs = sc.lagrange_for_subset(&roots_of_unity_indices);

Expand Down
49 changes: 49 additions & 0 deletions crates/aptos-crypto/src/weighted_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,62 @@ pub struct WeightedConfig<TC: ThresholdConfig> {
min_weight: usize,
}

/// Splits a flattened list into sublists for each player based on their weights.
///
/// # Parameters
/// - `flattened`: A slice containing the flattened list of items to split.
/// - `sc`: A `WeightedConfig` that provides the weight for each player.
///
/// # Returns
/// A vector of vectors, where each inner vector contains the items assigned to a single player.
pub fn unflatten_by_weights<T, TC>(flattened: &[T], sc: &WeightedConfig<TC>) -> Vec<Vec<T>>
where
T: Clone,
TC: ThresholdConfig,
{
let mut start = 0;
(0..sc.get_total_num_players())
.map(|i| {
let weight = sc.get_player_weight(&sc.get_player(i));
let slice = &flattened[start..start + weight];
start += weight;
slice.to_vec()
})
.collect()
}

/// Weighted config for the BLSTRS-based PVSS
pub type WeightedConfigBlstrs = WeightedConfig<ThresholdConfigBlstrs>;

/// Weighted config for the Arkworks-based PVSS
#[allow(type_alias_bounds)]
pub type WeightedConfigArkworks<F: FftField> = WeightedConfig<ShamirThresholdConfig<F>>;

// impl<F: FftField,
// SK: Reconstructable<ShamirThresholdConfig<F>>>
// Reconstructable<WeightedConfigArkworks<F>> for SK
// {
// type ShareValue = Vec<SK::ShareValue>;

// fn reconstruct(
// sc: &WeightedConfigArkworks<F>,
// shares: &[ShamirShare<Self::ShareValue>],
// ) -> anyhow::Result<Self> {
// let mut flattened_shares = Vec::with_capacity(sc.get_total_weight());

// for (player, sub_shares) in shares {
// for (pos, share) in sub_shares.iter().enumerate() {
// let virtual_player = sc.get_virtual_player(player, pos);
// // TODO(Performance): Avoiding the cloning here might be nice
// let tuple = (virtual_player, share.clone());
// flattened_shares.push(tuple);
// }
// }

// SK::reconstruct(sc.get_threshold_config(), &flattened_shares)
// }
// }

impl<TC: ThresholdConfig> WeightedConfig<TC> {
#[allow(non_snake_case)]
/// Initializes a weighted secret sharing configuration with threshold weight `threshold_weight`
Expand Down
148 changes: 147 additions & 1 deletion crates/aptos-dkg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub use aptos_crypto::{
blstrs::{G1_PROJ_NUM_BYTES, G2_PROJ_NUM_BYTES, SCALAR_NUM_BYTES},
};
use ark_ec::pairing::Pairing;
use ark_ff::{Fp, FpConfig};
use ark_ff::{FftField, Fp, FpConfig};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use more_asserts::{assert_ge, assert_le};
use rand::Rng;
Expand All @@ -42,6 +42,7 @@ pub mod sigma_protocol;
pub mod utils;
pub mod weighted_vuf;
use aptos_crypto::arkworks::shamir::ShamirShare;
use aptos_crypto::weighted_config::WeightedConfigArkworks;

/// A wrapper around `E::ScalarField` to prevent overlapping trait implementations.
///
Expand Down Expand Up @@ -86,6 +87,11 @@ impl<E: Pairing> Scalar<E> {
vv.into_iter().map(Self::vec_from_inner).collect()
}

/// Converts a `Vec<Vec<Vec<E::ScalarField>>>` into a `Vec<Vec<Vec<Scalar<E>>>>` safely.
pub fn vecvecvec_from_inner(vvv: Vec<Vec<Vec<E::ScalarField>>>) -> Vec<Vec<Vec<Self>>> {
vvv.into_iter().map(Self::vecvec_from_inner).collect()
}

/// Converts a `&[E::ScalarField]` into a `Vec<Scalar<E>>` safely.
pub fn vec_from_inner_slice(slice: &[E::ScalarField]) -> Vec<Self> {
slice.iter().copied().map(Self).collect()
Expand Down Expand Up @@ -123,3 +129,143 @@ impl<const N: usize, P: FpConfig<N>, E: Pairing<ScalarField = Fp<P, N>>>
)?))
}
}

// TODO: make this stuff more generic, like blstrs....
// TODO: can make stuff more generic... what if we make a thresholdconfig a weightedconfig with weights 1?
impl<const N: usize, P: FpConfig<N>, E: Pairing<ScalarField = Fp<P, N>>>
Reconstructable<WeightedConfigArkworks<E::ScalarField>> for Scalar<E>
{
type ShareValue = Vec<Scalar<E>>;

fn reconstruct(
sc: &WeightedConfigArkworks<E::ScalarField>,
shares: &[ShamirShare<Self::ShareValue>],
) -> anyhow::Result<Self> {
let mut flattened_shares = Vec::with_capacity(sc.get_total_weight());

// println!();
for (player, sub_shares) in shares {
// println!(
// "Flattening {} share(s) for player {player}",
// sub_shares.len()
// );
for (pos, share) in sub_shares.iter().enumerate() {
let virtual_player = sc.get_virtual_player(player, pos);

// println!(
// " + Adding share {pos} as virtual player {virtual_player}: {:?}",
// share
// );
// TODO(Performance): Avoiding the cloning here might be nice
let tuple = (virtual_player, share.clone());
flattened_shares.push(tuple);
}
}

assert_ge!(flattened_shares.len(), sc.get_threshold_weight());
assert_le!(flattened_shares.len(), sc.get_total_weight());

Scalar::<E>::reconstruct(sc.get_threshold_config(), &flattened_shares)
}
}


// impl<F: FftField, SK: Reconstructable<ShamirThresholdConfig<F>>> Reconstructable<WeightedConfigArkworks<F>> for SK {
// type ShareValue = Vec<SK::ShareValue>;

// fn reconstruct(
// sc: &WeightedConfigArkworks<F>,
// shares: &[ShamirShare<Self::ShareValue>],
// ) -> anyhow::Result<Self> {
// let mut flattened_shares = Vec::with_capacity(sc.get_total_weight());

// // println!();
// for (player, sub_shares) in shares {
// // println!(
// // "Flattening {} share(s) for player {player}",
// // sub_shares.len()
// // );
// for (pos, share) in sub_shares.iter().enumerate() {
// let virtual_player = sc.get_virtual_player(player, pos);

// // println!(
// // " + Adding share {pos} as virtual player {virtual_player}: {:?}",
// // share
// // );
// // TODO(Performance): Avoiding the cloning here might be nice
// let tuple = (virtual_player, share.clone());
// flattened_shares.push(tuple);
// }
// }

// SK::reconstruct(sc.get_threshold_config(), &flattened_shares)
// }
// }


// impl<const N: usize, P: FpConfig<N>, E: Pairing<ScalarField = Fp<P, N>>>
// Reconstructable<WeightedConfigArkworks<E::ScalarField>> for Vec<Scalar<E>>
// {
// type ShareValue = Vec<Scalar<E>>;

// fn reconstruct(
// sc: &WeightedConfigArkworks<E::ScalarField>,
// shares: &[ShamirShare<Self::ShareValue>],
// ) -> anyhow::Result<Self> {
// assert_ge!(shares.len(), sc.get_threshold_weight());
// assert_le!(shares.len(), sc.get_total_weight());

// // Number of entries per vector (assume all shares are same length)
// let entry_count = shares[0].1.len();

// // Reconstruct each entry independently
// let mut result = Vec::with_capacity(entry_count);

// for idx in 0..entry_count {
// // Extract the idx-th element from each share
// let shares_for_idx: Vec<(Player, E::ScalarField)> = shares
// .iter()
// .map(|(player, vec)| (*player, vec[idx].0))
// .collect();

// // Reconstruct this element
// let reconstructed = Scalar(E::ScalarField::reconstruct(
// &sc.get_threshold_config(),
// &shares_for_idx,
// )?);

// result.push(reconstructed);
// }

// Ok(result)
// }
// }

// impl<C, T> Reconstructable<C> for Vec<T>
// where
// T: Reconstructable<C> + Clone, // Clone may be needed depending on ShareValue
// {
// type ShareValue = Vec<T>;

// fn reconstruct(
// config: &C,
// shares: &[ShamirShare<Self::ShareValue>],
// ) -> anyhow::Result<Self> {
// assert!(!shares.is_empty(), "No shares provided");

// // Number of entries per vector (assume all shares are the same length)
// let entry_count = shares[0].1.len();

// // Reconstruct each entry independently
// (0..entry_count)
// .map(|idx| {
// let shares_for_idx: Vec<(Player, T::ShareValue)> = shares
// .iter()
// .map(|(player, vec)| (*player, vec[idx].clone()))
// .collect();

// T::reconstruct(config, &shares_for_idx)
// })
// .collect()
// }
// }
Loading
Loading