Skip to content

hash: Hash.siphash, SipHash in C with any round count and a 64- or 128-bit tag - #2605

Merged
whilp merged 2 commits into
mainfrom
hash-siphash
Oct 3, 2026
Merged

whilp merged 2 commits into
mainfrom
hash-siphash

Conversation

@whilp

@whilp whilp commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator

Adds Hash.siphash(key, data, options?) to cosmic.hash, a C binding (hash_siphash in core/hash.c) following SipHash's reference implementation:

  • SipHash-2-4 with an 8-byte tag by default.
  • Hash.SipHashOptions picks the compression and finalization rounds (1 to 64) and a 16-byte tag (SipHash-128).
  • It raises on a key that isn't 16 bytes, or options out of range.

Why: zig's build cache keys its manifests with SipHash128-1-3, and ci/cosmic_ci/zig_prune.tl (and the CI restat step to follow) recompute those keys. They do it in pure Lua at about 6 MB/s. CI's driver runs on the release ci/cosmic-driver.pin names, so it can use this only once the pin moves. A TODO: at the Lua SipHash marks the switch.

Verification

  • Reference vectors: cosmic/hash_test.tl checks SipHash64 2-4, SipHash128 2-4, SipHash128 1-3 and SipHash64 1-3 at lengths 0, 1, 3, 7, 8, 15, 16, 63 and 257. That covers word tails of 0, 1, 3 and 7 bytes, and the length byte wrapping past 255. Key and message follow the reference vectors' scheme. Tags come from zig's std.crypto.auth.siphash; the first SipHash-2-4 tag, 310e0edd47db6f72, is the paper's own.
  • Refusals: a separate test checks the refused keys and options, and that the defaults equal SipHash-2-4/8.
  • Example.siphash in cosmic/hash_example.tl.
  • o/bin/cosmic test (3521 pass), fix --check ., ci/'s fix --check, bin/zig build analyze and bin/zig build sanitized all pass. The hash tests also pass on the sanitized core.

Review

A separate agent compared 144 tags against zig's: SipHash64 2-4/1-3/4-8, SipHash128 2-4/1-3/3-1, and lengths 0 to 70000. All matched, and it found no argument-handling or C-rule issue. Its findings and what was done:

  • BLOCKING, fixed: export justified by an allowed entry rather than an example. Replaced by Example.siphash.
  • BLOCKING, fixed: missing TODO: at the Lua SipHash the driver will replace. Added.
  • nit, fixed: test lengths didn't reach a 3-byte tail or a wrapped length byte. Added 3 and 257.
  • nit, fixed: the C function's comment sat above a helper. Moved.
  • nit, left: a bad option's error names the raw binding's argument position ("bad argument Switch bootstrap to cosmic prerelease 2026-01-19-9452528 #4"), not the option field. The message itself names the option ("finalization rounds must be 1 to 64"). Mapping positions to fields would duplicate the C's checks in Teal.

TODOs

  • Added: ci/cosmic_ci/zig_prune.tl:89. Hash with Hash.siphash's SipHash128-1-3 rather than in Lua, once ci/cosmic-driver.pin names a release that carries it.
  • Resolved: none.

🤖 Generated with Claude Code

https://claude.ai/code/session_01CA9Ta9WPb5KjCoDaYyN7qE


Generated by Claude Code

claude added 2 commits October 3, 2026 04:47
…t tag

SipHash-2-4 by default; SipHashOptions picks the rounds and a 16-byte
tag, as zig's build cache keys its manifests (SipHash128-1-3), which
ci/cosmic_ci reads in Lua until the driver pin carries this. Checked
against zig's std.crypto.auth.siphash at every tail length.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CA9Ta9WPb5KjCoDaYyN7qE
Review: an Example.siphash rather than an `allowed` entry, the Lua
SipHash in ci/cosmic_ci/zig_prune.tl marked for the driver pin, test
lengths reaching a 3-byte tail and a wrapped length byte (257), and the
function's comment above the function.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CA9Ta9WPb5KjCoDaYyN7qE
@whilp
whilp enabled auto-merge October 3, 2026 04:54
@whilp
whilp added this pull request to the merge queue Oct 3, 2026
Merged via the queue into main with commit d4fadd9 Oct 3, 2026
9 checks passed
@whilp
whilp deleted the hash-siphash branch October 3, 2026 05:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants