#!/usr/bin/env python3 """Bounded native StreamVByte checks, not a speed or corruption-rate benchmark. Fetch fast-pack/streamvbyte revision REVISION (Apache-2.0), then run: python3 stream-vbyte-probe.py --source /path/to/streamvbyte --output result.json Only generated valid inputs reach the decoder. No third-party code is bundled. """ import argparse import ctypes as C import hashlib import json import platform import random import subprocess import tempfile from datetime import datetime, timezone from pathlib import Path REVISION = "089549994faf3657c13e62f6a1a7b2f544308922" SOURCE_DIGEST = "188bde916401ff52ab79e1fc6bbb3528b84dcf09cc2338c3ea584af5f06d6f77" def require(condition, message): if not condition: raise ValueError(message) def scalar_format(values): controls = bytearray((len(values) + 3) // 4) data = bytearray() for i, value in enumerate(values): width = max(1, (value.bit_length() + 7) // 8) controls[i // 4] |= (width - 1) << (2 * (i % 4)) data.extend(value.to_bytes(width, "little")) return bytes(controls + data) def source_digest(root): paths = sorted(p for folder in ("include", "src") for p in (root / folder).rglob("*") if p.is_file()) lines = "".join(hashlib.sha256(p.read_bytes()).hexdigest() + " " + p.relative_to(root).as_posix() + "\n" for p in paths) return hashlib.sha256(lines.encode()).hexdigest() def run(root): require(source_digest(root) == SOURCE_DIGEST, "Source differs from the pinned revision") command = ["cc", "-O2", "-fPIC", "-shared", "-Iinclude", "src/streamvbyte_encode.c", "src/streamvbyte_decode.c"] with tempfile.TemporaryDirectory(prefix="hesela-streamvbyte-") as temporary: library = Path(temporary) / "probe.so" subprocess.run(command + ["-o", str(library)], cwd=root, check=True, capture_output=True, timeout=60) lib = C.CDLL(str(library)) u8, u32 = C.POINTER(C.c_uint8), C.POINTER(C.c_uint32) lib.streamvbyte_encode.argtypes = [u32, C.c_uint32, u8] lib.streamvbyte_encode.restype = C.c_size_t lib.streamvbyte_decode_unpadded.argtypes = [u8, u32, C.c_uint32] lib.streamvbyte_decode_unpadded.restype = C.c_size_t lib.streamvbyte_validate_stream.argtypes = [u8, C.c_size_t, C.c_uint32] lib.streamvbyte_validate_stream.restype = C.c_bool def buffer(data): return (C.c_uint8 * max(1, len(data)))(*data) def valid(data, count): return bool(lib.streamvbyte_validate_stream(buffer(data), len(data), count)) def decode(data, count): require(valid(data, count), "Only validated generated streams may be decoded") output = (C.c_uint32 * max(1, count))() consumed = lib.streamvbyte_decode_unpadded(buffer(data), output, count) require(consumed == len(data), "Wrong decoded byte count") return list(output)[:count] examples = [ ("empty", []), ("one", [0]), ("partial-group", [1, 255, 256]), ("four-127", [127] * 4), ("four-128", [128] * 4), ("four-u32-max", [2**32 - 1] * 4), ("mixed-widths", [0, 255, 256, 2**24]), ("group-boundary", [1, 2, 3, 4, 5]), ] rng = random.Random(6069) cases = list(examples) lengths = [0, 1, 2, 3, 4, 5, 7, 8, 15, 16, 31, 32, 33, 128] boundaries = [0, 1, 127, 128, 255, 256, 65535, 65536, 2**24 - 1, 2**24, 2**32 - 1] for index in range(128): length = lengths[index % len(lengths)] values = [rng.choice(boundaries) if index % 2 else rng.getrandbits(32) for _ in range(length)] cases.append(("selected-" + str(index), values)) rows = [] truncations = 0 for name, values in cases: count = len(values) inputs = (C.c_uint32 * max(1, count))(*values) encoded = (C.c_uint8 * (4 * count + (count + 3) // 4 + 16))() size = lib.streamvbyte_encode(inputs, count, encoded) raw = bytes(encoded[:size]) require(raw == scalar_format(values), name + ": byte disagreement") require(decode(raw, count) == values, name + ": value disagreement") require(not valid(raw + b"\x00", count), name + ": extra byte accepted") require(not valid(raw, count + 1), name + ": wrong count accepted") if raw: require(not valid(raw[:-1], count), name + ": truncation accepted") truncations += 1 if len(rows) < len(examples): rows.append(dict(name=name, values=values, hex=raw.hex(" "), bytes=size, base128Bytes=sum(max(1, (v.bit_length() + 6) // 7) for v in values))) original = scalar_format([1, 2, 3, 4]) changed = original[:-1] + bytes([original[-1] ^ 1]) mutation = dict(beforeHex=original.hex(" "), afterHex=changed.hex(" "), sizeValidation=valid(changed, 4), before=decode(original, 4), after=decode(changed, 4)) require(mutation["sizeValidation"] and mutation["after"] == [1, 2, 3, 5], "Mutation control failed") return dict(schemaVersion=1, generatedAt=datetime.now(timezone.utc).isoformat(), upstream=dict(repository="https://github.com/fast-pack/streamvbyte", revision=REVISION, license="Apache-2.0", includeSrcSha256=SOURCE_DIGEST), environment=dict(python=platform.python_version(), machine=platform.machine(), compiler=subprocess.check_output(["cc", "--version"], text=True).splitlines()[0], compileCommand=command + ["-o", "/probe.so"]), observations=dict(passed=True, cases=len(cases), seed=6069, truncatedRejected=truncations, extraByteRejected=len(cases), wrongCountRejected=len(cases), examples=rows, payloadMutation=mutation), limits=["Generated ordinary-format inputs only; no 0124 or delta-format test.", "Not exhaustive fuzzing, a performance reproduction, or a field corruption rate."]) def main(): parser = argparse.ArgumentParser() parser.add_argument("--source", required=True, type=Path) parser.add_argument("--output", type=Path) parser.add_argument("--check", type=Path) args = parser.parse_args() report = run(args.source.resolve()) if args.check: prior = json.loads(args.check.read_text()) require(report["upstream"] == prior["upstream"] and report["observations"] == prior["observations"], "Retained observations differ") if args.output: args.output.write_text(json.dumps(report, indent=2) + "\n") print(json.dumps(report["observations"], sort_keys=True)) if __name__ == "__main__": main()