modified: .gitlab-ci.yml modified: bench/qa_questions.txt modified: bench/qa_sweep.py modified: bench/run.sh modified: docs/TICKETS.md modified: docs/_source/README.md modified: docs/_source/_ext/makefile_targets.py modified: docs/_source/api/cli.rst modified: docs/_source/api/distill.rst modified: docs/_source/api/mesh.rst modified: docs/_source/api/qa.rst modified: docs/_source/api/retrieval.rst modified: docs/_source/api/storage.rst modified: docs/_source/api/substrate.rst modified: docs/_source/concepts.rst modified: docs/_source/conf.py modified: docs/_source/cookbook.rst modified: docs/_source/index.rst modified: docs/_source/license.rst modified: docs/_source/quickstart.rst modified: docs/bench-maxing.md modified: docs/benchmarks.md modified: docs/cti-architecture.md modified: docs/diagrams/aborist-modules.dot modified: docs/diagrams/aborist-modules.svg modified: docs/diagrams/mesh-data-flow.dot modified: docs/diagrams/mesh-epoch-lifecycle.dot modified: docs/diagrams/mesh-epoch-lifecycle.svg modified: docs/diagrams/mesh-group-decisions.dot modified: docs/diagrams/mesh-group-decisions.svg modified: docs/diagrams/mesh-identity-stack.dot modified: docs/diagrams/mesh-secret-envelope.dot modified: docs/mesh.md modified: docs/qa-modes-bench.md modified: docs/seven-point-program.md modified: docs/tickets/ticket-000001-retrieval-keywords-audit-gap.md modified: docs/tickets/ticket-000002-reference-frame-polarity-contract.md modified: docs/tickets/ticket-000003-anchor-class-warrant.md modified: docs/tickets/ticket-000005-label-ladder-migration.md modified: docs/tickets/ticket-000006-bench-emergent-findings.md modified: docs/tickets/ticket-000007-query-layer-hyphen-fold.md modified: docs/tickets/ticket-000008-broad-quantifier-preflight-guard.md modified: docs/tickets/ticket-000009-quantifier-preflight-dag-binding.md modified: docs/tickets/ticket-000010-metacognition-preflight-guard.md modified: docs/tickets/ticket-000011-soft-preflight-hint-sidecar.md modified: scripts/backfill_concepts.py modified: scripts/bench_emergent.py modified: tests/crawler/test_async_web_fetcher.py modified: tests/crawler/test_bridge.py modified: tests/crawler/test_web_fetch.py modified: tests/test_bench_qa_sweep.py modified: tests/test_burn.py modified: tests/test_burn_doc.py modified: tests/test_claim_lattice.py modified: tests/test_cli_render.py modified: tests/test_compress.py modified: tests/test_concepts.py modified: tests/test_dag.py modified: tests/test_directives.py modified: tests/test_distill.py modified: tests/test_distill_recursive.py modified: tests/test_evict.py modified: tests/test_frame.py modified: tests/test_grok_source.py modified: tests/test_html_source.py modified: tests/test_ingest.py modified: tests/test_inspect.py modified: tests/test_journal.py modified: tests/test_keys.py modified: tests/test_llm_context_base.py modified: tests/test_merkle.py modified: tests/test_mesh.py modified: tests/test_mesh_aead.py modified: tests/test_mesh_chain.py modified: tests/test_mesh_cli.py modified: tests/test_mesh_cli_pull.py modified: tests/test_mesh_wire.py modified: tests/test_mesh_wire_e2e.py modified: tests/test_metacognition.py modified: tests/test_migration_audit_mode.py modified: tests/test_providence_source.py modified: tests/test_qa.py modified: tests/test_qa_quality_live.py modified: tests/test_quantifier_caps.py modified: tests/test_quantifier_classifier.py modified: tests/test_quantifier_phase4.py modified: tests/test_quantifier_reminder.py modified: tests/test_query.py modified: tests/test_reclassify.py modified: tests/test_repair.py modified: tests/test_resume.py modified: tests/test_snapshot.py modified: tests/test_soft_preflight.py modified: tests/test_tfidf.py modified: tests/test_vcs_source.py modified: tests/test_verify.py modified: tests/test_verify_json.py modified: tests/test_versioned_ingest.py modified: tests/test_warrant.py modified: tests/test_wikipedia_old.py modified: tests/test_wikipedia_xml.py modified: tests/test_wikitext.py
155 lines
4.3 KiB
Python
155 lines
4.3 KiB
Python
"""Merkle round-trip and tamper-detection tests.
|
|
|
|
These exercise the proxy.unturf.com Go-merkle conventions ported into Python:
|
|
- non-commutative HashCombine (0x03 prefix)
|
|
- self-duplicate odd elements
|
|
- explicit IsLeft per sibling (no lexical ordering)
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from arborist.merkle import (
|
|
HASH_LEN,
|
|
MerkleTree,
|
|
ZERO_HASH,
|
|
hash_combine,
|
|
hash_leaf,
|
|
proof_from_dict,
|
|
proof_to_dict,
|
|
verify_proof,
|
|
)
|
|
|
|
|
|
def _leaf(s: str) -> bytes:
|
|
return hash_leaf(s.encode("utf-8"))
|
|
|
|
|
|
def test_empty_tree_root_is_zero_hash():
|
|
tree = MerkleTree.build([])
|
|
assert tree.root == ZERO_HASH
|
|
|
|
|
|
def test_single_leaf_root_equals_leaf():
|
|
leaves = [_leaf("only-chunk")]
|
|
tree = MerkleTree.build(leaves)
|
|
# No interior layer needed; root is the single leaf itself.
|
|
assert tree.root == leaves[0]
|
|
|
|
|
|
def test_combine_is_non_commutative():
|
|
a = _leaf("a")
|
|
b = _leaf("b")
|
|
assert hash_combine(a, b) != hash_combine(b, a)
|
|
|
|
|
|
def test_two_leaves_round_trip():
|
|
leaves = [_leaf("alpha"), _leaf("beta")]
|
|
tree = MerkleTree.build(leaves)
|
|
expected_root = hash_combine(leaves[0], leaves[1])
|
|
assert tree.root == expected_root
|
|
|
|
for i in range(len(leaves)):
|
|
proof = tree.proof(i)
|
|
assert verify_proof(proof)
|
|
assert proof.root == tree.root
|
|
|
|
|
|
def test_three_leaves_self_duplicate_odd():
|
|
leaves = [_leaf("a"), _leaf("b"), _leaf("c")]
|
|
tree = MerkleTree.build(leaves)
|
|
# Layer 1: combine(a,b), combine(c,c)
|
|
n01 = hash_combine(leaves[0], leaves[1])
|
|
n22 = hash_combine(leaves[2], leaves[2])
|
|
expected_root = hash_combine(n01, n22)
|
|
assert tree.root == expected_root
|
|
|
|
for i in range(3):
|
|
proof = tree.proof(i)
|
|
assert verify_proof(proof), f"proof for index {i} should verify"
|
|
|
|
|
|
def test_four_leaves_full_round_trip():
|
|
leaves = [_leaf(f"chunk-{i}") for i in range(4)]
|
|
tree = MerkleTree.build(leaves)
|
|
for i in range(4):
|
|
proof = tree.proof(i)
|
|
assert verify_proof(proof)
|
|
assert len(proof.siblings) == 2 # log2(4) = 2 levels
|
|
|
|
|
|
def test_seven_leaves_full_round_trip():
|
|
"""Odd intermediate layers self-duplicate; every proof must still verify."""
|
|
leaves = [_leaf(f"chunk-{i}") for i in range(7)]
|
|
tree = MerkleTree.build(leaves)
|
|
for i in range(7):
|
|
proof = tree.proof(i)
|
|
assert verify_proof(proof), f"proof for index {i} should verify"
|
|
|
|
|
|
def test_proof_serialization_round_trip():
|
|
leaves = [_leaf(f"x-{i}") for i in range(5)]
|
|
tree = MerkleTree.build(leaves)
|
|
proof = tree.proof(2)
|
|
d = proof_to_dict(proof)
|
|
restored = proof_from_dict(d)
|
|
assert restored == proof
|
|
assert verify_proof(restored)
|
|
|
|
|
|
def test_tampered_leaf_fails_verification():
|
|
leaves = [_leaf(f"chunk-{i}") for i in range(4)]
|
|
tree = MerkleTree.build(leaves)
|
|
proof = tree.proof(1)
|
|
# Tamper: replace the leaf bytes.
|
|
bad_leaf = _leaf("not-the-real-chunk")
|
|
bad_proof = type(proof)(
|
|
leaf=bad_leaf,
|
|
leaf_index=proof.leaf_index,
|
|
siblings=proof.siblings,
|
|
root=proof.root,
|
|
)
|
|
assert not verify_proof(bad_proof)
|
|
|
|
|
|
def test_tampered_sibling_fails_verification():
|
|
leaves = [_leaf(f"chunk-{i}") for i in range(4)]
|
|
tree = MerkleTree.build(leaves)
|
|
proof = tree.proof(0)
|
|
from arborist.merkle import ProofNode
|
|
|
|
bad_siblings = list(proof.siblings)
|
|
s0 = bad_siblings[0]
|
|
bad_siblings[0] = ProofNode(hash=b"\xff" * HASH_LEN, is_left=s0.is_left)
|
|
bad_proof = type(proof)(
|
|
leaf=proof.leaf,
|
|
leaf_index=proof.leaf_index,
|
|
siblings=tuple(bad_siblings),
|
|
root=proof.root,
|
|
)
|
|
assert not verify_proof(bad_proof)
|
|
|
|
|
|
def test_swapped_is_left_flag_fails():
|
|
"""Order must be preserved — flipping the IsLeft flag must reject."""
|
|
leaves = [_leaf(f"chunk-{i}") for i in range(4)]
|
|
tree = MerkleTree.build(leaves)
|
|
proof = tree.proof(1)
|
|
from arborist.merkle import MerkleProof, ProofNode
|
|
|
|
flipped = MerkleProof(
|
|
leaf=proof.leaf,
|
|
leaf_index=proof.leaf_index,
|
|
siblings=tuple(
|
|
ProofNode(hash=s.hash, is_left=not s.is_left) for s in proof.siblings
|
|
),
|
|
root=proof.root,
|
|
)
|
|
assert not verify_proof(flipped)
|
|
|
|
|
|
def test_out_of_range_leaf_index():
|
|
tree = MerkleTree.build([_leaf("x"), _leaf("y")])
|
|
with pytest.raises(IndexError):
|
|
tree.proof(5)
|