From 55acf93dfe75aa9676aa8d72c6a0330d8269ce13 Mon Sep 17 00:00:00 2001 From: Danny Willems Date: Wed, 16 Oct 2024 17:13:12 +0200 Subject: [PATCH] MVPoly/monomials: improve generation of random generation --- mvpoly/src/monomials.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/mvpoly/src/monomials.rs b/mvpoly/src/monomials.rs index 8fb495ef02..3a11fe4ef5 100644 --- a/mvpoly/src/monomials.rs +++ b/mvpoly/src/monomials.rs @@ -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, @@ -291,9 +291,31 @@ impl MVPoly for Sparse(rng: &mut RNG, max_degree: Option) -> 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> = compute_indices_nested_loop(vec![degree; N]); + // Filtering the monomials with degree <= degree + let exponents: Vec> = nested_loops_indices + .into_iter() + .filter(|indices| { + let sum = indices.iter().sum::(); + 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>(