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: opti return result type. #158

Merged
merged 5 commits into from
Nov 12, 2023
Merged
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
17 changes: 9 additions & 8 deletions plonky/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ pub fn prove(
pub fn calculate_witness(wasm_file: &str, input_json: &str, output: &str) -> Result<()> {
let inputs = load_input_for_witness(input_json);

// let mut wtns = WitnessCalculator::new(wasm_file).unwrap();
let mut wtns = WitnessCalculator::from_file(wasm_file)?;
assert_eq!(
wtns.memory.prime.to_str_radix(16),
Expand Down Expand Up @@ -122,16 +121,17 @@ pub fn verify(vk_file: &str, proof_bin: &str, transcript: &str) -> Result<()> {
let vk = reader::load_verification_key::<Bn256>(vk_file);
let proof = reader::load_proof::<Bn256>(proof_bin);
let ok = plonk::verify(&vk, &proof, transcript)?;
if !ok {
return Err(EigenError::from("Proof is invalid".to_string()));
if ok {
Ok(())
} else {
Err(EigenError::from("Proof is invalid".to_string()))
}
Result::Ok(())
}

pub fn generate_verifier(vk_file: &str, sol: &str) -> Result<()> {
let vk = reader::load_verification_key::<Bn256>(vk_file);
bellman_vk_codegen::render_verification_key_from_default_template(&vk, sol, true);
Result::Ok(())
Ok(())
}

#[cfg(not(feature = "wasm"))]
Expand Down Expand Up @@ -184,10 +184,11 @@ pub fn aggregation_verify(proof: &str, vk: &str) -> Result<()> {
let vk = reader::load_aggregation_verification_key(vk);
let proof = reader::load_aggregated_proof(proof);
let correct = aggregation::verify(vk, proof)?;
if !correct {
return Err(EigenError::from("Proof is invalid".to_string()));
if correct {
Result::Ok(())
} else {
Err(EigenError::from("Proof is invalid".to_string()))
}
Result::Ok(())
}

// check an aggregated proof is corresponding to the original proofs
Expand Down
27 changes: 13 additions & 14 deletions starky/src/compressor12/compressor12_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,22 @@ pub fn exec(
commit_file: &str,
) -> Result<()> {
// 0. load exec_file,
let (adds_len, s_map_column_len, adds, s_map) = read_exec_file(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();
let input = serde_json::to_string(&pil_json).unwrap();
write!(file, "{}", input).unwrap();
let mut file = File::create(Path::new(&format!("{pil_file}.json")))?;
let input = serde_json::to_string(&pil_json)?;
write!(file, "{}", input)?;

// 2. construct cmPol: .pil.json -> .cm
let mut cm_pols = PolsArray::new(&pil_json, PolKind::Commit);

// 3. calculate witness. wasm+input->witness
let inputs = load_input_for_witness(input_file);
// let mut wtns = WitnessCalculator::new(wasm_file).unwrap();
let mut wtns = WitnessCalculator::from_file(wasm_file)?;
let w = wtns.calculate_witness(inputs, false).unwrap();
let inputs = load_input_for_witness(input_file);
let w = wtns.calculate_witness(inputs, false)?;
let mut w = w
.iter()
.map(|wi| {
Expand All @@ -55,8 +54,8 @@ 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();
let w2 = FGL::from_raw_repr(<FGL as PrimeField>::Repr::from(adds[i * 4 + 2]))?;
let w3 = FGL::from_raw_repr(<FGL as PrimeField>::Repr::from(adds[i * 4 + 3]))?;

let f_w = (w[adds[i * 4] as usize] * w2) + (w[adds[i * 4 + 1] as usize] * w3);
w.push(f_w);
Expand Down Expand Up @@ -100,8 +99,8 @@ pub fn exec(
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();
fn read_exec_file(exec_file: &str) -> Result<(usize, usize, Vec<u64>, Vec<u64>)> {
let mut buff = read_vec_from_file(exec_file)?;

let mut new_buff = buff.split_off(2);
let adds_len = buff[0] as usize;
Expand All @@ -113,7 +112,7 @@ fn read_exec_file(exec_file: &str) -> (usize, usize, Vec<u64>, Vec<u64>) {
let s_map = new_buff.split_off(adds_len * 4);
let adds = new_buff;

(adds_len, s_map_column_len, adds, s_map)
Ok((adds_len, s_map_column_len, adds, s_map))
}

#[cfg(test)]
Expand Down Expand Up @@ -144,9 +143,9 @@ mod test {
vec![3, 4, 5],
];

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

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

assert_eq!(adds_len, target_adds.len());
}
Expand Down
14 changes: 9 additions & 5 deletions starky/src/compressor12/compressor12_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,24 @@ pub fn setup(
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();
let mut file = File::create(pil_file)?;
write!(file, "{}", res.pil_str)?;

// 3. 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);
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>>) {
pub(super) fn write_exec_file(
exec_file: &str,
adds: &Vec<PlonkAdd>,
s_map: &Vec<Vec<u64>>,
) -> Result<()> {
let adds_len = adds.len();
let s_map_row_len = s_map.len();
let s_map_column_len = s_map[0].len();
Expand All @@ -74,5 +78,5 @@ pub(super) fn write_exec_file(exec_file: &str, adds: &Vec<PlonkAdd>, s_map: &Vec
}
}

write_vec_to_file(exec_file, &buff).unwrap();
Ok(write_vec_to_file(exec_file, &buff)?)
}
16 changes: 7 additions & 9 deletions starky/src/prove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ pub fn stark_prove(
zkin: &str,
prover_addr: &str,
) -> Result<()> {
let mut pil = load_json::<PIL>(pil_file).unwrap();
let mut pil = load_json::<PIL>(pil_file)?;
let mut const_pol = PolsArray::new(&pil, PolKind::Constant);
const_pol.load(const_pol_file).unwrap();
const_pol.load(const_pol_file)?;

let mut cm_pol = PolsArray::new(&pil, PolKind::Commit);
cm_pol.load(cm_pol_file).unwrap();
cm_pol.load(cm_pol_file)?;

let stark_struct = load_json::<StarkStruct>(stark_struct).unwrap();
let stark_struct = load_json::<StarkStruct>(stark_struct)?;
match stark_struct.verificationHashType.as_str() {
"BN128" => prove::<MerkleTreeBN128, TranscriptBN128>(
&mut pil,
Expand Down Expand Up @@ -89,7 +89,7 @@ fn prove<M: MerkleTree<MTNode = ElementDigest<4>>, T: Transcript>(
zkin: &str,
prover_addr: &str,
) -> Result<()> {
let mut setup = StarkSetup::<M>::new(const_pol, pil, stark_struct, None).unwrap();
let mut setup = StarkSetup::<M>::new(const_pol, pil, stark_struct, None)?;
let mut starkproof = StarkProof::<M>::stark_gen::<T>(
cm_pol,
const_pol,
Expand All @@ -99,8 +99,7 @@ fn prove<M: MerkleTree<MTNode = ElementDigest<4>>, T: Transcript>(
pil,
stark_struct,
prover_addr,
)
.unwrap();
)?;
log::debug!("generate the proof done");

let result = stark_verify::<M, T>(
Expand All @@ -109,8 +108,7 @@ fn prove<M: MerkleTree<MTNode = ElementDigest<4>>, T: Transcript>(
&setup.starkinfo,
stark_struct,
&mut setup.program,
)
.unwrap();
)?;

assert!(result);
log::debug!("verify the proof done");
Expand Down
14 changes: 3 additions & 11 deletions starky/src/zkin_join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,13 @@ pub fn join_zkin(
zkout: &String,
) -> Result<()> {
// 1. load files.
// porting from compressor12_exec.(input_file)
// let stark_struct = load_json::<StarkStruct>(&stark_setup_file).unwrap();
let inputs_str = std::fs::read_to_string(zkin1).unwrap();
let inputs_str = std::fs::read_to_string(zkin1)?;
let zkin1_map: BTreeMap<String, serde_json::Value> = serde_json::from_str(&inputs_str)?;

let inputs_str = std::fs::read_to_string(zkin2).unwrap();
let inputs_str = std::fs::read_to_string(zkin2)?;
let zkin2_map: BTreeMap<String, serde_json::Value> = serde_json::from_str(&inputs_str)?;

// 2. construct zkout
// node /Users/paul/blockchain/eigen-zkvm/test/../starkjs/src/recursive/main_joinzkin.js
// --starksetup ../starky/data/c12.starkStruct.json
// --zkin1 /tmp/aggregation_bn128_fibonacci/aggregation/0/fibonacci.recursive1/input.zkin.json
// --zkin2 /tmp/aggregation_bn128_fibonacci/aggregation/1/fibonacci.recursive1/input.zkin.json
// --zkinout /tmp/aggregation_bn128_fibonacci/aggregation/0/fibonacci.recursive1/r1_input.zkin.json
let mut zkout_map = BTreeMap::new();

for (k, v) in zkin1_map {
Expand All @@ -36,10 +29,9 @@ pub fn join_zkin(
}

// 3. save zkout to file
// dump zkin file porting from stark_prove
let input = serde_json::to_string(&zkout_map)?;
let mut file = File::create(zkout)?;
write!(file, "{}", input).unwrap();
write!(file, "{}", input)?;
log::trace!("zkout file Generated Correctly");
Ok(())
}
Loading