Skip to content

Commit

Permalink
feat: SuperNova CyleFold first draft
Browse files Browse the repository at this point in the history
  • Loading branch information
Forpee committed Jun 10, 2024
1 parent f3d7790 commit e0fc2c8
Show file tree
Hide file tree
Showing 12 changed files with 2,181 additions and 13 deletions.
7 changes: 4 additions & 3 deletions src/cyclefold/gadgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,14 +417,15 @@ pub mod emulated {
}
}

#[derive(Clone)]
/// A non-native circuit version of a `RelaxedR1CSInstance`. This is used for the in-circuit
/// representation of the primary running instance
pub struct AllocatedEmulRelaxedR1CSInstance<E: Engine> {
pub comm_W: AllocatedEmulPoint<E::GE>,
pub comm_E: AllocatedEmulPoint<E::GE>,
u: AllocatedNum<E::Base>,
x0: AllocatedNum<E::Base>,
x1: AllocatedNum<E::Base>,
pub(crate) u: AllocatedNum<E::Base>,
pub(crate) x0: AllocatedNum<E::Base>,
pub(crate) x1: AllocatedNum<E::Base>,
}

impl<E> AllocatedEmulRelaxedR1CSInstance<E>
Expand Down
6 changes: 3 additions & 3 deletions src/cyclefold/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! This module defines CycleFold folding scheme and its related functions.

mod circuit;
mod gadgets;
pub(crate) mod circuit;
pub(crate) mod gadgets;
mod nova_circuit;
mod util;
pub(crate) mod util;

pub mod nifs;
pub mod snark;
4 changes: 2 additions & 2 deletions src/cyclefold/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub(super) fn absorb_cyclefold_r1cs<E: Engine>(u: &R1CSInstance<E>, ro: &mut E::
});
}

pub(super) fn absorb_primary_relaxed_r1cs<E1, E2>(U: &RelaxedR1CSInstance<E1>, ro: &mut E2::RO)
pub(crate) fn absorb_primary_relaxed_r1cs<E1, E2>(U: &RelaxedR1CSInstance<E1>, ro: &mut E2::RO)
where
E1: Engine<Base = <E2 as Engine>::Scalar>,
E2: Engine<Base = <E1 as Engine>::Scalar>,
Expand All @@ -76,7 +76,7 @@ where

