Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wasm support - virtualize filesystem #264

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ members = [
"constraint_writers",
"constant_tracking",
"code_producers",
"dag"
]
"dag",
"virtual_fs"
]
1 change: 1 addition & 0 deletions circom/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ clap = "2.33.0"
ansi_term = "0.12.1"
wast = "39.0.0"
exitcode = "1.1.2"
virtual_fs = { path = "../virtual_fs" }
48 changes: 18 additions & 30 deletions circom/src/compilation_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use compiler::compiler_interface::{Config, VCP};
use program_structure::error_definition::Report;
use program_structure::error_code::ReportCode;
use program_structure::file_definition::FileLibrary;
use virtual_fs::{FileSystem, FsError, FsResult};
use crate::VERSION;


pub struct CompilerConfig {
pub js_folder: String,
pub wasm_name: String,
Expand All @@ -24,18 +24,17 @@ pub struct CompilerConfig {
pub vcp: VCP,
}

pub fn compile(config: CompilerConfig) -> Result<(), ()> {


if config.c_flag || config.wat_flag || config.wasm_flag{
pub fn compile(fs: &mut dyn FileSystem, config: CompilerConfig) -> FsResult<()> {
if config.c_flag || config.wat_flag || config.wasm_flag {
let circuit = compiler_interface::run_compiler(
fs,
config.vcp,
Config { debug_output: config.debug_output, produce_input_log: config.produce_input_log, wat_flag: config.wat_flag },
VERSION
)?;

if config.c_flag {
compiler_interface::write_c(&circuit, &config.c_folder, &config.c_run_name, &config.c_file, &config.dat_file)?;
compiler_interface::write_c(fs, &circuit, &config.c_folder, &config.c_run_name, &config.c_file, &config.dat_file)?;
println!(
"{} {} and {}",
Colour::Green.paint("Written successfully:"),
Expand All @@ -59,55 +58,50 @@ pub fn compile(config: CompilerConfig) -> Result<(), ()> {

match (config.wat_flag, config.wasm_flag) {
(true, true) => {
compiler_interface::write_wasm(&circuit, &config.js_folder, &config.wasm_name, &config.wat_file)?;
compiler_interface::write_wasm(fs, &circuit, &config.js_folder, &config.wasm_name, &config.wat_file)?;
println!("{} {}", Colour::Green.paint("Written successfully:"), config.wat_file);
let result = wat_to_wasm(&config.wat_file, &config.wasm_file);
let result = wat_to_wasm(fs, &config.wat_file, &config.wasm_file);
match result {
Result::Err(report) => {
Report::print_reports(&[report], &FileLibrary::new());
return Err(());
return Err(FsError::Unknown);
}
Result::Ok(()) => {
println!("{} {}", Colour::Green.paint("Written successfully:"), config.wasm_file);
}
}
}
(false, true) => {
compiler_interface::write_wasm(&circuit, &config.js_folder, &config.wasm_name, &config.wat_file)?;
let result = wat_to_wasm(&config.wat_file, &config.wasm_file);
std::fs::remove_file(&config.wat_file).unwrap();
compiler_interface::write_wasm(fs, &circuit, &config.js_folder, &config.wasm_name, &config.wat_file)?;
let result = wat_to_wasm(fs, &config.wat_file, &config.wasm_file);
fs.remove_file(&config.wat_file.into())?;
match result {
Result::Err(report) => {
Report::print_reports(&[report], &FileLibrary::new());
return Err(());
return Err(FsError::Unknown);
}
Result::Ok(()) => {
println!("{} {}", Colour::Green.paint("Written successfully:"), config.wasm_file);
}
}
}
(true, false) => {
compiler_interface::write_wasm(&circuit, &config.js_folder, &config.wasm_name, &config.wat_file)?;
compiler_interface::write_wasm(fs, &circuit, &config.js_folder, &config.wasm_name, &config.wat_file)?;
println!("{} {}", Colour::Green.paint("Written successfully:"), config.wat_file);
}
(false, false) => {}
}
}


Ok(())
}


fn wat_to_wasm(wat_file: &str, wasm_file: &str) -> Result<(), Report> {
use std::fs::read_to_string;
use std::fs::File;
use std::io::BufWriter;
use std::io::Write;
fn wat_to_wasm(fs: &mut dyn FileSystem, wat_file: &str, wasm_file: &str) -> Result<(), Report> {
use wast::Wat;
use wast::parser::{self, ParseBuffer};

let wat_contents = read_to_string(wat_file).unwrap();
let wat_contents = fs.read_string(&wat_file.into()).unwrap();

let buf = ParseBuffer::new(&wat_contents).unwrap();
let result_wasm_contents = parser::parse::<Wat>(&buf);
match result_wasm_contents {
Expand All @@ -127,14 +121,8 @@ fn wat_to_wasm(wat_file: &str, wasm_file: &str) -> Result<(), Report> {
))
}
Result::Ok(wasm_contents) => {
let file = File::create(wasm_file).unwrap();
let mut writer = BufWriter::new(file);
writer.write_all(&wasm_contents).map_err(|_err| Report::error(
format!("Error writing the circuit. Exception generated: {}", _err),
ReportCode::ErrorWat2Wasm,
))?;
writer.flush().map_err(|_err| Report::error(
format!("Error writing the circuit. Exception generated: {}", _err),
fs.write(&wasm_file.into(), &wasm_contents).map_err(|err| Report::error(
format!("Error writing the circuit. Exception generated: {:?}", err),
ReportCode::ErrorWat2Wasm,
))?;
Ok(())
Expand Down
29 changes: 15 additions & 14 deletions circom/src/execution_user.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use ansi_term::Colour;
use compiler::hir::very_concrete_program::VCP;
use constraint_writers::debug_writer::DebugWriter;
use constraint_writers::ConstraintExporter;
use program_structure::program_archive::ProgramArchive;

use virtual_fs::{FileSystem, VPath};

pub struct ExecutionConfig {
pub r1cs: String,
Expand All @@ -25,11 +24,12 @@ pub struct ExecutionConfig {
}

pub fn execute_project(
fs: &mut dyn FileSystem,
program_archive: ProgramArchive,
config: ExecutionConfig,
) -> Result<VCP, ()> {
use constraint_generation::{build_circuit, BuildConfig};
let debug = DebugWriter::new(config.json_constraints).unwrap();
let json_constraints_path: VPath = config.json_constraints.into();
let build_config = BuildConfig {
no_rounds: config.no_rounds,
flag_json_sub: config.json_substitution_flag,
Expand All @@ -43,21 +43,21 @@ pub fn execute_project(
prime : config.prime,
};
let custom_gates = program_archive.custom_gates;
let (exporter, vcp) = build_circuit(program_archive, build_config)?;
let (exporter, vcp) = build_circuit(fs, program_archive, build_config)?;
if config.r1cs_flag {
generate_output_r1cs(&config.r1cs, exporter.as_ref(), custom_gates)?;
generate_output_r1cs(fs, &config.r1cs, exporter.as_ref(), custom_gates)?;
}
if config.sym_flag {
generate_output_sym(&config.sym, exporter.as_ref())?;
generate_output_sym(fs, &config.sym, exporter.as_ref())?;
}
if config.json_constraint_flag {
generate_json_constraints(&debug, exporter.as_ref())?;
generate_json_constraints(fs, &json_constraints_path, exporter.as_ref())?;
}
Result::Ok(vcp)
}

fn generate_output_r1cs(file: &str, exporter: &dyn ConstraintExporter, custom_gates: bool) -> Result<(), ()> {
if let Result::Ok(()) = exporter.r1cs(file, custom_gates) {
fn generate_output_r1cs(fs: &mut dyn FileSystem, file: &str, exporter: &dyn ConstraintExporter, custom_gates: bool) -> Result<(), ()> {
if let Result::Ok(()) = exporter.r1cs(fs, file, custom_gates) {
println!("{} {}", Colour::Green.paint("Written successfully:"), file);
Result::Ok(())
} else {
Expand All @@ -66,8 +66,8 @@ fn generate_output_r1cs(file: &str, exporter: &dyn ConstraintExporter, custom_ga
}
}

fn generate_output_sym(file: &str, exporter: &dyn ConstraintExporter) -> Result<(), ()> {
if let Result::Ok(()) = exporter.sym(file) {
fn generate_output_sym(fs: &mut dyn FileSystem, file: &str, exporter: &dyn ConstraintExporter) -> Result<(), ()> {
if let Result::Ok(()) = exporter.sym(fs, file) {
println!("{} {}", Colour::Green.paint("Written successfully:"), file);
Result::Ok(())
} else {
Expand All @@ -77,11 +77,12 @@ fn generate_output_sym(file: &str, exporter: &dyn ConstraintExporter) -> Result<
}

fn generate_json_constraints(
debug: &DebugWriter,
fs: &mut dyn FileSystem,
json_constraints_path: &VPath,
exporter: &dyn ConstraintExporter,
) -> Result<(), ()> {
if let Ok(()) = exporter.json_constraints(&debug) {
println!("{} {}", Colour::Green.paint("Constraints written in:"), debug.json_constraints);
if let Ok(()) = exporter.json_constraints(fs, json_constraints_path) {
println!("{} {}", Colour::Green.paint("Constraints written in:"), json_constraints_path);
Result::Ok(())
} else {
eprintln!("{}", Colour::Red.paint("Could not write the output in the given path"));
Expand Down
Loading