Skip to content

Commit

Permalink
MVPoly: add a from to increase the number of variables
Browse files Browse the repository at this point in the history
  • Loading branch information
dannywillems committed Sep 9, 2024
1 parent 1a72f3f commit f9d94c2
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
17 changes: 17 additions & 0 deletions mvpoly/src/monomials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,3 +603,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 @@ -498,3 +498,64 @@ fn test_mvpoly_compute_cross_terms_degree_seven() {
};
assert_eq!(lhs, rhs);
}

#[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);
}
}

0 comments on commit f9d94c2

Please sign in to comment.