Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
223880 committed Sep 28, 2024
1 parent 2233edb commit 403b1ab
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
23 changes: 12 additions & 11 deletions zk_coinjoin_lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// src/lib.rs

pub mod proof; // Module for zero-knowledge proofspub mod transaction;
pub mod utils; // Module for Coinjoin transaction logic
pub mod utils;
pub mod transaction; // Module for Coinjoin transaction logic


use utils::generate_nonce;

use crate::proof::{generate_proof, verify_proof, ZKProof};

// Public function to create a Coinjoin transaction with ZK proofs
Expand All @@ -12,21 +15,19 @@ pub fn create_coinjoin_transaction(/* parameters */) -> Result<(transaction::Coi
let transaction = transaction::CoinjoinTransaction::new(/* parameters */);

// Generate the ZK proof
let proof = generate_proof(/* parameters */)
.map_err(|e| format!("Error generating proof: {}", e))?;

let proof = generate_nonce();
let proof = generate_proof();
Ok((transaction, proof))
}

// Public function to verify a Coinjoin transaction's proof
use bellman::groth16::prepare_verifying_key;
pub fn verify_coinjoin_transaction(proof: &ZKProof) -> Result<bool, String> {
let vk = prepare_verifying_key(); // You should implement this function to get the verifying key

pub fn verify_coinjoin_transaction(transaction: &transaction::CoinjoinTransaction, proof: &ZKProof) -> bool {
// Prepare the verifying key
let vk = prepare_verifying_key(&proof.vk);
// Verify the proof
let is_valid = verify_proof(proof, &vk)
.map_err(|e| format!("Error verifying proof: {}", e))?;

Ok(is_valid)
let result = vk.verify(&proof.inputs, &proof.outputs);
result
}

13 changes: 6 additions & 7 deletions zk_coinjoin_lib/src/proof.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use bellman::{groth16::{create_random_proof, prepare_verifying_key, verify_proof, Proof, PreparedVerifyingKey}, Engine};
use bellman::groth16::create_random_proof;
use bellman::Engine;
use rand::thread_rng;
use std::error::Error;
use serde::Deserialize;
use serde::{Serialize, Deserialize};

#[derive(Deserialize)]
pub struct ZKProof<E: bellman::Engine> {
Expand All @@ -11,14 +11,14 @@ pub struct ZKProof<E: bellman::Engine> {
}

pub fn generate_proof<E: Engine>(
circuit: impl bellman::Circuit<E>,
pk: &bellman::groth16::ProvingKey<E>
circuit: impl bellman::Circuit<E>,
pk: &bellman::groth16::Parameters<E>,
) -> Result<ZKProof<E>, Box<dyn Error>> {
// Random number generator for proof generation
let rng = &mut thread_rng();

// Generate the proof using the bellman `create_random_proof`
let proof = create_random_proof(circuit, pk, rng)?;
let proof = create_random_proof(circuit, &pk, rng)?;

// Populate public_inputs based on the circuit's public inputs
// For simplicity, assuming the circuit's public inputs are accessible
Expand All @@ -28,8 +28,7 @@ pub fn generate_proof<E: Engine>(
proof,
public_inputs,
})
}
pub fn verify_zk_proof<E: Engine>(
}pub fn verify_zk_proof<E: Engine>(
zk_proof: &ZKProof<E>,
vk: &PreparedVerifyingKey<E>
) -> Result<bool, Box<dyn Error>> {
Expand Down
2 changes: 1 addition & 1 deletion zk_coinjoin_lib/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use rand::Rng;
use serde::{Serialize, Deserialize};
use serde_json::{Deserialize, Serialize};
use serde_json::data::Value;

// Function to generate a random nonce
pub fn generate_nonce() -> Vec<u8> {
Expand Down

0 comments on commit 403b1ab

Please sign in to comment.