Skip to content

Commit

Permalink
[frost] indicies not indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
nickfarrow committed Jan 10, 2024
1 parent b60d6c4 commit ed06d8a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
8 changes: 4 additions & 4 deletions schnorr_fun/src/frost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
//! # let public_poly2 = poly::to_point_poly(&secret_poly2);
//! # let public_poly3 = poly::to_point_poly(&secret_poly3);
//!
//! // Party indexes can be any non-zero scalar
//! // Party indicies can be any non-zero scalar
//! let my_index = s!(1).public();
//! let party_index2 = s!(2).public();
//! let party_index3 = s!(3).public();
Expand Down Expand Up @@ -197,8 +197,8 @@ use secp256kfun::{
/// It is used in interpolation and computation of the shared secret.
///
/// This index can be any non-zero [`Scalar`], but must be unique between parties.
/// In most cases it will make sense to use simple indexes `s!(1), s!(2), ...` for smaller backups.
/// Other applications may desire to use indexes corresponding to pre-existing keys or identifiers.
/// In most cases it will make sense to use simple indicies `s!(1), s!(2), ...` for smaller backups.
/// Other applications may desire to use indicies corresponding to pre-existing keys or identifiers.
pub type PartyIndex = Scalar<Public, NonZero>;

/// The FROST context.
Expand Down Expand Up @@ -245,7 +245,7 @@ impl<H, NG> Frost<H, NG> {
&self.nonce_gen
}

/// Create our secret shares to be shared with other participants using pre-existing indexes
/// Create our secret shares to be shared with other participants using pre-existing indicies
///
/// Each secret share needs to be securely communicated to the intended participant.
///
Expand Down
46 changes: 24 additions & 22 deletions secp256kfun/src/poly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,34 +67,36 @@ fn powers<S: Secrecy, Z: ZeroChoice>(x: Scalar<S, Z>) -> impl Iterator<Item = Sc
})
}

