Skip to content

Commit

Permalink
Removing the last iter_mut
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminsavage committed Mar 16, 2024
1 parent 98c0543 commit 65066c4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
2 changes: 1 addition & 1 deletion ipa-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ tower = { version = "0.4.13", optional = true }
tower-http = { version = "0.4.0", optional = true, features = ["trace"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
typenum = "1.16"
typenum = { version = "1.17", features = ["i128"] }
# hpke is pinned to it
x25519-dalek = "2.0.0-rc.3"

Expand Down
54 changes: 31 additions & 23 deletions ipa-core/src/protocol/ipa_prf/malicious_security/lagrange.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt::Debug;
use std::iter::repeat;

use generic_array::{sequence::GenericSequence, ArrayLength, GenericArray};
use generic_array::{ArrayLength, GenericArray};
use typenum::{Unsigned, U1};

use crate::ff::{Field, PrimeField, Serializable};
Expand Down Expand Up @@ -35,20 +36,20 @@ where
// assertion that field is large enough
// when it is large enough, `F::try_from().unwrap()` below does not panic
assert!(
u128::from(N::U64) < F::PRIME.into(),
N::U128 < F::PRIME.into(),
"Field size {} is not large enough to hold {} points",
F::PRIME.into(),

Check warning on line 41 in ipa-core/src/protocol/ipa_prf/malicious_security/lagrange.rs

View check run for this annotation

Codecov / codecov/patch

ipa-core/src/protocol/ipa_prf/malicious_security/lagrange.rs#L41

Added line #L41 was not covered by tests
N::U64
N::U128
);

// assertion that table is not too large for the stack
assert!(<F as Serializable>::Size::USIZE * N::USIZE < 2024);

Self {
denominator: (0..u128::from(N::U64))
denominator: (0..N::U128)
.into_iter()
.map(|i| {
(0..u128::from(N::U64))
(0..N::U128)
.into_iter()
.filter(|&j| i != j)
.map(|j| F::try_from(i).unwrap() - F::try_from(j).unwrap())
Expand Down Expand Up @@ -84,12 +85,11 @@ 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<F, N>, x_output: &F) -> Self {
pub fn new(denominator: &CanonicalLagrangeDenominator<F, N>, x_output: &F) -> Self {
// assertion that table is not too large for the stack
assert!(<F as Serializable>::Size::USIZE * N::USIZE < 2024);

let mut table = denominator.denominator;
Self::compute_table_row(x_output, &mut table);
let table = Self::compute_table_row(x_output, denominator);
LagrangeTable::<F, N, U1> {
table: GenericArray::from_array([table; 1]),
}
Expand Down Expand Up @@ -120,17 +120,25 @@ where
///
/// ## Panics
/// When the field size is too small for `N` evaluation points
fn compute_table_row(x_output: &F, table_row: &mut GenericArray<F, N>)
fn compute_table_row(
x_output: &F,
denominator: &CanonicalLagrangeDenominator<F, N>,
) -> GenericArray<F, N>
where
F: Field + TryFrom<u128>,
<F as TryFrom<u128>>::Error: Debug,
N: ArrayLength,
{
for (entry, i) in table_row.iter_mut().zip(0u64..) {
for j in (0..N::U64).filter(|&j| j != i) {
*entry *= *x_output - F::try_from(u128::from(j)).unwrap();
}
}
(0..N::U128)
.zip(repeat(0..N::U128))
.map(|(i, range)| {
range
.filter(|&j| j != i)
.fold(F::ONE, |acc, j| acc * (*x_output - F::try_from(j).unwrap()))
})
.zip(&denominator.denominator)
.map(|(numerator, denominator)| *denominator * numerator)
.collect()
}
}

Expand All @@ -144,21 +152,21 @@ where
// assertion that field is large enough
// when it is large enough, `F::try_from().unwrap()` below does not panic
assert!(
u128::from(N::U64 + M::U64) < F::PRIME.into(),
N::U128 + M::U128 < F::PRIME.into(),
"Field size {} is not large enough to hold {} + {} points",
F::PRIME.into(),

Check warning on line 157 in ipa-core/src/protocol/ipa_prf/malicious_security/lagrange.rs

View check run for this annotation

Codecov / codecov/patch

ipa-core/src/protocol/ipa_prf/malicious_security/lagrange.rs#L157

Added line #L157 was not covered by tests
N::U64,
M::U64
N::U128,
M::U128
);

// assertion that table is not too large for the stack
assert!(<F as Serializable>::Size::USIZE * N::USIZE * M::USIZE < 2024);

let mut table = GenericArray::generate(|_| value.denominator.clone());
table.iter_mut().zip(0u64..).for_each(|(row, i)| {
Self::compute_table_row(&F::try_from(u128::from(i + N::U64)).unwrap(), row);
});
LagrangeTable { table }
LagrangeTable {
table: (N::U128..(N::U128 + M::U128))
.map(|i| Self::compute_table_row(&F::try_from(i).unwrap(), &value))
.collect(),
}
}
}

Expand Down Expand Up @@ -240,7 +248,7 @@ mod test {
let polynomial = Polynomial::from(polynomial_monomial_form.clone());
let denominator = CanonicalLagrangeDenominator::<TestField, U32>::new();
// generate table using new
let lagrange_table = LagrangeTable::<TestField, U32, U1>::new(denominator, &output_point);
let lagrange_table = LagrangeTable::<TestField, U32, U1>::new(&denominator, &output_point);
let output = lagrange_table.eval(&polynomial);
assert_eq!(output, output_expected);
}
Expand Down

0 comments on commit 65066c4

Please sign in to comment.