feat: read number of public inputs from vkey file

This commit is contained in:
zkFriendly
2024-07-12 17:20:52 +02:00
parent f40cca90b0
commit 1c06cf0c66
3 changed files with 31 additions and 8 deletions

View File

@@ -6,7 +6,10 @@ use std::{
use ark_serialize::{CanonicalSerialize, Write};
use clap::{Parser, Subcommand};
use utils::verifier_utils::{GrothBnProof, GrothBnVkey, JsonDecoder, PublicInputs};
use serde::Deserialize;
use utils::verifier_utils::{
GrothBnProof, GrothBnVkey, JsonDecoder, PublicInputs, PublicInputsCount,
};
#[derive(Parser)]
#[command(name = "rust verifier")]
@@ -48,14 +51,21 @@ fn main() {
} => {
println!("Generating verifier with verifying key: {}", verifying_key);
let vkey = GrothBnVkey::from_json_file(verifying_key);
let public_inputs_count = PublicInputsCount::from_json_file(verifying_key);
let mut serialized_vkey = Vec::new();
let writer = BufWriter::new(&mut serialized_vkey);
vkey.serialize_compressed(writer).unwrap();
let template = include_str!("verifier_template.rs");
let verifier_content =
template.replace("[COMPRESSED_VKEY]", &format!("{:?}", &serialized_vkey));
let verifier_content = template
.replace("[COMPRESSED_VKEY]", &format!("{:?}", &serialized_vkey))
.replace(
"PUBLIC_INPUTS_COUNT",
public_inputs_count.nPublic.to_string().as_str(),
);
let formatted_content =
format_rust_code(&verifier_content).expect("Failed to format the code");

View File

@@ -18,11 +18,12 @@ pub fn verify(proof: &[u8], public_inputs: &[u8]) -> bool {
let vk = VerifyingKey::<Bn254>::deserialize_compressed_unchecked([COMPRESSED_VKEY].as_slice())
.unwrap();
let public_inputs = <[Fp<MontBackend<FrConfig, 4>, 4>; 3]>::deserialize_with_mode(
&public_inputs[..],
Compress::Yes,
Validate::Yes,
);
let public_inputs =
<[Fp<MontBackend<FrConfig, 4>, 4>; PUBLIC_INPUTS_COUNT]>::deserialize_with_mode(
&public_inputs[..],
Compress::Yes,
Validate::Yes,
);
let public_inputs = match public_inputs {
Ok(inputs) => inputs,

View File

@@ -43,6 +43,12 @@ pub struct PublicInputs<const N: usize> {
pub inputs: [GrothFp; N],
}
// helper struct for deserializing public inputs count
#[derive(Deserialize)]
pub struct PublicInputsCount {
pub nPublic: usize,
}
pub trait JsonDecoder {
fn from_json(json: &str) -> Self;
fn from_json_file(file_path: &str) -> Self
@@ -144,6 +150,12 @@ impl JsonDecoder for GrothBnVkey {
}
}
impl JsonDecoder for PublicInputsCount {
fn from_json(json: &str) -> Self {
serde_json::from_str(json).unwrap()
}
}
impl<const N: usize> JsonDecoder for PublicInputs<N> {
fn from_json(json: &str) -> Self {
let inputs: Vec<String> = serde_json::from_str(json).unwrap();