From 412e2428f9efa8f89d92f89e881ea7397486b13b Mon Sep 17 00:00:00 2001 From: Cesar199999 Date: Mon, 17 Jun 2024 17:15:08 +0200 Subject: [PATCH 1/6] Use PRNG on `get_indices_from_sponge` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Mejías Gil --- poly-commit/Cargo.toml | 2 +- poly-commit/src/linear_codes/mod.rs | 2 ++ poly-commit/src/linear_codes/utils.rs | 38 ++++++++++++++------------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/poly-commit/Cargo.toml b/poly-commit/Cargo.toml index 49b48083..eb589434 100644 --- a/poly-commit/Cargo.toml +++ b/poly-commit/Cargo.toml @@ -20,6 +20,7 @@ rand = { version = "0.8.0", optional = true } ark-relations = { version = "^0.4.0", default-features = false, optional = true } ark-r1cs-std = { version = "^0.4.0", default-features = false, optional = true } hashbrown = { version = "0.14", default-features = false, optional = true } +rand_chacha = { version = "0.3.0", default-features = false } digest = "0.10" derivative = { version = "2", features = [ "use_core" ] } @@ -56,7 +57,6 @@ ark-bls12-381 = { version = "^0.4.0", default-features = false, features = [ "cu ark-bls12-377 = { version = "^0.4.0", default-features = false, features = [ "curve" ] } ark-bn254 = { version = "^0.4.0", default-features = false, features = [ "curve" ] } -rand_chacha = { version = "0.3.0", default-features = false } ark-pcs-bench-templates = { path = "../bench-templates" } [target.'cfg(target_arch = "aarch64")'.dependencies] diff --git a/poly-commit/src/linear_codes/mod.rs b/poly-commit/src/linear_codes/mod.rs index 91181311..ddb514f5 100644 --- a/poly-commit/src/linear_codes/mod.rs +++ b/poly-commit/src/linear_codes/mod.rs @@ -44,6 +44,8 @@ use utils::{calculate_t, get_indices_from_sponge}; const FIELD_SIZE_ERROR: &str = "This field is not suitable for the proposed parameters"; +const CHACHA_SEED_BYTES: usize = 256 / 8; + /// For linear code PC schemes, the universal parameters, committer key /// and verifier key are all the same. This trait abstracts the common /// information contained in these. diff --git a/poly-commit/src/linear_codes/utils.rs b/poly-commit/src/linear_codes/utils.rs index d4cacc4f..3e7ea732 100644 --- a/poly-commit/src/linear_codes/utils.rs +++ b/poly-commit/src/linear_codes/utils.rs @@ -1,3 +1,6 @@ +use core::convert::TryInto; +use std::collections::HashSet; + use crate::{utils::ceil_div, Error}; use ark_crypto_primitives::sponge::CryptographicSponge; use ark_ff::{FftField, Field, PrimeField}; @@ -8,6 +11,10 @@ use ark_std::vec::Vec; #[cfg(all(not(feature = "std"), target_arch = "aarch64"))] use num_traits::Float; +use rand::{Rng, SeedableRng}; +use rand_chacha::ChaChaRng; + +use super::CHACHA_SEED_BYTES; #[cfg(test)] use { @@ -126,30 +133,25 @@ impl SprsMat { } } -#[inline] -pub(crate) fn get_num_bytes(n: usize) -> usize { - ceil_div((usize::BITS - n.leading_zeros()) as usize, 8) -} - /// Generate `t` (not necessarily distinct) random points in `[0, n)` -/// using the current state of the `transcript`. +/// using the current state of the `transcript`. Duplicates are removed (leading +/// to possibly fewer than `t` points being returned). pub(crate) fn get_indices_from_sponge( n: usize, t: usize, sponge: &mut S, ) -> Result, Error> { - let bytes_to_squeeze = get_num_bytes(n); - let mut indices = Vec::with_capacity(t); - for _ in 0..t { - let bytes = sponge.squeeze_bytes(bytes_to_squeeze); - sponge.absorb(&bytes); - - // get the usize from Vec: - let ind = bytes.iter().fold(0, |acc, &x| (acc << 8) + x as usize); - // modulo the number of columns in the encoded matrix - indices.push(ind % n); - } - Ok(indices) + // Squeeze 256 bits from the sponge and use them to seed a ChaCha20 PRNG + let seed = sponge.squeeze_bytes(CHACHA_SEED_BYTES); + let mut rng = ChaChaRng::from_seed(seed.try_into().unwrap()); + + // Squeeze t elements, then removing duplicates. Crucially, this must be + // done deterministically to ensure prover-verifier consistency. + let mut seen = HashSet::new(); + Ok((0..t) + .map(|_| rng.gen_range(0..n)) + .filter(|x| seen.insert(*x)) + .collect()) } #[inline] From 41643de0aeb811cb8b891d8c966c15e72d903ab1 Mon Sep 17 00:00:00 2001 From: Cesar199999 Date: Mon, 17 Jun 2024 17:20:25 +0200 Subject: [PATCH 2/6] Remove unused imports --- poly-commit/src/linear_codes/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/poly-commit/src/linear_codes/utils.rs b/poly-commit/src/linear_codes/utils.rs index 3e7ea732..a73abb56 100644 --- a/poly-commit/src/linear_codes/utils.rs +++ b/poly-commit/src/linear_codes/utils.rs @@ -1,7 +1,7 @@ use core::convert::TryInto; use std::collections::HashSet; -use crate::{utils::ceil_div, Error}; +use crate::Error; use ark_crypto_primitives::sponge::CryptographicSponge; use ark_ff::{FftField, Field, PrimeField}; use ark_poly::{EvaluationDomain, GeneralEvaluationDomain}; From bc38727198473c179a73baadf9839bb00f25834d Mon Sep 17 00:00:00 2001 From: Cesar199999 Date: Mon, 17 Jun 2024 17:28:19 +0200 Subject: [PATCH 3/6] Fix missing function --- poly-commit/src/linear_codes/utils.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/poly-commit/src/linear_codes/utils.rs b/poly-commit/src/linear_codes/utils.rs index a73abb56..aac29e25 100644 --- a/poly-commit/src/linear_codes/utils.rs +++ b/poly-commit/src/linear_codes/utils.rs @@ -1,6 +1,8 @@ use core::convert::TryInto; use std::collections::HashSet; +#[cfg(test)] +use crate::utils::ceil_div; use crate::Error; use ark_crypto_primitives::sponge::CryptographicSponge; use ark_ff::{FftField, Field, PrimeField}; @@ -133,6 +135,12 @@ impl SprsMat { } } +#[inline] +#[cfg(test)] +pub(crate) fn get_num_bytes(n: usize) -> usize { + ceil_div((usize::BITS - n.leading_zeros()) as usize, 8) +} + /// Generate `t` (not necessarily distinct) random points in `[0, n)` /// using the current state of the `transcript`. Duplicates are removed (leading /// to possibly fewer than `t` points being returned). From b25795f73f1db7200349d2df7da807c0affbcc6c Mon Sep 17 00:00:00 2001 From: Cesar199999 Date: Mon, 17 Jun 2024 17:44:58 +0200 Subject: [PATCH 4/6] Use `BTreeSet`. Rearrange imports --- poly-commit/src/linear_codes/utils.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/poly-commit/src/linear_codes/utils.rs b/poly-commit/src/linear_codes/utils.rs index aac29e25..40eee6ea 100644 --- a/poly-commit/src/linear_codes/utils.rs +++ b/poly-commit/src/linear_codes/utils.rs @@ -1,5 +1,4 @@ use core::convert::TryInto; -use std::collections::HashSet; #[cfg(test)] use crate::utils::ceil_div; @@ -8,8 +7,11 @@ use ark_crypto_primitives::sponge::CryptographicSponge; use ark_ff::{FftField, Field, PrimeField}; use ark_poly::{EvaluationDomain, GeneralEvaluationDomain}; use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; -use ark_std::string::ToString; -use ark_std::vec::Vec; +use ark_std::{ + string::ToString, + vec::Vec, + collections::BTreeSet, +}; #[cfg(all(not(feature = "std"), target_arch = "aarch64"))] use num_traits::Float; @@ -155,7 +157,7 @@ pub(crate) fn get_indices_from_sponge( // Squeeze t elements, then removing duplicates. Crucially, this must be // done deterministically to ensure prover-verifier consistency. - let mut seen = HashSet::new(); + let mut seen = BTreeSet::new(); Ok((0..t) .map(|_| rng.gen_range(0..n)) .filter(|x| seen.insert(*x)) From aba26d2a3121f3788aaf5148e2ab44fefb8ae567 Mon Sep 17 00:00:00 2001 From: Cesar199999 Date: Mon, 17 Jun 2024 17:50:57 +0200 Subject: [PATCH 5/6] Import `rand` from `ark-std` --- poly-commit/src/linear_codes/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/poly-commit/src/linear_codes/utils.rs b/poly-commit/src/linear_codes/utils.rs index 40eee6ea..332263f4 100644 --- a/poly-commit/src/linear_codes/utils.rs +++ b/poly-commit/src/linear_codes/utils.rs @@ -15,7 +15,7 @@ use ark_std::{ #[cfg(all(not(feature = "std"), target_arch = "aarch64"))] use num_traits::Float; -use rand::{Rng, SeedableRng}; +use ark_std::rand::{Rng, SeedableRng}; use rand_chacha::ChaChaRng; use super::CHACHA_SEED_BYTES; From 4e6f28ddba2f68c0149337744025dd06aa6592a3 Mon Sep 17 00:00:00 2001 From: Cesar199999 Date: Mon, 17 Jun 2024 17:53:26 +0200 Subject: [PATCH 6/6] Run cargo fmt --- poly-commit/src/linear_codes/utils.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/poly-commit/src/linear_codes/utils.rs b/poly-commit/src/linear_codes/utils.rs index 332263f4..bc31ddcb 100644 --- a/poly-commit/src/linear_codes/utils.rs +++ b/poly-commit/src/linear_codes/utils.rs @@ -7,15 +7,11 @@ use ark_crypto_primitives::sponge::CryptographicSponge; use ark_ff::{FftField, Field, PrimeField}; use ark_poly::{EvaluationDomain, GeneralEvaluationDomain}; use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; -use ark_std::{ - string::ToString, - vec::Vec, - collections::BTreeSet, -}; +use ark_std::{collections::BTreeSet, string::ToString, vec::Vec}; +use ark_std::rand::{Rng, SeedableRng}; #[cfg(all(not(feature = "std"), target_arch = "aarch64"))] use num_traits::Float; -use ark_std::rand::{Rng, SeedableRng}; use rand_chacha::ChaChaRng; use super::CHACHA_SEED_BYTES;