northroot-proof-engine 0.3.2

Core proof computation and validation library for PoSH (Proof of Shape) and PoX (Proof of Execution)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//! Northroot Proof Engine CLI
//!
//! Command-line tool for demonstrating end-to-end workflows,
//! validating proofs, and benchmarking operations.

use clap::{Parser, Subcommand};
use colored::*;
use ed25519_dalek::SigningKey;
use northroot_proof_engine::{
    canonicalize_span_shape, compute_data_shape_hash, compute_identity_root,
    compute_method_shape_hash, compute_sha256, compute_span_commitment_hash,
    compute_span_shape_hash, compute_trace_hash, compute_trace_roots_hash, compute_trace_seq_root,
    compute_trace_set_root, normalize_timestamp, sign_span_commitment, validate_signature_format,
    verify_span_commitment_signature, ArtifactRef, MethodRef, MethodShapeManifest, RuntimePins,
    Signature, SpanCommitment, SpanShape, SpanStatus, Trace, TraceRoots,
};
use rand::rngs::OsRng;
use std::fs;
use std::time::Instant;

#[derive(Parser)]
#[command(name = "northroot-cli")]
#[command(about = "Northroot Proof Engine CLI - Demo and validation tool")]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Demonstrate end-to-end PoSH → PoX workflow
    Demo {
        #[command(subcommand)]
        workflow: DemoWorkflow,
    },
    /// Benchmark hash and canonicalization operations
    Bench {
        #[command(subcommand)]
        bench_type: BenchType,
    },
    /// Validate proof JSON files
    Validate {
        /// Path to JSON file to validate
        file: String,
    },
}

#[derive(Subcommand)]
enum DemoWorkflow {
    /// Full PoSH → PoX workflow demonstration
    Workflow,
}

#[derive(Subcommand)]
enum BenchType {
    /// Benchmark hash operations
    Hash,
}

fn main() {
    let cli = Cli::parse();

    match cli.command {
        Commands::Demo { workflow } => match workflow {
            DemoWorkflow::Workflow => demo_workflow(),
        },
        Commands::Bench { bench_type } => match bench_type {
            BenchType::Hash => bench_hash(),
        },
        Commands::Validate { file } => validate_file(&file),
    }
}

