Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 59 additions & 8 deletions benches/sumcheck-benches/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion benches/sumcheck-benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ edition = "2021"
[dependencies]
ark-ff = "0.5.0"
ark-poly = "0.5.0"
ark-serialize = "0.5.0"
ark-std ="0.5.0"
ark-bn254 = "0.5.0"
space-efficient-sumcheck = { path = "../../." }
efficient-sumcheck = { path = "../../." }

[profile.release]
debug = true
2 changes: 1 addition & 1 deletion benches/sumcheck-benches/run_benches.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# We measure (i) wall time; and (ii) maximum resident set size, using the GNU-time facility.

algorithms="ProductBlendy2 ProductVSBW ProductCTY Blendy1 Blendy2 VSBW Blendy3 Blendy4 CTY"
algorithms="Blendy2 VSBW Blendy1 Blendy3 Blendy4 CTY ProductBlendy2 ProductVSBW ProductCTY"
fields="Field64 Field128 FieldBn254"

for algorithm in $algorithms; do
Expand Down
7 changes: 4 additions & 3 deletions benches/sumcheck-benches/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ark_bn254::Fr as BN254Field;
use ark_ff::Field;

use space_efficient_sumcheck::{
use efficient_sumcheck::{
hypercube::Hypercube,
multilinear::{
BlendyProver, BlendyProverConfig, SpaceProver, SpaceProverConfig, TimeProver,
Expand All @@ -14,7 +14,7 @@ use space_efficient_sumcheck::{
order_strategy::SignificantBitOrder,
prover::{Prover, ProverConfig},
streams::{multivariate_claim, multivariate_product_claim},
tests::{BenchStream, F128, F64},
tests::{BenchStream, F128, F64}, // SmallGoldilocks as F64
ProductSumcheck, Sumcheck,
};

Expand Down Expand Up @@ -131,7 +131,8 @@ fn main() {
run_on_field::<F128>(bench_args);
}
FieldLabel::FieldBn254 => {
run_on_field::<BN254Field>(bench_args);
// run_on_field::<BN254Field>(bench_args);
run_on_field::<F64>(bench_args);
}
};
}
81 changes: 69 additions & 12 deletions src/multilinear/provers/blendy/core.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use ark_ff::Field;
use ark_std::vec::Vec;
use ark_std::cfg_iter_mut;
use ark_std::{cfg_into_iter, vec::Vec};

use crate::{
hypercube::Hypercube, interpolation::LagrangePolynomial, messages::VerifierMessages,
order_strategy::GraycodeOrder, streams::Stream,
};
#[cfg(feature = "parallel")]
use rayon::iter::{
IndexedParallelIterator, IntoParallelIterator, IntoParallelRefMutIterator, ParallelIterator,
};

pub struct BlendyProver<F, S>
where
Expand Down Expand Up @@ -160,20 +165,72 @@ where
}

