Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removing one more iter_mut in the denominators #977

Merged
merged 5 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"] }
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Required to unlock the N::U128 constants

# hpke is pinned to it
x25519-dalek = "2.0.0-rc.3"

Expand Down
76 changes: 44 additions & 32 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,23 +36,28 @@
// assertion that field is large enough
// when it is large enough, `F::try_from().unwrap()` below does not panic
assert!(
F::BITS > usize::BITS - N::USIZE.leading_zeros(),
N::U128 < F::PRIME.into(),
"Field size {} is not large enough to hold {} points",
F::BITS,
N::USIZE
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::U128
);

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

let mut denominator = GenericArray::generate(|_| F::ONE);
for (d, i) in denominator.iter_mut().zip(0u64..) {
for j in (0..N::U64).filter(|&j| i != j) {
*d *= F::try_from(u128::from(i)).unwrap() - F::try_from(u128::from(j)).unwrap();
}
*d = d.invert();
Self {
denominator: (0..N::U128)
.into_iter()
.map(|i| {
(0..N::U128)
.into_iter()
.filter(|&j| i != j)
.map(|j| F::try_from(i).unwrap() - F::try_from(j).unwrap())
.fold(F::ONE, |acc, a| acc * a)
.invert()
})
.collect(),
}
Self { denominator }
}
}

Expand Down Expand Up @@ -79,12 +85,11 @@
{
/// 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 @@ -115,46 +120,53 @@
///
/// ## 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()
}
}

impl<F, N, M> From<CanonicalLagrangeDenominator<F, N>> for LagrangeTable<F, N, M>
where
F: Field + TryFrom<u128>,
<F as TryFrom<u128>>::Error: Debug,
F: PrimeField,
N: ArrayLength,
M: ArrayLength,
{
fn from(value: CanonicalLagrangeDenominator<F, N>) -> Self {
// assertion that field is large enough
// when it is large enough, `F::try_from().unwrap()` below does not panic
assert!(
F::BITS > usize::BITS - (N::USIZE + M::USIZE).leading_zeros(),
N::U128 + M::U128 < F::PRIME.into(),
"Field size {} is not large enough to hold {} + {} points",
F::BITS,
N::USIZE,
M::USIZE
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::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 @@ -236,7 +248,7 @@
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
Loading