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    #[error("ark_relations::r1cs::SynthesisError")]
 25    SynthesisError(#[from] ark_relations::r1cs::SynthesisError),
 26    #[error("ark_serialize::SerializationError")]
 27    SerializationError(#[from] ark_serialize::SerializationError),
 28    #[error("ark_poly_commit::Error")]
 29    PolyCommitError(#[from] ark_poly_commit::Error),
 30    #[error("crate::utils::espresso::virtual_polynomial::ArithErrors")]
 31    ArithError(#[from] utils::espresso::virtual_polynomial::ArithErrors),
 32    #[error("{0}")]
 33    Other(String),
 34
 35    #[error("Relation not satisfied")]
 36    NotSatisfied,
 37    #[error("Not equal")]
 38    NotEqual,
 39    #[error("Vectors should have the same length ({0}: {1}, {2}: {3})")]
 40    NotSameLength(String, usize, String, usize),
 41    #[error("Vector's length ({0}) is not the expected ({1})")]
 42    NotExpectedLength(usize, usize),
 43    #[error("Vector ({0}) length ({1}) is not a power of two")]
 44    NotPowerOfTwo(String, usize),
 45    #[error("Can not be empty")]
 46    Empty,
 47    #[error("Pedersen parameters length is not sufficient (generators.len={0} < vector.len={1} unsatisfied)")]
 48    PedersenParamsLen(usize, usize),
 49    #[error("Randomness for blinding not found")]
 50    MissingRandomness,
 51    #[error("Commitment verification failed")]
 52    CommitmentVerificationFail,
 53    #[error("IVC verification failed")]
 54    IVCVerificationFail,
 55    #[error("R1CS instance is expected to not be relaxed")]
 56    R1CSUnrelaxedFail,
 57    #[error("Could not find the inner ConstraintSystem")]
 58    NoInnerConstraintSystem,
 59    #[error("Sum-check prove failed: {0}")]
 60    SumCheckProveError(String),
 61    #[error("Sum-check verify failed: {0}")]
 62    SumCheckVerifyError(String),
 63    #[error("Value out of bounds")]
 64    OutOfBounds,
 65    #[error("Could not construct the Evaluation Domain")]
 66    NewDomainFail,
 67    #[error("Feature '{0}' not supported yet")]
 68    NotSupportedYet(String),
 69
 70    #[error(transparent)]
 71    ProtoGalaxy(folding::protogalaxy::ProtoGalaxyError),
 72}
 73
 74/// FoldingScheme defines trait that is implemented by the diverse folding schemes. It is defined
 75/// over a cycle of curves (C1, C2), where:
 76/// - C1 is the main curve, which ScalarField we use as our F for al the field operations
 77/// - C2 is the auxiliary curve, which we use for the commitments, whose BaseField (for point
 78/// coordinates) are in the C1::ScalarField.
 79/// In other words, C1.Fq == C2.Fr, and C1.Fr == C2.Fq.
 80pub trait FoldingScheme<C1: CurveGroup, C2: CurveGroup, FC>: Clone + Debug
 81where
 82    C1: CurveGroup<BaseField = C2::ScalarField, ScalarField = C2::BaseField>,
 83    C2::BaseField: PrimeField,
 84    FC: FCircuit<C1::ScalarField>,
 85{
 86    type PreprocessorParam: Debug;
 87    type ProverParam: Debug;
 88    type VerifierParam: Debug;
 89    type CommittedInstanceWithWitness: Debug;
 90    type CFCommittedInstanceWithWitness: Debug; // CycleFold CommittedInstance & Witness
 91
 92    fn preprocess(
 93        prep_param: &Self::PreprocessorParam,
 94    ) -> Result<(Self::ProverParam, Self::VerifierParam), Error>;
 95
 96    fn init(
 97        pp: &Self::ProverParam,
 98        step_circuit: FC,
 99        z_0: Vec<C1::ScalarField>, // initial state
100    ) -> Result<Self, Error>;
101
102    fn prove_step(&mut self) -> Result<(), Error>;
103
104    // returns the state at the current step
105    fn state(&self) -> Vec<C1::ScalarField>;
106
107    // returns the instances at the current step
108    fn instances(
109        &self,
110    ) -> (
111        Self::CommittedInstanceWithWitness,
112        Self::CommittedInstanceWithWitness,
113        Self::CFCommittedInstanceWithWitness,
114    );
115
116    fn verify(
117        vp: Self::VerifierParam,
118        z_0: Vec<C1::ScalarField>, // initial state
119        z_i: Vec<C1::ScalarField>, // last state
120        // number of steps between the initial state and the last state
121        num_steps: C1::ScalarField,
122        running_instance: Self::CommittedInstanceWithWitness,
123        incoming_instance: Self::CommittedInstanceWithWitness,
124        cyclefold_instance: Self::CFCommittedInstanceWithWitness,
125    ) -> Result<(), Error>;
126}
127
128pub trait Decider<
129    C1: CurveGroup,
130    C2: CurveGroup,
131    FC: FCircuit<C1::ScalarField>,
132    FS: FoldingScheme<C1, C2, FC>,
133> where
134    C1: CurveGroup<BaseField = C2::ScalarField, ScalarField = C2::BaseField>,
135    C2::BaseField: PrimeField,
136{
137    type ProverParam: Clone;
138    type Proof: Clone;
139    type VerifierParam;
140    type PublicInput: Debug;
141    type CommittedInstanceWithWitness: Debug;
142    type CommittedInstance: Clone + Debug;
143
144    fn prove(
145        pp: &Self::ProverParam,
146        rng: impl RngCore + CryptoRng,
147        folding_scheme: FS,
148    ) -> Result<Self::Proof, Error>;
149
150    fn verify(
151        vp: &Self::VerifierParam,
152        i: C1::ScalarField,
153        z_0: Vec<C1::ScalarField>,
154        z_i: Vec<C1::ScalarField>,
155        running_instance: &Self::CommittedInstance,
156        proof: Self::Proof,
157        // returns `Result<bool, Error>` to differentiate between an error occurred while performing
158        // the verification steps, and the verification logic of the scheme not passing.
159    ) -> Result<bool, Error>;
160}