Skip to content

Commit

Permalink
Merge pull request #2506 from o1-labs/dw/mvpoly-random-add-max-degree
Browse files Browse the repository at this point in the history
MVPoly: add max_degree optional parameter into random
  • Loading branch information
dannywillems authored Aug 29, 2024
2 parents 9c0474d + 293d74e commit fe46dd2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 33 deletions.
18 changes: 9 additions & 9 deletions mvpoly/benches/prime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use mvpoly::prime::Dense;
// Should roughly cover the cases we care about
fn bench_dense_add(c: &mut Criterion) {
let mut rng = o1_utils::tests::make_test_rng(None);
let p1: Dense<Fp, 10, 3> = unsafe { Dense::random(&mut rng) };
let p2: Dense<Fp, 10, 3> = unsafe { Dense::random(&mut rng) };
let p1: Dense<Fp, 10, 3> = unsafe { Dense::random(&mut rng, None) };
let p2: Dense<Fp, 10, 3> = unsafe { Dense::random(&mut rng, None) };
c.bench_function("dense_add", |b: &mut Bencher| {
b.iter(|| {
let _ = black_box(&p1) + black_box(&p2);
Expand All @@ -20,18 +20,18 @@ fn bench_dense_mul(c: &mut Criterion) {
let mut rng = o1_utils::tests::make_test_rng(None);
c.bench_function("dense_mul", |b: &mut Bencher| {
b.iter(|| {
// IMPROVEME: impleemnt mul on references and define the random
// IMPROVEME: implement mul on references and define the random
// values before the benchmark
let p1: Dense<Fp, 10, 3> = unsafe { Dense::random(&mut rng) };
let p2: Dense<Fp, 10, 3> = unsafe { Dense::random(&mut rng) };
let p1: Dense<Fp, 10, 3> = unsafe { Dense::random(&mut rng, None) };
let p2: Dense<Fp, 10, 3> = unsafe { Dense::random(&mut rng, None) };
let _ = black_box(p1) * black_box(p2);
})
});
}

fn bench_dense_neg(c: &mut Criterion) {
let mut rng = o1_utils::tests::make_test_rng(None);
let p1: Dense<Fp, 10, 3> = unsafe { Dense::random(&mut rng) };
let p1: Dense<Fp, 10, 3> = unsafe { Dense::random(&mut rng, None) };
c.bench_function("dense_neg", |b: &mut Bencher| {
b.iter(|| {
let _ = -black_box(&p1);
Expand All @@ -41,8 +41,8 @@ fn bench_dense_neg(c: &mut Criterion) {

fn bench_dense_sub(c: &mut Criterion) {
let mut rng = o1_utils::tests::make_test_rng(None);
let p1: Dense<Fp, 10, 3> = unsafe { Dense::random(&mut rng) };
let p2: Dense<Fp, 10, 3> = unsafe { Dense::random(&mut rng) };
let p1: Dense<Fp, 10, 3> = unsafe { Dense::random(&mut rng, None) };
let p2: Dense<Fp, 10, 3> = unsafe { Dense::random(&mut rng, None) };
c.bench_function("dense_sub", |b: &mut Bencher| {
b.iter(|| {
let _ = black_box(&p1) - black_box(&p2);
Expand All @@ -52,7 +52,7 @@ fn bench_dense_sub(c: &mut Criterion) {

fn bench_dense_eval(c: &mut Criterion) {
let mut rng = o1_utils::tests::make_test_rng(None);
let p1: Dense<Fp, 10, 3> = unsafe { Dense::random(&mut rng) };
let p1: Dense<Fp, 10, 3> = unsafe { Dense::random(&mut rng, None) };
let x: [Fp; 10] = std::array::from_fn(|_| Fp::rand(&mut rng));
c.bench_function("dense_eval", |b: &mut Bencher| {
b.iter(|| {
Expand Down
32 changes: 26 additions & 6 deletions mvpoly/src/prime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,10 @@ impl<F: PrimeField, const N: usize, const D: usize> Dense<F, N, D> {
}
}

/// Generate a random polynomial
/// Generate a random polynomial of maximum degree `max_degree`.
///
/// If `None` is provided as the maximum degree, the polynomial will be
/// generated with a maximum degree of `D`.
///
/// # Safety
///
Expand All @@ -234,12 +237,29 @@ impl<F: PrimeField, const N: usize, const D: usize> Dense<F, N, D> {
/// polynomial random generator, if needed.
///
/// For now, the function is only used for testing.
pub unsafe fn random<RNG: RngCore>(rng: &mut RNG) -> Self {
pub unsafe fn random<RNG: RngCore>(rng: &mut RNG, max_degree: Option<usize>) -> Self {
let mut prime_gen = PrimeNumberGenerator::new();
let normalized_indices = Self::compute_normalized_indices();
let coeff = normalized_indices
.iter()
.map(|_| F::rand(rng))
.collect::<Vec<F>>();
// Different cases to avoid complexity in the case no maximum degree is
// provided
let coeff = if let Some(max_degree) = max_degree {
normalized_indices
.iter()
.map(|idx| {
let degree = naive_prime_factors(*idx, &mut prime_gen)
.iter()
.fold(0, |acc, (_, d)| acc + d);
if degree > max_degree {
F::zero()
} else {
F::rand(rng)
}
})
.collect::<Vec<F>>()
} else {
normalized_indices.iter().map(|_| F::rand(rng)).collect()
};
Dense {
coeff,
normalized_indices,
Expand Down
36 changes: 18 additions & 18 deletions mvpoly/tests/prime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ fn test_mul() {
#[test]
fn test_mul_by_one() {
let mut rng = o1_utils::tests::make_test_rng(None);
let p1 = unsafe { Dense::<Fp, 7, 2>::random(&mut rng) };
let p1 = unsafe { Dense::<Fp, 7, 2>::random(&mut rng, None) };
let one = Dense::<Fp, 7, 2>::one();
let p2 = p1.clone() * one.clone();
assert_eq!(p1.clone(), p2);
Expand All @@ -170,7 +170,7 @@ fn test_mul_by_one() {
#[test]
fn test_mul_by_zero() {
let mut rng = o1_utils::tests::make_test_rng(None);
let p1 = unsafe { Dense::<Fp, 5, 4>::random(&mut rng) };
let p1 = unsafe { Dense::<Fp, 5, 4>::random(&mut rng, None) };
let zero = Dense::<Fp, 5, 4>::zero();
let p2 = p1.clone() * zero.clone();
assert_eq!(zero, p2);
Expand All @@ -181,7 +181,7 @@ fn test_mul_by_zero() {
#[test]
fn test_add_zero() {
let mut rng = o1_utils::tests::make_test_rng(None);
let p1 = unsafe { Dense::<Fp, 3, 4>::random(&mut rng) };
let p1 = unsafe { Dense::<Fp, 3, 4>::random(&mut rng, None) };

let zero = Dense::<Fp, 3, 4>::zero();
let p2 = p1.clone() + zero.clone();
Expand All @@ -193,7 +193,7 @@ fn test_add_zero() {
#[test]
fn test_double_is_add_twice() {
let mut rng = o1_utils::tests::make_test_rng(None);
let p1 = unsafe { Dense::<Fp, 3, 4>::random(&mut rng) };
let p1 = unsafe { Dense::<Fp, 3, 4>::random(&mut rng, None) };
let p2 = p1.clone() + p1.clone();
let p3 = p1.clone().double();
assert_eq!(p2, p3);
Expand All @@ -202,7 +202,7 @@ fn test_double_is_add_twice() {
#[test]
fn test_sub_zero() {
let mut rng = o1_utils::tests::make_test_rng(None);
let p1 = unsafe { Dense::<Fp, 3, 4>::random(&mut rng) };
let p1 = unsafe { Dense::<Fp, 3, 4>::random(&mut rng, None) };
let zero = Dense::<Fp, 3, 4>::zero();
let p2 = p1.clone() - zero.clone();
assert_eq!(p1.clone(), p2);
Expand All @@ -211,7 +211,7 @@ fn test_sub_zero() {
#[test]
fn test_neg() {
let mut rng = o1_utils::tests::make_test_rng(None);
let p1 = unsafe { Dense::<Fp, 3, 4>::random(&mut rng) };
let p1 = unsafe { Dense::<Fp, 3, 4>::random(&mut rng, None) };
let p2 = -p1.clone();

// Test that p1 + (-p1) = 0
Expand All @@ -231,7 +231,7 @@ fn test_neg() {
#[test]
fn test_neg_ref() {
let mut rng = o1_utils::tests::make_test_rng(None);
let p1 = unsafe { Dense::<Fp, 3, 4>::random(&mut rng) };
let p1 = unsafe { Dense::<Fp, 3, 4>::random(&mut rng, None) };
let p2 = -&p1;

// Test that p1 + (-&p1) = 0
Expand All @@ -246,7 +246,7 @@ fn test_neg_ref() {
#[test]
fn test_mul_by_scalar() {
let mut rng = o1_utils::tests::make_test_rng(None);
let p1 = unsafe { Dense::<Fp, 4, 5>::random(&mut rng) };
let p1 = unsafe { Dense::<Fp, 4, 5>::random(&mut rng, None) };
let mut p2 = Dense::<Fp, 4, 5>::zero();
let c = Fp::rand(&mut rng);
p2[0] = c;
Expand All @@ -256,23 +256,23 @@ fn test_mul_by_scalar() {
#[test]
fn test_mul_by_scalar_with_zero() {
let mut rng = o1_utils::tests::make_test_rng(None);
let p1 = unsafe { Dense::<Fp, 4, 5>::random(&mut rng) };
let p1 = unsafe { Dense::<Fp, 4, 5>::random(&mut rng, None) };
let c = Fp::zero();
assert_eq!(p1.mul_by_scalar(c), Dense::<Fp, 4, 5>::zero())
}

#[test]
fn test_mul_by_scalar_with_one() {
let mut rng = o1_utils::tests::make_test_rng(None);
let p1 = unsafe { Dense::<Fp, 4, 5>::random(&mut rng) };
let p1 = unsafe { Dense::<Fp, 4, 5>::random(&mut rng, None) };
let c = Fp::one();
assert_eq!(p1.mul_by_scalar(c), p1)
}

#[test]
fn test_mul_by_scalar_with_from() {
let mut rng = o1_utils::tests::make_test_rng(None);
let p = unsafe { Dense::<Fp, 4, 5>::random(&mut rng) };
let p = unsafe { Dense::<Fp, 4, 5>::random(&mut rng, None) };
let c = Fp::rand(&mut rng);

// Create a constant polynomial from the field element
Expand Down Expand Up @@ -410,8 +410,8 @@ fn test_eval_pbt_add() {
let mut rng = o1_utils::tests::make_test_rng(None);

let random_evaluation: [Fp; 6] = std::array::from_fn(|_| Fp::rand(&mut rng));
let p1 = unsafe { Dense::<Fp, 6, 4>::random(&mut rng) };
let p2 = unsafe { Dense::<Fp, 6, 4>::random(&mut rng) };
let p1 = unsafe { Dense::<Fp, 6, 4>::random(&mut rng, None) };
let p2 = unsafe { Dense::<Fp, 6, 4>::random(&mut rng, None) };
let p3 = p1.clone() + p2.clone();
let eval_p1 = p1.eval(&random_evaluation);
let eval_p2 = p2.eval(&random_evaluation);
Expand All @@ -424,8 +424,8 @@ fn test_eval_pbt_sub() {
let mut rng = o1_utils::tests::make_test_rng(None);

let random_evaluation: [Fp; 6] = std::array::from_fn(|_| Fp::rand(&mut rng));
let p1 = unsafe { Dense::<Fp, 6, 4>::random(&mut rng) };
let p2 = unsafe { Dense::<Fp, 6, 4>::random(&mut rng) };
let p1 = unsafe { Dense::<Fp, 6, 4>::random(&mut rng, None) };
let p2 = unsafe { Dense::<Fp, 6, 4>::random(&mut rng, None) };
let p3 = p1.clone() - p2.clone();
let eval_p1 = p1.eval(&random_evaluation);
let eval_p2 = p2.eval(&random_evaluation);
Expand All @@ -438,7 +438,7 @@ fn test_eval_pbt_mul_by_scalar() {
let mut rng = o1_utils::tests::make_test_rng(None);

let random_evaluation: [Fp; 6] = std::array::from_fn(|_| Fp::rand(&mut rng));
let p1 = unsafe { Dense::<Fp, 6, 4>::random(&mut rng) };
let p1 = unsafe { Dense::<Fp, 6, 4>::random(&mut rng, None) };
let c = Fp::rand(&mut rng);
let p2 = p1.clone() * Dense::<Fp, 6, 4>::from(c);
let eval_p1 = p1.eval(&random_evaluation);
Expand All @@ -451,7 +451,7 @@ fn test_eval_pbt_neg() {
let mut rng = o1_utils::tests::make_test_rng(None);

let random_evaluation: [Fp; 6] = std::array::from_fn(|_| Fp::rand(&mut rng));
let p1 = unsafe { Dense::<Fp, 6, 4>::random(&mut rng) };
let p1 = unsafe { Dense::<Fp, 6, 4>::random(&mut rng, None) };
let p2 = -p1.clone();
let eval_p1 = p1.eval(&random_evaluation);
let eval_p2 = p2.eval(&random_evaluation);
Expand Down Expand Up @@ -607,7 +607,7 @@ fn test_from_expr_ec_addition() {
assert_eq!(eval, exp_eval);
}
{
// - Constraint 3: Y3 - λ (X1 - X3) - Y1 = 0
// - Constraint 3: Y3 - λ (X1 - X3) + Y1 = 0
let expr = y3.clone() - lambda.clone() * (x1.clone() - x3.clone()) + y1.clone();
let p = Dense::<Fp, 7, 2>::from(expr);
let random_evaluation: [Fp; 7] = std::array::from_fn(|_| Fp::rand(&mut rng));
Expand Down

0 comments on commit fe46dd2

Please sign in to comment.