ash-core 2.3.6

DEPRECATED — Use `ashcore` instead. This crate is End of Life (EOL).
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
//! ASH Conformance Suite — Master Vector Validator
//!
//! This integration test reads `tests/conformance/vectors.json` and validates
//! every vector against the Rust core. It serves as both validation and
//! documentation of expected ASH behavior.
//!
//! CRITICAL: For canonicalization vectors, raw `input_json_text` is passed
//! directly to `ash_canonicalize_json()`. The input is NOT parsed into
//! serde_json::Value first — doing so would hide parser-dependent bugs.
//!
//! Run: `cargo test --test conformance_suite`

use ash_core::*;
use serde_json::Value;

/// Load the master vector file relative to the workspace root.
fn load_vectors() -> Value {
    let manifest_dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
    let vectors_path = manifest_dir
        .parent()
        .unwrap()
        .parent()
        .unwrap()
        .join("tests")
        .join("conformance")
        .join("vectors.json");

    let content = std::fs::read_to_string(&vectors_path)
        .unwrap_or_else(|e| panic!("Failed to read vectors.json at {}: {}", vectors_path.display(), e));
    serde_json::from_str(&content)
        .unwrap_or_else(|e| panic!("Failed to parse vectors.json: {}", e))
}

fn get_vectors(root: &Value, category: &str) -> Vec<Value> {
    root.get("vectors")
        .and_then(|v| v.get(category))
        .and_then(|v| v.as_array())
        .cloned()
        .unwrap_or_default()
}

// =========================================================================
// A. JSON Canonicalization
// =========================================================================

#[test]
fn test_json_canonicalization_vectors() {
    let root = load_vectors();
    let vectors = get_vectors(&root, "json_canonicalization");
    assert!(!vectors.is_empty(), "No json_canonicalization vectors found");

    let mut passed = 0;
    for v in &vectors {
        let id = v["id"].as_str().unwrap();
        let input = v["input_json_text"].as_str().unwrap();
        let expected = v["expected"].as_str().unwrap();

        // CRITICAL: Pass raw text directly — do NOT parse into Value first
        let result = ash_canonicalize_json(input)
            .unwrap_or_else(|e| panic!("[{}] canonicalize_json failed: {}", id, e));

        assert_eq!(
            result, expected,
            "[{}] JSON canonicalization mismatch.\n  Input:    {}\n  Expected: {}\n  Got:      {}",
            id, input, expected, result
        );
        passed += 1;
    }

    eprintln!("json_canonicalization: {}/{} passed", passed, vectors.len());
}

// =========================================================================
// B. Query String Canonicalization
// =========================================================================

#[test]
fn test_query_canonicalization_vectors() {
    let root = load_vectors();
    let vectors = get_vectors(&root, "query_canonicalization");
    assert!(!vectors.is_empty(), "No query_canonicalization vectors found");

    let mut passed = 0;
    for v in &vectors {
        let id = v["id"].as_str().unwrap();
        let input = v["input"].as_str().unwrap();
        let expected = v["expected"].as_str().unwrap();

        let result = ash_canonicalize_query(input)
            .unwrap_or_else(|e| panic!("[{}] canonicalize_query failed: {}", id, e));

        assert_eq!(
            result, expected,
            "[{}] Query canonicalization mismatch.\n  Input:    {}\n  Expected: {}\n  Got:      {}",
            id, input, expected, result
        );
        passed += 1;
    }

    eprintln!("query_canonicalization: {}/{} passed", passed, vectors.len());
}

// =========================================================================
// C. URL-Encoded Canonicalization
// =========================================================================

#[test]
fn test_urlencoded_canonicalization_vectors() {
    let root = load_vectors();
    let vectors = get_vectors(&root, "urlencoded_canonicalization");
    assert!(!vectors.is_empty(), "No urlencoded_canonicalization vectors found");

    let mut passed = 0;
    for v in &vectors {
        let id = v["id"].as_str().unwrap();
        let input = v["input"].as_str().unwrap();
        let expected = v["expected"].as_str().unwrap();

        let result = ash_canonicalize_urlencoded(input)
            .unwrap_or_else(|e| panic!("[{}] canonicalize_urlencoded failed: {}", id, e));

        assert_eq!(
            result, expected,
            "[{}] URL-encoded canonicalization mismatch.\n  Input:    {}\n  Expected: {}\n  Got:      {}",
            id, input, expected, result
        );
        passed += 1;
    }

    eprintln!("urlencoded_canonicalization: {}/{} passed", passed, vectors.len());
}

