Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit f26ccb7fad302a884710a41815b198d86ea2409b
Author: Forpee <khalilhassam3@gmail.com>
Date:   Sun Jun 16 22:53:44 2024 +0200

    chore: cleanup

commit bfa71390acf5a11d4b76123ae34673a794a3c0e8
Author: Forpee <khalilhassam3@gmail.com>
Date:   Sun Jun 16 22:40:45 2024 +0200

    feat: add data_p

commit a960d069a953f913713c9f24695465b131673798
Author: Forpee <khalilhassam3@gmail.com>
Date:   Sun Jun 16 22:33:10 2024 +0200

    feat: return data_p

commit c687a9d8993c869aaa7678484ad963ad9dee9979
Author: Forpee <khalilhassam3@gmail.com>
Date:   Sun Jun 16 22:30:37 2024 +0200

    feat: alloc data_p

commit af6298734bb7f83b80f1ee6a361298cf844fd85f
Author: Forpee <khalilhassam3@gmail.com>
Date:   Sun Jun 16 22:23:22 2024 +0200

    feat: add data_p in F'

commit c1860c21bd68aee9d37fd3d3043e0fd7ea8eb012
Author: Forpee <khalilhassam3@gmail.com>
Date:   Sun Jun 16 22:16:49 2024 +0200

    feat: add data_p

commit e79619ea82f3235ecc15b4b60869e8260551d6a1
Author: Forpee <khalilhassam3@gmail.com>
Date:   Sun Jun 16 22:12:14 2024 +0200

    feat: alloc sn_fd U

commit ea9c75cd4fd5ca6e24e762f6b7a6512d18d2febb
Author: Forpee <khalilhassam3@gmail.com>
Date:   Sun Jun 16 21:56:32 2024 +0200

     feat: init data_P

commit 1c866b8c40f2c84340055600a8d3771677252892
Author: Forpee <khalilhassam3@gmail.com>
Date:   Sun Jun 16 21:51:08 2024 +0200

    feat: init SuperNova allocated folding data

commit 850672a4f554bb748fd70b3b0a5f6727765c5c70
Author: Forpee <khalilhassam3@gmail.com>
Date:   Fri Jun 14 23:06:31 2024 +0200

    feat: make sure we have a default instance, witness for circuit_idx

commit 176278d068f8858ed13596dd958defd9ea4a46aa
Author: Forpee <khalilhassam3@gmail.com>
Date:   Fri Jun 14 18:51:16 2024 +0200

    chore: restructure arguments

commit 00387fbcc5fc0da695d96a5297b7a280a934e192
Author: Forpee <khalilhassam3@gmail.com>
Date:   Fri Jun 14 18:35:53 2024 +0200

    feat: z0, zi

commit c111037f81e5a863e5b658064c7ed1bb067b59ee
Author: Forpee <khalilhassam3@gmail.com>
Date:   Fri Jun 14 18:29:34 2024 +0200

    feat: add utils

commit 8b2d27becf63b848b86aa1dfd62420b5c62ed5d4
Author: Forpee <khalilhassam3@gmail.com>
Date:   Thu Jun 13 21:37:28 2024 +0200

    feat: add memory buffers
  • Loading branch information
Forpee committed Jun 16, 2024
1 parent ef5242e commit 2817d43
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 171 deletions.
161 changes: 47 additions & 114 deletions src/supernova/cyclefold/augmented_circuit.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use crate::constants::{BN_N_LIMBS, NIO_CYCLE_FOLD, NUM_FE_IN_EMULATED_POINT, NUM_HASH_BITS};
use crate::cyclefold::gadgets::emulated::{AllocatedEmulPoint, AllocatedEmulRelaxedR1CSInstance};
use crate::cyclefold::gadgets::emulated::AllocatedEmulRelaxedR1CSInstance;
use crate::cyclefold::gadgets::AllocatedCycleFoldData;
use crate::gadgets::{
alloc_num_equals, alloc_scalar_as_base, alloc_zero, conditionally_select_emul_alloc_relaxed_r1cs,
conditionally_select_vec_emul_allocated_relaxed_r1cs_instance, le_bits_to_num,
AllocatedRelaxedR1CSInstance,
};
use crate::supernova::cyclefold::gadgets::emulated as supernova_emulated;
use itertools::Itertools as _;

