Skip to content

Commit

Permalink
use common::Polynomial for private polys; add operators and helpers t…
Browse files Browse the repository at this point in the history
…o Polynomial to make code cleaner
  • Loading branch information
xoloki committed Oct 9, 2024
1 parent 7ade470 commit 6f81a1d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 19 deletions.
37 changes: 34 additions & 3 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, AddAssign, Mul, MulAssign},
ops::{Add, AddAssign, Index, Mul, MulAssign},
};
use hashbrown::HashMap;
use num_traits::{One, Zero};
Expand Down Expand Up @@ -41,6 +41,7 @@ impl Random for Scalar {
}

/// A Polynomial where the parameters are not necessarily the same type as the args
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct Polynomial<Param, Arg> {
/// parameters for the polynomial
pub params: Vec<Param>,
Expand All @@ -53,7 +54,7 @@ impl<
> Polynomial<Param, Arg>
{
/// construct new random polynomial of the specified degree
pub fn random<RNG: RngCore + CryptoRng>(n: usize, rng: &mut RNG) -> Self {
pub fn random<RNG: RngCore + CryptoRng>(n: u32, rng: &mut RNG) -> Self {
let params = (0..n + 1).map(|_| Param::fill(rng)).collect::<Vec<Param>>();
Self {
params,
Expand All @@ -78,6 +79,32 @@ impl<
}
ret
}

/// length of the params
pub fn len(&self) -> usize {

Check failure on line 84 in src/common.rs

View workflow job for this annotation

GitHub Actions / clippy

struct `Polynomial` has a public `len` method, but no `is_empty` method
self.params.len()
}
}

impl<Param, Arg> Index<usize> for Polynomial<Param, Arg> {
type Output = Param;
fn index<'a>(&'a self, i: usize) -> &'a Param {

Check failure on line 91 in src/common.rs

View workflow job for this annotation

GitHub Actions / clippy

the following explicit lifetimes could be elided: 'a
&self.params[i]
}
}