// =========================================================================
// D. Binding Normalization
// =========================================================================

#[test]
fn test_binding_normalization_vectors() {
    let root = load_vectors();
    let vectors = get_vectors(&root, "binding_normalization");
    assert!(!vectors.is_empty(), "No binding_normalization vectors found");

    let mut passed = 0;
    for v in &vectors {
        let id = v["id"].as_str().unwrap();
        let input = &v["input"];
        let method = input["method"].as_str().unwrap();
        let path = input["path"].as_str().unwrap();
        let query = input["query"].as_str().unwrap();

        if let Some(expected) = v.get("expected").and_then(|e| e.as_str()) {
            // Success case
            let result = ash_normalize_binding(method, path, query)
                .unwrap_or_else(|e| panic!("[{}] normalize_binding failed: {}", id, e));

            assert_eq!(
                result, expected,
                "[{}] Binding normalization mismatch.\n  Method:   {}\n  Path:     {}\n  Query:    {}\n  Expected: {}\n  Got:      {}",
                id, method, path, query, expected, result
            );
        } else if let Some(expected_error) = v.get("expected_error") {
            // Error case
            let result = ash_normalize_binding(method, path, query);
            assert!(result.is_err(), "[{}] Expected error but got success", id);

            let err = result.unwrap_err();
            let expected_code = expected_error["code"].as_str().unwrap();
            let expected_status = expected_error["http_status"].as_u64().unwrap() as u16;

            assert_eq!(
                err.code().as_str(), expected_code,
                "[{}] Error code mismatch: expected {}, got {}",
                id, expected_code, err.code().as_str()
            );
            assert_eq!(
                err.http_status(), expected_status,
                "[{}] HTTP status mismatch: expected {}, got {}",
                id, expected_status, err.http_status()
            );
        }

        passed += 1;
    }

    eprintln!("binding_normalization: {}/{} passed", passed, vectors.len());
}

// =========================================================================
// E. Body Hashing
// =========================================================================

#[test]
fn test_body_hashing_vectors() {
    let root = load_vectors();
    let vectors = get_vectors(&root, "body_hashing");
    assert!(!vectors.is_empty(), "No body_hashing vectors found");

    let mut passed = 0;
    for v in &vectors {
        let id = v["id"].as_str().unwrap();
        let input = v["input"].as_str().unwrap();
        let expected = v["expected"].as_str().unwrap();

        let result = ash_hash_body(input);

        assert_eq!(
            result, expected,
            "[{}] Body hash mismatch.\n  Input:    {:?}\n  Expected: {}\n  Got:      {}",
            id, input, expected, result
        );
        assert_eq!(result.len(), 64, "[{}] Hash must be 64 hex chars", id);
        passed += 1;
    }

    eprintln!("body_hashing: {}/{} passed", passed, vectors.len());
}

// =========================================================================
// F. Client Secret Derivation
// =========================================================================

#[test]
fn test_client_secret_derivation_vectors() {
    let root = load_vectors();
    let vectors = get_vectors(&root, "client_secret_derivation");
    assert!(!vectors.is_empty(), "No client_secret_derivation vectors found");

    let mut passed = 0;
    for v in &vectors {
        let id = v["id"].as_str().unwrap();
        let input = &v["input"];
        let nonce = input["nonce"].as_str().unwrap();
        let context_id = input["context_id"].as_str().unwrap();
        let binding = input["binding"].as_str().unwrap();
        let expected = v["expected"].as_str().unwrap();

        let result = ash_derive_client_secret(nonce, context_id, binding)
            .unwrap_or_else(|e| panic!("[{}] derive_client_secret failed: {}", id, e));

        assert_eq!(
            result, expected,
            "[{}] Client secret mismatch.\n  Nonce:      {}...\n  ContextId:  {}\n  Binding:    {}\n  Expected:   {}\n  Got:        {}",
            id, &nonce[..16], context_id, binding, expected, result
        );
        assert_eq!(result.len(), 64, "[{}] Secret must be 64 hex chars", id);
        passed += 1;
    }

    eprintln!("client_secret_derivation: {}/{} passed", passed, vectors.len());
}

