Skip to content

Commit

Permalink
Expr.rs can be parametrized by challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
marcbeunardeau88 committed Sep 18, 2024
1 parent 512e663 commit 8c2afd1
Show file tree
Hide file tree
Showing 42 changed files with 557 additions and 317 deletions.
6 changes: 4 additions & 2 deletions book/src/specs/kimchi.md
Original file line number Diff line number Diff line change
Expand Up @@ -1716,7 +1716,8 @@ pub struct ProverIndex<G: KimchiCurve, OpeningProof: OpenProof<G>> {

/// The symbolic linearization of our circuit, which can compile to concrete types once certain values are learned in the protocol.
#[serde(skip)]
pub linearization: Linearization<Vec<PolishToken<G::ScalarField, Column>>, Column>,
pub linearization:
Linearization<Vec<PolishToken<G::ScalarField, Column, BerkeleyChallengeTerm>>, Column>,

/// The mapping between powers of alpha and constraints
#[serde(skip)]
Expand Down Expand Up @@ -1862,7 +1863,8 @@ pub struct VerifierIndex<G: KimchiCurve, OpeningProof: OpenProof<G>> {
pub lookup_index: Option<LookupVerifierIndex<G>>,

#[serde(skip)]
pub linearization: Linearization<Vec<PolishToken<G::ScalarField, Column>>, Column>,
pub linearization:
Linearization<Vec<PolishToken<G::ScalarField, Column, BerkeleyChallengeTerm>>, Column>,
/// The mapping between powers of alpha and constraints
#[serde(skip)]
pub powers_of_alpha: Alphas<G::ScalarField>,
Expand Down
41 changes: 24 additions & 17 deletions folding/src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,9 @@ use ark_ff::One;
use derivative::Derivative;
use itertools::Itertools;
use kimchi::circuits::{
expr::{ChallengeTerm, ConstantExprInner, ConstantTerm, ExprInner, Operations, Variable},
expr::{
BerkeleyChallengeTerm, ConstantExprInner, ConstantTerm, ExprInner, Operations, Variable,
},
gate::CurrOrNext,
};
use num_traits::Zero;
Expand Down Expand Up @@ -1001,12 +1003,13 @@ pub fn folding_expression<C: FoldingConfig>(

// CONVERSIONS FROM EXPR TO FOLDING COMPATIBLE EXPRESSIONS

impl<F, Config: FoldingConfig> From<ConstantExprInner<F>> for FoldingCompatibleExprInner<Config>
impl<F, Config: FoldingConfig> From<ConstantExprInner<F, BerkeleyChallengeTerm>>
for FoldingCompatibleExprInner<Config>
where
Config::Curve: AffineCurve<ScalarField = F>,
Config::Challenge: From<ChallengeTerm>,
Config::Challenge: From<BerkeleyChallengeTerm>,
{
fn from(expr: ConstantExprInner<F>) -> Self {
fn from(expr: ConstantExprInner<F, BerkeleyChallengeTerm>) -> Self {
match expr {
ConstantExprInner::Challenge(chal) => {
FoldingCompatibleExprInner::Challenge(chal.into())
Expand All @@ -1021,14 +1024,15 @@ where
}
}

impl<F, Col, Config: FoldingConfig<Column = Col>> From<ExprInner<ConstantExprInner<F>, Col>>
impl<F, Col, Config: FoldingConfig<Column = Col>>
From<ExprInner<ConstantExprInner<F, BerkeleyChallengeTerm>, Col>>
for FoldingCompatibleExprInner<Config>
where
Config::Curve: AffineCurve<ScalarField = F>,
Config::Challenge: From<ChallengeTerm>,
Config::Challenge: From<BerkeleyChallengeTerm>,
{
// TODO: check if this needs some special treatment for Extensions
fn from(expr: ExprInner<ConstantExprInner<F>, Col>) -> Self {
fn from(expr: ExprInner<ConstantExprInner<F, BerkeleyChallengeTerm>, Col>) -> Self {
match expr {
ExprInner::Constant(cexpr) => cexpr.into(),
ExprInner::Cell(col) => FoldingCompatibleExprInner::Cell(col),
Expand All @@ -1043,12 +1047,13 @@ where
}

impl<F, Col, Config: FoldingConfig<Column = Col>>
From<Operations<ExprInner<ConstantExprInner<F>, Col>>> for FoldingCompatibleExpr<Config>
From<Operations<ExprInner<ConstantExprInner<F, BerkeleyChallengeTerm>, Col>>>
for FoldingCompatibleExpr<Config>
where
Config::Curve: AffineCurve<ScalarField = F>,
Config::Challenge: From<ChallengeTerm>,
Config::Challenge: From<BerkeleyChallengeTerm>,
{
fn from(expr: Operations<ExprInner<ConstantExprInner<F>, Col>>) -> Self {
fn from(expr: Operations<ExprInner<ConstantExprInner<F, BerkeleyChallengeTerm>, Col>>) -> Self {
match expr {
Operations::Atom(inner) => FoldingCompatibleExpr::Atom(inner.into()),
Operations::Add(x, y) => {
Expand All @@ -1068,13 +1073,13 @@ where
}
}

impl<F, Col, Config: FoldingConfig<Column = Col>> From<Operations<ConstantExprInner<F>>>
for FoldingCompatibleExpr<Config>
impl<F, Col, Config: FoldingConfig<Column = Col>>
From<Operations<ConstantExprInner<F, BerkeleyChallengeTerm>>> for FoldingCompatibleExpr<Config>
where
Config::Curve: AffineCurve<ScalarField = F>,
Config::Challenge: From<ChallengeTerm>,
Config::Challenge: From<BerkeleyChallengeTerm>,
{
fn from(expr: Operations<ConstantExprInner<F>>) -> Self {
fn from(expr: Operations<ConstantExprInner<F, BerkeleyChallengeTerm>>) -> Self {
match expr {
Operations::Add(x, y) => {
FoldingCompatibleExpr::Add(Box::new((*x).into()), Box::new((*y).into()))
Expand All @@ -1094,13 +1099,15 @@ where
}

impl<F, Col, Config: FoldingConfig<Column = Col>>
From<Operations<ExprInner<Operations<ConstantExprInner<F>>, Col>>>
From<Operations<ExprInner<Operations<ConstantExprInner<F, BerkeleyChallengeTerm>>, Col>>>
for FoldingCompatibleExpr<Config>
where
Config::Curve: AffineCurve<ScalarField = F>,
Config::Challenge: From<ChallengeTerm>,
Config::Challenge: From<BerkeleyChallengeTerm>,
{
fn from(expr: Operations<ExprInner<Operations<ConstantExprInner<F>>, Col>>) -> Self {
fn from(
expr: Operations<ExprInner<Operations<ConstantExprInner<F, BerkeleyChallengeTerm>>, Col>>,
) -> Self {
match expr {
Operations::Atom(inner) => match inner {
ExprInner::Constant(op) => match op {
Expand Down
14 changes: 7 additions & 7 deletions ivc/src/plonkish_lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ark_ff::{FftField, Field, One};
use ark_poly::{Evaluations, Radix2EvaluationDomain as R2D};
use folding::{instance_witness::Foldable, Alphas, Instance, Witness};
use itertools::Itertools;
use kimchi::{self, circuits::expr::ChallengeTerm, curve::KimchiCurve};
use kimchi::{self, circuits::expr::BerkeleyChallengeTerm, curve::KimchiCurve};
use kimchi_msm::{columns::Column, witness::Witness as GenericWitness};
use mina_poseidon::FqSponge;
use poly_commitment::{
Expand Down Expand Up @@ -241,13 +241,13 @@ pub enum PlonkishChallenge {
JointCombiner,
}

impl From<ChallengeTerm> for PlonkishChallenge {
fn from(chal: ChallengeTerm) -> Self {
impl From<BerkeleyChallengeTerm> for PlonkishChallenge {
fn from(chal: BerkeleyChallengeTerm) -> Self {
match chal {
ChallengeTerm::Beta => PlonkishChallenge::Beta,
ChallengeTerm::Gamma => PlonkishChallenge::Gamma,
ChallengeTerm::JointCombiner => PlonkishChallenge::JointCombiner,
ChallengeTerm::Alpha => panic!("Alpha not allowed in folding expressions"),
BerkeleyChallengeTerm::Beta => PlonkishChallenge::Beta,
BerkeleyChallengeTerm::Gamma => PlonkishChallenge::Gamma,
BerkeleyChallengeTerm::JointCombiner => PlonkishChallenge::JointCombiner,
BerkeleyChallengeTerm::Alpha => panic!("Alpha not allowed in folding expressions"),
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions ivc/tests/folding_ivc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use folding::{
FoldingScheme, Instance, Side, Witness,
};
use ivc::ivc::{constraints::constrain_ivc, lookups::IVCLookupTable, N_ADDITIONAL_WIT_COL_QUAD};
use kimchi::circuits::{expr::ChallengeTerm, gate::CurrOrNext};
use kimchi::circuits::{expr::BerkeleyChallengeTerm, gate::CurrOrNext};
use kimchi_msm::{circuit_design::ConstraintBuilderEnv, columns::Column, Ff1};
use poly_commitment::srs::SRS;

Expand All @@ -24,13 +24,13 @@ fn test_regression_additional_columns_reduction_to_degree_2() {
JointCombiner,
}

impl From<ChallengeTerm> for Challenge {
fn from(chal: ChallengeTerm) -> Self {
impl From<BerkeleyChallengeTerm> for Challenge {
fn from(chal: BerkeleyChallengeTerm) -> Self {
match chal {
ChallengeTerm::Beta => Challenge::Beta,
ChallengeTerm::Gamma => Challenge::Gamma,
ChallengeTerm::JointCombiner => Challenge::JointCombiner,
ChallengeTerm::Alpha => panic!("Alpha not allowed in folding expressions"),
BerkeleyChallengeTerm::Beta => Challenge::Beta,
BerkeleyChallengeTerm::Gamma => Challenge::Gamma,
BerkeleyChallengeTerm::JointCombiner => Challenge::JointCombiner,
BerkeleyChallengeTerm::Alpha => panic!("Alpha not allowed in folding expressions"),
}
}
}
Expand Down
19 changes: 13 additions & 6 deletions kimchi/src/circuits/argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ use crate::{alphas::Alphas, circuits::expr::prologue::*};
use ark_ff::{Field, PrimeField};
use serde::{Deserialize, Serialize};

//TODO use generic challenge
use super::{
expr::{constraints::ExprOps, Cache, Challenges, ConstantExpr, ConstantTerm, Constants},
expr::{
constraints::ExprOps, BerkeleyChallengeTerm, BerkeleyChallenges, Cache, ConstantExpr,
ConstantTerm, Constants,
},
gate::{CurrOrNext, GateType},
polynomial::COLUMNS,
};
Expand Down Expand Up @@ -53,14 +57,14 @@ impl<F, T> Default for ArgumentEnv<F, T> {
}
}

impl<F: Field, T: ExprOps<F>> ArgumentEnv<F, T> {
impl<F: Field, T: ExprOps<F, BerkeleyChallengeTerm>> ArgumentEnv<F, T> {
/// Initialize the environment for creating constraints of real field elements that can be
/// evaluated directly over the witness without the prover/verifier
pub fn create(
witness: ArgumentWitness<F>,
coeffs: Vec<F>,
constants: Constants<F>,
challenges: Challenges<F>,
challenges: BerkeleyChallenges<F>,
) -> Self {
ArgumentEnv {
data: Some(ArgumentData {
Expand Down Expand Up @@ -121,7 +125,7 @@ impl<F: Field, T: ExprOps<F>> ArgumentEnv<F, T> {
}

/// Constant value (see [ConstantExpr] for supported constants)
pub fn constant(&self, expr: ConstantExpr<F>) -> T {
pub fn constant(&self, expr: ConstantExpr<F, BerkeleyChallengeTerm>) -> T {
T::constant(expr, self.data.as_ref())
}

Expand Down Expand Up @@ -150,7 +154,7 @@ pub struct ArgumentData<F: 'static> {
pub coeffs: Vec<F>,
/// Constants
pub constants: Constants<F>,
pub challenges: Challenges<F>,
pub challenges: BerkeleyChallenges<F>,
}

/// Witness data for a argument
Expand Down Expand Up @@ -183,7 +187,10 @@ pub trait Argument<F: PrimeField> {
const CONSTRAINTS: u32;

/// Constraints for this argument
fn constraint_checks<T: ExprOps<F>>(env: &ArgumentEnv<F, T>, cache: &mut Cache) -> Vec<T>;
fn constraint_checks<T: ExprOps<F, BerkeleyChallengeTerm>>(
env: &ArgumentEnv<F, T>,
cache: &mut Cache,
) -> Vec<T>;

/// Returns the set of constraints required to prove this argument.
fn constraints(cache: &mut Cache) -> Vec<E<F>> {
Expand Down
16 changes: 11 additions & 5 deletions kimchi/src/circuits/berkeley_columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ use serde::{Deserialize, Serialize};
use ark_ff::FftField;
use ark_poly::{Evaluations, Radix2EvaluationDomain as D};

use crate::circuits::expr::{Challenges, ColumnEnvironment, Constants, Domain, FormattedOutput};
use crate::circuits::expr::{
BerkeleyChallenges, ColumnEnvironment, Constants, Domain, FormattedOutput,
};

use crate::circuits::wires::COLUMNS;

use std::collections::HashMap;

use super::expr::BerkeleyChallengeTerm;

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
/// A type representing one of the polynomials involved in the PLONK IOP, use in
/// the Berkeley hardfork.
Expand Down Expand Up @@ -146,7 +150,9 @@ impl<F: Copy> ColumnEvaluations<F> for ProofEvaluations<PointEvaluations<F>> {
}
}

impl<'a, F: FftField> ColumnEnvironment<'a, F> for Environment<'a, F> {
impl<'a, F: FftField> ColumnEnvironment<'a, F, BerkeleyChallengeTerm, BerkeleyChallenges<F>>
for Environment<'a, F>
{
type Column = Column;

fn get_column(&self, col: &Self::Column) -> Option<&'a Evaluations<F, D<F>>> {
Expand Down Expand Up @@ -191,7 +197,7 @@ impl<'a, F: FftField> ColumnEnvironment<'a, F> for Environment<'a, F> {
&self.constants
}

fn get_challenges(&self) -> &Challenges<F> {
fn get_challenges(&self) -> &BerkeleyChallenges<F> {
&self.challenges
}

Expand Down Expand Up @@ -243,7 +249,7 @@ pub struct Environment<'a, F: FftField> {
/// Constant values required
pub constants: Constants<F>,
/// Challenges from the IOP.
pub challenges: Challenges<F>,
pub challenges: BerkeleyChallenges<F>,
/// The domains used in the PLONK argument.
pub domain: EvaluationDomains<F>,
/// Lookup specific polynomials
Expand All @@ -255,7 +261,7 @@ pub struct Environment<'a, F: FftField> {
//

/// An alias for the intended usage of the expression type in constructing constraints.
pub type E<F> = Expr<ConstantExpr<F>, Column>;
pub type E<F> = Expr<ConstantExpr<F, BerkeleyChallengeTerm>, Column>;

/// Convenience function to create a constant as [Expr].
pub fn constant<F>(x: F) -> E<F> {
Expand Down
Loading

0 comments on commit 8c2afd1

Please sign in to comment.