Skip to content

Commit

Permalink
preallocate witness cs (microsoft#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
winston-h-zhang authored and huitseeker committed Dec 5, 2023
1 parent 534f952 commit a6c44c1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 29 deletions.
11 changes: 6 additions & 5 deletions src/bellpepper/r1cs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use ff::PrimeField;
pub trait NovaWitness<E: Engine> {
/// Return an instance and witness, given a shape and ck.
fn r1cs_instance_and_witness(
&self,
self,
shape: &R1CSShape<E>,
ck: &CommitmentKey<E>,
) -> Result<(R1CSInstance<E>, R1CSWitness<E>), NovaError>;
Expand All @@ -32,16 +32,17 @@ pub trait NovaShape<E: Engine> {

impl<E: Engine> NovaWitness<E> for SatisfyingAssignment<E> {
fn r1cs_instance_and_witness(
&self,
self,
shape: &R1CSShape<E>,
ck: &CommitmentKey<E>,
) -> Result<(R1CSInstance<E>, R1CSWitness<E>), NovaError> {
let W = R1CSWitness::<E>::new(shape, self.aux_assignment())?;
let X = &self.input_assignment()[1..];
let (input_assignment, aux_assignment) = self.to_assignments();
let W = R1CSWitness::<E>::new(shape, aux_assignment)?;
let X = input_assignment[1..].to_owned();

let comm_W = W.commit(ck);

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

Ok((instance, W))
}
Expand Down
14 changes: 10 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,9 @@ where
// IVC proof for the primary circuit
let l_w_primary = w_primary;
let l_u_primary = u_primary;
let r_W_primary = RelaxedR1CSWitness::from_r1cs_witness(&pp.r1cs_shape_primary, &l_w_primary);
let r_W_primary = RelaxedR1CSWitness::from_r1cs_witness(&pp.r1cs_shape_primary, l_w_primary);
let r_U_primary =
RelaxedR1CSInstance::from_r1cs_instance(&pp.ck_primary, &pp.r1cs_shape_primary, &l_u_primary);
RelaxedR1CSInstance::from_r1cs_instance(&pp.ck_primary, &pp.r1cs_shape_primary, l_u_primary);

// IVC proof for the secondary circuit
let l_w_secondary = w_secondary;
Expand Down Expand Up @@ -395,7 +395,10 @@ where
)
.expect("Unable to fold secondary");

let mut cs_primary = SatisfyingAssignment::<E1>::new();
let mut cs_primary = SatisfyingAssignment::<E1>::with_capacity(
pp.r1cs_shape_primary.num_io + 1,
pp.r1cs_shape_primary.num_vars,
);
let inputs_primary: NovaAugmentedCircuitInputs<E2> = NovaAugmentedCircuitInputs::new(
scalar_as_base::<E1>(pp.digest()),
E1::Scalar::from(self.i as u64),
Expand Down Expand Up @@ -434,7 +437,10 @@ where
)
.expect("Unable to fold primary");

let mut cs_secondary = SatisfyingAssignment::<E2>::new();
let mut cs_secondary = SatisfyingAssignment::<E2>::with_capacity(
pp.r1cs_shape_secondary.num_io + 1,
pp.r1cs_shape_secondary.num_vars,
);
let inputs_secondary: NovaAugmentedCircuitInputs<E1> = NovaAugmentedCircuitInputs::new(
pp.digest(),
E2::Scalar::from(self.i as u64),
Expand Down
4 changes: 2 additions & 2 deletions src/nifs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,13 @@ mod tests {
};

let W = {
let res = R1CSWitness::new(&S, &vars);
let res = R1CSWitness::new(&S, vars);
assert!(res.is_ok());
res.unwrap()
};
let U = {
let comm_W = W.commit(ck);
let res = R1CSInstance::new(&S, &comm_W, &X);
let res = R1CSInstance::new(&S, comm_W, X);
assert!(res.is_ok());
res.unwrap()
};
Expand Down
34 changes: 17 additions & 17 deletions src/r1cs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,11 @@ impl<E: Engine> R1CSShape<E> {

impl<E: Engine> R1CSWitness<E> {
/// A method to create a witness object using a vector of scalars
pub fn new(S: &R1CSShape<E>, W: &[E::Scalar]) -> Result<R1CSWitness<E>, 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 {
Ok(R1CSWitness { W: W.to_owned() })
Ok(R1CSWitness { W })
}
}

Expand All @@ -399,16 +399,13 @@ impl<E: Engine> R1CSInstance<E> {
/// A method to create an instance object using constituent elements
pub fn new(
S: &R1CSShape<E>,
comm_W: &Commitment<E>,
X: &[E::Scalar],
comm_W: Commitment<E>,
X: Vec<E::Scalar>,
) -> Result<R1CSInstance<E>, NovaError> {
if S.num_io != X.len() {
Err(NovaError::InvalidInputLength)
} else {
Ok(R1CSInstance {
comm_W: *comm_W,
X: X.to_owned(),
})
Ok(R1CSInstance { comm_W, X })
}
}
}
Expand All @@ -432,9 +429,9 @@ impl<E: Engine> RelaxedR1CSWitness<E> {
}

/// Initializes a new `RelaxedR1CSWitness` from an `R1CSWitness`
pub fn from_r1cs_witness(S: &R1CSShape<E>, witness: &R1CSWitness<E>) -> RelaxedR1CSWitness<E> {
pub fn from_r1cs_witness(S: &R1CSShape<E>, witness: R1CSWitness<E>) -> RelaxedR1CSWitness<E> {
RelaxedR1CSWitness {
W: witness.W.clone(),
W: witness.W,
E: vec![E::Scalar::ZERO; S.num_cons],
}
}
Expand Down Expand Up @@ -497,15 +494,18 @@ impl<E: Engine> RelaxedR1CSInstance<E> {

/// Initializes a new `RelaxedR1CSInstance` from an `R1CSInstance`
pub fn from_r1cs_instance(
ck: &CommitmentKey<E>,
_ck: &CommitmentKey<E>,
S: &R1CSShape<E>,
instance: &R1CSInstance<E>,
instance: R1CSInstance<E>,
) -> RelaxedR1CSInstance<E> {
let mut r_instance = RelaxedR1CSInstance::default(ck, S);
r_instance.comm_W = instance.comm_W;
r_instance.u = E::Scalar::ONE;
r_instance.X = instance.X.clone();
r_instance
assert_eq!(S.num_io, instance.X.len());

RelaxedR1CSInstance {
comm_W: instance.comm_W,
comm_E: Commitment::<E>::default(),
u: E::Scalar::ONE,
X: instance.X,
}
}

/// Initializes a new `RelaxedR1CSInstance` from an `R1CSInstance`
Expand Down
2 changes: 1 addition & 1 deletion src/spartan/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl<E: Engine, S: RelaxedR1CSSNARKTrait<E>, C: StepCircuit<E::Scalar>> DirectSN
// convert the instance and witness to relaxed form
let (u_relaxed, w_relaxed) = (
RelaxedR1CSInstance::from_r1cs_instance_unchecked(&u.comm_W, &u.X),
RelaxedR1CSWitness::from_r1cs_witness(&pk.S, &w),
RelaxedR1CSWitness::from_r1cs_witness(&pk.S, w),
);

// prove the instance using Spartan
Expand Down

0 comments on commit a6c44c1

Please sign in to comment.