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