feat(vm): add reference identity and deterministic value hashing
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -0,0 +1,23 @@
|
||||
# Hash-v1 oracle provenance
|
||||
|
||||
`hash-conformance.tsv` is generated by `hash-oracle.py` against the official C
|
||||
XXH64 reference, not the Rust VM implementation. Ordinary tests read the checked-in
|
||||
vectors and need no library, network or Python dependency.
|
||||
|
||||
Reference: https://github.com/Cyan4973/xxHash/blob/v0.8.3/xxhash.h
|
||||
|
||||
Header SHA-256: `17973c0dc49d9854ca26caa191f0e12f7a424b68858d9a78de3860d959d85e4b`.
|
||||
The generator verifies both the header digest and `XXH_versionNumber()==803`.
|
||||
|
||||
Example reproduction on macOS, after downloading that pinned header:
|
||||
|
||||
```sh
|
||||
clang -dynamiclib -O2 -x c -DXXH_IMPLEMENTATION -DXXH_STATIC_LINKING_ONLY xxhash.h -o libxxhash-oracle.dylib
|
||||
python3 hash-oracle.py --header xxhash.h --library ./libxxhash-oracle.dylib
|
||||
```
|
||||
|
||||
On Linux, use `-shared -fPIC` instead of `-dynamiclib`. The generator explicitly
|
||||
encodes little-endian integers/reference components and finite Float bit patterns,
|
||||
canonicalizes signed zeros, and uses seed zero. The null row is the machine
|
||||
contract's direct zero result, not a hash of an empty message. Arbitrary `bytes`
|
||||
rows exercise the internal algorithm; they do not expose guest byte strings.
|
||||
@@ -0,0 +1,51 @@
|
||||
"""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()
|
||||
Reference in New Issue
Block a user