examples/prove_inclusion.py
| 1 | """Prove a specific document was in the training set without revealing the others. |
| 2 | |
| 3 | Run: python examples/prove_inclusion.py |
| 4 | """ |
| 5 | |
| 6 | from quantumshield import AgentIdentity |
| 7 | |
| 8 | from pqc_training_data import ( |
| 9 | CommitmentBuilder, |
| 10 | CommitmentSigner, |
| 11 | CommitmentVerifier, |
| 12 | DataRecord, |
| 13 | ) |
| 14 | |
| 15 | |
| 16 | def main() -> None: |
| 17 | identity = AgentIdentity.create("model-creator") |
| 18 | signer = CommitmentSigner(identity) |
| 19 | |
| 20 | # Creator has 100 private documents. We simulate that. |
| 21 | corpus = [ |
| 22 | DataRecord(content=f"private-document-{i}".encode(), metadata={"id": i}) |
| 23 | for i in range(100) |
| 24 | ] |
| 25 | builder = CommitmentBuilder("private-corpus", "1.0.0") |
| 26 | builder.add_records(corpus) |
| 27 | commitment = signer.sign(builder.build()) |
| 28 | print(f"[OK] Committed to {commitment.record_count} records") |
| 29 | print(f" root: {commitment.root}") |
| 30 | |
| 31 | # --- Auditor asks: "Was document #42 in the training set?" --- |
| 32 | # Creator produces an inclusion proof WITHOUT revealing the other 99 records. |
| 33 | proof = builder.tree.inclusion_proof(index=42) |
| 34 | print("\n[PROOF] Generated inclusion proof for record #42") |
| 35 | print(f" siblings: {len(proof.siblings)} (tree depth)") |
| 36 | print(f" size: {proof.tree_size} leaves") |
| 37 | |
| 38 | # --- Auditor verifies with ONLY: record, proof, commitment --- |
| 39 | claimed = corpus[42] # the auditor has only this record |
| 40 | result = CommitmentVerifier.verify(claimed, proof, commitment) |
| 41 | print(f"\n[VERIFY] result: fully_verified={result.fully_verified}") |
| 42 | print(f" signature_valid={result.signature_valid}") |
| 43 | print(f" proof_valid={result.proof_valid}") |
| 44 | print(f" leaf_matches={result.leaf_matches_record}") |
| 45 | |
| 46 | |
| 47 | if __name__ == "__main__": |
| 48 | main() |
| 49 | |