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::bellperson::{
12    r1cs::{NovaShape, NovaWitness},
13    shape_cs::ShapeCS,
14    solver::SatisfyingAssignment,
15  };
16  use bellperson::{gadgets::num::AllocatedNum, ConstraintSystem, SynthesisError};
17  use ff::PrimeField;
18
19  fn synthesize_alloc_bit<Fr: PrimeField, CS: ConstraintSystem<Fr>>(
20    cs: &mut CS,
21  ) -> Result<(), SynthesisError> {
22    // get two bits as input and check that they are indeed bits
23    let a = AllocatedNum::alloc(cs.namespace(|| "a"), || Ok(Fr::ONE))?;
24    let _ = a.inputize(cs.namespace(|| "a is input"));
25    cs.enforce(
26      || "check a is 0 or 1",
27      |lc| lc + CS::one() - a.get_variable(),
28      |lc| lc + a.get_variable(),
29      |lc| lc,
30    );
31    let b = AllocatedNum::alloc(cs.namespace(|| "b"), || Ok(Fr::ONE))?;
32    let _ = b.inputize(cs.namespace(|| "b is input"));
33    cs.enforce(
34      || "check b is 0 or 1",
35      |lc| lc + CS::one() - b.get_variable(),
36      |lc| lc + b.get_variable(),
37      |lc| lc,
38    );
39    Ok(())
40  }
41
42  #[test]
43  fn test_alloc_bit() {
44    type G = pasta_curves::pallas::Point;
45
46    // First create the shape
47    let mut cs: ShapeCS<G> = ShapeCS::new();
48    let _ = synthesize_alloc_bit(&mut cs);
49    let (shape, ck) = cs.r1cs_shape();
50
51    // Now get the assignment
52    let mut cs: SatisfyingAssignment<G> = SatisfyingAssignment::new();
53    let _ = synthesize_alloc_bit(&mut cs);
54    let (inst, witness) = cs.r1cs_instance_and_witness(&shape, &ck).unwrap();
55
56    // Make sure that this is satisfiable
57    assert!(shape.is_sat(&ck, &inst, &witness).is_ok());
58  }
59}