Skip to content

Commit

Permalink
use backend enum
Browse files Browse the repository at this point in the history
  • Loading branch information
Schaeff committed May 30, 2023
1 parent fd5bc4c commit 3bf21d1
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 50 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ members = [
"pil_analyzer",
"compiler",
"pilgen",
"halo2"
"halo2",
]

[patch."https://github.com/privacy-scaling-explorations/halo2.git"]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ which is compiled to RISCV, then to powdr-asm and finally to PIL.

*powdr*-PIL can be used to generate proofs using multiple backends, such as:

- Halo2
- eSTARKs: *powdr*-PIL is fully compatible with the eSTARKS backend from Polygon Hermez,
although not yet fully integrated in an automatic way.
- Halo2: ongoing work, should be ready soon.
- Nova: ongoing work, should be ready after soon.
- other STARKs: maybe?

Expand Down
1 change: 1 addition & 0 deletions compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ executor = { path = "../executor" }
pilgen = { path = "../pilgen" }
pil_analyzer = { path = "../pil_analyzer" }
halo2 = { path = "../halo2" }
strum = { version = "0.24.1", features = ["derive"] }
7 changes: 7 additions & 0 deletions compiler/src/backends.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use strum::{Display, EnumString, EnumVariantNames};

#[derive(Clone, EnumString, EnumVariantNames, Display)]
pub enum Backend {
#[strum(serialize = "halo2")]
Halo2,
}
35 changes: 19 additions & 16 deletions compiler/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
//! The main powdr lib, used to compile from assembly to PIL

use std::ffi::OsStr;
use std::fs;
use std::io::BufWriter;
use std::path::Path;
use std::time::Instant;
use std::{ffi::OsStr, io::Write};

mod backends;
mod verify;
use halo2::prove_ast;

pub use backends::Backend;
use number::write_polys_file;
use pil_analyzer::json_exporter;
pub use verify::{compile_asm_string_temp, verify, verify_asm_string};
Expand All @@ -27,16 +29,16 @@ pub fn compile_pil_or_asm<T: FieldElement>(
inputs: Vec<T>,
output_dir: &Path,
force_overwrite: bool,
should_prove: bool,
prove_with: Option<Backend>,
) {
if file_name.ends_with(".asm") {
compile_asm(file_name, inputs, output_dir, force_overwrite, should_prove)
compile_asm(file_name, inputs, output_dir, force_overwrite, prove_with)
} else {
compile_pil(
Path::new(file_name),
output_dir,
Some(inputs_to_query_callback(inputs)),
should_prove,
prove_with,
);
};
}
Expand All @@ -49,14 +51,14 @@ pub fn compile_pil<T: FieldElement>(
pil_file: &Path,
output_dir: &Path,
query_callback: Option<impl FnMut(&str) -> Option<T>>,
should_prove: bool,
prove_with: Option<Backend>,
) -> bool {
compile(
&pil_analyzer::analyze(pil_file),
pil_file.file_name().unwrap(),
output_dir,
query_callback,
should_prove,
prove_with,
)
}

Expand All @@ -65,7 +67,7 @@ pub fn compile_pil_ast<T: FieldElement>(
file_name: &OsStr,
output_dir: &Path,
query_callback: Option<impl FnMut(&str) -> Option<T>>,
should_prove: bool,
prove_with: Option<Backend>,
) -> bool {
// TODO exporting this to string as a hack because the parser
// is tied into the analyzer due to imports.
Expand All @@ -74,7 +76,7 @@ pub fn compile_pil_ast<T: FieldElement>(
file_name,
output_dir,
query_callback,
should_prove,
prove_with,
)
}