/// Calculate the lagrange coefficient for participant with index x_j and other signers indexes x_ms
/// Calculate the lagrange coefficient for participant with index x_j and other signers indicies x_ms
pub fn lagrange_lambda(
x_j: Scalar<impl Secrecy>,
x_ms: impl Iterator<Item = Scalar<impl Secrecy>>,
) -> Scalar<Public> {
x_ms.fold(Scalar::one(), |acc, x_m| {
let denominator = s!(x_m - x_j).non_zero().expect("indexes must be unique");
let denominator = s!(x_m - x_j).non_zero().expect("indicies must be unique");
s!(acc * x_m / denominator).public()
})
}
/// Get each lagrange basis polynomial a set of scalar indices.
///
/// The nth polynomial at the nth index takes on the value of 1 (unit).
fn unit_basis_polys(indexes: &[Scalar<Public, impl ZeroChoice>]) -> Vec<Vec<Scalar<Public, Zero>>> {
fn unit_basis_polys(
indicies: &[Scalar<Public, impl ZeroChoice>],
) -> Vec<Vec<Scalar<Public, Zero>>> {
// Calculated from the product of these indices coefficients:
// l_j(x) = Product[ (x-x_m)/(x_j-x_m), j!=m ]
// Or
// l_j(x) = Product[ a_m*x + b_m, j!=m], where a_m = 1/(x_j-x_m) and b_m = -x_m*a_m.
indexes
indicies
.clone()
.into_iter()
.enumerate()
.map(|(j, x_j)| {
let mut coefficients: Vec<_> = vec![];
for (_, x_m) in indexes.iter().enumerate().filter(|(m, _)| *m != j) {
for (_, x_m) in indicies.iter().enumerate().filter(|(m, _)| *m != j) {
let a_m = s!(x_j - x_m)
.non_zero()
.expect("points must lie at unique indexes to interpolate");
.expect("points must lie at unique indicies to interpolate");
let b_m = s!(-x_m / a_m).mark_zero();

// Multiply out the product. Beginning with the first two coefficients
Expand Down Expand Up @@ -122,17 +124,17 @@ fn unit_basis_polys(indexes: &[Scalar<Public, impl ZeroChoice>]) -> Vec<Vec<Scal
.collect()
}

/// Find the coefficients of the polynomial that interpolates a set of points at unique indexes.
/// Find the coefficients of the polynomial that interpolates a set of points at unique indicies.
///
/// Panics if the indexes are not unique.
/// Panics if the indicies are not unique.
///
/// A vector with a tail of zero coefficients means the interpolation was overdetermined.
pub fn interpolate_point_polynomial(
points_at_indexes: Vec<(Scalar<Public, impl ZeroChoice>, Point)>,
points_at_indicies: Vec<(Scalar<Public, impl ZeroChoice>, Point)>,
) -> Vec<Point<impl PointType, Public, Zero>> {
let (indexes, points): (Vec<_>, Vec<_>) = points_at_indexes.into_iter().unzip();
let (indicies, points): (Vec<_>, Vec<_>) = points_at_indicies.into_iter().unzip();

let basis_polynomials: Vec<_> = unit_basis_polys(indexes.as_slice());
let basis_polynomials: Vec<_> = unit_basis_polys(indicies.as_slice());

let interpolating_basis: Vec<_> = basis_polynomials
.iter()
Expand Down Expand Up @@ -179,17 +181,17 @@ pub fn add<T: PointType + Default, S: Secrecy>(
///
/// Each shamir secret share is associated with a participant index (index, share).
///
/// Panics if the indexes are not unique.
/// Panics if the indicies are not unique.
pub fn reconstruct_shared_secret(
secrets_at_indices: Vec<(Scalar, Scalar<impl Secrecy, impl ZeroChoice>)>,
) -> Scalar {
let (indexes, secrets): (Vec<_>, Vec<_>) = secrets_at_index.into_iter().unzip();
let coefficients: Vec<_> = indexes
let (indicies, secrets): (Vec<_>, Vec<_>) = secrets_at_index.into_iter().unzip();

Check failure on line 188 in secp256kfun/src/poly.rs

View workflow job for this annotation

GitHub Actions / test (x86_64-unknown-linux-gnu)

cannot find value `secrets_at_index` in this scope

Check failure on line 188 in secp256kfun/src/poly.rs

View workflow job for this annotation

GitHub Actions / test (armv7-unknown-linux-gnueabihf)

cannot find value `secrets_at_index` in this scope
let coefficients: Vec<_> = indicies
.iter()
.map(|my_index| {

Check failure on line 191 in secp256kfun/src/poly.rs

View workflow job for this annotation

GitHub Actions / test (x86_64-unknown-linux-gnu)

type annotations needed for `&T`

Check failure on line 191 in secp256kfun/src/poly.rs

View workflow job for this annotation

GitHub Actions / test (armv7-unknown-linux-gnueabihf)

type annotations needed for `&T`
lagrange_lambda(
my_index.clone(),
indexes.clone().into_iter().filter(|j| j != my_index),
indicies.clone().into_iter().filter(|j| j != my_index),
)
})
.collect();
Expand Down Expand Up @@ -221,8 +223,8 @@ mod test {
#[test]
fn test_recover_public_poly() {
let poly = vec![g!(1 * G), g!(2 * G), g!(3 * G)];
let indexes = vec![s!(1).public(), s!(3).public(), s!(2).public()];
let points = indexes
let indicies = vec![s!(1).public(), s!(3).public(), s!(2).public()];
let points = indicies
.clone()
.into_iter()
.map(|index| {
Expand All @@ -243,14 +245,14 @@ mod test {
#[test]
fn test_recover_overdetermined_poly() {
let poly = vec![g!(1 * G), g!(2 * G), g!(3 * G)];
let indexes = vec![
let indicies = vec![
s!(1).public(),
s!(2).public(),
s!(3).public(),
s!(4).public(),
s!(5).public(),
];
let points = indexes
let points = indicies
.clone()
.into_iter()
.map(|index| {
Expand All @@ -267,7 +269,7 @@ mod test {
let interpolation = interpolate_point_polynomial(points);

let (interpolated_coeffs, zero_coeffs) = interpolation.split_at(poly.len());
let n_extra_points = indexes.len() - poly.len();
let n_extra_points = indicies.len() - poly.len();
assert_eq!(
(0..n_extra_points)
.into_iter()
Expand All @@ -281,9 +283,9 @@ mod test {
#[test]
fn test_reconstruct_shared_secret() {
let scalar_poly = vec![s!(42), s!(53), s!(64)];
let indexes = vec![s!(1), s!(2), s!(3)];
let indicies = vec![s!(1), s!(2), s!(3)];

let secret_shares: Vec<_> = indexes
let secret_shares: Vec<_> = indicies
.clone()
.into_iter()
.map(|index| (index, scalar_poly_eval(&scalar_poly, index)))
Expand Down

0 comments on commit ed06d8a

Please sign in to comment.