use crate::{
constants::NUM_HASH_BITS,
frontend::{
num::AllocatedNum, AllocatedBit, Assignment, Boolean, ConstraintSystem, SynthesisError,
},
gadgets::{
ecc::AllocatedPoint,
utils::{
alloc_num_equals, alloc_scalar_as_base, alloc_zero, conditionally_select_vec, le_bits_to_num,
},
},
r1cs::{R1CSInstance, RelaxedR1CSInstance},
traits::{
circuit::StepCircuit, commitment::CommitmentTrait, Engine, ROCircuitTrait, ROConstantsCircuit,
},
Commitment,
};
use ff::Field;
use serde::{Deserialize, Serialize};
mod r1cs;
use r1cs::{AllocatedR1CSInstance, AllocatedRelaxedR1CSInstance};
#[derive(Debug, Serialize, Deserialize)]
#[serde(bound = "")]
pub struct NovaAugmentedCircuitInputs<E: Engine> {
pp_digest: E::Scalar,
i: E::Base,
z0: Vec<E::Base>,
zi: Option<Vec<E::Base>>,
U: Option<RelaxedR1CSInstance<E>>,
ri: Option<E::Base>,
r_next: E::Base,
u: Option<R1CSInstance<E>>,
T: Option<Commitment<E>>,
}
impl<E: Engine> NovaAugmentedCircuitInputs<E> {
pub fn new(
pp_digest: E::Scalar,
i: E::Base,
z0: Vec<E::Base>,
zi: Option<Vec<E::Base>>,
U: Option<RelaxedR1CSInstance<E>>,
ri: Option<E::Base>,
r_next: E::Base,
u: Option<R1CSInstance<E>>,
T: Option<Commitment<E>>,
) -> Self {
Self {
pp_digest,
i,
z0,
zi,
U,
ri,
r_next,
u,
T,
}
}
}
pub struct NovaAugmentedCircuit<'a, E: Engine, SC: StepCircuit<E::Base>> {
is_primary_circuit: bool, ro_consts: ROConstantsCircuit<E>,
inputs: Option<NovaAugmentedCircuitInputs<E>>,
step_circuit: &'a SC, }
impl<'a, E: Engine, SC: StepCircuit<E::Base>> NovaAugmentedCircuit<'a, E, SC> {
pub const fn new(
is_primary_circuit: bool,
inputs: Option<NovaAugmentedCircuitInputs<E>>,
step_circuit: &'a SC,
ro_consts: ROConstantsCircuit<E>,
) -> Self {
Self {
is_primary_circuit,
inputs,
step_circuit,
ro_consts,
}
}
fn alloc_witness<CS: ConstraintSystem<<E as Engine>::Base>>(
&self,
mut cs: CS,
arity: usize,
) -> Result<
(
AllocatedNum<E::Base>,
AllocatedNum<E::Base>,
Vec<AllocatedNum<E::Base>>,
Vec<AllocatedNum<E::Base>>,
AllocatedRelaxedR1CSInstance<E>,
AllocatedNum<E::Base>,
AllocatedNum<E::Base>,
AllocatedR1CSInstance<E>,
AllocatedPoint<E>,
),
SynthesisError,
> {
let pp_digest = alloc_scalar_as_base::<E, _>(
cs.namespace(|| "pp_digest"),
self.inputs.as_ref().map(|inputs| inputs.pp_digest),
)?;
let i = AllocatedNum::alloc(cs.namespace(|| "i"), || Ok(self.inputs.get()?.i))?;
let z_0 = (0..arity)
.map(|i| {
AllocatedNum::alloc(cs.namespace(|| format!("z0_{i}")), || {
Ok(self.inputs.get()?.z0[i])
})
})
.collect::<Result<Vec<AllocatedNum<E::Base>>, _>>()?;
let zero = vec![E::Base::ZERO; arity];
let z_i = (0..arity)
.map(|i| {
AllocatedNum::alloc(cs.namespace(|| format!("zi_{i}")), || {
Ok(self.inputs.get()?.zi.as_ref().unwrap_or(&zero)[i])
})
})
.collect::<Result<Vec<AllocatedNum<E::Base>>, _>>()?;
let U: AllocatedRelaxedR1CSInstance<E> = AllocatedRelaxedR1CSInstance::alloc(
cs.namespace(|| "Allocate U"),
self.inputs.as_ref().and_then(|inputs| inputs.U.as_ref()),
)?;
let r_i = AllocatedNum::alloc(cs.namespace(|| "ri"), || {
Ok(self.inputs.get()?.ri.unwrap_or(E::Base::ZERO))
})?;
let r_next = AllocatedNum::alloc(cs.namespace(|| "r_i+1"), || Ok(self.inputs.get()?.r_next))?;
let u = AllocatedR1CSInstance::alloc(
cs.namespace(|| "allocate instance u to fold"),
self.inputs.as_ref().and_then(|inputs| inputs.u.as_ref()),
)?;
let T = AllocatedPoint::alloc(
cs.namespace(|| "allocate T"),
self
.inputs
.as_ref()
.and_then(|inputs| inputs.T.map(|T| T.to_coordinates())),
)?;
T.check_on_curve(cs.namespace(|| "check T on curve"))?;
Ok((pp_digest, i, z_0, z_i, U, r_i, r_next, u, T))
}
fn synthesize_hash_check<CS: ConstraintSystem<E::Base>>(
&self,
mut cs: CS,
pp_digest: &AllocatedNum<E::Base>,
i: &AllocatedNum<E::Base>,
z_0: &[AllocatedNum<E::Base>],
z_i: &[AllocatedNum<E::Base>],
U: &AllocatedRelaxedR1CSInstance<E>,
r_i: &AllocatedNum<E::Base>,
) -> Result<AllocatedNum<E::Base>, SynthesisError> {
let mut ro = E::ROCircuit::new(self.ro_consts.clone());
ro.absorb(pp_digest);
ro.absorb(i);
for e in z_0 {
ro.absorb(e);
}
for e in z_i {
ro.absorb(e);
}
U.absorb_in_ro(cs.namespace(|| "absorb U"), &mut ro)?;
ro.absorb(r_i);
let hash_bits = ro.squeeze(cs.namespace(|| "Input hash"), NUM_HASH_BITS, false)?;
let hash = le_bits_to_num(cs.namespace(|| "bits to hash"), &hash_bits)?;
Ok(hash)
}
fn synthesize_base_case<CS: ConstraintSystem<E::Base>>(
&self,
mut cs: CS,
u: AllocatedR1CSInstance<E>,
) -> Result<AllocatedRelaxedR1CSInstance<E>, SynthesisError> {
let U_default: AllocatedRelaxedR1CSInstance<E> = if self.is_primary_circuit {
AllocatedRelaxedR1CSInstance::default(cs.namespace(|| "Allocate U_default"))?
} else {
AllocatedRelaxedR1CSInstance::from_r1cs_instance(cs.namespace(|| "Allocate U_default"), u)?
};
Ok(U_default)
}
fn synthesize_non_base_case<CS: ConstraintSystem<<E as Engine>::Base>>(
&self,
mut cs: CS,
pp_digest: &AllocatedNum<E::Base>,
U: &AllocatedRelaxedR1CSInstance<E>,
u: &AllocatedR1CSInstance<E>,
T: &AllocatedPoint<E>,
) -> Result<AllocatedRelaxedR1CSInstance<E>, SynthesisError> {
let U_fold = U.fold_with_r1cs(
cs.namespace(|| "compute fold of U and u"),
pp_digest,
u,
T,
self.ro_consts.clone(),
)?;
Ok(U_fold)
}
}
impl<E: Engine, SC: StepCircuit<E::Base>> NovaAugmentedCircuit<'_, E, SC> {
pub fn synthesize<CS: ConstraintSystem<<E as Engine>::Base>>(
self,
cs: &mut CS,
) -> Result<Vec<AllocatedNum<E::Base>>, SynthesisError> {
let arity = self.step_circuit.arity();
let (pp_digest, i, z_0, z_i, U, r_i, r_next, u, T) =
self.alloc_witness(cs.namespace(|| "allocate the circuit witness"), arity)?;
let zero = alloc_zero(cs.namespace(|| "zero"));
let is_base_case = alloc_num_equals(cs.namespace(|| "Check if base case"), &i.clone(), &zero)?;
let hash = self.synthesize_hash_check(
cs.namespace(|| "synthesize input hash check"),
&pp_digest,
&i,
&z_0,
&z_i,
&U,
&r_i,
)?;
let check_non_base_pass = alloc_num_equals(
cs.namespace(|| "check consistency of u.X[0] with H(params, U, i, z0, zi)"),
&u.X0,
&hash,
)?;
let Unew_base = self.synthesize_base_case(cs.namespace(|| "base case"), u.clone())?;
let Unew_non_base = self.synthesize_non_base_case(
cs.namespace(|| "synthesize non base case"),
&pp_digest,
&U,
&u,
&T,
)?;
let should_be_false = AllocatedBit::nor(
cs.namespace(|| "check_non_base_pass nor base_case"),
&check_non_base_pass,
&is_base_case,
)?;
cs.enforce(
|| "check_non_base_pass nor base_case = false",
|lc| lc + should_be_false.get_variable(),
|lc| lc + CS::one(),
|lc| lc,
);
let Unew = Unew_base.conditionally_select(
cs.namespace(|| "compute U_new"),
&Unew_non_base,
&Boolean::from(is_base_case.clone()),
)?;
let i_new = AllocatedNum::alloc(cs.namespace(|| "i + 1"), || {
Ok(*i.get_value().get()? + E::Base::ONE)
})?;
cs.enforce(
|| "check i + 1",
|lc| lc,
|lc| lc,
|lc| lc + i_new.get_variable() - CS::one() - i.get_variable(),
);
let z_input = conditionally_select_vec(
cs.namespace(|| "select input to F"),
&z_0,
&z_i,
&Boolean::from(is_base_case),
)?;
let z_next = self
.step_circuit
.synthesize(&mut cs.namespace(|| "F"), &z_input)?;
if z_next.len() != arity {
return Err(SynthesisError::IncompatibleLengthVector(
"z_next".to_string(),
));
}
let hash = self.synthesize_hash_check(
cs.namespace(|| "synthesize output hash check"),
&pp_digest,
&i_new,
&z_0,
&z_next,
&Unew,
&r_next,
)?;
u.X1
.inputize(cs.namespace(|| "Output unmodified hash of the other circuit"))?;
hash.inputize(cs.namespace(|| "output new hash of this circuit"))?;
Ok(z_next)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
frontend::{
r1cs::{NovaShape, NovaWitness},
solver::SatisfyingAssignment,
test_shape_cs::TestShapeCS,
},
gadgets::utils::scalar_as_base,
provider::{
Bn256EngineKZG, GrumpkinEngine, PallasEngine, Secp256k1Engine, Secq256k1Engine, VestaEngine,
},
r1cs::R1CSShape,
traits::{circuit::TrivialCircuit, snark::default_ck_hint},
};
fn test_recursive_circuit_with<E1, E2>(
num_constraints_primary: usize,
num_constraints_secondary: usize,
) where
E1: Engine<Base = <E2 as Engine>::Scalar>,
E2: Engine<Base = <E1 as Engine>::Scalar>,
{
let ro_consts1 = ROConstantsCircuit::<E2>::default();
let ro_consts2 = ROConstantsCircuit::<E1>::default();
let tc1 = TrivialCircuit::default();
let circuit1: NovaAugmentedCircuit<'_, E2, TrivialCircuit<<E2 as Engine>::Base>> =
NovaAugmentedCircuit::new(true, None, &tc1, ro_consts1.clone());
let mut cs: TestShapeCS<E1> = TestShapeCS::new();
let _ = circuit1.synthesize(&mut cs);
let shape1 = cs.r1cs_shape().unwrap();
let ck1 = R1CSShape::commitment_key(&[&shape1], &[&*default_ck_hint()]).unwrap();
assert_eq!(cs.num_constraints(), num_constraints_primary);
let tc2 = TrivialCircuit::default();
let circuit2: NovaAugmentedCircuit<'_, E1, TrivialCircuit<<E1 as Engine>::Base>> =
NovaAugmentedCircuit::new(false, None, &tc2, ro_consts2.clone());
let mut cs: TestShapeCS<E2> = TestShapeCS::new();
let _ = circuit2.synthesize(&mut cs);
let shape2 = cs.r1cs_shape().unwrap();
let ck2 = R1CSShape::commitment_key(&[&shape2], &[&*default_ck_hint()]).unwrap();
assert_eq!(cs.num_constraints(), num_constraints_secondary);
let zero1 = <<E2 as Engine>::Base as Field>::ZERO;
let ri_1 = <<E2 as Engine>::Base as Field>::ZERO;
let mut cs1 = SatisfyingAssignment::<E1>::new();
let inputs1: NovaAugmentedCircuitInputs<E2> = NovaAugmentedCircuitInputs::new(
scalar_as_base::<E1>(zero1), zero1,
vec![zero1],
None,
None,
None,
ri_1,
None,
None,
);
let circuit1: NovaAugmentedCircuit<'_, E2, TrivialCircuit<<E2 as Engine>::Base>> =
NovaAugmentedCircuit::new(true, Some(inputs1), &tc1, ro_consts1);
let _ = circuit1.synthesize(&mut cs1);
let (inst1, witness1) = cs1.r1cs_instance_and_witness(&shape1, &ck1).unwrap();
assert!(shape1.is_sat(&ck1, &inst1, &witness1).is_ok());
let zero2 = <<E1 as Engine>::Base as Field>::ZERO;
let ri_2 = <<E1 as Engine>::Base as Field>::ZERO;
let mut cs2 = SatisfyingAssignment::<E2>::new();
let inputs2: NovaAugmentedCircuitInputs<E1> = NovaAugmentedCircuitInputs::new(
scalar_as_base::<E2>(zero2), zero2,
vec![zero2],
None,
None,
None,
ri_2,
Some(inst1),
None,
);
let circuit2: NovaAugmentedCircuit<'_, E1, TrivialCircuit<<E1 as Engine>::Base>> =
NovaAugmentedCircuit::new(false, Some(inputs2), &tc2, ro_consts2);
let _ = circuit2.synthesize(&mut cs2);
let (inst2, witness2) = cs2.r1cs_instance_and_witness(&shape2, &ck2).unwrap();
assert!(shape2.is_sat(&ck2, &inst2, &witness2).is_ok());
}
#[test]
fn test_recursive_circuit() {
test_recursive_circuit_with::<PallasEngine, VestaEngine>(9833, 10365);
test_recursive_circuit_with::<Bn256EngineKZG, GrumpkinEngine>(10001, 10554);
test_recursive_circuit_with::<Secp256k1Engine, Secq256k1Engine>(10280, 10977);
}
}