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

feat: unify compressor_setup and compressor_exec #152

12 changes: 8 additions & 4 deletions starkjs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ export CIRCUIT=fib
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
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

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

RUST_LOG=debug ../target/release/eigen-zkit compressor12 \
--r /tmp/$CIRCUIT.verifier.r1cs \
--w /tmp/$CIRCUIT.verifier_js/$CIRCUIT.verifier.wasm\
--i circuits/$CIRCUIT.verifier.zkin.json \
--c /tmp/c12.const \
--m /tmp/c12.cm \
--p /tmp/c12.pil.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 \
Expand Down
86 changes: 18 additions & 68 deletions starky/src/compressor12/compressor12_exec.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::compressor12::plonk_setup::PlonkSetup;
use crate::compressor12_pil::CompressorNameSpace::*;
use crate::compressor12_pil::CompressorPolName::a;
use crate::errors::EigenError;
use crate::io_utils::read_vec_from_file;
use crate::pilcom::compile_pil_from_path;
use crate::polsarray::{PolKind, PolsArray};
use num_traits::Zero;
use plonky::ff::PrimeField;
Expand All @@ -18,19 +17,21 @@ pub type Result<T> = std::result::Result<T, EigenError>;
// input files: .wasm, .exec, .pil, zkin.json(input file),
// output: .cm
pub fn exec(
plonk_setup: PlonkSetup,
input_file: &str,
wasm_file: &str,
pil_file: &str,
exec_file: &str,
pil_json_file: &str,
commit_file: &str,
) -> Result<()> {
// 0. load exec_file,
let (adds_len, s_map_column_len, adds, s_map) = read_exec_file(exec_file);

// 1. Compiles a .pil file to its json form , and save it.
// TODO: the pil_str has been compiled in plonk_setup#3
let pil_json = compile_pil_from_path(pil_file);
let mut file = File::create(Path::new(&format!("{pil_file}.json"))).unwrap();
// 0. prepare data,
let adds = plonk_setup.plonk_additions;
let s_map = plonk_setup.s_map;
let adds_len = adds.len();
let s_map_column_len = s_map[0].len();

// 1. save pil_json data..
let pil_json = plonk_setup.pil_json;
let mut file = File::create(Path::new(pil_json_file)).unwrap();
let input = serde_json::to_string(&pil_json).unwrap();
write!(file, "{}", input).unwrap();

Expand All @@ -54,10 +55,11 @@ pub fn exec(
.collect::<Vec<_>>();

for i in 0..adds_len {
let w2 = FGL::from_raw_repr(<FGL as PrimeField>::Repr::from(adds[i * 4 + 2])).unwrap();
let w3 = FGL::from_raw_repr(<FGL as PrimeField>::Repr::from(adds[i * 4 + 3])).unwrap();
// TODO: here we can's assign `let w2 = adds[i].2;`. As adds[i].2 is mont form. But here w2 need mont_reduce form.?
let w2 = FGL::from_raw_repr(adds[i].2.into_raw_repr())?;
let w3 = FGL::from_raw_repr(adds[i].3.into_raw_repr())?;

let f_w = (w[adds[i * 4] as usize] * w2) + (w[adds[i * 4 + 1] as usize] * w3);
let f_w = (w[adds[i].0] * w2) + (w[adds[i].1] * w3);
w.push(f_w);
}

Expand All @@ -67,7 +69,7 @@ pub fn exec(

for i in 0..s_map_column_len {
for c in 0..12 {
let s = s_map[i * 12 + c] as usize;
let s = s_map[c][i] as usize;

cm_pols.set_matrix(
&pil_json,
Expand Down Expand Up @@ -96,57 +98,5 @@ pub fn exec(
cm_pols.save(commit_file)?;

log::debug!("files Generated Correctly");
Result::Ok(())
}

fn read_exec_file(exec_file: &str) -> (usize, usize, Vec<u64>, Vec<u64>) {
let mut buff = read_vec_from_file(exec_file).unwrap();

let mut new_buff = buff.split_off(2);
let adds_len = buff[0] as usize;
let s_map_column_len = buff[1] as usize;

let size = adds_len * 4 + s_map_column_len * 12;
assert_eq!(new_buff.len(), size);

let s_map = new_buff.split_off(adds_len * 4);
let adds = new_buff;

(adds_len, s_map_column_len, adds, s_map)
}

#[cfg(test)]
mod test {
use super::*;
use crate::compressor12_setup::write_exec_file;

#[test]
fn test_write_and_read_exec_file() {
let file_path = String::from("/tmp/test_write_and_read_exec_file.txt");

let target_adds = vec![
// PlonkAdd()
];

let target_s_map = vec![
vec![1, 2, 4],
vec![2, 3, 42],
vec![1, 1, 3],
vec![4, 5, 2],
vec![3, 4, 5],
vec![1, 2, 4],
vec![2, 3, 42],
vec![1, 1, 3],
vec![4, 5, 2],
vec![3, 4, 5],
vec![3, 4, 5],
vec![3, 4, 5],
];

write_exec_file(&file_path, &target_adds, &target_s_map);

let (adds_len, _s_map_column_len, _adds, _s_map) = read_exec_file(&file_path);

assert_eq!(adds_len, target_adds.len());
}
Ok(())
}
18 changes: 17 additions & 1 deletion starky/src/compressor12/compressor12_pil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ macro_rules! c_mul_add {
#[cfg(test)]
mod test {
use crate::compressor12_pil::render;
use crate::pilcom::{compile_pil_from_path, compile_pil_from_str};
use std::fs::File;
use std::io::Write;
use std::path::Path;
Expand All @@ -387,8 +388,23 @@ mod test {

#[test]
fn test_render_and_compile() {
let pil_file_path = "/tmp/render_pil_rs.pil";
let pil_string = render(5, 5);
let mut file = File::create(Path::new("/tmp/render_pil_rs.pil")).unwrap();
let mut file = File::create(Path::new(pil_file_path)).unwrap();
write!(file, "{}", pil_string).unwrap();

let pil_json_from_str = compile_pil_from_str(&pil_string);
let pil_json_from_path = compile_pil_from_path(pil_file_path);

let mut file = File::create(Path::new(&format!("{pil_file_path}.str.json"))).unwrap();
let input_old = serde_json::to_string(&pil_json_from_str).unwrap();
write!(file, "{}", input_old).unwrap();

let mut file = File::create(Path::new(&format!("{pil_file_path}.path.json"))).unwrap();
let input_old = serde_json::to_string(&pil_json_from_path).unwrap();
write!(file, "{}", input_old).unwrap();

// The fileName in them are different.
// assert_eq!(pil_json_from_str, pil_json_from_path);
}
}
56 changes: 4 additions & 52 deletions starky/src/compressor12/compressor12_setup.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
#![allow(non_snake_case)]
use crate::compressor12::plonk_setup::PlonkSetup;
use crate::errors::EigenError;
use crate::io_utils::write_vec_to_file;
use crate::r1cs2plonk::PlonkAdd;
use algebraic::reader::load_r1cs;
use plonky::field_gl::GL;
use std::fs::File;
use std::io::Write;

pub type Result<T> = std::result::Result<T, EigenError>;

Expand All @@ -17,62 +13,18 @@ pub struct Options {
// setup phase:
// input: .r1cs
// output: .pil, .const, .exec,
pub fn setup(
r1cs_file: &str,
pil_file: &str,
const_file: &str,
exec_file: &str,
force_n_bits: usize,
) -> Result<()> {
pub fn setup(r1cs_file: &str, const_file: &str, force_n_bits: usize) -> Result<PlonkSetup> {
// 0. readR1cs
let r1cs = load_r1cs::<GL>(r1cs_file);
let opts = Options {
force_bits: force_n_bits,
};

// 1. plonk setup: generate plonk circuit, the pil file.
// 1. plonk setup: generate plonk circuit, the pil_json.
let res = PlonkSetup::new(&r1cs, &opts);

// 2. And write it into pil_file.
let mut file = File::create(pil_file).unwrap();
write!(file, "{}", res.pil_str).unwrap();

// 3. write const pols file
// 2. write const pols file
res.const_pols.save(const_file)?;

// 4. construct and save ExecFile: plonk additions + sMap -> BigUint64Array
write_exec_file(exec_file, &res.plonk_additions, &res.s_map);

Ok(())
}

// construct and save ExecFile: plonk additions + sMap -> BigUint64Array
pub(super) fn write_exec_file(exec_file: &str, adds: &Vec<PlonkAdd>, s_map: &Vec<Vec<u64>>) {
let adds_len = adds.len();
let s_map_row_len = s_map.len();
let s_map_column_len = s_map[0].len();

assert_eq!(s_map_row_len, 12, "s_map should have 12 rows");
let size = 2 + adds_len * 4 + s_map_row_len * s_map_column_len;

let mut buff = vec![0; size];

buff[0] = adds_len as u64;
buff[1] = s_map_column_len as u64;

for i in 0..adds_len {
buff[2 + i * 4] = adds[i].0 as u64;
buff[2 + i * 4 + 1] = adds[i].1 as u64;
buff[2 + i * 4 + 2] = adds[i].2.into();
buff[2 + i * 4 + 3] = adds[i].3.into();
}

// TODO: Should this be a fixed constant or use the s_map_row_len.
for c in 0..12 {
for i in 0..s_map_column_len {
buff[2 + adds_len * 4 + 12 * i + c] = s_map[c][i];
}
}

write_vec_to_file(exec_file, &buff).unwrap();
Ok(res)
}
29 changes: 29 additions & 0 deletions starky/src/compressor12/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
#![allow(non_snake_case)]

use crate::compressor12_exec::exec;
use crate::compressor12_setup::setup;
use algebraic::errors::EigenError;
pub type Result<T> = std::result::Result<T, EigenError>;

pub mod compressor12_exec;
pub(crate) mod compressor12_pil;
pub mod compressor12_setup;
pub(crate) mod constants;
pub(crate) mod plonk_setup;

// compress12 phase:
// input: .r1cs, .wasm, zkin.json(input_file)
// output: .const, .cm, pil.json
pub fn compress12(
force_n_bits: usize,
r1cs_file: &str,
wasm_file: &str,
input_file: &str,
const_file: &str,
commit_file: &str,
pil_json_file: &str,
) -> Result<()> {
let plonk_setup = setup(r1cs_file, const_file, force_n_bits)?;

exec(
plonk_setup,
input_file,
wasm_file,
pil_json_file,
commit_file,
)
}
9 changes: 3 additions & 6 deletions starky/src/compressor12/plonk_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use plonky::field_gl::Fr as FGL;
use plonky::field_gl::GL;
use std::collections::BTreeMap;

#[derive(Default, Debug)]
#[derive(Debug)]
pub struct PlonkSetup {
pub(crate) pil_str: String,
pub(crate) pil_json: PIL,
pub(crate) const_pols: PolsArray,
pub(crate) s_map: Vec<Vec<u64>>,
pub(crate) plonk_additions: Vec<PlonkAdd>,
Expand All @@ -26,10 +26,7 @@ impl PlonkSetup {
// 1. plonk_setup_render phase
let plonk_setup_info = PlonkSetupRenderInfo::plonk_setup_render(r1cs, opts);
// 2. render .pil file by template.
// // And save as a file.
let pil_str = compressor12_pil::render(plonk_setup_info.n_bits, plonk_setup_info.n_publics);
// let mut file = File::create(out_pil.clone()).unwrap();
// write!(file, "{}", pil_str).unwrap();

// 3. compile pil to pil_json
let pil_json = compile_pil_from_str(&pil_str);
Expand All @@ -38,7 +35,7 @@ impl PlonkSetup {
let (const_pols, s_map) = plonk_setup_compressor(r1cs, &pil_json, &plonk_setup_info);

Self {
pil_str,
pil_json,
const_pols,
s_map,
plonk_additions: plonk_setup_info.pa,
Expand Down
19 changes: 4 additions & 15 deletions test/recursive_proof_to_snark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,13 @@ mkdir -p $RUNDIR/circuits && node $RUNDIR/$PILEXECJS -w $RUNDIR/circuits -i $TAS

../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
# output files : $C12_VERIFIER.exec, $C12_VERIFIER.const $C12_VERIFIER.pil
../target/release/eigen-zkit compressor12_setup \
../target/release/eigen-zkit compressor12 \
--r $WORKSPACE/$C12_VERIFIER.r1cs \
--c $WORKSPACE/$C12_VERIFIER.const \
--p $WORKSPACE/$C12_VERIFIER.pil \
--e $WORKSPACE/$C12_VERIFIER.exec

# generate the commit polynomials files
# input files : $CIRCUIT.c12.wasm $C12_VERIFIER.zkin.json $C12_VERIFIER.pil $C12_VERIFIER.exec
# output files : $C12_VERIFIER.cm
../target/release/eigen-zkit compressor12_exec \
--w $WORKSPACE/$C12_VERIFIER"_js"/$CIRCUIT.c12.wasm \
--i $WORKSPACE/circuits/$C12_VERIFIER.zkin.json \
--p $WORKSPACE/$C12_VERIFIER.pil \
--e $WORKSPACE/$C12_VERIFIER.exec \
--m $WORKSPACE/$C12_VERIFIER.cm
--c $WORKSPACE/$C12_VERIFIER.const \
--m $WORKSPACE/$C12_VERIFIER.cm \
--p $WORKSPACE/$C12_VERIFIER.pil.json

mkdir -p $WORKSPACE/aggregation/$RECURSIVE1_VERIFIER/

Expand Down
14 changes: 4 additions & 10 deletions test/simple_bls.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,13 @@ npm run $CIRCUIT

../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 \
../target/release/eigen-zkit compressor12 \
--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
--c /tmp/c12.const \
--m /tmp/c12.cm \
--p /tmp/c12.pil.json

../target/release/eigen-zkit stark_prove -s ../starky/data/c12.starkStruct.bls12381.json \
-p /tmp/c12.pil.json \
Expand Down
Loading
Loading