A Rust implementation of Zero Knowledge Proof using the Chaum-Pedersen protocol. This library demonstrates how a prover can convince a verifier that they know a secret value without revealing the secret itself.
Zero Knowledge Proof is a cryptographic method where one party (the prover) can prove to another party (the verifier) that they know a value x, without conveying any information apart from the fact that they know the value x.
This implementation uses the Schnorr identification protocol, which works as follows:
- Setup: Public parameters include generators
g,h, modulusp, and subgroupq - Commitment: Prover generates random
rand computes commitmentst1 = g^r mod pandt2 = h^r mod p - Challenge: Verifier sends a random challenge
c - Response: Prover computes response
s = r - cx mod qwherexis the secret - Verification: Verifier checks if
t1 = g^s * y1^c mod pandt2 = h^s * y2^c mod p
- Secure Implementation: Uses cryptographically secure random number generation
- Big Integer Support: Handles large numbers using
num_bigintcrate - Comprehensive Testing: Includes fixed value tests, dynamic tests, and randomized testing
- Clean API: Simple and intuitive interface for ZKP operations
Add this to your Cargo.toml:
[dependencies]
num-bigint = "0.4"
rand = "0.8"- Clone the repository:
git clone https://github.com/Vipul-sharma119/Zero-knowledge-Proof.git
cd Zero-knowledge-Proof- Build the project:
cargo build- Run tests:
cargo testuse num_bigint::BigUint;
use your_crate_name::ZKP;
fn main() {
// Setup public parameters
let modulus_p = BigUint::from(23u32);
let subgrp_q = BigUint::from(11u32);
let gen_g = BigUint::from(2u32);
let gen_h = BigUint::from(3u32);
// Create ZKP instance
let zkp = ZKP {
gen_g: gen_g.clone(),
gen_h: gen_h.clone(),
modulus_p: modulus_p.clone(),
subgrp_q: subgrp_q.clone(),
};
// Secret value (known only to prover)
let secret_x = BigUint::from(4u32);
// Generate public keys
let y1 = ZKP::mod_exp(&gen_g, &secret_x, &modulus_p);
let y2 = ZKP::mod_exp(&gen_h, &secret_x, &modulus_p);
// Prover generates random commitment
let r = ZKP::random(&subgrp_q);
let t1 = ZKP::mod_exp(&gen_g, &r, &modulus_p);
let t2 = ZKP::mod_exp(&gen_h, &r, &modulus_p);
// Verifier sends challenge
let challenge_c = BigUint::from(5u32);
// Prover computes response
let response_s = zkp.response(&r, &challenge_c, &secret_x);
// Verification
let is_valid = zkp.verify(&t1, &t2, &challenge_c, &response_s, &y1, &y2);
println!("Proof is valid: {}", is_valid);
}pub struct ZKP {
pub gen_g: BigUint, // Generator g
pub gen_h: BigUint, // Generator h
pub modulus_p: BigUint, // Prime modulus
pub subgrp_q: BigUint, // Subgroup order
}Performs modular exponentiation: base^exp mod modulus
Generates a cryptographically secure random number below the given bound
Computes the ZKP response: s = r - cx mod q
verify(&self, t1: &BigUint, t2: &BigUint, c: &BigUint, s: &BigUint, y1: &BigUint, y2: &BigUint) -> bool
Verifies the zero knowledge proof
The project includes comprehensive tests:
- Fixed Values Test: Tests with predetermined values for debugging
- Dynamic Values Test: Tests with known random values
- Random Values Test: Runs 10 iterations with completely random parameters
Run tests with:
cargo testWILL ADD CLIENT SIDE AND GRPC IN NEAR FUTURE