// =========================================================================
// G. Proof Generation & Verification
// =========================================================================

#[test]
fn test_proof_generation_vectors() {
    let root = load_vectors();
    let vectors = get_vectors(&root, "proof_generation");
    assert!(!vectors.is_empty(), "No proof_generation vectors found");

    let mut passed = 0;
    for v in &vectors {
        let id = v["id"].as_str().unwrap();
        let expected = &v["expected"];

        if let Some(expected_obj) = expected.as_object() {
            let input = &v["input"];

            // Proof generation vectors with full expected outputs
            if let Some(expected_proof) = expected_obj.get("proof").and_then(|p| p.as_str()) {
                let nonce = input["nonce"].as_str().unwrap();
                let context_id = input["context_id"].as_str().unwrap();
                let binding = input["binding"].as_str().unwrap();
                let timestamp = input["timestamp"].as_str().unwrap();
                let payload = input["payload"].as_str().unwrap();

                // Derive secret
                let client_secret = ash_derive_client_secret(nonce, context_id, binding).unwrap();

                if let Some(expected_secret) = expected_obj.get("client_secret").and_then(|s| s.as_str()) {
                    assert_eq!(
                        client_secret, expected_secret,
                        "[{}] Client secret mismatch", id
                    );
                }

                // Canonicalize and hash
                let canonical = ash_canonicalize_json(payload).unwrap();
                if let Some(expected_canon) = expected_obj.get("canonical_payload").and_then(|c| c.as_str()) {
                    assert_eq!(canonical, expected_canon, "[{}] Canonical payload mismatch", id);
                }

                let body_hash = ash_hash_body(&canonical);
                if let Some(expected_hash) = expected_obj.get("body_hash").and_then(|h| h.as_str()) {
                    assert_eq!(body_hash, expected_hash, "[{}] Body hash mismatch", id);
                }

                // Build proof
                let proof = ash_build_proof(&client_secret, timestamp, binding, &body_hash).unwrap();
                assert_eq!(
                    proof, expected_proof,
                    "[{}] Proof mismatch.\n  Expected: {}\n  Got:      {}",
                    id, expected_proof, proof
                );
                assert_eq!(proof.len(), 64, "[{}] Proof must be 64 hex chars", id);
            }

            // Verification vectors
            if let Some(expected_valid) = expected_obj.get("valid").and_then(|v| v.as_bool()) {
                let nonce = input["nonce"].as_str().unwrap();
                let context_id = input["context_id"].as_str().unwrap();
                let binding = input["binding"].as_str().unwrap();
                let timestamp = input["timestamp"].as_str().unwrap();
                let body_hash = input["body_hash"].as_str().unwrap();
                let proof = input["proof"].as_str().unwrap();

                let result = ash_verify_proof(nonce, context_id, binding, timestamp, body_hash, proof).unwrap();
                assert_eq!(
                    result, expected_valid,
                    "[{}] Verification result mismatch: expected {}, got {}",
                    id, expected_valid, result
                );
            }

            // Format validation
            if let Some(expected_len) = expected_obj.get("proof_length").and_then(|l| l.as_u64()) {
                let nonce = input["nonce"].as_str().unwrap();
                let context_id = input["context_id"].as_str().unwrap();
                let binding = input["binding"].as_str().unwrap();
                let timestamp = input["timestamp"].as_str().unwrap();
                let payload = input["payload"].as_str().unwrap();

                let secret = ash_derive_client_secret(nonce, context_id, binding).unwrap();
                let canonical = ash_canonicalize_json(payload).unwrap();
                let hash = ash_hash_body(&canonical);
                let proof = ash_build_proof(&secret, timestamp, binding, &hash).unwrap();

                assert_eq!(proof.len() as u64, expected_len, "[{}] Proof length mismatch", id);
                assert!(
                    proof.chars().all(|c| c.is_ascii_hexdigit() && !c.is_ascii_uppercase()),
                    "[{}] Proof must be lowercase hex", id
                );
            }
        }

        passed += 1;
    }

    eprintln!("proof_generation: {}/{} passed", passed, vectors.len());
}

