diff --git a/plonky/src/api.rs b/plonky/src/api.rs index 6aa706a1..3d46b09a 100644 --- a/plonky/src/api.rs +++ b/plonky/src/api.rs @@ -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), @@ -122,16 +121,17 @@ pub fn verify(vk_file: &str, proof_bin: &str, transcript: &str) -> Result<()> { let vk = reader::load_verification_key::(vk_file); let proof = reader::load_proof::(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::(vk_file); bellman_vk_codegen::render_verification_key_from_default_template(&vk, sol, true); - Result::Ok(()) + Ok(()) } #[cfg(not(feature = "wasm"))] @@ -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 diff --git a/starky/src/compressor12/compressor12_exec.rs b/starky/src/compressor12/compressor12_exec.rs index 076a8ed1..eaecbd00 100644 --- a/starky/src/compressor12/compressor12_exec.rs +++ b/starky/src/compressor12/compressor12_exec.rs @@ -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| { @@ -55,8 +54,8 @@ pub fn exec( .collect::>(); for i in 0..adds_len { - let w2 = FGL::from_raw_repr(::Repr::from(adds[i * 4 + 2])).unwrap(); - let w3 = FGL::from_raw_repr(::Repr::from(adds[i * 4 + 3])).unwrap(); + let w2 = FGL::from_raw_repr(::Repr::from(adds[i * 4 + 2]))?; + let w3 = FGL::from_raw_repr(::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); @@ -100,8 +99,8 @@ pub fn exec( Result::Ok(()) } -fn read_exec_file(exec_file: &str) -> (usize, usize, Vec, Vec) { - let mut buff = read_vec_from_file(exec_file).unwrap(); +fn read_exec_file(exec_file: &str) -> Result<(usize, usize, Vec, Vec)> { + let mut buff = read_vec_from_file(exec_file)?; let mut new_buff = buff.split_off(2); let adds_len = buff[0] as usize; @@ -113,7 +112,7 @@ fn read_exec_file(exec_file: &str) -> (usize, usize, Vec, Vec) { 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)] @@ -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()); } diff --git a/starky/src/compressor12/compressor12_setup.rs b/starky/src/compressor12/compressor12_setup.rs index 8912b9cc..66159b08 100644 --- a/starky/src/compressor12/compressor12_setup.rs +++ b/starky/src/compressor12/compressor12_setup.rs @@ -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, s_map: &Vec>) { +pub(super) fn write_exec_file( + exec_file: &str, + adds: &Vec, + s_map: &Vec>, +) -> Result<()> { let adds_len = adds.len(); let s_map_row_len = s_map.len(); let s_map_column_len = s_map[0].len(); @@ -74,5 +78,5 @@ pub(super) fn write_exec_file(exec_file: &str, adds: &Vec, s_map: &Vec } } - write_vec_to_file(exec_file, &buff).unwrap(); + Ok(write_vec_to_file(exec_file, &buff)?) } diff --git a/starky/src/prove.rs b/starky/src/prove.rs index 916ac9c4..cba2f65c 100644 --- a/starky/src/prove.rs +++ b/starky/src/prove.rs @@ -30,14 +30,14 @@ pub fn stark_prove( zkin: &str, prover_addr: &str, ) -> Result<()> { - let mut pil = load_json::(pil_file).unwrap(); + let mut pil = load_json::(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::(stark_struct).unwrap(); + let stark_struct = load_json::(stark_struct)?; match stark_struct.verificationHashType.as_str() { "BN128" => prove::( &mut pil, @@ -89,7 +89,7 @@ fn prove>, T: Transcript>( zkin: &str, prover_addr: &str, ) -> Result<()> { - let mut setup = StarkSetup::::new(const_pol, pil, stark_struct, None).unwrap(); + let mut setup = StarkSetup::::new(const_pol, pil, stark_struct, None)?; let mut starkproof = StarkProof::::stark_gen::( cm_pol, const_pol, @@ -99,8 +99,7 @@ fn prove>, T: Transcript>( pil, stark_struct, prover_addr, - ) - .unwrap(); + )?; log::debug!("generate the proof done"); let result = stark_verify::( @@ -109,8 +108,7 @@ fn prove>, T: Transcript>( &setup.starkinfo, stark_struct, &mut setup.program, - ) - .unwrap(); + )?; assert!(result); log::debug!("verify the proof done"); diff --git a/starky/src/zkin_join.rs b/starky/src/zkin_join.rs index 82f5e141..91e66196 100644 --- a/starky/src/zkin_join.rs +++ b/starky/src/zkin_join.rs @@ -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::(&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 = 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 = 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 { @@ -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(()) }