Adds compiler module

This commit is contained in:
howardwu 2020-04-23 18:52:43 -07:00
parent 1ec03fdb09
commit 0a49dd1ec3
8 changed files with 151 additions and 9 deletions

18
Cargo.lock generated
View File

@ -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",

View File

@ -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" }

View File

@ -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" }

17
compiler/Cargo.toml Normal file
View File

@ -0,0 +1,17 @@
[package]
name = "leo-compiler"
version = "0.1.0"
authors = ["The Aleo Team <hello@aleo.org>"]
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" }

4
compiler/simple.leo Normal file
View File

@ -0,0 +1,4 @@
function main() -> (u32) {
a = 1 + 1
return a
}

View File

@ -0,0 +1,4 @@
struct Point {
u32 x
u32 y
}

0
compiler/src/lib.rs Normal file
View File

111
compiler/src/main.rs Normal file
View File

@ -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<F: Field + PrimeField> {
_engine: PhantomData<F>,
}
impl<F: Field + PrimeField> Benchmark<F> {
pub fn new() -> Self {
Self {
_engine: PhantomData,
}
}
}
impl<F: Field + PrimeField> ConstraintSynthesizer<F> for Benchmark<F> {
fn generate_constraints<CS: ConstraintSystem<F>>(
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::<Fr>::new();
generate_random_parameters::<Bls12_377, _, _>(circuit, rng).unwrap()
};
let prepared_verifying_key = prepare_verifying_key::<Bls12_377>(&params.vk);
setup += start.elapsed();
let start = Instant::now();
let proof = {
let c = Benchmark::new();
create_random_proof(c, &params, 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::<Fr>::new();
//
// println!("\n satisfied: {:?}", cs.is_satisfied());
//
// println!(
// "\n number of constraints for input: {}",
// cs.num_constraints()
// );
//
}