From afc83079a3f1dd8d3da59abd7c8fea012262919f Mon Sep 17 00:00:00 2001 From: Danny Willems Date: Wed, 12 Feb 2025 19:42:07 +0100 Subject: [PATCH] Arrabbiata: move challenge semantic into its own module Simple move, only as the code is larger than initially, and should not be related to the column module. --- arrabbiata/src/challenge.rs | 114 +++++++++++++++++++++++++++++++++++ arrabbiata/src/columns.rs | 116 +----------------------------------- arrabbiata/src/lib.rs | 1 + arrabbiata/src/witness.rs | 3 +- 4 files changed, 120 insertions(+), 114 deletions(-) create mode 100644 arrabbiata/src/challenge.rs diff --git a/arrabbiata/src/challenge.rs b/arrabbiata/src/challenge.rs new file mode 100644 index 0000000000..1095349474 --- /dev/null +++ b/arrabbiata/src/challenge.rs @@ -0,0 +1,114 @@ +use ark_ff::{Field, Zero}; +use core::{ + fmt::{Display, Formatter, Result}, + ops::Index, +}; +use kimchi::circuits::expr::AlphaChallengeTerm; +use serde::{Deserialize, Serialize}; +use strum::EnumCount; +use strum_macros::EnumCount as EnumCountMacro; + +#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, EnumCountMacro)] +pub enum ChallengeTerm { + /// Used to aggregate the constraints describing the relation. It is used to + /// enforce all constraints are satisfied at the same time. + /// Often noted `α`. + ConstraintRandomiser, + /// Both challenges used in the permutation argument + Beta, + Gamma, + /// Used to homogenize the constraints and allow the protocol to fold two + /// instances of the same relation into a new one. + /// Often noted `u` in the paper mentioning "folding protocols". + ConstraintHomogeniser, + /// Used by the accumulation protocol (folding) to perform a random linear + /// transformation of the witnesses and the public values. + /// Often noted `r` in the paper mentioning "folding protocols". + RelationRandomiser, +} + +impl Display for ChallengeTerm { + fn fmt(&self, f: &mut Formatter) -> Result { + match self { + ChallengeTerm::ConstraintRandomiser => write!(f, "alpha"), + ChallengeTerm::Beta => write!(f, "beta"), + ChallengeTerm::Gamma => write!(f, "gamma"), + ChallengeTerm::ConstraintHomogeniser => write!(f, "u"), + ChallengeTerm::RelationRandomiser => write!(f, "r"), + } + } +} + +pub struct Challenges { + /// Used to aggregate the constraints describing the relation. It is used to + /// enforce all constraints are satisfied at the same time. + /// Often noted `α`. + pub constraint_randomiser: F, + + /// Both challenges used in the permutation argument. + pub beta: F, + pub gamma: F, + + /// Used to homogenize the constraints and allow the protocol to fold two + /// instances of the same relation into a new one. + /// Often noted `u` in the paper mentioning "folding protocols". + pub constraint_homogeniser: F, + + /// Used by the accumulation protocol (folding) to perform a random linear + /// transformation of the witnesses and the public values. + /// Often noted `r` in the paper mentioning "folding protocols". + pub relation_randomiser: F, +} + +impl Index for Challenges { + type Output = F; + + fn index(&self, index: usize) -> &Self::Output { + if index == 0 { + &self.constraint_randomiser + } else if index == 1 { + &self.beta + } else if index == 2 { + &self.gamma + } else if index == 3 { + &self.constraint_homogeniser + } else if index == 4 { + &self.relation_randomiser + } else { + panic!( + "Index out of bounds, only {} are defined", + ChallengeTerm::COUNT + ) + } + } +} + +impl Default for Challenges { + fn default() -> Self { + Self { + constraint_randomiser: F::zero(), + beta: F::zero(), + gamma: F::zero(), + constraint_homogeniser: F::zero(), + relation_randomiser: F::zero(), + } + } +} + +impl Index for Challenges { + type Output = F; + + fn index(&self, term: ChallengeTerm) -> &Self::Output { + match term { + ChallengeTerm::ConstraintRandomiser => &self.constraint_randomiser, + ChallengeTerm::Beta => &self.beta, + ChallengeTerm::Gamma => &self.gamma, + ChallengeTerm::ConstraintHomogeniser => &self.constraint_homogeniser, + ChallengeTerm::RelationRandomiser => &self.relation_randomiser, + } + } +} + +impl<'a> AlphaChallengeTerm<'a> for ChallengeTerm { + const ALPHA: Self = Self::ConstraintRandomiser; +} diff --git a/arrabbiata/src/columns.rs b/arrabbiata/src/columns.rs index 9fad02ca5f..51192e79b6 100644 --- a/arrabbiata/src/columns.rs +++ b/arrabbiata/src/columns.rs @@ -1,12 +1,6 @@ -use ark_ff::{Field, Zero}; -use kimchi::circuits::expr::{AlphaChallengeTerm, CacheId, ConstantExpr, Expr, FormattedOutput}; -use serde::{Deserialize, Serialize}; -use std::{ - collections::HashMap, - fmt::{Display, Formatter, Result}, - ops::Index, -}; -use strum::EnumCount; +use crate::challenge::ChallengeTerm; +use kimchi::circuits::expr::{CacheId, ConstantExpr, Expr, FormattedOutput}; +use std::collections::HashMap; use strum_macros::{EnumCount as EnumCountMacro, EnumIter}; use crate::NUMBER_OF_COLUMNS; @@ -60,110 +54,6 @@ impl From for usize { } } -pub struct Challenges { - /// Used to aggregate the constraints describing the relation. It is used to - /// enforce all constraints are satisfied at the same time. - /// Often noted `α`. - pub constraint_randomiser: F, - - /// Both challenges used in the permutation argument. - pub beta: F, - pub gamma: F, - - /// Used to homogenize the constraints and allow the protocol to fold two - /// instances of the same relation into a new one. - /// Often noted `u` in the paper mentioning "folding protocols". - pub constraint_homogeniser: F, - - /// Used by the accumulation protocol (folding) to perform a random linear - /// transformation of the witnesses and the public values. - /// Often noted `r` in the paper mentioning "folding protocols". - pub relation_randomiser: F, -} - -impl Index for Challenges { - type Output = F; - - fn index(&self, index: usize) -> &Self::Output { - if index == 0 { - &self.constraint_randomiser - } else if index == 1 { - &self.beta - } else if index == 2 { - &self.gamma - } else if index == 3 { - &self.constraint_homogeniser - } else if index == 4 { - &self.relation_randomiser - } else { - panic!( - "Index out of bounds, only {} are defined", - ChallengeTerm::COUNT - ) - } - } -} - -impl Default for Challenges { - fn default() -> Self { - Self { - constraint_randomiser: F::zero(), - beta: F::zero(), - gamma: F::zero(), - constraint_homogeniser: F::zero(), - relation_randomiser: F::zero(), - } - } -} - -#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, EnumCountMacro)] -pub enum ChallengeTerm { - /// Used to aggregate the constraints describing the relation. It is used to - /// enforce all constraints are satisfied at the same time. - /// Often noted `α`. - ConstraintRandomiser, - /// Both challenges used in the permutation argument - Beta, - Gamma, - /// Used to homogenize the constraints and allow the protocol to fold two - /// instances of the same relation into a new one. - /// Often noted `u` in the paper mentioning "folding protocols". - ConstraintHomogeniser, - /// Used by the accumulation protocol (folding) to perform a random linear - /// transformation of the witnesses and the public values. - /// Often noted `r` in the paper mentioning "folding protocols". - RelationRandomiser, -} - -impl Display for ChallengeTerm { - fn fmt(&self, f: &mut Formatter) -> Result { - match self { - ChallengeTerm::ConstraintRandomiser => write!(f, "alpha"), - ChallengeTerm::Beta => write!(f, "beta"), - ChallengeTerm::Gamma => write!(f, "gamma"), - ChallengeTerm::ConstraintHomogeniser => write!(f, "u"), - ChallengeTerm::RelationRandomiser => write!(f, "r"), - } - } -} -impl Index for Challenges { - type Output = F; - - fn index(&self, term: ChallengeTerm) -> &Self::Output { - match term { - ChallengeTerm::ConstraintRandomiser => &self.constraint_randomiser, - ChallengeTerm::Beta => &self.beta, - ChallengeTerm::Gamma => &self.gamma, - ChallengeTerm::ConstraintHomogeniser => &self.constraint_homogeniser, - ChallengeTerm::RelationRandomiser => &self.relation_randomiser, - } - } -} - -impl<'a> AlphaChallengeTerm<'a> for ChallengeTerm { - const ALPHA: Self = Self::ConstraintRandomiser; -} - pub type E = Expr, Column>; // Code to allow for pretty printing of the expressions diff --git a/arrabbiata/src/lib.rs b/arrabbiata/src/lib.rs index 91902f7846..c954ad7715 100644 --- a/arrabbiata/src/lib.rs +++ b/arrabbiata/src/lib.rs @@ -2,6 +2,7 @@ use curve::PlonkSpongeConstants; use mina_poseidon::constants::SpongeConstants; use strum::EnumCount as _; +pub mod challenge; pub mod column_env; pub mod columns; pub mod constraints; diff --git a/arrabbiata/src/witness.rs b/arrabbiata/src/witness.rs index 428b4d1461..b8faaddd70 100644 --- a/arrabbiata/src/witness.rs +++ b/arrabbiata/src/witness.rs @@ -12,7 +12,8 @@ use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; use std::time::Instant; use crate::{ - columns::{Challenges, Column, Gadget}, + challenge::Challenges, + columns::{Column, Gadget}, curve::{ArrabbiataCurve, PlonkSpongeConstants}, interpreter::{Instruction, InterpreterEnv, Side}, MAXIMUM_FIELD_SIZE_IN_BITS, NUMBER_OF_COLUMNS, NUMBER_OF_PUBLIC_INPUTS, NUMBER_OF_SELECTORS,