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::{fmt::Debug, rand::RngCore};
  9use thiserror::Error;
 10
 11use crate::frontend::FCircuit;
 12
 13pub mod transcript;
 14use transcript::Transcript;
 15pub mod ccs;
 16pub mod commitment;
 17pub mod constants;
 18pub mod folding;
 19pub mod frontend;
 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 Witness: Debug;
 86    type CommittedInstanceWithWitness: Debug;
 87    type CFCommittedInstanceWithWitness: Debug; // CycleFold CommittedInstance & Witness
 88    type CommittedInstance: Clone + Debug;
 89
 90    fn preprocess(
 91        prep_param: &Self::PreprocessorParam,
 92    ) -> Result<(Self::ProverParam, Self::VerifierParam), Error>;
 93
 94    fn init(
 95        pp: &Self::ProverParam,
 96        step_circuit: FC,
 97        z_0: Vec<C1::ScalarField>, // initial state
 98    ) -> Result<Self, Error>;
 99
100    fn prove_step(&mut self) -> Result<(), Error>;
101
102    // returns the state at the current step
103    fn state(&self) -> Vec<C1::ScalarField>;
104
105    // returns the instances at the current step
106    fn instances(
107        &self,
108    ) -> (
109        Self::CommittedInstanceWithWitness,
110        Self::CommittedInstanceWithWitness,
111        Self::CFCommittedInstanceWithWitness,
112    );
113
114    fn verify(
115        vp: Self::VerifierParam,
116        z_0: Vec<C1::ScalarField>, // initial state
117        z_i: Vec<C1::ScalarField>, // last state
118        // number of steps between the initial state and the last state
119        num_steps: C1::ScalarField,
120        running_instance: Self::CommittedInstanceWithWitness,
121        incomming_instance: Self::CommittedInstanceWithWitness,
122        cyclefold_instance: Self::CFCommittedInstanceWithWitness,
123    ) -> Result<(), Error>;
124}
125
126pub trait Decider<C: CurveGroup>: Clone + Debug {
127    type PreprocessorParam: Debug;
128    type ProverParam: Debug;
129    type VerifierParam: Debug;
130    type FreshInstance: Debug;
131    type PublicInput: Debug;
132    type CommittedInstanceWithWitness: Debug;
133    type CommittedInstance: Clone + Debug;
134
135    fn prove(
136        pp: &Self::ProverParam,
137        running_instance: &Self::CommittedInstanceWithWitness,
138        transcript: &mut impl Transcript<C>,
139        rng: impl RngCore,
140    ) -> Result<(), Error>;
141
142    fn verify(
143        vp: &Self::VerifierParam,
144        running_instance: &Self::CommittedInstance,
145        transcript: &mut impl Transcript<C>,
146        rng: impl RngCore,
147    ) -> Result<(), Error>;
148}