Expand All @@ -85,7 +87,7 @@ pub fn compile_asm<T: FieldElement>(
inputs: Vec<T>,
output_dir: &Path,
force_overwrite: bool,
should_prove: bool,
prove_with: Option<Backend>,
) {
let contents = fs::read_to_string(file_name).unwrap();
compile_asm_string(
Expand All @@ -94,7 +96,7 @@ pub fn compile_asm<T: FieldElement>(
inputs,
output_dir,
force_overwrite,
should_prove,
prove_with,
)
}

Expand All @@ -106,7 +108,7 @@ pub fn compile_asm_string<T: FieldElement>(
inputs: Vec<T>,
output_dir: &Path,
force_overwrite: bool,
should_prove: bool,
prove_with: Option<Backend>,
) {
let pil = pilgen::compile(Some(file_name), contents).unwrap_or_else(|err| {
eprintln!("Error parsing .asm file:");
Expand All @@ -131,7 +133,7 @@ pub fn compile_asm_string<T: FieldElement>(
pil_file_name.file_name().unwrap(),
output_dir,
Some(inputs_to_query_callback(inputs)),
should_prove,
prove_with,
);
}

Expand All @@ -140,7 +142,7 @@ fn compile<T: FieldElement>(
file_name: &OsStr,
output_dir: &Path,
query_callback: Option<impl FnMut(&str) -> Option<T>>,
should_prove: bool,
prove_with: Option<Backend>,
) -> bool {
let mut success = true;
let start = Instant::now();
Expand All @@ -162,8 +164,9 @@ fn compile<T: FieldElement>(
&commits,
);
log::info!("Wrote commits.bin.");
if should_prove {
let proof = prove_ast(analyzed, constants, commits);
if let Some(Backend::Halo2) = prove_with {
use std::io::Write;
let proof = halo2::prove_ast(analyzed, constants, commits);
let mut proof_file = fs::File::create(output_dir.join("proof.bin")).unwrap();
let mut proof_writer = BufWriter::new(&mut proof_file);
proof_writer.write_all(&proof).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn compile_asm_string_temp<T: FieldElement>(
_ => None,
}
}),
false,
None,
));
(pil_file_name.to_string(), temp_dir)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/tests/pil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn verify_pil(file_name: &str, query_callback: Option<fn(&str) -> Option<Gol
&input_file,
&temp_dir,
query_callback,
false,
None,
));
compiler::verify(file_name, &temp_dir);
}
Expand Down
3 changes: 0 additions & 3 deletions executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,3 @@ parser = { path = "../parser" }
pil_analyzer = { path = "../pil_analyzer" }
rayon = "1.7.0"
num-traits = "0.2.15"

[dev-dependencies]
mktemp = "0.5.0"
5 changes: 1 addition & 4 deletions halo2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,4 @@ rand = "0.8.5"

[dev-dependencies]
executor = { path = "../executor" }
pilgen = { path = "../pilgen" }

[build-dependencies]
lalrpop = "^0.19"
pilgen = { path = "../pilgen" }
32 changes: 16 additions & 16 deletions powdr_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
mod util;

use clap::{Parser, Subcommand};
use compiler::compile_pil_or_asm;
use compiler::{compile_pil_or_asm, Backend};
use env_logger::{Builder, Target};
use log::LevelFilter;
use number::{Bn254Field, FieldElement, GoldilocksField};
Expand Down Expand Up @@ -57,10 +57,10 @@ enum Commands {
#[arg(default_value_t = false)]
force: bool,

/// Generate a halo2 proof
/// Generate a proof with a given backend
#[arg(short, long)]
#[arg(default_value_t = false)]
prove: bool,
#[arg(value_parser = clap_enum_variants!(Backend))]
prove_with: Option<Backend>,
},
/// Compiles (no-std) rust code to riscv assembly, then to powdr assembly
/// and finally to PIL and generates fixed and witness columns.
Expand Down Expand Up @@ -90,10 +90,10 @@ enum Commands {
#[arg(default_value_t = false)]
force: bool,

/// Generate a halo2 proof
/// Generate a proof with a given backend
#[arg(short, long)]
#[arg(default_value_t = false)]
prove: bool,
#[arg(value_parser = clap_enum_variants!(Backend))]
prove_with: Option<Backend>,
},

