leo/compiler/src/compiler.rs

341 lines
12 KiB
Rust
Raw Normal View History

2021-02-02 07:26:56 +03:00
// Copyright (C) 2019-2021 Aleo Systems Inc.
2020-08-18 13:50:26 +03:00
// This file is part of the Leo library.
// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
2020-05-06 04:40:25 +03:00
//! Compiles a Leo program from a file path.
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,
GroupType,
OutputBytes,
OutputFile,
};
2021-03-03 20:59:24 +03:00
use indexmap::IndexMap;
use leo_asg::Asg;
2021-03-05 01:11:17 +03:00
pub use leo_asg::{new_context, AsgContext as Context, AsgContext};
2021-03-03 20:59:24 +03:00
use leo_ast::{Input, LeoError, MainInput, Program};
2020-08-01 05:39:30 +03:00
use leo_input::LeoInputParser;
2020-08-16 08:09:22 +03:00
use leo_package::inputs::InputPairs;
2021-03-05 01:11:17 +03:00
use leo_parser::parse_ast;
use leo_state::verify_local_data_commitment;
2020-04-25 11:47:10 +03:00
2020-12-30 19:40:45 +03:00
use snarkvm_dpc::{base_dpc::instantiated::Components, SystemParameters};
2021-03-04 21:41:33 +03:00
use snarkvm_fields::PrimeField;
use snarkvm_r1cs::{ConstraintSynthesizer, ConstraintSystem, SynthesisError};
2020-04-25 11:47:10 +03:00
2020-05-04 21:40:29 +03:00
use sha2::{Digest, Sha256};
use std::{
2021-03-03 20:59:24 +03:00
cell::RefCell,
fs,
marker::PhantomData,
path::{Path, PathBuf},
2021-03-03 20:59:24 +03:00
rc::Rc,
};
2020-04-25 11:47:10 +03:00
2021-02-11 19:38:08 +03:00
thread_local! {
2021-02-25 18:40:47 +03:00
static THREAD_GLOBAL_CONTEXT: AsgContext<'static> = {
let leaked = Box::leak(Box::new(leo_asg::new_alloc_context()));
leo_asg::new_context(leaked)
}
2021-02-11 19:38:08 +03:00
}
2021-03-05 01:11:17 +03:00
/// Convenience function to return a leaked thread-local global context. Should only be used for transient programs (like cli).
2021-02-11 19:38:08 +03:00
pub fn thread_leaked_context() -> AsgContext<'static> {
THREAD_GLOBAL_CONTEXT.with(|f| *f)
}
2020-10-31 02:23:18 +03:00
/// Stores information to compile a Leo program.
#[derive(Clone)]
2021-02-18 19:39:35 +03:00
pub struct Compiler<'a, F: PrimeField, G: GroupType<F>> {
2021-02-05 04:26:29 +03:00
program_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,
program: Program,
2020-08-01 05:39:30 +03:00
program_input: Input,
context: AsgContext<'a>,
2021-02-11 19:38:08 +03:00
asg: Option<Asg<'a>>,
2021-03-03 20:59:24 +03:00
file_contents: RefCell<IndexMap<String, Rc<Vec<String>>>>,
2020-04-25 11:47:10 +03:00
_engine: PhantomData<F>,
_group: PhantomData<G>,
2020-04-25 11:47:10 +03:00
}
2021-02-18 19:39:35 +03:00
impl<'a, F: PrimeField, G: GroupType<F>> Compiler<'a, F, G> {
2020-10-27 10:31:36 +03:00
///
/// Returns a new Leo program compiler.
///
pub fn new(
package_name: String,
main_file_path: PathBuf,
output_directory: PathBuf,
context: AsgContext<'a>,
) -> Self {
2020-06-09 03:28:09 +03:00
Self {
2021-02-05 04:26:29 +03:00
program_name: package_name.clone(),
main_file_path,
2020-08-01 07:15:33 +03:00
output_directory,
program: Program::new(package_name),
2020-08-01 05:39:30 +03:00
program_input: Input::new(),
2020-12-17 00:02:31 +03:00
asg: None,
context,
2021-03-03 20:59:24 +03:00
file_contents: RefCell::new(IndexMap::new()),
2020-06-09 03:28:09 +03:00
_engine: PhantomData,
_group: PhantomData,
2020-06-09 03:28:09 +03:00
}
}
2020-10-27 10:31:36 +03:00
///
2020-10-31 02:23:18 +03:00
/// Returns a new `Compiler` from the given main file path.
2020-10-27 10:31:36 +03:00
///
2020-10-31 02:23:18 +03:00
/// Parses and stores a program from the main file path.
/// Parses and stores all imported programs.
/// Performs type inference checking on the program and imported programs.
2020-10-27 10:31:36 +03:00
///
pub fn parse_program_without_input(
package_name: String,
main_file_path: PathBuf,
output_directory: PathBuf,
context: AsgContext<'a>,
2020-10-27 10:31:36 +03:00
) -> Result<Self, CompilerError> {
let mut compiler = Self::new(package_name, main_file_path, output_directory, context);
2020-10-27 10:31:36 +03:00
2021-02-05 04:26:29 +03:00
compiler.parse_program()?;
2020-10-27 10:31:36 +03:00
Ok(compiler)
}
///
2020-10-31 02:23:18 +03:00
/// Returns a new `Compiler` from the given main file path.
2020-10-27 10:31:36 +03:00
///
2020-10-31 02:23:18 +03:00
/// Parses and stores program input from from the input file path and state file path
/// Parses and stores a program from the main file path.
/// Parses and stores all imported programs.
/// Performs type inference checking on the program, imported programs, and program input.
2020-10-27 10:31:36 +03:00
///
2021-02-17 00:30:26 +03:00
#[allow(clippy::too_many_arguments)]
2020-10-27 10:31:36 +03:00
pub fn parse_program_with_input(
package_name: String,
main_file_path: PathBuf,
output_directory: PathBuf,
input_string: &str,
input_path: &Path,
state_string: &str,
state_path: &Path,
context: AsgContext<'a>,
2020-10-27 10:31:36 +03:00
) -> Result<Self, CompilerError> {
let mut compiler = Self::new(package_name, main_file_path, output_directory, context);
2020-10-27 10:31:36 +03:00
compiler.parse_input(input_string, input_path, state_string, state_path)?;
2021-02-05 04:26:29 +03:00
compiler.parse_program()?;
2020-10-27 10:31:36 +03:00
Ok(compiler)
}
///
2020-10-31 02:23:18 +03:00
/// Parses and stores program input from from the input file path and state file path
2020-10-27 10:31:36 +03:00
///
2020-10-31 02:23:18 +03:00
/// Calls `set_path()` on compiler errors with the given input file path or state file path
2020-10-27 10:31:36 +03:00
///
2020-09-03 08:59:54 +03:00
pub fn parse_input(
&mut self,
input_string: &str,
input_path: &Path,
2020-09-03 08:59:54 +03:00
state_string: &str,
state_path: &Path,
2020-09-03 08:59:54 +03:00
) -> Result<(), CompilerError> {
let input_syntax_tree = LeoInputParser::parse_file(&input_string).map_err(|mut e| {
2021-03-03 20:59:24 +03:00
e.set_path(
input_path.to_str().unwrap_or_default(),
&input_string.lines().map(|x| x.to_string()).collect::<Vec<String>>()[..],
);
2020-09-03 08:59:54 +03:00
e
})?;
2020-09-03 09:05:03 +03:00
let state_syntax_tree = LeoInputParser::parse_file(&state_string).map_err(|mut e| {
2021-03-03 20:59:24 +03:00
e.set_path(
state_path.to_str().unwrap_or_default(),
&state_string.lines().map(|x| x.to_string()).collect::<Vec<String>>()[..],
);
2020-09-03 09:05:03 +03:00
e
})?;
2020-04-25 11:47:10 +03:00
2020-09-03 08:59:54 +03:00
self.program_input.parse_input(input_syntax_tree).map_err(|mut e| {
2021-03-03 20:59:24 +03:00
e.set_path(
input_path.to_str().unwrap_or_default(),
&input_string.lines().map(|x| x.to_string()).collect::<Vec<String>>()[..],
);
2020-09-03 08:59:54 +03:00
e
})?;
2020-09-03 09:05:03 +03:00
self.program_input.parse_state(state_syntax_tree).map_err(|mut e| {
2021-03-03 20:59:24 +03:00
e.set_path(
state_path.to_str().unwrap_or_default(),
&state_string.lines().map(|x| x.to_string()).collect::<Vec<String>>()[..],
);
2020-09-03 09:05:03 +03:00
e
})?;
2020-08-03 03:24:31 +03:00
Ok(())
2020-06-11 02:14:55 +03:00
}
2021-03-03 20:59:24 +03:00
fn resolve_content(&self, path: &str) -> Result<Rc<Vec<String>>, CompilerError> {
let mut file_contents = self.file_contents.borrow_mut();
if file_contents.contains_key(path) {
// using this pattern because of mutable reference in branch below
Ok(file_contents.get(path).unwrap().clone())
} else {
let content = fs::read_to_string(path).map_err(|e| CompilerError::FileReadError(PathBuf::from(path), e))?;
let content = Rc::new(content.lines().map(|x| x.to_string()).collect::<Vec<String>>());
file_contents.insert(path.to_string(), content);
Ok(file_contents.get(path).unwrap().clone())
}
}
2020-10-27 10:31:36 +03:00
///
2020-10-31 02:23:18 +03:00
/// Parses and stores the main program file, constructs a syntax tree, and generates a program.
///
/// Parses and stores all programs imported by the main program file.
2020-10-27 10:31:36 +03:00
///
2021-02-05 04:26:29 +03:00
pub fn parse_program(&mut self) -> Result<(), CompilerError> {
2020-10-27 10:31:36 +03:00
// Load the program file.
2021-03-03 20:59:24 +03:00
let content = fs::read_to_string(&self.main_file_path)
.map_err(|e| CompilerError::FileReadError(self.main_file_path.clone(), e))?;
2021-03-03 20:59:24 +03:00
self.parse_program_from_string(&content)
}
2020-10-27 10:31:36 +03:00
///
2020-10-31 02:23:18 +03:00
/// Equivalent to parse_and_check_program but uses the given program_string instead of a main
/// file path.
///
pub fn parse_program_from_string(&mut self, program_string: &str) -> Result<(), CompilerError> {
2021-03-03 20:59:24 +03:00
// Use the parser to construct the abstract syntax tree (ast).
let lines = program_string.lines().map(|x| x.to_string()).collect();
self.file_contents.borrow_mut().insert(
self.main_file_path.to_str().map(|x| x.to_string()).unwrap_or_default(),
Rc::new(lines),
);
2021-03-05 01:11:17 +03:00
let ast = parse_ast(self.main_file_path.to_str().unwrap_or_default(), program_string)?;
2020-10-31 02:23:18 +03:00
// Store the main program file.
2021-03-03 20:59:24 +03:00
self.program = ast.into_repr();
self.program.name = self.program_name.clone();
2020-10-31 02:23:18 +03:00
2020-08-22 02:36:50 +03:00
tracing::debug!("Program parsing complete\n{:#?}", self.program);
2021-02-05 04:26:29 +03:00
// Create a new symbol table from the program, imported_programs, and program_input.
2021-03-03 20:59:24 +03:00
let asg = Asg::new(self.context, &self.program, &mut leo_imports::ImportParser::default())?;
2021-02-05 04:26:29 +03:00
tracing::debug!("ASG generation complete");
// Store the ASG.
self.asg = Some(asg);
2020-12-17 00:02:31 +03:00
Ok(())
}
2020-10-27 10:31:36 +03:00
///
2021-02-05 04:26:29 +03:00
/// Synthesizes the circuit with program input to verify correctness.
2020-10-27 10:31:36 +03:00
///
2021-02-05 04:26:29 +03:00
pub fn compile_constraints<CS: ConstraintSystem<F>>(&self, cs: &mut CS) -> Result<OutputBytes, CompilerError> {
generate_constraints::<F, G, CS>(cs, &self.asg.as_ref().unwrap(), &self.program_input).map_err(|mut error| {
2021-03-03 20:59:24 +03:00
if let Some(path) = error.get_path().map(|x| x.to_string()) {
let content = match self.resolve_content(&path) {
Err(e) => return e,
Ok(x) => x,
};
error.set_path(&path, &content[..]);
}
2021-02-05 04:26:29 +03:00
error
})
}
2020-10-27 10:31:36 +03:00
///
2021-02-05 04:26:29 +03:00
/// Synthesizes the circuit for test functions with program input.
2020-10-27 10:31:36 +03:00
///
2021-02-05 04:26:29 +03:00
pub fn compile_test_constraints(self, input_pairs: InputPairs) -> Result<(u32, u32), CompilerError> {
generate_test_constraints::<F, G>(
&self.asg.as_ref().unwrap(),
2021-02-05 04:26:29 +03:00
input_pairs,
&self.main_file_path,
&self.output_directory,
)
}
2020-10-27 10:31:36 +03:00
///
2021-02-05 04:26:29 +03:00
/// Returns a SHA256 checksum of the program file.
2020-10-27 10:31:36 +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)
2021-03-03 20:59:24 +03:00
.map_err(|e| CompilerError::FileReadError(self.main_file_path.clone(), e))?;
// 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();
Ok(hex::encode(hash))
}
2021-02-05 07:34:24 +03:00
/// TODO (howardwu): Incorporate this for real program executions and intentionally-real
/// test executions. Exclude it for test executions on dummy data.
///
/// Verifies the input to the program.
///
pub fn verify_local_data_commitment(
&self,
system_parameters: &SystemParameters<Components>,
) -> Result<bool, CompilerError> {
let result = verify_local_data_commitment(system_parameters, &self.program_input)?;
Ok(result)
}
2020-10-31 02:23:18 +03:00
///
2021-02-05 04:26:29 +03:00
/// Manually sets main function input.
2020-10-31 02:23:18 +03:00
///
2021-02-05 04:26:29 +03:00
/// Used for testing only.
2020-10-31 02:23:18 +03:00
///
2021-02-05 04:26:29 +03:00
pub fn set_main_input(&mut self, input: MainInput) {
self.program_input.set_main_input(input);
}
2020-05-02 08:10:40 +03:00
}
2020-04-25 11:47:10 +03:00
2021-02-18 19:39:35 +03:00
impl<'a, F: PrimeField, G: GroupType<F>> ConstraintSynthesizer<F> for Compiler<'a, F, G> {
2020-10-31 02:23:18 +03:00
///
2020-08-01 05:39:30 +03:00
/// Synthesizes the circuit with program input.
2020-10-31 02:23:18 +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();
2021-02-05 04:26:29 +03:00
let package_name = self.program_name.clone();
let result = self.compile_constraints(cs).map_err(|e| {
2020-08-22 02:36:50 +03:00
tracing::error!("{}", e);
SynthesisError::Unsatisfiable
})?;
2020-05-06 03:24:34 +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
}