Files
aji/tools/hash_vectors.py
dev 89c63c306b cli: add standard hashes and propose owning map contract
Embed std.hash with four ordinary safe byte functions: FNV-1a 64 and
Castagnoli CRC-32C, each with fresh and numeric continuation forms.
Specify exact constants, empty/chunk semantics, arbitrary continuation
states, target-independent numeric results and explicit serialization.
No heap allocation, authority, AIR, verifier or runtime change.

Propose the next U64Map<V> contract: exact key equality, bounded insert,
returned replacement/full ownership, tombstones, moving drain and
failure-preserving peak-charged rehash. Keep general HashMap<K,V> key
hash/equality policy open as Q-037. A checked slot prerequisite confirms
read-only Copy key/state fields over Option<V>, with explicit accounting
for both tags and owning payload cleanup; no map module is registered.

Validation: unit tests, 797 full conformance cases, 12 new fixtures;
all 11 hash cases with per-instruction collection and 12 schedule seeds
across four quanta, plus owning slot collection; 84 source/AIR, package
and hosted CLI checks; independent forward-polynomial CRC and arbitrary
precision FNV oracles for 286 inputs / 1144 results; 95/95 source coverage;
Zig formatting, whitespace and reference generation. Published vectors
come from RFC 9923 and RFC 3720, with explicit numeric/wire conventions.

Reference cost: source excluding AIR 53,993 -> 55,281 cl100k (+2.4%);
Quick Reference 7,747 -> 7,806 (+0.8%); AIR remains 175,736 bytes.
Map design remains outside both bundles. No empirical MCC or native
throughput claim; CRC initially uses eight bit steps per byte.
2026-10-07 00:53:50 +03:00

53 lines
2.1 KiB
Python

#!/usr/bin/env python3
"""Check recorded FNV/CRC vectors with arbitrary precision and a forward CRC."""
from pathlib import Path
import re
FIXTURES = Path(__file__).resolve().parents[1] / "tests/conformance/source/hash"
def reflect(value, width):
return int(f"{value:0{width}b}"[::-1], 2)
def crc32c(data, previous=0):
# Forward polynomial and left shifts: independent of Aji's reflected loop.
state = reflect(previous ^ 0xffffffff, 32)
for byte in data:
state ^= reflect(byte, 8) << 24
for _ in range(8):
top = state >> 31
state = ((state << 1) & 0xffffffff) ^ (0x1edc6f41 if top else 0)
return reflect(state, 32) ^ 0xffffffff
def fnv1a64(data, previous=0xcbf29ce484222325):
for byte in data:
previous = ((previous ^ byte) * 1099511628211) % (1 << 64)
return previous
def main():
# RFC 9923 FNV-1a; RFC 3720 B.4 numeric CRCs (its wire bytes are LE).
assert fnv1a64(b"foobar") == 0x85944171f73967e8
assert crc32c(b"123456789") == 0xe3069283
assert crc32c(bytes(32)) == 0x8a9136aa
assert crc32c(bytes([255]) * 32) == 0x62a8ab43
assert crc32c(bytes(range(32))) == 0x46dd794e
assert crc32c(bytes(range(31, -1, -1))) == 0x113fdb5c
pattern = r'fixture\(ref (b"(?:\\x[0-9a-f]{2})*"|empty\[0\.\.0\]), (0x[0-9a-f]+)u64, (0x[0-9a-f]+)u32, (0x[0-9a-f]+)u64, (0x[0-9a-f]+)u32\);'
count = 0
for name, expected_count in (("known", 10), ("single_bytes", 256), ("patterns", 20)):
rows = re.findall(pattern, (FIXTURES / f"{name}.aji").read_text())
assert len(rows) == expected_count, (name, len(rows))
for escaped, *numbers in rows:
data = bytes.fromhex(escaped[2:-1].replace("\\x", "")) if escaped.startswith('b"') else b""
expected = (fnv1a64(data), crc32c(data), fnv1a64(data, (1 << 64) - 1), crc32c(data, 0x12345678))
assert tuple(int(number, 16) for number in numbers) == expected, (name, data.hex())
count += 1
print(f"hash oracle vectors passed ({count} inputs, {count * 4} fresh/continuation results)")
if __name__ == "__main__":
main()