From b751e870e53f0e036e3e760497b3088ca5b4a83c Mon Sep 17 00:00:00 2001 From: howardwu Date: Thu, 4 Feb 2021 19:23:58 -0800 Subject: [PATCH] Adds initial wasm template --- .cargo/config | 5 +- Cargo.lock | 79 ++++++++--- Cargo.toml | 3 +- wasm/.resources/basic/expected_ast.json | 138 +++++++++---------- wasm/.resources/basic/main.leo | 3 +- wasm/.resources/dummy_pedersen_hash/main.leo | 2 +- wasm/Cargo.toml | 33 +++++ wasm/src/asg.rs | 123 ++++++++--------- wasm/src/ast.rs | 62 +++++++++ wasm/src/compiler.rs | 47 +++++++ wasm/src/lib.rs | 21 +++ 11 files changed, 357 insertions(+), 159 deletions(-) create mode 100644 wasm/Cargo.toml create mode 100644 wasm/src/lib.rs diff --git a/.cargo/config b/.cargo/config index d6e0c4437c..7c6d594717 100644 --- a/.cargo/config +++ b/.cargo/config @@ -1,5 +1,2 @@ -[build] +[target.'cfg(not(target_arch = "wasm32"))'] rustflags = ["-C", "target-cpu=native"] - -[net] -git-fetch-with-cli = true diff --git a/Cargo.lock b/Cargo.lock index f4ce3ed157..df932d21a7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -50,9 +50,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.37" +version = "1.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee67c11feeac938fae061b232e38e0b6d94f97a9df10e6271319325ac4c56a86" +checksum = "afddf7f520a80dbf76e6f50a35bca42a2331ef227a28b3b6dc5c2e2338d114b1" [[package]] name = "arrayref" @@ -385,6 +385,16 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "console_error_panic_hook" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8d976903543e0c48546a91908f21588a680a8c8f984df9a5d69feccb2b2a211" +dependencies = [ + "cfg-if 0.1.10", + "wasm-bindgen", +] + [[package]] name = "const_fn" version = "0.4.4" @@ -1188,9 +1198,9 @@ dependencies = [ [[package]] name = "js-sys" -version = "0.3.46" +version = "0.3.47" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf3d7383929f7c9c7c2d0fa596f325832df98c3704f2c60553080f7127a58175" +checksum = "5cfb73131c35423a367daf8cbd24100af0d077668c8c2943f0e7dd775fef0f65" dependencies = [ "wasm-bindgen", ] @@ -1422,6 +1432,19 @@ dependencies = [ "thiserror", ] +[[package]] +name = "leo-wasm" +version = "1.2.0" +dependencies = [ + "js-sys", + "leo-asg", + "leo-ast", + "leo-grammar", + "serde", + "wasm-bindgen", + "wasm-bindgen-test", +] + [[package]] name = "libc" version = "0.2.81" @@ -3158,9 +3181,9 @@ checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" [[package]] name = "wasm-bindgen" -version = "0.2.69" +version = "0.2.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3cd364751395ca0f68cafb17666eee36b63077fb5ecd972bbcd74c90c4bf736e" +checksum = "55c0f7123de74f0dab9b7d00fd614e7b19349cd1e2f5252bbe9b1754b59433be" dependencies = [ "cfg-if 1.0.0", "serde", @@ -3170,9 +3193,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.69" +version = "0.2.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1114f89ab1f4106e5b55e688b828c0ab0ea593a1ea7c094b141b14cbaaec2d62" +checksum = "7bc45447f0d4573f3d65720f636bbcc3dd6ce920ed704670118650bcd47764c7" dependencies = [ "bumpalo", "lazy_static", @@ -3185,9 +3208,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-futures" -version = "0.4.19" +version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fe9756085a84584ee9457a002b7cdfe0bfff169f45d2591d8be1345a6780e35" +checksum = "3de431a2910c86679c34283a33f66f4e4abd7e0aec27b6669060148872aadf94" dependencies = [ "cfg-if 1.0.0", "js-sys", @@ -3197,9 +3220,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.69" +version = "0.2.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a6ac8995ead1f084a8dea1e65f194d0973800c7f571f6edd70adf06ecf77084" +checksum = "3b8853882eef39593ad4174dd26fc9865a64e84026d223f63bb2c42affcbba2c" dependencies = [ "quote 1.0.7", "wasm-bindgen-macro-support", @@ -3207,9 +3230,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.69" +version = "0.2.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5a48c72f299d80557c7c62e37e7225369ecc0c963964059509fbafe917c7549" +checksum = "4133b5e7f2a531fa413b3a1695e925038a05a71cf67e87dafa295cb645a01385" dependencies = [ "proc-macro2 1.0.24", "quote 1.0.7", @@ -3220,9 +3243,33 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.69" +version = "0.2.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e7811dd7f9398f14cc76efd356f98f03aa30419dea46aa810d71e819fc97158" +checksum = "dd4945e4943ae02d15c13962b38a5b1e81eadd4b71214eee75af64a4d6a4fd64" + +[[package]] +name = "wasm-bindgen-test" +version = "0.3.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0d4da138503a4cf86801b94d95781ee3619faa8feca830569cc6b54997b8b5c" +dependencies = [ + "console_error_panic_hook", + "js-sys", + "scoped-tls", + "wasm-bindgen", + "wasm-bindgen-futures", + "wasm-bindgen-test-macro", +] + +[[package]] +name = "wasm-bindgen-test-macro" +version = "0.3.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3199c33f06500c731d5544664c24d0c2b742b98debc6b1c6f0c6d6e8fb7c19b" +dependencies = [ + "proc-macro2 1.0.24", + "quote 1.0.7", +] [[package]] name = "web-sys" diff --git a/Cargo.toml b/Cargo.toml index 83896b2c51..ef8e828d59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ path = "leo/main.rs" [workspace] members = [ + "asg", "ast", "compiler", "gadgets", @@ -35,7 +36,7 @@ members = [ "linter", "package", "state", - "asg" + "wasm" ] [dependencies.leo-ast] diff --git a/wasm/.resources/basic/expected_ast.json b/wasm/.resources/basic/expected_ast.json index cbbcf20a69..809ce782cf 100644 --- a/wasm/.resources/basic/expected_ast.json +++ b/wasm/.resources/basic/expected_ast.json @@ -1,85 +1,77 @@ { - "definitions": [ - { - "Function": { - "identifier": { - "value": "main", - "span": { - "input": "main", - "start": 9, - "end": 13 - } - }, - "parameters": [], - "returns": null, - "block": { - "statements": [ - { - "Return": { - "expression": { - "Binary": { - "operation": "Add", - "left": { - "Value": { - "Implicit": { - "Positive": { - "value": "1", - "span": { - "input": "1", - "start": 29, - "end": 30 - } - } + "name": "basic", + "expected_input": [], + "imports": [], + "circuits": {}, + "functions": { + "{\"name\":\"main\",\"span\":\"{\\\"text\\\":\\\" function main() {\\\",\\\"line\\\":1,\\\"start\\\":10,\\\"end\\\":14}\"}": { + "identifier": "{\"name\":\"main\",\"span\":\"{\\\"text\\\":\\\" function main() {\\\",\\\"line\\\":1,\\\"start\\\":10,\\\"end\\\":14}\"}", + "input": [], + "output": null, + "block": { + "statements": [ + { + "Return": { + "expression": { + "Binary": { + "left": { + "Value": { + "Implicit": [ + "1", + { + "text": " return 1 + 1", + "line": 2, + "start": 12, + "end": 13 } - } - }, - "right": { - "Value": { - "Implicit": { - "Positive": { - "value": "1", - "span": { - "input": "1", - "start": 33, - "end": 34 - } - } - } - } - }, - "span": { - "input": "1 + 1", - "start": 29, - "end": 34 + ] } + }, + "op": "Add", + "right": { + "Value": { + "Implicit": [ + "1", + { + "text": " return 1 + 1", + "line": 2, + "start": 16, + "end": 17 + } + ] + } + }, + "span": { + "text": " return 1 + 1", + "line": 2, + "start": 12, + "end": 17 } - }, - "span": { - "input": "return 1 + 1", - "start": 22, - "end": 34 } + }, + "span" : { + "text": " return 1 + 1", + "line": 2, + "start": 5, + "end": 17 } } - ], - "span": { - "input": "{\n return 1 + 1\n}", - "start": 16, - "end": 36 } - }, - "span": { - "input": "function main() {\n return 1 + 1\n}", - "start": 0, - "end": 36 + ], + "span" : { + "text": " return 1 + 1", + "line": 2, + "start": 5, + "end": 17 } + }, + "span": { + "text": " function main() {", + "line": 1, + "start": 1, + "end": 1 } } - ], - "eoi": null, - "span": { - "input": "function main() {\n return 1 + 1\n}\n", - "start": 0, - "end": 37 - } + }, + "tests": {} } \ No newline at end of file diff --git a/wasm/.resources/basic/main.leo b/wasm/.resources/basic/main.leo index 41db522f5f..ef22115243 100644 --- a/wasm/.resources/basic/main.leo +++ b/wasm/.resources/basic/main.leo @@ -1,4 +1,3 @@ -// The dummy 'pedersen-hash' main function. -function main() -> u32 { +function main() { return 1 + 1 } diff --git a/wasm/.resources/dummy_pedersen_hash/main.leo b/wasm/.resources/dummy_pedersen_hash/main.leo index af8fdfe2bc..a3413bbe39 100644 --- a/wasm/.resources/dummy_pedersen_hash/main.leo +++ b/wasm/.resources/dummy_pedersen_hash/main.leo @@ -17,7 +17,7 @@ circuit PedersenHash { } } -// The dummy 'pedersen-hash' main function. +// The 'dummy-pedersen-hash' main function. function main() -> group { const parameters = [1group; 256]; const pedersen = PedersenHash::new(parameters); diff --git a/wasm/Cargo.toml b/wasm/Cargo.toml new file mode 100644 index 0000000000..aedba85363 --- /dev/null +++ b/wasm/Cargo.toml @@ -0,0 +1,33 @@ +[package] +name = "leo-wasm" +version = "1.2.0" +authors = [ "The Aleo Team " ] +description = "WASM interpreter 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" + +[lib] +crate-type = ["cdylib", "rlib"] + +[dependencies] +leo-asg = { path = "../asg", version = "1.2.0" } +leo-ast = { path = "../ast", version = "1.2.0" } +leo-grammar = { path = "../grammar", version = "1.2.0" } + +js-sys = "0.3.47" +serde = { version = "1.0", features = ["derive"] } +wasm-bindgen = { version = "0.2", features = ["serde-serialize"] } + +[dev-dependencies] +wasm-bindgen-test = { version = "0.3.20" } diff --git a/wasm/src/asg.rs b/wasm/src/asg.rs index fd066a357d..1ee108b086 100644 --- a/wasm/src/asg.rs +++ b/wasm/src/asg.rs @@ -1,62 +1,61 @@ -// Copyright (C) 2019-2021 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 . - -use leo_ast::Ast as LeoAst; -use leo_grammar::Grammar as LeoGrammar; - -use std::path::Path; -use wasm_bindgen::prelude::*; - -#[wasm_bindgen] -pub struct Ast(String); - -#[wasm_bindgen] -impl Ast { - #[wasm_bindgen(constructor)] - pub fn new(filepath: &str, program_name: &str, program_string: &str) -> Self { - let grammar = LeoGrammar::new(&Path::new(filepath), &program_string).unwrap(); - let ast = LeoAst::new(program_name, &grammar).unwrap(); - Self(ast.to_json_string().unwrap()) - } - - #[wasm_bindgen] - pub fn to_string(&self) -> String { - self.0.clone() - } -} - -#[cfg(test)] -mod tests { - use super::*; - - use wasm_bindgen_test::*; - - #[wasm_bindgen_test] - fn ast_test() { - let expected = include_str!("../.resources/basic/expected_ast.json"); - - let filepath = "../.resources/basic/main.leo"; - let program_name = "basic"; - let program_string = include_str!("../.resources/basic/main.leo"); - - let candidate = Ast::new(filepath, program_name, program_string).to_string(); - - let expected = JsValue::from_str(expected); - let candidate = JsValue::from_serde(&candidate).unwrap(); - - assert_eq!(expected, candidate); - } -} +// // Copyright (C) 2019-2021 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 . +// +// use leo_asg::Asg as LeoAsg; +// use leo_grammar::Grammar as LeoGrammar; +// +// use std::path::Path; +// use wasm_bindgen::prelude::*; +// +// #[wasm_bindgen] +// pub struct Ast(String); +// +// #[wasm_bindgen] +// impl Ast { +// #[wasm_bindgen(constructor)] +// pub fn new(filepath: &str, program_name: &str, program_string: &str) -> Self { +// let asg = LeoAsg::new(&self.program, &mut leo_imports::ImportParser::default())?; +// Self(ast.to_json_string().unwrap()) +// } +// +// #[wasm_bindgen] +// pub fn to_string(&self) -> String { +// self.0.clone() +// } +// } +// +// #[cfg(test)] +// mod tests { +// use super::*; +// +// use wasm_bindgen_test::*; +// +// #[wasm_bindgen_test] +// fn ast_test() { +// let expected = include_str!("../.resources/basic/expected_ast.json"); +// +// let filepath = "../.resources/basic/main.leo"; +// let program_name = "basic"; +// let program_string = include_str!("../.resources/basic/main.leo"); +// +// let candidate = Ast::new(filepath, program_name, program_string).to_string(); +// +// let expected = JsValue::from_str(expected); +// let candidate = JsValue::from_serde(&candidate).unwrap(); +// +// assert_eq!(expected, candidate); +// } +// } diff --git a/wasm/src/ast.rs b/wasm/src/ast.rs index e69de29bb2..b0661c8eab 100644 --- a/wasm/src/ast.rs +++ b/wasm/src/ast.rs @@ -0,0 +1,62 @@ +// Copyright (C) 2019-2021 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 . + +use leo_ast::Ast as LeoAst; +use leo_grammar::Grammar as LeoGrammar; + +use std::path::Path; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub struct Ast(LeoAst); + +#[wasm_bindgen] +impl Ast { + #[wasm_bindgen(constructor)] + pub fn new(filepath: &str, program_name: &str, program_string: &str) -> Self { + let grammar = LeoGrammar::new(&Path::new(filepath), &program_string).unwrap(); + let ast = LeoAst::new(program_name, &grammar).unwrap(); + Self(ast) + } + + #[wasm_bindgen] + pub fn to_string(&self) -> String { + self.0.to_json_string().unwrap() + } +} + +// #[cfg(test)] +// mod tests { +// use super::*; +// +// use wasm_bindgen_test::*; +// +// #[wasm_bindgen_test] +// fn ast_test() { +// let expected = include_str!("../.resources/basic/expected_ast.json"); +// +// let filepath = "../.resources/basic/main.leo"; +// let program_name = "basic"; +// let program_string = include_str!("../.resources/basic/main.leo"); +// +// let candidate = Ast::new(filepath, program_name, program_string).to_string(); +// +// let expected = JsValue::from_str(expected); +// let candidate = JsValue::from_serde(&candidate).unwrap(); +// +// assert_eq!(expected, candidate); +// } +// } diff --git a/wasm/src/compiler.rs b/wasm/src/compiler.rs index e69de29bb2..ea5430574a 100644 --- a/wasm/src/compiler.rs +++ b/wasm/src/compiler.rs @@ -0,0 +1,47 @@ +// // Copyright (C) 2019-2021 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 . +// +// use crate::ast::Ast; +// +// use std::path::Path; +// use wasm_bindgen::prelude::*; +// +// #[wasm_bindgen] +// pub struct Compiler(String); +// +// #[wasm_bindgen] +// impl Compiler { +// #[wasm_bindgen(constructor)] +// pub fn new(filepath: &str, program_name: &str, program_string: &str) -> Self { +// let ast = Ast::new(filepath, program_name, program_string).unwrap(); +// Self(ast.to_string()) +// } +// +// #[wasm_bindgen] +// pub fn to_string(&self) -> String { +// self.0.clone() +// } +// } +// +// #[cfg(test)] +// mod tests { +// use super::*; +// +// use wasm_bindgen_test::*; +// +// #[wasm_bindgen_test] +// fn ast_test() {} +// } diff --git a/wasm/src/lib.rs b/wasm/src/lib.rs new file mode 100644 index 0000000000..1b5e5d9baa --- /dev/null +++ b/wasm/src/lib.rs @@ -0,0 +1,21 @@ +// Copyright (C) 2019-2021 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 . + +pub mod ast; +pub use ast::*; + +// pub mod compiler; +// pub use compiler::*;