Skip to content

Commit ef059da

Browse files
authored
Merge pull request #11 from aniket866/export-proof
Extend the Merkle proof system by adding portable proof export and standalone
2 parents de31ae1 + 65cecf8 commit ef059da

2 files changed

Lines changed: 98 additions & 2 deletions

File tree

‎openverifiablellm/utils.py‎

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
import defusedxml.ElementTree as ET
44
from pathlib import Path
55
import sys
6-
from typing import Union
76
import hashlib
87
import logging
98
import json
109
import platform
11-
from typing import Union, Optional
10+
from typing import Union, Optional, Dict, Any, List, Tuple
1211

1312
logger = logging.getLogger(__name__)
1413
MERKLE_CHUNK_SIZE_BYTES = 1024 * 1024 # 1MB
@@ -227,6 +226,76 @@ def generate_manifest(raw_path, processed_path):
227226

228227
logger.info("Manifest written to %s", manifest_path)
229228

229+
def export_merkle_proof(
230+
proof: List[Tuple[str, bool]],
231+
chunk_index: int,
232+
chunk_size: int,
233+
output_path: Union[str, Path]
234+
) -> None:
235+
"""
236+
Export Merkle proof to a JSON file for portable verification.
237+
"""
238+
239+
if chunk_size <= 0:
240+
raise ValueError("chunk_size must be a positive integer")
241+
242+
if not isinstance(proof, list):
243+
raise ValueError("proof must be a list")
244+
245+
if chunk_index < 0:
246+
raise ValueError("chunk_index must be non-negative")
247+
248+
data = {
249+
"chunk_index": chunk_index,
250+
"chunk_size": chunk_size,
251+
"proof": proof,
252+
}
253+
254+
output_path = Path(output_path)
255+
with output_path.open("w", encoding="utf-8") as f:
256+
json.dump(data, f, indent=2)
257+
258+
def load_merkle_proof(
259+
proof_path: Union[str, Path]
260+
) -> Dict[str, Any]:
261+
"""
262+
Load Merkle proof from a JSON file.
263+
"""
264+
proof_path = Path(proof_path)
265+
266+
with proof_path.open("r", encoding="utf-8") as f:
267+
return json.load(f)
268+
269+
270+
# Content before line 270 remains unchanged
271+
# Entire function definition from lines 270-314 should be deleted
272+
def verify_merkle_proof_from_file(
273+
proof_file_path: Union[str, Path],
274+
chunk_data: bytes,
275+
expected_root: str
276+
) -> bool:
277+
proof_file_path = Path(proof_file_path)
278+
279+
if not proof_file_path.exists():
280+
raise FileNotFoundError(f"Proof file not found: {proof_file_path}")
281+
282+
with proof_file_path.open("r", encoding="utf-8") as f:
283+
data = json.load(f)
284+
285+
if not isinstance(data, dict):
286+
raise ValueError("Malformed proof file: expected JSON object")
287+
288+
required_keys = {"chunk_index", "chunk_size", "proof"}
289+
if not required_keys.issubset(data.keys()):
290+
raise ValueError("Malformed proof file: missing required keys")
291+
292+
proof = data["proof"]
293+
294+
if not isinstance(proof, list):
295+
raise ValueError("Malformed proof: proof must be a list")
296+
297+
return verify_merkle_proof(chunk_data, proof, expected_root)
298+
230299
# helpers:Update compute_sha256() to support bytes input directly.
231300
def compute_sha256(
232301
*,

‎tests/test_util.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,30 @@ def test_merkle_proof_verification(tmp_path):
220220
bad_proof = proof.copy()
221221
bad_proof[0] = ("00" * 32, proof[0][1])
222222
assert not utils.verify_merkle_proof(chunk, bad_proof, root)
223+
224+
def test_export_and_load_merkle_proof(tmp_path):
225+
file = tmp_path / "data.txt"
226+
content = b"portable proof verification example"
227+
file.write_bytes(content)
228+
229+
root = utils.compute_merkle_root(file, chunk_size=8)
230+
proof = utils.generate_merkle_proof(file, chunk_index=1, chunk_size=8)
231+
232+
proof_file = tmp_path / "proof.json"
233+
234+
utils.export_merkle_proof(
235+
proof,
236+
chunk_index=1,
237+
chunk_size=8,
238+
output_path=proof_file
239+
)
240+
241+
with file.open("rb") as f:
242+
f.seek(8)
243+
chunk = f.read(8)
244+
245+
assert utils.verify_merkle_proof_from_file(
246+
proof_file_path=proof_file,
247+
chunk_data=chunk,
248+
expected_root=root,
249+
)

0 commit comments

Comments
 (0)