Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: define objects over base fields #52

Open
wants to merge 25 commits into
base: feat/goldilocks-spartan
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c1619b7
assignment is defined over base field
kunxian-xia Dec 4, 2024
214fe75
dense polynomial can be defined over base field
kunxian-xia Dec 4, 2024
c86debd
Remove zk
darth-cy Dec 4, 2024
88029e3
Remove blinds
darth-cy Dec 4, 2024
d746a2d
Merge branch 'feat/goldilocks-spartan' into feat/basefield_backing_ob…
darth-cy Dec 4, 2024
6b2df83
Add typeset pattern
darth-cy Dec 5, 2024
d134a9f
Remove zk
darth-cy Dec 4, 2024
d4e022b
Remove blinds
darth-cy Dec 4, 2024
acbc979
remove nizk
kunxian-xia Dec 5, 2024
4b160ca
Adapt proving methods onto SumcheckInstanceProof. Remove ZK proof str…
darth-cy Dec 5, 2024
d1bfaca
Revert "Adapt proving methods onto SumcheckInstanceProof. Remove ZK p…
darth-cy Dec 5, 2024
6b39853
Resolve compilation issues
darth-cy Dec 6, 2024
a8ba63f
Remove additional commitment proofs
Dec 6, 2024
7d12338
fmtr
darth-cy Dec 6, 2024
ce6cd4e
Remove debug flag
darth-cy Dec 9, 2024
aa19362
Remove blinds
darth-cy Dec 9, 2024
e905720
Remove random tape
darth-cy Dec 9, 2024
1a4fdb3
Remove PolyEvalProof prove, verify logic
darth-cy Dec 9, 2024
abef32a
fmt
darth-cy Dec 9, 2024
185a779
Merge branch 'feat/remove_backend_zk' into feat/basefield_backing_obj…
darth-cy Dec 9, 2024
8ff1852
Use typeset. Resolve compilation issues
darth-cy Dec 10, 2024
cee5800
Merge branch 'feat/goldilocks-spartan' into feat/basefield_backing_ob…
darth-cy Dec 10, 2024
daa75d4
Remove merge duplicates
darth-cy Dec 10, 2024
dad8e14
more base field backed objects
kunxian-xia Dec 11, 2024
ec71f08
more base field backed objects
kunxian-xia Dec 11, 2024
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
4 changes: 3 additions & 1 deletion circ_blocks/Cargo.lock

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

