Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): verify proof with arkzkey #33

Merged
merged 2 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions mopro-core/src/middleware/circom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use ark_circom::{
WitnessCalculator, //read_zkey,
};
use ark_crypto_primitives::snark::SNARK;
use ark_groth16::{Groth16, ProvingKey};
use ark_groth16::{prepare_verifying_key, Groth16, ProvingKey};
use ark_std::UniformRand;

use ark_relations::r1cs::ConstraintMatrices;
Expand Down Expand Up @@ -214,7 +214,22 @@ pub fn generate_proof2(
Ok((SerializableProof(proof), SerializableInputs(public_inputs)))
}

// TODO: Write pub fn verify_proof2 function
pub fn verify_proof2(
serialized_proof: SerializableProof,
serialized_inputs: SerializableInputs,
) -> Result<bool, MoproError> {
let start = Instant::now();
let zkey = arkzkey();
let pvk = prepare_verifying_key(&zkey.0.vk);

let proof_verified =
GrothBn::verify_with_processed_vk(&pvk, &serialized_inputs.0, &serialized_proof.0)
.map_err(|e| MoproError::CircomError(e.to_string()))?;

let verification_duration = start.elapsed();
println!("Verification time 2: {:?}", verification_duration);
Ok(proof_verified)
}

impl CircomState {
pub fn new() -> Self {
Expand Down Expand Up @@ -546,8 +561,13 @@ mod tests {
let serialized_outputs = bytes_to_circuit_outputs(&expected_output_vec);

let generate_proof_res = generate_proof2(inputs);
let (_, serialized_inputs) = generate_proof_res.unwrap();
let (serialized_proof, serialized_inputs) = generate_proof_res.unwrap();
assert_eq!(serialized_inputs, serialized_outputs);

// Proof verification
let verify_res = verify_proof2(serialized_proof, serialized_inputs);
assert!(verify_res.is_ok());
assert!(verify_res.unwrap()); // Verifying that the proof was indeed verified
}

#[test]
Expand Down
7 changes: 7 additions & 0 deletions mopro-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ pub fn generate_proof2(
})
}

pub fn verify_proof2(proof: Vec<u8>, public_input: Vec<u8>) -> Result<bool, MoproError> {
let deserialized_proof = circom::serialization::deserialize_proof(proof);
let deserialized_public_input = circom::serialization::deserialize_inputs(public_input);
let is_valid = circom::verify_proof2(deserialized_proof, deserialized_public_input)?;
Ok(is_valid)
}

// TODO: Use FFIError::SerializationError instead
impl MoproCircom {
pub fn new() -> Self {
Expand Down
3 changes: 3 additions & 0 deletions mopro-ffi/src/mopro.udl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ namespace mopro {

[Throws=MoproError]
GenerateProofResult generate_proof2(record<string, sequence<string>> circuit_inputs);

[Throws=MoproError]
boolean verify_proof2(bytes proof, bytes public_input);
};

dictionary SetupResult {
Expand Down
5 changes: 5 additions & 0 deletions mopro-ios/MoproKit/Include/moproFFI.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ void uniffi_mopro_fn_func_initialize_mopro(RustCallStatus *_Nonnull out_status
);
RustBuffer uniffi_mopro_fn_func_generate_proof2(RustBuffer circuit_inputs, RustCallStatus *_Nonnull out_status
);
int8_t uniffi_mopro_fn_func_verify_proof2(RustBuffer proof, RustBuffer public_input, RustCallStatus *_Nonnull out_status
);
RustBuffer ffi_mopro_rustbuffer_alloc(int32_t size, RustCallStatus *_Nonnull out_status
);
RustBuffer ffi_mopro_rustbuffer_from_bytes(ForeignBytes bytes, RustCallStatus *_Nonnull out_status
Expand All @@ -107,6 +109,9 @@ uint16_t uniffi_mopro_checksum_func_initialize_mopro(void
);
uint16_t uniffi_mopro_checksum_func_generate_proof2(void

);
uint16_t uniffi_mopro_checksum_func_verify_proof2(void

);
uint16_t uniffi_mopro_checksum_method_moprocircom_setup(void

Expand Down
Loading