fn demo_workflow() {
    println!("{}", "=== Northroot Proof Engine Demo ===\n".bold().cyan());

    // Step 1: Create Method Shape (PoSH)
    println!("{}", "Step 1: Creating Method Shape (PoSH)".bold());
    let span_shape = SpanShape {
        tool_id: "io.read_csv".to_string(),
        tool_version: "1".to_string(),
        param_schema: serde_json::json!({
            "delimiter": {"type": "string", "default": ","},
            "header": {"type": "boolean", "default": true}
        }),
        checker_ref: Some("chk:csv-valid@1".to_string()),
    };

    let span_shape_hash =
        compute_span_shape_hash(&span_shape).expect("Failed to compute span shape hash");
    println!("  Span Shape Hash: {}", span_shape_hash.green());

    // Create method shape manifest
    let mut csprng = OsRng;
    let _publisher_key = SigningKey::generate(&mut csprng);
    let publisher_kid = "did:key:zPublisher".to_string();

    let method_shape_manifest = MethodShapeManifest {
        schema_version: "method.v1".to_string(),
        method_id: "com.example/data-processor".to_string(),
        version: "1.0.0".to_string(),
        method_shape_root: span_shape_hash.clone(),
        method_dag_hash: None,
        runtime: RuntimePins {
            image_digest: "sha256:deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
                .to_string(),
            method_fingerprint: "git:abc123".to_string(),
        },
        checker_refs: vec!["chk:csv-valid@1".to_string()],
        default_policy_id: Some("pol:standard-v1".to_string()),
        publisher_sig: Signature {
            alg: "ed25519".to_string(),
            kid: publisher_kid.clone(),
            sig: "dGVzdHNpZ25hdHVyZQ".to_string(), // Placeholder
        },
    };

    let method_shape_hash = compute_method_shape_hash(&method_shape_manifest)
        .expect("Failed to compute method shape hash");
    println!("  Method Shape Hash: {}", method_shape_hash.green());
    println!();

    // Step 2: Create Span Commitment (PoX)
    println!("{}", "Step 2: Creating Span Commitment (PoX)".bold());
    let commitment = SpanCommitment {
        schema_version: "proof.v2".to_string(),
        trace_id: "tr_01H1234567890".to_string(),
        span_id: "sp-0001".to_string(),
        method_ref: MethodRef {
            method_id: method_shape_manifest.method_id.clone(),
            version: method_shape_manifest.version.clone(),
            method_shape_root: method_shape_hash.clone(),
            image_digest: method_shape_manifest.runtime.image_digest.clone(),
            method_fingerprint: method_shape_manifest.runtime.method_fingerprint.clone(),
        },
        data_shape_hash: compute_data_shape_hash(&serde_json::json!({"type": "object"}), None)
            .expect("Failed to compute data shape hash"),
        span_shape_hash: span_shape_hash.clone(),
        inputs: vec![ArtifactRef {
            name: "input.csv".to_string(),
            content_hash: "sha256:1111111111111111111111111111111111111111111111111111111111111111"
                .to_string(),
            uri: None,
        }],
        outputs: vec![ArtifactRef {
            name: "output.json".to_string(),
            content_hash: "sha256:2222222222222222222222222222222222222222222222222222222222222222"
                .to_string(),
            uri: None,
        }],
        start_time: normalize_timestamp("2025-11-06T12:00:00.000Z")
            .expect("Failed to normalize timestamp"),
        end_time: normalize_timestamp("2025-11-06T12:00:00.042Z")
            .expect("Failed to normalize timestamp"),
        status: SpanStatus::OK,
        meta: None,
    };

    let commitment_hash =
        compute_span_commitment_hash(&commitment).expect("Failed to compute span commitment hash");
    println!("  Span Commitment Hash: {}", commitment_hash.green());
    println!();

    // Step 3: Sign Span Commitment
    println!("{}", "Step 3: Signing Span Commitment".bold());
    let executor_key = SigningKey::generate(&mut csprng);
    let executor_kid = "did:key:zExecutor".to_string();

    let signature = sign_span_commitment(&commitment, &executor_key, executor_kid.clone())
        .expect("Failed to sign span commitment");
    println!("  Signature Algorithm: {}", signature.alg.green());
    println!("  Signature KID: {}", signature.kid.green());
    println!("  Signature: {}", signature.sig[..20].to_string() + "...");
    println!();

    // Step 4: Verify Signature
    println!("{}", "Step 4: Verifying Signature".bold());
    let verifying_key = executor_key.verifying_key();
    let verified =
        verify_span_commitment_signature(&commitment, &signature, verifying_key.as_bytes())
            .expect("Failed to verify signature");

    if verified {
        println!("  {} Signature verification: PASSED", "✓".green());
    } else {
        println!("  {} Signature verification: FAILED", "✗".red());
    }
    println!();

    // Step 5: Create Trace with Multiple Spans
    println!("{}", "Step 5: Creating Trace with Multiple Spans".bold());
    let trace = Trace {
        schema_version: "trace.v2".to_string(),
        trace_id: commitment.trace_id.clone(),
        method_ref: commitment.method_ref.clone(),
        data_shape_hash: commitment.data_shape_hash.clone(),
        policy_id: "pol:standard-v1".to_string(),
        identity_route: "did:key:zTenant".to_string(),
        content_route: "s3://bucket/trace".to_string(),
        execution_route: None,
    };

    let trace_hash = compute_trace_hash(&trace).expect("Failed to compute trace hash");
    println!("  Trace Hash: {}", trace_hash.green());
    println!();

    // Step 6: Compute Trace Roots
    println!("{}", "Step 6: Computing Trace Roots".bold());
    let span_hashes = vec![
        commitment_hash.clone(),
        "sha256:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb".to_string(),
    ];

    let trace_set_root =
        compute_trace_set_root(&span_hashes).expect("Failed to compute trace set root");
    println!("  Trace Set Root: {}", trace_set_root.green());

    let trace_seq_root =
        compute_trace_seq_root(&span_hashes).expect("Failed to compute trace seq root");
    println!("  Trace Seq Root: {}", trace_seq_root.green());

    let identity_root = compute_identity_root(&trace.trace_id, &trace.identity_route, &span_hashes)
        .expect("Failed to compute identity root");
    println!("  Identity Root: {}", identity_root.green());
    println!();

    // Summary
    println!("{}", "=== Summary ===".bold().cyan());
    println!("✓ Method Shape (PoSH) created and hashed");
    println!("✓ Span Commitment (PoX) created and hashed");
    println!("✓ Signature generated and verified");
    println!("✓ Trace created with multiple spans");
    println!("✓ Trace roots computed");
    println!();
    println!("{}", "Demo completed successfully!".green().bold());
}

