Skip to content

Commit

Permalink
Merge pull request #5835 from oasisprotocol/peternose/trivial/fix-set…
Browse files Browse the repository at this point in the history
…-coefficient

secret-sharing/src/poly: Return bool when setting coefficient
  • Loading branch information
peternose authored Aug 27, 2024
2 parents 18da8bc + bb963c5 commit 0a7f26b
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 14 deletions.
Empty file added .changelog/5835.trivial.md
Empty file.
3 changes: 2 additions & 1 deletion secret-sharing/src/churp/dealer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ where
/// Creates a new dealer with a predefined shared secret.
pub fn new(threshold: u8, secret: G::Scalar, rng: &mut impl RngCore) -> Result<Self> {
let mut bp = Self::random_bivariate_polynomial(threshold, rng)?;
bp.set_coefficient(0, 0, secret);
let updated = bp.set_coefficient(0, 0, secret);
debug_assert!(updated);
Ok(bp.into())
}

Expand Down
11 changes: 7 additions & 4 deletions secret-sharing/src/poly/bivariate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,21 @@ where
/// Sets the coefficient `b_{i,j}` that belongs to the term `x^i y^j`.
///
/// If the coefficient does not exist, this is a no-op.
pub fn set_coefficient(&mut self, i: usize, j: usize, bij: F) {
pub fn set_coefficient(&mut self, i: usize, j: usize, bij: F) -> bool {
if let Some(bi) = self.b.get_mut(i) {
if let Some(old_bij) = bi.get_mut(j) {
*old_bij = bij;
return true;
}
}
false
}

/// Sets the coefficient `b_{0,0}` of the constant term to zero,
/// effectively creating a zero-hole bivariate polynomial.
pub fn to_zero_hole(&mut self) {
self.set_coefficient(0, 0, F::ZERO);
let updated = self.set_coefficient(0, 0, F::ZERO);
debug_assert!(updated);
}

/// Returns true iff the coefficient `b_{0,0}` of the constant term is zero.
Expand Down Expand Up @@ -338,7 +341,7 @@ mod tests {
let mut bp = BivariatePolynomial::zero(2, 3);
assert_eq!(bp.b[0][0], scalar(0));

bp.set_coefficient(0, 0, scalar(1));
assert!(bp.set_coefficient(0, 0, scalar(1)));
assert_eq!(bp.b[0][0], scalar(1));
}

Expand All @@ -347,7 +350,7 @@ mod tests {
let mut rng: StdRng = SeedableRng::from_seed([1u8; 32]);
let mut bp = BivariatePolynomial::random(2, 3, &mut rng);

bp.set_coefficient(0, 0, scalar(1));
assert!(bp.set_coefficient(0, 0, scalar(1)));
assert_eq!(bp.b[0][0], scalar(1));

bp.to_zero_hole();
Expand Down
14 changes: 8 additions & 6 deletions secret-sharing/src/poly/univariate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ use crate::poly::powers;
///
/// Trailing zeros are never trimmed to ensure that all polynomials of the same
/// degree are consistently represented by vectors of the same size, resulting
/// in encodings of equal length. If you wish to remove them, consider using
/// the `trim` method after each operation.
/// in encodings of equal length.
#[derive(Clone, PartialEq, Eq)]
pub struct Polynomial<F> {
pub(crate) a: Vec<F>,
Expand Down Expand Up @@ -68,16 +67,19 @@ where
/// Sets the coefficient `a_i` that belongs to the term `x^i`.
///
/// If the coefficient does not exist, this is a no-op.
pub fn set_coefficient(&mut self, i: usize, ai: F) {
pub fn set_coefficient(&mut self, i: usize, ai: F) -> bool {
if let Some(old_ai) = self.a.get_mut(i) {
*old_ai = ai;
return true;
}
false
}

/// Sets the coefficient `a_0` of the constant term to zero,
/// effectively creating a zero-hole univariate polynomial.
pub fn to_zero_hole(&mut self) {
self.set_coefficient(0, F::ZERO);
let updated = self.set_coefficient(0, F::ZERO);
debug_assert!(updated)
}

/// Returns true iff the coefficient `a_0` of the constant term is zero.
Expand Down Expand Up @@ -484,10 +486,10 @@ mod tests {
fn test_set_coefficients() {
let mut p = Polynomial::with_coefficients(scalars(&[1, 2, 3]));

p.set_coefficient(3, scalar(4));
assert!(!p.set_coefficient(3, scalar(4)));
assert_eq!(p.a, scalars(&[1, 2, 3]));

p.set_coefficient(1, scalar(4));
assert!(p.set_coefficient(1, scalar(4)));
assert_eq!(p.a, scalars(&[1, 4, 3]));
}

Expand Down
3 changes: 2 additions & 1 deletion secret-sharing/src/shamir/dealer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ where
/// Creates a new dealer with a predefined shared secret.
pub fn new(threshold: u8, secret: F, rng: &mut impl RngCore) -> Self {
let mut sharer = Self::random(threshold, rng);
sharer.poly.set_coefficient(0, secret);
let updated = sharer.poly.set_coefficient(0, secret);
debug_assert!(updated);
sharer
}

Expand Down
4 changes: 2 additions & 2 deletions secret-sharing/src/vss/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ mod tests {
// Two non-zero coefficients (fast).
let mut rng: StdRng = SeedableRng::from_seed([1u8; 32]);
let mut bp = BivariatePolynomial::<p384::Scalar>::zero(2, 3);
bp.set_coefficient(0, 0, p384::Scalar::ONE);
bp.set_coefficient(2, 2, p384::Scalar::ONE.double());
assert!(bp.set_coefficient(0, 0, p384::Scalar::ONE));
assert!(bp.set_coefficient(2, 2, p384::Scalar::ONE.double()));

let vm = VerificationMatrix::<p384::ProjectivePoint>::from(&bp);
assert_eq!(vm.m.len(), 3);
Expand Down

0 comments on commit 0a7f26b

Please sign in to comment.