diff --git a/Cargo.lock b/Cargo.lock index cf0444284d..ec4fb87a42 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -609,11 +609,21 @@ name = "leo-benchmark" version = "0.1.0" dependencies = [ "from-pest", - "lazy_static", "leo-program", - "pest", - "pest-ast", - "pest_derive", + "rand 0.7.3", + "snarkos-algorithms", + "snarkos-curves", + "snarkos-errors", + "snarkos-gadgets", + "snarkos-models", +] + +[[package]] +name = "leo-compiler" +version = "0.1.0" +dependencies = [ + "from-pest", + "leo-program", "rand 0.7.3", "snarkos-algorithms", "snarkos-curves", diff --git a/Cargo.toml b/Cargo.toml index cd660f70e7..57626b0002 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ name = "leo" path = "benchmark/src/main.rs" [workspace] -members = [ "benchmark", "program" ] +members = [ "benchmark", "compiler", "program" ] [dependencies] snarkos-algorithms = { path = "../snarkOS/algorithms", version = "0.8.0" } diff --git a/benchmark/Cargo.toml b/benchmark/Cargo.toml index b415248975..bd5a8fb8a2 100644 --- a/benchmark/Cargo.toml +++ b/benchmark/Cargo.toml @@ -14,8 +14,4 @@ snarkos-gadgets = { path = "../../snarkOS/gadgets", version = "0.8.0" } snarkos-models = { path = "../../snarkOS/models", version = "0.8.0" } from-pest = { version = "0.3.1" } -lazy_static = { version = "1.3.0" } -pest = { version = "2.0" } -pest-ast = { version = "0.3.3" } -pest_derive = { version = "2.0" } rand = { version = "0.7" } diff --git a/compiler/Cargo.toml b/compiler/Cargo.toml new file mode 100644 index 0000000000..63a514c6cf --- /dev/null +++ b/compiler/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "leo-compiler" +version = "0.1.0" +authors = ["The Aleo Team "] +edition = "2018" + +[dependencies] +leo-program = { path = "../program", version = "0.1.0" } + +snarkos-algorithms = { path = "../../snarkOS/algorithms", version = "0.8.0" } +snarkos-curves = { path = "../../snarkOS/curves", version = "0.8.0" } +snarkos-errors = { path = "../../snarkOS/errors", version = "0.8.0" } +snarkos-gadgets = { path = "../../snarkOS/gadgets", version = "0.8.0" } +snarkos-models = { path = "../../snarkOS/models", version = "0.8.0" } + +from-pest = { version = "0.3.1" } +rand = { version = "0.7" } diff --git a/compiler/simple.leo b/compiler/simple.leo new file mode 100644 index 0000000000..847c630eb2 --- /dev/null +++ b/compiler/simple.leo @@ -0,0 +1,4 @@ +function main() -> (u32) { + a = 1 + 1 + return a +} \ No newline at end of file diff --git a/compiler/simple_import.leo b/compiler/simple_import.leo new file mode 100644 index 0000000000..667fa6871c --- /dev/null +++ b/compiler/simple_import.leo @@ -0,0 +1,4 @@ +struct Point { + u32 x + u32 y +} \ No newline at end of file diff --git a/compiler/src/lib.rs b/compiler/src/lib.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/compiler/src/main.rs b/compiler/src/main.rs new file mode 100644 index 0000000000..baea1f03d1 --- /dev/null +++ b/compiler/src/main.rs @@ -0,0 +1,111 @@ +use leo_program::{self, ast}; + +use snarkos_algorithms::snark::{ + create_random_proof, generate_random_parameters, prepare_verifying_key, verify_proof, +}; +use snarkos_curves::bls12_377::{Bls12_377, Fr}; +use snarkos_errors::gadgets::SynthesisError; +use snarkos_models::{ + curves::{Field, PrimeField}, + gadgets::r1cs::{ConstraintSynthesizer, ConstraintSystem}, +}; + +use from_pest::FromPest; +use rand::thread_rng; +use std::{ + fs, + marker::PhantomData, + time::{Duration, Instant}, +}; + +pub struct Benchmark { + _engine: PhantomData, +} + +impl Benchmark { + pub fn new() -> Self { + Self { + _engine: PhantomData, + } + } +} + +impl ConstraintSynthesizer for Benchmark { + fn generate_constraints>( + self, + cs: &mut CS, + ) -> Result<(), SynthesisError> { + // Read in file as string + let unparsed_file = fs::read_to_string("simple.leo").expect("cannot read file"); + + // Parse the file using leo.pest + let mut file = ast::parse(&unparsed_file).expect("unsuccessful parse"); + + // Build the abstract syntax tree + let syntax_tree = ast::File::from_pest(&mut file).expect("infallible"); + // println!("{:#?}", syntax_tree); + + let program = leo_program::Program::<'_, F>::from(syntax_tree); + println!(" compiled: {:#?}", program); + + let program = program.name("simple".into()); + leo_program::ResolvedProgram::generate_constraints(cs, program); + + Ok(()) + } +} + +fn main() { + 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 params = { + let circuit = Benchmark::::new(); + generate_random_parameters::(circuit, 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 is_success = verify_proof(&prepared_verifying_key, &proof, &[]).unwrap(); + + verifying += start.elapsed(); + + println!(" "); + println!(" Setup time : {:?} milliseconds", setup.as_millis()); + println!(" Prover time : {:?} milliseconds", proving.as_millis()); + println!( + " Verifier time : {:?} milliseconds", + verifying.as_millis() + ); + println!(" Verifier output : {}", is_success); + println!(" "); + + // let mut cs = TestConstraintSystem::::new(); + // + // println!("\n satisfied: {:?}", cs.is_satisfied()); + // + // println!( + // "\n number of constraints for input: {}", + // cs.num_constraints() + // ); + // +}