#[derive(Debug, Serialize, Deserialize)]
#[serde(bound = "")]
pub(super) struct FoldingData<E: Engine> {
pub(crate) struct FoldingData<E: Engine> {
pub U: RelaxedR1CSInstance<E>,
pub u: R1CSInstance<E>,
pub T: Commitment<E>,
Expand Down
4 changes: 3 additions & 1 deletion src/gadgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ pub(crate) use nonnative::{bignat::nat_to_limbs, bignat::BigNat, util::f_to_nat,

mod r1cs;
pub(crate) use r1cs::{
conditionally_select_alloc_relaxed_r1cs, conditionally_select_vec_allocated_relaxed_r1cs_instance,
conditionally_select_alloc_relaxed_r1cs, conditionally_select_emul_alloc_relaxed_r1cs,
conditionally_select_vec_allocated_relaxed_r1cs_instance,
conditionally_select_vec_emul_allocated_relaxed_r1cs_instance,
};
pub(crate) use r1cs::{AllocatedR1CSInstance, AllocatedRelaxedR1CSInstance};

Expand Down
109 changes: 106 additions & 3 deletions src/gadgets/r1cs.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//! This module implements various gadgets necessary for folding R1CS types.
use super::nonnative::{
bignat::BigNat,
util::{f_to_nat, Num},
use super::{
conditionally_select_allocated_bit,
nonnative::{
bignat::BigNat,
util::{f_to_nat, Num},
},
};
use crate::{
cyclefold::gadgets::emulated::{AllocatedEmulPoint, AllocatedEmulRelaxedR1CSInstance},
constants::{NUM_CHALLENGE_BITS, NUM_FE_WITHOUT_IO_FOR_NOVA_FOLD},
gadgets::{
ecc::AllocatedPoint,
Expand Down Expand Up @@ -394,6 +398,75 @@ pub fn conditionally_select_vec_allocated_relaxed_r1cs_instance<
.collect::<Result<Vec<AllocatedRelaxedR1CSInstance<E, N>>, _>>()
}

/// c = cond ? a: b, where a, b: `Vec<AllocatedRelaxedR1CSInstance>`
pub fn conditionally_select_vec_emul_allocated_relaxed_r1cs_instance<
E: Engine,
CS: ConstraintSystem<<E as Engine>::Base>,
>(
mut cs: CS,
a: &[AllocatedEmulRelaxedR1CSInstance<E>],
b: &[AllocatedEmulRelaxedR1CSInstance<E>],
condition: &Boolean,
) -> Result<Vec<AllocatedEmulRelaxedR1CSInstance<E>>, SynthesisError> {
a.iter()
.enumerate()
.zip_eq(b.iter())
.map(|((i, a), b)| {
a.conditionally_select(
cs.namespace(|| format!("cond ? a[{}]: b[{}]", i, i)),
b,
condition,
)
})
.collect::<Result<Vec<AllocatedEmulRelaxedR1CSInstance<E>>, _>>()
}

/// c = cond ? a: b, where a, b: `AllocatedRelaxedR1CSInstance`
pub fn conditionally_select_emul_alloc_relaxed_r1cs<
E: Engine,
CS: ConstraintSystem<<E as Engine>::Base>,
>(
mut cs: CS,
a: &AllocatedEmulRelaxedR1CSInstance<E>,
b: &AllocatedEmulRelaxedR1CSInstance<E>,
condition: &Boolean,
) -> Result<AllocatedEmulRelaxedR1CSInstance<E>, SynthesisError> {
let c = AllocatedEmulRelaxedR1CSInstance {
comm_W: conditionally_select_emul_point(
cs.namespace(|| "comm_W = cond ? a.comm_W : b.comm_W"),
&a.comm_W,
&b.comm_W,
condition,
)?,
comm_E: conditionally_select_emul_point(
cs.namespace(|| "comm_E = cond ? a.comm_E : b.comm_E"),
&a.comm_E,
&b.comm_E,
condition,
)?,
u: conditionally_select(
cs.namespace(|| "u = cond ? a.u : b.u"),
&a.u,
&b.u,
condition,
)?,
x0: conditionally_select(
cs.namespace(|| "x0 = cond ? a.x0 : b.x0"),
&a.x0,
&b.x0,
condition,
)?,
x1: conditionally_select(
cs.namespace(|| "x1 = cond ? a.x1 : b.x1"),
&a.x1,
&b.x1,
condition,
)?,
};
Ok(c)
}


/// c = cond ? a: b, where a, b: `AllocatedPoint`
pub fn conditionally_select_point<G: Group, CS: ConstraintSystem<G::Base>>(
mut cs: CS,
Expand Down Expand Up @@ -423,3 +496,33 @@ pub fn conditionally_select_point<G: Group, CS: ConstraintSystem<G::Base>>(
};
Ok(c)
}

/// c = cond ? a: b, where a, b: `EmulAllocatedPoint`
pub fn conditionally_select_emul_point<G: Group, CS: ConstraintSystem<G::Base>>(
mut cs: CS,
a: &AllocatedEmulPoint<G>,
b: &AllocatedEmulPoint<G>,
condition: &Boolean,
) -> Result<AllocatedEmulPoint<G>, SynthesisError> {
let c = AllocatedEmulPoint {
x: conditionally_select_bignat(
cs.namespace(|| "x = cond ? a.x : b.x"),
&a.x,
&b.x,
condition,
)?,
y: conditionally_select_bignat(
cs.namespace(|| "y = cond ? a.y : b.y"),
&a.y,
&b.y,
condition,
)?,
is_infinity: conditionally_select_allocated_bit(
cs.namespace(|| "is_infinity = cond ? a.is_infinity : b.is_infinity"),
&a.is_infinity,
&b.is_infinity,
condition,
)?,
};
Ok(c)
}
Loading

0 comments on commit e0fc2c8

Please sign in to comment.