From 3427729f7eeff86b79b5e7f03a4985a1478d4d4b Mon Sep 17 00:00:00 2001 From: G <28568419+s1fr0@users.noreply.github.com> Date: Wed, 23 Nov 2022 10:55:12 +0100 Subject: [PATCH] fix(semaphore): temporarily use fixed semaphore-rs fork (#79) * fix(semaphore): temporarily use fixed semaphore-rs fork * fix(semaphore): update semaphore logic to work with latest master of semaphore-rs --- semaphore/Cargo.toml | 7 +++++-- semaphore/src/protocol.rs | 19 ++++++++++--------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/semaphore/Cargo.toml b/semaphore/Cargo.toml index b6d0132..9efbaa7 100644 --- a/semaphore/Cargo.toml +++ b/semaphore/Cargo.toml @@ -19,9 +19,12 @@ ark-std = { version = "0.3.0", default-features = false, features = ["parallel"] color-eyre = "0.5" num-bigint = { version = "0.4", default-features = false, features = ["rand"] } once_cell = "1.8" -primitive-types = "0.11.1" rand = "0.8.4" -semaphore = { git = "https://github.com/worldcoin/semaphore-rs", rev = "d462a43"} +# TODO: change back to original repo once https://github.com/worldcoin/semaphore-rs/pull/24 is merged +#semaphore = { git = "https://github.com/worldcoin/semaphore-rs", rev = "d462a43"} +semaphore = { git = "https://github.com/vacp2p/semaphore-rs", branch="fix-u256-data-type"} +ethers-core = { git = "https://github.com/gakonst/ethers-rs", default-features = false } +ruint = { version = "1.2.0", features = [ "serde", "num-bigint", "ark-ff" ] } serde = "1.0" thiserror = "1.0.0" wasmer = { version = "2.0" } diff --git a/semaphore/src/protocol.rs b/semaphore/src/protocol.rs index 7b82b60..f863370 100644 --- a/semaphore/src/protocol.rs +++ b/semaphore/src/protocol.rs @@ -13,12 +13,12 @@ use ark_groth16::{ use ark_relations::r1cs::SynthesisError; use ark_std::UniformRand; use color_eyre::Result; -use primitive_types::U256; +use ethers_core::types::U256; use rand::{thread_rng, Rng}; use semaphore::{ identity::Identity, merkle_tree::{self, Branch}, - poseidon_hash, + poseidon, poseidon_tree::PoseidonHash, Field, }; @@ -81,7 +81,7 @@ fn merkle_proof_to_vec(proof: &merkle_tree::Proof) -> Vec { /// Generates the nullifier hash #[must_use] pub fn generate_nullifier_hash(identity: &Identity, external_nullifier: Field) -> Field { - poseidon_hash(&[external_nullifier, identity.nullifier]) + poseidon::hash2(external_nullifier, identity.nullifier) } #[derive(Error, Debug)] @@ -92,6 +92,8 @@ pub enum ProofError { WitnessError(color_eyre::Report), #[error("Error producing proof: {0}")] SynthesisError(#[from] SynthesisError), + #[error("Error converting public input: {0}")] + ToFieldError(#[from] ruint::ToFieldError), } /// Generates a semaphore proof @@ -202,12 +204,11 @@ pub fn verify_proof( let zkey = zkey(); let pvk = prepare_verifying_key(&zkey.0.vk); - let public_inputs = [ - root.into(), - nullifier_hash.into(), - signal_hash.into(), - external_nullifier_hash.into(), - ]; + let public_inputs = [root, nullifier_hash, signal_hash, external_nullifier_hash] + .iter() + .map(ark_bn254::Fr::try_from) + .collect::, _>>()?; + let ark_proof = (*proof).into(); let result = ark_groth16::verify_proof(&pvk, &ark_proof, &public_inputs[..])?; Ok(result)