Repository navigation
feat(simple): implement per-article membership proofs against chain.jsonl #298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
2a37b67
b21ef6e
e63cae7
b1ae102
eb0871c
320bb9c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| """Per-article membership proofs against OpenVerifiableLLM chain.jsonl. | ||
|
|
||
| Given a Wikipedia article (by title or page ID), proves which training segment | ||
| trained on it, or proves that an exclusion fork never saw it, tied to the | ||
| cryptographic chain commitments. | ||
|
|
||
| Usage: | ||
| python scratch/membership_proof.py --data PREP --run RUN --article "Albert Einstein" | ||
| python scratch/membership_proof.py --data PREP --run RUN --article 736 | ||
| """ | ||
|
|
||
| import argparse | ||
| import hashlib | ||
| import json | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def read_chain(run_dir): | ||
| path = Path(run_dir) / "chain.jsonl" | ||
| if not path.is_file(): | ||
| sys.exit(f"{path} not found") | ||
| chain = [] | ||
| for n, line in enumerate(path.read_text(encoding="utf-8").splitlines(), 1): | ||
| line = line.strip() | ||
| if not line: | ||
| continue | ||
| try: | ||
| chain.append(json.loads(line)) | ||
| except json.JSONDecodeError as err: | ||
| sys.exit( | ||
| f"{path}: line {n} is not valid JSON ({err.msg}); the file is cut off or damaged" | ||
| ) | ||
| if not chain: | ||
| sys.exit(f"{path} has no chain entries") | ||
| # Verify sha256 chain links | ||
| for i in range(1, len(chain)): | ||
| want = hashlib.sha256( | ||
| json.dumps(chain[i - 1], sort_keys=True).encode() | ||
| ).hexdigest() | ||
| if chain[i].get("previous") != want: | ||
| sys.exit(f"chain link broken at entry {i}") | ||
| return chain | ||
|
|
||
|
|
||
| def find_article(data_dir, query): | ||
| if not query or not query.strip(): | ||
| return None | ||
| query = query.strip() | ||
| path = Path(data_dir) / "wikipedia" / "documents.jsonl" | ||
| if not path.is_file(): | ||
| sys.exit(f"{path} not found") | ||
| is_id = query.isdigit() | ||
| query_lower = query.lower() | ||
| with path.open("rb") as f: | ||
| for line in f: | ||
| line = line.strip() | ||
| if not line: | ||
| continue | ||
| try: | ||
| d = json.loads(line) | ||
| except json.JSONDecodeError: | ||
| continue | ||
| # identity format: [page_id, title] | ||
| page_id, title = str(d["identity"][0]), str(d["identity"][1]) | ||
| if is_id and page_id == query: | ||
| return d | ||
| if title.lower() == query_lower: | ||
| return d | ||
| return None | ||
|
|
||
|
|
||
| def verify_data_commitment(data_dir, chain_entry_0): | ||
| """Check that stream.json matches genesis commitment in chain.jsonl entry 0.""" | ||
| stream_path = Path(data_dir) / "wikipedia" / "stream.json" | ||
| if not stream_path.is_file(): | ||
| return False, "stream.json not found" | ||
| stream_bytes = stream_path.read_bytes() | ||
| stream_hash = hashlib.sha256(stream_bytes).hexdigest() | ||
| expected = chain_entry_0.get("data", {}).get("wikipedia") | ||
| if expected and stream_hash != expected: | ||
| return ( | ||
| False, | ||
| f"stream.json hash {stream_hash} differs from chain genesis {expected}", | ||
| ) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| # Verify documents.jsonl matches the hash listed in stream.json | ||
| try: | ||
| stream_meta = json.loads(stream_bytes.decode("utf-8")) | ||
| doc_files = [ | ||
| f | ||
| for f in stream_meta.get("files", []) | ||
| if f.get("path") == "documents.jsonl" or f.get("name") == "documents.jsonl" | ||
| ] | ||
| if doc_files: | ||
| expected_doc_hash = doc_files[0]["sha256"] | ||
| actual_doc_hash = hashlib.sha256( | ||
| (Path(data_dir) / "wikipedia" / "documents.jsonl").read_bytes() | ||
| ).hexdigest() | ||
| if actual_doc_hash != expected_doc_hash: | ||
| return ( | ||
| False, | ||
| f"documents.jsonl hash {actual_doc_hash} differs from stream.json {expected_doc_hash}", | ||
| ) | ||
| except Exception as e: | ||
| return False, f"failed checking stream.json file table: {e}" | ||
|
Comment on lines
+67
to
+93
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| return True, stream_hash | ||
|
|
||
|
|
||
| def build_membership_proof(doc, chain, data_bound, data_hash): | ||
| target_start = doc["target_start"] | ||
| target_count = doc["targets"] | ||
| target_end = target_start + target_count | ||
| page_id, title = doc["identity"][0], doc["identity"][1] | ||
|
|
||
| # Find the Wikipedia phase boundaries | ||
| segments = [] | ||
| is_excluded = False | ||
| exclusion_detail = None | ||
|
|
||
| for k in range(len(chain) - 1): | ||
| c_k = chain[k].get("control", {}) | ||
| c_next = chain[k + 1].get("control", {}) | ||
|
|
||
| if c_k.get("phase") != "wikipedia": | ||
| continue | ||
|
|
||
| cur_start = c_k.get("cursor", 0) | ||
| cur_end = c_next.get("cursor", cur_start) | ||
|
|
||
| # Check if the fork explicitly excluded this article range | ||
| exclude_ranges = c_k.get("exclude", {}).get("wikipedia", []) | ||
| for ex_start, ex_len in exclude_ranges: | ||
| if ex_start == target_start and ex_len == target_count: | ||
| is_excluded = True | ||
| exclusion_detail = { | ||
| "at_boundary": k, | ||
| "excluded_range": [ex_start, ex_start + ex_len], | ||
| "fork_entry_state": chain[k].get("state"), | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| # Check interval overlap | ||
| overlap_start = max(target_start, cur_start) | ||
| overlap_end = min(target_end, cur_end) | ||
| if overlap_start < overlap_end: | ||
| segments.append( | ||
| { | ||
| "segment": k, | ||
| "boundary_start": k, | ||
| "boundary_end": k + 1, | ||
| "cursor_interval": [cur_start, cur_end], | ||
| "article_overlap_targets": [overlap_start, overlap_end], | ||
| "boundary_start_state": chain[k].get("state"), | ||
| "boundary_end_state": chain[k + 1].get("state"), | ||
| "verify_cmd": f"python simple/ovl.py verify --data PREP --run RUN --segment {k}", | ||
| } | ||
| ) | ||
|
|
||
| status = "excluded" if is_excluded else ("trained" if segments else "unreached") | ||
|
|
||
| proof = { | ||
| "article": { | ||
| "title": title, | ||
| "page_id": page_id, | ||
| "targets": { | ||
| "start": target_start, | ||
| "end": target_end, | ||
| "count": target_count, | ||
| }, | ||
| "tokens": doc.get("tokens"), | ||
| }, | ||
| "membership_status": status, | ||
| "data_provenance": { | ||
| "bound_to_chain": data_bound, | ||
| "stream_genesis_hash": data_hash, | ||
| }, | ||
| "segments": [s["segment"] for s in segments], | ||
| "segment_details": segments, | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| if is_excluded: | ||
| proof["exclusion_proof"] = exclusion_detail | ||
|
|
||
| return proof | ||
|
|
||
|
|
||
| def main(): | ||
| p = argparse.ArgumentParser( | ||
| description="Prove Wikipedia article membership in OpenVerifiableLLM training" | ||
| ) | ||
| p.add_argument( | ||
| "--data", | ||
| required=True, | ||
| help="Prepared data directory (containing wikipedia/documents.jsonl)", | ||
| ) | ||
| p.add_argument( | ||
| "--run", required=True, help="Run directory (containing chain.jsonl)" | ||
| ) | ||
| p.add_argument( | ||
| "--article", required=True, help="Article title or Wikipedia page_id" | ||
| ) | ||
| a = p.parse_args() | ||
|
|
||
| chain = read_chain(a.run) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Authenticate the chain head before reporting membership. If someone adds an article exclusion to the last entry,
|
||
| doc = find_article(a.data, a.article) | ||
| if not doc: | ||
| sys.exit( | ||
| f"Article '{a.article}' not found in {Path(a.data) / 'wikipedia' / 'documents.jsonl'}" | ||
| ) | ||
|
|
||
| bound, data_hash = verify_data_commitment(a.data, chain[0]) | ||
| proof = build_membership_proof(doc, chain, bound, data_hash) | ||
|
|
||
| print(json.dumps(proof, indent=2, ensure_ascii=False)) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Uh oh!
There was an error while loading. Please reload this page.