impl<Param, Arg, T> Mul<T> for Polynomial<Param, Arg>
where
Param: Clone + Zero + Random + Add + AddAssign<<Arg as Mul<Param>>::Output> + Mul<T>,
Arg: Clone + One + Mul<T> + Mul<Param> + MulAssign,
T: Clone + Zero + Random + Add + AddAssign<<Arg as Mul<T>>::Output>,
Vec<T>: FromIterator<<Param as Mul<T>>::Output>,
{
type Output = Polynomial<T, Arg>;
fn mul(self, x: T) -> Self::Output {
let params: Vec<T> = self.params.iter().map(|p| p.clone() * x.clone()).collect();
Polynomial::new(params)
}
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
Expand Down Expand Up @@ -395,7 +422,7 @@ pub mod test {
#[allow(non_snake_case)]
fn polynomial() {
let mut rng = OsRng;
let n = 16usize;
let n = 16u32;

let poly = super::Polynomial::<Scalar, Scalar>::random(n - 1, &mut rng);
let params = poly.params.clone();
Expand All @@ -417,6 +444,10 @@ pub mod test {
let b = public_poly.eval(Scalar::from(8));
assert_eq!(a * G, b);

let mul_poly = poly * G;
let m = mul_poly.eval(Scalar::from(8));
assert_eq!(a * G, m);

let b = compute::poly(&Scalar::from(8), &public_params);
assert_eq!(a * G, b.unwrap());

Expand Down
6 changes: 4 additions & 2 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use rand_core::{CryptoRng, RngCore};
use serde::{Deserialize, Serialize};

use crate::{
common::{MerkleRoot, Nonce, PolyCommitment, PublicNonce, Signature, SignatureShare},
common::{
MerkleRoot, Nonce, PolyCommitment, Polynomial, PublicNonce, Signature, SignatureShare,
},
curve::{point::Point, scalar::Scalar},
errors::{AggregatorError, DkgError},
taproot::SchnorrProof,
Expand All @@ -14,7 +16,7 @@ use crate::{
/// The saved state required to reconstruct a party
pub struct PartyState {
/// The party's private polynomial
pub polynomial: Option<Vec<Scalar>>,
pub polynomial: Option<Polynomial<Scalar, Scalar>>,
/// The key IDS and associate private keys for this party
pub private_keys: Vec<(u32, Scalar)>,
/// The nonce being used by this party
Expand Down
11 changes: 7 additions & 4 deletions src/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use rand_core::{CryptoRng, RngCore};
use tracing::warn;

use crate::{
common::{CheckPrivateShares, Nonce, PolyCommitment, PublicNonce, Signature, SignatureShare},
common::{
CheckPrivateShares, Nonce, PolyCommitment, Polynomial, PublicNonce, Signature,
SignatureShare,
},
compute,
curve::{
point::{Point, G},
Expand All @@ -25,7 +28,7 @@ pub struct Party {
/// The public key
pub public_key: Point,
/// The polynomial used for Lagrange interpolation
pub f: Option<Vec<Scalar>>,
pub f: Option<Polynomial<Scalar, Scalar>>,
num_keys: u32,
threshold: u32,
private_key: Scalar,
Expand Down Expand Up @@ -92,7 +95,7 @@ impl Party {
if let Some(poly) = &self.f {
Some(PolyCommitment {
id: ID::new(&self.id(), &poly[0], rng),
poly: (0..poly.len()).map(|i| &poly[i] * G).collect(),
poly: (poly.clone() * G).params,
})
} else {
warn!("get_poly_commitment called with no polynomial");
Expand Down Expand Up @@ -129,7 +132,7 @@ impl Party {
if let Some(poly) = &self.f {
let mut shares = HashMap::new();
for i in 1..self.num_keys + 1 {
shares.insert(i, compute::private_poly(compute::id(i), poly));
shares.insert(i, poly.eval(compute::id(i)));
}
shares
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rand_core::{CryptoRng, RngCore};
use tracing::warn;

use crate::{
common::{Nonce, PolyCommitment, PublicNonce, Signature, SignatureShare},
common::{Nonce, PolyCommitment, Polynomial, PublicNonce, Signature, SignatureShare},
compute,
curve::{
point::{Point, G},
Expand All @@ -28,7 +28,7 @@ pub struct Party {
num_keys: u32,
num_parties: u32,
threshold: u32,
f: Option<Vec<Scalar>>,
f: Option<Polynomial<Scalar, Scalar>>,
private_keys: HashMap<u32, Scalar>,
group_key: Point,
nonce: Nonce,
Expand Down Expand Up @@ -72,7 +72,7 @@ impl Party {
if let Some(poly) = &self.f {
Some(PolyCommitment {
id: ID::new(&self.id(), &poly[0], rng),
poly: (0..poly.len()).map(|i| &poly[i] * G).collect(),
poly: (poly.clone() * G).params,
})
} else {
warn!("get_poly_commitment called with no polynomial");
Expand All @@ -85,7 +85,7 @@ impl Party {
let mut shares = HashMap::new();
if let Some(poly) = &self.f {
for i in 1..self.num_keys + 1 {
shares.insert(i, compute::private_poly(compute::id(i), poly));
shares.insert(i, poly.eval(compute::id(i)));
}
} else {
warn!("get_poly_commitment called with no polynomial");
Expand Down
16 changes: 10 additions & 6 deletions src/vss.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
use rand_core::{CryptoRng, RngCore};

use crate::common::Polynomial;
use crate::curve::scalar::Scalar;

/// A verifiable secret share algorithm
pub struct VSS {}

impl VSS {
/// Construct a random polynomial of the passed degree `n`
pub fn random_poly<RNG: RngCore + CryptoRng>(n: u32, rng: &mut RNG) -> Vec<Scalar> {
(0..n + 1).map(|_| Scalar::random(rng)).collect()
pub fn random_poly<RNG: RngCore + CryptoRng>(
n: u32,
rng: &mut RNG,
) -> Polynomial<Scalar, Scalar> {
Polynomial::random(n, rng)
}

/// Construct a random polynomial of the passed degree `n` using the passed constant term
pub fn random_poly_with_constant<RNG: RngCore + CryptoRng>(
n: u32,
constant: Scalar,
rng: &mut RNG,
) -> Vec<Scalar> {
let mut params: Vec<Scalar> = (0..n + 1).map(|_| Scalar::random(rng)).collect();
params[0] = constant;
) -> Polynomial<Scalar, Scalar> {
let mut poly = Polynomial::random(n, rng);
poly.params[0] = constant;

params
poly
}
}

0 comments on commit 6f81a1d

Please sign in to comment.