Skip to content

Commit

Permalink
fix state machine tests by adding keep_constant field to DkgBegin; fi…
Browse files Browse the repository at this point in the history
…x compute::private_poly by exponentiating the arg
  • Loading branch information
xoloki committed Oct 8, 2024
1 parent 8b3aa33 commit 1d6c447
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wsts"
version = "9.2.0"
version = "10.0.0"
edition = "2021"
authors = ["Joey Yandle <[email protected]>"]
license = "Apache-2.0"
Expand Down
100 changes: 58 additions & 42 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::{
fmt::{Debug, Display, Formatter, Result as FmtResult},
ops::Add,
ops::{Add, AddAssign, Mul},
};
use hashbrown::HashMap;
use num_traits::{One, Zero};
Expand Down Expand Up @@ -30,25 +30,34 @@ pub struct Polynomial<Param, Arg> {
}

impl<
Param: Clone + Zero + Add + std::ops::AddAssign<<Arg as std::ops::Mul<Param>>::Output>,
Arg: Clone + std::ops::Mul<Param>,
Param: Clone + Zero + Add + AddAssign<<Arg as Mul<Param>>::Output>,
Arg: Clone + Mul<Param>,
> Polynomial<Param, Arg>
{
/*
/// evaluate the polynomial with the passed arg
pub fn new<RNG: RngCore + CryptoRng>(n: usize, rng: &RNG) -> Self {
let data = (0..n).map(|_| Param::random(rng)).collect::<Vec<Param>>();
/// evaluate the polynomial with the passed arg
pub fn new<RNG: RngCore + CryptoRng>(n: usize, rng: &RNG) -> Self {
let data = (0..n).map(|_| Param::random(rng)).collect::<Vec<Param>>();
Self {
data,
_x: std::marker::PhantomData,
}
}
*/
/// construct new polynomial from passed params
pub fn new(params: Vec<Param>) -> Self {
Self {
data,
params,
_x: std::marker::PhantomData,
}
}
*/
}
/// evaluate the polynomial with the passed arg
pub fn eval(&self, x: Arg) -> Param {
//let mut pow = Scalar::one();
let mut ret = Param::zero();
for i in 0..self.params.len() {
ret += x.clone() * self.params[i].clone();
//pow *= x.clone();
}
ret
}
Expand Down Expand Up @@ -353,49 +362,56 @@ pub mod test_helpers {

#[cfg(test)]
pub mod test {
use num_traits::Zero;
//use num_traits::Zero;
use rand_core::OsRng;

use crate::{
common::TupleProof,
//compute,
curve::{point::Point, scalar::Scalar},
};

#[test]
#[allow(non_snake_case)]
fn polynomial() {
let mut rng = OsRng;
let n = 16usize;

let params = (0..n)
.map(|_| Scalar::random(&mut rng))
.collect::<Vec<Scalar>>();
let poly = super::Polynomial {
params,
_x: std::marker::PhantomData,
};

let y = poly.eval(Scalar::from(1));
let mut z = Scalar::zero();
for i in 0..poly.params.len() {
z += poly.params[i];
}
assert_eq!(y, z);

let params = (0..n)
.map(|_| Point::from(Scalar::random(&mut rng)))
.collect::<Vec<Point>>();
let poly = super::Polynomial {
params,
_x: std::marker::PhantomData,
};

let y = poly.eval(Scalar::from(1));
let mut z = Point::zero();
for i in 0..poly.params.len() {
z += poly.params[i];
}
assert_eq!(y, z);
/*
let mut rng = OsRng;
let n = 16usize;
let params = (0..n)
.map(|_| Scalar::random(&mut rng))
.collect::<Vec<Scalar>>();
let poly = super::Polynomial::new(params.clone());
let y = poly.eval(Scalar::from(1));
let mut z = Scalar::zero();
for i in 0..poly.params.len() {
z += poly.params[i];
}
assert_eq!(y, z);
let b = compute::private_poly(Scalar::from(8), &params);
assert_eq!(y, b);
let public_params = params.iter().map(|p| p * G).collect::<Vec<Point>>();
let public_poly: super::Polynomial<Point, Scalar> = super::Polynomial::new(public_params.clone());
let a = poly.eval(Scalar::from(8));
let b = public_poly.eval(Scalar::from(8));
assert_eq!(a * G, b);
let b = compute::poly(&Scalar::from(8), &public_params);
assert_eq!(a * G, b.unwrap());
let params = (0..n)
.map(|_| Point::from(Scalar::random(&mut rng)))
.collect::<Vec<Point>>();
let poly = super::Polynomial::new(params);
let y = poly.eval(Scalar::from(1));
let mut z = Point::zero();
for i in 0..poly.params.len() {
z += poly.params[i];
}
assert_eq!(y, z);
*/
}

#[test]
Expand Down
37 changes: 35 additions & 2 deletions src/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,14 @@ pub fn poly(x: &Scalar, f: &Vec<Point>) -> Result<Point, PointError> {
Point::multimult(s, f.clone())
}

/// Evaluate the public polynomial `f` at scalar `x` using multi-exponentiation
/// Evaluate the private polynomial `f` at scalar `x`
#[allow(clippy::ptr_arg)]
pub fn private_poly(x: Scalar, f: &Vec<Scalar>) -> Scalar {
let mut pow = Scalar::one();
let mut sum = Scalar::zero();
for i in 0..f.len() {
sum += x * f[i];
sum += pow * f[i];
pow *= x;
}
sum
}
Expand Down Expand Up @@ -179,3 +181,34 @@ pub fn merkle_root(data: &[u8]) -> [u8; 32] {

hasher.finalize().into()
}

#[cfg(test)]
pub mod test {
//use num_traits::Zero;
use rand_core::OsRng;

use crate::{
compute,
curve::{
point::{Point, G},
scalar::Scalar,
},
};

#[test]
#[allow(non_snake_case)]
fn poly() {
let mut rng = OsRng;
let n = 16usize;

let private_poly = (0..n)
.map(|_| Scalar::random(&mut rng))
.collect::<Vec<Scalar>>();
let poly = private_poly.iter().map(|p| p * G).collect::<Vec<Point>>();

let x = compute::private_poly(Scalar::from(8), &private_poly);
let y = compute::poly(&Scalar::from(8), &poly);

assert_eq!(x * G, y.unwrap());
}
}
7 changes: 6 additions & 1 deletion src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,10 @@ pub struct DkgBegin {

impl Signable for DkgBegin {
fn hash(&self, hasher: &mut Sha256) {
let keep_constant = if self.keep_constant { [1u8] } else { [0u8] };
hasher.update("DKG_BEGIN".as_bytes());
hasher.update(self.dkg_id.to_be_bytes());
hasher.update(&keep_constant);

Check failure on line 137 in src/net.rs

View workflow job for this annotation

GitHub Actions / clippy

the borrowed expression implements the required traits
}
}

Expand Down Expand Up @@ -631,7 +633,10 @@ mod test {
#[test]
fn dkg_begin_verify_msg() {
let test_config = TestConfig::default();
let dkg_begin = DkgBegin { dkg_id: 0 };
let dkg_begin = DkgBegin {
dkg_id: 0,
keep_constant: false,
};
let dkg_private_begin = DkgPrivateBegin {
dkg_id: 0,
key_ids: Default::default(),
Expand Down
10 changes: 8 additions & 2 deletions src/state_machine/coordinator/fire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2697,7 +2697,10 @@ pub mod test {
let (packets, results) = coordinator
.process_inbound_messages(&[Packet {
sig: vec![],
msg: Message::DkgBegin(DkgBegin { dkg_id: old_id }),
msg: Message::DkgBegin(DkgBegin {
dkg_id: old_id,
keep_constant: false,
}),
}])
.unwrap();
assert!(packets.is_empty());
Expand All @@ -2709,7 +2712,10 @@ pub mod test {
let (packets, results) = coordinator
.process_inbound_messages(&[Packet {
sig: vec![],
msg: Message::DkgBegin(DkgBegin { dkg_id: id }),
msg: Message::DkgBegin(DkgBegin {
dkg_id: id,
keep_constant: false,
}),
}])
.unwrap();
assert!(packets.is_empty());
Expand Down
10 changes: 8 additions & 2 deletions src/state_machine/coordinator/frost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,10 @@ pub mod test {
let (packets, results) = coordinator
.process_inbound_messages(&[Packet {
sig: vec![],
msg: Message::DkgBegin(DkgBegin { dkg_id: old_id }),
msg: Message::DkgBegin(DkgBegin {
dkg_id: old_id,
keep_constant: false,
}),
}])
.unwrap();
assert!(packets.is_empty());
Expand All @@ -901,7 +904,10 @@ pub mod test {
let (packets, results) = coordinator
.process_inbound_messages(&[Packet {
sig: vec![],
msg: Message::DkgBegin(DkgBegin { dkg_id: id }),
msg: Message::DkgBegin(DkgBegin {
dkg_id: id,
keep_constant: false,
}),
}])
.unwrap();
assert!(packets.is_empty());
Expand Down
1 change: 1 addition & 0 deletions src/state_machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub enum SignError {
}

/// Result of a DKG or sign operation
#[derive(Debug, Clone)]
pub enum OperationResult {
/// DKG succeeded with the wrapped public key
Dkg(Point),
Expand Down
5 changes: 4 additions & 1 deletion src/state_machine/signer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,10 @@ pub mod test {
assert!(!signer.can_dkg_end());

// meet the conditions for DKG_END
let dkg_begin = Message::DkgBegin(DkgBegin { dkg_id: 1 });
let dkg_begin = Message::DkgBegin(DkgBegin {
dkg_id: 1,
keep_constant: false,
});
let dkg_public_shares = signer
.process(&dkg_begin)
.expect("failed to process DkgBegin");
Expand Down

0 comments on commit 1d6c447

Please sign in to comment.