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    #[error("max i-th step reached (usize limit reached)")]
 70    MaxStep,
 71
 72    #[error(transparent)]
 73    ProtoGalaxy(folding::protogalaxy::ProtoGalaxyError),
 74}
 75
 76/// FoldingScheme defines trait that is implemented by the diverse folding schemes. It is defined
 77/// over a cycle of curves (C1, C2), where:
 78/// - C1 is the main curve, which ScalarField we use as our F for al the field operations
 79/// - C2 is the auxiliary curve, which we use for the commitments, whose BaseField (for point
 80/// coordinates) are in the C1::ScalarField.
 81/// In other words, C1.Fq == C2.Fr, and C1.Fr == C2.Fq.
 82pub trait FoldingScheme<C1: CurveGroup, C2: CurveGroup, FC>: Clone + Debug
 83where
 84    C1: CurveGroup<BaseField = C2::ScalarField, ScalarField = C2::BaseField>,
 85    C2::BaseField: PrimeField,
 86    FC: FCircuit<C1::ScalarField>,
 87{
 88    type PreprocessorParam: Debug;
 89    type ProverParam: Debug;
 90    type VerifierParam: Debug;
 91    type CommittedInstanceWithWitness: Debug;
 92    type CFCommittedInstanceWithWitness: Debug; // CycleFold CommittedInstance & Witness
 93
 94    fn preprocess(
 95        prep_param: &Self::PreprocessorParam,
 96    ) -> Result<(Self::ProverParam, Self::VerifierParam), Error>;
 97
 98    fn init(
 99        pp: &Self::ProverParam,
100        step_circuit: FC,
101        z_0: Vec<C1::ScalarField>, // initial state
102    ) -> Result<Self, Error>;
103
104    fn prove_step(&mut self) -> Result<(), Error>;
105
106    // returns the state at the current step
107    fn state(&self) -> Vec<C1::ScalarField>;
108
109    // returns the instances at the current step, in the following order:
110    // (running_instance, incoming_instance, cyclefold_instance)
111    fn instances(
112        &self,
113    ) -> (
114        Self::CommittedInstanceWithWitness,
115        Self::CommittedInstanceWithWitness,
116        Self::CFCommittedInstanceWithWitness,
117    );
118
119    fn verify(
120        vp: Self::VerifierParam,
121        z_0: Vec<C1::ScalarField>, // initial state
122        z_i: Vec<C1::ScalarField>, // last state
123        // number of steps between the initial state and the last state
124        num_steps: C1::ScalarField,
125        running_instance: Self::CommittedInstanceWithWitness,
126        incoming_instance: Self::CommittedInstanceWithWitness,
127        cyclefold_instance: Self::CFCommittedInstanceWithWitness,
128    ) -> Result<(), Error>;
129}
130
131pub trait Decider<
132    C1: CurveGroup,
133    C2: CurveGroup,
134    FC: FCircuit<C1::ScalarField>,
135    FS: FoldingScheme<C1, C2, FC>,
136> where
137    C1: CurveGroup<BaseField = C2::ScalarField, ScalarField = C2::BaseField>,
138    C2::BaseField: PrimeField,
139{
140    type ProverParam: Clone;
141    type Proof: Clone;
142    type VerifierParam;
143    type PublicInput: Debug;
144    type CommittedInstanceWithWitness: Debug;
145    type CommittedInstance: Clone + Debug;
146
147    fn prove(
148        pp: &Self::ProverParam,
149        rng: impl RngCore + CryptoRng,
150        folding_scheme: FS,
151    ) -> Result<Self::Proof, Error>;
152
153    fn verify(
154        vp: &Self::VerifierParam,
155        i: C1::ScalarField,
156        z_0: Vec<C1::ScalarField>,
157        z_i: Vec<C1::ScalarField>,
158        running_instance: &Self::CommittedInstance,
159        proof: Self::Proof,
160        // returns `Result<bool, Error>` to differentiate between an error occurred while performing
161        // the verification steps, and the verification logic of the scheme not passing.
162    ) -> Result<bool, Error>;
163}