fn bench_hash() {
    println!("{}", "=== Hash Operations Benchmark ===\n".bold().cyan());

    const ITERATIONS: usize = 10000;
    let test_data = "x".repeat(1000); // 1KB test data

    // Benchmark SHA-256 computation
    println!("Benchmarking SHA-256 computation...");
    let start = Instant::now();
    for _ in 0..ITERATIONS {
        let _ = compute_sha256(&test_data);
    }
    let duration = start.elapsed();
    let ops_per_sec = ITERATIONS as f64 / duration.as_secs_f64();
    println!("  {} iterations in {:?}", ITERATIONS, duration);
    println!(
        "  Throughput: {}\n",
        format!("{:.2} ops/sec", ops_per_sec).green()
    );

    // Benchmark canonicalization
    println!("Benchmarking canonicalization...");
    let span_shape = SpanShape {
        tool_id: "io.read_csv".to_string(),
        tool_version: "1".to_string(),
        param_schema: serde_json::json!({"key": "value"}),
        checker_ref: None,
    };

    let start = Instant::now();
    for _ in 0..ITERATIONS {
        let _ = canonicalize_span_shape(&span_shape);
    }
    let duration = start.elapsed();
    let ops_per_sec = ITERATIONS as f64 / duration.as_secs_f64();
    println!("  {} iterations in {:?}", ITERATIONS, duration);
    println!(
        "  Throughput: {}\n",
        format!("{:.2} ops/sec", ops_per_sec).green()
    );

    // Benchmark span commitment hash
    println!("Benchmarking span commitment hash...");
    let commitment = SpanCommitment {
        schema_version: "proof.v2".to_string(),
        trace_id: "tr_01H1234567890".to_string(),
        span_id: "sp-0001".to_string(),
        method_ref: MethodRef {
            method_id: "com.test/method".to_string(),
            version: "1.0.0".to_string(),
            method_shape_root:
                "sha256:0000000000000000000000000000000000000000000000000000000000000000"
                    .to_string(),
            image_digest: "sha256:0000000000000000000000000000000000000000000000000000000000000000"
                .to_string(),
            method_fingerprint: "git:deadbeef".to_string(),
        },
        data_shape_hash: "sha256:1111111111111111111111111111111111111111111111111111111111111111"
            .to_string(),
        span_shape_hash: "sha256:2222222222222222222222222222222222222222222222222222222222222222"
            .to_string(),
        inputs: vec![],
        outputs: vec![],
        start_time: "2025-11-06T12:00:00.000Z".to_string(),
        end_time: "2025-11-06T12:00:00.042Z".to_string(),
        status: SpanStatus::OK,
        meta: None,
    };

    let start = Instant::now();
    for _ in 0..ITERATIONS {
        let _ = compute_span_commitment_hash(&commitment);
    }
    let duration = start.elapsed();
    let ops_per_sec = ITERATIONS as f64 / duration.as_secs_f64();
    println!("  {} iterations in {:?}", ITERATIONS, duration);
    println!(
        "  Throughput: {}\n",
        format!("{:.2} ops/sec", ops_per_sec).green()
    );

    println!("{}", "Benchmark completed!".green().bold());
}