use crate::r1cs::{R1CSInstance, RelaxedR1CSInstance};
use crate::supernova::utils::{get_from_vec_alloc_emul_relaxed_r1cs, get_selector_vec_from_index};
use crate::supernova::utils::get_selector_vec_from_index;
use crate::traits::commitment::CommitmentTrait;
use crate::traits::ROCircuitTrait;
use crate::zip_with;
Expand All @@ -26,6 +26,9 @@ use bellpepper_core::boolean::{AllocatedBit, Boolean};
use bellpepper_core::{num::AllocatedNum, ConstraintSystem, SynthesisError};
use ff::Field;
use serde::{Deserialize, Serialize};

use super::gadgets::emulated::SuperNovaAllocatedFoldingData;
use super::util::SuperNovaFoldingData;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Abomonation)]
pub struct SuperNovaAugmentedCircuitParams {
limb_width: usize,
Expand All @@ -42,7 +45,7 @@ impl SuperNovaAugmentedCircuitParams {
}

#[derive(Debug)]
pub struct SuperNovaAugmentedCircuitInputs<'a, E1, E2>
pub struct SuperNovaAugmentedCircuitInputs<E1, E2>
where
E1: Engine<Base = <E2 as Engine>::Scalar>,
E2: Engine<Base = <E1 as Engine>::Scalar>,
Expand All @@ -55,9 +58,7 @@ where
/// Input to the circuit for the non-base case
zi: Option<Vec<E1::Base>>,

U: Option<&'a [Option<RelaxedR1CSInstance<E2>>]>,
u: Option<R1CSInstance<E2>>,
T: Option<Commitment<E2>>,
data_p: Option<SuperNovaFoldingData<E2>>,

data_c_1: Option<FoldingData<E1>>,
data_c_2: Option<FoldingData<E1>>,
Expand All @@ -71,7 +72,7 @@ where
last_augmented_circuit_index: E1::Base,
}

