-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/0xEigenLabs/eigen-zkit into…
… main
- Loading branch information
Showing
20 changed files
with
302 additions
and
341 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,6 @@ members = [ | |
"zkit", | ||
"plonky", | ||
"starky", | ||
"dsl_compile" | ||
] | ||
resolver = "2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[package] | ||
name = "dsl_compile" | ||
version = "0.1.0" | ||
edition = "2021" | ||
description = "Domain Specific Language(DSL) compiler: include circom compile" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
ansi_term = "0.12.1" | ||
wast = "39.0.0" | ||
|
||
# error and log | ||
log = "0.4.0" | ||
|
||
## from circom | ||
parser = { git = "https://github.com/iden3/circom.git", version = "2.1.6" } | ||
program_structure = { git = "https://github.com/iden3/circom.git", version = "2.1.6" } | ||
type_analysis = { git = "https://github.com/iden3/circom.git", version = "2.1.5"} | ||
constraint_generation = { git = "https://github.com/iden3/circom.git", version = "2.1.6" } | ||
constraint_writers = { git = "https://github.com/iden3/circom.git", version = "2.1.5"} | ||
compiler = { git = "https://github.com/iden3/circom.git", version = "2.1.6"} | ||
dag = { git = "https://github.com/iden3/circom.git", version = "2.1.5"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
//! It's for: Domain Specific Language(DSL) compiler: now only include circom compile. | ||
mod compilation_user; | ||
mod execution_user; | ||
mod input_user; | ||
mod parser_user; | ||
mod type_analysis_user; | ||
|
||
/// Align with https://github.com/iden3/circom/blob/master/circom/Cargo.toml#L3 | ||
const CIRCOM_VERSION: &'static str = "2.1.2"; | ||
|
||
/// Compile circom circuits to r1cs, and generate witness | ||
pub fn circom_compiler( | ||
input: String, | ||
prime: String, | ||
full_simplification: String, | ||
link_directories: Vec<String>, | ||
output: String, | ||
no_simplification: bool, | ||
reduced_simplification: bool, | ||
) -> Result<(), ()> { | ||
use compilation_user::CompilerConfig; | ||
use execution_user::ExecutionConfig; | ||
let fullopt = full_simplification.len() > 0; | ||
let o2_arg = full_simplification.as_str(); | ||
let o_style = input_user::get_simplification_style( | ||
no_simplification, | ||
reduced_simplification, | ||
fullopt, | ||
&o2_arg, | ||
)?; | ||
let input = std::path::PathBuf::from(input); | ||
let output = std::path::PathBuf::from(output); | ||
|
||
let user_input = input_user::Input::new(input, output, o_style, prime, link_directories)?; | ||
let mut program_archive = parser_user::parse_project(&user_input)?; | ||
|
||
type_analysis_user::analyse_project(&mut program_archive)?; | ||
|
||
let config = ExecutionConfig { | ||
no_rounds: user_input.no_rounds(), | ||
flag_p: user_input.parallel_simplification_flag(), | ||
flag_s: user_input.reduced_simplification_flag(), | ||
flag_f: user_input.unsimplified_flag(), | ||
flag_verbose: user_input.flag_verbose(), | ||
inspect_constraints_flag: user_input.inspect_constraints_flag(), | ||
r1cs_flag: user_input.r1cs_flag(), | ||
json_constraint_flag: user_input.json_constraints_flag(), | ||
json_substitution_flag: user_input.json_substitutions_flag(), | ||
sym_flag: user_input.sym_flag(), | ||
sym: user_input.sym_file().to_string(), | ||
r1cs: user_input.r1cs_file().to_string(), | ||
json_constraints: user_input.json_constraints_file().to_string(), | ||
prime: user_input.get_prime(), | ||
}; | ||
let circuit = execution_user::execute_project(program_archive, config)?; | ||
let compilation_config = CompilerConfig { | ||
vcp: circuit, | ||
debug_output: user_input.print_ir_flag(), | ||
c_flag: user_input.c_flag(), | ||
wasm_flag: user_input.wasm_flag(), | ||
wat_flag: user_input.wat_flag(), | ||
js_folder: user_input.js_folder().to_string(), | ||
wasm_name: user_input.wasm_name().to_string(), | ||
c_folder: user_input.c_folder().to_string(), | ||
c_run_name: user_input.c_run_name().to_string(), | ||
c_file: user_input.c_file().to_string(), | ||
dat_file: user_input.dat_file().to_string(), | ||
wat_file: user_input.wat_file().to_string(), | ||
wasm_file: user_input.wasm_file().to_string(), | ||
produce_input_log: user_input.main_inputs_flag(), | ||
}; | ||
compilation_user::compile(compilation_config)?; | ||
Result::Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
use crate::errors::Result; | ||
use crate::{ | ||
merklehash::MerkleTreeGL, | ||
merklehash_bls12381::MerkleTreeBLS12381, | ||
merklehash_bn128::MerkleTreeBN128, | ||
pil2circom, | ||
polsarray::{PolKind, PolsArray}, | ||
stark_gen::StarkProof, | ||
stark_setup::StarkSetup, | ||
stark_verify::stark_verify, | ||
traits::{MerkleTree, Transcript}, | ||
transcript::TranscriptGL, | ||
transcript_bls12381::TranscriptBLS128, | ||
transcript_bn128::TranscriptBN128, | ||
types::*, | ||
ElementDigest, | ||
}; | ||
use std::fs::File; | ||
use std::io::Write; | ||
|
||
pub fn stark_prove( | ||
stark_struct: &String, | ||
pil_file: &String, | ||
norm_stage: bool, | ||
agg_stage: bool, | ||
const_pol_file: &String, | ||
cm_pol_file: &String, | ||
circom_file: &String, | ||
zkin: &String, | ||
prover_addr: &String, | ||
) -> Result<()> { | ||
let mut pil = load_json::<PIL>(pil_file.as_str()).unwrap(); | ||
let mut const_pol = PolsArray::new(&pil, PolKind::Constant); | ||
const_pol.load(const_pol_file.as_str()).unwrap(); | ||
|
||
let mut cm_pol = PolsArray::new(&pil, PolKind::Commit); | ||
cm_pol.load(cm_pol_file.as_str()).unwrap(); | ||
|
||
let stark_struct = load_json::<StarkStruct>(stark_struct.as_str()).unwrap(); | ||
match stark_struct.verificationHashType.as_str() { | ||
"BN128" => prove::<MerkleTreeBN128, TranscriptBN128>( | ||
&mut pil, | ||
&const_pol, | ||
&cm_pol, | ||
&stark_struct, | ||
false, | ||
norm_stage, | ||
circom_file, | ||
zkin, | ||
prover_addr, | ||
), | ||
"BLS12381" => prove::<MerkleTreeBLS12381, TranscriptBLS128>( | ||
&mut pil, | ||
&const_pol, | ||
&cm_pol, | ||
&stark_struct, | ||
false, | ||
norm_stage, | ||
circom_file, | ||
zkin, | ||
prover_addr, | ||
), | ||
"GL" => prove::<MerkleTreeGL, TranscriptGL>( | ||
&mut pil, | ||
&const_pol, | ||
&cm_pol, | ||
&stark_struct, | ||
agg_stage, | ||
norm_stage, | ||
circom_file, | ||
zkin, | ||
prover_addr, | ||
), | ||
_ => panic!("Invalid hashtype {}", stark_struct.verificationHashType), | ||
} | ||
} | ||
|
||
// Adopt with different curve, eg: BN128, BLS12381, Goldilocks | ||
fn prove<M: MerkleTree<MTNode = ElementDigest<4>>, T: Transcript>( | ||
pil: &mut PIL, | ||
const_pol: &PolsArray, | ||
cm_pol: &PolsArray, | ||
stark_struct: &StarkStruct, | ||
agg_stage: bool, | ||
norm_stage: bool, | ||
circom_file: &String, | ||
zkin: &String, | ||
prover_addr: &String, | ||
) -> Result<()> { | ||
let mut setup = StarkSetup::<M>::new(const_pol, pil, stark_struct, None).unwrap(); | ||
let mut starkproof = StarkProof::<M>::stark_gen::<T>( | ||
cm_pol, | ||
const_pol, | ||
&setup.const_tree, | ||
&setup.starkinfo, | ||
&setup.program, | ||
pil, | ||
&stark_struct, | ||
prover_addr, | ||
) | ||
.unwrap(); | ||
log::debug!("verify the proof..."); | ||
|
||
let result = stark_verify::<M, T>( | ||
&starkproof, | ||
&setup.const_root, | ||
&setup.starkinfo, | ||
stark_struct, | ||
&mut setup.program, | ||
) | ||
.unwrap(); | ||
|
||
assert_eq!(result, true); | ||
log::debug!("verify the proof done"); | ||
|
||
let opt = pil2circom::StarkOption { | ||
enable_input: false, | ||
verkey_input: norm_stage, | ||
skip_main: false, | ||
agg_stage: agg_stage, | ||
}; | ||
|
||
log::debug!("generate circom"); | ||
let str_ver = pil2circom::pil2circom( | ||
&pil, | ||
&setup.const_root, | ||
&stark_struct, | ||
&mut setup.starkinfo, | ||
&mut setup.program, | ||
&opt, | ||
)?; | ||
let mut file = File::create(&circom_file)?; | ||
write!(file, "{}", str_ver)?; | ||
log::debug!("generate circom done"); | ||
|
||
if !norm_stage { | ||
starkproof.rootC = None; | ||
} | ||
|
||
let input = serde_json::to_string(&starkproof)?; | ||
let mut file = File::create(&zkin)?; | ||
write!(file, "{}", input)?; | ||
log::debug!("generate zkin done"); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.