Skip to content

Commit

Permalink
feat(nifs): support generic number of sangria c-markers
Browse files Browse the repository at this point in the history
**Motivation**
Within the sangria folding scheme we have two consistency markers that
fold via multiplication by `r`. For use within cyclefold (#373) we need
9 of them, so we make support for an arbitrary markers len.

Also the second problem was the availability of an accumulator for the
step-circuit instance in nifs::sangria of the columns. Without changes -
it would be necessary to do hash counting for an empty set within
Cyclefold IVC as part of the sagnria check of the accumulator.

**Overview**

The first problem was solved by adding generics to all types

The second problem was solved by wrapping the
accumulator.step_circuit_hash_acc in `Option`, which always zeroes for
step-circuit instances, if they not present. To make it more expressive
as an `Option`, the custom type `SCInstancesHashAcc` was used

- Also, to cure conflicts, I temporarily removed the ivc::cyclefold implementation
- Also modules related to sangria were moved to the ivc::sangria submodule.
  • Loading branch information
cyphersnake committed Jan 8, 2025
1 parent 5f3ed33 commit 72770d4
Show file tree
Hide file tree
Showing 14 changed files with 329 additions and 287 deletions.
109 changes: 6 additions & 103 deletions src/ivc/cyclefold/incrementally_verifiable_computation/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unused_imports)]

use std::{marker::PhantomData, num::NonZeroUsize};

use public_params::PublicParams;
Expand Down Expand Up @@ -50,109 +52,10 @@ where
CSup::Scalar: PrimeFieldBits + FromUniformBytes<64>,
{
pub fn new(
pp: &PublicParams<ARITY, ARITY, CMain, CSup, SC>,
sc: &SC,
z_0: [CMain::ScalarExt; ARITY],
_pp: &PublicParams<ARITY, ARITY, CMain, CSup, SC>,
_sc: &SC,
_z_0: [CMain::ScalarExt; ARITY],
) -> Self {
let _primary_span = info_span!("primary").entered();

let initial_self_acc = ProtoGalaxy::<CMain, 1>::new_accumulator(
AccumulatorArgs::from(&pp.primary_S),
&nifs::protogalaxy::ProverParam {
S: pp.primary_S.clone(),
pp_digest: pp.cmain_pp_digest(),
},
&mut ro(),
);

// At zero step cyclefold ivc - output protogalaxy-accumulator is input
// protogalaxy-accumulator. Bug proof still should be valid.
let (_new_acc, self_proof) = ProtoGalaxy::prove(
&pp.primary_ck,
&nifs::protogalaxy::ProverParam {
S: pp.primary_S.clone(),
pp_digest: pp.cmain_pp_digest(),
},
&mut ro(),
initial_self_acc.clone(),
&[pp.primary_initial_trace.clone()],
)
.unwrap();

// At zero step cyclefold ivc - output sangria-accumulator is input
// sangria-accumulator. Bug proofs still should be valid.
//
// At this block we fold three same support-circuit initial traces (from pp) but result of
// this folding will be not used in next step, because of zero step
let paired_incoming = {
let mut proofs = Vec::with_capacity(initial_self_acc.W_commitment_len());

let mut paired_acc_ptr = nifs::sangria::accumulator::RelaxedPlonkTrace::from_regular(
pp.support_initial_trace.clone(),
SupportCircuit::<CMain>::MIN_K_TABLE_SIZE as usize,
);

for _ in 0..initial_self_acc.W_commitment_len() {
let (new_acc, paired_proof) =
SangriaFS::<CSup, { support_circuit::INSTANCES_LEN }>::prove(
&pp.support_ck,
&nifs::sangria::ProverParam {
S: pp.support_S.clone(),
pp_digest: pp.csup_pp_digest(),
},
&mut ro(),
paired_acc_ptr,
&[pp.support_initial_trace.clone()],
)
.unwrap();

proofs.push((pp.support_initial_trace.u.clone(), paired_proof));

paired_acc_ptr = new_acc;
}

proofs
};

let primary_sfc = StepFoldingCircuit::<'_, ARITY, CMain, CSup, SC> {
sc,
input: sfc::InputBuilder {
step: 0,
pp_digest: pp.csup_pp_digest(),
self_incoming: &pp.primary_initial_trace.u,
self_proof,
paired_acc: &pp.support_initial_trace.u.clone().into(),
paired_incoming: paired_incoming.as_slice(),
self_acc: &initial_self_acc.into(),
z_i: z_0,
z_0,
}
.build(),
_p: PhantomData,
};

let primary_initial_instances = primary_sfc.initial_instances();
let primary_witness = CircuitRunner::new(
pp.primary_k_table_size,
primary_sfc,
primary_initial_instances.clone(),
)
.try_collect_witness()
.unwrap();

let primary_post_initial_trace = ProtoGalaxy::<CMain, 1>::generate_plonk_trace(
&pp.primary_ck,
&primary_initial_instances,
&primary_witness,
&pp.protogalaxy_prover_params(),
&mut ro(),
)
.unwrap();

Self {
step: NonZeroUsize::new(1).unwrap(),
primary_trace: primary_post_initial_trace,
_p: PhantomData,
}
todo!("temporarily removed for the purposes of a simple merge into main")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ where
&support_cr.try_collect_witness().unwrap(),
&nifs::sangria::ProverParam {
S: support_cr.try_collect_plonk_structure().unwrap(),
pp_digest: CSup::identity(),
pp_digest: (CSup::Base::ZERO, CSup::Base::ZERO),
},
&mut ro(),
)
Expand Down Expand Up @@ -249,7 +249,7 @@ where
pub fn sangria_prover_params(&self) -> nifs::sangria::ProverParam<CSup> {
nifs::sangria::ProverParam {
S: self.support_S.clone(),
pp_digest: self.csup_pp_digest(),
pp_digest: self.csup_pp_digest_coordinates(),
}
}
}
40 changes: 1 addition & 39 deletions src/ivc/cyclefold/sfc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
};