// =========================================================================
// H. Scoped Field Extraction
// =========================================================================

#[test]
fn test_scoped_field_vectors() {
    let root = load_vectors();
    let vectors = get_vectors(&root, "scoped_field_extraction");
    assert!(!vectors.is_empty(), "No scoped_field_extraction vectors found");

    let mut passed = 0;
    for v in &vectors {
        let id = v["id"].as_str().unwrap();

        // Error case
        if let Some(expected_error) = v.get("expected_error") {
            let input = &v["input"];
            let payload: Value = serde_json::from_str(input["payload"].as_str().unwrap()).unwrap();
            let scope_arr = input["scope"].as_array().unwrap();
            let scope: Vec<&str> = scope_arr.iter().map(|s| s.as_str().unwrap()).collect();
            let strict = input.get("strict").and_then(|s| s.as_bool()).unwrap_or(false);

            let result = ash_extract_scoped_fields_strict(&payload, &scope, strict);
            assert!(result.is_err(), "[{}] Expected error but got success", id);

            let err = result.unwrap_err();
            let expected_code = expected_error["code"].as_str().unwrap();
            let expected_status = expected_error["http_status"].as_u64().unwrap() as u16;
            assert_eq!(err.code().as_str(), expected_code, "[{}] Error code mismatch", id);
            assert_eq!(err.http_status(), expected_status, "[{}] HTTP status mismatch", id);
            passed += 1;
            continue;
        }

        let expected = &v["expected"];
        let input = &v["input"];

        // Scope hash only
        if let Some(expected_hash) = expected.get("scope_hash") {
            if !expected.as_object().unwrap().contains_key("proof") && !expected.as_object().unwrap().contains_key("extracted_fields") {
                let scope_arr = input["scope"].as_array().unwrap();
                let scope: Vec<&str> = scope_arr.iter().map(|s| s.as_str().unwrap()).collect();
                let hash = ash_hash_scope(&scope).unwrap();
                assert_eq!(hash, expected_hash.as_str().unwrap(), "[{}] Scope hash mismatch", id);
                passed += 1;
                continue;
            }
        }

        // Extraction vectors
        if let Some(expected_fields) = expected.get("extracted_fields") {
            let payload_str = input["payload"].as_str().unwrap();
            let payload: Value = serde_json::from_str(payload_str).unwrap();
            let scope_arr = input["scope"].as_array().unwrap();
            let scope: Vec<&str> = scope_arr.iter().map(|s| s.as_str().unwrap()).collect();

            let scoped = ash_extract_scoped_fields(&payload, &scope).unwrap();
            assert_eq!(
                scoped, *expected_fields,
                "[{}] Extracted fields mismatch", id
            );

            // If there's also a proof expected, verify it
            if let Some(expected_proof) = expected.get("proof").and_then(|p| p.as_str()) {
                let nonce = input["nonce"].as_str().unwrap();
                let context_id = input["context_id"].as_str().unwrap();
                let binding = input["binding"].as_str().unwrap();
                let timestamp = input["timestamp"].as_str().unwrap();

                let secret = ash_derive_client_secret(nonce, context_id, binding).unwrap();
                let (proof, scope_hash) = ash_build_proof_scoped(
                    &secret, timestamp, binding, payload_str, &scope
                ).unwrap();

                assert_eq!(proof, expected_proof, "[{}] Scoped proof mismatch", id);

                if let Some(expected_sh) = expected.get("scope_hash").and_then(|s| s.as_str()) {
                    assert_eq!(scope_hash, expected_sh, "[{}] Scope hash mismatch", id);
                }
            }
        }

        passed += 1;
    }

    eprintln!("scoped_field_extraction: {}/{} passed", passed, vectors.len());
}

