merge master

This commit is contained in:
collin 2020-06-19 23:16:12 -07:00
commit 9e93462f2b
34 changed files with 103 additions and 78 deletions

View File

@ -355,8 +355,10 @@ function main() -> Circ {
## Imports
Both struct and function imports are supported.
```leo
import all: `*`
import alias: `symbol as alias`
```
`src/simple_import.leo`
```rust

View File

@ -1,10 +1,9 @@
use crate::{
ast::Rule,
common::{LineEnd, Variable},
common::{Declare, LineEnd, Variable},
expressions::Expression,
};
use crate::common::Declare;
use pest::Span;
use pest_ast::FromPest;
use std::fmt;

View File

@ -1,10 +1,9 @@
use crate::{
ast::Rule,
common::{Identifier, LineEnd, Variable},
common::{Declare, Identifier, LineEnd, Variable},
expressions::Expression,
};
use crate::common::Declare;
use pest::Span;
use pest_ast::FromPest;
use std::fmt;

View File

@ -105,12 +105,8 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
Ok(())
}
pub fn parse_inputs<'ast>(
&mut self,
input_file_path: &'ast PathBuf,
input_file_string: &'ast str,
) -> Result<(), CompilerError> {
let syntax_tree = LeoInputsParser::parse_file(input_file_path, input_file_string)?;
pub fn parse_inputs(&mut self, inputs_string: &str) -> Result<(), CompilerError> {
let syntax_tree = LeoInputsParser::parse_file(&inputs_string)?;
// Check number/order of parameters here
self.program_inputs = Inputs::from_inputs_file(syntax_tree, self.program.expected_inputs.clone())?;

View File

@ -121,7 +121,7 @@ fn test_not_u32() {
#[test]
fn test_true_or_true() {
let bytes = include_bytes!("true_||_true.leo");
let bytes = include_bytes!("true_or_true.leo");
let program = parse_program(bytes).unwrap();
output_true(program);
@ -129,7 +129,7 @@ fn test_true_or_true() {
#[test]
fn test_true_or_false() {
let bytes = include_bytes!("true_||_false.leo");
let bytes = include_bytes!("true_or_false.leo");
let program = parse_program(bytes).unwrap();
output_true(program);
@ -137,7 +137,7 @@ fn test_true_or_false() {
#[test]
fn test_false_or_false() {
let bytes = include_bytes!("false_||_false.leo");
let bytes = include_bytes!("false_or_false.leo");
let program = parse_program(bytes).unwrap();
output_false(program);
@ -145,7 +145,7 @@ fn test_false_or_false() {
#[test]
fn test_true_or_u32() {
let bytes = include_bytes!("true_||_u32.leo");
let bytes = include_bytes!("true_or_u32.leo");
let program = parse_program(bytes).unwrap();
fail_enforce(program);
@ -155,7 +155,7 @@ fn test_true_or_u32() {
#[test]
fn test_true_and_true() {
let bytes = include_bytes!("true_&&_true.leo");
let bytes = include_bytes!("true_and_true.leo");
let program = parse_program(bytes).unwrap();
output_true(program);
@ -163,7 +163,7 @@ fn test_true_and_true() {
#[test]
fn test_true_and_false() {
let bytes = include_bytes!("true_&&_false.leo");
let bytes = include_bytes!("true_and_false.leo");
let program = parse_program(bytes).unwrap();
output_false(program);
@ -171,7 +171,7 @@ fn test_true_and_false() {
#[test]
fn test_false_and_false() {
let bytes = include_bytes!("false_&&_false.leo");
let bytes = include_bytes!("false_and_false.leo");
let program = parse_program(bytes).unwrap();
output_false(program);
@ -179,7 +179,7 @@ fn test_false_and_false() {
#[test]
fn test_true_and_u32() {
let bytes = include_bytes!("true_&&_u32.leo");
let bytes = include_bytes!("true_and_u32.leo");
let program = parse_program(bytes).unwrap();
fail_enforce(program);

View File

@ -101,6 +101,7 @@ fn test_multiple_returns() {
output_multiple(program);
}
#[test]
fn test_multiple_returns_main() {
let bytes = include_bytes!("multiple_main.leo");

View File

@ -2,8 +2,6 @@ use crate::{boolean::output_true, parse_program};
use leo_compiler::errors::CompilerError;
use leo_inputs::InputParserError;
use std::path::PathBuf;
fn fail_input_parser(error: CompilerError) {
match error {
CompilerError::InputParserError(InputParserError::InputNotFound(_)) => {}
@ -14,12 +12,11 @@ fn fail_input_parser(error: CompilerError) {
#[test]
fn test_inputs_pass() {
let program_bytes = include_bytes!("main.leo");
let input_bytes = include_bytes!("inputs.leo");
let input_bytes = include_bytes!("main.in");
let input_string = String::from_utf8_lossy(input_bytes);
let mut program = parse_program(program_bytes).unwrap();
let path = PathBuf::new();
program.parse_inputs(&path, &input_string).unwrap();
program.parse_inputs(&input_string).unwrap();
output_true(program);
}
@ -27,12 +24,11 @@ fn test_inputs_pass() {
#[test]
fn test_inputs_fail_name() {
let program_bytes = include_bytes!("main.leo");
let input_bytes = include_bytes!("inputs_fail_name.leo");
let input_bytes = include_bytes!("main_fail_name.in");
let input_string = String::from_utf8_lossy(input_bytes);
let mut program = parse_program(program_bytes).unwrap();
let path = PathBuf::new();
let error = program.parse_inputs(&path, &input_string).unwrap_err();
let error = program.parse_inputs(&input_string).unwrap_err();
fail_input_parser(error);
}
@ -40,12 +36,11 @@ fn test_inputs_fail_name() {
#[test]
fn test_inputs_fail_type() {
let program_bytes = include_bytes!("main.leo");
let input_bytes = include_bytes!("inputs_fail_type.leo");
let input_bytes = include_bytes!("main_fail_type.in");
let input_string = String::from_utf8_lossy(input_bytes);
let mut program = parse_program(program_bytes).unwrap();
let path = PathBuf::new();
let error = program.parse_inputs(&path, &input_string).unwrap_err();
let error = program.parse_inputs(&input_string).unwrap_err();
fail_input_parser(error);
}
@ -53,12 +48,11 @@ fn test_inputs_fail_type() {
#[test]
fn test_inputs_multiple() {
let program_bytes = include_bytes!("main_multiple.leo");
let input_bytes = include_bytes!("inputs_multiple.leo");
let input_bytes = include_bytes!("main_multiple.in");
let input_string = String::from_utf8_lossy(input_bytes);
let mut program = parse_program(program_bytes).unwrap();
let path = PathBuf::new();
program.parse_inputs(&path, &input_string).unwrap();
program.parse_inputs(&input_string).unwrap();
output_true(program);
}

View File

@ -20,7 +20,6 @@ use leo_compiler::{
use snarkos_curves::edwards_bls12::Fq;
use snarkos_models::gadgets::r1cs::TestConstraintSystem;
use std::path::PathBuf;
pub type EdwardsTestCompiler = Compiler<Fq, EdwardsGroupType>;
pub type EdwardsConstrainedValue = ConstrainedValue<Fq, EdwardsGroupType>;
@ -59,7 +58,7 @@ pub(crate) fn parse_inputs(bytes: &[u8]) -> Result<EdwardsTestCompiler, Compiler
let mut compiler = EdwardsTestCompiler::new();
compiler.parse_inputs(&PathBuf::new(), &inputs_string)?;
compiler.parse_inputs(&inputs_string)?;
Ok(compiler)
}

View File

@ -0,0 +1 @@
[main]

View File

@ -6,7 +6,6 @@ extern crate thiserror;
pub mod errors;
pub use errors::*;
//extern crate from_pest;
pub mod assignments;
mod ast;
pub mod common;
@ -29,10 +28,9 @@ impl LeoInputsParser {
}
/// Parses the input file and constructs a syntax tree.
pub fn parse_file<'a>(file_path: &'a PathBuf, input_file: &'a str) -> Result<files::File<'a>, InputParserError> {
pub fn parse_file(input_file: &str) -> Result<files::File, InputParserError> {
// Parse the file using leo.pest
let mut file = ast::parse(input_file)
.map_err(|error| InputParserError::from(error.with_path(file_path.to_str().unwrap())))?;
let mut file = ast::parse(input_file)?;
// Build the abstract syntax tree
let syntax_tree = files::File::from_pest(&mut file).map_err(|_| InputParserError::SyntaxTreeError)?;

View File

@ -66,9 +66,10 @@ impl CLI for InitCommand {
InputsDirectory::create(&path)?;
// Verify the inputs file does not exist
if !InputsFile::exists_at(&path) {
// Create the main file in the source directory
InputsFile::new(&package_name).write_to(&path)?;
let inputs_file = InputsFile::new(&package_name);
if !inputs_file.exists_at(&path) {
// Create the inputs file in the inputs directory
inputs_file.write_to(&path)?;
}
// Verify the main file does not exist

View File

@ -3,7 +3,7 @@ use crate::{
cli_types::*,
directories::{InputsDirectory, SourceDirectory},
errors::{CLIError, NewError},
files::{Gitignore, MainFile, Manifest},
files::{Gitignore, InputsFile, MainFile, Manifest},
};
use clap::ArgMatches;
@ -77,6 +77,9 @@ impl CLI for NewCommand {
// Create the inputs directory
InputsDirectory::create(&path)?;
// Create the inputs file in the inputs directory
InputsFile::new(&package_name).write_to(&path)?;
// Create the main file in the source directory
MainFile::new(&package_name).write_to(&path)?;

View File

@ -2,11 +2,9 @@ use crate::{
cli::*,
cli_types::*,
commands::SetupCommand,
directories::INPUTS_DIRECTORY_NAME,
errors::CLIError,
files::{Manifest, ProofFile, INPUTS_FILE_NAME},
files::{InputsFile, Manifest, ProofFile},
};
use leo_inputs::LeoInputsParser;
use snarkos_algorithms::snark::{create_random_proof, PreparedVerifyingKey, Proof};
use snarkos_curves::bls12_377::Bls12_377;
@ -42,14 +40,9 @@ impl CLI for ProveCommand {
let path = current_dir()?;
let package_name = Manifest::try_from(&path)?.get_package_name();
// Construct the path to the inputs file in the inputs directory
let mut inputs_file_path = path.clone();
inputs_file_path.push(INPUTS_DIRECTORY_NAME);
inputs_file_path.push(INPUTS_FILE_NAME);
// Fetch program inputs here
let inputs_file_string = LeoInputsParser::load_file(&inputs_file_path)?;
program.parse_inputs(&inputs_file_path, &inputs_file_string)?;
let inputs_string = InputsFile::new(&package_name).read_from(&path)?;
program.parse_inputs(&inputs_string)?;
// Start the timer
let start = Instant::now();

View File

@ -20,6 +20,9 @@ pub enum CLIError {
#[error("{}", _0)]
InputsDirectoryError(#[from] InputsDirectoryError),
#[error("{}", _0)]
InputsFileError(#[from] InputsFileError),
#[error("{}", _0)]
MainFileError(#[from] MainFileError),

View File

@ -0,0 +1,22 @@
use std::{io, path::PathBuf};
#[derive(Debug, Error)]
pub enum InputsFileError {
#[error("{}: {}", _0, _1)]
Crate(&'static str, String),
#[error("creating: {}", _0)]
Creating(io::Error),
#[error("Cannot read from the provided file path - {:?}", _0)]
FileReadError(PathBuf),
#[error("writing: {}", _0)]
Writing(io::Error),
}
impl From<std::io::Error> for InputsFileError {
fn from(error: std::io::Error) -> Self {
InputsFileError::Crate("std::io", format!("{}", error))
}
}

View File

@ -4,6 +4,9 @@ pub use self::checksum::*;
pub mod gitignore;
pub use self::gitignore::*;
pub mod inputs;
pub use self::inputs::*;
pub mod main;
pub use self::main::*;

View File

@ -9,7 +9,7 @@ use std::{
path::PathBuf,
};
pub static CHECKSUM_FILE_EXTENSION: &str = ".leo.checksum";
pub static CHECKSUM_FILE_EXTENSION: &str = ".sum";
#[derive(Deserialize)]
pub struct ChecksumFile {

View File

@ -35,8 +35,7 @@ impl Gitignore {
fn template(&self) -> String {
format!(
r#"/output
/.leo
r#"outputs/
"#,
)
}

View File

@ -1,11 +1,15 @@
//! The `inputs.leo` file.
use crate::{directories::inputs::INPUTS_DIRECTORY_NAME, errors::MainFileError};
use crate::{directories::inputs::INPUTS_DIRECTORY_NAME, errors::InputsFileError};
use serde::Deserialize;
use std::{fs::File, io::Write, path::PathBuf};
use std::{
fs::{self, File},
io::Write,
path::PathBuf,
};
pub static INPUTS_FILE_NAME: &str = "inputs.leo";
pub static INPUTS_FILE_EXTENSION: &str = ".in";
#[derive(Deserialize)]
pub struct InputsFile {
@ -19,25 +23,22 @@ impl InputsFile {
}
}
pub fn exists_at(path: &PathBuf) -> bool {
let mut path = path.to_owned();
if path.is_dir() {
if !path.ends_with(INPUTS_DIRECTORY_NAME) {
path.push(PathBuf::from(INPUTS_DIRECTORY_NAME));
}
path.push(PathBuf::from(INPUTS_FILE_NAME));
}
pub fn exists_at(&self, path: &PathBuf) -> bool {
let path = self.setup_file_path(path);
path.exists()
}
pub fn write_to(self, path: &PathBuf) -> Result<(), MainFileError> {
let mut path = path.to_owned();
if path.is_dir() {
if !path.ends_with(INPUTS_DIRECTORY_NAME) {
path.push(PathBuf::from(INPUTS_DIRECTORY_NAME));
}
path.push(PathBuf::from(INPUTS_FILE_NAME));
}
/// Reads the proof from the given file path if it exists.
pub fn read_from(&self, path: &PathBuf) -> Result<String, InputsFileError> {
let path = self.setup_file_path(path);
let inputs = fs::read_to_string(&path).map_err(|_| InputsFileError::FileReadError(path.clone()))?;
Ok(inputs)
}
/// Writes the standard input format to a file.
pub fn write_to(self, path: &PathBuf) -> Result<(), InputsFileError> {
let path = self.setup_file_path(path);
let mut file = File::create(&path)?;
Ok(file.write_all(self.template().as_bytes())?)
@ -45,7 +46,7 @@ impl InputsFile {
fn template(&self) -> String {
format!(
r#"// The inputs for {}/src/main.leo
r#"// The program inputs for {}/src/main.leo
[main]
a: u32 = 1;
b: u32 = 2;
@ -53,4 +54,15 @@ b: u32 = 2;
self.package_name
)
}
fn setup_file_path(&self, path: &PathBuf) -> PathBuf {
let mut path = path.to_owned();
if path.is_dir() {
if !path.ends_with(INPUTS_DIRECTORY_NAME) {
path.push(PathBuf::from(INPUTS_DIRECTORY_NAME));
}
path.push(PathBuf::from(format!("{}{}", self.package_name, INPUTS_FILE_EXTENSION)));
}
path
}
}

View File

@ -9,7 +9,7 @@ use std::{
path::PathBuf,
};
pub static PROOF_FILE_EXTENSION: &str = ".leo.proof";
pub static PROOF_FILE_EXTENSION: &str = ".proof";
#[derive(Deserialize)]
pub struct ProofFile {

View File

@ -9,7 +9,7 @@ use std::{
path::PathBuf,
};
pub static PROVING_KEY_FILE_EXTENSION: &str = ".leo.pk";
pub static PROVING_KEY_FILE_EXTENSION: &str = ".lpk";
#[derive(Deserialize)]
pub struct ProvingKeyFile {

View File

@ -9,7 +9,7 @@ use std::{
path::PathBuf,
};
pub static VERIFICATION_KEY_FILE_EXTENSION: &str = ".leo.vk";
pub static VERIFICATION_KEY_FILE_EXTENSION: &str = ".lvk";
#[derive(Deserialize)]
pub struct VerificationKeyFile {