1 change: 1 addition & 0 deletions circ_blocks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ thiserror = "1"
bellman = { git = "https://github.com/alex-ozdemir/bellman.git", branch = "mirage", optional = true }
rayon = { version = "1", optional = true }
ff = { version = "0.12", optional = true }
halo2curves = "0.1.0"
fxhash = "0.2"
good_lp = { version = "1.10", features = [
"lp-solvers",
Expand Down
37 changes: 29 additions & 8 deletions circ_blocks/examples/zxc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use circ::target::r1cs::wit_comp::StagedWitCompEvaluator;
use circ::target::r1cs::ProverData;
use circ::target::r1cs::{Lc, VarType};
use core::cmp::min;
use halo2curves::serde::SerdeObject;
use libspartan::scalar::{ScalarExt2, SpartanExtensionField};
use rug::Integer;

Expand All @@ -33,7 +34,6 @@ use libspartan::{
use merlin::Transcript;
use serde::{Deserialize, Serialize};
use std::time::*;
use std::time::*;

// How many reserved variables (EXCLUDING V) are in front of the actual input / output?
// %BN, %RET, %TS, %AS, %SP, %BP
Expand Down Expand Up @@ -437,7 +437,10 @@ impl<S: SpartanExtensionField> RunTimeKnowledge<S> {
for exec in block {
writeln!(&mut f, "EXEC {}", exec_counter)?;
for assg in &exec.assignment {
write!(&mut f, "{} ", bytes_to_integer(&assg.to_bytes()))?;
let mut padded = [0; 32];
padded[..8].copy_from_slice(&assg.to_raw_bytes());

write!(&mut f, "{} ", bytes_to_integer(&padded))?;
}
writeln!(&mut f)?;
exec_counter += 1;
Expand All @@ -449,7 +452,10 @@ impl<S: SpartanExtensionField> RunTimeKnowledge<S> {
for exec in &self.exec_inputs {
writeln!(&mut f, "EXEC {}", exec_counter)?;
for assg in &exec.assignment {
write!(&mut f, "{} ", bytes_to_integer(&assg.to_bytes()))?;
let mut padded = [0; 32];
padded[..8].copy_from_slice(&assg.to_raw_bytes());

write!(&mut f, "{} ", bytes_to_integer(&padded))?;
}
writeln!(&mut f)?;
exec_counter += 1;
Expand All @@ -459,7 +465,10 @@ impl<S: SpartanExtensionField> RunTimeKnowledge<S> {
for addr in &self.init_phy_mems_list {
writeln!(&mut f, "ACCESS {}", addr_counter)?;
for assg in &addr.assignment {
write!(&mut f, "{} ", bytes_to_integer(&assg.to_bytes()))?;
let mut padded = [0; 32];
padded[..8].copy_from_slice(&assg.to_raw_bytes());

write!(&mut f, "{} ", bytes_to_integer(&padded))?;
}
writeln!(&mut f)?;
addr_counter += 1;
Expand All @@ -469,7 +478,10 @@ impl<S: SpartanExtensionField> RunTimeKnowledge<S> {
for addr in &self.init_vir_mems_list {
writeln!(&mut f, "ACCESS {}", addr_counter)?;
for assg in &addr.assignment {
write!(&mut f, "{} ", bytes_to_integer(&assg.to_bytes()))?;
let mut padded = [0; 32];
padded[..8].copy_from_slice(&assg.to_raw_bytes());

write!(&mut f, "{} ", bytes_to_integer(&padded))?;
}
writeln!(&mut f)?;
addr_counter += 1;
Expand All @@ -479,7 +491,10 @@ impl<S: SpartanExtensionField> RunTimeKnowledge<S> {
for addr in &self.addr_phy_mems_list {
writeln!(&mut f, "ACCESS {}", addr_counter)?;
for assg in &addr.assignment {
write!(&mut f, "{} ", bytes_to_integer(&assg.to_bytes()))?;
let mut padded = [0; 32];
padded[..8].copy_from_slice(&assg.to_raw_bytes());

write!(&mut f, "{} ", bytes_to_integer(&padded))?;
}
writeln!(&mut f)?;
addr_counter += 1;
Expand All @@ -489,7 +504,10 @@ impl<S: SpartanExtensionField> RunTimeKnowledge<S> {
for addr in &self.addr_vir_mems_list {
writeln!(&mut f, "ACCESS {}", addr_counter)?;
for assg in &addr.assignment {
write!(&mut f, "{} ", bytes_to_integer(&assg.to_bytes()))?;
let mut padded = [0; 32];
padded[..8].copy_from_slice(&assg.to_raw_bytes());

write!(&mut f, "{} ", bytes_to_integer(&padded))?;
}
writeln!(&mut f)?;
addr_counter += 1;
Expand All @@ -499,7 +517,10 @@ impl<S: SpartanExtensionField> RunTimeKnowledge<S> {
for addr in &self.addr_ts_bits_list {
writeln!(&mut f, "ACCESS {}", addr_counter)?;
for assg in &addr.assignment {
write!(&mut f, "{} ", bytes_to_integer(&assg.to_bytes()))?;
let mut padded = [0; 32];
padded[..8].copy_from_slice(&assg.to_raw_bytes());

write!(&mut f, "{} ", bytes_to_integer(&padded))?;
}
writeln!(&mut f)?;
addr_counter += 1;
Expand Down
1 change: 1 addition & 0 deletions spartan_parallel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ colored = { version = "2", default-features = false, optional = true }
flate2 = { version = "1" }
goldilocks = { git = "https://github.com/scroll-tech/ceno-Goldilocks" }
ff = "0.13.0"
halo2curves = "0.1.0"

[dev-dependencies]
criterion = "0.5"
Expand Down
6 changes: 3 additions & 3 deletions spartan_parallel/src/custom_dense_mlpoly.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#![allow(clippy::too_many_arguments)]
use std::cmp::min;

use super::math::Math;
use crate::dense_mlpoly::DensePolynomial;
use crate::mle::Ext;
use crate::scalar::SpartanExtensionField;

use super::math::Math;

const MODE_P: usize = 1;
const MODE_Q: usize = 2;
const MODE_W: usize = 3;
Expand Down Expand Up @@ -328,7 +328,7 @@ impl<S: SpartanExtensionField> DensePolynomialPqx<S> {
}

// Convert to a (p, q_rev, x_rev) regular dense poly of form (p, q, x)
pub fn to_dense_poly(&self) -> DensePolynomial<S> {
pub fn to_dense_poly(&self) -> DensePolynomial<S, Ext> {
let mut Z_poly =
vec![
S::field_zero();
Expand Down
73 changes: 46 additions & 27 deletions spartan_parallel/src/dense_mlpoly.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
#![allow(clippy::too_many_arguments)]
use crate::scalar::SpartanExtensionField;

use super::errors::ProofVerifyError;
use super::math::Math;
use super::random::RandomTape;
use super::transcript::ProofTranscript;
use crate::mle::{Base, Ext, MLEType, MLE};
use crate::scalar::SpartanExtensionField;
use core::ops::Index;
use ff::Field;
use merlin::Transcript;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::{collections::HashMap, process::Output};

#[cfg(feature = "multicore")]
use rayon::prelude::*;

#[derive(Debug, Clone)]
pub struct DensePolynomial<S: SpartanExtensionField> {
pub struct DensePolynomial<S: SpartanExtensionField, T: MLEType> {
num_vars: usize, // the number of variables in the multilinear polynomial
len: usize,
Z: Vec<S>, // evaluations of the polynomial in all the 2^num_vars Boolean inputs
Z: MLE<S, T>, // evaluations of the polynomial in all the 2^num_vars Boolean inputs
}

pub struct EqPolynomial<S: SpartanExtensionField> {
Expand Down Expand Up @@ -114,15 +115,15 @@ impl<S: SpartanExtensionField> IdentityPolynomial<S> {
}
}

impl<S: SpartanExtensionField> DensePolynomial<S> {
impl<S: SpartanExtensionField> DensePolynomial<S, Ext> {
pub fn new(mut Z: Vec<S>) -> Self {
// If length of Z is not a power of 2, append Z with 0
let zero = S::field_zero();
Z.extend(vec![zero; Z.len().next_power_of_two() - Z.len()]);
DensePolynomial {
num_vars: Z.len().log_2(),
len: Z.len(),
Z,
Z: MLE::<S, Ext>::new(Z),
}
}

Expand All @@ -134,11 +135,7 @@ impl<S: SpartanExtensionField> DensePolynomial<S> {
self.len
}

pub fn clone(&self) -> DensePolynomial<S> {
DensePolynomial::new(self.Z[0..self.len].to_vec())
}

pub fn split(&self, idx: usize) -> (DensePolynomial<S>, DensePolynomial<S>) {
pub fn split(&self, idx: usize) -> (DensePolynomial<S, Ext>, DensePolynomial<S, Ext>) {
assert!(idx < self.len());
(
DensePolynomial::new(self.Z[..idx].to_vec()),
Expand Down Expand Up @@ -253,7 +250,7 @@ impl<S: SpartanExtensionField> DensePolynomial<S> {
assert_eq!(r.len(), self.get_num_vars());
let chis = EqPolynomial::new(r.to_vec()).evals();
assert_eq!(chis.len(), self.Z.len());
Self::compute_dotproduct(&self.Z, &chis)
Self::compute_dotproduct(&self.Z[0..], &chis)
}

fn compute_dotproduct(a: &[S], b: &[S]) -> S {
Expand All @@ -262,10 +259,10 @@ impl<S: SpartanExtensionField> DensePolynomial<S> {
}

fn vec(&self) -> &Vec<S> {
&self.Z
&self.Z.inner_ref()
}

pub fn extend(&mut self, other: &DensePolynomial<S>) {
pub fn extend(&mut self, other: &DensePolynomial<S, Ext>) {
// TODO: allow extension even when some vars are bound
assert_eq!(self.Z.len(), self.len);
let other_vec = other.vec();
Expand All @@ -276,9 +273,9 @@ impl<S: SpartanExtensionField> DensePolynomial<S> {
assert_eq!(self.Z.len(), self.len);
}

pub fn merge<'a, I>(polys: I) -> DensePolynomial<S>
pub fn merge<'a, I>(polys: I) -> DensePolynomial<S, Ext>
where
I: IntoIterator<Item = DensePolynomial<S>>,
I: IntoIterator<Item = DensePolynomial<S, Ext>>,
{
let mut Z: Vec<S> = Vec::new();
for poly in polys.into_iter() {
Expand All @@ -300,12 +297,34 @@ impl<S: SpartanExtensionField> DensePolynomial<S> {
}
}

impl<S: SpartanExtensionField> Index<usize> for DensePolynomial<S> {
impl<S: SpartanExtensionField> DensePolynomial<S, Base> {
pub fn new_from_base(mut Z: Vec<S::BaseField>) -> Self {
// If length of Z is not a power of 2, append Z with 0
let zero = S::BaseField::ZERO;
Z.extend(vec![zero; Z.len().next_power_of_two() - Z.len()]);
DensePolynomial {
num_vars: Z.len().log_2(),
len: Z.len(),
Z: MLE::<S, Base>::new(Z),
}
}
}

impl<S: SpartanExtensionField> Index<usize> for DensePolynomial<S, Ext> {
type Output = S;

#[inline(always)]
fn index(&self, _index: usize) -> &S {
&(self.Z[_index])
fn index(&self, index: usize) -> &Self::Output {
&(self.Z[index])
}
}

impl<S: SpartanExtensionField> Index<usize> for DensePolynomial<S, Base> {
type Output = S::BaseField;

#[inline(always)]
fn index(&self, index: usize) -> &Self::Output {
&(self.Z[index])
}
}

Expand All @@ -320,7 +339,7 @@ impl<S: SpartanExtensionField> PolyEvalProof<S> {
}

pub fn prove(
_poly: &DensePolynomial<S>,
_poly: &DensePolynomial<S, Ext>,
_r: &[S], // point at which the polynomial is evaluated
_Zr: &S, // evaluation of \widetilde{Z}(r)
_transcript: &mut Transcript,
Expand Down Expand Up @@ -352,7 +371,7 @@ impl<S: SpartanExtensionField> PolyEvalProof<S> {

// Evaluation of multiple points on the same instance
pub fn prove_batched_points(
_poly: &DensePolynomial<S>,
_poly: &DensePolynomial<S, Ext>,
_r_list: Vec<Vec<S>>, // point at which the polynomial is evaluated
_Zr_list: Vec<S>, // evaluation of \widetilde{Z}(r) on each point
_transcript: &mut Transcript,
Expand All @@ -375,9 +394,9 @@ impl<S: SpartanExtensionField> PolyEvalProof<S> {
// Evaluation on multiple instances, each at different point
// Size of each instance might be different, but all are larger than the evaluation point
pub fn prove_batched_instances(
_poly_list: &Vec<DensePolynomial<S>>, // list of instances
_r_list: Vec<&Vec<S>>, // point at which the polynomial is evaluated
_Zr_list: &Vec<S>, // evaluation of \widetilde{Z}(r) on each instance
_poly_list: &Vec<DensePolynomial<S, Ext>>, // list of instances
_r_list: Vec<&Vec<S>>, // point at which the polynomial is evaluated
_Zr_list: &Vec<S>, // evaluation of \widetilde{Z}(r) on each instance
_transcript: &mut Transcript,
_random_tape: &mut RandomTape<S>,
) -> Vec<PolyEvalProof<S>> {
Expand All @@ -399,7 +418,7 @@ impl<S: SpartanExtensionField> PolyEvalProof<S> {
// Like prove_batched_instances, but r is divided into rq ++ ry
// Each polynomial is supplemented with num_proofs and num_inputs
pub fn prove_batched_instances_disjoint_rounds(
_poly_list: &Vec<&DensePolynomial<S>>,
_poly_list: &Vec<&DensePolynomial<S, Ext>>,
_num_proofs_list: &Vec<usize>,
_num_inputs_list: &Vec<usize>,
_rq: &[S],
Expand All @@ -426,7 +445,7 @@ impl<S: SpartanExtensionField> PolyEvalProof<S> {

// Treat the polynomial(s) as univariate and open on a single point
pub fn prove_uni_batched_instances(
_poly_list: &Vec<&DensePolynomial<S>>,
_poly_list: &Vec<&DensePolynomial<S, Ext>>,
_r: &S, // point at which the polynomial is evaluated
_Zr: &Vec<S>, // evaluation of \widetilde{Z}(r)
_transcript: &mut Transcript,
Expand Down
Loading
Loading