// =========================================================================
// I. Unified Proof
// =========================================================================

#[test]
fn test_unified_proof_vectors() {
    let root = load_vectors();
    let vectors = get_vectors(&root, "unified_proof");
    assert!(!vectors.is_empty(), "No unified_proof vectors found");

    let mut passed = 0;
    for v in &vectors {
        let id = v["id"].as_str().unwrap();
        let expected = &v["expected"];
        let input = &v["input"];

        // Chain hash only vector
        if let (Some(prev_proof), Some(expected_chain)) = (
            input.get("previous_proof").and_then(|p| p.as_str()),
            expected.get("chain_hash").and_then(|c| c.as_str()),
        ) {
            if !expected.as_object().unwrap().contains_key("proof") {
                let chain_hash = ash_hash_proof(prev_proof).unwrap();
                assert_eq!(chain_hash, expected_chain, "[{}] Chain hash mismatch", id);
                passed += 1;
                continue;
            }
        }

        // Verification vector
        if let Some(expected_valid) = expected.get("valid").and_then(|v| v.as_bool()) {
            let nonce = input["nonce"].as_str().unwrap();
            let context_id = input["context_id"].as_str().unwrap();
            let binding = input["binding"].as_str().unwrap();
            let timestamp = input["timestamp"].as_str().unwrap();
            let payload = input["payload"].as_str().unwrap();
            let proof = input["proof"].as_str().unwrap();
            let scope_hash = input["scope_hash"].as_str().unwrap();
            let chain_hash = input["chain_hash"].as_str().unwrap();

            let scope_arr = input["scope"].as_array().unwrap();
            let scope: Vec<&str> = scope_arr.iter().map(|s| s.as_str().unwrap()).collect();

            let previous_proof = input.get("previous_proof").and_then(|p| p.as_str());

            let result = ash_verify_proof_unified(
                nonce, context_id, binding, timestamp, payload,
                proof, &scope, scope_hash, previous_proof, chain_hash,
            ).unwrap();

            assert_eq!(result, expected_valid, "[{}] Unified verify mismatch", id);
            passed += 1;
            continue;
        }

        // Build proof vector
        if let Some(expected_proof) = expected.get("proof").and_then(|p| p.as_str()) {
            let nonce = input["nonce"].as_str().unwrap();
            let context_id = input["context_id"].as_str().unwrap();
            let binding = input["binding"].as_str().unwrap();
            let timestamp = input["timestamp"].as_str().unwrap();
            let payload = input["payload"].as_str().unwrap();

            let scope_arr = input["scope"].as_array().unwrap();
            let scope: Vec<&str> = scope_arr.iter().map(|s| s.as_str().unwrap()).collect();

            let previous_proof = input.get("previous_proof").and_then(|p| p.as_str());

            let secret = ash_derive_client_secret(nonce, context_id, binding).unwrap();
            let result = ash_build_proof_unified(
                &secret, timestamp, binding, payload, &scope, previous_proof,
            ).unwrap();

            assert_eq!(result.proof, expected_proof, "[{}] Unified proof mismatch", id);

            if let Some(expected_sh) = expected.get("scope_hash").and_then(|s| s.as_str()) {
                assert_eq!(result.scope_hash, expected_sh, "[{}] Scope hash mismatch", id);
            }
            if let Some(expected_ch) = expected.get("chain_hash").and_then(|c| c.as_str()) {
                assert_eq!(result.chain_hash, expected_ch, "[{}] Chain hash mismatch", id);
            }
        }

        passed += 1;
    }

    eprintln!("unified_proof: {}/{} passed", passed, vectors.len());
}

// =========================================================================
// J. Timing-Safe Comparison
// =========================================================================

