diff --git a/rln/src/poseidon_hash.rs b/rln/src/hashers.rs similarity index 64% rename from rln/src/poseidon_hash.rs rename to rln/src/hashers.rs index ff3febb..f691f8c 100644 --- a/rln/src/poseidon_hash.rs +++ b/rln/src/hashers.rs @@ -1,7 +1,8 @@ // This crate instantiate the Poseidon hash algorithm -use crate::circuit::Fr; +use crate::{circuit::Fr, utils::bytes_le_to_fr}; use once_cell::sync::Lazy; +use tiny_keccak::{Hasher, Keccak}; use utils::poseidon::Poseidon; // These indexed constants hardcodes the supported round parameters tuples (t, RF, RN, SKIP_MATRICES) for the Bn254 scalar field @@ -26,3 +27,17 @@ pub fn poseidon_hash(input: &[Fr]) -> Fr { .hash(input.to_vec()) .expect("hash with fixed input size can't fail") } + +// Hashes arbitrary signal to the underlying prime field +pub fn hash_to_field(signal: &[u8]) -> Fr { + // We hash the input signal using Keccak256 + // (note that a bigger curve order might require a bigger hash blocksize) + let mut hash = [0; 32]; + let mut hasher = Keccak::v256(); + hasher.update(signal); + hasher.finalize(&mut hash); + + // We export the hash as a field element + let (el, _) = bytes_le_to_fr(hash.as_ref()); + el +} diff --git a/rln/src/lib.rs b/rln/src/lib.rs index 76bb747..5fc9c5d 100644 --- a/rln/src/lib.rs +++ b/rln/src/lib.rs @@ -1,7 +1,7 @@ #![allow(dead_code)] pub mod circuit; -pub mod poseidon_hash; +pub mod hashers; pub mod poseidon_tree; pub mod protocol; pub mod public; diff --git a/rln/src/poseidon_tree.rs b/rln/src/poseidon_tree.rs index bbed00d..e85ccf9 100644 --- a/rln/src/poseidon_tree.rs +++ b/rln/src/poseidon_tree.rs @@ -3,7 +3,7 @@ // Implementation inspired by https://github.com/worldcoin/semaphore-rs/blob/d462a4372f1fd9c27610f2acfe4841fab1d396aa/src/poseidon_tree.rs (no differences) use crate::circuit::Fr; -use crate::poseidon_hash::poseidon_hash; +use crate::hashers::poseidon_hash; use cfg_if::cfg_if; use utils::merkle_tree::*; diff --git a/rln/src/protocol.rs b/rln/src/protocol.rs index c4fdf33..fbd3dd8 100644 --- a/rln/src/protocol.rs +++ b/rln/src/protocol.rs @@ -17,7 +17,8 @@ use thiserror::Error; use tiny_keccak::{Hasher as _, Keccak}; use crate::circuit::{Curve, Fr}; -use crate::poseidon_hash::poseidon_hash; +use crate::hashers::hash_to_field; +use crate::hashers::poseidon_hash; use crate::poseidon_tree::*; use crate::public::RLN_IDENTIFIER; use crate::utils::*; @@ -483,20 +484,6 @@ pub fn extended_seeded_keygen(signal: &[u8]) -> (Fr, Fr, Fr, Fr) { ) } -// Hashes arbitrary signal to the underlying prime field -pub fn hash_to_field(signal: &[u8]) -> Fr { - // We hash the input signal using Keccak256 - // (note that a bigger curve order might require a bigger hash blocksize) - let mut hash = [0; 32]; - let mut hasher = Keccak::v256(); - hasher.update(signal); - hasher.finalize(&mut hash); - - // We export the hash as a field element - let (el, _) = bytes_le_to_fr(hash.as_ref()); - el -} - pub fn compute_id_secret( share1: (Fr, Fr), share2: (Fr, Fr), diff --git a/rln/src/public.rs b/rln/src/public.rs index b8c2639..1eba48a 100644 --- a/rln/src/public.rs +++ b/rln/src/public.rs @@ -1,5 +1,5 @@ use crate::circuit::{vk_from_raw, zkey_from_raw, Curve, Fr}; -use crate::poseidon_hash::poseidon_hash as utils_poseidon_hash; +use crate::hashers::{hash_to_field, poseidon_hash as utils_poseidon_hash}; use crate::poseidon_tree::PoseidonTree; use crate::protocol::*; use crate::utils::*; diff --git a/rln/tests/ffi.rs b/rln/tests/ffi.rs index ad516ec..b9dbf9f 100644 --- a/rln/tests/ffi.rs +++ b/rln/tests/ffi.rs @@ -4,7 +4,7 @@ mod test { use rand::Rng; use rln::circuit::*; use rln::ffi::{hash as ffi_hash, poseidon_hash as ffi_poseidon_hash, *}; - use rln::poseidon_hash::{poseidon_hash as utils_poseidon_hash, ROUND_PARAMS}; + use rln::hashers::{hash_to_field, poseidon_hash as utils_poseidon_hash, ROUND_PARAMS}; use rln::protocol::*; use rln::public::RLN; use rln::utils::*; diff --git a/rln/tests/poseidon_tree.rs b/rln/tests/poseidon_tree.rs index 282aa22..7072b39 100644 --- a/rln/tests/poseidon_tree.rs +++ b/rln/tests/poseidon_tree.rs @@ -88,7 +88,7 @@ mod pmtree_test { use pmtree::*; use rln::circuit::Fr; - use rln::poseidon_hash::poseidon_hash; + use rln::hashers::{hash_to_field, poseidon_hash}; use rln::poseidon_tree::PoseidonHash; use rln::protocol::hash_to_field; use rln::utils::str_to_fr; diff --git a/rln/tests/protocol.rs b/rln/tests/protocol.rs index 74e4035..8a6c4f7 100644 --- a/rln/tests/protocol.rs +++ b/rln/tests/protocol.rs @@ -4,7 +4,7 @@ mod test { circom_from_folder, vk_from_folder, zkey_from_folder, Fr, TEST_RESOURCES_FOLDER, TEST_TREE_HEIGHT, }; - use rln::poseidon_hash::poseidon_hash; + use rln::hashers::{hash_to_field, poseidon_hash}; use rln::poseidon_tree::PoseidonTree; use rln::protocol::*; use rln::utils::str_to_fr; diff --git a/rln/tests/public.rs b/rln/tests/public.rs index e2c8be8..d1bfd67 100644 --- a/rln/tests/public.rs +++ b/rln/tests/public.rs @@ -3,8 +3,8 @@ mod test { use ark_std::{rand::thread_rng, UniformRand}; use rand::Rng; use rln::circuit::{Fr, TEST_RESOURCES_FOLDER, TEST_TREE_HEIGHT}; - use rln::poseidon_hash::{poseidon_hash as utils_poseidon_hash, ROUND_PARAMS}; - use rln::protocol::{compute_tree_root, deserialize_identity_tuple, hash_to_field}; + use rln::hashers::{hash_to_field, poseidon_hash as utils_poseidon_hash, ROUND_PARAMS}; + use rln::protocol::{compute_tree_root, deserialize_identity_tuple}; use rln::public::{hash as public_hash, poseidon_hash as public_poseidon_hash, RLN}; use rln::utils::*; use std::io::Cursor;