qfall_math/integer/z/hash.rs
1// Copyright © 2025 Niklas Siemer
2//
3// This file is part of qFALL-math.
4//
5// qFALL-math is free software: you can redistribute it and/or modify it under
6// the terms of the Mozilla Public License Version 2.0 as published by the
7// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.
8
9//! Implementation of the [`Hash`] trait for [`Z`].
10
11use super::Z;
12use std::hash::Hash;
13
14// MODULUS is 2^62 - 57, which is the largest prime below 2^62.
15// We choose it prime to reduce the risk of accidentally introducing
16// a hash collision by multiplying values by a prime factor of the given modulus.
17static MODULUS: i64 = 4611686018427387847;
18
19impl Hash for Z {
20 /// Calculates the value of `self` mod 2^62 - 57 and then uses
21 /// the inbuilt hash function for [`i64`].
22 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
23 // We use Hash of i64 instead of String or Vec<u8> as it
24 // significantly reduces the runtime of this function.
25 let modulo_value = (self % MODULUS).value.0;
26 modulo_value.hash(state);
27 }
28}