arnaucube
arnaucube/sonobePublic Self-hosted repository
sonobe/folding-schemes/src/lib.rs
folding-schemes/src/lib.rsRaw
  1#![allow(non_snake_case)]
  2#![allow(non_upper_case_globals)]
  3#![allow(non_camel_case_types)]
  4#![allow(clippy::upper_case_acronyms)]
  5
  6use ark_ec::CurveGroup;
  7use ark_ff::PrimeField;
  8use ark_std::rand::CryptoRng;
  9use ark_std::{fmt::Debug, rand::RngCore};
 10use thiserror::Error;
 11
 12use crate::frontend::FCircuit;
 13
 14pub mod ccs;
 15pub mod commitment;
 16pub mod constants;
 17pub mod folding;
 18pub mod frontend;
 19pub mod transcript;
 20pub mod utils;
 21
 22#[derive(Debug, Error)]
 23pub enum Error {
 24    // Wrappers on top of other errors
 25    #[error("ark_relations::r1cs::SynthesisError")]
 26    SynthesisError(#[from] ark_relations::r1cs::SynthesisError),
 27    #[error("ark_serialize::SerializationError")]
 28    SerializationError(#[from] ark_serialize::SerializationError),
 29    #[error("ark_poly_commit::Error")]
 30    PolyCommitError(#[from] ark_poly_commit::Error),
 31    #[error("crate::utils::espresso::virtual_polynomial::ArithErrors")]
 32    ArithError(#[from] utils::espresso::virtual_polynomial::ArithErrors),
 33    #[error(transparent)]
 34    ProtoGalaxy(folding::protogalaxy::ProtoGalaxyError),
 35    #[error("std::io::Error")]
 36    IOError(#[from] std::io::Error),
 37    #[error("{0}")]
 38    Other(String),
 39
 40    // Relation errors
 41    #[error("Relation not satisfied")]
 42    NotSatisfied,
 43    #[error("SNARK verification failed")]
 44    SNARKVerificationFail,
 45    #[error("IVC verification failed")]
 46    IVCVerificationFail,
 47    #[error("R1CS instance is expected to not be relaxed")]
 48    R1CSUnrelaxedFail,
 49    #[error("Could not find the inner ConstraintSystem")]
 50    NoInnerConstraintSystem,
 51    #[error("Sum-check prove failed: {0}")]
 52    SumCheckProveError(String),
 53    #[error("Sum-check verify failed: {0}")]
 54    SumCheckVerifyError(String),
 55
 56    // Comparators errors
 57    #[error("Not equal")]
 58    NotEqual,
 59    #[error("Vectors should have the same length ({0}: {1}, {2}: {3})")]
 60    NotSameLength(String, usize, String, usize),
 61    #[error("Vector's length ({0}) is not the expected ({1})")]
 62    NotExpectedLength(usize, usize),
 63    #[error("Vector ({0}) length ({1}) is not a power of two")]
 64    NotPowerOfTwo(String, usize),
 65    #[error("Can not be empty")]
 66    Empty,
 67    #[error("Value out of bounds")]
 68    OutOfBounds,
 69    #[error("Could not construct the Evaluation Domain")]
 70    NewDomainFail,
 71
 72    // Commitment errors
 73    #[error("Pedersen parameters length is not sufficient (generators.len={0} < vector.len={1} unsatisfied)")]
 74    PedersenParamsLen(usize, usize),
 75    #[error("Blinding factor not 0 for Commitment without hiding")]
 76    BlindingNotZero,
 77    #[error("Commitment verification failed")]
 78    CommitmentVerificationFail,
 79
 80    // Other
 81    #[error("Randomness for blinding not found")]
 82    MissingRandomness,
 83    #[error("Missing value: {0}")]
 84    MissingValue(String),
 85    #[error("Feature '{0}' not supported yet")]
 86    NotSupportedYet(String),
 87    #[error("Feature '{0}' is not supported and it will not be")]
 88    NotSupported(String),
 89    #[error("max i-th step reached (usize limit reached)")]
 90    MaxStep,
 91    #[error("Circom Witness calculation error: {0}")]
 92    WitnessCalculationError(String),
 93    #[error("BigInt to PrimeField conversion error: {0}")]
 94    BigIntConversionError(String),
 95}
 96
 97/// FoldingScheme defines trait that is implemented by the diverse folding schemes. It is defined
 98/// over a cycle of curves (C1, C2), where:
 99/// - C1 is the main curve, which ScalarField we use as our F for al the field operations
100/// - C2 is the auxiliary curve, which we use for the commitments, whose BaseField (for point
101/// coordinates) are in the C1::ScalarField.
102/// In other words, C1.Fq == C2.Fr, and C1.Fr == C2.Fq.
103pub trait FoldingScheme<C1: CurveGroup, C2: CurveGroup, FC>: Clone + Debug
104where
105    C1: CurveGroup<BaseField = C2::ScalarField, ScalarField = C2::BaseField>,
106    C2::BaseField: PrimeField,
107    FC: FCircuit<C1::ScalarField>,
108{
109    type PreprocessorParam: Debug;
110    type ProverParam: Debug;
111    type VerifierParam: Debug;
112    type CommittedInstanceWithWitness: Debug;
113    type CFCommittedInstanceWithWitness: Debug; // CycleFold CommittedInstance & Witness
114
115    fn preprocess(
116        prep_param: &Self::PreprocessorParam,
117    ) -> Result<(Self::ProverParam, Self::VerifierParam), Error>;
118
119    fn init(
120        pp: &Self::ProverParam,
121        step_circuit: FC,
122        z_0: Vec<C1::ScalarField>, // initial state
123    ) -> Result<Self, Error>;
124
125    fn prove_step(&mut self) -> Result<(), Error>;
126
127    // returns the state at the current step
128    fn state(&self) -> Vec<C1::ScalarField>;
129
130    // returns the instances at the current step, in the following order:
131    // (running_instance, incoming_instance, cyclefold_instance)
132    fn instances(
133        &self,
134    ) -> (
135        Self::CommittedInstanceWithWitness,
136        Self::CommittedInstanceWithWitness,
137        Self::CFCommittedInstanceWithWitness,
138    );
139
140    fn verify(
141        vp: Self::VerifierParam,
142        z_0: Vec<C1::ScalarField>, // initial state
143        z_i: Vec<C1::ScalarField>, // last state
144        // number of steps between the initial state and the last state
145        num_steps: C1::ScalarField,
146        running_instance: Self::CommittedInstanceWithWitness,
147        incoming_instance: Self::CommittedInstanceWithWitness,
148        cyclefold_instance: Self::CFCommittedInstanceWithWitness,
149    ) -> Result<(), Error>;
150}
151
152pub trait Decider<
153    C1: CurveGroup,
154    C2: CurveGroup,
155    FC: FCircuit<C1::ScalarField>,
156    FS: FoldingScheme<C1, C2, FC>,
157> where
158    C1: CurveGroup<BaseField = C2::ScalarField, ScalarField = C2::BaseField>,
159    C2::BaseField: PrimeField,
160{
161    type ProverParam: Clone;
162    type Proof;
163    type VerifierParam;
164    type PublicInput: Debug;
165    type CommittedInstanceWithWitness: Debug;
166    type CommittedInstance: Clone + Debug;
167
168    fn prove(
169        pp: Self::ProverParam,
170        rng: impl RngCore + CryptoRng,
171        folding_scheme: FS,
172    ) -> Result<Self::Proof, Error>;
173
174    fn verify(
175        vp: Self::VerifierParam,
176        i: C1::ScalarField,
177        z_0: Vec<C1::ScalarField>,
178        z_i: Vec<C1::ScalarField>,
179        running_instance: &Self::CommittedInstance,
180        incoming_instance: &Self::CommittedInstance,
181        proof: Self::Proof,
182        // returns `Result<bool, Error>` to differentiate between an error occurred while performing
183        // the verification steps, and the verification logic of the scheme not passing.
184    ) -> Result<bool, Error>;
185}