From b970aed687d25dddeffb5019cf754400473c3708 Mon Sep 17 00:00:00 2001 From: aniket866 Date: Wed, 25 Feb 2026 16:46:34 +0530 Subject: [PATCH 01/10] adding merkel-root-tree-based-verification --- .gitignore | 2 ++ openverifiablellm/utils.py | 54 ++++++++++++++++++++++++++++++++++++++ tests/test_merkle.py | 53 +++++++++++++++++++++++++++++++++++++ tests/test_util.py | 21 ++++++++++++++- 4 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 tests/test_merkle.py diff --git a/.gitignore b/.gitignore index 392cb810..c55e4608 100644 --- a/.gitignore +++ b/.gitignore @@ -332,3 +332,5 @@ __pycache__/ *.pyo *.pyd *.bz2 + +*.venv \ No newline at end of file diff --git a/openverifiablellm/utils.py b/openverifiablellm/utils.py index 084e1e25..783581c5 100644 --- a/openverifiablellm/utils.py +++ b/openverifiablellm/utils.py @@ -11,6 +11,53 @@ logger = logging.getLogger(__name__) +# Merkle Tree Chunk-Level Hashing for Large Files +def compute_merkle_root(file_path: Union[str, Path], chunk_size: int = 1024 * 1024) -> str: + """ + Compute the Merkle Root hash of a file by splitting it into chunks. + + This allows researchers to cryptographically verify specific chunks + or subsets of the training data without re-hashing the entire dataset. + + Parameters + ---------- + file_path : Union[str, Path] + Path to the dataset file. + chunk_size : int + Size of each chunk in bytes (default: 1MB). + + Returns + ------- + str + The final Merkle Root hash string. + """ + path = Path(file_path) + leaves = [] + + # 1. Read file in chunks and hash each chunk (raw bytes) + with path.open("rb") as f: + while chunk := f.read(chunk_size): + leaves.append(hashlib.sha256(chunk).digest()) + + # Handle empty files deterministically + if not leaves: + return hashlib.sha256(b"").hexdigest() + + # 2. Build the tree bottom-up + while len(leaves) > 1: + next_level = [] + for i in range(0, len(leaves), 2): + left = leaves[i] + right = leaves[i + 1] if i + 1 < len(leaves) else left + + combined = left + right + next_level.append(hashlib.sha256(combined).digest()) + + leaves = next_level + + return leaves[0].hex() + + # extract clean wikipage from actual wikipage def extract_text_from_xml(input_path): """ @@ -74,6 +121,13 @@ def generate_manifest(raw_path, processed_path): "dump_date": extract_dump_date(raw_path.name), "raw_sha256": compute_sha256(str(raw_path)), "processed_sha256": compute_sha256(str(processed_path)), + + # ---------------- ADDED FIELDS ---------------- + "raw_merkle_root": compute_merkle_root(raw_path), + "processed_merkle_root": compute_merkle_root(processed_path), + "chunk_size_bytes": 1024 * 1024, + # --------------------------------------------------------------- + "preprocessing_version": "v1", "python_version": platform.python_version() } diff --git a/tests/test_merkle.py b/tests/test_merkle.py new file mode 100644 index 00000000..d165953f --- /dev/null +++ b/tests/test_merkle.py @@ -0,0 +1,53 @@ +import hashlib +import pytest +from pathlib import Path +from openverifiablellm import utils + + +# Merkle Root Tests + +def test_merkle_root_deterministic(tmp_path): + file = tmp_path / "data.txt" + file.write_text("hello wikipedia") + + root1 = utils.compute_merkle_root(file, chunk_size=4) + root2 = utils.compute_merkle_root(file, chunk_size=4) + + assert root1 == root2 + + +def test_merkle_root_changes_when_content_changes(tmp_path): + file = tmp_path / "data.txt" + file.write_text("content A") + + root1 = utils.compute_merkle_root(file) + + file.write_text("content B") + + root2 = utils.compute_merkle_root(file) + + assert root1 != root2 + + +def test_merkle_root_single_chunk_equals_sha256(tmp_path): + file = tmp_path / "data.txt" + content = "small file" + file.write_text(content) + + # Large chunk size ensures single leaf + merkle_root = utils.compute_merkle_root(file, chunk_size=10_000) + + expected = hashlib.sha256(content.encode()).hexdigest() + + assert merkle_root == expected + + +def test_merkle_root_empty_file(tmp_path): + file = tmp_path / "empty.txt" + file.write_text("") + + root = utils.compute_merkle_root(file) + + expected = hashlib.sha256(b"").hexdigest() + + assert root == expected \ No newline at end of file diff --git a/tests/test_util.py b/tests/test_util.py index c0cede34..1c18929c 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -126,4 +126,23 @@ def test_extract_text_from_xml_end_to_end(tmp_path, monkeypatch): assert processed_file.exists() assert "Hello World" in processed_file.read_text() - \ No newline at end of file + + # --------------- manifest includes merkle fields ------------------------------------ + +def test_manifest_contains_merkle_fields(tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + + raw_file = tmp_path / "raw.txt" + raw_file.write_text("dummy data") + + processed_file = tmp_path / "processed.txt" + processed_file.write_text("cleaned data") + + utils.generate_manifest(raw_file, processed_file) + + manifest_file = tmp_path / "data/dataset_manifest.json" + manifest = manifest_file.read_text() + + assert "raw_merkle_root" in manifest + assert "processed_merkle_root" in manifest + assert "chunk_size_bytes" in manifest \ No newline at end of file From ce7895813bedbede50e00a40ccf0d9c78ab5800e Mon Sep 17 00:00:00 2001 From: Aniket Date: Wed, 25 Feb 2026 17:05:32 +0530 Subject: [PATCH 02/10] Code rabbit follow up Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- openverifiablellm/utils.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/openverifiablellm/utils.py b/openverifiablellm/utils.py index 783581c5..d6111032 100644 --- a/openverifiablellm/utils.py +++ b/openverifiablellm/utils.py @@ -31,6 +31,9 @@ def compute_merkle_root(file_path: Union[str, Path], chunk_size: int = 1024 * 10 str The final Merkle Root hash string. """ + if chunk_size <= 0: + raise ValueError("chunk_size must be a positive integer") + path = Path(file_path) leaves = [] From 872e9a07092cc85c023941c898ee04d5ef3fffa0 Mon Sep 17 00:00:00 2001 From: Aniket Date: Wed, 25 Feb 2026 17:05:50 +0530 Subject: [PATCH 03/10] Code rabbit follow up Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- openverifiablellm/utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/openverifiablellm/utils.py b/openverifiablellm/utils.py index d6111032..614fd1dd 100644 --- a/openverifiablellm/utils.py +++ b/openverifiablellm/utils.py @@ -126,9 +126,9 @@ def generate_manifest(raw_path, processed_path): "processed_sha256": compute_sha256(str(processed_path)), # ---------------- ADDED FIELDS ---------------- - "raw_merkle_root": compute_merkle_root(raw_path), - "processed_merkle_root": compute_merkle_root(processed_path), - "chunk_size_bytes": 1024 * 1024, + "raw_merkle_root": compute_merkle_root(raw_path, chunk_size=MERKLE_CHUNK_SIZE_BYTES), + "processed_merkle_root": compute_merkle_root(processed_path, chunk_size=MERKLE_CHUNK_SIZE_BYTES), + "chunk_size_bytes": MERKLE_CHUNK_SIZE_BYTES, # --------------------------------------------------------------- "preprocessing_version": "v1", From e58ef1dc2e0decc43bc79f490f76596059ac2f56 Mon Sep 17 00:00:00 2001 From: Aniket Date: Wed, 25 Feb 2026 17:06:11 +0530 Subject: [PATCH 04/10] Code rabbit follow up Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- tests/test_util.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/test_util.py b/tests/test_util.py index 1c18929c..0846b136 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -140,9 +140,13 @@ def test_manifest_contains_merkle_fields(tmp_path, monkeypatch): utils.generate_manifest(raw_file, processed_file) +import json +@@ manifest_file = tmp_path / "data/dataset_manifest.json" - manifest = manifest_file.read_text() + manifest = json.loads(manifest_file.read_text()) assert "raw_merkle_root" in manifest assert "processed_merkle_root" in manifest - assert "chunk_size_bytes" in manifest \ No newline at end of file + assert manifest["chunk_size_bytes"] == 1024 * 1024 + assert len(manifest["raw_merkle_root"]) == 64 + assert len(manifest["processed_merkle_root"]) == 64 \ No newline at end of file From 593692a79493327e0fd81ee4af6e591cec24df67 Mon Sep 17 00:00:00 2001 From: Aniket Date: Wed, 25 Feb 2026 19:13:05 +0530 Subject: [PATCH 05/10] Code rabbit follow-up --- tests/test_util.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/test_util.py b/tests/test_util.py index 0846b136..0edcee5b 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -2,6 +2,7 @@ import hashlib import pytest from openverifiablellm import utils +import json """ Unit and integration tests for OpenVerifiableLLM preprocessing pipeline. @@ -140,8 +141,6 @@ def test_manifest_contains_merkle_fields(tmp_path, monkeypatch): utils.generate_manifest(raw_file, processed_file) -import json -@@ manifest_file = tmp_path / "data/dataset_manifest.json" manifest = json.loads(manifest_file.read_text()) @@ -149,4 +148,4 @@ def test_manifest_contains_merkle_fields(tmp_path, monkeypatch): assert "processed_merkle_root" in manifest assert manifest["chunk_size_bytes"] == 1024 * 1024 assert len(manifest["raw_merkle_root"]) == 64 - assert len(manifest["processed_merkle_root"]) == 64 \ No newline at end of file + assert len(manifest["processed_merkle_root"]) == 64 From 297f227c4017b566b7161035e457954427cbcbe0 Mon Sep 17 00:00:00 2001 From: aniket866 Date: Thu, 26 Feb 2026 12:45:00 +0530 Subject: [PATCH 06/10] Modified-merkel-root-to-use-existing-SHA --- openverifiablellm/utils.py | 56 ++++++++++---------------------------- tests/test_merkle.py | 53 ------------------------------------ tests/test_util.py | 49 ++++++++++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 95 deletions(-) delete mode 100644 tests/test_merkle.py diff --git a/openverifiablellm/utils.py b/openverifiablellm/utils.py index 783581c5..bbd3d693 100644 --- a/openverifiablellm/utils.py +++ b/openverifiablellm/utils.py @@ -13,37 +13,18 @@ # Merkle Tree Chunk-Level Hashing for Large Files def compute_merkle_root(file_path: Union[str, Path], chunk_size: int = 1024 * 1024) -> str: - """ - Compute the Merkle Root hash of a file by splitting it into chunks. - - This allows researchers to cryptographically verify specific chunks - or subsets of the training data without re-hashing the entire dataset. - - Parameters - ---------- - file_path : Union[str, Path] - Path to the dataset file. - chunk_size : int - Size of each chunk in bytes (default: 1MB). - - Returns - ------- - str - The final Merkle Root hash string. - """ path = Path(file_path) leaves = [] - # 1. Read file in chunks and hash each chunk (raw bytes) with path.open("rb") as f: while chunk := f.read(chunk_size): - leaves.append(hashlib.sha256(chunk).digest()) + # STRICTLY reuse compute_sha256 + leaf_hex = compute_sha256(chunk) + leaves.append(bytes.fromhex(leaf_hex)) - # Handle empty files deterministically if not leaves: - return hashlib.sha256(b"").hexdigest() + return compute_sha256(b"") - # 2. Build the tree bottom-up while len(leaves) > 1: next_level = [] for i in range(0, len(leaves), 2): @@ -51,7 +32,8 @@ def compute_merkle_root(file_path: Union[str, Path], chunk_size: int = 1024 * 10 right = leaves[i + 1] if i + 1 < len(leaves) else left combined = left + right - next_level.append(hashlib.sha256(combined).digest()) + parent_hex = compute_sha256(combined) + next_level.append(bytes.fromhex(parent_hex)) leaves = next_level @@ -140,28 +122,20 @@ def generate_manifest(raw_path, processed_path): logger.info("Manifest written to %s", manifest_path) -# helpers -def compute_sha256(file_path: Union[str, Path]) -> str: +# helpers:Update compute_sha256() to support bytes input directly. +def compute_sha256(file_path: Union[str, Path, bytes]) -> str: """ - Compute SHA256 hash of a file. - - This provides a deterministic fingerprint of the dataset, - enabling reproducibility and verification. + Compute SHA256 hash of a file OR raw bytes. + """ + sha256 = hashlib.sha256() - Parameters - ---------- - file_path : Union[str, Path] - Path to the dataset file (string or Path-like). + # If bytes provided + if isinstance(file_path, (bytes, bytearray)): + sha256.update(file_path) + return sha256.hexdigest() - Returns - ------- - str - SHA256 hash string. - """ path = Path(file_path) - sha256 = hashlib.sha256() - with path.open("rb") as f: while chunk := f.read(8192): sha256.update(chunk) diff --git a/tests/test_merkle.py b/tests/test_merkle.py deleted file mode 100644 index d165953f..00000000 --- a/tests/test_merkle.py +++ /dev/null @@ -1,53 +0,0 @@ -import hashlib -import pytest -from pathlib import Path -from openverifiablellm import utils - - -# Merkle Root Tests - -def test_merkle_root_deterministic(tmp_path): - file = tmp_path / "data.txt" - file.write_text("hello wikipedia") - - root1 = utils.compute_merkle_root(file, chunk_size=4) - root2 = utils.compute_merkle_root(file, chunk_size=4) - - assert root1 == root2 - - -def test_merkle_root_changes_when_content_changes(tmp_path): - file = tmp_path / "data.txt" - file.write_text("content A") - - root1 = utils.compute_merkle_root(file) - - file.write_text("content B") - - root2 = utils.compute_merkle_root(file) - - assert root1 != root2 - - -def test_merkle_root_single_chunk_equals_sha256(tmp_path): - file = tmp_path / "data.txt" - content = "small file" - file.write_text(content) - - # Large chunk size ensures single leaf - merkle_root = utils.compute_merkle_root(file, chunk_size=10_000) - - expected = hashlib.sha256(content.encode()).hexdigest() - - assert merkle_root == expected - - -def test_merkle_root_empty_file(tmp_path): - file = tmp_path / "empty.txt" - file.write_text("") - - root = utils.compute_merkle_root(file) - - expected = hashlib.sha256(b"").hexdigest() - - assert root == expected \ No newline at end of file diff --git a/tests/test_util.py b/tests/test_util.py index 1c18929c..ebda463a 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -145,4 +145,51 @@ def test_manifest_contains_merkle_fields(tmp_path, monkeypatch): assert "raw_merkle_root" in manifest assert "processed_merkle_root" in manifest - assert "chunk_size_bytes" in manifest \ No newline at end of file + assert "chunk_size_bytes" in manifest + +# --------------- compute_merkle_root ------------------------------------ + +def test_merkle_root_deterministic(tmp_path): + file = tmp_path / "data.txt" + file.write_text("hello wikipedia") + + root1 = utils.compute_merkle_root(file, chunk_size=4) + root2 = utils.compute_merkle_root(file, chunk_size=4) + + assert root1 == root2 + + +def test_merkle_root_changes_when_content_changes(tmp_path): + file = tmp_path / "data.txt" + file.write_text("content A") + + root1 = utils.compute_merkle_root(file) + + file.write_text("content B") + + root2 = utils.compute_merkle_root(file) + + assert root1 != root2 + + +def test_merkle_root_single_chunk_equals_sha256(tmp_path): + file = tmp_path / "data.txt" + content = "small file" + file.write_text(content) + + merkle_root = utils.compute_merkle_root(file, chunk_size=10_000) + + expected = hashlib.sha256(content.encode()).hexdigest() + + assert merkle_root == expected + + +def test_merkle_root_empty_file(tmp_path): + file = tmp_path / "empty.txt" + file.write_text("") + + root = utils.compute_merkle_root(file) + + expected = hashlib.sha256(b"").hexdigest() + + assert root == expected \ No newline at end of file From dbef17af04643a9b3d363af7f4dbd1226123c833 Mon Sep 17 00:00:00 2001 From: aniket866 Date: Thu, 26 Feb 2026 13:07:39 +0530 Subject: [PATCH 07/10] fixing-missing-doc-string --- openverifiablellm/utils.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/openverifiablellm/utils.py b/openverifiablellm/utils.py index 88d6e258..90567d8d 100644 --- a/openverifiablellm/utils.py +++ b/openverifiablellm/utils.py @@ -126,6 +126,9 @@ def generate_manifest(raw_path, processed_path): def compute_sha256(file_path: Union[str, Path, bytes]) -> str: """ Compute SHA256 hash of a file OR raw bytes. + This is used for both raw and processed files to ensure integrity. + This provides a deterministic fingerprint of the dataset, + enabling reproducibility and verification. """ sha256 = hashlib.sha256() From 28c4df311db4922a695cf7cd8beb070713f848d0 Mon Sep 17 00:00:00 2001 From: aniket866 Date: Thu, 26 Feb 2026 13:24:56 +0530 Subject: [PATCH 08/10] Fix type annotation and finalize Merkle integration --- openverifiablellm/utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/openverifiablellm/utils.py b/openverifiablellm/utils.py index 90567d8d..0416b1c8 100644 --- a/openverifiablellm/utils.py +++ b/openverifiablellm/utils.py @@ -10,6 +10,7 @@ import platform logger = logging.getLogger(__name__) +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: @@ -123,7 +124,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]) -> str: +def compute_sha256(file_path: 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. From bfdf4f56d515503f2b6cfda1fc723c5d1b7ccd0e Mon Sep 17 00:00:00 2001 From: Aniket Date: Fri, 27 Feb 2026 01:35:33 +0530 Subject: [PATCH 09/10] 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 0416b1c8..bdb0187c 100644 --- a/openverifiablellm/utils.py +++ b/openverifiablellm/utils.py @@ -124,7 +124,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. @@ -133,12 +133,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 e18f18d6ab6578d286fb8540b3628b330fa73fda Mon Sep 17 00:00:00 2001 From: Aniket Date: Fri, 27 Feb 2026 15:29:09 +0530 Subject: [PATCH 10/10] changed file_path to optional byte_array and file_path --- openverifiablellm/utils.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/openverifiablellm/utils.py b/openverifiablellm/utils.py index bdb0187c..06cd11e7 100644 --- a/openverifiablellm/utils.py +++ b/openverifiablellm/utils.py @@ -8,6 +8,7 @@ import logging import json import platform +from typing import Union, Optional logger = logging.getLogger(__name__) MERKLE_CHUNK_SIZE_BYTES = 1024 * 1024 # 1MB @@ -124,22 +125,33 @@ 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(data: Union[str, Path, bytes, bytearray]) -> str: +def compute_sha256( + *, + data: Optional[Union[bytes, bytearray]] = None, + file_path: Optional[Union[str, Path]] = None, +) -> str: """ Compute SHA256 hash of a file OR raw bytes. - This is used for both raw and processed files to ensure integrity. + + This is used for both raw and processed files to ensure integrity. This provides a deterministic fingerprint of the dataset, enabling reproducibility and verification. + + Exactly one of `data` or `file_path` must be provided. """ + + if (data is None) == (file_path is None): + raise ValueError( + "Exactly one of 'data' or 'file_path' must be provided." + ) + sha256 = hashlib.sha256() - # If raw bytes provided - if isinstance(data, (bytes, bytearray)): + if data is not None: sha256.update(data) return sha256.hexdigest() - path = Path(data) - + path = Path(file_path) with path.open("rb") as f: while chunk := f.read(8192): sha256.update(chunk)