pythnet_sdk/hashers/
prime.rs1use {crate::hashers::Hasher, serde::Serialize, sha3::Digest, slow_primes::is_prime_miller_rabin};
2
3#[derive(Clone, Default, Debug, Eq, PartialEq, Serialize)]
4pub struct PrimeHasher {}
5
6impl Hasher for PrimeHasher {
7 type Hash = [u8; 16];
9
10 fn hashv(data: &[impl AsRef<[u8]>]) -> [u8; 16] {
11 let mut search = 0usize;
15
16 loop {
17 search += 1;
19
20 let mut hasher = sha3::Sha3_256::new();
22 for d in data {
23 hasher.update(d);
24 }
25 hasher.update(search.to_be_bytes());
26 let hash_bytes: [u8; 32] = hasher.finalize().into();
27
28 let prime = u32::from_be_bytes(hash_bytes[28..].try_into().unwrap()) | 1;
30 if is_prime_miller_rabin(prime as u64) {
31 return (prime as u128).to_be_bytes();
32 }
33 }
34 }
35}