From 85d8e3f0845e269572b39c1e5605f485c9200016 Mon Sep 17 00:00:00 2001 From: aniket866 Date: Thu, 26 Feb 2026 13:30:29 +0530 Subject: [PATCH 1/5] generate_merkle_proof --- openverifiablellm/utils.py | 74 ++++++++++++++++++++++++++++++++++++++ tests/test_util.py | 16 +++++++++ 2 files changed, 90 insertions(+) diff --git a/openverifiablellm/utils.py b/openverifiablellm/utils.py index 0416b1c8..5180ae27 100644 --- a/openverifiablellm/utils.py +++ b/openverifiablellm/utils.py @@ -40,6 +40,80 @@ def compute_merkle_root(file_path: Union[str, Path], chunk_size: int = 1024 * 10 return leaves[0].hex() +def generate_merkle_proof( + file_path: Union[str, Path], + chunk_index: int, + chunk_size: int = MERKLE_CHUNK_SIZE_BYTES +): + """ + Generate Merkle proof for a specific chunk index. + + Returns: + List of tuples (sibling_hash_hex, is_left) + """ + path = Path(file_path) + leaves = [] + + # Build leaf level + with path.open("rb") as f: + while chunk := f.read(chunk_size): + leaf_hex = compute_sha256(chunk) + leaves.append(bytes.fromhex(leaf_hex)) + + if not leaves: + raise ValueError("Cannot generate proof for empty file") + + if chunk_index < 0 or chunk_index >= len(leaves): + raise IndexError("Chunk index out of range") + + proof = [] + index = chunk_index + + while len(leaves) > 1: + # If odd number of nodes, duplicate last + if len(leaves) % 2 == 1: + leaves.append(leaves[-1]) + + sibling_index = index ^ 1 + sibling = leaves[sibling_index] + + is_left = sibling_index < index + proof.append((sibling.hex(), is_left)) + + # Build next level + next_level = [] + for i in range(0, len(leaves), 2): + combined = leaves[i] + leaves[i + 1] + parent_hex = compute_sha256(combined) + next_level.append(bytes.fromhex(parent_hex)) + + index //= 2 + leaves = next_level + + return proof + +def verify_merkle_proof( + chunk_bytes: bytes, + proof, + merkle_root: str +) -> bool: + """ + Verify a Merkle proof for given chunk bytes. + """ + current_hash = bytes.fromhex(compute_sha256(chunk_bytes)) + + for sibling_hex, is_left in proof: + sibling = bytes.fromhex(sibling_hex) + + if is_left: + combined = sibling + current_hash + else: + combined = current_hash + sibling + + parent_hex = compute_sha256(combined) + current_hash = bytes.fromhex(parent_hex) + + return current_hash.hex() == merkle_root # extract clean wikipage from actual wikipage def extract_text_from_xml(input_path): diff --git a/tests/test_util.py b/tests/test_util.py index 843c90eb..933c7ca7 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -194,3 +194,19 @@ def test_merkle_root_empty_file(tmp_path): expected = hashlib.sha256(b"").hexdigest() assert root == expected + +# --------------- Merkle proof generation ------------------------------------ + +def test_merkle_proof_verification(tmp_path): + file = tmp_path / "data.txt" + content = b"hello world this is merkle proof test" + file.write_bytes(content) + + root = utils.compute_merkle_root(file, chunk_size=8) + proof = utils.generate_merkle_proof(file, chunk_index=1, chunk_size=8) + + with file.open("rb") as f: + f.seek(8) + chunk = f.read(8) + + assert utils.verify_merkle_proof(chunk, proof, root) \ No newline at end of file From 22bbd65daad0780dea8eb6d130923dc40ae67846 Mon Sep 17 00:00:00 2001 From: aniket866 Date: Thu, 26 Feb 2026 15:23:01 +0530 Subject: [PATCH 2/5] Code-rabbit-follow-up --- openverifiablellm/utils.py | 29 ++++++++++++++++++++++++----- tests/test_util.py | 14 ++++++++++++-- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/openverifiablellm/utils.py b/openverifiablellm/utils.py index 5180ae27..c5dfe044 100644 --- a/openverifiablellm/utils.py +++ b/openverifiablellm/utils.py @@ -13,7 +13,10 @@ MERKLE_CHUNK_SIZE_BYTES = 1024 * 1024 # 1MB # Merkle Tree Chunk-Level Hashing for Large Files -def compute_merkle_root(file_path: Union[str, Path], chunk_size: int = 1024 * 1024) -> str: +def compute_merkle_root(file_path: Union[str, Path], chunk_size: int = MERKLE_CHUNK_SIZE_BYTES) -> str: + if chunk_size <= 0: + raise ValueError("chunk_size must be a positive integer") + path = Path(file_path) leaves = [] @@ -52,6 +55,10 @@ def generate_merkle_proof( List of tuples (sibling_hash_hex, is_left) """ path = Path(file_path) + + if chunk_size <= 0: + raise ValueError("chunk_size must be a positive integer") + leaves = [] # Build leaf level @@ -100,10 +107,22 @@ def verify_merkle_proof( """ Verify a Merkle proof for given chunk bytes. """ - current_hash = bytes.fromhex(compute_sha256(chunk_bytes)) + try: + current_hash = bytes.fromhex(compute_sha256(chunk_bytes)) + expected_root = bytes.fromhex(merkle_root) + except (TypeError, ValueError): + return False + + for step in proof: + if not isinstance(step, (tuple, list)) or len(step) != 2: + return False + + sibling_hex, is_left = step - for sibling_hex, is_left in proof: - sibling = bytes.fromhex(sibling_hex) + try: + sibling = bytes.fromhex(sibling_hex) + except (TypeError, ValueError): + return False if is_left: combined = sibling + current_hash @@ -113,7 +132,7 @@ def verify_merkle_proof( parent_hex = compute_sha256(combined) current_hash = bytes.fromhex(parent_hex) - return current_hash.hex() == merkle_root + return current_hash == expected_root # extract clean wikipage from actual wikipage def extract_text_from_xml(input_path): diff --git a/tests/test_util.py b/tests/test_util.py index 933c7ca7..560e5fb6 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -208,5 +208,15 @@ def test_merkle_proof_verification(tmp_path): with file.open("rb") as f: f.seek(8) chunk = f.read(8) - - assert utils.verify_merkle_proof(chunk, proof, root) \ No newline at end of file + # Positive case + assert utils.verify_merkle_proof(chunk, proof, root) + + # Negative case: tampered chunk + tampered_chunk = bytearray(chunk) + tampered_chunk[0] ^= 1 + assert not utils.verify_merkle_proof(bytes(tampered_chunk), proof, root) + + # Negative case: tampered proof + bad_proof = proof.copy() + bad_proof[0] = ("00" * 32, proof[0][1]) + assert not utils.verify_merkle_proof(chunk, bad_proof, root) From 771c7a88dec0cbb74d9b60976281ecfc9f4ba2d4 Mon Sep 17 00:00:00 2001 From: Aniket Date: Fri, 27 Feb 2026 01:39:16 +0530 Subject: [PATCH 3/5] Changed file_path to data --- openverifiablellm/utils.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/openverifiablellm/utils.py b/openverifiablellm/utils.py index c5dfe044..e0e0ca74 100644 --- a/openverifiablellm/utils.py +++ b/openverifiablellm/utils.py @@ -217,7 +217,7 @@ def generate_manifest(raw_path, processed_path): logger.info("Manifest written to %s", manifest_path) # helpers:Update compute_sha256() to support bytes input directly. -def compute_sha256(file_path: Union[str, Path, bytes, bytearray]) -> str: +def compute_sha256(data: Union[str, Path, bytes, bytearray]) -> str: """ Compute SHA256 hash of a file OR raw bytes. This is used for both raw and processed files to ensure integrity. @@ -226,12 +226,12 @@ def compute_sha256(file_path: Union[str, Path, bytes, bytearray]) -> str: """ sha256 = hashlib.sha256() - # If bytes provided - if isinstance(file_path, (bytes, bytearray)): - sha256.update(file_path) + # If raw bytes provided + if isinstance(data, (bytes, bytearray)): + sha256.update(data) return sha256.hexdigest() - path = Path(file_path) + path = Path(data) with path.open("rb") as f: while chunk := f.read(8192): From 9c8039e6e33a3c9272969f58a39179e24c440e64 Mon Sep 17 00:00:00 2001 From: Aniket Date: Fri, 27 Feb 2026 14:07:19 +0530 Subject: [PATCH 4/5] Code rabbit follow-up --- openverifiablellm/utils.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/openverifiablellm/utils.py b/openverifiablellm/utils.py index e0e0ca74..f8e165f3 100644 --- a/openverifiablellm/utils.py +++ b/openverifiablellm/utils.py @@ -113,17 +113,27 @@ def verify_merkle_proof( except (TypeError, ValueError): return False + if not isinstance(proof, (list, tuple)): + return False + for step in proof: if not isinstance(step, (tuple, list)) or len(step) != 2: return False sibling_hex, is_left = step + if not isinstance(sibling_hex, str) or not isinstance(is_left, bool): + return False + try: sibling = bytes.fromhex(sibling_hex) except (TypeError, ValueError): return False + # Ensure correct hash length + if len(sibling) != hashlib.sha256().digest_size: + return False + if is_left: combined = sibling + current_hash else: From faa4c9857b1e8976e8f49f7a372cdb9691654d0b Mon Sep 17 00:00:00 2001 From: Aniket Date: Fri, 27 Feb 2026 15:57:14 +0530 Subject: [PATCH 5/5] Code --- openverifiablellm/utils.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/openverifiablellm/utils.py b/openverifiablellm/utils.py index ffe43868..3180b3fa 100644 --- a/openverifiablellm/utils.py +++ b/openverifiablellm/utils.py @@ -24,11 +24,11 @@ def compute_merkle_root(file_path: Union[str, Path], chunk_size: int = MERKLE_CH with path.open("rb") as f: while chunk := f.read(chunk_size): # reuse compute_sha256 - leaf_hex = compute_sha256(chunk) + leaf_hex = compute_sha256(data=chunk) leaves.append(bytes.fromhex(leaf_hex)) if not leaves: - return compute_sha256(b"") + return compute_sha256(data=b"") while len(leaves) > 1: next_level = [] @@ -37,7 +37,7 @@ def compute_merkle_root(file_path: Union[str, Path], chunk_size: int = MERKLE_CH right = leaves[i + 1] if i + 1 < len(leaves) else left combined = left + right - parent_hex = compute_sha256(combined) + parent_hex = compute_sha256(data=combined) next_level.append(bytes.fromhex(parent_hex)) leaves = next_level @@ -65,7 +65,7 @@ def generate_merkle_proof( # Build leaf level with path.open("rb") as f: while chunk := f.read(chunk_size): - leaf_hex = compute_sha256(chunk) + leaf_hex = compute_sha256(data=chunk) leaves.append(bytes.fromhex(leaf_hex)) if not leaves: @@ -92,7 +92,7 @@ def generate_merkle_proof( next_level = [] for i in range(0, len(leaves), 2): combined = leaves[i] + leaves[i + 1] - parent_hex = compute_sha256(combined) + parent_hex = compute_sha256(data=combined) next_level.append(bytes.fromhex(parent_hex)) index //= 2 @@ -109,7 +109,7 @@ def verify_merkle_proof( Verify a Merkle proof for given chunk bytes. """ try: - current_hash = bytes.fromhex(compute_sha256(chunk_bytes)) + current_hash = bytes.fromhex(compute_sha256(data=chunk_bytes)) expected_root = bytes.fromhex(merkle_root) except (TypeError, ValueError): return False @@ -140,7 +140,7 @@ def verify_merkle_proof( else: combined = current_hash + sibling - parent_hex = compute_sha256(combined) + parent_hex = compute_sha256(data=combined) current_hash = bytes.fromhex(parent_hex) return current_hash == expected_root @@ -206,8 +206,8 @@ def generate_manifest(raw_path, processed_path): manifest = { "wikipedia_dump": raw_path.name, "dump_date": extract_dump_date(raw_path.name), - "raw_sha256": compute_sha256(str(raw_path)), - "processed_sha256": compute_sha256(str(processed_path)), + "raw_sha256": compute_sha256(file_path=raw_path), + "processed_sha256": compute_sha256(file_path=processed_path), # ---------------- ADDED FIELDS ---------------- "raw_merkle_root": compute_merkle_root(raw_path, chunk_size=MERKLE_CHUNK_SIZE_BYTES),