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 06:57:22 +03:00
|
|
|
GroupType,
|
2020-05-07 03:22:00 +03:00
|
|
|
};
|
2020-06-08 08:21:31 +03:00
|
|
|
use leo_ast::LeoParser;
|
2020-06-10 02:36:15 +03:00
|
|
|
use leo_inputs::LeoInputsParser;
|
|
|
|
use leo_types::{InputValue, Inputs, Program};
|
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
|
|
|
};
|
|
|
|
|
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-20 09:02:58 +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-20 09:02:58 +03:00
|
|
|
program_inputs: Inputs,
|
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-20 09:02:58 +03:00
|
|
|
impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
|
2020-06-21 01:24:46 +03:00
|
|
|
pub fn new(package_name: String) -> Self {
|
2020-06-09 03:28:09 +03:00
|
|
|
Self {
|
2020-06-21 01:24:46 +03:00
|
|
|
package_name: package_name.clone(),
|
2020-06-09 03:28:09 +03:00
|
|
|
main_file_path: PathBuf::new(),
|
2020-06-21 01:24:46 +03:00
|
|
|
program: Program::new(package_name),
|
2020-06-10 02:36:15 +03:00
|
|
|
program_inputs: Inputs::new(),
|
2020-06-09 03:28:09 +03:00
|
|
|
output: None,
|
|
|
|
_engine: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 07:05:11 +03:00
|
|
|
pub fn new_from_path(package_name: String, main_file_path: PathBuf) -> Result<Self, CompilerError> {
|
|
|
|
let mut compiler = Self::new(package_name);
|
|
|
|
compiler.set_path(main_file_path);
|
2020-05-17 08:05:26 +03:00
|
|
|
|
|
|
|
// Generate the abstract syntax tree and assemble the program
|
2020-06-25 07:05:11 +03:00
|
|
|
let program_string = compiler.load_program()?;
|
|
|
|
compiler.parse_program(&program_string)?;
|
2020-05-17 08:05:26 +03:00
|
|
|
|
2020-06-25 07:05:11 +03:00
|
|
|
Ok(compiler)
|
2020-04-25 11:47:10 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 01:24:46 +03:00
|
|
|
pub fn set_path(&mut self, main_file_path: PathBuf) {
|
|
|
|
self.main_file_path = main_file_path
|
|
|
|
}
|
|
|
|
|
2020-06-20 09:02:58 +03:00
|
|
|
pub fn set_inputs(&mut self, program_inputs: Vec<Option<InputValue>>) {
|
2020-06-11 02:14:55 +03:00
|
|
|
self.program_inputs.set_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-06-23 08:32:57 +03:00
|
|
|
let path = self.main_file_path;
|
|
|
|
generate_constraints(cs, self.program, self.program_inputs.get_inputs()).map_err(|mut error| {
|
|
|
|
error.set_path(path);
|
|
|
|
|
|
|
|
error
|
|
|
|
})
|
2020-05-17 08:05:26 +03:00
|
|
|
}
|
|
|
|
|
2020-06-08 09:30:39 +03:00
|
|
|
pub fn compile_test_constraints(self, cs: &mut TestConstraintSystem<F>) -> Result<(), CompilerError> {
|
2020-06-05 03:39:57 +03:00
|
|
|
generate_test_constraints::<F, G>(cs, self.program)
|
|
|
|
}
|
|
|
|
|
2020-06-09 03:28:09 +03:00
|
|
|
fn load_program(&mut self) -> Result<String, CompilerError> {
|
|
|
|
// Load the program syntax tree from the file path
|
2020-06-11 21:43:05 +03:00
|
|
|
Ok(LeoParser::load_file(&self.main_file_path)?)
|
2020-06-09 03:28:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_program(&mut self, program_string: &str) -> Result<(), CompilerError> {
|
|
|
|
// Parse the program syntax tree
|
|
|
|
let syntax_tree = LeoParser::parse_file(&self.main_file_path, program_string)?;
|
2020-04-25 11:47:10 +03:00
|
|
|
|
2020-06-08 08:21:31 +03:00
|
|
|
// Build program from syntax tree
|
2020-05-02 08:10:40 +03:00
|
|
|
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-06-12 00:40:27 +03:00
|
|
|
self.program_inputs.set_inputs_size(self.program.expected_inputs.len());
|
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-06-10 02:36:15 +03:00
|
|
|
|
2020-06-20 08:07:02 +03:00
|
|
|
pub fn parse_inputs(&mut self, inputs_string: &str) -> Result<(), CompilerError> {
|
|
|
|
let syntax_tree = LeoInputsParser::parse_file(&inputs_string)?;
|
2020-06-10 02:36:15 +03:00
|
|
|
|
2020-06-15 23:38:07 +03:00
|
|
|
// Check number/order of parameters here
|
2020-06-12 00:40:27 +03:00
|
|
|
self.program_inputs = Inputs::from_inputs_file(syntax_tree, self.program.expected_inputs.clone())?;
|
2020-06-10 02:36:15 +03:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-06-25 07:05:11 +03:00
|
|
|
|
|
|
|
pub fn to_bytes(&self) -> Result<Vec<u8>, CompilerError> {
|
|
|
|
Ok(bincode::serialize(&self.program)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_bytes(bytes: &[u8]) -> Result<Self, CompilerError> {
|
|
|
|
let program: Program = bincode::deserialize(bytes)?;
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
package_name: program.name.clone(),
|
|
|
|
main_file_path: PathBuf::new(),
|
|
|
|
program,
|
|
|
|
program_inputs: Inputs::new(),
|
|
|
|
output: None,
|
|
|
|
_engine: PhantomData,
|
|
|
|
})
|
|
|
|
}
|
2020-05-02 08:10:40 +03:00
|
|
|
}
|
2020-04-25 11:47:10 +03:00
|
|
|
|
2020-06-20 09:02:58 +03:00
|
|
|
impl<F: Field + PrimeField, G: GroupType<F>> ConstraintSynthesizer<F> for Compiler<F, G> {
|
2020-06-08 09:30:39 +03:00
|
|
|
fn generate_constraints<CS: ConstraintSystem<F>>(self, cs: &mut CS) -> Result<(), SynthesisError> {
|
2020-06-23 07:39:24 +03:00
|
|
|
let result =
|
2020-06-18 01:40:39 +03:00
|
|
|
generate_constraints::<_, G, _>(cs, self.program, self.program_inputs.get_inputs()).map_err(|e| {
|
|
|
|
log::error!("{}", e);
|
|
|
|
SynthesisError::Unsatisfiable
|
|
|
|
})?;
|
2020-05-06 03:24:34 +03:00
|
|
|
|
|
|
|
// Write results to file or something
|
2020-06-23 07:39:24 +03:00
|
|
|
log::info!("{}", result);
|
2020-05-06 03:24:34 +03:00
|
|
|
|
2020-04-25 11:47:10 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-28 00:36:05 +03:00
|
|
|
}
|