Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/0xEigenLabs/eigen-zkit into…
Browse files Browse the repository at this point in the history
… main
  • Loading branch information
eigmax committed Oct 15, 2023
2 parents 3eba683 + 80ba372 commit a1ee82c
Show file tree
Hide file tree
Showing 20 changed files with 302 additions and 341 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ members = [
"zkit",
"plonky",
"starky",
"dsl_compile"
]
resolver = "2"
23 changes: 23 additions & 0 deletions dsl_compile/Cargo.toml
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"}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn compile(config: CompilerConfig) -> Result<(), ()> {
produce_input_log: config.produce_input_log,
wat_flag: config.wat_flag,
},
crate::VERSION,
crate::CIRCOM_VERSION,
)?;

if config.c_flag {
Expand Down
File renamed without changes.
File renamed without changes.
74 changes: 74 additions & 0 deletions dsl_compile/src/lib.rs
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(())
}
11 changes: 7 additions & 4 deletions zkit/src/parser_user.rs → dsl_compile/src/parser_user.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use super::input_user::Input;
use crate::VERSION;
use crate::input_user::Input;
use crate::CIRCOM_VERSION;
use program_structure::error_definition::Report;
use program_structure::program_archive::ProgramArchive;

pub fn parse_project(input_info: &Input) -> Result<ProgramArchive, ()> {
let initial_file = input_info.input_file().to_string();
let result_program_archive =
parser::run_parser(initial_file, VERSION, input_info.link_libraries.clone());
let result_program_archive = parser::run_parser(
initial_file,
CIRCOM_VERSION,
input_info.link_libraries.clone(),
);
match result_program_archive {
Result::Err((file_library, report_collection)) => {
Report::print_reports(&report_collection, &file_library);
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion plonky/src/witness/witness_calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ mod tests {
})
.collect::<HashMap<_, _>>();

let res = wtns.calculate_witness(inputs, false).unwrap();
let res = wtns.calculate_witness(inputs, true).unwrap();
for (r, w) in res.iter().zip(case.witness) {
assert_eq!(r, &BigInt::from_str(w).unwrap());
}
Expand Down
19 changes: 9 additions & 10 deletions stark-circuits/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,15 @@ the struct `ElementDigest` stands for the value of node in Merkle tree. For BN25
### Stark Genreation

- [x] F5G
- [ ] linearhash_bls12381
- [ ] merkle_hash_bls12381
- [ ] transcripts_bls12381
- [x] poseidon_bls12381
- [ ] update stark_setup,stark_gen,stark_verify to support bls12-381 curve
- [ ] update zkit main
- [x] linearhash_bls12381
- [x] merkle_hash_bls12381
- [x] transcripts_bls12381
- [x] poseidon_bls12381
- [x] update stark_setup,stark_gen,stark_verify to support bls12-381 curve
- [x] update zkit main

### Stark Verify Circuit
- [ ] merklehash.circom
- [x] merklehash.circom
- [x] poseidon.circom for bls12381
- [ ] linerhash.circom
- [ ] BN1toGL5.circom
- [ ] stark_verify.circom
- [x] linerhash.circom
- [x] stark_verify.circom
10 changes: 5 additions & 5 deletions starkjs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ will generate the PIL json, Commitment Polynomial file and Constant Polynomial f

```bash
export CIRCUIT=fib
../target/release/eigen-zkit stark_prove -s ../starky/data/starkStruct.json.gl \
RUST_LOG=debug ../target/release/eigen-zkit stark_prove -s ../starky/data/starkStruct.json.gl \
-p /tmp/$CIRCUIT.pil.json \
--o /tmp/$CIRCUIT.const \
--m /tmp/$CIRCUIT.cm -c circuits/$CIRCUIT.verifier.circom --i circuits/$CIRCUIT.verifier.zkin.json
Expand All @@ -24,14 +24,14 @@ export CIRCUIT=fib
### Recursive Layer: FRI Proof
```bash
export CIRCUIT=fib
../target/release/eigen-zkit compile -p goldilocks -i circuits/$CIRCUIT.verifier.circom -l node_modules/pil-stark/circuits.gl --O2=full -o /tmp/
RUST_LOG=debug ../target/release/eigen-zkit compile -p goldilocks -i circuits/$CIRCUIT.verifier.circom -l node_modules/pil-stark/circuits.gl --O2=full -o /tmp/

# Circom to Stark
../target/release/eigen-zkit compressor12_setup --r /tmp/$CIRCUIT.verifier.r1cs --c /tmp/c12.const --p /tmp/c12.pil --e /tmp/c12.exec
RUST_LOG=debug ../target/release/eigen-zkit compressor12_setup --r /tmp/$CIRCUIT.verifier.r1cs --c /tmp/c12.const --p /tmp/c12.pil --e /tmp/c12.exec

../target/release/eigen-zkit compressor12_exec --w /tmp/$CIRCUIT.verifier_js/$CIRCUIT.verifier.wasm --i circuits/$CIRCUIT.verifier.zkin.json --p /tmp/c12.pil --e /tmp/c12.exec --m /tmp/c12.cm
RUST_LOG=debug ../target/release/eigen-zkit compressor12_exec --w /tmp/$CIRCUIT.verifier_js/$CIRCUIT.verifier.wasm --i circuits/$CIRCUIT.verifier.zkin.json --p /tmp/c12.pil --e /tmp/c12.exec --m /tmp/c12.cm

../target/release/eigen-zkit stark_prove -s ../starky/data/c12.starkStruct.bn128.json \
RUST_LOG=debug ../target/release/eigen-zkit stark_prove -s ../starky/data/c12.starkStruct.bn128.json \
-p /tmp/c12.pil.json \
--o /tmp/c12.const \
--m /tmp/c12.cm -c circuits/circuit.circom --i circuits/circuit.zkin.json --norm_stage
Expand Down
1 change: 1 addition & 0 deletions starky/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub mod r1cs2plonk;
mod io_utils;
pub mod pil2circom;
pub mod pilcom;
pub mod prove;
pub mod serializer;
pub mod zkin_join;

Expand Down
145 changes: 145 additions & 0 deletions starky/src/prove.rs
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(())
}
12 changes: 6 additions & 6 deletions test/recursive_proof_to_snark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ RUST_LOG=info
# ./recursive_proof_to_snark.sh $i $WORKSPACE $CIRCUIT $PILEXECJS "stark"

mkdir -p $WORKSPACE/$TASK_NO
mkdir -p $RUNDIR/circuits/$TASK_NO
mkdir -p $WORKSPACE/circuits/$TASK_NO

C12_VERIFIER=$TASK_NO/${CIRCUIT}".c12"
RECURSIVE1_VERIFIER=$TASK_NO/${CIRCUIT}".recursive1"
Expand All @@ -35,9 +35,9 @@ mkdir -p $RUNDIR/circuits && node $RUNDIR/$PILEXECJS -w $RUNDIR/circuits -i $TAS
../target/release/eigen-zkit stark_prove -s ../starky/data/starkStruct.json.gl \
-p $WORKSPACE/$TASK_NO/$CIRCUIT.pil.json \
--o $WORKSPACE/$TASK_NO/$CIRCUIT.const \
--m $WORKSPACE/$TASK_NO/$CIRCUIT.cm -c $RUNDIR/circuits/$C12_VERIFIER.circom --i $RUNDIR/circuits/$C12_VERIFIER.zkin.json
--m $WORKSPACE/$TASK_NO/$CIRCUIT.cm -c $WORKSPACE/circuits/$C12_VERIFIER.circom --i $WORKSPACE/circuits/$C12_VERIFIER.zkin.json

../target/release/eigen-zkit compile -p goldilocks -i $RUNDIR/circuits/$C12_VERIFIER.circom -l $RUNDIR/node_modules/pil-stark/circuits.gl --O2=full -o $WORKSPACE/$TASK_NO
../target/release/eigen-zkit compile -p goldilocks -i $WORKSPACE/circuits/$C12_VERIFIER.circom -l $RUNDIR/node_modules/pil-stark/circuits.gl --O2=full -o $WORKSPACE/$TASK_NO

# generate the pil files and const constant polynomial files
# input files : $C12_VERIFIER.r1cs
Expand All @@ -53,7 +53,7 @@ mkdir -p $RUNDIR/circuits && node $RUNDIR/$PILEXECJS -w $RUNDIR/circuits -i $TAS
# output files : $C12_VERIFIER.cm
../target/release/eigen-zkit compressor12_exec \
--w $WORKSPACE/$C12_VERIFIER"_js"/$CIRCUIT.c12.wasm \
--i $RUNDIR/circuits/$C12_VERIFIER.zkin.json \
--i $WORKSPACE/circuits/$C12_VERIFIER.zkin.json \
--p $WORKSPACE/$C12_VERIFIER.pil \
--e $WORKSPACE/$C12_VERIFIER.exec \
--m $WORKSPACE/$C12_VERIFIER.cm
Expand All @@ -68,7 +68,7 @@ if [ "$GENERATE_PROOF_TYPE" = "stark" ]; then
../target/release/eigen-zkit stark_prove -s ../starky/data/c12.starkStruct.json \
-p $WORKSPACE/$C12_VERIFIER.pil.json \
--o $WORKSPACE/$C12_VERIFIER.const \
--m $WORKSPACE/$C12_VERIFIER.cm -c $RUNDIR/circuits/$RECURSIVE1_VERIFIER.circom --i $WORKSPACE/aggregation/$RECURSIVE1_VERIFIER/input.zkin.json --agg_stage --norm_stage
--m $WORKSPACE/$C12_VERIFIER.cm -c $WORKSPACE/circuits/$RECURSIVE1_VERIFIER.circom --i $WORKSPACE/aggregation/$RECURSIVE1_VERIFIER/input.zkin.json --agg_stage --norm_stage

else
echo "Generate snark proof"
Expand All @@ -78,5 +78,5 @@ else
../target/release/eigen-zkit stark_prove -s ../starky/data/c12.starkStruct.bn128.json \
-p $WORKSPACE/$C12_VERIFIER.pil.json \
--o $WORKSPACE/$C12_VERIFIER.const \
--m $WORKSPACE/$C12_VERIFIER.cm -c $RUNDIR/circuits/$RECURSIVE1_VERIFIER.circom --i $WORKSPACE/aggregation/$RECURSIVE1_VERIFIER/input.json --norm_stage
--m $WORKSPACE/$C12_VERIFIER.cm -c $WORKSPACE/circuits/$RECURSIVE1_VERIFIER.circom --i $WORKSPACE/aggregation/$RECURSIVE1_VERIFIER/input.json --norm_stage
fi
Loading

0 comments on commit a1ee82c

Please sign in to comment.