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, in the following order:
108 // (running_instance, incoming_instance, cyclefold_instance)
109 fn instances(
110 &self,
111 ) -> (
112 Self::CommittedInstanceWithWitness,
113 Self::CommittedInstanceWithWitness,
114 Self::CFCommittedInstanceWithWitness,
115 );
116
117 fn verify(
118 vp: Self::VerifierParam,
119 z_0: Vec<C1::ScalarField>, // initial state
120 z_i: Vec<C1::ScalarField>, // last state
121 // number of steps between the initial state and the last state
122 num_steps: C1::ScalarField,
123 running_instance: Self::CommittedInstanceWithWitness,
124 incoming_instance: Self::CommittedInstanceWithWitness,
125 cyclefold_instance: Self::CFCommittedInstanceWithWitness,
126 ) -> Result<(), Error>;
127}
128
129pub trait Decider<
130 C1: CurveGroup,
131 C2: CurveGroup,
132 FC: FCircuit<C1::ScalarField>,
133 FS: FoldingScheme<C1, C2, FC>,
134> where
135 C1: CurveGroup<BaseField = C2::ScalarField, ScalarField = C2::BaseField>,
136 C2::BaseField: PrimeField,
137{
138 type ProverParam: Clone;
139 type Proof: Clone;
140 type VerifierParam;
141 type PublicInput: Debug;
142 type CommittedInstanceWithWitness: Debug;
143 type CommittedInstance: Clone + Debug;
144
145 fn prove(
146 pp: &Self::ProverParam,
147 rng: impl RngCore + CryptoRng,
148 folding_scheme: FS,
149 ) -> Result<Self::Proof, Error>;
150
151 fn verify(
152 vp: &Self::VerifierParam,
153 i: C1::ScalarField,
154 z_0: Vec<C1::ScalarField>,
155 z_i: Vec<C1::ScalarField>,
156 running_instance: &Self::CommittedInstance,
157 proof: Self::Proof,
158 // returns `Result<bool, Error>` to differentiate between an error occurred while performing
159 // the verification steps, and the verification logic of the scheme not passing.
160 ) -> Result<bool, Error>;
161}