leo/compiler/src/compiler.rs

367 lines
12 KiB
Rust
Raw Normal View History

2020-08-18 13:50:26 +03:00
// Copyright (C) 2019-2020 Aleo Systems Inc.
// 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,
2020-06-08 06:57:22 +03:00
GroupType,
OutputBytes,
2020-08-01 07:15:33 +03:00
OutputFile,
};
2020-10-31 03:31:09 +03:00
use leo_ast::{Input, LeoAst, MainInput, Program};
2020-10-31 03:17:17 +03:00
use leo_grammar::Grammar;
2020-09-18 22:26:51 +03:00
use leo_imports::ImportParser;
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;
use leo_state::verify_local_data_commitment;
2020-10-30 20:51:45 +03:00
use leo_symbol_table::SymbolTable;
use leo_type_inference::TypeInference;
2020-04-25 11:47:10 +03:00
use snarkos_dpc::{base_dpc::instantiated::Components, SystemParameters};
2020-04-25 11:47:10 +03:00
use snarkos_errors::gadgets::SynthesisError;
use snarkos_models::{
curves::{Field, PrimeField},
2020-08-04 06:45:29 +03:00
gadgets::r1cs::{ConstraintSynthesizer, ConstraintSystem},
2020-04-25 11:47:10 +03:00
};
2020-05-04 21:40:29 +03:00
use sha2::{Digest, Sha256};
use std::{
fs,
marker::PhantomData,
path::{Path, PathBuf},
};
2020-04-25 11:47:10 +03:00
2020-10-31 02:23:18 +03:00
/// Stores information to compile a Leo program.
#[derive(Clone)]
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,
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>,
_group: PhantomData<G>,
2020-04-25 11:47:10 +03:00
}
impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
2020-10-27 10:31:36 +03:00
///
/// Returns a new Leo program compiler.
///
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 {
package_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-11-11 03:23:55 +03:00
imported_programs: ImportParser::default(),
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,
) -> Result<Self, CompilerError> {
let mut compiler = Self::new(package_name, main_file_path, output_directory);
compiler.parse_and_check_program()?;
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
///
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,
) -> Result<Self, CompilerError> {
let mut compiler = Self::new(package_name, main_file_path, output_directory);
compiler.parse_input(input_string, input_path, state_string, state_path)?;
compiler.parse_and_check_program()?;
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| {
e.set_path(input_path);
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| {
e.set_path(state_path);
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| {
e.set_path(input_path);
e
})?;
2020-09-03 09:05:03 +03:00
self.program_input.parse_state(state_syntax_tree).map_err(|mut e| {
e.set_path(state_path);
e
})?;
2020-08-03 03:24:31 +03:00
Ok(())
2020-06-11 02:14:55 +03:00
}
2020-10-27 10:31:36 +03:00
///
2020-10-31 02:23:18 +03:00
/// Runs program parser and type inference checker consecutively.
2020-10-27 10:31:36 +03:00
///
pub(crate) fn parse_and_check_program(&mut self) -> Result<(), CompilerError> {
self.parse_program()?;
2020-10-27 10:31:36 +03:00
self.check_program()
}
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
///
pub(crate) fn parse_program(&mut self) -> Result<(), CompilerError> {
// Load the program file.
2020-10-31 03:17:17 +03:00
let program_string = Grammar::load_file(&self.main_file_path)?;
2020-10-31 02:23:18 +03:00
// Use the parser to construct the pest abstract syntax tree (ast).
2020-10-31 03:17:17 +03:00
let pest_ast = Grammar::new(&self.main_file_path, &program_string).map_err(|mut e| {
2020-10-27 10:31:36 +03:00
e.set_path(&self.main_file_path);
2020-09-03 08:59:54 +03:00
2020-10-27 10:31:36 +03:00
e
})?;
2020-10-31 02:23:18 +03:00
// Construct the core ast from the pest ast.
2020-10-31 03:31:09 +03:00
let core_ast = LeoAst::new(&self.package_name, &pest_ast);
2020-10-27 10:31:36 +03:00
2020-10-31 02:23:18 +03:00
// Store the main program file.
self.program = core_ast.into_repr();
// Parse and store all programs imported by the main program file.
2020-10-27 10:31:36 +03:00
self.imported_programs = ImportParser::parse(&self.program)?;
tracing::debug!("Program parsing complete\n{:#?}", self.program);
Ok(())
2020-04-25 11:47:10 +03:00
}
2020-10-27 10:31:36 +03:00
///
/// Runs a type check on the program, imports, and input.
///
/// First, a symbol table of all user defined types is created.
/// Second, a type inference check is run on the program - inferring a data type for all implicit types and
/// catching type mismatch errors.
2020-10-27 10:31:36 +03:00
///
pub(crate) fn check_program(&self) -> Result<(), CompilerError> {
2020-10-30 20:25:44 +03:00
// Create a new symbol table from the program, imported_programs, and program_input.
let symbol_table =
2020-10-30 20:44:44 +03:00
SymbolTable::new(&self.program, &self.imported_programs, &self.program_input).map_err(|mut e| {
2020-10-30 20:25:44 +03:00
e.set_path(&self.main_file_path);
e
})?;
2020-10-27 10:31:36 +03:00
// Run type inference check on program.
2020-11-11 03:34:44 +03:00
TypeInference::new(&self.program, symbol_table).map_err(|mut e| {
2020-10-30 20:25:44 +03:00
e.set_path(&self.main_file_path);
e
})?;
2020-10-27 10:31:36 +03:00
tracing::debug!("Program checks complete");
2020-10-27 10:31:36 +03:00
Ok(())
}
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.
///
/// Used for testing only.
2020-10-27 10:31:36 +03:00
///
2020-08-03 03:24:31 +03:00
#[deprecated(note = "Please use the 'parse_program' method instead.")]
pub fn parse_program_from_string(&mut self, program_string: &str) -> Result<(), CompilerError> {
// Use the given bytes to construct the abstract syntax tree.
2020-10-31 03:17:17 +03:00
let ast = Grammar::new(&self.main_file_path, &program_string).map_err(|mut e| {
e.set_path(&self.main_file_path);
2020-09-03 09:23:50 +03:00
e
})?;
// Derive the package name.
let package_name = &self.package_name;
2020-10-31 02:23:18 +03:00
// Construct the core ast from the pest ast.
2020-10-31 03:31:09 +03:00
let core_ast = LeoAst::new(package_name, &ast);
2020-10-31 02:23:18 +03:00
// Store the main program file.
self.program = core_ast.into_repr();
// Parse and store all programs imported by the main program file.
2020-10-26 22:24:24 +03:00
self.imported_programs = ImportParser::parse(&self.program)?;
2020-10-24 01:23:48 +03:00
2020-10-30 20:51:45 +03:00
// Create a new symbol table from the program, imported programs, and program input.
2020-10-30 20:44:44 +03:00
let symbol_table = SymbolTable::new(&self.program, &self.imported_programs, &self.program_input)?;
2020-10-24 01:23:48 +03:00
// Run type inference check on program.
2020-11-11 03:34:44 +03:00
TypeInference::new(&self.program, symbol_table)?;
2020-10-24 01:23:48 +03:00
2020-08-22 02:36:50 +03:00
tracing::debug!("Program parsing complete\n{:#?}", self.program);
Ok(())
}
2020-10-27 10:31:36 +03:00
///
2020-10-31 02:23:18 +03:00
/// Manually sets main function input.
///
/// Used for testing only.
2020-10-27 10:31:36 +03:00
///
2020-08-01 05:39:30 +03:00
pub fn set_main_input(&mut self, input: MainInput) {
self.program_input.set_main_input(input);
}
2020-10-27 10:31:36 +03:00
///
2020-10-31 02:23:18 +03:00
/// Verifies the input to the program.
2020-10-27 10:31:36 +03:00
///
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-27 10:31:36 +03:00
///
/// Returns a Sha256 checksum of the program file.
///
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()))?;
// 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))
}
2020-10-27 10:31:36 +03:00
///
2020-08-01 05:39:30 +03:00
/// Synthesizes the circuit without program input to verify correctness.
2020-10-27 10:31:36 +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-07-02 07:43:06 +03:00
generate_constraints::<F, G, CS>(cs, self.program, self.program_input, &self.imported_programs).map_err(
|mut error| {
error.set_path(&path);
2020-06-23 08:32:57 +03:00
error
},
)
}
2020-10-31 02:23:18 +03:00
///
2020-08-01 05:39:30 +03:00
/// Synthesizes the circuit for test functions with program input.
2020-10-31 02:23:18 +03:00
///
2020-08-22 04:08:14 +03:00
pub fn compile_test_constraints(self, input_pairs: InputPairs) -> Result<(u32, u32), CompilerError> {
generate_test_constraints::<F, G>(
self.program,
input_pairs,
&self.imported_programs,
2020-08-22 04:08:14 +03:00
&self.main_file_path,
&self.output_directory,
)
2020-06-05 03:39:57 +03:00
}
2020-10-31 02:23:18 +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);
2020-07-31 03:19:10 +03:00
error
},
)
}
2020-05-02 08:10:40 +03:00
}
2020-04-25 11:47:10 +03:00
impl<F: Field + PrimeField, G: GroupType<F>> ConstraintSynthesizer<F> for Compiler<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
///
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();
let package_name = self.package_name.clone();
let result = self.generate_constraints_helper(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
}