Skip to content

Commit

Permalink
feat: suppport serde groth16 proof into hex (#161)
Browse files Browse the repository at this point in the history
* feat: suppport serde groth16 proof into hex

* clippy

* opti

* sync to_hex to command
  • Loading branch information
SuccinctPaul authored Nov 22, 2023
1 parent 0de3c0a commit 63f1790
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
14 changes: 9 additions & 5 deletions groth16/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@ pub fn groth16_setup(
circuit_file: &str,
pk_file: &str,
vk_file: &str,
to_hex: bool,
) -> Result<()> {
let mut rng = rand::thread_rng();
match curve_type {
"BN128" => {
let circuit = create_circuit_from_file::<Bn256>(circuit_file, None);
let (pk, vk) = Groth16::circuit_specific_setup(circuit, &mut rng)?;
write_pk_vk_to_files(curve_type, pk, vk, pk_file, vk_file)?
write_pk_vk_to_files(curve_type, pk, vk, pk_file, vk_file, to_hex)?
}
"BLS12381" => {
let circuit = create_circuit_from_file::<Bls12>(circuit_file, None);
let (pk, vk) = Groth16::circuit_specific_setup(circuit, &mut rng)?;
write_pk_vk_to_files(curve_type, pk, vk, pk_file, vk_file)?
write_pk_vk_to_files(curve_type, pk, vk, pk_file, vk_file, to_hex)?
}
_ => {
return Err(EigenError::Unknown(format!(
Expand All @@ -48,6 +49,7 @@ pub fn groth16_setup(
Ok(())
}

#[allow(clippy::too_many_arguments)]
pub fn groth16_prove(
curve_type: &str,
circuit_file: &str,
Expand All @@ -56,6 +58,7 @@ pub fn groth16_prove(
input_file: &str,
public_input_file: &str,
proof_file: &str,
to_hex: bool,
) -> Result<()> {
let mut rng = rand::thread_rng();

Expand All @@ -77,7 +80,7 @@ pub fn groth16_prove(
.collect::<Vec<_>>();
let circuit = create_circuit_from_file::<Bn256>(circuit_file, Some(w));
let proof = Groth16::prove(&pk, circuit.clone(), &mut rng)?;
let proof_json = serialize_proof(&proof, curve_type, false)?;
let proof_json = serialize_proof(&proof, curve_type, to_hex)?;
std::fs::write(proof_file, proof_json)?;
let input_json = circuit.get_public_inputs_json();
std::fs::write(public_input_file, input_json)?;
Expand All @@ -96,7 +99,7 @@ pub fn groth16_prove(
.collect::<Vec<_>>();
let circuit = create_circuit_from_file::<Bls12>(circuit_file, Some(w));
let proof = Groth16::prove(&pk, circuit.clone(), &mut rng)?;
let proof_json = serialize_proof(&proof, curve_type, false)?;
let proof_json = serialize_proof(&proof, curve_type, to_hex)?;
std::fs::write(proof_file, proof_json)?;
let input_json = circuit.get_public_inputs_json();
std::fs::write(public_input_file, input_json)?;
Expand Down Expand Up @@ -195,10 +198,11 @@ fn write_pk_vk_to_files<P: Parser>(
vk: VerifyingKey<P>,
pk_file: &str,
vk_file: &str,
to_hex: bool,
) -> Result<()> {
let writer = std::fs::File::create(pk_file)?;
pk.write(writer)?;
let vk_json = serialize_vk(&vk, curve_type, false)?;
let vk_json = serialize_vk(&vk, curve_type, to_hex)?;
std::fs::write(vk_file, vk_json)?;
Ok(())
}
6 changes: 6 additions & 0 deletions zkit/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ pub struct Groth16SetupOpt {
pk_file: String,
#[arg(short, required = true, default_value = "verification_key.json")]
vk_file: String,
#[arg(short, default_value = "0")]
to_hex: bool,
}

/// Prove with groth16
Expand All @@ -310,6 +312,8 @@ pub struct Groth16ProveOpt {
public_input_file: String,
#[arg(long = "proof", required = true, default_value = "proof.json")]
proof_file: String,
#[arg(short, default_value = "0")]
to_hex: bool,
}

/// Verify with groth16
Expand Down Expand Up @@ -479,6 +483,7 @@ fn main() {
&args.circuit_file,
&args.pk_file,
&args.vk_file,
args.to_hex,
)
.map_err(|e| EigenError::from(format!("groth16 setup error {:?}", e))),
Command::Groth16Prove(args) => groth16_prove(
Expand All @@ -489,6 +494,7 @@ fn main() {
&args.input_file,
&args.public_input_file,
&args.proof_file,
args.to_hex,
)
.map_err(|e| EigenError::from(format!("groth16 prove error {:?}", e))),
Command::Groth16Verify(args) => groth16_verify(
Expand Down

0 comments on commit 63f1790

Please sign in to comment.