Skip to content

Commit

Permalink
Code improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
darth-cy committed Nov 26, 2024
1 parent 19b03e9 commit 1f4108a
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 99 deletions.
2 changes: 1 addition & 1 deletion spartan_parallel/src/dense_mlpoly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ impl<S: SpartanExtensionField> PolyEvalProof<S> {

for i in 0..poly_size.len() {
let num_vars = poly_size[i].next_power_of_two().log_2();
let L = if let Some(L) = L_map.get(&num_vars) {
let _L = if let Some(L) = L_map.get(&num_vars) {
L
} else {
let (left_num_vars, right_num_vars) = EqPolynomial::<S>::compute_factored_lens(num_vars);
Expand Down
97 changes: 56 additions & 41 deletions spartan_parallel/src/nizk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::scalar::SpartanExtensionField;
use super::errors::ProofVerifyError;
use super::math::Math;
use super::random::RandomTape;
use super::transcript::{AppendToTranscript, ProofTranscript};
use super::transcript::ProofTranscript;
use merlin::Transcript;
use serde::{Deserialize, Serialize};
mod bullet;
Expand Down Expand Up @@ -45,12 +45,15 @@ impl<S: SpartanExtensionField> KnowledgeProof<S> {
}

pub fn verify(&self, transcript: &mut Transcript) -> Result<(), ProofVerifyError> {
<Transcript as ProofTranscript<S>>::append_protocol_name(
transcript,
KnowledgeProof::<S>::protocol_name(),
);

let c: S = transcript.challenge_scalar(b"c");
// Transcript operations to preserve consistency for the verify function
{
<Transcript as ProofTranscript<S>>::append_protocol_name(
transcript,
KnowledgeProof::<S>::protocol_name(),
);

let _c: S = transcript.challenge_scalar(b"c");
}

// TODO: Alternative PCS Verification
Ok(())
Expand Down Expand Up @@ -89,12 +92,15 @@ impl<S: SpartanExtensionField> EqualityProof<S> {
}

pub fn verify(&self, transcript: &mut Transcript) -> Result<(), ProofVerifyError> {
<Transcript as ProofTranscript<S>>::append_protocol_name(
transcript,
EqualityProof::<S>::protocol_name(),
);

let c: S = transcript.challenge_scalar(b"c");
// Transcript operations to preserve consistency for the verify function
{
<Transcript as ProofTranscript<S>>::append_protocol_name(
transcript,
EqualityProof::<S>::protocol_name(),
);

let _c: S = transcript.challenge_scalar(b"c");
}

// TODO: Alternative PCS Verification
Ok(())
Expand Down Expand Up @@ -145,18 +151,21 @@ impl<S: SpartanExtensionField> ProductProof<S> {
ProductProof { z }
}

fn check_equality(_c: &S, _z1: &S, _z2: &S) -> bool {
fn _check_equality(_c: &S, _z1: &S, _z2: &S) -> bool {
// TODO: Alternative PCS Verification
true
}

pub fn verify(&self, transcript: &mut Transcript) -> Result<(), ProofVerifyError> {
<Transcript as ProofTranscript<S>>::append_protocol_name(
transcript,
ProductProof::<S>::protocol_name(),
);

let c: S = transcript.challenge_scalar(b"c");
// Transcript operations to preserve consistency for the verify function
{
<Transcript as ProofTranscript<S>>::append_protocol_name(
transcript,
ProductProof::<S>::protocol_name(),
);

let _c: S = transcript.challenge_scalar(b"c");
}

// TODO: Alternative PCS Verification
Ok(())
Expand Down Expand Up @@ -218,12 +227,15 @@ impl<S: SpartanExtensionField> DotProductProof<S> {
}

pub fn verify(&self, transcript: &mut Transcript, a: &[S]) -> Result<(), ProofVerifyError> {
<Transcript as ProofTranscript<S>>::append_protocol_name(
transcript,
DotProductProof::<S>::protocol_name(),
);
S::append_field_vector_to_transcript(b"a", transcript, a);
let c: S = transcript.challenge_scalar(b"c");
// Transcript operations to preserve consistency for the verify function
{
<Transcript as ProofTranscript<S>>::append_protocol_name(
transcript,
DotProductProof::<S>::protocol_name(),
);
S::append_field_vector_to_transcript(b"a", transcript, a);
let _c: S = transcript.challenge_scalar(b"c");
}

let _dotproduct_z_a = DotProductProof::compute_dotproduct(&self.z, a);

Expand Down Expand Up @@ -304,26 +316,29 @@ impl<S: SpartanExtensionField> DotProductProofLog<S> {
) -> Result<(), ProofVerifyError> {
assert_eq!(a.len(), n);

<Transcript as ProofTranscript<S>>::append_protocol_name(
transcript,
DotProductProofLog::<S>::protocol_name(),
);
// Transcript operations to preserve consistency for the verify function
{
<Transcript as ProofTranscript<S>>::append_protocol_name(
transcript,
DotProductProofLog::<S>::protocol_name(),
);

S::append_field_vector_to_transcript(b"a", transcript, a);
S::append_field_vector_to_transcript(b"a", transcript, a);

// sample a random base and scale the generator used for
// the output of the inner product
let r: S = transcript.challenge_scalar(b"r");
// sample a random base and scale the generator used for
// the output of the inner product
let _r: S = transcript.challenge_scalar(b"r");

// BulletReductionProof - verification_scalars
let mut m = a.len();
while m != 1 {
m /= 2;
// BulletReductionProof - verification_scalars
let mut m = a.len();
while m != 1 {
m /= 2;

let u: S = transcript.challenge_scalar(b"u");
}
let _u: S = transcript.challenge_scalar(b"u");
}

let c: S = transcript.challenge_scalar(b"c");
let _c: S = transcript.challenge_scalar(b"c");
}

// TODO: Alternative PCS Verification
Ok(())
Expand Down
1 change: 0 additions & 1 deletion spartan_parallel/src/product_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::scalar::SpartanExtensionField;
use super::dense_mlpoly::DensePolynomial;
use super::dense_mlpoly::EqPolynomial;
use super::math::Math;
use super::scalar::Scalar;
use super::sumcheck::SumcheckInstanceProof;
use super::transcript::ProofTranscript;
use merlin::Transcript;
Expand Down
2 changes: 1 addition & 1 deletion spartan_parallel/src/r1csproof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ impl<S: SpartanExtensionField> R1CSProof<S> {
// So we need to multiply each entry by (1 - rq0)(1 - rq1)
let mut eval_vars_comb_list = Vec::new();
for p in 0..num_instances {
let wit_sec_p = |i: usize| {
let _wit_sec_p = |i: usize| {
if witness_secs[i].w_mat.len() == 1 {
0
} else {
Expand Down
2 changes: 1 addition & 1 deletion spartan_parallel/src/scalar/fp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::SpartanExtensionField;
use crate::{AppendToTranscript, ProofTranscript, Transcript};
use crate::{ProofTranscript, Transcript};
use ceno_goldilocks::Goldilocks;
use core::borrow::Borrow;
use core::iter::{Product, Sum};
Expand Down
4 changes: 1 addition & 3 deletions spartan_parallel/src/scalar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use ceno_goldilocks::ExtensionField;
use ff::Field;
pub use fp::Scalar;
pub use fp2::ScalarExt2;
use merlin::Transcript;
use rand::{CryptoRng, RngCore};
use serde::Serialize;
use std::fmt;
Expand All @@ -17,9 +18,6 @@ use std::{
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
use zeroize::Zeroize;

use crate::transcript::AppendToTranscript;
use merlin::Transcript;

/// Trait describing the field element
/// Wraps around Goldilocks field towers from ceno-goldilocks
/// See: https://github.com/scroll-tech/ceno-Goldilocks
Expand Down
49 changes: 1 addition & 48 deletions spartan_parallel/src/sparse_mlpoly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ impl<S: SpartanExtensionField> SparseMatPolynomial<S> {
.collect::<Vec<S>>()
}

pub fn multiply_vec(&self, num_rows: usize, num_cols: usize, z: &[S]) -> Vec<S> {
pub fn _multiply_vec(&self, num_rows: usize, num_cols: usize, z: &[S]) -> Vec<S> {
assert_eq!(z.len(), num_cols);

(0..self.M.len())
Expand Down Expand Up @@ -1472,53 +1472,6 @@ impl<S: SpartanExtensionField> SparseMatPolyEvalProof<S> {
}
}

pub struct SparsePolyEntry<S: SpartanExtensionField> {
pub idx: usize,
pub val: S,
}

impl<S: SpartanExtensionField> SparsePolyEntry<S> {
pub fn new(idx: usize, val: S) -> Self {
SparsePolyEntry { idx, val }
}
}

pub struct SparsePolynomial<S: SpartanExtensionField> {
num_vars: usize,
Z: Vec<SparsePolyEntry<S>>,
}

impl<S: SpartanExtensionField> SparsePolynomial<S> {
pub fn new(num_vars: usize, Z: Vec<SparsePolyEntry<S>>) -> Self {
SparsePolynomial { num_vars, Z }
}

fn compute_chi(a: &[bool], r: &[S]) -> S {
assert_eq!(a.len(), r.len());
let mut chi_i = S::field_one();
for j in 0..r.len() {
if a[j] {
chi_i = chi_i * r[j];
} else {
chi_i = chi_i * (S::field_one() - r[j]);
}
}
chi_i
}

// Takes O(n log n). TODO: do this in O(n) where n is the number of entries in Z
pub fn evaluate(&self, r: &[S]) -> S {
assert_eq!(self.num_vars, r.len());

(0..self.Z.len())
.map(|i| {
let bits = self.Z[i].idx.get_bits(r.len());
SparsePolynomial::compute_chi(&bits, r) * self.Z[i].val
})
.sum()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion spartan_parallel/src/sumcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl<S: SpartanExtensionField> ZKSumcheckInstanceProof<S> {
let r_i = transcript.challenge_scalar(b"challenge_nextround");

// verify the proof of sum-check and evals
let res = {
let _res = {
// produce two weights
let w: Vec<S> = transcript.challenge_vector(b"combine_two_claims_to_one", 2);

Expand Down
4 changes: 2 additions & 2 deletions spartan_parallel/src/unipoly.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::scalar::{Scalar, SpartanExtensionField};
use super::scalar::SpartanExtensionField;
use super::transcript::{AppendToTranscript, ProofTranscript};
use merlin::Transcript;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -115,8 +115,8 @@ impl<S: SpartanExtensionField> AppendToTranscript for UniPoly<S> {

#[cfg(test)]
mod tests {

use super::*;
use crate::scalar::Scalar;

#[test]
fn test_from_evals_quad() {
Expand Down

0 comments on commit 1f4108a

Please sign in to comment.