#[test]
fn test_timing_safe_comparison_vectors() {
    let root = load_vectors();
    let vectors = get_vectors(&root, "timing_safe_comparison");
    assert!(!vectors.is_empty(), "No timing_safe_comparison vectors found");

    let mut passed = 0;
    for v in &vectors {
        let id = v["id"].as_str().unwrap();
        let input = &v["input"];
        let a = input["a"].as_str().unwrap();
        let b = input["b"].as_str().unwrap();
        let expected = v["expected"].as_bool().unwrap();

        let result = ash_timing_safe_compare(a, b);
        assert_eq!(
            result, expected,
            "[{}] Timing-safe comparison mismatch: {:?} vs {:?} => expected {}, got {}",
            id, a, b, expected, result
        );
        passed += 1;
    }

    eprintln!("timing_safe_comparison: {}/{} passed", passed, vectors.len());
}

// =========================================================================
// K. Error Behavior
// =========================================================================

#[test]
fn test_error_behavior_vectors() {
    let root = load_vectors();
    let vectors = get_vectors(&root, "error_behavior");
    assert!(!vectors.is_empty(), "No error_behavior vectors found");

    let mut passed = 0;
    for v in &vectors {
        let id = v["id"].as_str().unwrap();
        let expected_error = &v["expected_error"];
        let expected_code = expected_error["code"].as_str().unwrap();
        let expected_status = expected_error["http_status"].as_u64().unwrap() as u16;
        let input = &v["input"];
        let operation = input["operation"].as_str().unwrap_or("");

        // Re-execute the operation and verify the error
        let result: Result<String, AshError> = match operation {
            "derive_client_secret" => {
                let nonce = input["nonce"].as_str().unwrap_or("");
                let ctx = input["context_id"].as_str().unwrap_or("");
                let binding = input["binding"].as_str().unwrap_or("");
                ash_derive_client_secret(nonce, ctx, binding)
            }
            "canonicalize_json" => {
                if let Some(text) = input.get("input_json_text").and_then(|t| t.as_str()) {
                    ash_canonicalize_json(text)
                } else {
                    // Skip vectors that describe but don't include actual input (like oversized)
                    passed += 1;
                    continue;
                }
            }
            "verify_proof" => {
                let timestamp = input.get("timestamp").and_then(|t| t.as_str()).unwrap_or("");
                // Use dummy values for fields not being tested
                let dummy_hash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
                let dummy_proof = "0000000000000000000000000000000000000000000000000000000000000000";
                let nonce = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
                let ctx = "ctx_test_conformance_v1";
                let binding = "POST|/api/transfer|";

                ash_verify_proof(nonce, ctx, binding, timestamp, dummy_hash, dummy_proof)
                    .map(|v| v.to_string())
            }
            "extract_scoped_fields_strict" => {
                let payload_str = input["payload"].as_str().unwrap();
                let payload: Value = serde_json::from_str(payload_str).unwrap();
                let scope_arr = input["scope"].as_array().unwrap();
                let scope: Vec<&str> = scope_arr.iter().map(|s| s.as_str().unwrap()).collect();

                ash_extract_scoped_fields_strict(&payload, &scope, true)
                    .map(|v| serde_json::to_string(&v).unwrap())
            }
            "build_proof" => {
                let body_hash = input["body_hash"].as_str().unwrap_or("");
                ash_build_proof("secret", "1700000000", "POST|/api/transfer|", body_hash)
            }
            "hash_proof" => {
                let proof = input["proof"].as_str().unwrap_or("");
                ash_hash_proof(proof)
            }
            _ => {
                // Skip unknown operations or vectors without direct re-execution
                passed += 1;
                continue;
            }
        };

        assert!(result.is_err(), "[{}] Expected error from {} but got success", id, operation);
        let err = result.unwrap_err();

        assert_eq!(
            err.code().as_str(), expected_code,
            "[{}] Error code mismatch: expected {}, got {}",
            id, expected_code, err.code().as_str()
        );
        assert_eq!(
            err.http_status(), expected_status,
            "[{}] HTTP status mismatch: expected {}, got {}",
            id, expected_status, err.http_status()
        );

        passed += 1;
    }

    eprintln!("error_behavior: {}/{} passed", passed, vectors.len());
}

// =========================================================================
// L. Timestamp Validation
// =========================================================================

