diff --git a/gkr_iop/examples/multi_layer_logup.rs b/gkr_iop/examples/multi_layer_logup.rs index 5e003ea73..857c78432 100644 --- a/gkr_iop/examples/multi_layer_logup.rs +++ b/gkr_iop/examples/multi_layer_logup.rs @@ -8,15 +8,19 @@ use gkr_iop::{ gkr::{ GKRCircuitWitness, GKRProverOutput, layer::{Layer, LayerType, LayerWitness}, - mock::MockProver, }, }; use goldilocks::GoldilocksExt2; use itertools::{Itertools, izip}; use rand::{Rng, rngs::OsRng}; -use subprotocols::expression::{Constant, Expression, VectorType}; +use subprotocols::expression::{Constant, Expression}; use transcript::{BasicTranscript, Transcript}; +#[cfg(debug_assertions)] +use gkr_iop::gkr::mock::MockProver; +#[cfg(debug_assertions)] +use subprotocols::expression::VectorType; + type E = GoldilocksExt2; #[derive(Clone, Debug, Default)] @@ -28,9 +32,9 @@ struct TowerParams { struct TowerChipLayout { params: TowerParams, - // Commit poly indices. - committed_table: usize, - committed_count: usize, + // Committed poly indices. + committed_table_id: usize, + committed_count_id: usize, lookup_challenge: Constant, @@ -49,8 +53,8 @@ impl ProtocolBuilder for TowerChipLayout { } } - fn build_commit_phase1(&mut self, chip: &mut Chip) { - [self.committed_table, self.committed_count] = chip.allocate_committed_base(); + fn build_commit_phase(&mut self, chip: &mut Chip) { + [self.committed_table_id, self.committed_count_id] = chip.allocate_committed_base(); [self.lookup_challenge] = chip.allocate_challenges(); } @@ -134,14 +138,14 @@ impl ProtocolBuilder for TowerChipLayout { vec![updated_table], )); - chip.allocate_base_opening(self.committed_table, table.1); - chip.allocate_base_opening(self.committed_count, count); + chip.allocate_base_opening(self.committed_table_id, table.1); + chip.allocate_base_opening(self.committed_count_id, count); } } pub struct TowerChipTrace { pub table: Vec, - pub count: Vec, + pub multiplicity: Vec, } impl ProtocolWitnessGenerator for TowerChipLayout @@ -152,15 +156,19 @@ where fn phase1_witness(&self, phase1: Self::Trace) -> Vec> { let mut res = vec![vec![]; 2]; - res[self.committed_table] = phase1.table.into_iter().map(E::BaseField::from).collect(); - res[self.committed_count] = phase1.count.into_iter().map(E::BaseField::from).collect(); + res[self.committed_table_id] = phase1.table.into_iter().map(E::BaseField::from).collect(); + res[self.committed_count_id] = phase1 + .multiplicity + .into_iter() + .map(E::BaseField::from) + .collect(); res } fn gkr_witness(&self, phase1: &[Vec], challenges: &[E]) -> GKRCircuitWitness { // Generate witnesses. - let table = &phase1[self.committed_table]; - let count = &phase1[self.committed_count]; + let table = &phase1[self.committed_table_id]; + let count = &phase1[self.committed_count_id]; let beta = self.lookup_challenge.entry(challenges); // Compute table + beta. @@ -212,7 +220,10 @@ fn main() { let count = (0..1 << log_size) .map(|_| OsRng.gen_range(0..1 << log_size as u64)) .collect_vec(); - let phase1_witness = layout.phase1_witness(TowerChipTrace { table, count }); + let phase1_witness = layout.phase1_witness(TowerChipTrace { + table, + multiplicity: count, + }); let mut prover_transcript = BasicTranscript::::new(b"protocol"); diff --git a/gkr_iop/src/chip.rs b/gkr_iop/src/chip.rs index db75ff1c6..b40b66f9c 100644 --- a/gkr_iop/src/chip.rs +++ b/gkr_iop/src/chip.rs @@ -3,7 +3,7 @@ use crate::{evaluation::EvalExpression, gkr::layer::Layer}; pub mod builder; pub mod protocol; -/// Chip stores all information required in the GKR protocol, including the +/// Chip stores all information required in the GKR protocol, including the /// commit phases, the GKR phase and the opening phase. #[derive(Clone, Debug, Default)] pub struct Chip { diff --git a/gkr_iop/src/lib.rs b/gkr_iop/src/lib.rs index 0e3ea1506..dff90eb6d 100644 --- a/gkr_iop/src/lib.rs +++ b/gkr_iop/src/lib.rs @@ -20,8 +20,7 @@ pub trait ProtocolBuilder: Sized { fn build(params: Self::Params) -> (Self, Chip) { let mut chip_spec = Self::init(params); let mut chip = Chip::default(); - chip_spec.build_commit_phase1(&mut chip); - chip_spec.build_commit_phase2(&mut chip); + chip_spec.build_commit_phase(&mut chip); chip_spec.build_gkr_phase(&mut chip); (chip_spec, chip) @@ -29,10 +28,7 @@ pub trait ProtocolBuilder: Sized { /// Specify the polynomials and challenges to be committed and generated in /// Phase 1. - fn build_commit_phase1(&mut self, spec: &mut Chip); - /// Specify the polynomials and challenges to be committed and generated in - /// Phase 2. - fn build_commit_phase2(&mut self, _spec: &mut Chip) {} + fn build_commit_phase(&mut self, spec: &mut Chip); /// Create the GKR layers in the reverse order. For each layer, specify the /// polynomial expressions, evaluation expressions of outputs and evaluation /// positions of the inputs. diff --git a/multilinear_extensions/src/virtual_poly.rs b/multilinear_extensions/src/virtual_poly.rs index c2502620c..a2e65178a 100644 --- a/multilinear_extensions/src/virtual_poly.rs +++ b/multilinear_extensions/src/virtual_poly.rs @@ -378,7 +378,10 @@ pub fn build_eq_x_r_vec(r: &[E]) -> Vec { } } -#[tracing::instrument(skip_all, name = "multilinear_extensions::build_eq_x_r_vec")] +#[tracing::instrument( + skip_all, + name = "multilinear_extensions::build_eq_x_r_vec_with_scalar" +)] pub fn build_eq_x_r_vec_with_scalar + From, F>( r: &[E], scalar: F, diff --git a/subprotocols/src/expression.rs b/subprotocols/src/expression.rs index 807d7afb8..7750280a6 100644 --- a/subprotocols/src/expression.rs +++ b/subprotocols/src/expression.rs @@ -148,7 +148,7 @@ impl std::fmt::Debug for VectorType { } #[derive(Clone, Debug)] -enum FieldType { +enum ScalarType { Base(E::BaseField), Ext(E), } diff --git a/subprotocols/src/expression/evaluate.rs b/subprotocols/src/expression/evaluate.rs index a7cd8fa37..b37f63cd2 100644 --- a/subprotocols/src/expression/evaluate.rs +++ b/subprotocols/src/expression/evaluate.rs @@ -5,7 +5,7 @@ use multilinear_extensions::virtual_poly::eq_eval; use crate::{op_by_type, utils::i64_to_field}; -use super::{Constant, Expression, FieldType, UniPolyVectorType, VectorType, Witness}; +use super::{Constant, Expression, ScalarType, UniPolyVectorType, VectorType, Witness}; impl Expression { pub fn degree(&self) -> usize { @@ -325,13 +325,13 @@ impl Constant { pub fn evaluate(&self, challenges: &[E]) -> E { let res = self.evaluate_inner(challenges); - op_by_type!(FieldType, res, |b| b, |e| e, |bf| E::from(bf)) + op_by_type!(ScalarType, res, |b| b, |e| e, |bf| E::from(bf)) } - fn evaluate_inner(&self, challenges: &[E]) -> FieldType { + fn evaluate_inner(&self, challenges: &[E]) -> ScalarType { match self { - Constant::Base(value) => FieldType::Base(i64_to_field(*value)), - Constant::Challenge(index) => FieldType::Ext(challenges[*index]), + Constant::Base(value) => ScalarType::Base(i64_to_field(*value)), + Constant::Challenge(index) => ScalarType::Ext(challenges[*index]), Constant::Sum(c0, c1) => c0.evaluate_inner(challenges) + c1.evaluate_inner(challenges), Constant::Product(c0, c1) => { c0.evaluate_inner(challenges) * c1.evaluate_inner(challenges) @@ -340,11 +340,11 @@ impl Constant { Constant::Pow(c, degree) => { let value = c.evaluate_inner(challenges); op_by_type!( - FieldType, + ScalarType, value, |value| { value.pow([*degree as u64]) }, - |ext| FieldType::Ext(ext), - |base| FieldType::Base(base) + |ext| ScalarType::Ext(ext), + |base| ScalarType::Base(base) ) } } diff --git a/subprotocols/src/expression/op.rs b/subprotocols/src/expression/op.rs index 5109fca82..1690c24e4 100644 --- a/subprotocols/src/expression/op.rs +++ b/subprotocols/src/expression/op.rs @@ -8,7 +8,7 @@ use itertools::zip_eq; use crate::{define_commutative_op_mle2, define_op_mle, define_op_mle2}; -use super::{Expression, FieldType, UniPolyVectorType, VectorType}; +use super::{Expression, ScalarType, UniPolyVectorType, VectorType}; impl Add for Expression { type Output = Self; @@ -75,7 +75,7 @@ define_op_mle!(VectorType, Neg, neg, |x| { x }); -define_commutative_op_mle2!(FieldType, Add, add, |x, y| x + y); -define_commutative_op_mle2!(FieldType, Mul, mul, |x, y| x * y); -define_op_mle2!(FieldType, Sub, sub, |x, y| x + (-y)); -define_op_mle!(FieldType, Neg, neg, |x| -x); +define_commutative_op_mle2!(ScalarType, Add, add, |x, y| x + y); +define_commutative_op_mle2!(ScalarType, Mul, mul, |x, y| x * y); +define_op_mle2!(ScalarType, Sub, sub, |x, y| x + (-y)); +define_op_mle!(ScalarType, Neg, neg, |x| -x);