Skip to content

Commit

Permalink
chore(refactor): unify share creating methods
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Jan 29, 2024
1 parent 280c37e commit 0f3b9f4
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
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),
}
}

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

0 comments on commit 0f3b9f4

Please sign in to comment.