#[test]
fn test_timestamp_validation_vectors() {
    let root = load_vectors();
    let vectors = get_vectors(&root, "timestamp_validation");
    assert!(!vectors.is_empty(), "No timestamp_validation vectors found");

    let mut passed = 0;
    for v in &vectors {
        let id = v["id"].as_str().unwrap();

        if let Some(expected_error) = v.get("expected_error") {
            let timestamp = v["input"]["timestamp"].as_str().unwrap();
            let expected_code = expected_error["code"].as_str().unwrap();
            let expected_status = expected_error["http_status"].as_u64().unwrap() as u16;

            // Use verify_proof to test timestamp validation (it calls validate_timestamp_format)
            let dummy_hash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
            let dummy_proof = "0000000000000000000000000000000000000000000000000000000000000000";
            let result = ash_verify_proof(
                "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
                "ctx_test", "POST|/api|",
                timestamp, dummy_hash, dummy_proof,
            );

            assert!(result.is_err(), "[{}] Expected timestamp error but got success", id);
            let err = result.unwrap_err();
            assert_eq!(err.code().as_str(), expected_code, "[{}] Error code mismatch", id);
            assert_eq!(err.http_status(), expected_status, "[{}] HTTP status mismatch", id);
        } else if let Some(expected) = v.get("expected") {
            if let Some(true) = expected.get("valid_format").and_then(|v| v.as_bool()) {
                // Valid format — verify_proof should NOT fail on timestamp format
                // (it may fail on proof mismatch, but that's OK — we're testing format)
                let timestamp = v["input"]["timestamp"].as_str().unwrap();
                let dummy_hash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
                let dummy_proof = "0000000000000000000000000000000000000000000000000000000000000000";
                let result = ash_verify_proof(
                    "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
                    "ctx_test", "POST|/api|",
                    timestamp, dummy_hash, dummy_proof,
                );
                // Should succeed (returning false for bad proof) or NOT be a timestamp error
                match result {
                    Ok(_) => {} // Good - proof doesn't match but format is valid
                    Err(e) => {
                        assert_ne!(
                            e.code().as_str(), "ASH_TIMESTAMP_INVALID",
                            "[{}] Valid timestamp rejected as invalid", id
                        );
                    }
                }
            }
        }

        passed += 1;
    }

    eprintln!("timestamp_validation: {}/{} passed", passed, vectors.len());
}

// =========================================================================
// Meta: Vector File Integrity
// =========================================================================

#[test]
fn test_vector_file_metadata() {
    let root = load_vectors();

    // Verify schema version
    assert_eq!(root["schema_version"].as_u64(), Some(1));

    // Verify ash version
    assert_eq!(root["ash_version"].as_str(), Some("2.3.5"));

    // Verify all expected categories exist
    let vectors = root.get("vectors").expect("Missing 'vectors' key");
    let expected_categories = [
        "json_canonicalization",
        "query_canonicalization",
        "urlencoded_canonicalization",
        "binding_normalization",
        "body_hashing",
        "client_secret_derivation",
        "proof_generation",
        "scoped_field_extraction",
        "unified_proof",
        "timing_safe_comparison",
        "error_behavior",
        "timestamp_validation",
    ];

    for cat in &expected_categories {
        let arr = vectors.get(cat)
            .unwrap_or_else(|| panic!("Missing category: {}", cat))
            .as_array()
            .unwrap_or_else(|| panic!("Category {} is not an array", cat));
        assert!(!arr.is_empty(), "Category {} has no vectors", cat);
    }

    // Count total vectors
    let total: usize = expected_categories.iter()
        .map(|cat| vectors[cat].as_array().unwrap().len())
        .sum();
    eprintln!("Total vectors in file: {}", total);
    assert!(total >= 120, "Expected at least 120 vectors, got {}", total);

    // Verify no placeholders exist
    let content = serde_json::to_string(&root).unwrap();
    assert!(!content.contains("a1b2c3d4"), "Found placeholder value 'a1b2c3d4'");
    assert!(!content.contains("TODO"), "Found TODO in vectors");
    assert!(!content.contains("PLACEHOLDER"), "Found PLACEHOLDER in vectors");
}