/// Compiles riscv assembly to powdr assembly and then to PIL
Expand Down Expand Up @@ -123,10 +123,10 @@ enum Commands {
#[arg(default_value_t = false)]
force: bool,

/// Generate a halo2 proof.
/// Generate a proof with a given backend.
#[arg(short, long)]
#[arg(default_value_t = false)]
prove: bool,
#[arg(value_parser = clap_enum_variants!(Backend))]
prove_with: Option<Backend>,
},

/// Apply the Halo2 workflow on an input file and prover values over the Bn254 field
Expand Down Expand Up @@ -175,23 +175,23 @@ fn main() {
inputs,
output_directory,
force,
prove,
prove_with,
} => call_with_field!(
compile_rust,
field,
&file,
split_inputs(&inputs),
Path::new(&output_directory),
force,
prove
prove_with
),
Commands::RiscvAsm {
file,
field,
inputs,
output_directory,
force,
prove,
prove_with,
} => call_with_field!(
compile_riscv_asm,
field,
Expand All @@ -200,7 +200,7 @@ fn main() {
split_inputs(&inputs),
Path::new(&output_directory),
force,
prove
prove_with
),
Commands::Reformat { file } => {
let contents = fs::read_to_string(&file).unwrap();
Expand All @@ -215,15 +215,15 @@ fn main() {
output_directory,
inputs,
force,
prove,
prove_with,
} => call_with_field!(
compile_pil_or_asm,
field,
&file,
split_inputs(&inputs),
Path::new(&output_directory),
force,
prove
prove_with
),
Commands::Halo2MockProver { file, dir } => {
halo2::mock_prove::<Bn254Field>(Path::new(&file), Path::new(&dir));
Expand Down
14 changes: 7 additions & 7 deletions riscv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::{collections::BTreeMap, path::Path, process::Command};

use ::compiler::compile_asm_string;
use ::compiler::{compile_asm_string, Backend};
use mktemp::Temp;
use std::fs;
use walkdir::WalkDir;
Expand All @@ -22,7 +22,7 @@ pub fn compile_rust<T: FieldElement>(
inputs: Vec<T>,
output_dir: &Path,
force_overwrite: bool,
should_prove: bool,
prove_with: Option<Backend>,
) {
let riscv_asm = if file_name.ends_with("Cargo.toml") {
compile_rust_crate_to_riscv_asm(file_name)
Expand Down Expand Up @@ -57,7 +57,7 @@ pub fn compile_rust<T: FieldElement>(
inputs,
output_dir,
force_overwrite,
should_prove,
prove_with,
)
}

Expand All @@ -67,7 +67,7 @@ pub fn compile_riscv_asm_bundle<T: FieldElement>(
inputs: Vec<T>,
output_dir: &Path,
force_overwrite: bool,
should_prove: bool,
prove_with: Option<Backend>,
) {
let powdr_asm_file_name = output_dir.join(format!(
"{}.asm",
Expand Down Expand Up @@ -96,7 +96,7 @@ pub fn compile_riscv_asm_bundle<T: FieldElement>(
inputs,
output_dir,
force_overwrite,
should_prove,
prove_with,
)
}

Expand All @@ -108,7 +108,7 @@ pub fn compile_riscv_asm<T: FieldElement>(
inputs: Vec<T>,
output_dir: &Path,
force_overwrite: bool,
should_prove: bool,
prove_with: Option<Backend>,
) {
let contents = fs::read_to_string(file_name).unwrap();
compile_riscv_asm_bundle(
Expand All @@ -119,7 +119,7 @@ pub fn compile_riscv_asm<T: FieldElement>(
inputs,
output_dir,
force_overwrite,
should_prove,
prove_with,
)
}

Expand Down

0 comments on commit 3bf21d1

Please sign in to comment.