fn validate_file(file_path: &str) {
    println!("{}", "=== Validating Proof File ===\n".bold().cyan());
    println!("File: {}\n", file_path);

    let content = match fs::read_to_string(file_path) {
        Ok(c) => c,
        Err(e) => {
            eprintln!("{} Failed to read file: {}", "✗".red(), e);
            std::process::exit(1);
        }
    };

    // Try to parse as various proof types
    let mut validated = false;

    // Try as MethodShapeManifest (PoSH)
    if let Ok(manifest) = serde_json::from_str::<MethodShapeManifest>(&content) {
        println!("{} Detected: Method Shape Manifest (PoSH)", "✓".green());
        println!("  Method ID: {}", manifest.method_id);
        println!("  Version: {}", manifest.version);

        match validate_signature_format(&manifest.publisher_sig) {
            Ok(_) => println!("  {} Signature format: Valid", "✓".green()),
            Err(e) => println!("  {} Signature format: Invalid - {}", "✗".red(), e),
        }

        match compute_method_shape_hash(&manifest) {
            Ok(hash) => println!("  Method Shape Hash: {}", hash),
            Err(e) => println!("  {} Hash computation failed: {}", "✗".red(), e),
        }

        validated = true;
    }

    // Try as SpanCommitment (PoX)
    if let Ok(commitment) = serde_json::from_str::<SpanCommitment>(&content) {
        println!("{} Detected: Span Commitment (PoX)", "✓".green());
        println!("  Trace ID: {}", commitment.trace_id);
        println!("  Span ID: {}", commitment.span_id);

        match compute_span_commitment_hash(&commitment) {
            Ok(hash) => println!("  Span Commitment Hash: {}", hash),
            Err(e) => println!("  {} Hash computation failed: {}", "✗".red(), e),
        }

        validated = true;
    }

    // Try as Trace (PoX)
    if let Ok(trace) = serde_json::from_str::<Trace>(&content) {
        println!("{} Detected: Trace (PoX)", "✓".green());
        println!("  Trace ID: {}", trace.trace_id);

        match compute_trace_hash(&trace) {
            Ok(hash) => println!("  Trace Hash: {}", hash),
            Err(e) => println!("  {} Hash computation failed: {}", "✗".red(), e),
        }

        validated = true;
    }

    // Try as TraceRoots (PoX)
    if let Ok(roots) = serde_json::from_str::<TraceRoots>(&content) {
        println!("{} Detected: Trace Roots (PoX)", "✓".green());
        println!("  Trace ID: {}", roots.trace_id);

        if let Some(ref sig) = roots.server_sig {
            match validate_signature_format(sig) {
                Ok(_) => println!("  {} Signature format: Valid", "✓".green()),
                Err(e) => println!("  {} Signature format: Invalid - {}", "✗".red(), e),
            }
        } else {
            println!("  {} No server signature present", "⚠".yellow());
        }

        match compute_trace_roots_hash(&roots) {
            Ok(hash) => println!("  Trace Roots Hash: {}", hash),
            Err(e) => println!("  {} Hash computation failed: {}", "✗".red(), e),
        }

        validated = true;
    }

    if !validated {
        eprintln!(
            "{} Could not identify proof type. File may be invalid JSON or unsupported type.",
            "✗".red()
        );
        std::process::exit(1);
    }

    println!();
    println!("{}", "Validation completed!".green().bold());
}