core package imports wip

This commit is contained in:
collin 2020-09-12 23:10:01 -07:00
parent eff7d9d599
commit 32cf945c61
14 changed files with 337 additions and 11 deletions

14
Cargo.lock generated
View File

@ -1251,6 +1251,19 @@ dependencies = [
"tracing-subscriber",
]
[[package]]
name = "leo-core"
version = "1.0.1"
dependencies = [
"rand",
"rand_xorshift",
"snarkos-errors",
"snarkos-gadgets",
"snarkos-models",
"snarkos-utilities",
"thiserror",
]
[[package]]
name = "leo-gadgets"
version = "1.0.3"
@ -1293,6 +1306,7 @@ dependencies = [
"from-pest",
"lazy_static",
"leo-compiler",
"leo-core",
"leo-gadgets",
"leo-input",
"leo-package",

View File

@ -28,6 +28,7 @@ path = "leo/main.rs"
members = [
"ast",
"compiler",
"core",
"gadgets",
"input",
"linter",
@ -40,6 +41,10 @@ members = [
path = "./compiler"
version = "1.0.3"
[dependencies.leo-core]
path = "./core"
version = "1.0.1"
[dependencies.leo-gadgets]
path = "./gadgets"
version = "1.0.3"

View File

@ -0,0 +1,30 @@
// 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/>.
use crate::{errors::ImportError, ImportParser};
use leo_typed::Package;
pub static CORE_PACKAGE_NAME: &str = "core";
impl ImportParser {
// import a core package into scope
pub fn parse_core_package(&mut self, package: &Package) -> Result<(), ImportError> {
println!("expecting core package {}", package);
self.insert_core_package(package);
Ok(())
}
}

View File

@ -15,7 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::errors::ImportError;
use leo_typed::Program;
use leo_typed::{Package, Program};
use std::{collections::HashMap, env::current_dir};
@ -24,12 +24,14 @@ use std::{collections::HashMap, env::current_dir};
#[derive(Clone)]
pub struct ImportParser {
imports: HashMap<String, Program>,
core_packages: Vec<Package>,
}
impl ImportParser {
pub fn new() -> Self {
Self {
imports: HashMap::new(),
core_packages: vec![],
}
}
@ -38,10 +40,18 @@ impl ImportParser {
let _res = self.imports.insert(file_name, program);
}
pub(crate) fn insert_core_package(&mut self, package: &Package) {
let _res = self.core_packages.push(package.clone());
}
pub fn get(&self, file_name: &String) -> Option<&Program> {
self.imports.get(file_name)
}
pub fn get_core_packages(&self) -> &Vec<Package> {
&self.core_packages
}
pub fn parse(program: &Program) -> Result<Self, ImportError> {
let mut imports = Self::new();

View File

@ -15,6 +15,9 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
/// The import parser creates a hashmap of import program names -> import program structs
pub mod core_package;
pub use self::core_package::*;
pub mod parse_symbol;
pub use self::parse_symbol::*;

View File

@ -14,7 +14,7 @@
// 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/>.
use crate::{errors::ImportError, ImportParser};
use crate::{errors::ImportError, ImportParser, CORE_PACKAGE_NAME};
use leo_typed::{Package, PackageAccess};
use std::{fs, fs::DirEntry, path::PathBuf};
@ -47,20 +47,23 @@ impl ImportParser {
let error_path = path.clone();
let package_name = package.name.clone();
// trim path if importing from another file
// Fetch a core package
let core_package = package_name.name.eq(CORE_PACKAGE_NAME);
// Trim path if importing from another file
if path.is_file() {
path.pop();
}
// search for package name in local directory
// Search for package name in local directory
let mut source_directory = path.clone();
source_directory.push(SOURCE_DIRECTORY_NAME);
// search for package name in `imports` directory
// Search for package name in `imports` directory
let mut imports_directory = path.clone();
imports_directory.push(IMPORTS_DIRECTORY_NAME);
// read from local `src` directory or the current path
// Read from local `src` directory or the current path
if source_directory.exists() {
path = source_directory
}
@ -81,7 +84,10 @@ impl ImportParser {
.eq(&package_name.name)
});
if imports_directory.exists() {
if core_package {
// Enforce core library package access
self.parse_core_package(&package)
} else if imports_directory.exists() {
let entries = fs::read_dir(imports_directory)
.map_err(|error| ImportError::directory_error(error, package_name.span.clone(), error_path.clone()))?
.into_iter()

View File

@ -0,0 +1,129 @@
// 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/>.
use crate::{errors::ImportError, ConstrainedProgram, GroupType};
use leo_typed::{Identifier, ImportSymbol, Package, PackageAccess};
use snarkos_models::curves::{Field, PrimeField};
use std::collections::HashMap;
static UNSTABLE_CORE_PACKAGE_KEYWORD: &str = "unstable";
impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
pub(crate) fn store_core_package(&mut self, package: Package) {
println!("storing core package: {}", package);
// create core package list
println!("creating core package list");
let list = CorePackageList::from_package_access(package.access);
println!("{:?}", list);
// fetch packages from `leo-core`
// println!("fetching packages from leo core");
// store packages
// println!("storing dependencies from leo core into leo program");
}
}
/// A list of core package dependencies
#[derive(Debug)]
pub struct CorePackageList {
packages: Vec<CorePackage>,
}
/// A core package dependency to be imported into a Leo program
#[derive(Debug)]
pub struct CorePackage {
name: Identifier,
unstable: bool,
symbols: Vec<ImportSymbol>,
}
impl CorePackageList {
pub(crate) fn new() -> Self {
Self { packages: vec![] }
}
pub(crate) fn push(&mut self, package: CorePackage) {
self.packages.push(package);
}
// Parse all dependencies after `core.`
pub(crate) fn from_package_access(access: PackageAccess) -> Self {
let mut new = Self::new();
match access {
PackageAccess::Symbol(_symbol) => unimplemented!("cannot import a symbol directly from Leo core"),
PackageAccess::Multiple(_) => unimplemented!("multiple imports not yet implemented for Leo core"),
PackageAccess::SubPackage(package) => {
println!("importing package access {}", *package);
let core_package = CorePackage::from(*package);
new.push(core_package);
}
PackageAccess::Star(_) => unimplemented!("cannot import star from Leo core"),
}
new
}
}
impl CorePackage {
pub(crate) fn new(name: Identifier) -> Self {
Self {
name,
unstable: false,
symbols: vec![],
}
}
// Set the `unstable` flag to true if we are importing an unstable core package
pub(crate) fn set_unstable(&mut self, identifier: &Identifier) {
if identifier.name.eq(UNSTABLE_CORE_PACKAGE_KEYWORD) {
self.unstable = true;
}
}
// Recursively set all symbols we are importing from a core package
pub(crate) fn set_symbols(&mut self, access: PackageAccess) {
match access {
PackageAccess::SubPackage(package) => {
self.set_unstable(&package.name);
self.set_symbols(package.access);
}
PackageAccess::Star(_) => unimplemented!("cannot import star from core package"),
PackageAccess::Multiple(accesses) => {
for access in accesses {
self.set_symbols(access);
}
}
PackageAccess::Symbol(symbol) => self.symbols.push(symbol),
}
}
}
impl From<Package> for CorePackage {
fn from(package: Package) -> Self {
// Name of core package
let mut core_package = Self::new(package.name);
core_package.set_symbols(package.access);
core_package
}
}

View File

@ -26,19 +26,30 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
import: &Import,
imported_programs: &ImportParser,
) -> Result<(), ImportError> {
// fetch dependencies for the current import
// Fetch core dependencies
let core_dependency = imported_programs
.get_core_packages()
.iter()
.find(|package| import.package.eq(package));
if let Some(package) = core_dependency {
self.store_core_package(package.clone());
return Ok(());
}
// Fetch dependencies for the current import
let imported_symbols = ImportedSymbols::from(import);
for (package, symbol) in imported_symbols.symbols {
// find imported program
// Find imported program
let program = imported_programs
.get(&package)
.ok_or(ImportError::unknown_package(import.package.name.clone()))?;
// parse imported program
// Parse imported program
self.store_definitions(program.clone(), imported_programs)?;
// store the imported symbol
// Store the imported symbol
self.store_symbol(scope.clone(), package, &symbol, program)?;
}

View File

@ -15,6 +15,9 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
/// The import store brings an imported symbol into the main program from an import program struct
pub mod core_package;
pub use self::core_package::*;
pub mod import;
pub use self::import::*;

47
core/Cargo.toml Normal file
View File

@ -0,0 +1,47 @@
[package]
name = "leo-core"
version = "1.0.1"
authors = [ "The Aleo Team <hello@aleo.org>" ]
description = "Core package dependencies of the Leo programming language"
homepage = "https://aleo.org"
repository = "https://github.com/AleoHQ/leo"
keywords = [
"aleo",
"cryptography",
"leo",
"programming-language",
"zero-knowledge"
]
categories = [ "cryptography::cryptocurrencies", "web-programming" ]
include = [ "Cargo.toml", "src", "README.md", "LICENSE.md" ]
license = "GPL-3.0"
edition = "2018"
[dependencies.snarkos-errors]
version = "1.1.3"
default-features = false
[dependencies.snarkos-gadgets]
version = "1.1.3"
default-features = false
[dependencies.snarkos-models]
version = "1.1.3"
default-features = false
[dependencies.snarkos-utilities]
version = "1.1.3"
[dependencies.rand]
version = "0.7"
default-features = false
[dependencies.rand_xorshift]
version = "0.2"
default-features = false
[dependencies.thiserror]
version = "1.0"
[dev-dependencies.snarkos-utilities]
version = "1.1.3"

17
core/src/blake2s/mod.rs Normal file
View File

@ -0,0 +1,17 @@
// 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/>.
pub mod unstable;

View File

@ -0,0 +1,17 @@
// 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/>.
use snarkos_gadgets::algorithms::prf::blake2s_gadget;

View File

@ -0,0 +1,17 @@
// 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/>.
pub mod hash;

17
core/src/lib.rs Normal file
View File

@ -0,0 +1,17 @@
// 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/>.
pub mod blake2s;