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

Validator ordering changes #172

Merged
merged 9 commits into from
Feb 5, 2024
2 changes: 1 addition & 1 deletion ferveo-wasm/tests/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn setup_dkg(shares_num: u32, security_threshold: u32) -> TestSetup {
// Each validator holds their own DKG instance and generates a transcript every
// validator, including themselves
let messages = validators.iter().map(|sender| {
let dkg = Dkg::new(
let mut dkg = Dkg::new(
TAU,
shares_num,
security_threshold,
Expand Down
12 changes: 8 additions & 4 deletions ferveo/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::bindings_python;
use crate::bindings_wasm;
pub use crate::EthereumAddress;
use crate::{
do_verify_aggregation, Error, PVSSMap, PubliclyVerifiableParams,
do_verify_aggregation, Error, Message, PVSSMap, PubliclyVerifiableParams,
PubliclyVerifiableSS, Result,
};

Expand Down Expand Up @@ -222,10 +222,14 @@ impl Dkg {
}

pub fn generate_transcript<R: RngCore>(
&self,
&mut self,
rng: &mut R,
) -> Result<Transcript> {
self.0.create_share(rng)
match self.0.share(rng) {
Ok(Message::Deal(transcript)) => Ok(transcript),
Err(e) => Err(e),
_ => Err(Error::InvalidDkgStateToDeal),
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this block code do?

Copy link
Author

@piotr-roslaniec piotr-roslaniec Feb 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a pattern-matching expression with different cases on each line:

  • The first line describes a case where we get the intended result - We just need to unwrap transcript from a Message struct, and specifically from Message::Deal variant. I'm considering deprecating Message as we only use it internally.
  • The second line handles the error result
  • The third one handles other Message variants. There are two currently, Message::Deal and Message:Aggregate. We treat results other than Message::Deal as errors, as we don't expect the DKG to be ready to aggregate at this point.

}

pub fn aggregate_transcripts(
Expand Down Expand Up @@ -435,7 +439,7 @@ mod test_ferveo_api {
let messages: Vec<_> = validators
.iter()
.map(|sender| {
let dkg = Dkg::new(
let mut dkg = Dkg::new(
tau,
shares_num,
security_threshold,
Expand Down
4 changes: 2 additions & 2 deletions ferveo/src/bindings_python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ impl Dkg {
DkgPublicKey(self.0.public_key())
}

pub fn generate_transcript(&self) -> PyResult<Transcript> {
pub fn generate_transcript(&mut self) -> PyResult<Transcript> {
let rng = &mut thread_rng();
let transcript = self
.0
Expand Down Expand Up @@ -776,7 +776,7 @@ mod test_ferveo_python {
.iter()
.cloned()
.map(|sender| {
let dkg = Dkg::new(
let mut dkg = Dkg::new(
tau,
shares_num,
security_threshold,
Expand Down
2 changes: 1 addition & 1 deletion ferveo/src/bindings_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ impl Dkg {
}

#[wasm_bindgen(js_name = "generateTranscript")]
pub fn generate_transcript(&self) -> JsResult<Transcript> {
pub fn generate_transcript(&mut self) -> JsResult<Transcript> {
let rng = &mut thread_rng();
let transcript = self.0.generate_transcript(rng).map_err(map_js_err)?;
Ok(Transcript(transcript))
Expand Down
16 changes: 6 additions & 10 deletions ferveo/src/dkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::BTreeMap;

use ark_ec::{pairing::Pairing, AffineRepr, CurveGroup};
use ark_poly::EvaluationDomain;
use ark_std::UniformRand;
use ferveo_common::PublicKey;
use measure_time::print_time;
use rand::RngCore;
Expand Down Expand Up @@ -159,24 +160,19 @@ impl<E: Pairing> PubliclyVerifiableDkg<E> {
/// Returns a PVSS dealing message to post on-chain
pub fn share<R: RngCore>(&mut self, rng: &mut R) -> Result<Message<E>> {
print_time!("PVSS Sharing");
let vss = self.create_share(rng)?;
match self.state {
DkgState::Sharing { .. } | DkgState::Dealt => {
let vss = PubliclyVerifiableSS::<E>::new(
&E::ScalarField::rand(rng),
self,
rng,
)?;
Ok(Message::Deal(vss))
}
_ => Err(Error::InvalidDkgStateToDeal),
}
}

// TODO: Make private, use `share` instead. Currently used only in bindings
pub fn create_share<R: RngCore>(
&self,
rng: &mut R,
) -> Result<PubliclyVerifiableSS<E>> {
use ark_std::UniformRand;
PubliclyVerifiableSS::<E>::new(&E::ScalarField::rand(rng), self, rng)
}

/// Aggregate all received PVSS messages into a single message, prepared to post on-chain
pub fn aggregate(&self) -> Result<Message<E>> {
match self.state {
Expand Down
Loading