Skip to content

Commit

Permalink
MVPoly/monomials: improve generation of random generation
Browse files Browse the repository at this point in the history
  • Loading branch information
dannywillems committed Dec 11, 2024
1 parent eb0c6a9 commit 55acf93
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions mvpoly/src/monomials.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ark_ff::{One, PrimeField, Zero};
use kimchi::circuits::{expr::Variable, gate::CurrOrNext};
use num_integer::binomial;
use rand::RngCore;
use rand::{Rng, RngCore};
use std::{
collections::HashMap,
fmt::Debug,
Expand Down Expand Up @@ -291,9 +291,31 @@ impl<const N: usize, const D: usize, F: PrimeField> MVPoly<F, N, D> for Sparse<F
///
/// For now, the function is only used for testing.
unsafe fn random<RNG: RngCore>(rng: &mut RNG, max_degree: Option<usize>) -> Self {
// IMPROVEME: using prime::Dense::random to ease the implementaiton.
// Feel free to change
prime::Dense::random(rng, max_degree).into()
let degree = max_degree.unwrap_or(D);
// Generating all monomials with degree <= degree^N
let nested_loops_indices: Vec<Vec<usize>> = compute_indices_nested_loop(vec![degree; N]);
// Filtering the monomials with degree <= degree
let exponents: Vec<Vec<usize>> = nested_loops_indices
.into_iter()
.filter(|indices| {
let sum = indices.iter().sum::<usize>();
sum <= degree
})
.collect();
// We add 10% of zeroes.
let exponents: Vec<_> = exponents
.into_iter()
.filter(|_indices| rng.gen_range(0..10) != 0)
.collect();
// Generating random coefficients for the 90%
let monomials: HashMap<[usize; N], F> = exponents
.into_iter()
.map(|indices| {
let coeff = F::rand(rng);
(indices.try_into().unwrap(), coeff)
})
.collect();
Self { monomials }
}

fn from_variable<Column: Into<usize>>(
Expand Down

0 comments on commit 55acf93

Please sign in to comment.