You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.4 KiB

  1. use bellperson::{gadgets::num::AllocatedNum, ConstraintSystem, SynthesisError};
  2. use ff::PrimeField;
  3. use nova_snark::bellperson::{
  4. r1cs::{NovaShape, NovaWitness},
  5. shape_cs::ShapeCS,
  6. solver::SatisfyingAssignment,
  7. };
  8. fn synthesize_use_cs_one<Fr: PrimeField, CS: ConstraintSystem<Fr>>(
  9. cs: &mut CS,
  10. ) -> Result<(), SynthesisError> {
  11. let a = AllocatedNum::alloc(cs.namespace(|| "a"), || Ok(Fr::one()))?;
  12. let b = AllocatedNum::alloc(cs.namespace(|| "b"), || Ok(Fr::one()))?;
  13. cs.enforce(
  14. || "check a = b",
  15. |lc| lc + a.get_variable() - b.get_variable(),
  16. |lc| lc + CS::one(),
  17. |lc| lc,
  18. );
  19. let _ = a.inputize(cs.namespace(|| "a is input"));
  20. let _ = b.inputize(cs.namespace(|| "b is input"));
  21. Ok(())
  22. }
  23. fn synthesize_use_cs_one_after_inputize<Fr: PrimeField, CS: ConstraintSystem<Fr>>(
  24. cs: &mut CS,
  25. ) -> Result<(), SynthesisError> {
  26. let a = AllocatedNum::alloc(cs.namespace(|| "a"), || Ok(Fr::one()))?;
  27. let b = AllocatedNum::alloc(cs.namespace(|| "b"), || Ok(Fr::one()))?;
  28. let _ = a.inputize(cs.namespace(|| "a is input"));
  29. cs.enforce(
  30. || "check a = b",
  31. |lc| lc + a.get_variable() - b.get_variable(),
  32. |lc| lc + CS::one(),
  33. |lc| lc,
  34. );
  35. let _ = b.inputize(cs.namespace(|| "b is input"));
  36. Ok(())
  37. }
  38. #[test]
  39. fn test_use_cs_one() {
  40. type G = pasta_curves::pallas::Point;
  41. // First create the shape
  42. let mut cs: ShapeCS<G> = ShapeCS::new();
  43. let _ = synthesize_use_cs_one(&mut cs);
  44. let shape = cs.r1cs_shape();
  45. let gens = cs.r1cs_gens();
  46. // Now get the assignment
  47. let mut cs: SatisfyingAssignment<G> = SatisfyingAssignment::new();
  48. let _ = synthesize_use_cs_one(&mut cs);
  49. let (inst, witness) = cs.r1cs_instance_and_witness(&shape, &gens).unwrap();
  50. // Make sure that this is satisfiable
  51. assert!(shape.is_sat(&gens, &inst, &witness).is_ok());
  52. }
  53. #[test]
  54. fn test_use_cs_one_after_inputize() {
  55. type G = pasta_curves::pallas::Point;
  56. // First create the shape
  57. let mut cs: ShapeCS<G> = ShapeCS::new();
  58. let _ = synthesize_use_cs_one_after_inputize(&mut cs);
  59. let shape = cs.r1cs_shape();
  60. let gens = cs.r1cs_gens();
  61. // Now get the assignment
  62. let mut cs: SatisfyingAssignment<G> = SatisfyingAssignment::new();
  63. let _ = synthesize_use_cs_one_after_inputize(&mut cs);
  64. let (inst, witness) = cs.r1cs_instance_and_witness(&shape, &gens).unwrap();
  65. // Make sure that this is satisfiable
  66. assert!(shape.is_sat(&gens, &inst, &witness).is_ok());
  67. }