Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "spec/multihash"]
path = spec/multihash
url = https://github.com/multiformats/multihash.git
[submodule "spec/multicodec"]
path = spec/multicodec
url = https://github.com/multiformats/multicodec
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exclude: '.project-template|docs/conf.py|.*pb2\..*'
exclude: '.project-template|docs/conf.py|.*pb2\..*|^spec/'
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
Expand Down
5 changes: 5 additions & 0 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ Ready to contribute? Here's how to set up `multihash` for local development.

$ git clone git@github.com:your_name_here/py-multihash.git

Spec compliance and vector tests need git submodules::

$ cd py-multihash
$ git submodule update --init --recursive

3. Install your local copy into a virtualenv. Create and activate a virtual environment::

$ python -m venv venv
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ lint:
pre-commit run --all-files

fix:
python -m ruff check --fix
python -m ruff check --fix --exclude spec

typecheck:
pre-commit run mypy-local --all-files && pre-commit run pyrefly-local --all-files
Expand Down
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#
import os
import sys

sys.path.insert(0, os.path.abspath('..'))

import multihash
Expand Down
457 changes: 229 additions & 228 deletions multihash/constants.py

Large diffs are not rendered by default.

487 changes: 361 additions & 126 deletions multihash/funcs.py

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions newsfragments/52.breaking.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hash name strings now match the upstream multicodec table: ``id`` is ``identity``,
``kangarootwelve`` is ``kt-128``, and Skein family names use lowercase ``skein*``
(previously ``Skein*``). Callers that look up hashes by these string keys must update.
3 changes: 3 additions & 0 deletions newsfragments/52.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Synced supported multihash codes with the upstream multicodec ``table.csv`` (including
``kt-256`` and expanded ``Func`` coverage) and added a bidirectional spec compliance
test against that table.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ addopts = "-m 'not benchmark'"
markers = [
"benchmark: performance benchmarks (excluded from default test runs)",
]
norecursedirs = ["spec"]

[tool.towncrier]
# Read https://github.com/multiformats/py-multihash/blob/master/newsfragments/README.md for instructions
Expand Down Expand Up @@ -198,3 +199,6 @@ project_excludes = [
"dist/**",
"tests/**",
]

[tool.ruff]
extend-exclude = ["spec/", "spec/**/*"]
1 change: 1 addition & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ exclude = [
"build",
"dist",
"docs",
"spec",
"*.egg",
]

Expand Down
1 change: 1 addition & 0 deletions spec/multicodec
Submodule multicodec added at 808fe7
2 changes: 1 addition & 1 deletion tests/test_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class FuncTestCase:

def test_values_match_constants(self):
assert Func.sha2_256 == HASH_CODES["sha2-256"]
assert Func.identity == HASH_CODES["id"]
assert Func.identity == HASH_CODES["identity"]

def test_is_int_enum(self):
assert isinstance(Func.sha2_256, int)
Expand Down
64 changes: 64 additions & 0 deletions tests/test_spec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""Spec compliance tests against multicodec table.csv."""

import csv
import os

from multihash.constants import CODE_HASHES, HASH_CODES
from multihash.funcs import Func

# Codes present locally but tagged ``hash`` (not ``multihash``) in multicodec/table.csv.
# Kept for compatibility; reverse completeness checks allow these intentionally.
_NON_MULTIHASH_LOCAL_ALLOWLIST = {
0x22: "murmur3-128",
0x23: "murmur3-32",
}


def _load_multihash_spec():
"""Return {code: name} for rows tagged multihash in table.csv."""
spec_path = os.path.join(os.path.dirname(__file__), "..", "spec", "multicodec", "table.csv")
expected = {}
with open(spec_path) as f:
reader = csv.DictReader(f, skipinitialspace=True)
for row in reader:
if row["tag"].strip() != "multihash":
continue
name = row["name"].strip()
code = int(row["code"].strip(), 16)
expected[code] = name
return expected


def test_spec_table_completeness():
"""Every multihash entry in table.csv should be in HASH_CODES and Func."""
expected = _load_multihash_spec()
func_values = {f.value for f in Func}

for code, name in expected.items():
assert name in HASH_CODES, f"Missing hash in HASH_CODES: {name} (0x{code:x})"
assert HASH_CODES[name] == code, f"Code mismatch in HASH_CODES for {name}"
assert code in func_values, f"Missing code in Func enum for {name} (0x{code:x})"


def test_spec_no_unexpected_local_entries():
"""Local tables must not contain codes absent from the multihash CSV (except allowlist)."""
expected = _load_multihash_spec()

for name, code in HASH_CODES.items():
if code in _NON_MULTIHASH_LOCAL_ALLOWLIST:
assert _NON_MULTIHASH_LOCAL_ALLOWLIST[code] == name, (
f"Allowlisted code 0x{code:x} expected name {_NON_MULTIHASH_LOCAL_ALLOWLIST[code]!r}, got {name!r}"
)
continue
assert code in expected, f"Local hash {name!r} (0x{code:x}) not defined as tag=multihash in the spec"
assert expected[code] == name, f"Name mismatch for 0x{code:x}: local {name!r}, spec {expected[code]!r}"

for code, name in CODE_HASHES.items():
if code in _NON_MULTIHASH_LOCAL_ALLOWLIST:
continue
assert code in expected, f"CODE_HASHES entry {name!r} (0x{code:x}) not in multihash spec"

for func in Func:
if func.value in _NON_MULTIHASH_LOCAL_ALLOWLIST:
continue
assert func.value in expected, f"Func.{func.name} (0x{func.value:x}) not in multihash spec"
Loading