2020-05-06 04:40:25 +03:00
|
|
|
//! Compiles a Leo program from a file path.
|
|
|
|
|
2020-05-07 03:22:00 +03:00
|
|
|
use crate::{
|
2020-06-05 03:39:57 +03:00
|
|
|
constraints::{generate_constraints, generate_test_constraints, ConstrainedValue},
|
2020-05-09 02:35:00 +03:00
|
|
|
errors::CompilerError,
|
2020-06-08 04:22:59 +03:00
|
|
|
GroupType, Program,
|
2020-05-07 03:22:00 +03:00
|
|
|
};
|
2020-06-08 03:22:22 +03:00
|
|
|
use leo_ast::{ast, files::File};
|
2020-06-08 04:22:59 +03:00
|
|
|
use leo_types::InputValue;
|
2020-04-25 11:47:10 +03:00
|
|
|
|
|
|
|
use snarkos_errors::gadgets::SynthesisError;
|
|
|
|
use snarkos_models::{
|
2020-05-30 01:55:57 +03:00
|
|
|
curves::{Field, PrimeField},
|
2020-06-05 03:39:57 +03:00
|
|
|
gadgets::r1cs::{ConstraintSynthesizer, ConstraintSystem, TestConstraintSystem},
|
2020-04-25 11:47:10 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
use from_pest::FromPest;
|
2020-05-04 21:40:29 +03:00
|
|
|
use sha2::{Digest, Sha256};
|
2020-04-28 00:36:05 +03:00
|
|
|
use std::{fs, marker::PhantomData, path::PathBuf};
|
2020-04-25 11:47:10 +03:00
|
|
|
|
2020-04-26 04:11:49 +03:00
|
|
|
#[derive(Clone)]
|
2020-06-02 03:52:33 +03:00
|
|
|
pub struct Compiler<F: Field + PrimeField, G: GroupType<F>> {
|
2020-05-02 08:10:40 +03:00
|
|
|
package_name: String,
|
2020-04-25 11:47:10 +03:00
|
|
|
main_file_path: PathBuf,
|
2020-06-02 22:33:24 +03:00
|
|
|
program: Program,
|
2020-06-02 22:15:55 +03:00
|
|
|
program_inputs: Vec<Option<InputValue>>,
|
2020-06-02 03:52:33 +03:00
|
|
|
output: Option<ConstrainedValue<F, G>>,
|
2020-04-25 11:47:10 +03:00
|
|
|
_engine: PhantomData<F>,
|
|
|
|
}
|
|
|
|
|
2020-06-02 03:52:33 +03:00
|
|
|
impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
|
2020-05-17 08:05:26 +03:00
|
|
|
pub fn init(package_name: String, main_file_path: PathBuf) -> Result<Self, CompilerError> {
|
|
|
|
let mut program = Self {
|
2020-05-02 08:10:40 +03:00
|
|
|
package_name,
|
2020-04-28 00:36:05 +03:00
|
|
|
main_file_path,
|
2020-05-06 03:24:34 +03:00
|
|
|
program: Program::new(),
|
2020-05-09 02:35:00 +03:00
|
|
|
program_inputs: vec![],
|
2020-05-02 08:10:40 +03:00
|
|
|
output: None,
|
2020-04-28 00:36:05 +03:00
|
|
|
_engine: PhantomData,
|
2020-05-17 08:05:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Generate the abstract syntax tree and assemble the program
|
|
|
|
program.parse_program()?;
|
|
|
|
|
|
|
|
Ok(program)
|
2020-04-25 11:47:10 +03:00
|
|
|
}
|
|
|
|
|
2020-06-02 22:15:55 +03:00
|
|
|
pub fn set_inputs(&mut self, program_inputs: Vec<Option<InputValue>>) {
|
2020-05-19 22:01:19 +03:00
|
|
|
self.program_inputs = program_inputs;
|
|
|
|
}
|
|
|
|
|
2020-05-03 06:10:22 +03:00
|
|
|
pub fn checksum(&self) -> Result<String, CompilerError> {
|
|
|
|
// Read in the main file as string
|
2020-05-04 21:40:29 +03:00
|
|
|
let unparsed_file = fs::read_to_string(&self.main_file_path)
|
|
|
|
.map_err(|_| CompilerError::FileReadError(self.main_file_path.clone()))?;
|
2020-05-03 06:10:22 +03:00
|
|
|
|
|
|
|
// Hash the file contents
|
|
|
|
let mut hasher = Sha256::new();
|
|
|
|
hasher.input(unparsed_file.as_bytes());
|
|
|
|
let hash = hasher.result();
|
|
|
|
|
|
|
|
Ok(hex::encode(hash))
|
|
|
|
}
|
|
|
|
|
2020-05-17 08:05:26 +03:00
|
|
|
pub fn compile_constraints<CS: ConstraintSystem<F>>(
|
|
|
|
self,
|
|
|
|
cs: &mut CS,
|
2020-06-02 03:52:33 +03:00
|
|
|
) -> Result<ConstrainedValue<F, G>, CompilerError> {
|
2020-05-19 22:01:19 +03:00
|
|
|
generate_constraints(cs, self.program, self.program_inputs)
|
2020-05-17 08:05:26 +03:00
|
|
|
}
|
|
|
|
|
2020-06-05 03:39:57 +03:00
|
|
|
pub fn compile_test_constraints(
|
|
|
|
self,
|
|
|
|
cs: &mut TestConstraintSystem<F>,
|
|
|
|
) -> Result<(), CompilerError> {
|
|
|
|
generate_test_constraints::<F, G>(cs, self.program)
|
|
|
|
}
|
|
|
|
|
2020-06-08 03:22:22 +03:00
|
|
|
// pub fn compile(&self) -> Result<File, CompilerError> {
|
2020-05-03 06:10:22 +03:00
|
|
|
// // Read in the main file as string
|
|
|
|
// let unparsed_file = fs::read_to_string(&self.main_file_path).map_err(|_| CompilerError::FileReadError(self.main_file_path.clone()))?;
|
|
|
|
//
|
|
|
|
// // Parse the file using leo.pest
|
|
|
|
// let mut file = ast::parse(&unparsed_file).map_err(|_| CompilerError::FileParsingError)?;
|
|
|
|
//
|
|
|
|
// // Build the abstract syntax tree
|
2020-06-08 03:22:22 +03:00
|
|
|
// let syntax_tree = File::from_pest(&mut file).map_err(|_| CompilerError::SyntaxTreeError)?;
|
2020-05-03 06:10:22 +03:00
|
|
|
// log::debug!("{:#?}", syntax_tree);
|
|
|
|
//
|
|
|
|
// Ok(syntax_tree)
|
|
|
|
// }
|
|
|
|
|
2020-05-17 08:05:26 +03:00
|
|
|
fn parse_program(&mut self) -> Result<(), CompilerError> {
|
2020-04-25 11:47:10 +03:00
|
|
|
// Read in the main file as string
|
2020-05-03 05:03:50 +03:00
|
|
|
let unparsed_file = fs::read_to_string(&self.main_file_path)
|
|
|
|
.map_err(|_| CompilerError::FileReadError(self.main_file_path.clone()))?;
|
2020-04-25 11:47:10 +03:00
|
|
|
|
|
|
|
// Parse the file using leo.pest
|
2020-06-05 01:44:38 +03:00
|
|
|
let mut file = ast::parse(&unparsed_file).map_err(|error| {
|
|
|
|
CompilerError::from(error.with_path(&self.main_file_path.to_str().unwrap()))
|
|
|
|
})?;
|
2020-04-25 11:47:10 +03:00
|
|
|
|
|
|
|
// Build the abstract syntax tree
|
2020-05-03 05:03:50 +03:00
|
|
|
let syntax_tree =
|
2020-06-08 03:22:22 +03:00
|
|
|
File::from_pest(&mut file).map_err(|_| CompilerError::SyntaxTreeError)?;
|
2020-04-26 03:18:49 +03:00
|
|
|
log::debug!("{:#?}", syntax_tree);
|
2020-04-25 11:47:10 +03:00
|
|
|
|
2020-05-02 08:10:40 +03:00
|
|
|
// Build program from abstract syntax tree
|
|
|
|
let package_name = self.package_name.clone();
|
2020-04-25 11:47:10 +03:00
|
|
|
|
2020-06-02 22:33:24 +03:00
|
|
|
self.program = Program::from(syntax_tree, package_name);
|
2020-05-09 02:35:00 +03:00
|
|
|
self.program_inputs = vec![None; self.program.num_parameters];
|
2020-05-06 03:24:34 +03:00
|
|
|
|
2020-05-17 08:05:26 +03:00
|
|
|
log::debug!("Program parsing complete\n{:#?}", self.program);
|
2020-05-06 03:24:34 +03:00
|
|
|
|
|
|
|
Ok(())
|
2020-05-02 08:10:40 +03:00
|
|
|
}
|
|
|
|
}
|
2020-04-25 11:47:10 +03:00
|
|
|
|
2020-06-02 03:52:33 +03:00
|
|
|
impl<F: Field + PrimeField, G: GroupType<F>> ConstraintSynthesizer<F> for Compiler<F, G> {
|
2020-05-02 08:10:40 +03:00
|
|
|
fn generate_constraints<CS: ConstraintSystem<F>>(
|
|
|
|
self,
|
|
|
|
cs: &mut CS,
|
|
|
|
) -> Result<(), SynthesisError> {
|
2020-05-30 02:43:39 +03:00
|
|
|
let _result =
|
2020-06-02 03:52:33 +03:00
|
|
|
generate_constraints::<_, G, _>(cs, self.program, self.program_inputs).unwrap();
|
2020-05-06 03:24:34 +03:00
|
|
|
|
|
|
|
// Write results to file or something
|
|
|
|
|
2020-04-25 11:47:10 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-28 00:36:05 +03:00
|
|
|
}
|