impl<'a, E1, E2> SuperNovaAugmentedCircuitInputs<'a, E1, E2>
impl<E1, E2> SuperNovaAugmentedCircuitInputs<E1, E2>
where
E1: Engine<Base = <E2 as Engine>::Scalar>,
E2: Engine<Base = <E1 as Engine>::Scalar>,
Expand All @@ -81,9 +82,7 @@ where
i: E1::Base,
z0: Vec<E1::Base>,
zi: Option<Vec<E1::Base>>,
U: Option<&'a [Option<RelaxedR1CSInstance<E2>>]>,
u: Option<R1CSInstance<E2>>,
T: Option<Commitment<E2>>,
data_p: Option<SuperNovaFoldingData<E2>>,
data_c_1: Option<FoldingData<E1>>,
data_c_2: Option<FoldingData<E1>>,
E_new: Option<Commitment<E2>>,
Expand All @@ -96,9 +95,7 @@ where
i,
z0,
zi,
U,
u,
T,
data_p,
data_c_1,
data_c_2,
E_new,
Expand All @@ -117,7 +114,7 @@ where
{
params: &'a SuperNovaAugmentedCircuitParams,
ro_consts: ROConstantsCircuit<E1>,
inputs: Option<SuperNovaAugmentedCircuitInputs<'a, E1, E2>>,
inputs: Option<SuperNovaAugmentedCircuitInputs<E1, E2>>,
step_circuit: &'a SC, // The function that is applied for each step
num_augmented_circuits: usize, // number of overall augmented circuits
}
Expand All @@ -130,9 +127,9 @@ where
{
pub fn new(
params: &'a SuperNovaAugmentedCircuitParams,
inputs: Option<SuperNovaAugmentedCircuitInputs<'a, E1, E2>>,
ro_consts: ROConstantsCircuit<E1>,
inputs: Option<SuperNovaAugmentedCircuitInputs<E1, E2>>,
step_circuit: &'a SC,
ro_consts: ROConstantsCircuit<E1>,
num_augmented_circuits: usize,
) -> Self {
Self {
Expand All @@ -151,21 +148,17 @@ where
num_augmented_circuits: usize,
) -> Result<
(
AllocatedNum<E1::Base>, // pp_digest
AllocatedNum<E1::Base>, // i
Vec<AllocatedNum<E1::Base>>, // z0
Vec<AllocatedNum<E1::Base>>, // zi
Vec<emulated::AllocatedEmulRelaxedR1CSInstance<E1>>, // U
emulated::AllocatedEmulPoint<E1::GE>, // u_W
AllocatedNum<E1::Base>, // u_x0
AllocatedNum<E1::Base>, // u_x1
emulated::AllocatedEmulPoint<E1::GE>, // T
AllocatedCycleFoldData<E1>, // data_c_1
AllocatedCycleFoldData<E1>, // data_c_2
emulated::AllocatedEmulPoint<E1::GE>, // E_new
emulated::AllocatedEmulPoint<E1::GE>, // W_new
AllocatedNum<E1::Base>, // program_counter
Vec<Boolean>, // last_augmented_circuit_selector
AllocatedNum<E1::Base>, // pp_digest
AllocatedNum<E1::Base>, // i
Vec<AllocatedNum<E1::Base>>, // z0
Vec<AllocatedNum<E1::Base>>, // zi
supernova_emulated::SuperNovaAllocatedFoldingData<E1>, // data_p
AllocatedCycleFoldData<E1>, // data_c_1
AllocatedCycleFoldData<E1>, // data_c_2
emulated::AllocatedEmulPoint<E1::GE>, // E_new
emulated::AllocatedEmulPoint<E1::GE>, // W_new
AllocatedNum<E1::Base>, // program_counter
Vec<Boolean>, // last_augmented_circuit_selector
),
SynthesisError,
> {
Expand Down Expand Up @@ -197,61 +190,15 @@ where
})
.collect::<Result<Vec<AllocatedNum<E1::Base>>, _>>()?;

// x: <E2::Base> & y: <E2::Base> coords stored as BigNats<E1::Base>
let U = (0..num_augmented_circuits)
.map(|i| {
emulated::AllocatedEmulRelaxedR1CSInstance::alloc(
cs.namespace(|| format!("Allocate U {:?}", i)),
self
.inputs
.as_ref()
.and_then(|inputs| inputs.U.and_then(|U| U[i].as_ref())),
self.params.limb_width,
self.params.n_limbs,
)
})
.collect::<Result<Vec<AllocatedEmulRelaxedR1CSInstance<E1>>, _>>()?;

let u_W: AllocatedEmulPoint<E1::GE> = AllocatedEmulPoint::alloc(
cs.namespace(|| "allocate u_W"),
self
.inputs
.as_ref()
.and_then(|inputs| inputs.u.as_ref())
.as_ref()
.map(|u| u.comm_W.to_coordinates()),
limb_width,
n_limbs,
)?;

let u_x0 = AllocatedNum::alloc(cs.namespace(|| "allocate u_x0"), || {
self
.inputs
.as_ref()
.and_then(|inputs| inputs.u.as_ref())
.as_ref()
.map_or(Ok(E1::Base::ZERO), |u| Ok(u.X[0]))
})?;

let u_x1 = AllocatedNum::alloc(cs.namespace(|| "allocate u_x1"), || {
self
.inputs
.as_ref()
.and_then(|inputs| inputs.u.as_ref())
.as_ref()
.map_or(Ok(E1::Base::ZERO), |u| Ok(u.X[1]))
})?;

let T: AllocatedEmulPoint<E1::GE> = AllocatedEmulPoint::alloc(
cs.namespace(|| "allocate T"),
let data_p = SuperNovaAllocatedFoldingData::<E1>::alloc(
cs.namespace(|| "data_p"),
self
.inputs
.as_ref()
.and_then(|inputs| inputs.T)
.as_ref()
.map(|t| t.to_coordinates()),
.and_then(|inputs| inputs.data_p.as_ref()),
limb_width,
n_limbs,
num_augmented_circuits,
)?;

// x: <E1::Base> & y: <E1::Base> coords stored as E1::Bases
Expand Down Expand Up @@ -330,11 +277,7 @@ where
i,
z_0,
z_i,
U,
u_W,
u_x0,
u_x1,
T,
data_p,
data_c_1,
data_c_2,
E_new,
Expand Down Expand Up @@ -384,11 +327,7 @@ where
i: &AllocatedNum<E1::Base>,
z_0: &[AllocatedNum<E1::Base>],
z_i: &[AllocatedNum<E1::Base>],
U: Vec<emulated::AllocatedEmulRelaxedR1CSInstance<E1>>, // U
u_W: emulated::AllocatedEmulPoint<E1::GE>, // u_W
u_x0: AllocatedNum<E1::Base>, // u_x0
u_x1: AllocatedNum<E1::Base>, // u_x1
T: emulated::AllocatedEmulPoint<E1::GE>, // T
data_p: &SuperNovaAllocatedFoldingData<E1>,
data_c_1: &AllocatedCycleFoldData<E1>,
data_c_2: &AllocatedCycleFoldData<E1>,
E_new: emulated::AllocatedEmulPoint<E1::GE>,
Expand Down Expand Up @@ -422,7 +361,7 @@ where
ro.absorb(e);
}

U.iter().enumerate().try_for_each(|(i, U)| {
data_p.U.iter().enumerate().try_for_each(|(i, U)| {
U.absorb_in_ro(cs.namespace(|| format!("absorb U_new {:?}", i)), &mut ro)
})?;

Expand All @@ -431,7 +370,7 @@ where

let check_primary = alloc_num_equals(
cs.namespace(|| "u.X[0] = H(params, i, z0, zi, U_p)"),
&u_x0,
&data_p.u_x0,
&hash_p,
)?;

Expand All @@ -450,8 +389,11 @@ where
let hash_c = le_bits_to_num(cs.namespace(|| "cyclefold hash"), &hash_c_bits)?;

// check the hash matches the public IO from the last primary instance
let check_cyclefold =
alloc_num_equals(cs.namespace(|| "u.X[1] = H(params, U_c)"), &u_x1, &hash_c)?;
let check_cyclefold = alloc_num_equals(
cs.namespace(|| "u.X[1] = H(params, U_c)"),
&data_p.u_x1,
&hash_c,
)?;

let check_io = AllocatedBit::and(
cs.namespace(|| "both IOs match"),
Expand Down Expand Up @@ -511,9 +453,8 @@ where
)?;

// Run NIFS Verifier
let U_to_fold = get_from_vec_alloc_emul_relaxed_r1cs(
cs.namespace(|| "U to fold"),
&U,
let U_to_fold = data_p.U_to_fold(
cs.namespace(|| "data_p.U_to_fold"),
last_augmented_circuit_selector,
)?;

Expand All @@ -522,16 +463,16 @@ where
pp_digest,
W_new,
E_new,
&u_W,
&u_x0,
&u_x1,
&T,
&data_p.u_W,
&data_p.u_x0,
&data_p.u_x1,
&data_p.T,
self.ro_consts.clone(),
)?;

// update AllocatedRelaxedR1CSInstance on index match augmented circuit index
let U_next: Vec<AllocatedEmulRelaxedR1CSInstance<E1>> = zip_with!(
(U.iter(), last_augmented_circuit_selector.iter()),
(data_p.U.iter(), last_augmented_circuit_selector.iter()),
|U, equal_bit| {
conditionally_select_emul_alloc_relaxed_r1cs(
cs.namespace(|| "select on index namespace"),
Expand All @@ -558,11 +499,7 @@ where
i,
z_0,
z_i,
U,
u_W,
u_x0,
u_x1,
T,
data_p,
data_c_1,
data_c_2,
E_new,
Expand All @@ -588,11 +525,7 @@ where
&i,
&z_0,
&z_i,
U,
u_W,
u_x0,
u_x1,
T,
&data_p,
&data_c_1,
&data_c_2,
E_new,
Expand Down
Loading

0 comments on commit 2817d43

Please sign in to comment.