pub fn update_prefix_sums(&mut self) {
self.sums = self
.sums
.clone()
.into_iter()
.enumerate()
.scan(F::ZERO, |sum, (index, item)| {
match self.is_single_staged() {
true => *sum += self.evaluation_stream.evaluation(index),
false => *sum += item,
let n = self.sums.len();
if n == 0 {
return;
}

// Step 0: Unified input vector
let input: Vec<F> = if self.is_single_staged() {
(0..n)
.map(|i| self.evaluation_stream.evaluation(i))
.collect()
} else {
self.sums.clone()
};

// Step 1: Determine chunk size and boundaries
#[cfg(feature = "parallel")]
let num_threads = rayon::current_num_threads().max(1);
#[cfg(not(feature = "parallel"))]
let num_threads = 1;
let chunk_size: usize = (n / num_threads).max(1);

// Compute chunk start indices
let chunk_starts: Vec<usize> = (0..n).step_by(chunk_size).collect();

// Step 2: Parallel local prefix sums
let mut partial_sums: Vec<Vec<F>> = cfg_into_iter!(chunk_starts.clone())
.map(|start: usize| {
let end: usize = (start + chunk_size).min(n);
let mut local_sum = F::ZERO;
let mut out = Vec::with_capacity(end - start);
for &x in &input[start..end] {
local_sum += x;
out.push(local_sum);
}
Some(*sum)
out
})
.collect::<Vec<F>>();
.collect();

// Step 3: Collect per-chunk totals
let mut chunk_totals: Vec<F> = partial_sums
.iter()
.map(|chunk| *chunk.last().unwrap())
.collect();

// Step 4: Exclusive prefix sum of chunk totals (serial)
for i in 1..chunk_totals.len() {
let prev = chunk_totals[i - 1];
chunk_totals[i] += prev;
}

// Step 5: Offset adjust each chunk in parallel
cfg_iter_mut!(partial_sums)
.enumerate()
.for_each(|(i, chunk)| {
let offset = if i == 0 { F::ZERO } else { chunk_totals[i - 1] };
if offset != F::ZERO {
for x in chunk.iter_mut() {
*x += offset;
}
}
});

// Step 6: Flatten into final result
self.sums = partial_sums.into_iter().flatten().collect();
}

pub fn total_rounds(&self) -> usize {
self.num_variables
}
Expand Down
34 changes: 29 additions & 5 deletions src/multilinear/sumcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,27 @@ impl<F: Field> Sumcheck<F> {
#[cfg(test)]
mod tests {
use super::Sumcheck;
use crate::streams::Stream;
use crate::{
multilinear::{BlendyProver, BlendyProverConfig, ReduceMode, TimeProver},
prover::{Prover, ProverConfig},
tests::{BenchStream, F19},
tests::{
multilinear::{BasicProver, BasicProverConfig},
polynomials::Polynomial,
BenchStream, F19,
},
};
use ark_poly::multivariate;

#[test]
fn sanity() {
const NUM_VARIABLES: usize = 20;
const NUM_VARIABLES: usize = 16;

// take an evaluation stream
let evaluation_stream: BenchStream<F19> = BenchStream::new(NUM_VARIABLES);
let claim = evaluation_stream.claimed_sum;

// blendy
// 1) blendy
let mut blendy_k3_prover = BlendyProver::<F19, BenchStream<F19>>::new(
BlendyProverConfig::new(claim, 3, NUM_VARIABLES, evaluation_stream.clone()),
);
Expand All @@ -84,7 +90,7 @@ mod tests {
BlendyProver<F19, BenchStream<F19>>,
>(&mut blendy_k3_prover, &mut ark_std::test_rng());

// time_prover_variablewise
// 2) time_prover_variablewise
let mut time_prover_variablewise = TimeProver::<F19, BenchStream<F19>>::new(<TimeProver<
F19,
BenchStream<F19>,
Expand All @@ -100,11 +106,29 @@ mod tests {
&mut ark_std::test_rng(),
);

// ensure transcripts identical
// 3) basic prover
let s_evaluations: Vec<F19> = (0..1 << NUM_VARIABLES)
.map(|i| evaluation_stream.evaluation(i))
.collect();
let p = <multivariate::SparsePolynomial<F19, multivariate::SparseTerm> as Polynomial<
F19,
>>::from_hypercube_evaluations(s_evaluations);
let mut basic_prover =
BasicProver::<F19>::new(BasicProverConfig::new(claim, NUM_VARIABLES, p));
let basic_prover_transcript = Sumcheck::<F19>::prove::<BenchStream<F19>, BasicProver<F19>>(
&mut basic_prover,
&mut ark_std::test_rng(),
);

// ensure all transcripts (1, 2, 3) identical
assert_eq!(
time_prover_variablewise_transcript.prover_messages,
blendy_prover_transcript.prover_messages
);
assert_eq!(
time_prover_variablewise_transcript.prover_messages,
basic_prover_transcript.prover_messages
);

// time_prover_pairwise: this should pass but I have nothing to compare it with
let mut time_prover_pairwise = TimeProver::<F19, BenchStream<F19>>::new(<TimeProver<
Expand Down
22 changes: 20 additions & 2 deletions src/tests/fields.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use ark_ff::fields::{Fp128, Fp64, MontBackend, MontConfig};

// use ark_ff::SqrtPrecomputation;
use ark_ff::{
// ark_ff_macros::SmallFpConfig,
fields::{Fp128, Fp64, MontBackend, MontConfig},
// BigInt, SmallFp, SmallFpConfig,
};
#[derive(MontConfig)]
#[modulus = "19"]
#[generator = "2"]
Expand All @@ -23,3 +27,17 @@ pub type F64 = Fp64<MontBackend<F64Config, 1>>;
#[generator = "2"]
pub struct F128Config;
pub type F128 = Fp128<MontBackend<F128Config, 2>>;

// #[derive(SmallFpConfig)]
// #[modulus = "2147483647"] // 2 ^ 31 - 1
// #[generator = "2"]
// #[backend = "montgomery"]
// pub struct SmallM31ConfigMont;
// pub type SmallM31 = SmallFp<SmallM31ConfigMont>;

// #[derive(SmallFpConfig)]
// #[modulus = "18446744069414584321"] // Goldilock's prime 2^64 - 2^32 + 1
// #[generator = "2"]
// #[backend = "montgomery"]
// pub struct SmallF64ConfigMont;
// pub type SmallGoldilocks = SmallFp<SmallF64ConfigMont>;
2 changes: 1 addition & 1 deletion src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ mod streams;
pub mod multilinear;
pub mod multilinear_product;
pub mod polynomials;
pub use fields::{F128, F19, F64, M31};
pub use fields::{F128, F19, F64, M31}; // SmallGoldilocks, SmallM31,
pub use streams::BenchStream;
Loading
Loading