cosmicledger-verifiable-off.../tests/test_delta_inclusion.py

34 lines
1.2 KiB
Python

import sys
import pathlib
import hashlib
# Ensure repository root is on sys.path so tests can import the package
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from cosmic_ledger.delta import DeltaLog, merkle_path_for_index, verify_inclusion
def test_merkle_inclusion_valid_and_tampered():
dl = DeltaLog()
# add three simple payloads
entries = [ {"v": i} for i in range(3) ]
for e in entries:
dl.add_entry(e)
leaves = [en["digest"] for en in dl.entries]
# for each leaf, compute path and verify inclusion
for i, digest in enumerate(leaves):
path_info = merkle_path_for_index(leaves, i)
root = path_info["root"]
path = path_info["path"]
assert verify_inclusion(digest, path, root, i)
# Tamper with a sibling digest and ensure verification fails
tampered_path = path.copy()
# flip one byte in the first sibling (if present) to simulate corruption
if tampered_path:
first = tampered_path[0]
# replace first hex char with something else (safe mutation)
tampered_path[0] = ("0" if first[0] != "0" else "1") + first[1:]
assert not verify_inclusion(leaves[0], tampered_path, root, 0)