Skip to content

Commit

Permalink
fixes and clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
fabrizio-m committed Oct 18, 2023
1 parent b1a6bc1 commit 8b9e4e0
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 49 deletions.
4 changes: 2 additions & 2 deletions kimchi/src/folding/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ pub(crate) fn eval_exp_error<'a, F: Field, E: FoldingEnv<F>>(
}
}

pub(crate) fn compute_error<'a, F: Field, E: FoldingEnv<F>>(
pub(crate) fn compute_error<F: Field, E: FoldingEnv<F>>(
exp: &IntegratedFoldingExpr<F>,
env: &'a E,
env: &E,
u: (F, F),
) -> Vec<F> {
let add = |a, b| EvalLeaf::bin_op(a, b, |a, b| *a + b, |a, b| *a += b);
Expand Down
12 changes: 8 additions & 4 deletions kimchi/src/folding/expressions.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use super::{CacheId, GateType};
use crate::circuits::{
expr::{Column, Expr, FeatureFlag, Op2, Variable},
gate::CurrOrNext,
};

use super::{CacheId, GateType};

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum FoldingColumn {
Witness(usize),
Expand Down Expand Up @@ -282,7 +281,10 @@ fn simplify_expression<C: Copy, F: Fn(&FeatureFlag) -> bool>(
Op2::Mul => FoldingExp::Mul(e1, e2),
}
}
Expr::UnnormalizedLagrangeBasis(i) => FoldingExp::UnnormalizedLagrangeBasis(*i),
Expr::UnnormalizedLagrangeBasis(i) => {
assert!(!i.zk_rows);
FoldingExp::UnnormalizedLagrangeBasis(i.offset)
}
Expr::Cache(id, e) => {
let e = simplify_expression(e, flag_resolver);
FoldingExp::Cache(*id, Box::new(e))
Expand All @@ -294,8 +296,10 @@ fn simplify_expression<C: Copy, F: Fn(&FeatureFlag) -> bool>(
simplify_expression(e2, flag_resolver)
}
}
Expr::VanishesOnLast4Rows | Expr::Pow(_, _) => {
Expr::Pow(_, _) => {
unreachable!()
}
//TODO: check that this is fine
Expr::VanishesOnZeroKnowledgeAndPreviousRows => todo!(),
}
}
54 changes: 27 additions & 27 deletions kimchi/src/folding/instance_witness.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{KimchiCurve, One, PolyComm};
pub trait InstanceTrait<G: KimchiCurve>: Sized {
use super::{CommitmentCurve, One, PolyComm};
pub trait InstanceTrait<G: CommitmentCurve>: Sized {
///should return a linear combination
fn combine(a: Self, b: Self, challenge: G::ScalarField) -> Self;
fn relax(self, zero_commit: PolyComm<G>) -> RelaxedInstance<G, Self> {
Expand All @@ -11,84 +11,84 @@ pub trait InstanceTrait<G: KimchiCurve>: Sized {
}
}
}
pub trait WitnessTrait<G: KimchiCurve>: Sized {
pub trait WitnessTrait<G: CommitmentCurve>: Sized {
fn witness(&self, i: usize) -> &Vec<G::ScalarField>;
///number of rows, or size of domain
fn len(&self) -> usize;
fn rows(&self) -> usize;
fn witness_ext(&self, i: usize) -> &Vec<G::ScalarField>;
///should return a linear combination
fn combine(a: Self, b: Self, challenge: G::ScalarField) -> Self;

fn relax(self, zero_poly: &Vec<G::ScalarField>) -> RelaxedWitness<G, Self> {
fn relax(self, zero_poly: &[G::ScalarField]) -> RelaxedWitness<G, Self> {
let witness = self;
RelaxedWitness {
witness,
error_vec: zero_poly.clone(),
error_vec: zero_poly.to_vec(),
}
}
}
pub struct RelaxedInstance<G: KimchiCurve, I: InstanceTrait<G>> {
pub struct RelaxedInstance<G: CommitmentCurve, I: InstanceTrait<G>> {
instance: I,
pub u: G::ScalarField,
error_commitment: PolyComm<G>,
}
trait Relaxable<G: KimchiCurve>: InstanceTrait<G> + Sized {
trait Relaxable<G: CommitmentCurve>: InstanceTrait<G> + Sized {
fn relax(
self,
zero_poly: &Vec<G::ScalarField>,
zero_poly: &[G::ScalarField],
zero_commit: PolyComm<G>,
) -> RelaxedInstance<G, Self>;
}

pub struct RelaxedWitness<G: KimchiCurve, W: WitnessTrait<G>> {
pub struct RelaxedWitness<G: CommitmentCurve, W: WitnessTrait<G>> {
pub witness: W,
pub error_vec: Vec<G::ScalarField>,
}

pub trait RelaxableInstance<G: KimchiCurve, I: InstanceTrait<G>> {
pub trait RelaxableInstance<G: CommitmentCurve, I: InstanceTrait<G>> {
fn relax(self, zero_commitment: PolyComm<G>) -> RelaxedInstance<G, I>;
}
impl<G: KimchiCurve, I: InstanceTrait<G>> RelaxableInstance<G, I> for I {
impl<G: CommitmentCurve, I: InstanceTrait<G>> RelaxableInstance<G, I> for I {
fn relax(self, zero_commitment: PolyComm<G>) -> RelaxedInstance<G, I> {
self.relax(zero_commitment)
}
}
impl<G: KimchiCurve, I: InstanceTrait<G>> RelaxableInstance<G, I> for RelaxedInstance<G, I> {
impl<G: CommitmentCurve, I: InstanceTrait<G>> RelaxableInstance<G, I> for RelaxedInstance<G, I> {
fn relax(self, _zero_commitment: PolyComm<G>) -> RelaxedInstance<G, I> {
self
}
}
pub trait RelaxableWitness<G: KimchiCurve, W: WitnessTrait<G>> {
fn relax(self, zero_poly: &Vec<G::ScalarField>) -> RelaxedWitness<G, W>;
pub trait RelaxableWitness<G: CommitmentCurve, W: WitnessTrait<G>> {
fn relax(self, zero_poly: &[G::ScalarField]) -> RelaxedWitness<G, W>;
}
impl<G: KimchiCurve, W: WitnessTrait<G>> RelaxableWitness<G, W> for W {
fn relax(self, zero_poly: &Vec<G::ScalarField>) -> RelaxedWitness<G, W> {
impl<G: CommitmentCurve, W: WitnessTrait<G>> RelaxableWitness<G, W> for W {
fn relax(self, zero_poly: &[G::ScalarField]) -> RelaxedWitness<G, W> {
self.relax(zero_poly)
}
}
impl<G: KimchiCurve, W: WitnessTrait<G>> RelaxableWitness<G, W> for RelaxedWitness<G, W> {
fn relax(self, _zero_poly: &Vec<G::ScalarField>) -> RelaxedWitness<G, W> {
impl<G: CommitmentCurve, W: WitnessTrait<G>> RelaxableWitness<G, W> for RelaxedWitness<G, W> {
fn relax(self, _zero_poly: &[G::ScalarField]) -> RelaxedWitness<G, W> {
self
}
}

// pub trait RelaxableInstance<G: KimchiCurve, >: seal::Seal<G, I, 0> {
pub trait RelaxablePair<G: KimchiCurve, I: InstanceTrait<G>, W: WitnessTrait<G>> {
pub trait RelaxablePair<G: CommitmentCurve, I: InstanceTrait<G>, W: WitnessTrait<G>> {
fn relax(
self,
zero_poly: &Vec<G::ScalarField>,
zero_poly: &[G::ScalarField],
zero_commitment: PolyComm<G>,
) -> (RelaxedInstance<G, I>, RelaxedWitness<G, W>);
}
impl<G, I, W> RelaxablePair<G, I, W> for (I, W)
where
G: KimchiCurve,
G: CommitmentCurve,
I: InstanceTrait<G> + RelaxableInstance<G, I>,
W: WitnessTrait<G> + RelaxableWitness<G, W>,
{
fn relax(
self,
zero_poly: &Vec<<G>::ScalarField>,
zero_poly: &[G::ScalarField],
zero_commitment: PolyComm<G>,
) -> (RelaxedInstance<G, I>, RelaxedWitness<G, W>) {
let (instance, witness) = self;
Expand All @@ -99,12 +99,12 @@ where
}
}

trait Pair2<G: KimchiCurve> {
trait Pair2<G: CommitmentCurve> {
type Instance: InstanceTrait<G>;
type Witness: WitnessTrait<G>;
}

impl<G: KimchiCurve, I: InstanceTrait<G>> RelaxedInstance<G, I> {
impl<G: CommitmentCurve, I: InstanceTrait<G>> RelaxedInstance<G, I> {
pub fn add_error(self, error_commitment: &PolyComm<G>, challenge: G::ScalarField) -> Self {
let RelaxedInstance {
instance,
Expand Down Expand Up @@ -142,7 +142,7 @@ impl<G: KimchiCurve, I: InstanceTrait<G>> RelaxedInstance<G, I> {
}
}

impl<G: KimchiCurve, W: WitnessTrait<G>> RelaxedWitness<G, W> {
impl<G: CommitmentCurve, W: WitnessTrait<G>> RelaxedWitness<G, W> {
pub fn add_error(mut self, error: Vec<G::ScalarField>, challenge: G::ScalarField) -> Self {
for (a, b) in self.error_vec.iter_mut().zip(error.into_iter()) {
*a += b * challenge;
Expand All @@ -162,7 +162,7 @@ impl<G: KimchiCurve, W: WitnessTrait<G>> RelaxedWitness<G, W> {
let challenge = challenge * challenge;
let witness = W::combine(a, b, challenge);
for (a, b) in e1.iter_mut().zip(e2.into_iter()) {
*a += b * &challenge;
*a += b * challenge;
}
let error_vec = e1;
RelaxedWitness { witness, error_vec }
Expand Down
30 changes: 14 additions & 16 deletions kimchi/src/folding/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use self::error::compute_error;
use crate::{
circuits::{
expr::{CacheId, Expr, FeatureFlag},
gate::GateType,
},
curve::KimchiCurve,
use crate::circuits::{
expr::{CacheId, Expr, FeatureFlag},
gate::GateType,
};
use ark_ff::{Field, One, Zero};
use ark_poly::{EvaluationDomain, Evaluations, Radix2EvaluationDomain};
use expressions::{folding_expression, IntegratedFoldingExpr};
pub use instance_witness::{InstanceTrait, RelaxedInstance, RelaxedWitness, WitnessTrait};
use instance_witness::{RelaxableInstance, RelaxablePair};
use poly_commitment::{srs::SRS, PolyComm};
use poly_commitment::PolyComm;
use poly_commitment::{commitment::CommitmentCurve, SRS};
use std::{collections::HashMap, marker::PhantomData, sync::Arc};

mod error;
Expand Down Expand Up @@ -94,7 +92,7 @@ impl<'a, F: Field> EvalLeaf<'a, F> {
}
struct Env<'a, G, I, W>
where
G: KimchiCurve,
G: CommitmentCurve,
I: InstanceTrait<G>,
W: WitnessTrait<G>,
{
Expand All @@ -107,7 +105,7 @@ where
}
impl<'a, G, I, W> FoldingEnv<G::ScalarField> for Env<'a, G, I, W>
where
G: KimchiCurve,
G: CommitmentCurve,
I: InstanceTrait<G>,
W: WitnessTrait<G>,
{
Expand Down Expand Up @@ -150,33 +148,33 @@ where
vec![zero; self.len]
}
}
pub trait Sponge<G: KimchiCurve> {
pub trait Sponge<G: CommitmentCurve> {
fn challenge(absorbe: &PolyComm<G>) -> G::ScalarField;
}

pub struct FoldingScheme<G: KimchiCurve, S: Sponge<G>> {
pub struct FoldingScheme<G: CommitmentCurve, S: Sponge<G>, C: SRS<G>> {
_sponge: PhantomData<S>,
// original_exp: Expr<ConstantExpr<G::ScalarField>>,
expression: IntegratedFoldingExpr<G::ScalarField>,
index: HashMap<GateType, Vec<G::ScalarField>>,
coefficients: Vec<Vec<G::ScalarField>>,
shift: Vec<G::ScalarField>,
srs: Arc<SRS<G>>,
srs: Arc<C>,
domain: Radix2EvaluationDomain<G::ScalarField>,
zero_commitment: PolyComm<G>,
zero_vec: Vec<G::ScalarField>,
}
impl<G: KimchiCurve, S: Sponge<G>> FoldingScheme<G, S> {
impl<G: CommitmentCurve, S: Sponge<G>, C: SRS<G>> FoldingScheme<G, S, C> {
pub fn new<F: Fn(&FeatureFlag) -> bool>(
expression: &Expr<G::ScalarField>,
index: HashMap<GateType, Vec<G::ScalarField>>,
coefficients: Vec<Vec<G::ScalarField>>,
srs: Arc<SRS<G>>,
srs: Arc<C>,
domain: Radix2EvaluationDomain<G::ScalarField>,
flag_resolver: &F,
) -> Self {
let expression = folding_expression(expression, flag_resolver);
let shift = domain.elements().into_iter().collect();
let shift = domain.elements().collect();
let zero: G::ScalarField = G::ScalarField::zero();
let evals = std::iter::repeat(zero).take(domain.size()).collect();
let zero_vec_evals = Evaluations::from_vec_and_domain(evals, domain);
Expand Down Expand Up @@ -209,7 +207,7 @@ impl<G: KimchiCurve, S: Sponge<G>> FoldingScheme<G, S> {
let a = a.relax(&self.zero_vec, self.zero_commitment.clone());
let b = b.relax(&self.zero_vec, self.zero_commitment.clone());

let len = a.1.witness.len();
let len = a.1.witness.rows();
let u = (a.0.u, b.0.u);

let env = Env {
Expand Down

0 comments on commit 8b9e4e0

Please sign in to comment.