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

MVPoly: add a from to increase the number of variables #2552

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions mvpoly/src/monomials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,3 +609,20 @@ impl<F: PrimeField, const N: usize, const D: usize> From<F> for Sparse<F, N, D>
result
}
}

impl<F: PrimeField, const N: usize, const D: usize, const M: usize> From<Sparse<F, N, D>>
for Result<Sparse<F, M, D>, String>
{
fn from(poly: Sparse<F, N, D>) -> Result<Sparse<F, M, D>, String> {
if M < N {
return Err("The number of variables must be greater than N".to_string());
}
let mut monomials = HashMap::new();
poly.monomials.iter().for_each(|(exponents, coeff)| {
let mut new_exponents = [0; M];
new_exponents[0..N].copy_from_slice(&exponents[0..N]);
monomials.insert(new_exponents, *coeff);
});
Ok(Sparse { monomials })
}
}
61 changes: 61 additions & 0 deletions mvpoly/tests/monomials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,64 @@ fn test_is_multilinear() {
let p3 = unsafe { Sparse::<Fp, 6, 4>::random(&mut rng, None) };
assert!(!p3.is_multilinear());
}

#[test]
fn test_increase_number_of_variables() {
let mut rng = o1_utils::tests::make_test_rng(None);
let p1: Sparse<Fp, 4, 2> = unsafe { Sparse::<Fp, 4, 2>::random(&mut rng, None) };

let p2: Result<Sparse<Fp, 5, 2>, String> = p1.into();
p2.unwrap();
}

#[test]
fn test_pbt_increase_number_of_variables_with_addition() {
let mut rng = o1_utils::tests::make_test_rng(None);
let p1: Sparse<Fp, 4, 2> = unsafe { Sparse::<Fp, 4, 2>::random(&mut rng, None) };
let p2: Sparse<Fp, 4, 2> = unsafe { Sparse::<Fp, 4, 2>::random(&mut rng, None) };

let lhs: Sparse<Fp, 5, 2> = {
let p: Result<Sparse<Fp, 5, 2>, String> = (p1.clone() + p2.clone()).into();
p.unwrap()
};

let rhs: Sparse<Fp, 5, 2> = {
let p1: Result<Sparse<Fp, 5, 2>, String> = p1.clone().into();
let p2: Result<Sparse<Fp, 5, 2>, String> = p2.clone().into();
p1.unwrap() + p2.unwrap()
};

assert_eq!(lhs, rhs);
}

#[test]
fn test_pbt_increase_number_of_variables_zero_one_cst() {
let mut rng = o1_utils::tests::make_test_rng(None);
{
let lhs_zero: Sparse<Fp, 5, 2> = {
let p: Result<Sparse<Fp, 5, 2>, String> = Sparse::<Fp, 4, 2>::zero().into();
p.unwrap()
};
let rhs_zero: Sparse<Fp, 5, 2> = Sparse::<Fp, 5, 2>::zero();
assert_eq!(lhs_zero, rhs_zero);
}

{
let lhs_one: Sparse<Fp, 5, 2> = {
let p: Result<Sparse<Fp, 5, 2>, String> = Sparse::<Fp, 4, 2>::one().into();
p.unwrap()
};
let rhs_one: Sparse<Fp, 5, 2> = Sparse::<Fp, 5, 2>::one();
assert_eq!(lhs_one, rhs_one);
}

{
let c = Fp::rand(&mut rng);
let lhs: Sparse<Fp, 5, 2> = {
let p: Result<Sparse<Fp, 5, 2>, String> = Sparse::<Fp, 4, 2>::from(c).into();
p.unwrap()
};
let rhs: Sparse<Fp, 5, 2> = Sparse::<Fp, 5, 2>::from(c);
assert_eq!(lhs, rhs);
}
}