Skip to content

Commit 4b9d8d8

Browse files
authored
feat: opti return result type. (#158)
* opti: better result
1 parent 8263e14 commit 4b9d8d8

File tree

5 files changed

+41
-47
lines changed

5 files changed

+41
-47
lines changed

plonky/src/api.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ pub fn prove(
8484
pub fn calculate_witness(wasm_file: &str, input_json: &str, output: &str) -> Result<()> {
8585
let inputs = load_input_for_witness(input_json);
8686

87-
// let mut wtns = WitnessCalculator::new(wasm_file).unwrap();
8887
let mut wtns = WitnessCalculator::from_file(wasm_file)?;
8988
assert_eq!(
9089
wtns.memory.prime.to_str_radix(16),
@@ -122,16 +121,17 @@ pub fn verify(vk_file: &str, proof_bin: &str, transcript: &str) -> Result<()> {
122121
let vk = reader::load_verification_key::<Bn256>(vk_file);
123122
let proof = reader::load_proof::<Bn256>(proof_bin);
124123
let ok = plonk::verify(&vk, &proof, transcript)?;
125-
if !ok {
126-
return Err(EigenError::from("Proof is invalid".to_string()));
124+
if ok {
125+
Ok(())
126+
} else {
127+
Err(EigenError::from("Proof is invalid".to_string()))
127128
}
128-
Result::Ok(())
129129
}
130130

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

137137
#[cfg(not(feature = "wasm"))]
@@ -184,10 +184,11 @@ pub fn aggregation_verify(proof: &str, vk: &str) -> Result<()> {
184184
let vk = reader::load_aggregation_verification_key(vk);
185185
let proof = reader::load_aggregated_proof(proof);
186186
let correct = aggregation::verify(vk, proof)?;
187-
if !correct {
188-
return Err(EigenError::from("Proof is invalid".to_string()));
187+
if correct {
188+
Result::Ok(())
189+
} else {
190+
Err(EigenError::from("Proof is invalid".to_string()))
189191
}
190-
Result::Ok(())
191192
}
192193

193194
// check an aggregated proof is corresponding to the original proofs

starky/src/compressor12/compressor12_exec.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,22 @@ pub fn exec(
2525
commit_file: &str,
2626
) -> Result<()> {
2727
// 0. load exec_file,
28-
let (adds_len, s_map_column_len, adds, s_map) = read_exec_file(exec_file);
28+
let (adds_len, s_map_column_len, adds, s_map) = read_exec_file(exec_file)?;
2929

3030
// 1. Compiles a .pil file to its json form , and save it.
3131
// TODO: the pil_str has been compiled in plonk_setup#3
3232
let pil_json = compile_pil_from_path(pil_file);
33-
let mut file = File::create(Path::new(&format!("{pil_file}.json"))).unwrap();
34-
let input = serde_json::to_string(&pil_json).unwrap();
35-
write!(file, "{}", input).unwrap();
33+
let mut file = File::create(Path::new(&format!("{pil_file}.json")))?;
34+
let input = serde_json::to_string(&pil_json)?;
35+
write!(file, "{}", input)?;
3636

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

4040
// 3. calculate witness. wasm+input->witness
41-
let inputs = load_input_for_witness(input_file);
42-
// let mut wtns = WitnessCalculator::new(wasm_file).unwrap();
4341
let mut wtns = WitnessCalculator::from_file(wasm_file)?;
44-
let w = wtns.calculate_witness(inputs, false).unwrap();
42+
let inputs = load_input_for_witness(input_file);
43+
let w = wtns.calculate_witness(inputs, false)?;
4544
let mut w = w
4645
.iter()
4746
.map(|wi| {
@@ -55,8 +54,8 @@ pub fn exec(
5554
.collect::<Vec<_>>();
5655

5756
for i in 0..adds_len {
58-
let w2 = FGL::from_raw_repr(<FGL as PrimeField>::Repr::from(adds[i * 4 + 2])).unwrap();
59-
let w3 = FGL::from_raw_repr(<FGL as PrimeField>::Repr::from(adds[i * 4 + 3])).unwrap();
57+
let w2 = FGL::from_raw_repr(<FGL as PrimeField>::Repr::from(adds[i * 4 + 2]))?;
58+
let w3 = FGL::from_raw_repr(<FGL as PrimeField>::Repr::from(adds[i * 4 + 3]))?;
6059

6160
let f_w = (w[adds[i * 4] as usize] * w2) + (w[adds[i * 4 + 1] as usize] * w3);
6261
w.push(f_w);
@@ -100,8 +99,8 @@ pub fn exec(
10099
Result::Ok(())
101100
}
102101

103-
fn read_exec_file(exec_file: &str) -> (usize, usize, Vec<u64>, Vec<u64>) {
104-
let mut buff = read_vec_from_file(exec_file).unwrap();
102+
fn read_exec_file(exec_file: &str) -> Result<(usize, usize, Vec<u64>, Vec<u64>)> {
103+
let mut buff = read_vec_from_file(exec_file)?;
105104

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

116-
(adds_len, s_map_column_len, adds, s_map)
115+
Ok((adds_len, s_map_column_len, adds, s_map))
117116
}
118117

119118
#[cfg(test)]
@@ -144,9 +143,9 @@ mod test {
144143
vec![3, 4, 5],
145144
];
146145

147-
write_exec_file(&file_path, &target_adds, &target_s_map);
146+
write_exec_file(&file_path, &target_adds, &target_s_map).unwrap();
148147

149-
let (adds_len, _s_map_column_len, _adds, _s_map) = read_exec_file(&file_path);
148+
let (adds_len, _s_map_column_len, _adds, _s_map) = read_exec_file(&file_path).unwrap();
150149

151150
assert_eq!(adds_len, target_adds.len());
152151
}

starky/src/compressor12/compressor12_setup.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,24 @@ pub fn setup(
3434
let res = PlonkSetup::new(&r1cs, &opts);
3535

3636
// 2. And write it into pil_file.
37-
let mut file = File::create(pil_file).unwrap();
38-
write!(file, "{}", res.pil_str).unwrap();
37+
let mut file = File::create(pil_file)?;
38+
write!(file, "{}", res.pil_str)?;
3939

4040
// 3. write const pols file
4141
res.const_pols.save(const_file)?;
4242

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

4646
Ok(())
4747
}
4848

4949
// construct and save ExecFile: plonk additions + sMap -> BigUint64Array
50-
pub(super) fn write_exec_file(exec_file: &str, adds: &Vec<PlonkAdd>, s_map: &Vec<Vec<u64>>) {
50+
pub(super) fn write_exec_file(
51+
exec_file: &str,
52+
adds: &Vec<PlonkAdd>,
53+
s_map: &Vec<Vec<u64>>,
54+
) -> Result<()> {
5155
let adds_len = adds.len();
5256
let s_map_row_len = s_map.len();
5357
let s_map_column_len = s_map[0].len();
@@ -74,5 +78,5 @@ pub(super) fn write_exec_file(exec_file: &str, adds: &Vec<PlonkAdd>, s_map: &Vec
7478
}
7579
}
7680

77-
write_vec_to_file(exec_file, &buff).unwrap();
81+
Ok(write_vec_to_file(exec_file, &buff)?)
7882
}

starky/src/prove.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ pub fn stark_prove(
3030
zkin: &str,
3131
prover_addr: &str,
3232
) -> Result<()> {
33-
let mut pil = load_json::<PIL>(pil_file).unwrap();
33+
let mut pil = load_json::<PIL>(pil_file)?;
3434
let mut const_pol = PolsArray::new(&pil, PolKind::Constant);
35-
const_pol.load(const_pol_file).unwrap();
35+
const_pol.load(const_pol_file)?;
3636

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

40-
let stark_struct = load_json::<StarkStruct>(stark_struct).unwrap();
40+
let stark_struct = load_json::<StarkStruct>(stark_struct)?;
4141
match stark_struct.verificationHashType.as_str() {
4242
"BN128" => prove::<MerkleTreeBN128, TranscriptBN128>(
4343
&mut pil,
@@ -89,7 +89,7 @@ fn prove<M: MerkleTree<MTNode = ElementDigest<4>>, T: Transcript>(
8989
zkin: &str,
9090
prover_addr: &str,
9191
) -> Result<()> {
92-
let mut setup = StarkSetup::<M>::new(const_pol, pil, stark_struct, None).unwrap();
92+
let mut setup = StarkSetup::<M>::new(const_pol, pil, stark_struct, None)?;
9393
let mut starkproof = StarkProof::<M>::stark_gen::<T>(
9494
cm_pol,
9595
const_pol,
@@ -99,8 +99,7 @@ fn prove<M: MerkleTree<MTNode = ElementDigest<4>>, T: Transcript>(
9999
pil,
100100
stark_struct,
101101
prover_addr,
102-
)
103-
.unwrap();
102+
)?;
104103
log::debug!("generate the proof done");
105104

106105
let result = stark_verify::<M, T>(
@@ -109,8 +108,7 @@ fn prove<M: MerkleTree<MTNode = ElementDigest<4>>, T: Transcript>(
109108
&setup.starkinfo,
110109
stark_struct,
111110
&mut setup.program,
112-
)
113-
.unwrap();
111+
)?;
114112

115113
assert!(result);
116114
log::debug!("verify the proof done");

starky/src/zkin_join.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,13 @@ pub fn join_zkin(
1212
zkout: &String,
1313
) -> Result<()> {
1414
// 1. load files.
15-
// porting from compressor12_exec.(input_file)
16-
// let stark_struct = load_json::<StarkStruct>(&stark_setup_file).unwrap();
17-
let inputs_str = std::fs::read_to_string(zkin1).unwrap();
15+
let inputs_str = std::fs::read_to_string(zkin1)?;
1816
let zkin1_map: BTreeMap<String, serde_json::Value> = serde_json::from_str(&inputs_str)?;
1917

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

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

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

3831
// 3. save zkout to file
39-
// dump zkin file porting from stark_prove
4032
let input = serde_json::to_string(&zkout_map)?;
4133
let mut file = File::create(zkout)?;
42-
write!(file, "{}", input).unwrap();
34+
write!(file, "{}", input)?;
4335
log::trace!("zkout file Generated Correctly");
4436
Ok(())
4537
}

0 commit comments

Comments
 (0)