|
Download docs/PROVENANCE.md from Snapkitty/forge-code: direct link, hf CLI and curl.
- Browser
- Download file 20.3 kB
-
https://huggingface.co/datasets/Snapkitty/forge-code/resolve/main/docs/PROVENANCE.md
- Command line
-
hf download hf://datasets/Snapkitty/forge-code/docs/PROVENANCE.md
-
curl -L -o PROVENANCE.md https://huggingface.co/datasets/Snapkitty/forge-code/resolve/main/docs/PROVENANCE.md
20.3 kB
| # FORGE CODE Provenance System | |
| **Cryptographic Chain of Custody for Elixir Artifacts** | |
| --- | |
| ## Overview | |
| The FORGE CODE Provenance System provides a complete, tamper-evident chain of custody for all artifacts in the compilation and analysis pipeline. Every transformation from source code to final results is cryptographically sealed, ensuring that: | |
| 1. **Integrity**: No artifact can be modified without detection | |
| 2. **Traceability**: Every artifact can be traced back to its source | |
| 3. **Auditability**: The complete history of transformations is preserved | |
| 4. **Reproducibility**: Same inputs produce same outputs with same provenance | |
| --- | |
| ## Provenance Chain | |
| ### Complete Pipeline with Hashes | |
| ``` | |
| ┌─────────────────────────────────────────────────────────────────┐ | |
| │ PROVENANCE CHAIN │ | |
| ├─────────────────────────────────────────────────────────────────┤ | |
| │ │ | |
| │ SOURCE CODE │ | |
| │ │ │ | |
| │ ├─ SHA-256: abc123... │ | |
| │ ├─ Timestamp: 2024-01-15T10:30:00Z │ | |
| │ ├─ Path: contestants/alice/hasher.ex │ | |
| │ └─ Artifact ID: alice_hasher_ex_v1 │ | |
| │ │ │ | |
| │ ▼ │ | |
| │ ┌─────────────────┐ │ | |
| │ │ ELIXIR AST │ │ | |
| │ │ SHA-256: def456..│ │ | |
| │ │ Timestamp: ... │ │ | |
| │ └────────┬────────┘ │ | |
| │ │ │ | |
| │ ▼ │ | |
| │ ┌─────────────────┐ │ | |
| │ │ QUOTED FORM │ │ | |
| │ │ SHA-256: ghi789..│ │ | |
| │ └────────┬────────┘ │ | |
| │ │ │ | |
| │ ▼ │ | |
| │ ┌─────────────────┐ │ | |
| │ │ ERLANG FORMS │ │ | |
| │ │ SHA-256: jkl012..│ │ | |
| │ └────────┬────────┘ │ | |
| │ │ │ | |
| │ ▼ │ | |
| │ ┌─────────────────┐ │ | |
| │ │ BEAM BINARY │ │ | |
| │ │ SHA-256: mno345..│ │ | |
| │ │ Path: _build/... │ │ | |
| │ └────────┬────────┘ │ | |
| │ │ │ | |
| │ ▼ │ | |
| │ ┌─────────────────┐ │ | |
| │ │ BINARY IR │ │ | |
| │ │ SHA-256: pqr678..│ │ | |
| │ └────────┬────────┘ │ | |
| │ │ │ | |
| │ ▼ │ | |
| │ ┌─────────────────┐ │ | |
| │ │ TOURNAMENT │ │ | |
| │ │ RESULT │ │ | |
| │ │ SHA-256: stu901..│ │ | |
| │ │ Seal: final_hash │ │ | |
| │ └─────────────────┘ │ | |
| │ │ | |
| └─────────────────────────────────────────────────────────────────┘ | |
| ``` | |
| --- | |
| ## Artifact Identification | |
| Every artifact in FORGE CODE receives a unique identifier: | |
| ```elixir | |
| %Forge.Provenance.ArtifactID{ | |
| id: "alice_hasher_ex_v1_abc123", | |
| type: :source, | |
| hash: "abc123...", | |
| timestamp: DateTime.utc_now(), | |
| path: "contestants/alice/hasher.ex", | |
| metadata: %{ | |
| contestant: "alice", | |
| module: "Alice.Hasher", | |
| compiler_version: "1.15.0", | |
| otp_version: "25.0" | |
| } | |
| } | |
| ``` | |
| ### ID Format | |
| ``` | |
| {contestant}_{module}_{type}_{version}_{hash_prefix} | |
| Examples: | |
| - alice_hasher_ex_v1_abc123 (Source) | |
| - alice_hasher_beam_v1_def456 (BEAM binary) | |
| - alice_hasher_ir_v1_ghi789 (Binary IR) | |
| ``` | |
| --- | |
| ## Hashing System | |
| ### SHA-256 Implementation | |
| FORGE CODE uses SHA-256 for all cryptographic hashes: | |
| ```elixir | |
| # Hash a string | |
| {:ok, hash} = Forge.Hash.sha256("hello world") | |
| # => {:ok, "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"} | |
| # Hash a file | |
| {:ok, hash} = Forge.Hash.file("path/to/file.ex") | |
| # Hash a binary | |
| {:ok, hash} = Forge.Hash.binary(<<1, 2, 3>>) | |
| ``` | |
| ### Hash Chain | |
| Each transformation stage produces a new hash based on: | |
| 1. Previous stage hash | |
| 2. Current stage artifact | |
| 3. Transformation metadata | |
| ```elixir | |
| # Stage 1: Source | |
| after_source = Forge.Provenance.start(source_hash, :source, path: path) | |
| # Stage 2: AST | |
| after_ast = Forge.Provenance.add_stage(after_source, ast_hash, :ast, | |
| compiler: Elixir.version()) | |
| # Stage 3: Forms | |
| after_forms = Forge.Provenance.add_stage(after_ast, forms_hash, :forms) | |
| # Continue through all stages... | |
| ``` | |
| ### Seal Verification | |
| The final tournament seal is computed from: | |
| - All stage hashes | |
| - All contestant hashes | |
| - Configuration hash | |
| - Timestamp | |
| ```elixir | |
| seal = Forge.Provenance.seal(tournament) | |
| # Verify | |
| {:ok, valid} = Forge.Provenance.verify_seal(tournament, seal) | |
| ``` | |
| --- | |
| ## Provenance Struct | |
| ### Complete Provenance Record | |
| ```elixir | |
| %Forge.Provenance{ | |
| artifact_id: "alice_hasher_tournament_v1", | |
| # Source | |
| source: %{ | |
| hash: "abc123...", | |
| path: "contestants/alice/hasher.ex", | |
| timestamp: DateTime, | |
| size: 1234 | |
| }, | |
| # AST | |
| ast: %{ | |
| hash: "def456...", | |
| nodes: 45, | |
| depth: 5, | |
| timestamp: DateTime | |
| }, | |
| # Quoted | |
| quoted: %{ | |
| hash: "ghi789...", | |
| size: 2345, | |
| timestamp: DateTime | |
| }, | |
| # Erlang Forms | |
| forms: %{ | |
| hash: "jkl012...", | |
| count: 12, | |
| timestamp: DateTime | |
| }, | |
| # BEAM | |
| beam: %{ | |
| hash: "mno345...", | |
| path: "_build/.../alice.hasher.beam", | |
| size: 8192, | |
| timestamp: DateTime, | |
| metadata: %{ | |
| module: "Alice.Hasher", | |
| exports: ["sha256", "sum_list", "factorial"], | |
| functions: 10, | |
| instructions: 256 | |
| } | |
| }, | |
| # Binary IR | |
| ir: %{ | |
| hash: "pqr678...", | |
| path: "_build/.../alice.hasher.ir.json", | |
| size: 4567, | |
| timestamp: DateTime | |
| }, | |
| # Tournament Result | |
| result: %{ | |
| hash: "stu901...", | |
| timestamp: DateTime, | |
| scores: %{...} | |
| }, | |
| # Seal | |
| seal: "final_hash_value", | |
| # Metadata | |
| metadata: %{ | |
| contestant: "alice", | |
| tournament: "integer_hashing_2024", | |
| compiler_version: "1.15.0", | |
| otp_version: "25.0", | |
| target_architecture: "x86_64", | |
| os: "Linux" | |
| }, | |
| # Chain | |
| chain: [ | |
| %{stage: :source, hash: "abc123...", timestamp: ...}, | |
| %{stage: :ast, hash: "def456...", timestamp: ...}, | |
| %{stage: :quoted, hash: "ghi789...", timestamp: ...}, | |
| %{stage: :forms, hash: "jkl012...", timestamp: ...}, | |
| %{stage: :beam, hash: "mno345...", timestamp: ...}, | |
| %{stage: :ir, hash: "pqr678...", timestamp: ...}, | |
| %{stage: :result, hash: "stu901...", timestamp: ...} | |
| ] | |
| } | |
| ``` | |
| --- | |
| ## Stage-by-Stage Tracking | |
| ### 1. Source Stage | |
| ```elixir | |
| %Forge.Provenance.Stage{ | |
| stage: :source, | |
| hash: "abc123...", | |
| path: "contestants/alice/hasher.ex", | |
| size: 1234, | |
| timestamp: DateTime.utc_now(), | |
| metadata: %{ | |
| language: :elixir, | |
| encoding: :utf8, | |
| line_count: 35, | |
| module_count: 1 | |
| } | |
| } | |
| ``` | |
| ### 2. AST Stage | |
| ```elixir | |
| %Forge.Provenance.Stage{ | |
| stage: :ast, | |
| hash: "def456...", | |
| previous_hash: "abc123...", | |
| size: 5678, | |
| timestamp: DateTime.utc_now(), | |
| metadata: %{ | |
| node_count: 45, | |
| max_depth: 5, | |
| function_count: 6, | |
| expression_count: 28 | |
| } | |
| } | |
| ``` | |
| ### 3. Quoted Stage | |
| ```elixir | |
| %Forge.Provenance.Stage{ | |
| stage: :quoted, | |
| hash: "ghi789...", | |
| previous_hash: "def456...", | |
| size: 2345, | |
| timestamp: DateTime.utc_now(), | |
| metadata: %{ | |
| term_size: 2345, | |
| free_vars: 3 | |
| } | |
| } | |
| ``` | |
| ### 4. Erlang Forms Stage | |
| ```elixir | |
| %Forge.Provenance.Stage{ | |
| stage: :forms, | |
| hash: "jkl012...", | |
| previous_hash: "ghi789...", | |
| size: 8901, | |
| timestamp: DateTime.utc_now(), | |
| metadata: %{ | |
| form_count: 12, | |
| attribute_count: 8, | |
| annotation_count: 15 | |
| } | |
| } | |
| ``` | |
| ### 5. BEAM Stage | |
| ```elixir | |
| %Forge.Provenance.Stage{ | |
| stage: :beam, | |
| hash: "mno345...", | |
| previous_hash: "jkl012...", | |
| path: "_build/dev/lib/alice.hasher/ebin/alice.hasher.beam", | |
| size: 8192, | |
| timestamp: DateTime.utc_now(), | |
| metadata: %{ | |
| module: "Alice.Hasher", | |
| version: "0.1.0", | |
| compiler_options: [...], | |
| exports: ["sha256/1", "sum_list/1", "factorial/1"], | |
| function_count: 10, | |
| instruction_count: 256, | |
| atom_count: 45, | |
| literal_count: 23 | |
| } | |
| } | |
| ``` | |
| ### 6. Binary IR Stage | |
| ```elixir | |
| %Forge.Provenance.Stage{ | |
| stage: :ir, | |
| hash: "pqr678...", | |
| previous_hash: "mno345...", | |
| path: "_build/ir/alice.hasher.ir.json", | |
| size: 4567, | |
| timestamp: DateTime.utc_now(), | |
| metadata: %{ | |
| ir_version: "1.0", | |
| section_count: 8, | |
| field_count: 45, | |
| normalized: true | |
| } | |
| } | |
| ``` | |
| ### 7. Tournament Result Stage | |
| ```elixir | |
| %Forge.Provenance.Stage{ | |
| stage: :result, | |
| hash: "stu901...", | |
| previous_hash: "pqr678...", | |
| path: "results/integer_hashing_2024/alice.json", | |
| size: 2345, | |
| timestamp: DateTime.utc_now(), | |
| metadata: %{ | |
| tournament_id: "integer_hashing_2024", | |
| contestant_id: "alice", | |
| total_score: 0.952, | |
| rank: 1, | |
| challenge_count: 3, | |
| correctness: 1.0 | |
| } | |
| } | |
| ``` | |
| --- | |
| ## Chain Verification | |
| ### Complete Chain Verification | |
| ```elixir | |
| # Verify entire chain | |
| {:ok, valid, issues} = Forge.Provenance.verify_chain(provenance) | |
| # Check specific stage | |
| {:ok, valid} = Forge.Provenance.verify_stage(provenance, :beam) | |
| ``` | |
| ### Integrity Checks | |
| ```elixir | |
| # Check if hash matches artifact | |
| def verify_artifact(provenance, :source) do | |
| {:ok, file_hash} = Forge.Hash.file(provenance.source.path) | |
| file_hash == provenance.source.hash | |
| end | |
| # Check chain continuity | |
| def verify_chain_continuity(provenance) do | |
| chain = provenance.chain | |
| # Each stage's previous_hash should match previous stage's hash | |
| Enum.zip(chain, tl(chain)) | |
| |> Enum.all?(fn {current, prev} -> | |
| current.previous_hash == prev.hash | |
| end) | |
| end | |
| ``` | |
| --- | |
| ## Manifest Files | |
| ### Tournament Manifest | |
| Every tournament generates a manifest file: | |
| ```json | |
| { | |
| "tournament_manifest_version": "1.0", | |
| "tournament_id": "integer_hashing_2024", | |
| "timestamp": "2024-01-15T10:30:00Z", | |
| "configuration": { | |
| "compiler_version": "1.15.0", | |
| "otp_version": "25.0", | |
| "timeout_ms": 5000, | |
| "memory_limit_kb": 8192, | |
| "iterations": 100 | |
| }, | |
| "provenance_chain": { | |
| "seal": "abc123...", | |
| "stages": [ | |
| { | |
| "stage": "source", | |
| "hashes": { | |
| "alice_hasher.ex": "abc123...", | |
| "bob_hasher.ex": "def456..." | |
| } | |
| }, | |
| { | |
| "stage": "beam", | |
| "hashes": { | |
| "alice_hasher.beam": "mno345...", | |
| "bob_hasher.beam": "pqr678..." | |
| } | |
| }, | |
| { | |
| "stage": "ir", | |
| "hashes": { | |
| "alice_hasher.ir.json": "stu901...", | |
| "bob_hasher.ir.json": "vwx234..." | |
| } | |
| }, | |
| { | |
| "stage": "result", | |
| "hashes": { | |
| "tournament_results.json": "yza567..." | |
| } | |
| } | |
| ] | |
| }, | |
| "contestants": [ | |
| { | |
| "id": "alice", | |
| "source_hash": "abc123...", | |
| "beam_hash": "mno345...", | |
| "ir_hash": "stu901...", | |
| "result_hash": "yza567..." | |
| } | |
| ], | |
| "verification": { | |
| "chain_integrity": true, | |
| "seal_valid": true, | |
| "warnings": [] | |
| } | |
| } | |
| ``` | |
| ### Artifact Manifest | |
| Each compiled artifact has its own manifest: | |
| ```json | |
| { | |
| "artifact_version": "1.0", | |
| "artifact_id": "alice_hasher_beam_v1_mno345", | |
| "type": "beam", | |
| "path": "_build/dev/lib/alice.hasher/ebin/alice.hasher.beam", | |
| "hash": "mno345...", | |
| "size": 8192, | |
| "timestamp": "2024-01-15T10:30:00Z", | |
| "compiler": { | |
| "elixir_version": "1.15.0", | |
| "otp_version": "25.0", | |
| "compiler_options": [] | |
| }, | |
| "metadata": { | |
| "module": "Alice.Hasher", | |
| "exports": ["sha256/1", "sum_list/1", "factorial/1"], | |
| "function_count": 10, | |
| "instruction_count": 256, | |
| "atom_count": 45, | |
| "literal_count": 23 | |
| }, | |
| "provenance": { | |
| "source_hash": "abc123...", | |
| "ast_hash": "def456...", | |
| "forms_hash": "ghi789...", | |
| "previous_hash": "jkl012..." | |
| }, | |
| "verification": { | |
| "hash_valid": true, | |
| "provenance_valid": true | |
| } | |
| } | |
| ``` | |
| --- | |
| ## Provenance API | |
| ### Creating Provenance | |
| ```elixir | |
| # Start a new provenance chain | |
| provenance = Forge.Provenance.start(source_hash, :source, | |
| path: "path/to/source.ex", | |
| metadata: %{contestant: "alice"}) | |
| # Add a stage | |
| provenance = Forge.Provenance.add_stage(provenance, ast_hash, :ast, | |
| metadata: %{node_count: 45}) | |
| # Add another stage | |
| provenance = Forge.Provenance.add_stage(provenance, forms_hash, :forms) | |
| # Continue... | |
| provenance = Forge.Provenance.add_stage(provenance, beam_hash, :beam, | |
| path: "path/to/beam.beam") | |
| ``` | |
| ### Querying Provenance | |
| ```elixir | |
| # Get hash for a stage | |
| beam_hash = Forge.Provenance.get_hash(provenance, :beam) | |
| # Get all hashes | |
| hashes = Forge.Provenance.get_all_hashes(provenance) | |
| # Get metadata for a stage | |
| metadata = Forge.Provenance.get_metadata(provenance, :beam) | |
| # Get the complete chain | |
| chain = Forge.Provenance.get_chain(provenance) | |
| # Get latest stage | |
| {:ok, latest} = Forge.Provenance.get_latest(provenance) | |
| ``` | |
| ### Sealing Provenance | |
| ```elixir | |
| # Create a seal | |
| seal = Forge.Provenance.seal(provenance) | |
| # Verify a seal | |
| {:ok, valid} = Forge.Provenance.verify_seal(provenance, seal) | |
| # Seal a tournament | |
| {:ok, tournament_seal} = Forge.Provenance.seal_tournament(tournament_results) | |
| ``` | |
| --- | |
| ## Hash Functions | |
| ### SHA-256 | |
| ```elixir | |
| # String | |
| Forge.Hash.sha256("hello") | |
| # => {:ok, "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"} | |
| # File | |
| Forge.Hash.file("path/to/file") | |
| # Binary | |
| Forge.Hash.binary(<<1, 2, 3>>) | |
| # Term (serialized) | |
| Forge.Hash.term({:ok, "data"}) | |
| ``` | |
| ### Hash Chaining | |
| ```elixir | |
| # Chain multiple hashes | |
| hash1 = Forge.Hash.sha256("data1") | |
| hash2 = Forge.Hash.sha256("data2") | |
| combined = Forge.Hash.chain(hash1, hash2) | |
| ``` | |
| ### Artifact Hash | |
| ```elixir | |
| # Hash an artifact with its metadata | |
| artifact_hash = Forge.Hash.artifact(source_hash, metadata) | |
| ``` | |
| --- | |
| ## Security Considerations | |
| ### Immutable Artifacts | |
| Once created, provenance records are: | |
| 1. **Append-only**: New stages can be added, but existing ones cannot be modified | |
| 2. **Hash-locked**: Each stage's hash depends on the previous stage's hash | |
| 3. **Timestamped**: Each stage has an immutable timestamp | |
| ### Tamper Detection | |
| Any modification to: | |
| - Source code | |
| - Compiler options | |
| - Build environment | |
| - Artifact content | |
| ...will result in a hash mismatch, breaking the provenance chain. | |
| ### Best Practices | |
| 1. **Commit before building**: Ensure source is committed to version control | |
| 2. **Verify chain**: Always verify provenance before accepting results | |
| 3. **Archive artifacts**: Store all artifacts with their provenance | |
| 4. **Use seals**: Seal tournaments to prevent tampering | |
| 5. **Audit regularly**: Periodically verify stored provenance | |
| --- | |
| ## Integration with Tournament System | |
| ### Automatic Provenance | |
| The tournament system automatically tracks provenance: | |
| ```elixir | |
| # When running a tournament | |
| {:ok, results, provenance} = Forge.Tournament.run(tournament_definition) | |
| # Each contestant has provenance | |
| contestant_provenance = Forge.Tournament.get_provenance(results, "alice") | |
| # Export provenance with results | |
| Forge.Tournament.export(results, provenance, format: :json) | |
| ``` | |
| ### Provenance in Reports | |
| All reports include provenance information: | |
| ```json | |
| { | |
| "contestant": "alice", | |
| "scores": {...}, | |
| "provenance": { | |
| "source_hash": "abc123...", | |
| "beam_hash": "def456...", | |
| "ir_hash": "ghi789...", | |
| "chain_seal": "final...", | |
| "verified": true | |
| } | |
| } | |
| ``` | |
| --- | |
| ## Use Cases | |
| ### 1. Audit Trail | |
| Track every transformation for compliance or debugging: | |
| ```elixir | |
| provenance = Forge.Provenance.load("results/provenance.json") | |
| # Show complete history | |
| Forge.Provenance.display_chain(provenance) | |
| # Verify at each stage | |
| Forge.Provenance.verify_all(provenance) | |
| ``` | |
| ### 2. Reproducibility | |
| Ensure same inputs produce same outputs: | |
| ```elixir | |
| # Run tournament | |
| {:ok, results1, prov1} = Forge.Tournament.run(tournament) | |
| # Run again | |
| {:ok, results2, prov2} = Forge.Tournament.run(tournament) | |
| # Compare | |
| Forge.Provenance.compare(prov1, prov2) | |
| # => {:identical, "All hashes match"} | |
| ``` | |
| ### 3. Artifact Integrity | |
| Verify that artifacts haven't been tampered with: | |
| ```elixir | |
| # Before accepting an artifact | |
| {:ok, valid} = Forge.Provenance.verify_artifact(provenance, :beam) | |
| # Check all artifacts | |
| {:ok, all_valid} = Forge.Provenance.verify_all_artifacts(provenance) | |
| ``` | |
| ### 4. Cross-Validation | |
| Compare provenance across different runs or systems: | |
| ```elixir | |
| # Compare provenance from two different machines | |
| {:ok, consistent} = Forge.Provenance.compare_cross(prov_a, prov_b) | |
| ``` | |
| --- | |
| ## Error Handling | |
| ### Missing Artifacts | |
| ```elixir | |
| # If artifact is missing | |
| {:error, :artifact_missing, "Source file not found"} | |
| ``` | |
| ### Hash Mismatch | |
| ```elixir | |
| # If hash doesn't match | |
| {:error, :hash_mismatch, expected: "abc...", actual: "def..."} | |
| ``` | |
| ### Chain Break | |
| ```elixir | |
| # If chain is broken | |
| {:error, :chain_break, stage: :beam, reason: "Previous hash mismatch"} | |
| ``` | |
| --- | |
| ## Traditional Chinese / 繁體中文 | |
| ### 追溯系統概述 | |
| FORGE CODE 追溯系統為編譯和分析管道中的所有工件提供完整的、防篡改的監管鏈。從原始碼到最終結果的每次轉換都通過密碼學封印,確保: | |
| 1. **完整性**: 沒有工件可以在未被檢測的情況下被修改 | |
| 2. **可追溯性**: 每個工件都可以追溯到其原始來源 | |
| 3. **可審計性**: 完整的轉換歷史被保存 | |
| 4. **可重現性**: 相同的輸入在相同的追溯下產生相同的輸出 | |
| ### 追溯鏈 | |
| ``` | |
| 原始碼 → AST → 引用表單 → Erlang 表單 → BEAM 二進位 → IR → 賽事結果 | |
| ↓ ↓ ↓ ↓ ↓ ↓ ↓ | |
| SHA-256 SHA-256 SHA-256 SHA-256 SHA-256 SHA-256 封印 | |
| ``` | |
| ### 完整文檔請參見英文版本 | |