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-07-08 05:11:26 +03:00
|
|
|
constraints::{generate_constraints, generate_test_constraints},
|
2020-05-09 02:35:00 +03:00
|
|
|
errors::CompilerError,
|
2020-06-08 06:57:22 +03:00
|
|
|
GroupType,
|
2020-07-08 07:05:03 +03:00
|
|
|
ImportParser,
|
2020-07-30 04:32:35 +03:00
|
|
|
OutputBytes,
|
2020-08-01 07:15:33 +03:00
|
|
|
OutputFile,
|
2020-05-07 03:22:00 +03:00
|
|
|
};
|
2020-06-08 08:21:31 +03:00
|
|
|
use leo_ast::LeoParser;
|
2020-08-01 05:39:30 +03:00
|
|
|
use leo_input::LeoInputParser;
|
|
|
|
use leo_types::{Input, MainInput, 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-08-01 07:15:33 +03:00
|
|
|
output_directory: PathBuf,
|
2020-06-02 22:33:24 +03:00
|
|
|
program: Program,
|
2020-08-01 05:39:30 +03:00
|
|
|
program_input: Input,
|
2020-07-08 07:05:03 +03:00
|
|
|
imported_programs: ImportParser,
|
2020-04-25 11:47:10 +03:00
|
|
|
_engine: PhantomData<F>,
|
2020-07-30 04:32:35 +03:00
|
|
|
_group: PhantomData<G>,
|
2020-04-25 11:47:10 +03:00
|
|
|
}
|
|
|
|
|
2020-06-20 09:02:58 +03:00
|
|
|
impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
|
2020-08-01 07:15:33 +03:00
|
|
|
pub fn new(package_name: String, main_file_path: PathBuf, output_directory: PathBuf) -> Self {
|
2020-06-09 03:28:09 +03:00
|
|
|
Self {
|
2020-06-21 01:24:46 +03:00
|
|
|
package_name: package_name.clone(),
|
2020-07-29 23:20:44 +03:00
|
|
|
main_file_path,
|
2020-08-01 07:15:33 +03:00
|
|
|
output_directory,
|
2020-06-21 01:24:46 +03:00
|
|
|
program: Program::new(package_name),
|
2020-08-01 05:39:30 +03:00
|
|
|
program_input: Input::new(),
|
2020-07-08 07:05:03 +03:00
|
|
|
imported_programs: ImportParser::new(),
|
2020-06-09 03:28:09 +03:00
|
|
|
_engine: PhantomData,
|
2020-07-30 04:32:35 +03:00
|
|
|
_group: PhantomData,
|
2020-06-09 03:28:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 23:20:44 +03:00
|
|
|
/// Parses program files.
|
|
|
|
/// Returns a compiler struct that stores the typed program abstract syntax trees (ast).
|
2020-08-01 05:39:30 +03:00
|
|
|
pub fn parse_program_without_input(
|
2020-07-30 04:32:35 +03:00
|
|
|
package_name: String,
|
|
|
|
main_file_path: PathBuf,
|
2020-08-01 07:15:33 +03:00
|
|
|
output_directory: PathBuf,
|
2020-07-30 04:32:35 +03:00
|
|
|
) -> Result<Self, CompilerError> {
|
2020-08-01 07:15:33 +03:00
|
|
|
let mut compiler = Self::new(package_name, main_file_path, output_directory);
|
2020-07-29 23:20:44 +03:00
|
|
|
|
|
|
|
let program_string = compiler.load_program()?;
|
|
|
|
compiler.parse_program(&program_string)?;
|
|
|
|
|
|
|
|
Ok(compiler)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses input, state, and program files.
|
2020-08-01 05:39:30 +03:00
|
|
|
/// Returns a compiler struct that stores the typed input and typed program abstract syntax trees (ast).
|
|
|
|
pub fn parse_program_with_input(
|
2020-07-29 06:46:26 +03:00
|
|
|
package_name: String,
|
|
|
|
main_file_path: PathBuf,
|
2020-08-01 07:15:33 +03:00
|
|
|
output_directory: PathBuf,
|
2020-08-01 05:39:30 +03:00
|
|
|
input_string: &str,
|
2020-07-29 23:20:44 +03:00
|
|
|
state_string: &str,
|
2020-07-29 06:46:26 +03:00
|
|
|
) -> Result<Self, CompilerError> {
|
2020-08-01 07:15:33 +03:00
|
|
|
let mut compiler = Self::new(package_name, main_file_path, output_directory);
|
2020-05-17 08:05:26 +03:00
|
|
|
|
2020-08-01 05:39:30 +03:00
|
|
|
compiler.parse_input(input_string, state_string)?;
|
2020-07-29 06:46:26 +03:00
|
|
|
|
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-07-29 23:20:44 +03:00
|
|
|
/// Parse the input and state files.
|
2020-08-01 05:39:30 +03:00
|
|
|
/// Stores a typed ast of all input variables to the program.
|
|
|
|
pub fn parse_input(&mut self, input_string: &str, state_string: &str) -> Result<(), CompilerError> {
|
|
|
|
let input_syntax_tree = LeoInputParser::parse_file(&input_string)?;
|
|
|
|
let state_syntax_tree = LeoInputParser::parse_file(&state_string)?;
|
2020-06-21 01:24:46 +03:00
|
|
|
|
2020-08-01 05:39:30 +03:00
|
|
|
self.program_input.parse_input(input_syntax_tree)?;
|
|
|
|
self.program_input.parse_state(state_syntax_tree)?;
|
2020-06-11 02:14:55 +03:00
|
|
|
|
2020-07-29 23:20:44 +03:00
|
|
|
Ok(())
|
2020-07-29 10:18:19 +03:00
|
|
|
}
|
|
|
|
|
2020-07-29 23:20:44 +03:00
|
|
|
/// Parse the program file and all associated import files.
|
2020-07-29 10:18:19 +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)?;
|
|
|
|
|
|
|
|
// Build program from syntax tree
|
|
|
|
let package_name = self.package_name.clone();
|
|
|
|
|
|
|
|
self.program = Program::from(syntax_tree, package_name);
|
|
|
|
self.imported_programs = ImportParser::parse(&self.program)?;
|
|
|
|
|
|
|
|
log::debug!("Program parsing complete\n{:#?}", self.program);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-07-29 23:20:44 +03:00
|
|
|
/// Loads the program file at `main_file_path`.
|
|
|
|
fn load_program(&mut self) -> Result<String, CompilerError> {
|
|
|
|
Ok(LeoParser::load_file(&self.main_file_path)?)
|
|
|
|
}
|
2020-07-29 10:18:19 +03:00
|
|
|
|
2020-08-01 05:39:30 +03:00
|
|
|
/// Manually sets main function input
|
|
|
|
pub fn set_main_input(&mut self, input: MainInput) {
|
|
|
|
self.program_input.set_main_input(input);
|
2020-07-30 09:32:21 +03:00
|
|
|
}
|
|
|
|
|
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();
|
2020-06-30 00:11:12 +03:00
|
|
|
hasher.update(unparsed_file.as_bytes());
|
|
|
|
let hash = hasher.finalize();
|
2020-05-03 06:10:22 +03:00
|
|
|
|
|
|
|
Ok(hex::encode(hash))
|
|
|
|
}
|
|
|
|
|
2020-08-01 05:39:30 +03:00
|
|
|
/// Synthesizes the circuit without program input to verify correctness.
|
2020-07-30 04:32:35 +03:00
|
|
|
pub fn compile_constraints<CS: ConstraintSystem<F>>(self, cs: &mut CS) -> Result<OutputBytes, CompilerError> {
|
2020-06-23 08:32:57 +03:00
|
|
|
let path = self.main_file_path;
|
2020-08-01 05:39:30 +03:00
|
|
|
let input = self.program_input.empty();
|
2020-07-02 07:43:06 +03:00
|
|
|
|
2020-08-01 05:39:30 +03:00
|
|
|
generate_constraints::<F, G, CS>(cs, self.program, input, &self.imported_programs).map_err(|mut error| {
|
2020-06-23 08:32:57 +03:00
|
|
|
error.set_path(path);
|
|
|
|
|
|
|
|
error
|
|
|
|
})
|
2020-05-17 08:05:26 +03:00
|
|
|
}
|
|
|
|
|
2020-08-01 05:39:30 +03:00
|
|
|
/// Synthesizes the circuit for test functions with program input.
|
2020-06-08 09:30:39 +03:00
|
|
|
pub fn compile_test_constraints(self, cs: &mut TestConstraintSystem<F>) -> Result<(), CompilerError> {
|
2020-08-01 05:39:30 +03:00
|
|
|
generate_test_constraints::<F, G>(cs, self.program, self.program_input, &self.imported_programs)
|
2020-06-05 03:39:57 +03:00
|
|
|
}
|
|
|
|
|
2020-07-30 06:52:37 +03:00
|
|
|
/// Calls the internal generate_constraints method with arguments
|
|
|
|
pub fn generate_constraints_helper<CS: ConstraintSystem<F>>(
|
|
|
|
self,
|
|
|
|
cs: &mut CS,
|
|
|
|
) -> Result<OutputBytes, CompilerError> {
|
2020-07-31 03:19:10 +03:00
|
|
|
let path = self.main_file_path;
|
2020-08-01 05:39:30 +03:00
|
|
|
generate_constraints::<_, G, _>(cs, self.program, self.program_input, &self.imported_programs).map_err(
|
2020-07-31 03:19:10 +03:00
|
|
|
|mut error| {
|
|
|
|
error.set_path(path);
|
|
|
|
|
|
|
|
error
|
|
|
|
},
|
|
|
|
)
|
2020-07-30 06:52:37 +03:00
|
|
|
}
|
|
|
|
|
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)?;
|
2020-08-01 05:39:30 +03:00
|
|
|
let program_input = Input::new();
|
2020-06-25 07:40:17 +03:00
|
|
|
|
2020-06-25 07:05:11 +03:00
|
|
|
Ok(Self {
|
|
|
|
package_name: program.name.clone(),
|
|
|
|
main_file_path: PathBuf::new(),
|
2020-08-01 07:15:33 +03:00
|
|
|
output_directory: PathBuf::new(),
|
2020-06-25 07:05:11 +03:00
|
|
|
program,
|
2020-08-01 05:39:30 +03:00
|
|
|
program_input,
|
2020-07-08 07:05:03 +03:00
|
|
|
imported_programs: ImportParser::new(),
|
2020-06-25 07:05:11 +03:00
|
|
|
_engine: PhantomData,
|
2020-07-30 04:32:35 +03:00
|
|
|
_group: PhantomData,
|
2020-06-25 07:05:11 +03:00
|
|
|
})
|
|
|
|
}
|
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-08-01 05:39:30 +03:00
|
|
|
/// Synthesizes the circuit with program input.
|
2020-06-08 09:30:39 +03:00
|
|
|
fn generate_constraints<CS: ConstraintSystem<F>>(self, cs: &mut CS) -> Result<(), SynthesisError> {
|
2020-08-01 07:15:33 +03:00
|
|
|
let output_directory = self.output_directory.clone();
|
2020-07-30 06:52:37 +03:00
|
|
|
let package_name = self.package_name.clone();
|
|
|
|
let result = self.generate_constraints_helper(cs).map_err(|e| {
|
|
|
|
log::error!("{}", e);
|
|
|
|
SynthesisError::Unsatisfiable
|
|
|
|
})?;
|
2020-05-06 03:24:34 +03:00
|
|
|
|
2020-07-30 04:32:35 +03:00
|
|
|
log::info!("Program circuit successfully synthesized!");
|
|
|
|
|
2020-07-30 06:52:37 +03:00
|
|
|
// Write results to file
|
2020-08-01 07:15:33 +03:00
|
|
|
let output_file = OutputFile::new(&package_name);
|
|
|
|
output_file.write(&output_directory, result.bytes()).unwrap();
|
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
|
|
|
}
|