Skip to content

Commit

Permalink
Tiny fix according to the latest comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamATD committed Jan 15, 2025
1 parent d51562b commit 87d1a30
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 37 deletions.
41 changes: 26 additions & 15 deletions gkr_iop/examples/multi_layer_logup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -28,9 +32,9 @@ struct TowerParams {
struct TowerChipLayout<E> {
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,

Expand All @@ -49,8 +53,8 @@ impl<E: ExtensionField> ProtocolBuilder for TowerChipLayout<E> {
}
}

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();
}

Expand Down Expand Up @@ -134,14 +138,14 @@ impl<E: ExtensionField> ProtocolBuilder for TowerChipLayout<E> {
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<u64>,
pub count: Vec<u64>,
pub multiplicity: Vec<u64>,
}

impl<E> ProtocolWitnessGenerator<E> for TowerChipLayout<E>
Expand All @@ -152,15 +156,19 @@ where

fn phase1_witness(&self, phase1: Self::Trace) -> Vec<Vec<E::BaseField>> {
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<E::BaseField>], challenges: &[E]) -> GKRCircuitWitness<E> {
// 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.
Expand Down Expand Up @@ -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::<E>::new(b"protocol");

Expand Down
2 changes: 1 addition & 1 deletion gkr_iop/src/chip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 2 additions & 6 deletions gkr_iop/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,15 @@ 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)
}

/// 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.
Expand Down
5 changes: 4 additions & 1 deletion multilinear_extensions/src/virtual_poly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,10 @@ pub fn build_eq_x_r_vec<E: ExtensionField>(r: &[E]) -> Vec<E> {
}
}

#[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<E: ExtensionField + Mul<F, Output = E> + From<F>, F>(
r: &[E],
scalar: F,
Expand Down
2 changes: 1 addition & 1 deletion subprotocols/src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl<E: ExtensionField> std::fmt::Debug for VectorType<E> {
}

#[derive(Clone, Debug)]
enum FieldType<E: ExtensionField> {
enum ScalarType<E: ExtensionField> {
Base(E::BaseField),
Ext(E),
}
Expand Down
16 changes: 8 additions & 8 deletions subprotocols/src/expression/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -325,13 +325,13 @@ impl Constant {

pub fn evaluate<E: ExtensionField>(&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<E: ExtensionField>(&self, challenges: &[E]) -> FieldType<E> {
fn evaluate_inner<E: ExtensionField>(&self, challenges: &[E]) -> ScalarType<E> {
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)
Expand All @@ -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)
)
}
}
Expand Down
10 changes: 5 additions & 5 deletions subprotocols/src/expression/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

0 comments on commit 87d1a30

Please sign in to comment.