chore(rln): bring hash functions under a single module (#146)

This commit is contained in:
tyshko-rostyslav 2023-04-20 12:54:29 +02:00 committed by GitHub
parent 9931e901e5
commit 4f98fd8028
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 26 additions and 24 deletions

View File

@ -1,7 +1,8 @@
// This crate instantiate the Poseidon hash algorithm // 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 once_cell::sync::Lazy;
use tiny_keccak::{Hasher, Keccak};
use utils::poseidon::Poseidon; use utils::poseidon::Poseidon;
// These indexed constants hardcodes the supported round parameters tuples (t, RF, RN, SKIP_MATRICES) for the Bn254 scalar field // 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()) .hash(input.to_vec())
.expect("hash with fixed input size can't fail") .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
}

View File

@ -1,7 +1,7 @@
#![allow(dead_code)] #![allow(dead_code)]
pub mod circuit; pub mod circuit;
pub mod poseidon_hash; pub mod hashers;
pub mod poseidon_tree; pub mod poseidon_tree;
pub mod protocol; pub mod protocol;
pub mod public; pub mod public;

View File

@ -3,7 +3,7 @@
// Implementation inspired by https://github.com/worldcoin/semaphore-rs/blob/d462a4372f1fd9c27610f2acfe4841fab1d396aa/src/poseidon_tree.rs (no differences) // Implementation inspired by https://github.com/worldcoin/semaphore-rs/blob/d462a4372f1fd9c27610f2acfe4841fab1d396aa/src/poseidon_tree.rs (no differences)
use crate::circuit::Fr; use crate::circuit::Fr;
use crate::poseidon_hash::poseidon_hash; use crate::hashers::poseidon_hash;
use cfg_if::cfg_if; use cfg_if::cfg_if;
use utils::merkle_tree::*; use utils::merkle_tree::*;

View File

@ -17,7 +17,8 @@ use thiserror::Error;
use tiny_keccak::{Hasher as _, Keccak}; use tiny_keccak::{Hasher as _, Keccak};
use crate::circuit::{Curve, Fr}; 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::poseidon_tree::*;
use crate::public::RLN_IDENTIFIER; use crate::public::RLN_IDENTIFIER;
use crate::utils::*; 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( pub fn compute_id_secret(
share1: (Fr, Fr), share1: (Fr, Fr),
share2: (Fr, Fr), share2: (Fr, Fr),

View File

@ -1,5 +1,5 @@
use crate::circuit::{vk_from_raw, zkey_from_raw, Curve, Fr}; 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::poseidon_tree::PoseidonTree;
use crate::protocol::*; use crate::protocol::*;
use crate::utils::*; use crate::utils::*;

View File

@ -4,7 +4,7 @@ mod test {
use rand::Rng; use rand::Rng;
use rln::circuit::*; use rln::circuit::*;
use rln::ffi::{hash as ffi_hash, poseidon_hash as ffi_poseidon_hash, *}; 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::protocol::*;
use rln::public::RLN; use rln::public::RLN;
use rln::utils::*; use rln::utils::*;

View File

@ -88,7 +88,7 @@ mod pmtree_test {
use pmtree::*; use pmtree::*;
use rln::circuit::Fr; 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::poseidon_tree::PoseidonHash;
use rln::protocol::hash_to_field; use rln::protocol::hash_to_field;
use rln::utils::str_to_fr; use rln::utils::str_to_fr;

View File

@ -4,7 +4,7 @@ mod test {
circom_from_folder, vk_from_folder, zkey_from_folder, Fr, TEST_RESOURCES_FOLDER, circom_from_folder, vk_from_folder, zkey_from_folder, Fr, TEST_RESOURCES_FOLDER,
TEST_TREE_HEIGHT, TEST_TREE_HEIGHT,
}; };
use rln::poseidon_hash::poseidon_hash; use rln::hashers::{hash_to_field, poseidon_hash};
use rln::poseidon_tree::PoseidonTree; use rln::poseidon_tree::PoseidonTree;
use rln::protocol::*; use rln::protocol::*;
use rln::utils::str_to_fr; use rln::utils::str_to_fr;

View File

@ -3,8 +3,8 @@ mod test {
use ark_std::{rand::thread_rng, UniformRand}; use ark_std::{rand::thread_rng, UniformRand};
use rand::Rng; use rand::Rng;
use rln::circuit::{Fr, TEST_RESOURCES_FOLDER, TEST_TREE_HEIGHT}; use rln::circuit::{Fr, TEST_RESOURCES_FOLDER, TEST_TREE_HEIGHT};
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::{compute_tree_root, deserialize_identity_tuple, hash_to_field}; 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::public::{hash as public_hash, poseidon_hash as public_poseidon_hash, RLN};
use rln::utils::*; use rln::utils::*;
use std::io::Cursor; use std::io::Cursor;