mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-09-11 05:45:30 +03:00
basic assign and returns
This commit is contained in:
parent
eb56ad6d7e
commit
f19eb4f066
@ -1 +1,2 @@
|
||||
return 5 + a
|
||||
x = 5 + a
|
||||
return x * x * 2
|
@ -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<F: Field, CS: ConstraintSystem<F>>(
|
||||
fn bool_from_variable<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
&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::<bool>()
|
||||
.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<F: Field, CS: ConstraintSystem<F>>(
|
||||
fn u32_from_variable<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
&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::<u32>()
|
||||
.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<F: Field, CS: ConstraintSystem<F>>(
|
||||
fn get_bool_value<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
&mut self,
|
||||
cs: &mut CS,
|
||||
expression: BooleanExpression,
|
||||
@ -89,7 +90,7 @@ impl ResolvedProgram {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_u32_value<F: Field, CS: ConstraintSystem<F>>(
|
||||
fn get_u32_value<F: Field + PrimeField + PrimeField, CS: ConstraintSystem<F>>(
|
||||
&mut self,
|
||||
cs: &mut CS,
|
||||
expression: FieldExpression,
|
||||
@ -101,7 +102,17 @@ impl ResolvedProgram {
|
||||
}
|
||||
}
|
||||
|
||||
fn enforce_or<F: Field, CS: ConstraintSystem<F>>(
|
||||
fn enforce_not<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
&mut self,
|
||||
cs: &mut CS,
|
||||
expression: BooleanExpression,
|
||||
) -> Boolean {
|
||||
let expression = self.get_bool_value(cs, expression);
|
||||
|
||||
expression.not()
|
||||
}
|
||||
|
||||
fn enforce_or<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
&mut self,
|
||||
cs: &mut CS,
|
||||
left: BooleanExpression,
|
||||
@ -113,7 +124,7 @@ impl ResolvedProgram {
|
||||
Boolean::or(cs, &left, &right).unwrap()
|
||||
}
|
||||
|
||||
fn enforce_and<F: Field, CS: ConstraintSystem<F>>(
|
||||
fn enforce_and<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
&mut self,
|
||||
cs: &mut CS,
|
||||
left: BooleanExpression,
|
||||
@ -125,7 +136,7 @@ impl ResolvedProgram {
|
||||
Boolean::and(cs, &left, &right).unwrap()
|
||||
}
|
||||
|
||||
fn enforce_bool_equality<F: Field, CS: ConstraintSystem<F>>(
|
||||
fn enforce_bool_equality<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
&mut self,
|
||||
cs: &mut CS,
|
||||
left: BooleanExpression,
|
||||
@ -140,7 +151,7 @@ impl ResolvedProgram {
|
||||
Boolean::Constant(true)
|
||||
}
|
||||
|
||||
fn enforce_field_equality<F: Field, CS: ConstraintSystem<F>>(
|
||||
fn enforce_field_equality<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
&mut self,
|
||||
cs: &mut CS,
|
||||
left: FieldExpression,
|
||||
@ -159,12 +170,13 @@ impl ResolvedProgram {
|
||||
Boolean::Constant(true)
|
||||
}
|
||||
|
||||
fn enforce_boolean_expression<F: Field, CS: ConstraintSystem<F>>(
|
||||
fn enforce_boolean_expression<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
&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<F: Field, CS: ConstraintSystem<F>>(
|
||||
fn enforce_add<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
&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<F: Field, CS: ConstraintSystem<F>>(
|
||||
fn enforce_sub<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
&mut self,
|
||||
cs: &mut CS,
|
||||
left: FieldExpression,
|
||||
@ -216,7 +219,7 @@ impl ResolvedProgram {
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn enforce_mul<F: Field, CS: ConstraintSystem<F>>(
|
||||
fn enforce_mul<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
&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<F: Field, CS: ConstraintSystem<F>>(
|
||||
fn enforce_div<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
&mut self,
|
||||
cs: &mut CS,
|
||||
left: FieldExpression,
|
||||
@ -256,7 +254,7 @@ impl ResolvedProgram {
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn enforce_pow<F: Field, CS: ConstraintSystem<F>>(
|
||||
fn enforce_pow<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
&mut self,
|
||||
cs: &mut CS,
|
||||
left: FieldExpression,
|
||||
@ -278,12 +276,11 @@ impl ResolvedProgram {
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn enforce_field_expression<F: Field, CS: ConstraintSystem<F>>(
|
||||
fn enforce_field_expression<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
&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<F: Field, CS: ConstraintSystem<F>>(cs: &mut CS, program: Program) {
|
||||
pub fn generate_constraints<F: Field + PrimeField, CS: ConstraintSystem<F>>(
|
||||
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!(),
|
||||
});
|
||||
|
66
src/main.rs
66
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<F: Field> {
|
||||
pub struct Benchmark<F: Field + PrimeField> {
|
||||
_engine: PhantomData<F>,
|
||||
}
|
||||
|
||||
impl<F: Field> Benchmark<F> {
|
||||
impl<F: Field + PrimeField> Benchmark<F> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
_engine: PhantomData,
|
||||
@ -32,7 +32,7 @@ impl<F: Field> Benchmark<F> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: Field> ConstraintSynthesizer<F> for Benchmark<F> {
|
||||
impl<F: Field + PrimeField> ConstraintSynthesizer<F> for Benchmark<F> {
|
||||
fn generate_constraints<CS: ConstraintSystem<F>>(
|
||||
self,
|
||||
cs: &mut CS,
|
||||
@ -57,42 +57,42 @@ impl<F: Field> ConstraintSynthesizer<F> for Benchmark<F> {
|
||||
}
|
||||
|
||||
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::<Fr>::new();
|
||||
generate_random_parameters::<Bls12_377, _, _>(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::<Bls12_377>(¶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::<Fr>::new();
|
||||
//
|
||||
|
Loading…
Reference in New Issue
Block a user