From 9d51abd843da44a3391721a158638ecf970865d1 Mon Sep 17 00:00:00 2001 From: danielmasny Date: Tue, 20 Feb 2024 12:02:55 -0800 Subject: [PATCH] address comments --- .../ipa_prf/malicious_security/lagrange.rs | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/ipa-core/src/protocol/ipa_prf/malicious_security/lagrange.rs b/ipa-core/src/protocol/ipa_prf/malicious_security/lagrange.rs index 10d79dd25..17254ba96 100644 --- a/ipa-core/src/protocol/ipa_prf/malicious_security/lagrange.rs +++ b/ipa-core/src/protocol/ipa_prf/malicious_security/lagrange.rs @@ -44,7 +44,7 @@ where } *d = d.invert(); } - CanonicalLagrangeDenominator { denominator } + Self { denominator } } } @@ -63,9 +63,9 @@ where { /// generates a `CanonicalLagrangeTable` from `CanoncialLagrangeDenominators` for a single output point /// The "x coordinate" of the output point is `x_output`. - pub fn new(denominator: &CanonicalLagrangeDenominator, x_output: &F) -> Self { - let mut table = denominator.denominator.clone(); - compute_table_row(x_output, &mut table); + pub fn new(denominator: CanonicalLagrangeDenominator, x_output: &F) -> Self { + let mut table = denominator.denominator; + Self::compute_table_row(x_output, &mut table); LagrangeTable:: { table: GenericArray::from_array([table; 1]), } @@ -102,6 +102,22 @@ where .fold(F::ZERO, |acc, (&base, &y)| acc + base * y); } } + + /// helper function to compute a single row of `CanonicalLagrangeTable` + /// + /// ## Panics + /// When the field size is too small for `N` evaluation points + fn compute_table_row(x_output: &F, table_row: &mut GenericArray) + where + F: Field, + N: ArrayLength, + { + for (i, entry) in table_row.iter_mut().enumerate() { + for j in (0usize..N::USIZE).filter(|&j| j != i) { + *entry *= *x_output - F::try_from(j as u128).unwrap(); + } + } + } } impl From> for LagrangeTable @@ -113,34 +129,18 @@ where fn from(value: CanonicalLagrangeDenominator) -> Self { // assertion that field is large enough // also checks that `try_from` for conversions from sufficiently small `u128` to `F` do not panic - debug_assert!(F::BITS > usize::BITS - N::USIZE.leading_zeros() - M::USIZE.leading_zeros()); + debug_assert!(F::BITS > usize::BITS - (N::USIZE + M::USIZE).leading_zeros()); let mut table = iter::repeat(value.denominator.clone()) .take(M::USIZE) .collect::>(); table.iter_mut().enumerate().for_each(|(i, row)| { - compute_table_row(&F::try_from((i + N::USIZE) as u128).unwrap(), row); + Self::compute_table_row(&F::try_from((i + N::USIZE) as u128).unwrap(), row); }); LagrangeTable { table } } } -/// helper function to compute a single row of `CanonicalLagrangeTable` -/// -/// ## Panics -/// When the field size is too small for `N` evaluation points -fn compute_table_row(x_output: &F, table_row: &mut GenericArray) -where - F: Field, - N: ArrayLength, -{ - for (i, entry) in table_row.iter_mut().enumerate() { - for j in (0usize..N::USIZE).filter(|&j| j != i) { - *entry *= *x_output - F::try_from(j as u128).unwrap(); - } - } -} - #[cfg(all(test, unit_test))] mod test { use std::iter; @@ -216,7 +216,7 @@ mod test { let polynomial = Polynomial::from(polynomial_monomial_form.clone()); let denominator = CanonicalLagrangeDenominator::::new(); // generate table using new - let lagrange_table = LagrangeTable::::new(&denominator,&output_point); + let lagrange_table = LagrangeTable::::new(denominator,&output_point); let output = lagrange_table.eval(&polynomial); assert_eq!(output,output_expected); }