Refactor circuit code (#37)

* update crate versions

* refactor

* small tweaks

* run cargo fmt

* fix comments

* remove unused code

* address clippy

Co-authored-by: Srinath Setty <srinath@microsoft.com>
This commit is contained in:
iontzialla
2022-04-25 17:54:53 -04:00
committed by GitHub
parent 72920fb62b
commit 4656a7179d
10 changed files with 630 additions and 506 deletions

View File

@@ -9,7 +9,7 @@ use nova_snark::bellperson::{
fn synthesize_alloc_bit<Fr: PrimeField, CS: ConstraintSystem<Fr>>(
cs: &mut CS,
) -> Result<(), SynthesisError> {
//get two bits as input and check that they are indeed bits
// get two bits as input and check that they are indeed bits
let a = AllocatedNum::alloc(cs.namespace(|| "a"), || Ok(Fr::one()))?;
let _ = a.inputize(cs.namespace(|| "a is input"));
cs.enforce(
@@ -33,18 +33,18 @@ fn synthesize_alloc_bit<Fr: PrimeField, CS: ConstraintSystem<Fr>>(
fn test_alloc_bit() {
type G = pasta_curves::pallas::Point;
//First create the shape
// First create the shape
let mut cs: ShapeCS<G> = ShapeCS::new();
let _ = synthesize_alloc_bit(&mut cs);
let shape = cs.r1cs_shape();
let gens = cs.r1cs_gens();
println!("Mult mod constraint no: {}", cs.num_constraints());
//Now get the assignment
// Now get the assignment
let mut cs: SatisfyingAssignment<G> = SatisfyingAssignment::new();
let _ = synthesize_alloc_bit(&mut cs);
let (inst, witness) = cs.r1cs_instance_and_witness(&shape, &gens).unwrap();
//Make sure that this is satisfiable
// Make sure that this is satisfiable
assert!(shape.is_sat(&gens, &inst, &witness).is_ok());
}

View File

@@ -169,7 +169,7 @@ fn synthesize_add_mod<Fr: PrimeField, CS: ConstraintSystem<Fr>>(
fn test_mult_mod() {
type G = pasta_curves::pallas::Point;
//Set the inputs
// Set the inputs
let a_val = Integer::from_str_radix(
"11572336752428856981970994795408771577024165681374400871001196932361466228192",
10,
@@ -196,19 +196,19 @@ fn test_mult_mod() {
)
.unwrap();
//First create the shape
// First create the shape
let mut cs: ShapeCS<G> = ShapeCS::new();
let _ = synthesize_mult_mod(&mut cs, &a_val, &b_val, &m_val, &q_val, &r_val, 32, 8);
let shape = cs.r1cs_shape();
let gens = cs.r1cs_gens();
println!("Mult mod constraint no: {}", cs.num_constraints());
//Now get the assignment
// Now get the assignment
let mut cs: SatisfyingAssignment<G> = SatisfyingAssignment::new();
let _ = synthesize_mult_mod(&mut cs, &a_val, &b_val, &m_val, &q_val, &r_val, 32, 8);
let (inst, witness) = cs.r1cs_instance_and_witness(&shape, &gens).unwrap();
//Make sure that this is satisfiable
// Make sure that this is satisfiable
assert!(shape.is_sat(&gens, &inst, &witness).is_ok());
}
@@ -216,7 +216,7 @@ fn test_mult_mod() {
fn test_add() {
type G = pasta_curves::pallas::Point;
//Set the inputs
// Set the inputs
let a_val = Integer::from_str_radix(
"11572336752428856981970994795408771577024165681374400871001196932361466228192",
10,
@@ -229,19 +229,19 @@ fn test_add() {
)
.unwrap();
//First create the shape
// First create the shape
let mut cs: ShapeCS<G> = ShapeCS::new();
let _ = synthesize_add(&mut cs, &a_val, &b_val, &c_val, 32, 8);
let shape = cs.r1cs_shape();
let gens = cs.r1cs_gens();
println!("Add mod constraint no: {}", cs.num_constraints());
//Now get the assignment
// Now get the assignment
let mut cs: SatisfyingAssignment<G> = SatisfyingAssignment::new();
let _ = synthesize_add(&mut cs, &a_val, &b_val, &c_val, 32, 8);
let (inst, witness) = cs.r1cs_instance_and_witness(&shape, &gens).unwrap();
//Make sure that this is satisfiable
// Make sure that this is satisfiable
assert!(shape.is_sat(&gens, &inst, &witness).is_ok());
}
@@ -249,7 +249,7 @@ fn test_add() {
fn test_add_mod() {
type G = pasta_curves::pallas::Point;
//Set the inputs
// Set the inputs
let a_val = Integer::from_str_radix(
"11572336752428856981970994795408771577024165681374400871001196932361466228192",
10,
@@ -267,19 +267,19 @@ fn test_add_mod() {
)
.unwrap();
//First create the shape
// First create the shape
let mut cs: ShapeCS<G> = ShapeCS::new();
let _ = synthesize_add_mod(&mut cs, &a_val, &b_val, &c_val, &m_val, 32, 8);
let shape = cs.r1cs_shape();
let gens = cs.r1cs_gens();
println!("Add mod constraint no: {}", cs.num_constraints());
//Now get the assignment
// Now get the assignment
let mut cs: SatisfyingAssignment<G> = SatisfyingAssignment::new();
let _ = synthesize_add_mod(&mut cs, &a_val, &b_val, &c_val, &m_val, 32, 8);
let (inst, witness) = cs.r1cs_instance_and_witness(&shape, &gens).unwrap();
//Make sure that this is satisfiable
// Make sure that this is satisfiable
assert!(shape.is_sat(&gens, &inst, &witness).is_ok());
}
@@ -287,21 +287,21 @@ fn test_add_mod() {
fn test_equal() {
type G = pasta_curves::pallas::Point;
//Set the inputs
// Set the inputs
let a_val = Integer::from_str_radix("1157233675242885698197099479540877", 10).unwrap();
//First create the shape
// First create the shape
let mut cs: ShapeCS<G> = ShapeCS::new();
let _ = synthesize_is_equal(&mut cs, &a_val, 32, 8);
let shape = cs.r1cs_shape();
let gens = cs.r1cs_gens();
println!("Equal constraint no: {}", cs.num_constraints());
//Now get the assignment
// Now get the assignment
let mut cs: SatisfyingAssignment<G> = SatisfyingAssignment::new();
let _ = synthesize_is_equal(&mut cs, &a_val, 32, 8);
let (inst, witness) = cs.r1cs_instance_and_witness(&shape, &gens).unwrap();
//Make sure that this is satisfiable
// Make sure that this is satisfiable
assert!(shape.is_sat(&gens, &inst, &witness).is_ok());
}

View File

@@ -42,18 +42,18 @@ fn synthesize_use_cs_one_after_inputize<Fr: PrimeField, CS: ConstraintSystem<Fr>
fn test_use_cs_one() {
type G = pasta_curves::pallas::Point;
//First create the shape
// First create the shape
let mut cs: ShapeCS<G> = ShapeCS::new();
let _ = synthesize_use_cs_one(&mut cs);
let shape = cs.r1cs_shape();
let gens = cs.r1cs_gens();
//Now get the assignment
// Now get the assignment
let mut cs: SatisfyingAssignment<G> = SatisfyingAssignment::new();
let _ = synthesize_use_cs_one(&mut cs);
let (inst, witness) = cs.r1cs_instance_and_witness(&shape, &gens).unwrap();
//Make sure that this is satisfiable
// Make sure that this is satisfiable
assert!(shape.is_sat(&gens, &inst, &witness).is_ok());
}
@@ -61,17 +61,17 @@ fn test_use_cs_one() {
fn test_use_cs_one_after_inputize() {
type G = pasta_curves::pallas::Point;
//First create the shape
// First create the shape
let mut cs: ShapeCS<G> = ShapeCS::new();
let _ = synthesize_use_cs_one_after_inputize(&mut cs);
let shape = cs.r1cs_shape();
let gens = cs.r1cs_gens();
//Now get the assignment
// Now get the assignment
let mut cs: SatisfyingAssignment<G> = SatisfyingAssignment::new();
let _ = synthesize_use_cs_one_after_inputize(&mut cs);
let (inst, witness) = cs.r1cs_instance_and_witness(&shape, &gens).unwrap();
//Make sure that this is satisfiable
// Make sure that this is satisfiable
assert!(shape.is_sat(&gens, &inst, &witness).is_ok());
}