Migrates synthesizer to a dedicated module

This commit is contained in:
howardwu 2021-02-27 08:48:20 -08:00
parent 5d605178e4
commit ec056a2877
13 changed files with 140 additions and 68 deletions

16
Cargo.lock generated
View File

@ -1334,8 +1334,8 @@ dependencies = [
"leo-input",
"leo-package",
"leo-state",
"leo-synthesizer",
"notify",
"num-bigint",
"rand",
"rand_core",
"reqwest",
@ -1395,12 +1395,26 @@ dependencies = [
"thiserror",
]
[[package]]
name = "leo-synthesizer"
version = "1.2.3"
dependencies = [
"num-bigint",
"serde",
"serde_json",
"snarkvm-curves",
"snarkvm-errors",
"snarkvm-models",
]
[[package]]
name = "leo-wasm"
version = "1.2.3"
dependencies = [
"leo-asg",
"leo-ast",
"leo-grammar",
"leo-imports",
"serde",
"wasm-bindgen",
"wasm-bindgen-test",

View File

@ -36,6 +36,7 @@ members = [
"linter",
"package",
"state",
"synthesizer",
"wasm"
]
@ -67,6 +68,10 @@ version = "1.2.3"
path = "./state"
version = "1.2.3"
[dependencies.leo-synthesizer]
path = "./synthesizer"
version = "1.2.3"
[dependencies.snarkvm-algorithms]
version = "0.0.5"
default-features = false
@ -117,9 +122,6 @@ version = "1.4.0"
[dependencies.notify]
version = "4.0.15"
[dependencies.num-bigint]
version = "0.3"
[dependencies.rand]
version = "0.8"

View File

@ -14,11 +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::{
commands::Command,
context::Context,
synthesizer::{CircuitSynthesizer, SerializedCircuit},
};
use crate::{commands::Command, context::Context};
use leo_compiler::{
compiler::{thread_leaked_context, Compiler},
group::targets::edwards_bls12::EdwardsGroupType,
@ -28,6 +24,7 @@ use leo_package::{
outputs::{ChecksumFile, CircuitFile, OutputsDirectory, OUTPUTS_DIRECTORY_NAME},
source::{LibraryFile, MainFile, LIBRARY_FILENAME, MAIN_FILENAME, SOURCE_DIRECTORY_NAME},
};
use leo_synthesizer::{CircuitSynthesizer, SerializedCircuit};
use anyhow::Result;
use snarkvm_curves::{bls12_377::Bls12_377, edwards_bls12::Fq};

View File

@ -19,7 +19,6 @@ pub mod commands;
pub mod config;
pub mod context;
pub mod logger;
pub mod synthesizer;
pub mod updater;
#[cfg(test)]

View File

@ -19,7 +19,6 @@ pub mod commands;
pub mod config;
pub mod context;
pub mod logger;
pub mod synthesizer;
pub mod updater;
use commands::{

40
synthesizer/Cargo.toml Normal file
View File

@ -0,0 +1,40 @@
[package]
name = "leo-synthesizer"
version = "1.2.3"
authors = [ "The Aleo Team <hello@aleo.org>" ]
description = "Circuit synthesizer 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.snarkvm-curves]
version = "0.0.5"
default-features = false
[dependencies.snarkvm-errors]
version = "0.0.5"
default-features = false
[dependencies.snarkvm-models]
version = "0.0.5"
default-features = false
[dependencies.num-bigint]
version = "0.3"
[dependencies.serde]
version = "1.0"
features = [ "derive" ]
[dependencies.serde_json]
version = "1.0"

View File

@ -22,13 +22,13 @@ use snarkvm_models::{
pub struct CircuitSynthesizer<E: PairingEngine> {
// Constraints
pub(crate) at: Vec<Vec<(E::Fr, Index)>>,
pub(crate) bt: Vec<Vec<(E::Fr, Index)>>,
pub(crate) ct: Vec<Vec<(E::Fr, Index)>>,
pub at: Vec<Vec<(E::Fr, Index)>>,
pub bt: Vec<Vec<(E::Fr, Index)>>,
pub ct: Vec<Vec<(E::Fr, Index)>>,
// Assignments of variables
pub(crate) input_assignment: Vec<E::Fr>,
pub(crate) aux_assignment: Vec<E::Fr>,
pub input_assignment: Vec<E::Fr>,
pub aux_assignment: Vec<E::Fr>,
}
impl<E: PairingEngine> ConstraintSystem<E::Fr> for CircuitSynthesizer<E> {

View File

@ -24,7 +24,7 @@ use snarkvm_models::{
gadgets::r1cs::{ConstraintSystem, Index},
};
use crate::synthesizer::{CircuitSynthesizer, SerializedField, SerializedIndex};
use crate::{CircuitSynthesizer, SerializedField, SerializedIndex};
#[derive(Serialize, Deserialize)]
pub struct SerializedCircuit {

View File

@ -15,12 +15,24 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::ast::Ast;
use leo_asg::{new_alloc_context, new_context, Asg as LeoAsg};
use leo_asg::{new_alloc_context, new_context, Asg as LeoAsg, AsgContext};
use leo_grammar::Grammar as LeoGrammar;
use std::path::Path;
use std::{boxed::Box, path::Path};
use wasm_bindgen::prelude::*;
thread_local! {
static THREAD_GLOBAL_CONTEXT: AsgContext<'static> = {
let leaked = Box::leak(Box::new(leo_asg::new_alloc_context()));
leo_asg::new_context(leaked)
}
}
/// Convenience function to return a leaked thread-local global context. Should only be used for transient programs.
pub fn thread_leaked_context() -> AsgContext<'static> {
THREAD_GLOBAL_CONTEXT.with(|f| *f)
}
#[wasm_bindgen]
pub struct Asg(LeoAsg<'static>);
@ -28,8 +40,12 @@ pub struct Asg(LeoAsg<'static>);
impl Asg {
#[wasm_bindgen(constructor)]
pub fn from(ast: &Ast) -> Self {
let arena = new_alloc_context();
let asg = LeoAsg::new(new_context(&arena), &ast.0, &mut leo_imports::ImportParser::default()).unwrap();
let asg = LeoAsg::new(
thread_leaked_context(),
&ast.0,
&mut leo_imports::ImportParser::default(),
)
.unwrap();
Self(asg)
}
}

View File

@ -1,47 +1,52 @@
// // 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 <https://www.gnu.org/licenses/>.
//
// 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() {}
// }
// 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 <https://www.gnu.org/licenses/>.
use crate::{asg::Asg, ast::Ast};
use leo_compiler::generate_constraints;
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();
let asg = Asg::from(&ast);
// TODO (howardwu): Support program inputs.
generate_constraints(cs, &asg.as_ref().unwrap(), &Input::new()).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() {}
}