mirror of
https://github.com/vacp2p/zerokit.git
synced 2025-01-10 14:26:34 +00:00
chore(rln): clippy and fmt
This commit is contained in:
parent
1bac4453db
commit
6d3571034d
@ -1,21 +1,20 @@
|
||||
use ark_bn254::{Bn254, Fq, Fq2, Fr, G1Affine, G2Affine};
|
||||
use ark_bn254::{G1Projective, G2Projective};
|
||||
use ark_circom::{read_zkey, CircomBuilder, CircomConfig, WitnessCalculator};
|
||||
use ark_ff::BigInteger256;
|
||||
/// Adapted from semaphore-rs
|
||||
use ark_groth16::{ProvingKey, VerifyingKey};
|
||||
use ark_relations::r1cs::ConstraintMatrices;
|
||||
use core::include_bytes;
|
||||
use std::io::{Cursor, Write};
|
||||
use once_cell::sync::Lazy;
|
||||
use tempfile::NamedTempFile;
|
||||
use std::fs::File;
|
||||
use std::path::Path;
|
||||
use ark_ff::{BigInteger256};
|
||||
use ark_bn254::{Bn254, Fq, Fq2, Fr, G1Affine, G2Affine};
|
||||
use ark_bn254::{G1Projective, G2Projective};
|
||||
use num_bigint::BigUint;
|
||||
use once_cell::sync::Lazy;
|
||||
use serde_json::Value;
|
||||
use std::str::FromStr;
|
||||
use std::convert::TryFrom;
|
||||
use ark_circom::{read_zkey,WitnessCalculator, CircomBuilder, CircomConfig};
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::{Cursor, Write};
|
||||
use std::path::Path;
|
||||
use std::str::FromStr;
|
||||
use tempfile::NamedTempFile;
|
||||
|
||||
const ZKEY_PATH: &str = "./resources/rln_final.zkey";
|
||||
const VK_PATH: &str = "./resources/verifying_key.json";
|
||||
@ -33,24 +32,18 @@ pub fn VK() -> VerifyingKey<Bn254> {
|
||||
|
||||
if Path::new(VK_PATH).exists() {
|
||||
let verifyingKey = vk_from_json(VK_PATH);
|
||||
verifyingKey
|
||||
}
|
||||
else if Path::new(ZKEY_PATH).exists() {
|
||||
verifyingKey
|
||||
} else if Path::new(ZKEY_PATH).exists() {
|
||||
verifyingKey = ZKEY().vk;
|
||||
verifyingKey
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
panic!("No proving/verification key present!");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn CIRCOM() -> CircomBuilder<Bn254> {
|
||||
|
||||
// Load the WASM and R1CS for witness and proof generation
|
||||
let cfg = CircomConfig::<Bn254>::new(
|
||||
WASM_PATH,
|
||||
R1CS_PATH,
|
||||
).unwrap(); // should be )?; but need to address "the trait `From<ErrReport>` is not implemented for `protocol::ProofError`"
|
||||
let cfg = CircomConfig::<Bn254>::new(WASM_PATH, R1CS_PATH).unwrap(); // should be )?; but need to address "the trait `From<ErrReport>` is not implemented for `protocol::ProofError`"
|
||||
|
||||
// We build the circuit
|
||||
let mut builder = CircomBuilder::new(cfg);
|
||||
@ -60,10 +53,10 @@ pub fn CIRCOM() -> CircomBuilder<Bn254> {
|
||||
|
||||
// Utilities to convert a json verification key in a groth16::VerificationKey
|
||||
fn fq_from_str(s: &str) -> Fq {
|
||||
BigInteger256::try_from(BigUint::from_str(s).unwrap())
|
||||
.unwrap()
|
||||
.into()
|
||||
}
|
||||
BigInteger256::try_from(BigUint::from_str(s).unwrap())
|
||||
.unwrap()
|
||||
.into()
|
||||
}
|
||||
|
||||
fn json_to_g1(json: &Value, key: &str) -> G1Affine {
|
||||
let els: Vec<String> = json
|
||||
@ -131,16 +124,15 @@ fn json_to_g2(json: &Value, key: &str) -> G2Affine {
|
||||
}
|
||||
|
||||
fn vk_from_json(vk_path: &str) -> VerifyingKey<Bn254> {
|
||||
|
||||
let json = std::fs::read_to_string(vk_path).unwrap();
|
||||
let json: Value = serde_json::from_str(&json).unwrap();
|
||||
|
||||
let mut vk = VerifyingKey {
|
||||
alpha_g1 : json_to_g1(&json, "vk_alpha_1"),
|
||||
beta_g2 : json_to_g2(&json, "vk_beta_2"),
|
||||
gamma_g2 : json_to_g2(&json, "vk_gamma_2"),
|
||||
delta_g2 : json_to_g2(&json, "vk_delta_2"),
|
||||
gamma_abc_g1 : json_to_g1_vec(&json, "IC")
|
||||
alpha_g1: json_to_g1(&json, "vk_alpha_1"),
|
||||
beta_g2: json_to_g2(&json, "vk_beta_2"),
|
||||
gamma_g2: json_to_g2(&json, "vk_gamma_2"),
|
||||
delta_g2: json_to_g2(&json, "vk_delta_2"),
|
||||
gamma_abc_g1: json_to_g1_vec(&json, "IC"),
|
||||
};
|
||||
|
||||
return vk;
|
||||
@ -150,8 +142,6 @@ pub fn check_vk_from_zkey(verifyingKey: VerifyingKey<Bn254>) {
|
||||
assert_eq!(ZKEY().vk, verifyingKey);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Not sure this is still useful...
|
||||
const WASM: &[u8] = include_bytes!("../resources/rln.wasm");
|
||||
pub static WITNESS_CALCULATOR: Lazy<WitnessCalculator> = Lazy::new(|| {
|
||||
|
@ -7,7 +7,6 @@ pub mod public;
|
||||
use ark_bn254::{Fr, Parameters};
|
||||
use ark_ec::bn::Bn;
|
||||
|
||||
|
||||
pub mod circuit;
|
||||
pub mod protocol;
|
||||
|
||||
@ -15,8 +14,7 @@ pub type Field = Fr;
|
||||
pub type Groth16Proof = ark_groth16::Proof<Bn<Parameters>>;
|
||||
pub type EthereumGroth16Proof = ark_circom::ethereum::Proof;
|
||||
|
||||
use crate::circuit::{ZKEY,VK,CIRCOM};
|
||||
|
||||
use crate::circuit::{CIRCOM, VK, ZKEY};
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
@ -25,7 +23,8 @@ mod test {
|
||||
use hex_literal::hex;
|
||||
use num_bigint::BigInt;
|
||||
use semaphore::{
|
||||
hash::Hash, hash_to_field, identity::Identity, poseidon_tree::PoseidonTree, Field, poseidon_hash
|
||||
hash::Hash, hash_to_field, identity::Identity, poseidon_hash, poseidon_tree::PoseidonTree,
|
||||
Field,
|
||||
};
|
||||
|
||||
#[test]
|
||||
@ -89,7 +88,6 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn test_end_to_end() {
|
||||
|
||||
let TREE_HEIGHT = 16;
|
||||
let leafIndex = 3;
|
||||
|
||||
@ -111,17 +109,18 @@ mod test {
|
||||
|
||||
// We set the remaining values to random ones
|
||||
let epoch = hash_to_field(b"test-epoch");
|
||||
let rln_identifier =hash_to_field(b"test-rln-identifier");
|
||||
let rln_identifier = hash_to_field(b"test-rln-identifier");
|
||||
|
||||
let rlnWitness: RLNWitnessInput = initRLNWitnessFromValues(identity_secret, &merkle_proof, x, epoch, rln_identifier);
|
||||
let rlnWitness: RLNWitnessInput =
|
||||
initRLNWitnessFromValues(identity_secret, &merkle_proof, x, epoch, rln_identifier);
|
||||
|
||||
println!("rlnWitness: {:#?}", rlnWitness);
|
||||
|
||||
// We generate all relevant keys
|
||||
let provingKey = &ZKEY();
|
||||
let verificationKey = &VK();
|
||||
let verificationKey = &VK();
|
||||
let mut builder = CIRCOM();
|
||||
|
||||
|
||||
// Let's generate a zkSNARK proof
|
||||
let (proof, inputs) = generate_proof(builder, provingKey, rlnWitness).unwrap();
|
||||
|
||||
@ -129,25 +128,22 @@ mod test {
|
||||
let success = verify_proof(verificationKey, proof, inputs).unwrap();
|
||||
|
||||
assert!(success);
|
||||
|
||||
}
|
||||
|
||||
|
||||
//to_str_radix(10);
|
||||
|
||||
//
|
||||
//// change signal_hash and external_nullifier_hash here
|
||||
//let signal_hash = hash_to_field(b"xxx");
|
||||
//let external_nullifier_hash = hash_to_field(b"appId");
|
||||
//
|
||||
//let nullifier_hash = generate_nullifier_hash(&id, external_nullifier_hash);
|
||||
//
|
||||
//
|
||||
//// We generate all relevant keys
|
||||
//let provingKey = &ZKEY();
|
||||
//let verificationKey = &VK();
|
||||
//let mut builder = CIRCOM();
|
||||
|
||||
//println!("Proof: {:#?}", proof);
|
||||
}
|
||||
|
||||
//to_str_radix(10);
|
||||
|
||||
//
|
||||
//// change signal_hash and external_nullifier_hash here
|
||||
//let signal_hash = hash_to_field(b"xxx");
|
||||
//let external_nullifier_hash = hash_to_field(b"appId");
|
||||
//
|
||||
//let nullifier_hash = generate_nullifier_hash(&id, external_nullifier_hash);
|
||||
//
|
||||
//
|
||||
//// We generate all relevant keys
|
||||
//let provingKey = &ZKEY();
|
||||
//let verificationKey = &VK();
|
||||
//let mut builder = CIRCOM();
|
||||
|
||||
//println!("Proof: {:#?}", proof);
|
||||
}
|
||||
|
@ -15,9 +15,8 @@ use tracing_subscriber::layer::SubscriberExt;
|
||||
// JSON
|
||||
use serde::Deserialize;
|
||||
|
||||
use rln::protocol::{initRLNWitnessFromJSON, verify_proof, generate_proof};
|
||||
use rln::circuit::{VK,ZKEY,CIRCOM};
|
||||
|
||||
use rln::circuit::{CIRCOM, VK, ZKEY};
|
||||
use rln::protocol::{generate_proof, initRLNWitnessFromJSON, verify_proof};
|
||||
|
||||
// RLN
|
||||
fn groth16_proof_example() -> Result<()> {
|
||||
@ -77,12 +76,12 @@ fn groth16_proof_example() -> Result<()> {
|
||||
|
||||
// We generate all relevant keys
|
||||
let provingKey = &ZKEY();
|
||||
let verificationKey = &VK();
|
||||
let verificationKey = &VK();
|
||||
let mut builder = CIRCOM();
|
||||
|
||||
// We compute witness from the json input example
|
||||
let rlnWitness = initRLNWitnessFromJSON(input_json_str);
|
||||
|
||||
|
||||
// Let's generate a zkSNARK proof
|
||||
let (proof, inputs) = generate_proof(builder, provingKey, rlnWitness).unwrap();
|
||||
|
||||
|
@ -1,10 +1,12 @@
|
||||
/// Adapted from semaphore-rs
|
||||
use crate::circuit::{WITNESS_CALCULATOR, ZKEY, VK};
|
||||
use ark_bn254::{Bn254, Parameters, Fr};
|
||||
use crate::circuit::{VK, WITNESS_CALCULATOR, ZKEY};
|
||||
use ark_bn254::{Bn254, Fr, Parameters};
|
||||
use ark_ec::bn::Bn;
|
||||
use ark_ff::{Fp256, PrimeField};
|
||||
use ark_groth16::{
|
||||
create_proof_with_reduction_and_matrices, prepare_verifying_key, Proof as ArkProof, create_random_proof_with_reduction, ProvingKey, VerifyingKey, verify_proof as ark_verify_proof
|
||||
create_proof_with_reduction_and_matrices, create_random_proof_with_reduction,
|
||||
prepare_verifying_key, verify_proof as ark_verify_proof, Proof as ArkProof, ProvingKey,
|
||||
VerifyingKey,
|
||||
};
|
||||
use ark_relations::r1cs::SynthesisError;
|
||||
use ark_std::{rand::thread_rng, UniformRand};
|
||||
@ -23,8 +25,7 @@ use serde::{Deserialize, Serialize};
|
||||
use std::time::Instant;
|
||||
use thiserror::Error;
|
||||
|
||||
use ark_circom::{read_zkey, CircomReduction, CircomBuilder, CircomConfig};
|
||||
|
||||
use ark_circom::{read_zkey, CircomBuilder, CircomConfig, CircomReduction};
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct RLNWitnessInput {
|
||||
@ -37,24 +38,29 @@ pub struct RLNWitnessInput {
|
||||
}
|
||||
|
||||
pub fn initRLNWitnessFromJSON(input_json_str: &str) -> RLNWitnessInput {
|
||||
let rlnWitness: RLNWitnessInput = serde_json::from_str(&input_json_str).expect("JSON was not well-formatted");
|
||||
let rlnWitness: RLNWitnessInput =
|
||||
serde_json::from_str(&input_json_str).expect("JSON was not well-formatted");
|
||||
return rlnWitness;
|
||||
}
|
||||
|
||||
|
||||
pub fn initRLNWitnessFromValues(identity_secret: Field, merkle_proof: &merkle_tree::Proof<PoseidonHash>, x: Field, epoch: Field, rln_identifier: Field) -> RLNWitnessInput {
|
||||
|
||||
pub fn initRLNWitnessFromValues(
|
||||
identity_secret: Field,
|
||||
merkle_proof: &merkle_tree::Proof<PoseidonHash>,
|
||||
x: Field,
|
||||
epoch: Field,
|
||||
rln_identifier: Field,
|
||||
) -> RLNWitnessInput {
|
||||
//println!("Merkle proof: {:#?}", merkle_proof);
|
||||
let path_elements = getPathElements(merkle_proof);
|
||||
let identity_path_index = getIdentityPathIndex(merkle_proof);
|
||||
|
||||
let rlnWitness = RLNWitnessInput {
|
||||
identity_secret: BigInt::from(identity_secret).to_str_radix(10),
|
||||
path_elements: path_elements,
|
||||
identity_path_index: identity_path_index,
|
||||
x: BigInt::from(x).to_str_radix(10),
|
||||
epoch: format!("{:#066x}", BigInt::from(epoch)), //We format it as a padded 32 bytes hex with leading 0x for compatibility with zk-kit
|
||||
rln_identifier: BigInt::from(rln_identifier).to_str_radix(10),
|
||||
identity_secret: BigInt::from(identity_secret).to_str_radix(10),
|
||||
path_elements: path_elements,
|
||||
identity_path_index: identity_path_index,
|
||||
x: BigInt::from(x).to_str_radix(10),
|
||||
epoch: format!("{:#066x}", BigInt::from(epoch)), //We format it as a padded 32 bytes hex with leading 0x for compatibility with zk-kit
|
||||
rln_identifier: BigInt::from(rln_identifier).to_str_radix(10),
|
||||
};
|
||||
|
||||
return rlnWitness;
|
||||
@ -115,16 +121,16 @@ pub fn getPathElements(proof: &merkle_tree::Proof<PoseidonHash>) -> Vec<String>
|
||||
}
|
||||
|
||||
pub fn getIdentityPathIndex(proof: &merkle_tree::Proof<PoseidonHash>) -> Vec<u8> {
|
||||
proof.0
|
||||
.iter()
|
||||
.map(|branch| match branch {
|
||||
Branch::Left(_) => 0,
|
||||
Branch::Right(_) => 1,
|
||||
})
|
||||
.collect()
|
||||
proof
|
||||
.0
|
||||
.iter()
|
||||
.map(|branch| match branch {
|
||||
Branch::Left(_) => 0,
|
||||
Branch::Right(_) => 1,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
|
||||
/// Internal helper to hash the signal to make sure it's in the field
|
||||
fn hash_signal(signal: &[u8]) -> Field {
|
||||
let hash = keccak256(signal);
|
||||
@ -150,32 +156,32 @@ pub enum ProofError {
|
||||
SynthesisError(#[from] SynthesisError),
|
||||
}
|
||||
|
||||
|
||||
/// Generates a RLN proof
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns a [`ProofError`] if proving fails.
|
||||
pub fn generate_proof(mut builder: CircomBuilder<Bn254>, proving_key: &ProvingKey<Bn254>, rln_witness: RLNWitnessInput) -> Result<(Proof, Vec<Fr>), ProofError> {
|
||||
|
||||
pub fn generate_proof(
|
||||
mut builder: CircomBuilder<Bn254>,
|
||||
proving_key: &ProvingKey<Bn254>,
|
||||
rln_witness: RLNWitnessInput,
|
||||
) -> Result<(Proof, Vec<Fr>), ProofError> {
|
||||
let now = Instant::now();
|
||||
|
||||
builder.push_input(
|
||||
"identity_secret",
|
||||
BigInt::parse_bytes(rln_witness.identity_secret.as_bytes(), 10).unwrap(),
|
||||
);
|
||||
);
|
||||
|
||||
for v in rln_witness.path_elements.iter() {
|
||||
builder.push_input(
|
||||
"path_elements",
|
||||
"path_elements",
|
||||
BigInt::parse_bytes(v.as_bytes(), 10).unwrap(),
|
||||
);
|
||||
}
|
||||
|
||||
for v in rln_witness.identity_path_index.iter() {
|
||||
builder.push_input(
|
||||
"identity_path_index",
|
||||
BigInt::from(*v));
|
||||
builder.push_input("identity_path_index", BigInt::from(*v));
|
||||
}
|
||||
|
||||
builder.push_input(
|
||||
@ -185,7 +191,7 @@ pub fn generate_proof(mut builder: CircomBuilder<Bn254>, proving_key: &ProvingKe
|
||||
|
||||
builder.push_input(
|
||||
"epoch",
|
||||
BigInt::parse_bytes(rln_witness.epoch.strip_prefix("0x").unwrap().as_bytes(),16).unwrap(),
|
||||
BigInt::parse_bytes(rln_witness.epoch.strip_prefix("0x").unwrap().as_bytes(), 16).unwrap(),
|
||||
);
|
||||
|
||||
builder.push_input(
|
||||
@ -205,8 +211,13 @@ pub fn generate_proof(mut builder: CircomBuilder<Bn254>, proving_key: &ProvingKe
|
||||
// Generate a random proof
|
||||
let mut rng = thread_rng();
|
||||
|
||||
let ark_proof = create_random_proof_with_reduction::<_, _, _, CircomReduction>(circom, proving_key, &mut rng).unwrap();
|
||||
|
||||
let ark_proof = create_random_proof_with_reduction::<_, _, _, CircomReduction>(
|
||||
circom,
|
||||
proving_key,
|
||||
&mut rng,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let proof = ark_proof.into();
|
||||
|
||||
println!("proof generation took: {:.2?}", now.elapsed());
|
||||
@ -214,19 +225,21 @@ pub fn generate_proof(mut builder: CircomBuilder<Bn254>, proving_key: &ProvingKe
|
||||
Ok((proof, inputs))
|
||||
}
|
||||
|
||||
|
||||
/// Verifies a given RLN proof
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns a [`ProofError`] if verifying fails. Verification failure does not
|
||||
/// necessarily mean the proof is incorrect.
|
||||
pub fn verify_proof(verifyingKey: &VerifyingKey<Bn254>, proof: Proof, inputs: Vec<Fr>) -> Result<bool, ProofError> {
|
||||
|
||||
pub fn verify_proof(
|
||||
verifyingKey: &VerifyingKey<Bn254>,
|
||||
proof: Proof,
|
||||
inputs: Vec<Fr>,
|
||||
) -> Result<bool, ProofError> {
|
||||
// Check that the proof is valid
|
||||
let pvk = prepare_verifying_key(verifyingKey);
|
||||
let pr: ArkProof<Bn254> = proof.into();
|
||||
let verified = ark_verify_proof(&pvk, &pr, &inputs)?;
|
||||
|
||||
|
||||
Ok(verified)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user