52 lines
2.4 KiB
Python
52 lines
2.4 KiB
Python
"""Regenerate hash-v1 vectors using upstream XXH64, never the Rust executor."""
|
|
import argparse
|
|
import ctypes
|
|
import hashlib
|
|
from pathlib import Path
|
|
import struct
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--library", required=True, type=Path)
|
|
parser.add_argument("--header", required=True, type=Path)
|
|
parser.add_argument("--output", type=Path, default=Path(__file__).with_name("hash-conformance.tsv"))
|
|
args = parser.parse_args()
|
|
expected = "17973c0dc49d9854ca26caa191f0e12f7a424b68858d9a78de3860d959d85e4b"
|
|
assert hashlib.sha256(args.header.read_bytes()).hexdigest() == expected
|
|
library = ctypes.CDLL(str(args.library.resolve()))
|
|
assert library.XXH_versionNumber() == 803
|
|
digest = library.XXH64
|
|
digest.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_uint64]
|
|
digest.restype = ctypes.c_uint64
|
|
rows = ["kind\tinput\texpected_hash_hex"]
|
|
|
|
def emit(kind, value, data):
|
|
buffer = ctypes.create_string_buffer(data)
|
|
result = digest(ctypes.cast(buffer, ctypes.c_void_p), len(data), 0)
|
|
rows.append(f"{kind}\t{value}\t{result:016X}")
|
|
|
|
for size in list(range(71)) + [95, 96, 97, 127, 128, 129, 255, 256, 257, 1024, 4097]:
|
|
data = bytes((i * 131 + 17) & 255 for i in range(size))
|
|
emit("bytes", data.hex(), data)
|
|
for value in [0, 1, -1, 2, -2, -(2**63), 2**63 - 1, 2**53 + 1, 2**32]:
|
|
emit("i64", value, struct.pack("<q", value))
|
|
for bits in [0, 0x8000000000000000, 0x3FF0000000000000, 0xBFF0000000000000,
|
|
0x3FE0000000000000, 0xBFE0000000000000, 1, 0x8000000000000001,
|
|
0x000FFFFFFFFFFFFF, 0x0010000000000000,
|
|
0x7FEFFFFFFFFFFFFF, 0xFFEFFFFFFFFFFFFF, 0x4340000000000000]:
|
|
canonical = 0 if bits & 0x7FFFFFFFFFFFFFFF == 0 else bits
|
|
emit("float", f"{bits:016X}", struct.pack("<Q", canonical))
|
|
for value in ["", "hello", "\0", "ação 😀", "a" * 31, "a" * 32, "a" * 33, "z" * 1025]:
|
|
data = value.encode("utf-8")
|
|
emit("string", data.hex(), data)
|
|
for index, generation in [(0, 1), (1, 1), (1, 2), (1024, 1), (2**32-1, 2**32-1)]:
|
|
emit("reference", f"{index},{generation}", struct.pack("<II", index, generation))
|
|
rows.append("null\tnull\t0000000000000000")
|
|
args.output.write_text("\n".join(rows) + "\n", encoding="utf-8")
|
|
print(f"Generated {len(rows)-1} vectors with upstream XXH64 v0.8.3, seed 0.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|