From f19eb4f06674192513bbefc94e33c3385cacd952 Mon Sep 17 00:00:00 2001 From: collin Date: Tue, 7 Apr 2020 12:44:59 -0700 Subject: [PATCH] basic assign and returns --- simple.program | 3 +- src/aleo_program/constraints.rs | 91 ++++++++++++++++----------------- src/main.rs | 66 ++++++++++++------------ 3 files changed, 80 insertions(+), 80 deletions(-) diff --git a/simple.program b/simple.program index 8e0dc94810..a5afe94873 100644 --- a/simple.program +++ b/simple.program @@ -1 +1,2 @@ -return 5 + a \ No newline at end of file +x = 5 + a +return x * x * 2 \ No newline at end of file diff --git a/src/aleo_program/constraints.rs b/src/aleo_program/constraints.rs index 4435d36b51..1b3de82d0c 100644 --- a/src/aleo_program/constraints.rs +++ b/src/aleo_program/constraints.rs @@ -2,7 +2,7 @@ use crate::aleo_program::{ BooleanExpression, Expression, FieldExpression, Program, Statement, Variable, }; -use snarkos_models::curves::Field; +use snarkos_models::curves::{Field, PrimeField}; use snarkos_models::gadgets::utilities::eq::EqGadget; use snarkos_models::gadgets::{ r1cs::ConstraintSystem, @@ -30,35 +30,36 @@ impl ResolvedProgram { self.resolved_variables.insert(variable, value); } - fn bool_from_variable>( + fn bool_from_variable>( &mut self, cs: &mut CS, variable: Variable, ) -> Boolean { if self.resolved_variables.contains_key(&variable) { + // TODO: return synthesis error: "assignment missing" here match self.resolved_variables.get(&variable).unwrap() { ResolvedValue::Boolean(boolean) => boolean.clone(), _ => panic!("expected a boolean, got field"), - }; - Boolean::Constant(true) + } } else { let argument = std::env::args() .nth(1) .unwrap_or("true".into()) .parse::() .unwrap(); - println!(" argument passed to command line a = {:?}", argument); + println!(" argument passed to command line a = {:?}\n", argument); // let a = true; - Boolean::alloc_input(cs.ns(|| variable.0), || Ok(argument)).unwrap() + Boolean::alloc(cs.ns(|| variable.0), || Ok(argument)).unwrap() } } - fn u32_from_variable>( + fn u32_from_variable>( &mut self, cs: &mut CS, variable: Variable, ) -> UInt32 { if self.resolved_variables.contains_key(&variable) { + // TODO: return synthesis error: "assignment missing" here match self.resolved_variables.get(&variable).unwrap() { ResolvedValue::FieldElement(field) => field.clone(), _ => panic!("expected a field, got boolean"), @@ -70,14 +71,14 @@ impl ResolvedProgram { .parse::() .unwrap(); - println!(" argument passed to command line a = {:?}", argument); + println!(" argument passed to command line a = {:?}\n", argument); // let a = 1; UInt32::alloc(cs.ns(|| variable.0), Some(argument)).unwrap() } } - fn get_bool_value>( + fn get_bool_value>( &mut self, cs: &mut CS, expression: BooleanExpression, @@ -89,7 +90,7 @@ impl ResolvedProgram { } } - fn get_u32_value>( + fn get_u32_value>( &mut self, cs: &mut CS, expression: FieldExpression, @@ -101,7 +102,17 @@ impl ResolvedProgram { } } - fn enforce_or>( + fn enforce_not>( + &mut self, + cs: &mut CS, + expression: BooleanExpression, + ) -> Boolean { + let expression = self.get_bool_value(cs, expression); + + expression.not() + } + + fn enforce_or>( &mut self, cs: &mut CS, left: BooleanExpression, @@ -113,7 +124,7 @@ impl ResolvedProgram { Boolean::or(cs, &left, &right).unwrap() } - fn enforce_and>( + fn enforce_and>( &mut self, cs: &mut CS, left: BooleanExpression, @@ -125,7 +136,7 @@ impl ResolvedProgram { Boolean::and(cs, &left, &right).unwrap() } - fn enforce_bool_equality>( + fn enforce_bool_equality>( &mut self, cs: &mut CS, left: BooleanExpression, @@ -140,7 +151,7 @@ impl ResolvedProgram { Boolean::Constant(true) } - fn enforce_field_equality>( + fn enforce_field_equality>( &mut self, cs: &mut CS, left: FieldExpression, @@ -159,12 +170,13 @@ impl ResolvedProgram { Boolean::Constant(true) } - fn enforce_boolean_expression>( + fn enforce_boolean_expression>( &mut self, cs: &mut CS, expression: BooleanExpression, ) -> Boolean { match expression { + BooleanExpression::Not(expression) => self.enforce_not(cs, *expression), BooleanExpression::Or(left, right) => self.enforce_or(cs, *left, *right), BooleanExpression::And(left, right) => self.enforce_and(cs, *left, *right), BooleanExpression::BoolEq(left, right) => self.enforce_bool_equality(cs, *left, *right), @@ -175,7 +187,7 @@ impl ResolvedProgram { } } - fn enforce_add>( + fn enforce_add>( &mut self, cs: &mut CS, left: FieldExpression, @@ -184,23 +196,14 @@ impl ResolvedProgram { let left = self.get_u32_value(cs, left); let right = self.get_u32_value(cs, right); - println!("left: {:#?}", left.value.unwrap()); - println!("right: {:#?}", right.value.unwrap()); - // println!("expected: {:#?}", UInt32::alloc(cs.ns(|| format!("expected")), Some(3))); - - let res = left - .add( - cs.ns(|| format!("enforce {} + {}", left.value.unwrap(), right.value.unwrap())), - &right, - ) - .unwrap(); - - println!("result: {:#?}", res.bits.to_vec()); - - res + UInt32::addmany( + cs.ns(|| format!("enforce {} + {}", left.value.unwrap(), right.value.unwrap())), + &[left, right], + ) + .unwrap() } - fn enforce_sub>( + fn enforce_sub>( &mut self, cs: &mut CS, left: FieldExpression, @@ -216,7 +219,7 @@ impl ResolvedProgram { .unwrap() } - fn enforce_mul>( + fn enforce_mul>( &mut self, cs: &mut CS, left: FieldExpression, @@ -225,9 +228,6 @@ impl ResolvedProgram { let left = self.get_u32_value(cs, left); let right = self.get_u32_value(cs, right); - println!("left: {}", left.value.unwrap()); - println!("right: {}", right.value.unwrap()); - let res = left .mul( cs.ns(|| format!("enforce {} * {}", left.value.unwrap(), right.value.unwrap())), @@ -235,12 +235,10 @@ impl ResolvedProgram { ) .unwrap(); - println!("result: {}", res.value.unwrap()); - res } - fn enforce_div>( + fn enforce_div>( &mut self, cs: &mut CS, left: FieldExpression, @@ -256,7 +254,7 @@ impl ResolvedProgram { .unwrap() } - fn enforce_pow>( + fn enforce_pow>( &mut self, cs: &mut CS, left: FieldExpression, @@ -278,12 +276,11 @@ impl ResolvedProgram { .unwrap() } - fn enforce_field_expression>( + fn enforce_field_expression>( &mut self, cs: &mut CS, expression: FieldExpression, ) -> UInt32 { - println!("enforcing: {}", expression); match expression { FieldExpression::Add(left, right) => self.enforce_add(cs, *left, *right), FieldExpression::Sub(left, right) => self.enforce_sub(cs, *left, *right), @@ -294,7 +291,10 @@ impl ResolvedProgram { } } - pub fn generate_constraints>(cs: &mut CS, program: Program) { + pub fn generate_constraints>( + cs: &mut CS, + program: Program, + ) { let mut resolved_program = ResolvedProgram::new(); program @@ -305,7 +305,7 @@ impl ResolvedProgram { Expression::Boolean(boolean_expression) => { let res = resolved_program.enforce_boolean_expression(cs, boolean_expression); - // println!("variable boolean result: {}", res.get_value().unwrap()); + println!("variable boolean result: {}", res.get_value().unwrap()); resolved_program.insert(variable, ResolvedValue::Boolean(res)); } Expression::FieldElement(field_expression) => { @@ -326,13 +326,12 @@ impl ResolvedProgram { Expression::Boolean(boolean_expression) => { let res = resolved_program .enforce_boolean_expression(cs, boolean_expression); - println!("boolean result: {}", res.get_value().unwrap()); + println!("boolean result: {}\n", res.get_value().unwrap()); } Expression::FieldElement(field_expression) => { - println!("expression {:?}", field_expression); let res = resolved_program.enforce_field_expression(cs, field_expression); - println!("field result: {}", res.value.unwrap()); + println!("field result: {}\n", res.value.unwrap()); } _ => unimplemented!(), }); diff --git a/src/main.rs b/src/main.rs index e624bf55c9..3a0d38f60e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ use std::{ use snarkos_curves::bls12_377::{Bls12_377, Fr}; use snarkos_errors::gadgets::SynthesisError; -use snarkos_models::curves::Field; +use snarkos_models::curves::{Field, PrimeField}; use snarkos_models::gadgets::r1cs::{ConstraintSynthesizer, ConstraintSystem}; use snarkos_algorithms::snark::{ @@ -20,11 +20,11 @@ use rand::thread_rng; // use std::env; -pub struct Benchmark { +pub struct Benchmark { _engine: PhantomData, } -impl Benchmark { +impl Benchmark { pub fn new() -> Self { Self { _engine: PhantomData, @@ -32,7 +32,7 @@ impl Benchmark { } } -impl ConstraintSynthesizer for Benchmark { +impl ConstraintSynthesizer for Benchmark { fn generate_constraints>( self, cs: &mut CS, @@ -57,42 +57,42 @@ impl ConstraintSynthesizer for Benchmark { } fn main() { - // let mut setup = Duration::new(0, 0); - // let mut proving = Duration::new(0, 0); - // let mut verifying = Duration::new(0, 0); + let mut setup = Duration::new(0, 0); + let mut proving = Duration::new(0, 0); + let mut verifying = Duration::new(0, 0); let rng = &mut thread_rng(); - // let start = Instant::now(); + let start = Instant::now(); - let _params = { + let params = { let c = Benchmark::::new(); generate_random_parameters::(c, rng).unwrap() }; - // - // let prepared_verifying_key = prepare_verifying_key(¶ms.vk); - // - // setup += start.elapsed(); - // - // let start = Instant::now(); - // let proof = { - // let c = Benchmark::new(); - // create_random_proof(c, ¶ms, rng).unwrap() - // }; - // - // proving += start.elapsed(); - // - // // let _inputs: Vec<_> = [1u32; 1].to_vec(); - // - // let start = Instant::now(); - // - // let _ = verify_proof(&prepared_verifying_key, &proof, &[]).unwrap(); - // - // verifying += start.elapsed(); - // - // println!("\n Setup time: {:?} seconds", setup.as_secs()); - // println!(" Proving time: {:?} seconds", proving.as_secs()); - // println!(" Verifying time: {:?} seconds", verifying.as_secs()); + + let prepared_verifying_key = prepare_verifying_key::(¶ms.vk); + + setup += start.elapsed(); + + let start = Instant::now(); + let proof = { + let c = Benchmark::new(); + create_random_proof(c, ¶ms, rng).unwrap() + }; + + proving += start.elapsed(); + + // let _inputs: Vec<_> = [1u32; 1].to_vec(); + + let start = Instant::now(); + + let _ = verify_proof(&prepared_verifying_key, &proof, &[]).unwrap(); + + verifying += start.elapsed(); + + println!("\n Setup time: {:?} seconds", setup.as_secs()); + println!(" Proving time: {:?} seconds", proving.as_secs()); + println!(" Verifying time: {:?} seconds", verifying.as_secs()); // let mut cs = TestConstraintSystem::::new(); //