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