arnaucube
arnaucube/NovaPublic Self-hosted repository
Nova/src/bellperson/mod.rs
src/bellperson/mod.rsRaw
 1//! Support for generating R1CS from [Bellperson].
 2//!
 3//! [Bellperson]: https://github.com/filecoin-project/bellperson
 4
 5pub mod r1cs;
 6pub mod shape_cs;
 7pub mod solver;
 8
 9#[cfg(test)]
10mod tests {
11  use crate::{
12    bellperson::{
13      r1cs::{NovaShape, NovaWitness},
14      shape_cs::ShapeCS,
15      solver::SatisfyingAssignment,
16    },
17    traits::Group,
18  };
19  use bellperson::{gadgets::num::AllocatedNum, ConstraintSystem, SynthesisError};
20  use ff::PrimeField;
21
22  fn synthesize_alloc_bit<Fr: PrimeField, CS: ConstraintSystem<Fr>>(
23    cs: &mut CS,
24  ) -> Result<(), SynthesisError> {
25    // get two bits as input and check that they are indeed bits
26    let a = AllocatedNum::alloc(cs.namespace(|| "a"), || Ok(Fr::ONE))?;
27    let _ = a.inputize(cs.namespace(|| "a is input"));
28    cs.enforce(
29      || "check a is 0 or 1",
30      |lc| lc + CS::one() - a.get_variable(),
31      |lc| lc + a.get_variable(),
32      |lc| lc,
33    );
34    let b = AllocatedNum::alloc(cs.namespace(|| "b"), || Ok(Fr::ONE))?;
35    let _ = b.inputize(cs.namespace(|| "b is input"));
36    cs.enforce(
37      || "check b is 0 or 1",
38      |lc| lc + CS::one() - b.get_variable(),
39      |lc| lc + b.get_variable(),
40      |lc| lc,
41    );
42    Ok(())
43  }
44
45  fn test_alloc_bit_with<G>()
46  where
47    G: Group,
48  {
49    // First create the shape
50    let mut cs: ShapeCS<G> = ShapeCS::new();
51    let _ = synthesize_alloc_bit(&mut cs);
52    let (shape, ck) = cs.r1cs_shape();
53
54    // Now get the assignment
55    let mut cs: SatisfyingAssignment<G> = SatisfyingAssignment::new();
56    let _ = synthesize_alloc_bit(&mut cs);
57    let (inst, witness) = cs.r1cs_instance_and_witness(&shape, &ck).unwrap();
58
59    // Make sure that this is satisfiable
60    assert!(shape.is_sat(&ck, &inst, &witness).is_ok());
61  }
62
63  #[test]
64  fn test_alloc_bit() {
65    test_alloc_bit_with::<pasta_curves::pallas::Point>();
66    test_alloc_bit_with::<crate::provider::bn256_grumpkin::bn256::Point>();
67  }
68}