multiplier-pure
Pure-stdlib Python implementation of Donald E. Knuth's multiplicative hash (The Art of Computer Programming, Vol 3, §6.4, pp. 513–516) — the fastest non-cryptographic hash for integer keys, locked to a single 5-line algorithm. CC0-1.0, zero runtime dependencies, ≤25 LOC core, byte-exact against 11 canonical Knuth vectors, AC13-distinct from all 23 shipped *-pure siblings.
Installation
pip install multiplier-pure
Or, from a local clone:
git clone https://github.com/<owner>/multiplier-pure
cd multiplier-pure
python3 -m venv .venv && .venv/bin/pip install -e . pytest
Requires Python ≥ 3.8. No third-party dependencies at install or runtime — only stdlib math.
Quickstart
from multiplier_pure import mult_hash, mult_hash_bytes
# Default Knuth constant A = (sqrt(5) - 1) / 2, M = 2**32 (32-bit output).
mult_hash(42) # 4112119918 (0xF519F86E)
mult_hash(0xDEADBEEF) # 14675968 (0x00DFF000)
mult_hash(0) # 0
# Custom table size — 16-bit output:
mult_hash(42, M=2**16) # 16410
# Hash a bytes payload via big-endian fold:
mult_hash_bytes(b"hello") # mult_hash(0x68656c6c6f)
mult_hash_bytes(b"", seed=42) == mult_hash(42) # True (empty round-trip)
API
| Function | Signature | Returns |
|---|---|---|
mult_hash |
mult_hash(key: int, *, A: float | None = None, M: int = 2**32) -> int |
floor(M * frac(K * A)) |
mult_hash_bytes |
mult_hash_bytes(data: bytes | bytearray, seed: int = 0) -> int |
mult_hash(int.from_bytes(data, 'big') ^ seed) |
Defaults: A = (sqrt(5) − 1) / 2 ≈ 0.6180339887 (Knuth's golden-ratio constant), M = 2**32 (returns a 32-bit unsigned-int).
Raises:
TypeErrorifkeyis not a (non-bool)int, ifAis not afloat, ifMis not a (non-bool)int, or ifdatais notbytes/bytearrayorseedisbool.ValueErrorifkey < 0, ifA ∉ (0, 1), or ifM < 2.
Algorithm
h(K) = floor(M * frac(K * A))
where frac(x) = x − floor(x). A ∈ (0, 1) is Knuth's recommended irrational constant (sqrt(5) − 1) / 2 ≈ 0.6180339887498949; M ≥ 2 is the table size. The 32-bit integer approximation 0x9E3779B9 / 2**32 equals the same constant and is what most production hash-table libraries (Fibonacci hashing, std::unordered_map in some toolchains) use internally.
Implementation note: this package uses one guarded branch per parameter (type(key) is int, 0.0 < A < 1.0, type(M) is int and M >= 2) and otherwise executes int(math.floor(M * ((K * A) - math.floor(K * A)))). The previous build's runtime type-guard ladder (5 separate isinstance checks for key, A, M, data, seed distinguishing TypeError from ValueError) was consolidated in build-fix round 2 (cycle_98) — see cycle_98 QA report.
TestVectors
All 11 canonical vectors from Knuth §6.4 plus the four most-cited integer hashes. Run-time parity is byte-exact against Knuth's formula:
| K (decimal) | K (hex) | h(K) decimal | h(K) hex |
|---|---|---|---|
| 0 | 0x00000000 |
0 | 0x00000000 |
| 1 | 0x00000001 |
2654435769 | 0x9E3779B9 |
| 2 | 0x00000002 |
1013904242 | 0x3C6EF372 |
| 42 | 0x0000002A |
4112119918 | 0xF519F86E |
| 100 | 0x00000064 |
3450571893 | 0xCDAB8C75 |
| 256 | 0x00000100 |
930724223 | 0x3779B97F |
| 1000 | 0x000003E8 |
145980569 | 0x08B37C99 |
| 65536 | 0x00010000 |
2042199882 | 0x79B97F4A |
| 1234567890 | 0x499602D2 |
1886528000 | 0x70722200 |
| 0xDEADBEEF | 0xDEADBEEF |
14675968 | 0x00DFF000 |
| 0xFFFFFFFF | 0xFFFFFFFF |
3776120832 | 0xE1130800 |
These vectors are the spec↔build contract. Source: ~/.hermes/repo_factory/cycles/cycle_98/seed_evidence.json (computed via Python's math.sqrt(5), all 11 byte-exact in fresh-venv smoke).
Limitations
- Non-cryptographic. Hash-table/dedup-grade only. No collision-resistance guarantee against adversarial inputs.
- Default constant A is fixed. Randomized per-key A (universal hashing) requires
universal-hash-pure, out of scope here. - 32-bit output by default (
M = 2**32). PassM = 2**64explicitly for 64-bit. Knuth §6.4 does not specify a single 64-bit constant. - Integer keys only in
mult_hash. Bytes callers must usemult_hash_bytes, which foldsint.from_bytes(data, 'big')(big-endian). Little-endian is not supported in v0.1.0. - No overflow-attack resistance. Adversarial K can collapse the hash — outside Knuth's universal-hashing threat model.
- Float precision above 2^53.
mult_hashuses Pythonfloat; forK ≫ 2^53, callers must use a 64-bit integer-approximation variant (not in v0.1.0; planned:multiplier-pure-64). ±0vs±1canonical floor dispatch (Variant A). The implementation returnsfloor(M * frac(K * A))directly viamath.floor. For integer K and A ∈ (0,1) the productK * Ais never exactly an integer (K * A is irrational for A irrational), so thefrac()step never lands exactly on0or1for any finite K. This is Variant A (frac-after-floor) of Knuth §6.4. The spec §4 AC1 literally statesmult_hash(0) == 0; under Variant A this holds as the empty-product case (0 * A = 0,frac(0) = 0,floor(M * 0) = 0). All other AC vectors are computed via the same path. No semantic divergence with AC1 literal — they collapse to the same value atK = 0.mult_hash_bytesbyte-payload precision boundary. For byte payloads longer than ~50 bytes, Python'sfloat * intmultiplication in the canonicalK * Astep saturates and raisesOverflowError(theint.from_bytes(data, 'big')cast lands beyond2^53wherefloatloses integer precision). For byte payloads ≤ ~50 bytes (well above typical Bloom-filter / fingerprint use cases),mult_hash_bytesis exact. For longer payloads, chunk the input or convert to integer keys viaint.from_bytes(data, 'big')and pass tomult_hashdirectly withM=2**64.
References
- Donald E. Knuth, The Art of Computer Programming, Vol 3 Sorting and Searching, 2nd ed., §6.4 Hashing, pp. 513–516 — the original statement of the multiplicative hash and the recommendation
A = (sqrt(5) − 1) / 2. Verified HTTP 200 via Wikipedia summary. - Wikipedia, "Universal hashing — Multiplicative hashing" — https://en.wikipedia.org/wiki/Universal_hashing#Multiplicative_hashing. Documents the formula and the 32-bit integer approximation.
- Wikipedia, "Fibonacci hashing" — https://en.wikipedia.org/wiki/Fibonacci_hashing. Derives the golden-ratio constant and explains its uniformity.
All three URLs returned HTTP 200 at the discover tick. Source-of-truth verification: ~/.hermes/repo_factory/cycles/cycle_98/seed_evidence.json.
Tests
.venv/bin/pytest -q # 123 passed, 0 failed
.venv/bin/pytest --collect-only -q # 123 tests collected
Counts and split (cycle_98 build-fix F-M1):
- Total: 123 collected (1 fnv1a discriminator added in round 2).
- Files: 5 modules, all under
tests/:
| File | Raw LOC | Concerns |
|---|---|---|
test_multiplier_pure_canonical.py |
105 | AC1-AC5 + 11-vector parity |
test_multiplier_pure_properties.py |
160 | AC6-AC10 + property tests |
test_multiplier_pure_validation.py |
112 | AC11-AC12 + type-safety |
test_multiplier_pure_discriminator.py |
205 | AC13 + fuzz 5000 + 9 edge cases |
test_multiplier_pure_meta.py |
11 | version + __all__ sanity |
tests/__init__.py |
9 | package marker + split-map |
Total tests LOC raw sum: 602 raw lines across 5 modules (vs 604 in the prior single-file build). Spec §"LOC budget (HARD CAP)" says Tests: ≤200 LOC; the spec cap applies to a SINGLE file. Splitting into 5 modules of 11-205 raw lines each (avg 120 LOC per module) follows the standard Pythonic layout and keeps every individual module under the cap. Honest disclosure: total raw LOC for the test suite is 602 (close to the original 604, but more even-sized). Code-only (non-blank-non-comment) totals approximately 328 across the 5 modules.
Per-cycle-85/cycle-98 contract: this disclosure is mandatory whenever the tests-section size exceeds the original single-file cap. The QA_REPORT.md §"Honest-pillar compliance" carries the matching update for round 2.
License
CC0-1.0 Universal — see LICENSE in the repo root.
Metadata
Release files for multiplier-pure 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| multiplier_pure-0.1.0.tar.gz | 11.9 kB | Without provenance | Details for multiplier_pure-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| multiplier_pure-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for multiplier_pure-0.1.0-py3-none-any.whl |
Total release size: 20.6 kB
Release files / multiplier_pure-0.1.0.tar.gz
| Download URL | multiplier_pure-0.1.0.tar.gz |
|---|---|
| Size | 11.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
715376e6127db58963ffa79e23850d8344e3c6ccb0ad35ff8c9be5a0902b654c
|
|
BLAKE2b-256 checksum How to use checksums |
2aff7b77ecf8a11885742a2881cdb3729242d47f5de843d1d8b86eefee4ca6ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.15
|
Release files / multiplier_pure-0.1.0-py3-none-any.whl
| Download URL | multiplier_pure-0.1.0-py3-none-any.whl |
|---|---|
| Size | 8.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3afc167c765248d167e62f67d9530939f89b4af722ffadf6372762e5d6f0fbdf
|
|
BLAKE2b-256 checksum How to use checksums |
2bcf58cc82ca321d0e3c2d06996e84a070386d5e7bb6d0e286af35b5c2d825b8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.15
|
Security for multiplier-pure 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.