chore(rln): clippy and fmt

This commit is contained in:
s1fr0 2022-06-01 16:57:56 +02:00
parent 1bac4453db
commit 6d3571034d
No known key found for this signature in database
GPG Key ID: 2C041D60117BFF46
4 changed files with 103 additions and 105 deletions

View File

@ -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";
@ -34,23 +33,17 @@ 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() {
} 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);
@ -131,7 +124,6 @@ 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();
@ -140,7 +132,7 @@ fn vk_from_json(vk_path: &str) -> VerifyingKey<Bn254> {
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")
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(|| {

View File

@ -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;
@ -113,7 +111,8 @@ mod test {
let epoch = hash_to_field(b"test-epoch");
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);
@ -129,10 +128,8 @@ mod test {
let success = verify_proof(verificationKey, proof, inputs).unwrap();
assert!(success);
}
//to_str_radix(10);
//
@ -150,4 +147,3 @@ mod test {
//println!("Proof: {:#?}", proof);
}

View File

@ -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<()> {

View File

@ -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,13 +38,18 @@ 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);
@ -115,7 +121,8 @@ pub fn getPathElements(proof: &merkle_tree::Proof<PoseidonHash>) -> Vec<String>
}
pub fn getIdentityPathIndex(proof: &merkle_tree::Proof<PoseidonHash>) -> Vec<u8> {
proof.0
proof
.0
.iter()
.map(|branch| match branch {
Branch::Left(_) => 0,
@ -124,7 +131,6 @@ pub fn getIdentityPathIndex(proof: &merkle_tree::Proof<PoseidonHash>) -> Vec<u8>
.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,14 +156,16 @@ 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(
@ -173,9 +181,7 @@ pub fn generate_proof(mut builder: CircomBuilder<Bn254>, proving_key: &ProvingKe
}
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(
@ -205,7 +211,12 @@ 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();
@ -214,15 +225,17 @@ 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();