sonobe/folding-schemes/src/folding/protogalaxy/mod.rs
folding-schemes/src/folding/protogalaxy/mod.rsRaw
1use std::borrow::Borrow;
2
3/// Implements the scheme described in [ProtoGalaxy](https://eprint.iacr.org/2023/1106.pdf)
4use ark_ec::CurveGroup;
5use ark_ff::PrimeField;
6use ark_r1cs_std::{
7 alloc::{AllocVar, AllocationMode},
8 fields::fp::FpVar,
9};
10use ark_relations::r1cs::{Namespace, SynthesisError};
11use thiserror::Error;
12
13use super::circuits::nonnative::affine::NonNativeAffineVar;
14
15pub mod circuits;
16pub mod folding;
17pub mod traits;
18pub(crate) mod utils;
19
20#[derive(Clone, Debug, PartialEq, Eq)]
21pub struct CommittedInstance<C: CurveGroup> {
22 phi: C,
23 betas: Vec<C::ScalarField>,
24 e: C::ScalarField,
25 u: C::ScalarField,
26 x: Vec<C::ScalarField>,
27}
28
29#[derive(Clone, Debug)]
30pub struct CommittedInstanceVar<C: CurveGroup> {
31 phi: NonNativeAffineVar<C>,
32 betas: Vec<FpVar<C::ScalarField>>,
33 e: FpVar<C::ScalarField>,
34 u: FpVar<C::ScalarField>,
35 x: Vec<FpVar<C::ScalarField>>,
36}
37
38impl<C: CurveGroup> AllocVar<CommittedInstance<C>, C::ScalarField> for CommittedInstanceVar<C> {
39 fn new_variable<T: Borrow<CommittedInstance<C>>>(
40 cs: impl Into<Namespace<C::ScalarField>>,
41 f: impl FnOnce() -> Result<T, SynthesisError>,
42 mode: AllocationMode,
43 ) -> Result<Self, SynthesisError> {
44 f().and_then(|u| {
45 let cs = cs.into();
46
47 let u = u.borrow();
48
49 Ok(Self {
50 phi: NonNativeAffineVar::new_variable(cs.clone(), || Ok(u.phi), mode)?,
51 betas: Vec::new_variable(cs.clone(), || Ok(u.betas.clone()), mode)?,
52 e: FpVar::new_variable(cs.clone(), || Ok(u.e), mode)?,
53 u: FpVar::new_variable(cs.clone(), || Ok(u.u), mode)?,
54 x: Vec::new_variable(cs.clone(), || Ok(u.x.clone()), mode)?,
55 })
56 })
57 }
58}
59
60#[derive(Clone, Debug)]
61pub struct Witness<F: PrimeField> {
62 w: Vec<F>,
63 r_w: F,
64}
65
66#[derive(Debug, Error, PartialEq)]
67pub enum ProtoGalaxyError {
68 #[error("The remainder from G(X)-F(α)*L_0(X)) / Z(X) should be zero")]
69 RemainderNotZero,
70 #[error("Could not divide by vanishing polynomial")]
71 CouldNotDivideByVanishing,
72 #[error("The number of incoming instances + 1 should be a power of two, current number of instances: {0}")]
73 WrongNumInstances(usize),
74 #[error("The number of incoming items should be a power of two, current number of coefficients: {0}")]
75 BTreeNotFull(usize),
76 #[error("The lengths of β and δ do not equal: |β| = {0}, |δ|={0}")]
77 WrongLenBetas(usize, usize),
78}