mod input;
pub use input::{Input, InputBuilder};
pub use input::Input;

pub mod sangria_adapter;

Expand Down Expand Up @@ -253,41 +253,3 @@ where
Ok(())
}
}

#[cfg(test)]
mod tests {
use tracing_test::traced_test;

use super::*;
use crate::{halo2_proofs::dev::MockProver, ivc::step_circuit::trivial, prelude::bn256};

type CMain = bn256::C1Affine;
type CSup = bn256::C2Affine;

type Base = <CMain as CurveAffine>::Base;
type Scalar = <CMain as CurveAffine>::ScalarExt;

const ARITY: usize = 2;

#[traced_test]
#[test]
fn e2e_zero_step() {
let mut input = Input::<2, Scalar>::random(&mut rand::thread_rng());
input.step = 0;

let sc = trivial::Circuit::default();

let sfc = StepFoldingCircuit::<ARITY, CMain, CSup, trivial::Circuit<ARITY, Scalar>> {
sc: &sc,
input,
_p: PhantomData,
};

let instances = sfc.initial_instances();

MockProver::run(17, &sfc, instances)
.unwrap()
.verify()
.unwrap();
}
}
9 changes: 3 additions & 6 deletions src/ivc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ pub mod step_circuit;

pub mod sangria;
pub use sangria::{
fold_relaxed_plonk_instance_chip, incrementally_verifiable_computation, step_folding_circuit,
fold_relaxed_plonk_instance_chip, incrementally_verifiable_computation,
public_params::{CircuitPublicParamsInput, PublicParams},
step_folding_circuit,
};

pub mod protogalaxy;

pub mod cyclefold;

mod consistency_markers_computation;
pub mod instances_accumulator_computation;
mod public_params;

pub use halo2_proofs::circuit::SimpleFloorPlanner;
pub use incrementally_verifiable_computation::*;
pub use public_params::{CircuitPublicParamsInput, PublicParams};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
gadgets::{ecc::AssignedPoint, nonnative::bn::big_uint::BigUint},
halo2curves::CurveAffine,
main_gate::{AssignedValue, MainGate, MainGateConfig, RegionCtx, WrapValue},
nifs::sangria::accumulator::RelaxedPlonkInstance,
nifs::sangria::accumulator::{RelaxedPlonkInstance, SCInstancesHashAcc},
poseidon::{AbsorbInRO, ROCircuitTrait, ROTrait},
util::{self, ScalarToBase},
};
Expand Down Expand Up @@ -94,7 +94,8 @@ where
pub(crate) consistency_markers: Vec<BigUint<C::Base>>,
pub(crate) challenges: Vec<BigUint<C::Base>>,
pub(crate) u: &'l C::ScalarExt,
pub(crate) step_circuit_instances_hash_accumulator: &'l C::ScalarExt,
pub(crate) step_circuit_instances_hash_accumulator:
SCInstancesHashAcc<&'l C::ScalarExt>,
}

impl<C: CurveAffine, RO: ROTrait<C::Base>> AbsorbInRO<C::Base, RO>
Expand All @@ -116,8 +117,11 @@ where
.copied(),
)
.absorb_field(C::scalar_to_base(self.u).unwrap())
.absorb_field(
C::scalar_to_base(self.step_circuit_instances_hash_accumulator).unwrap(),
.absorb(
&self
.step_circuit_instances_hash_accumulator
.as_ref()
.map(|v| C::scalar_to_base(v).unwrap()),
);
}
}
Expand Down Expand Up @@ -156,7 +160,8 @@ where
.unwrap()
})
.collect(),
step_circuit_instances_hash_accumulator,
step_circuit_instances_hash_accumulator: step_circuit_instances_hash_accumulator
.as_ref(),
u,
};

Expand Down Expand Up @@ -227,7 +232,9 @@ mod tests {
challenges: vec![Scalar::from_u128(0x123456); 10],
E_commitment: CommitmentKey::<C1>::default_value(),
u: Scalar::from_u128(u128::MAX),
step_circuit_instances_hash_accumulator: Scalar::from_u128(0xaaaaaaaaaaaaa),
step_circuit_instances_hash_accumulator: SCInstancesHashAcc::Hash(Scalar::from_u128(
0xaaaaaaaaaaaaa,
)),
};

let off_circuit_hash: Base = ConsistencyMarkerComputation::<
Expand Down
Loading

0 comments on commit 72770d4

Please sign in to comment.