Fix clippies
This commit is contained in:
parent
53ef9937cb
commit
2f8acc3c15
|
@ -34,7 +34,6 @@ ark-relations = { version = "0.3.0", default-features = false }
|
|||
ark-std = { version = "0.3.0", default-features = false, features = ["parallel"] }
|
||||
color-eyre = "0.5"
|
||||
criterion = { version = "0.3", optional = true, features = [ "async_tokio" ] }
|
||||
ethers-core = "0.6.3"
|
||||
ff = { package="ff_ce", version="0.11"}
|
||||
hex = "0.4.0"
|
||||
hex-literal = "0.3"
|
||||
|
@ -48,6 +47,9 @@ sha2 = "0.10.1"
|
|||
tiny-keccak = { version = "2.0.2", optional = true }
|
||||
zkp-u256 = { version = "0.2", optional = true }
|
||||
|
||||
# Use the same `ethers-core` version as ark-circom
|
||||
ethers-core = { git = "https://github.com/gakonst/ethers-rs", default-features = false }
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions = "1.0"
|
||||
serde_json = "1.0.79"
|
||||
|
|
|
@ -27,8 +27,8 @@ let merkle_proof = tree.proof(0).expect("proof should exist");
|
|||
let root = tree.root().into();
|
||||
|
||||
// change signal and external_nullifier here
|
||||
let signal = "hello".as_bytes();
|
||||
let external_nullifier = "123".as_bytes();
|
||||
let signal = b"hello";
|
||||
let external_nullifier = b"123";
|
||||
|
||||
let nullifier_hash = generate_nullifier_hash(&id, external_nullifier);
|
||||
|
||||
|
@ -39,4 +39,4 @@ let config = SnarkFileConfig {
|
|||
|
||||
let proof = generate_proof(&config, &id, &merkle_proof, external_nullifier, signal).unwrap();
|
||||
let success = verify_proof(&config, &root, &nullifier_hash, signal, external_nullifier, &proof).unwrap();
|
||||
```
|
||||
```
|
||||
|
|
|
@ -11,6 +11,9 @@
|
|||
"keccak",
|
||||
"merkle",
|
||||
"mimc",
|
||||
"mmaped",
|
||||
"modpow",
|
||||
"Repr",
|
||||
"snarkfiles",
|
||||
"zkey"
|
||||
],
|
||||
|
|
|
@ -65,7 +65,7 @@ impl From<Vec<u8>> for Hash {
|
|||
}
|
||||
}
|
||||
|
||||
/// Conversion to BigInt
|
||||
/// Conversion to `BigInt`
|
||||
impl From<Hash> for BigInt {
|
||||
fn from(hash: Hash) -> Self {
|
||||
Self::from_bytes_be(Sign::Plus, hash.as_bytes_be())
|
||||
|
|
|
@ -23,6 +23,7 @@ fn sha(msg: &[u8]) -> [u8; 32] {
|
|||
}
|
||||
|
||||
impl Identity {
|
||||
#[must_use]
|
||||
pub fn new(seed: &[u8]) -> Self {
|
||||
let seed_hash = &sha(seed);
|
||||
|
||||
|
@ -42,20 +43,22 @@ impl Identity {
|
|||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn secret_hash(&self) -> BigInt {
|
||||
let res = POSEIDON
|
||||
.hash(vec![
|
||||
bigint_to_fr(&self.nullifier),
|
||||
bigint_to_fr(&self.trapdoor),
|
||||
])
|
||||
.unwrap();
|
||||
.expect("input length is constant and valid for the hash");
|
||||
fr_to_bigint(res)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn commitment(&self) -> BigInt {
|
||||
let res = POSEIDON
|
||||
.hash(vec![bigint_to_fr(&self.secret_hash())])
|
||||
.unwrap();
|
||||
.expect("input length is constant and valid for the hash");
|
||||
fr_to_bigint(res)
|
||||
}
|
||||
}
|
||||
|
|
34
src/lib.rs
34
src/lib.rs
|
@ -21,23 +21,24 @@ pub type EthereumGroth16Proof = ark_circom::ethereum::Proof;
|
|||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use hash::*;
|
||||
use crate::{
|
||||
hash::Hash,
|
||||
identity::Identity,
|
||||
poseidon_tree::PoseidonTree,
|
||||
protocol::{generate_nullifier_hash, generate_proof, verify_proof, SnarkFileConfig},
|
||||
};
|
||||
use hex_literal::hex;
|
||||
use identity::*;
|
||||
use poseidon_tree::*;
|
||||
use protocol::*;
|
||||
|
||||
#[test]
|
||||
fn test_end_to_end() {
|
||||
// generate identity
|
||||
let id = Identity::new(b"hello");
|
||||
|
||||
// generate merkle tree
|
||||
const LEAF: Hash = Hash::from_bytes_be(hex!(
|
||||
"0000000000000000000000000000000000000000000000000000000000000000"
|
||||
));
|
||||
|
||||
// generate identity
|
||||
let id = Identity::new(b"hello");
|
||||
|
||||
// generate merkle tree
|
||||
let mut tree = PoseidonTree::new(21, LEAF);
|
||||
let (_, leaf) = id.commitment().to_bytes_be();
|
||||
tree.set(0, leaf.into());
|
||||
|
@ -46,8 +47,8 @@ mod test {
|
|||
let root = tree.root();
|
||||
|
||||
// change signal and external_nullifier here
|
||||
let signal = "xxx".as_bytes();
|
||||
let external_nullifier = "appId".as_bytes();
|
||||
let signal = b"xxx";
|
||||
let external_nullifier = b"appId";
|
||||
|
||||
let nullifier_hash = generate_nullifier_hash(&id, external_nullifier);
|
||||
|
||||
|
@ -92,19 +93,20 @@ pub mod bench {
|
|||
}
|
||||
|
||||
fn bench_proof(criterion: &mut Criterion) {
|
||||
// Create tree
|
||||
let id = Identity::new(b"hello");
|
||||
const LEAF: Hash = Hash::from_bytes_be(hex!(
|
||||
"0000000000000000000000000000000000000000000000000000000000000000"
|
||||
));
|
||||
|
||||
// Create tree
|
||||
let id = Identity::new(b"hello");
|
||||
let mut tree = PoseidonTree::new(21, LEAF);
|
||||
let (_, leaf) = id.commitment().to_bytes_be();
|
||||
tree.set(0, leaf.into());
|
||||
let merkle_proof = tree.proof(0).expect("proof should exist");
|
||||
|
||||
// change signal and external_nullifier here
|
||||
let signal = "xxx".as_bytes();
|
||||
let external_nullifier = "appId".as_bytes();
|
||||
let signal = b"xxx";
|
||||
let external_nullifier = b"appId";
|
||||
|
||||
let config = SnarkFileConfig {
|
||||
zkey: "./snarkfiles/semaphore.zkey".to_string(),
|
||||
|
@ -114,7 +116,7 @@ pub mod bench {
|
|||
criterion.bench_function("proof", move |b| {
|
||||
b.iter(|| {
|
||||
generate_proof(&config, &id, &merkle_proof, external_nullifier, signal).unwrap();
|
||||
})
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,12 +4,12 @@
|
|||
//!
|
||||
//! * Disk based storage backend (using mmaped files should be easy)
|
||||
|
||||
use num_bigint::BigInt;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
fmt::Debug,
|
||||
iter::{once, repeat, successors},
|
||||
};
|
||||
use num_bigint::BigInt;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Hash types, values and algorithms for a Merkle tree
|
||||
pub trait Hasher {
|
||||
|
@ -95,6 +95,7 @@ impl<H: Hasher> MerkleTree<H> {
|
|||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn num_leaves(&self) -> usize {
|
||||
self.depth
|
||||
.checked_sub(1)
|
||||
|
@ -102,6 +103,7 @@ impl<H: Hasher> MerkleTree<H> {
|
|||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn root(&self) -> H::Hash {
|
||||
self.nodes[0].clone()
|
||||
}
|
||||
|
@ -134,6 +136,7 @@ impl<H: Hasher> MerkleTree<H> {
|
|||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn proof(&self, leaf: usize) -> Option<Proof<H>> {
|
||||
if leaf >= self.num_leaves() {
|
||||
return None;
|
||||
|
@ -152,12 +155,12 @@ impl<H: Hasher> MerkleTree<H> {
|
|||
Some(Proof(path))
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[must_use]
|
||||
pub fn verify(&self, hash: H::Hash, proof: &Proof<H>) -> bool {
|
||||
proof.root(hash) == self.root()
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[must_use]
|
||||
pub fn leaves(&self) -> &[H::Hash] {
|
||||
&self.nodes[(self.num_leaves() - 1)..]
|
||||
}
|
||||
|
@ -165,7 +168,7 @@ impl<H: Hasher> MerkleTree<H> {
|
|||
|
||||
impl<H: Hasher> Proof<H> {
|
||||
/// Compute the leaf index for this proof
|
||||
#[allow(dead_code)]
|
||||
#[must_use]
|
||||
pub fn leaf_index(&self) -> usize {
|
||||
self.0.iter().rev().fold(0, |index, branch| match branch {
|
||||
Branch::Left(_) => index << 1,
|
||||
|
@ -174,7 +177,7 @@ impl<H: Hasher> Proof<H> {
|
|||
}
|
||||
|
||||
/// Compute path index (TODO: do we want to keep this here?)
|
||||
#[allow(dead_code)]
|
||||
#[must_use]
|
||||
pub fn path_index(&self) -> Vec<BigInt> {
|
||||
self.0
|
||||
.iter()
|
||||
|
@ -186,7 +189,7 @@ impl<H: Hasher> Proof<H> {
|
|||
}
|
||||
|
||||
/// Compute the Merkle root given a leaf hash
|
||||
#[allow(dead_code)]
|
||||
#[must_use]
|
||||
pub fn root(&self, hash: H::Hash) -> H::Hash {
|
||||
self.0.iter().fold(hash, |hash, branch| match branch {
|
||||
Branch::Left(sibling) => H::hash_node(&hash, sibling),
|
||||
|
|
|
@ -9,8 +9,8 @@
|
|||
//! * Instantiate a `PrimeField` to use Montgomery form.
|
||||
|
||||
use once_cell::sync::Lazy;
|
||||
use zkp_u256::U256;
|
||||
use tiny_keccak::{Hasher as _, Keccak};
|
||||
use zkp_u256::U256;
|
||||
|
||||
const NUM_ROUNDS: usize = 220;
|
||||
|
||||
|
@ -21,7 +21,7 @@ static MODULUS: Lazy<U256> = Lazy::new(|| {
|
|||
.unwrap()
|
||||
});
|
||||
|
||||
fn keccak256(bytes: &[u8]) -> [u8;32] {
|
||||
fn keccak256(bytes: &[u8]) -> [u8; 32] {
|
||||
let mut output = [0; 32];
|
||||
let mut hasher = Keccak::v256();
|
||||
hasher.update(bytes);
|
||||
|
@ -58,6 +58,7 @@ fn mix(left: &mut U256, right: &mut U256) {
|
|||
std::mem::swap(left, right);
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn hash(values: &[U256]) -> U256 {
|
||||
let mut left = U256::ZERO;
|
||||
let mut right = U256::ZERO;
|
||||
|
|
|
@ -4,16 +4,16 @@ use ark_ec::bn::Bn;
|
|||
use ark_ff::Fp256;
|
||||
use ark_groth16::{create_proof_with_reduction_and_matrices, prepare_verifying_key, Proof};
|
||||
use ark_relations::r1cs::SynthesisError;
|
||||
use ark_std::rand::thread_rng;
|
||||
use ark_std::{rand::thread_rng, UniformRand};
|
||||
use color_eyre::Result;
|
||||
use ethers_core::utils::keccak256;
|
||||
use num_bigint::{BigInt, Sign};
|
||||
use once_cell::sync::Lazy;
|
||||
use poseidon_rs::Poseidon;
|
||||
use std::{collections::HashMap, fs::File, ops::Shr};
|
||||
use std::{collections::HashMap, fs::File, ops::Shr, time::Instant};
|
||||
|
||||
use crate::{
|
||||
identity::*,
|
||||
identity::Identity,
|
||||
merkle_tree::{self, Branch},
|
||||
poseidon_tree::PoseidonHash,
|
||||
util::{bigint_to_fr, fr_to_bigint},
|
||||
|
@ -33,8 +33,7 @@ fn merkle_proof_to_vec(proof: &merkle_tree::Proof<PoseidonHash>) -> Vec<BigInt>
|
|||
.0
|
||||
.iter()
|
||||
.map(|x| match x {
|
||||
Branch::Left(value) => value.into(),
|
||||
Branch::Right(value) => value.into(),
|
||||
Branch::Left(value) | Branch::Right(value) => value.into(),
|
||||
})
|
||||
.collect::<Vec<BigInt>>()
|
||||
}
|
||||
|
@ -45,6 +44,7 @@ fn hash_signal(signal: &[u8]) -> BigInt {
|
|||
}
|
||||
|
||||
/// Internal helper to hash the external nullifier
|
||||
#[must_use]
|
||||
pub fn hash_external_nullifier(nullifier: &[u8]) -> BigInt {
|
||||
let mut hash = keccak256(nullifier).to_vec();
|
||||
hash.splice(..3, vec![0; 4]);
|
||||
|
@ -52,13 +52,14 @@ pub fn hash_external_nullifier(nullifier: &[u8]) -> BigInt {
|
|||
}
|
||||
|
||||
/// Generates the nullifier hash
|
||||
#[must_use]
|
||||
pub fn generate_nullifier_hash(identity: &Identity, external_nullifier: &[u8]) -> BigInt {
|
||||
let res = POSEIDON
|
||||
.hash(vec![
|
||||
bigint_to_fr(&hash_external_nullifier(external_nullifier)),
|
||||
bigint_to_fr(&identity.nullifier),
|
||||
])
|
||||
.unwrap();
|
||||
.expect("hash with fixed input size can't fail");
|
||||
fr_to_bigint(res)
|
||||
}
|
||||
|
||||
|
@ -97,19 +98,17 @@ pub fn generate_proof(
|
|||
inputs
|
||||
};
|
||||
|
||||
use std::time::Instant;
|
||||
let now = Instant::now();
|
||||
|
||||
let mut wtns = WitnessCalculator::new(&config.wasm).unwrap();
|
||||
let mut witness = WitnessCalculator::new(&config.wasm).unwrap();
|
||||
|
||||
let full_assignment = wtns
|
||||
let full_assignment = witness
|
||||
.calculate_witness_element::<Bn254, _>(inputs, false)
|
||||
.unwrap();
|
||||
|
||||
println!("witness generation took: {:.2?}", now.elapsed());
|
||||
|
||||
let mut rng = thread_rng();
|
||||
use ark_std::UniformRand;
|
||||
let rng = &mut rng;
|
||||
|
||||
let r = ark_bn254::Fr::rand(rng);
|
||||
|
|
|
@ -2,6 +2,7 @@ use ff::{PrimeField, PrimeFieldRepr};
|
|||
use num_bigint::{BigInt, Sign};
|
||||
use poseidon_rs::{Fr, FrRepr};
|
||||
|
||||
#[must_use]
|
||||
pub fn fr_to_bigint(fr: Fr) -> BigInt {
|
||||
let mut bytes = [0_u8; 32];
|
||||
fr.into_repr().write_be(&mut bytes[..]).unwrap();
|
||||
|
|
Loading…
Reference in New Issue