Skip to content

Commit 4ed1da7

Browse files
SuccinctPauleigmax
authored andcommitted
feat: suppport serde groth16 proof into hex (#161)
* feat: suppport serde groth16 proof into hex * clippy * opti * sync to_hex to command
1 parent 0de3c0a commit 4ed1da7

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

groth16/src/api.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,19 @@ pub fn groth16_setup(
2525
circuit_file: &str,
2626
pk_file: &str,
2727
vk_file: &str,
28+
to_hex: bool,
2829
) -> Result<()> {
2930
let mut rng = rand::thread_rng();
3031
match curve_type {
3132
"BN128" => {
3233
let circuit = create_circuit_from_file::<Bn256>(circuit_file, None);
3334
let (pk, vk) = Groth16::circuit_specific_setup(circuit, &mut rng)?;
34-
write_pk_vk_to_files(curve_type, pk, vk, pk_file, vk_file)?
35+
write_pk_vk_to_files(curve_type, pk, vk, pk_file, vk_file, to_hex)?
3536
}
3637
"BLS12381" => {
3738
let circuit = create_circuit_from_file::<Bls12>(circuit_file, None);
3839
let (pk, vk) = Groth16::circuit_specific_setup(circuit, &mut rng)?;
39-
write_pk_vk_to_files(curve_type, pk, vk, pk_file, vk_file)?
40+
write_pk_vk_to_files(curve_type, pk, vk, pk_file, vk_file, to_hex)?
4041
}
4142
_ => {
4243
return Err(EigenError::Unknown(format!(
@@ -48,6 +49,7 @@ pub fn groth16_setup(
4849
Ok(())
4950
}
5051

52+
#[allow(clippy::too_many_arguments)]
5153
pub fn groth16_prove(
5254
curve_type: &str,
5355
circuit_file: &str,
@@ -56,6 +58,7 @@ pub fn groth16_prove(
5658
input_file: &str,
5759
public_input_file: &str,
5860
proof_file: &str,
61+
to_hex: bool,
5962
) -> Result<()> {
6063
let mut rng = rand::thread_rng();
6164

@@ -77,7 +80,7 @@ pub fn groth16_prove(
7780
.collect::<Vec<_>>();
7881
let circuit = create_circuit_from_file::<Bn256>(circuit_file, Some(w));
7982
let proof = Groth16::prove(&pk, circuit.clone(), &mut rng)?;
80-
let proof_json = serialize_proof(&proof, curve_type, false)?;
83+
let proof_json = serialize_proof(&proof, curve_type, to_hex)?;
8184
std::fs::write(proof_file, proof_json)?;
8285
let input_json = circuit.get_public_inputs_json();
8386
std::fs::write(public_input_file, input_json)?;
@@ -96,7 +99,7 @@ pub fn groth16_prove(
9699
.collect::<Vec<_>>();
97100
let circuit = create_circuit_from_file::<Bls12>(circuit_file, Some(w));
98101
let proof = Groth16::prove(&pk, circuit.clone(), &mut rng)?;
99-
let proof_json = serialize_proof(&proof, curve_type, false)?;
102+
let proof_json = serialize_proof(&proof, curve_type, to_hex)?;
100103
std::fs::write(proof_file, proof_json)?;
101104
let input_json = circuit.get_public_inputs_json();
102105
std::fs::write(public_input_file, input_json)?;
@@ -195,10 +198,11 @@ fn write_pk_vk_to_files<P: Parser>(
195198
vk: VerifyingKey<P>,
196199
pk_file: &str,
197200
vk_file: &str,
201+
to_hex: bool,
198202
) -> Result<()> {
199203
let writer = std::fs::File::create(pk_file)?;
200204
pk.write(writer)?;
201-
let vk_json = serialize_vk(&vk, curve_type, false)?;
205+
let vk_json = serialize_vk(&vk, curve_type, to_hex)?;
202206
std::fs::write(vk_file, vk_json)?;
203207
Ok(())
204208
}

zkit/src/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ pub struct Groth16SetupOpt {
287287
pk_file: String,
288288
#[arg(short, required = true, default_value = "verification_key.json")]
289289
vk_file: String,
290+
#[arg(short, action= clap::ArgAction::SetTrue)]
291+
to_hex: bool,
290292
}
291293

292294
/// Prove with groth16
@@ -310,6 +312,8 @@ pub struct Groth16ProveOpt {
310312
public_input_file: String,
311313
#[arg(long = "proof", required = true, default_value = "proof.json")]
312314
proof_file: String,
315+
#[arg(short, action= clap::ArgAction::SetTrue)]
316+
to_hex: bool,
313317
}
314318

315319
/// Verify with groth16
@@ -479,6 +483,7 @@ fn main() {
479483
&args.circuit_file,
480484
&args.pk_file,
481485
&args.vk_file,
486+
args.to_hex,
482487
)
483488
.map_err(|e| EigenError::from(format!("groth16 setup error {:?}", e))),
484489
Command::Groth16Prove(args) => groth16_prove(
@@ -489,6 +494,7 @@ fn main() {
489494
&args.input_file,
490495
&args.public_input_file,
491496
&args.proof_file,
497+
args.to_hex,
492498
)
493499
.map_err(|e| EigenError::from(format!("groth16 prove error {:?}", e))),
494500
Command::Groth16Verify(args) => groth16_verify(

0 commit comments

Comments
 (0)