Skip to content

Commit

Permalink
Group -> Engine
Browse files Browse the repository at this point in the history
  • Loading branch information
winston-h-zhang committed Nov 27, 2023
1 parent bd9d635 commit 6230a2d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 68 deletions.
10 changes: 5 additions & 5 deletions src/bellpepper/r1cs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ pub trait NovaShape<E: Engine> {
impl<E: Engine> NovaWitness<E> for SatisfyingAssignment<E> {
fn r1cs_instance_and_witness(
&self,
shape: &R1CSShape<G>,
ck: &CommitmentKey<G>,
) -> Result<(R1CSInstance<G>, R1CSWitness<G>), NovaError> {
let W = R1CSWitness::<G>::new(shape, self.aux_assignment().to_owned())?;
shape: &R1CSShape<E>,
ck: &CommitmentKey<E>,
) -> Result<(R1CSInstance<E>, R1CSWitness<E>), NovaError> {
let W = R1CSWitness::<E>::new(shape, self.aux_assignment().to_owned())?;
let X = self.input_assignment()[1..].to_owned();

let comm_W = W.commit(ck);

let instance = R1CSInstance::<G>::new(shape, comm_W, X)?;
let instance = R1CSInstance::<E>::new(shape, comm_W, X)?;

Ok((instance, W))
}
Expand Down
4 changes: 2 additions & 2 deletions src/bellpepper/solver.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::traits::Group;
use crate::traits::Engine;
use bellpepper_core::{ConstraintSystem, Index, LinearCombination, SynthesisError, Variable};
use ff::PrimeField;

use bellpepper::util_cs::witness_cs::WitnessCS;

/// A `ConstraintSystem` which calculates witness values for a concrete instance of an R1CS circuit.
pub type SatisfyingAssignment<G> = WitnessCS<<G as Group>::Scalar>;
pub type SatisfyingAssignment<E> = WitnessCS<<E as Engine>::Scalar>;

#[derive(Debug, PartialEq, Eq)]
/// A `ConstraintSystem` which calculates witness values for a concrete instance of an R1CS circuit.
Expand Down
62 changes: 31 additions & 31 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,14 @@ where
/// A resource sink for [`RecursiveSNARK`]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(bound = "")]
pub struct ResourceSink<G1, G2>
pub struct ResourceSink<E1, E2>
where
G1: Group<Base = <G2 as Group>::Scalar>,
G2: Group<Base = <G1 as Group>::Scalar>,
E1: Engine<Base = <E2 as Engine>::Scalar>,
E2: Engine<Base = <E1 as Engine>::Scalar>,
{
l_w_primary: R1CSWitness<G1>,
l_u_primary: R1CSInstance<G1>,
_p: PhantomData<(G1, G2)>,
l_w_primary: R1CSWitness<E1>,
l_u_primary: R1CSInstance<E1>,
_p: PhantomData<(E1, E2)>,
}

/// A SNARK that proves the correct execution of an incremental computation
Expand All @@ -286,16 +286,16 @@ where
C1: StepCircuit<E1::Scalar>,
C2: StepCircuit<E2::Scalar>,
{
z0_primary: Vec<G1::Scalar>,
z0_secondary: Vec<G2::Scalar>,
r_W_primary: RelaxedR1CSWitness<G1>,
r_U_primary: RelaxedR1CSInstance<G1>,
r_W_secondary: RelaxedR1CSWitness<G2>,
r_U_secondary: RelaxedR1CSInstance<G2>,
l_w_secondary: R1CSWitness<G2>,
l_u_secondary: R1CSInstance<G2>,

sink: ResourceSink<G1, G2>,
z0_primary: Vec<E1::Scalar>,
z0_secondary: Vec<E2::Scalar>,
r_W_primary: RelaxedR1CSWitness<E1>,
r_U_primary: RelaxedR1CSInstance<E1>,
r_W_secondary: RelaxedR1CSWitness<E2>,
r_U_secondary: RelaxedR1CSInstance<E2>,
l_w_secondary: R1CSWitness<E2>,
l_u_secondary: R1CSInstance<E2>,

sink: ResourceSink<E1, E2>,
i: usize,
zi_primary: Vec<E1::Scalar>,
zi_secondary: Vec<E2::Scalar>,
Expand Down Expand Up @@ -327,10 +327,10 @@ where
// base case for the primary
let (mut input_assignment, mut aux_assignment) = (Vec::new(), Vec::new());
let mut cs_primary =
WitnessViewCS::<G1::Scalar>::new_view(&mut input_assignment, &mut aux_assignment);
let inputs_primary: NovaAugmentedCircuitInputs<G2> = NovaAugmentedCircuitInputs::new(
scalar_as_base::<G1>(pp.digest()),
G1::Scalar::ZERO,
WitnessViewCS::<E1::Scalar>::new_view(&mut input_assignment, &mut aux_assignment);
let inputs_primary: NovaAugmentedCircuitInputs<E2> = NovaAugmentedCircuitInputs::new(
scalar_as_base::<E1>(pp.digest()),
E1::Scalar::ZERO,
z0_primary.to_vec(),
None,
None,
Expand Down Expand Up @@ -359,8 +359,8 @@ where
// base case for the secondary
let (mut input_assignment, mut aux_assignment) = (Vec::new(), Vec::new());
let mut cs_secondary =
WitnessViewCS::<G2::Scalar>::new_view(&mut input_assignment, &mut aux_assignment);
let inputs_secondary: NovaAugmentedCircuitInputs<G1> = NovaAugmentedCircuitInputs::new(
WitnessViewCS::<E2::Scalar>::new_view(&mut input_assignment, &mut aux_assignment);
let inputs_secondary: NovaAugmentedCircuitInputs<E1> = NovaAugmentedCircuitInputs::new(
pp.digest(),
E2::Scalar::ZERO,
z0_secondary.to_vec(),
Expand Down Expand Up @@ -397,8 +397,8 @@ where
// IVC proof for the secondary circuit
let l_w_secondary = w_secondary;
let l_u_secondary = u_secondary;
let r_W_secondary = RelaxedR1CSWitness::<G2>::default(r1cs_secondary);
let r_U_secondary = RelaxedR1CSInstance::<G2>::default(&pp.ck_secondary, r1cs_secondary);
let r_W_secondary = RelaxedR1CSWitness::<E2>::default(r1cs_secondary);
let r_U_secondary = RelaxedR1CSInstance::<E2>::default(&pp.ck_secondary, r1cs_secondary);

assert!(
!(zi_primary.len() != pp.F_arity_primary || zi_secondary.len() != pp.F_arity_secondary),
Expand Down Expand Up @@ -487,13 +487,13 @@ where
.expect("Unable to fold secondary");

// increment `l_u_primary` and `l_w_primary`
let mut cs_primary = WitnessViewCS::<G1::Scalar>::new_view(
let mut cs_primary = WitnessViewCS::<E1::Scalar>::new_view(
&mut self.sink.l_u_primary.X,
&mut self.sink.l_w_primary.W,
);
let inputs_primary: NovaAugmentedCircuitInputs<G2> = NovaAugmentedCircuitInputs::new(
scalar_as_base::<G1>(pp.digest()),
G1::Scalar::from(self.i as u64),
let inputs_primary: NovaAugmentedCircuitInputs<E2> = NovaAugmentedCircuitInputs::new(
scalar_as_base::<E1>(pp.digest()),
E1::Scalar::from(self.i as u64),
self.z0_primary.to_vec(),
Some(self.zi_primary.clone()),
Some(self.r_U_secondary.clone()),
Expand Down Expand Up @@ -534,15 +534,15 @@ where

// increment `l_u_secondary` and `l_w_secondary`
let mut cs_secondary =
WitnessViewCS::<G2::Scalar>::new_view(&mut self.l_u_secondary.X, &mut self.l_w_secondary.W);
let inputs_secondary: NovaAugmentedCircuitInputs<G1> = NovaAugmentedCircuitInputs::new(
WitnessViewCS::<E2::Scalar>::new_view(&mut self.l_u_secondary.X, &mut self.l_w_secondary.W);
let inputs_secondary: NovaAugmentedCircuitInputs<E1> = NovaAugmentedCircuitInputs::new(
pp.digest(),
E2::Scalar::from(self.i as u64),
self.z0_secondary.to_vec(),
Some(self.zi_secondary.clone()),
Some(self.r_U_primary.clone()),
Some(self.sink.l_u_primary.clone()),
Some(Commitment::<G1>::decompress(&nifs_primary.comm_T)?),
Some(Commitment::<E1>::decompress(&nifs_primary.comm_T)?),
);

let circuit_secondary: NovaAugmentedCircuit<'_, E1, C2> = NovaAugmentedCircuit::new(
Expand Down
60 changes: 30 additions & 30 deletions src/r1cs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ impl<E: Engine> SimpleDigestible for R1CSShape<E> {}

/// A type that holds a witness for a given R1CS instance
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct R1CSWitness<G: Group> {
pub(crate) W: Vec<G::Scalar>,
pub struct R1CSWitness<E: Engine> {
pub(crate) W: Vec<E::Scalar>,
}

/// A type that holds an R1CS instance
Expand Down Expand Up @@ -384,16 +384,16 @@ impl<E: Engine> R1CSShape<E> {
}
}

impl<G: Group> R1CSWitness<G> {
impl<E: Engine> R1CSWitness<E> {
/// Produces a default `RelaxedR1CSWitness` given an `R1CSShape`
pub fn default(S: &R1CSShape<G>) -> R1CSWitness<G> {
pub fn default(S: &R1CSShape<E>) -> R1CSWitness<E> {
R1CSWitness {
W: vec![G::Scalar::ZERO; S.num_vars],
W: vec![E::Scalar::ZERO; S.num_vars],
}
}

/// A method to create a witness object using a vector of scalars
pub fn new(S: &R1CSShape<G>, W: Vec<G::Scalar>) -> Result<R1CSWitness<G>, NovaError> {
pub fn new(S: &R1CSShape<E>, W: Vec<E::Scalar>) -> Result<R1CSWitness<E>, NovaError> {
if S.num_vars != W.len() {
Err(NovaError::InvalidWitnessLength)
} else {
Expand All @@ -407,20 +407,20 @@ impl<G: Group> R1CSWitness<G> {
}
}

impl<G: Group> R1CSInstance<G> {
impl<E: Engine> R1CSInstance<E> {
/// Produces a default `R1CSInstance` given `CommitmentKey` and `R1CSShape`
pub fn default(_ck: &CommitmentKey<G>, S: &R1CSShape<G>) -> R1CSInstance<G> {
let comm_W = Commitment::<G>::default();
let X = vec![G::Scalar::ZERO; S.num_io];
pub fn default(_ck: &CommitmentKey<E>, S: &R1CSShape<E>) -> R1CSInstance<E> {
let comm_W = Commitment::<E>::default();
let X = vec![E::Scalar::ZERO; S.num_io];
R1CSInstance { comm_W, X }
}

/// A method to create an instance object using consitituent elements
pub fn new(
S: &R1CSShape<G>,
comm_W: Commitment<G>,
X: Vec<G::Scalar>,
) -> Result<R1CSInstance<G>, NovaError> {
S: &R1CSShape<E>,
comm_W: Commitment<E>,
X: Vec<E::Scalar>,
) -> Result<R1CSInstance<E>, NovaError> {
if S.num_io != X.len() {
Err(NovaError::InvalidInputLength)
} else {
Expand Down Expand Up @@ -448,10 +448,10 @@ impl<E: Engine> RelaxedR1CSWitness<E> {
}

/// Initializes a new `RelaxedR1CSWitness` from an `R1CSWitness`
pub fn from_r1cs_witness(S: &R1CSShape<G>, witness: R1CSWitness<G>) -> RelaxedR1CSWitness<G> {
pub fn from_r1cs_witness(S: &R1CSShape<E>, witness: R1CSWitness<E>) -> RelaxedR1CSWitness<E> {
RelaxedR1CSWitness {
W: witness.W,
E: vec![G::Scalar::ZERO; S.num_cons],
E: vec![E::Scalar::ZERO; S.num_cons],
}
}

Expand Down Expand Up @@ -513,17 +513,17 @@ impl<E: Engine> RelaxedR1CSInstance<E> {

/// Initializes a new `RelaxedR1CSInstance` from an `R1CSInstance`
pub fn from_r1cs_instance(
_ck: &CommitmentKey<G>,
S: &R1CSShape<G>,
instance: R1CSInstance<G>,
) -> RelaxedR1CSInstance<G> {
_ck: &CommitmentKey<E>,
S: &R1CSShape<E>,
instance: R1CSInstance<E>,
) -> RelaxedR1CSInstance<E> {
assert_eq!(S.num_io, instance.X.len());
// assert_eq!(G::Scalar::ONE, instance.X[0]);

RelaxedR1CSInstance {
comm_W: instance.comm_W,
comm_E: Commitment::<G>::default(),
u: G::Scalar::ONE,
comm_E: Commitment::<E>::default(),
u: E::Scalar::ONE,
X: instance.X,
}
}
Expand Down Expand Up @@ -600,16 +600,16 @@ impl<E: Engine> AbsorbInROTrait<E> for RelaxedR1CSInstance<E> {
}

/// Return an instance and witness, given a shape, ck, and the needed vectors.
pub fn instance_and_witness<G: Group>(
shape: &R1CSShape<G>,
ck: &CommitmentKey<G>,
input_assignment: &[G::Scalar],
aux_assignment: Vec<G::Scalar>,
) -> Result<(R1CSInstance<G>, R1CSWitness<G>), NovaError> {
let W = R1CSWitness::<G>::new(shape, aux_assignment)?;
pub fn instance_and_witness<E: Engine>(
shape: &R1CSShape<E>,
ck: &CommitmentKey<E>,
input_assignment: &[E::Scalar],
aux_assignment: Vec<E::Scalar>,
) -> Result<(R1CSInstance<E>, R1CSWitness<E>), NovaError> {
let W = R1CSWitness::<E>::new(shape, aux_assignment)?;
let comm_W = W.commit(ck);
let X = input_assignment[1..].to_owned();
let instance = R1CSInstance::<G>::new(shape, comm_W, X)?;
let instance = R1CSInstance::<E>::new(shape, comm_W, X)?;

Ok((instance, W))
}
Expand Down

0 comments on commit 6230a2d

Please sign in to comment.