vcp-core 4.2.0

Value Context Protocol (VCP) core types and parsing
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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
//! Bundle transport: content hashing, canonicalization, and signature verification.
//!
//! Implements the same canonicalization rules as the Python SDK:
//!
//! **Content canonicalization:**
//! 1. Unicode NFC normalization
//! 2. Line ending normalization (CRLF/CR -> LF)
//! 3. Strip trailing whitespace from each line
//! 4. Remove trailing empty lines, ensure single trailing newline
//! 5. Reject control characters (except `\n`, `\t`)
//! 6. UTF-8 encode without BOM
//!
//! **Manifest canonicalization (RFC 8785 JCS):**
//! - Sort object keys lexicographically
//! - No whitespace between tokens
//! - UTF-8 encoding

use base64::engine::general_purpose::STANDARD as BASE64;
use base64::Engine as _;
use ed25519_dalek::{Signer, SigningKey, Verifier, VerifyingKey};
use sha2::{Digest, Sha256};
use unicode_normalization::UnicodeNormalization;

use serde::{Deserialize, Serialize};

use crate::error::{VcpError, VcpResult, VerificationCode};

/// Parse protocol JSON while rejecting duplicate object keys.
///
/// Duplicate keys are ambiguous across decoders and unsafe in signed data.
///
/// # Errors
///
/// Returns [`VcpError::JsonError`] for malformed JSON, duplicate object keys,
/// non-finite numbers, or trailing data.
pub fn parse_json_strict(input: &str) -> VcpResult<serde_json::Value> {
    crate::strict_json::from_str(input).map_err(VcpError::from)
}

// ── Content canonicalization ────────────────────────────────

/// Unicode codepoints that are forbidden in constitution content.
const FORBIDDEN_CODEPOINTS: &[char] = &[
    '\u{202A}', '\u{202B}', '\u{202C}', '\u{202D}', '\u{202E}', // direction overrides
    '\u{2066}', '\u{2067}', '\u{2068}', '\u{2069}', // isolates
    '\u{200B}', '\u{200C}', '\u{200D}', '\u{FEFF}', // zero-width chars
];

/// Canonicalize constitution content to deterministic UTF-8 bytes.
///
/// Follows the same six-step process as the Python implementation.
///
/// # Errors
///
/// Returns [`VcpError::ParseError`] if the content contains illegal
/// control characters or forbidden Unicode codepoints.
pub fn canonicalize_content(text: &str) -> VcpResult<Vec<u8>> {
    // 1. Unicode NFC normalization.
    let text: String = text.nfc().collect();

    // 2. Line ending normalization.
    let text = text.replace("\r\n", "\n").replace('\r', "\n");

    // 3. Strip trailing whitespace from each line.
    let lines: Vec<&str> = text
        .split('\n')
        .map(|l| l.trim_end_matches([' ', '\t']))
        .collect();

    // 4. Remove trailing empty lines, ensure single trailing newline.
    let mut lines = lines;
    while lines.last().is_some_and(|l| l.is_empty()) {
        lines.pop();
    }
    let mut text = lines.join("\n");
    text.push('\n');

    // 5. Reject control characters (except \n, \t).
    for (i, ch) in text.char_indices() {
        if ch.is_control() && ch != '\n' && ch != '\t' {
            return Err(VcpError::ParseError(format!(
                "illegal control character at position {i}: U+{:04X}",
                ch as u32
            )));
        }
        if FORBIDDEN_CODEPOINTS.contains(&ch) {
            return Err(VcpError::ParseError(format!(
                "forbidden Unicode character at position {i}: U+{:04X}",
                ch as u32
            )));
        }
    }

    // 6. UTF-8 encode without BOM.
    Ok(text.into_bytes())
}

/// Compute `sha256:<hex>` hash of canonical content.
///
/// # Errors
///
/// Returns [`VcpError::ParseError`] if the content fails canonicalization.
pub fn compute_content_hash(content: &str) -> VcpResult<String> {
    let canonical = canonicalize_content(content)?;
    let mut hasher = Sha256::new();
    hasher.update(&canonical);
    let digest = hasher.finalize();
    Ok(format!("sha256:{digest:x}"))
}

/// Verify that content matches an expected hash string.
///
/// # Errors
///
/// Returns [`VcpError::ParseError`] if the content fails canonicalization.
pub fn verify_content_hash(content: &str, expected: &str) -> VcpResult<bool> {
    let computed = compute_content_hash(content)?;
    Ok(computed == expected)
}

// ── Manifest canonicalization (RFC 8785) ────────────────────

/// Canonicalize a JSON manifest for signature computation.
///
/// Implements RFC 8785 JSON Canonicalization Scheme:
/// - Keys sorted lexicographically
/// - No whitespace between tokens
/// - The `"signature"` field is excluded from canonicalization.
///
/// # Errors
///
/// Returns [`VcpError::ParseError`] if the manifest is not a JSON object,
/// or [`VcpError::JsonError`] if serialization fails.
pub fn canonicalize_manifest(manifest: &serde_json::Value) -> VcpResult<Vec<u8>> {
    let obj = manifest
        .as_object()
        .ok_or_else(|| VcpError::ParseError("manifest must be a JSON object".into()))?;

    // Remove "signature" before canonicalizing.
    let filtered: serde_json::Map<String, serde_json::Value> = obj
        .iter()
        .filter(|(k, _)| k.as_str() != "signature")
        .map(|(k, v)| (k.clone(), v.clone()))
        .collect();

    // JCS key ordering is based on UTF-16 code units and its number rendering
    // follows ECMAScript. `serde_json::to_string` differs on both edge cases,
    // so use an RFC 8785 serializer rather than relying on map insertion order.
    serde_json_canonicalizer::to_vec(&serde_json::Value::Object(filtered))
        .map_err(|e| VcpError::JsonError(e.to_string()))
}

// ── Ed25519 signature operations ────────────────────────────

/// Sign a manifest with an Ed25519 secret key.
///
/// Canonicalizes the manifest (excluding the `"signature"` field), signs
/// the canonical bytes with the provided 32-byte Ed25519 secret key, and
/// returns the signature as a standard base64-encoded string.
///
/// # Arguments
///
/// * `manifest` - A JSON manifest value (must be an object).
/// * `secret_key` - A 32-byte Ed25519 secret key (seed).
///
/// # Errors
///
/// Returns [`VcpError::SignatureError`] if the secret key is not exactly
/// 32 bytes, or [`VcpError::ParseError`] if canonicalization fails.
///
/// # Examples
///
/// ```
/// use vcp_core::transport::{sign_manifest, verify_manifest_signature};
/// use ed25519_dalek::SigningKey;
///
/// let signing_key = SigningKey::from_bytes(&[1u8; 32]);
/// let public_key = signing_key.verifying_key().to_bytes();
///
/// let manifest = serde_json::json!({
///     "vcp_version": "2.0",
///     "bundle": {"id": "test", "content_hash": "sha256:abc"}
/// });
///
/// let sig = sign_manifest(&manifest, &signing_key.to_bytes()).unwrap();
/// assert!(verify_manifest_signature(&manifest, &public_key, &sig).unwrap());
/// ```
pub fn sign_manifest(manifest: &serde_json::Value, secret_key: &[u8]) -> VcpResult<String> {
    let key_bytes: [u8; 32] = secret_key.try_into().map_err(|_| {
        VcpError::SignatureError(format!(
            "secret key must be exactly 32 bytes, got {}",
            secret_key.len()
        ))
    })?;

    let signing_key = SigningKey::from_bytes(&key_bytes);
    let canonical = canonicalize_manifest(manifest)?;
    let signature = signing_key.sign(&canonical);

    Ok(BASE64.encode(signature.to_bytes()))
}

/// Verify an Ed25519 signature against a manifest and public key.
///
/// Canonicalizes the manifest (excluding the `"signature"` field) and
/// verifies the base64-encoded signature against the provided 32-byte
/// Ed25519 public key.
///
/// # Arguments
///
/// * `manifest` - A JSON manifest value (must be an object).
/// * `public_key` - A 32-byte Ed25519 public key.
/// * `signature_b64` - Base64-encoded Ed25519 signature (64 bytes decoded).
///
/// # Errors
///
/// Returns [`VcpError::SignatureError`] if the public key or signature
/// bytes are malformed, or [`VcpError::ParseError`] if canonicalization
/// fails.
///
/// # Examples
///
/// ```
/// use vcp_core::transport::{sign_manifest, verify_manifest_signature};
/// use ed25519_dalek::SigningKey;
///
/// let signing_key = SigningKey::from_bytes(&[42u8; 32]);
/// let public_key = signing_key.verifying_key().to_bytes();
///
/// let manifest = serde_json::json!({"bundle": {"id": "abc"}});
///
/// let sig = sign_manifest(&manifest, &signing_key.to_bytes()).unwrap();
/// assert!(verify_manifest_signature(&manifest, &public_key, &sig).unwrap());
///
/// // Wrong key fails verification.
/// let wrong_key = [0u8; 32];
/// assert!(!verify_manifest_signature(&manifest, &wrong_key, &sig).unwrap_or(false));
/// ```
pub fn verify_manifest_signature(
    manifest: &serde_json::Value,
    public_key: &[u8],
    signature_b64: &str,
) -> VcpResult<bool> {
    let canonical = canonicalize_manifest(manifest)?;
    verify_ed25519_signature(&canonical, public_key, signature_b64)
}

/// Verify an Ed25519 signature over arbitrary canonical bytes.
///
/// # Errors
///
/// Returns [`VcpError::SignatureError`] when the public key or signature is malformed.
pub fn verify_ed25519_signature(
    message: &[u8],
    public_key: &[u8],
    signature_b64: &str,
) -> VcpResult<bool> {
    let key_bytes: [u8; 32] = public_key.try_into().map_err(|_| {
        VcpError::SignatureError(format!(
            "public key must be exactly 32 bytes, got {}",
            public_key.len()
        ))
    })?;

    let verifying_key = VerifyingKey::from_bytes(&key_bytes)
        .map_err(|e| VcpError::SignatureError(format!("invalid Ed25519 public key: {e}")))?;

    // Strip optional "base64:" prefix (matches Python SDK convention).
    let raw_b64 = signature_b64
        .strip_prefix("base64:")
        .unwrap_or(signature_b64);

    let sig_bytes = BASE64
        .decode(raw_b64)
        .map_err(|e| VcpError::SignatureError(format!("invalid base64 signature: {e}")))?;

    let sig_array: [u8; 64] = sig_bytes
        .try_into()
        .map_err(|_| VcpError::SignatureError("signature must be exactly 64 bytes".into()))?;

    let signature = ed25519_dalek::Signature::from_bytes(&sig_array);
    match verifying_key.verify(message, &signature) {
        Ok(()) => Ok(true),
        Err(_) => Ok(false),
    }
}

// ── Bundle verification ─────────────────────────────────────

/// Result of a bundle verification check.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct VerificationResult {
    pub code: VerificationCode,
    pub message: String,
}

impl VerificationResult {
    /// Create a successful verification result.
    #[must_use]
    pub fn valid() -> Self {
        Self {
            code: VerificationCode::Valid,
            message: "verification passed".into(),
        }
    }

    /// Create a failed result with a specific code.
    #[must_use]
    pub fn fail(code: VerificationCode, message: impl Into<String>) -> Self {
        Self {
            code,
            message: message.into(),
        }
    }

    /// Returns `true` if verification passed.
    pub fn is_valid(&self) -> bool {
        self.code.is_valid()
    }
}

// Manual Serialize/Deserialize for VerificationCode which is Copy + not a string.
impl Serialize for VerificationCode {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(&self.to_string())
    }
}

impl<'de> Deserialize<'de> for VerificationCode {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let s = String::deserialize(deserializer)?;
        match s.as_str() {
            "valid" => Ok(VerificationCode::Valid),
            "size_exceeded" => Ok(VerificationCode::SizeExceeded),
            "invalid_schema" => Ok(VerificationCode::InvalidSchema),
            "untrusted_issuer" => Ok(VerificationCode::UntrustedIssuer),
            "invalid_signature" => Ok(VerificationCode::InvalidSignature),
            "untrusted_auditor" => Ok(VerificationCode::UntrustedAuditor),
            "invalid_attestation" => Ok(VerificationCode::InvalidAttestation),
            "hash_mismatch" => Ok(VerificationCode::HashMismatch),
            "not_yet_valid" => Ok(VerificationCode::NotYetValid),
            "expired" => Ok(VerificationCode::Expired),
            "future_timestamp" => Ok(VerificationCode::FutureTimestamp),
            "replay_detected" => Ok(VerificationCode::ReplayDetected),
            "token_mismatch" => Ok(VerificationCode::TokenMismatch),
            "budget_exceeded" => Ok(VerificationCode::BudgetExceeded),
            "scope_mismatch" => Ok(VerificationCode::ScopeMismatch),
            "revoked" => Ok(VerificationCode::Revoked),
            "fetch_failed" => Ok(VerificationCode::FetchFailed),
            "revocation_unavailable" => Ok(VerificationCode::RevocationUnavailable),
            other => Err(serde::de::Error::unknown_variant(
                other,
                &[
                    "valid",
                    "size_exceeded",
                    "invalid_schema",
                    "untrusted_issuer",
                    "invalid_signature",
                    "untrusted_auditor",
                    "invalid_attestation",
                    "hash_mismatch",
                    "not_yet_valid",
                    "expired",
                    "future_timestamp",
                    "replay_detected",
                    "token_mismatch",
                    "budget_exceeded",
                    "scope_mismatch",
                    "revoked",
                    "fetch_failed",
                    "revocation_unavailable",
                ],
            )),
        }
    }
}

/// Verify that the content hash in a bundle matches the actual content.
pub fn verify_bundle_content(content: &str, expected_hash: &str) -> VerificationResult {
    match compute_content_hash(content) {
        Ok(computed) => {
            if computed == expected_hash {
                VerificationResult::valid()
            } else {
                VerificationResult::fail(
                    VerificationCode::HashMismatch,
                    format!("expected {expected_hash}, got {computed}"),
                )
            }
        }
        Err(e) => VerificationResult::fail(
            VerificationCode::InvalidSchema,
            format!("content canonicalization failed: {e}"),
        ),
    }
}

/// Verify a bundle manifest JSON against its content.
///
/// Checks:
/// 1. Content hash matches `bundle.content_hash`.
/// 2. Manifest is well-formed JSON with required fields.
///
/// # Errors
///
/// Returns [`VcpError::JsonError`] if `manifest_json` is not valid JSON,
/// or [`VcpError::ParseError`] if the manifest is missing required fields.
pub fn verify_bundle(manifest_json: &str, content: &str) -> VcpResult<VerificationResult> {
    let manifest = parse_json_strict(manifest_json)?;

    let bundle = manifest
        .get("bundle")
        .ok_or_else(|| VcpError::ParseError("missing 'bundle' field in manifest".into()))?;

    let expected_hash = bundle
        .get("content_hash")
        .and_then(|v| v.as_str())
        .ok_or_else(|| VcpError::ParseError("missing 'bundle.content_hash' in manifest".into()))?;

    Ok(verify_bundle_content(content, expected_hash))
}

// ── Tests ───────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq;

    #[test]
    fn canonicalize_normalizes_line_endings() {
        let input = "hello\r\nworld\rfoo\n";
        let result = canonicalize_content(input).unwrap();
        assert_eq!(result, b"hello\nworld\nfoo\n");
    }

    #[test]
    fn canonicalize_strips_trailing_whitespace() {
        let input = "hello   \nworld\t\t\n";
        let result = canonicalize_content(input).unwrap();
        assert_eq!(result, b"hello\nworld\n");
    }

    #[test]
    fn canonicalize_removes_trailing_empty_lines() {
        let input = "hello\n\n\n\n";
        let result = canonicalize_content(input).unwrap();
        assert_eq!(result, b"hello\n");
    }

    #[test]
    fn canonicalize_ensures_trailing_newline() {
        let input = "hello";
        let result = canonicalize_content(input).unwrap();
        assert_eq!(result, b"hello\n");
    }

    #[test]
    fn canonicalize_rejects_control_chars() {
        let input = "hello\x01world";
        assert!(canonicalize_content(input).is_err());
    }

    #[test]
    fn canonicalize_rejects_bidi_overrides() {
        let input = "hello\u{202E}world";
        assert!(canonicalize_content(input).is_err());
    }

    #[test]
    fn canonicalize_allows_tabs_and_newlines() {
        let input = "hello\tworld\nfoo";
        let result = canonicalize_content(input).unwrap();
        assert_eq!(result, b"hello\tworld\nfoo\n");
    }

    #[test]
    fn content_hash_deterministic() {
        let h1 = compute_content_hash("hello world").unwrap();
        let h2 = compute_content_hash("hello world").unwrap();
        assert_eq!(h1, h2);
        assert!(h1.starts_with("sha256:"));
    }

    #[test]
    fn content_hash_different_content() {
        let h1 = compute_content_hash("hello").unwrap();
        let h2 = compute_content_hash("world").unwrap();
        assert_ne!(h1, h2);
    }

    #[test]
    fn verify_content_hash_ok() {
        let hash = compute_content_hash("test content").unwrap();
        assert!(verify_content_hash("test content", &hash).unwrap());
    }

    #[test]
    fn verify_content_hash_mismatch() {
        assert!(!verify_content_hash("test", "sha256:wrong").unwrap());
    }

    #[test]
    fn canonicalize_crlf_equals_lf() {
        let h1 = compute_content_hash("line1\nline2").unwrap();
        let h2 = compute_content_hash("line1\r\nline2").unwrap();
        assert_eq!(h1, h2);
    }

    #[test]
    fn canonicalize_trailing_ws_ignored() {
        let h1 = compute_content_hash("hello").unwrap();
        let h2 = compute_content_hash("hello   ").unwrap();
        assert_eq!(h1, h2);
    }

    #[test]
    fn manifest_canonicalization_excludes_signature() {
        let manifest = serde_json::json!({
            "bundle": {"id": "test"},
            "signature": {"value": "should-be-excluded"},
            "version": "1.0"
        });

        let canonical = canonicalize_manifest(&manifest).unwrap();
        let canonical_str = String::from_utf8(canonical).unwrap();

        assert!(!canonical_str.contains("signature"));
        assert!(canonical_str.contains("bundle"));
    }

    #[test]
    fn manifest_canonicalization_sorts_keys() {
        let manifest = serde_json::json!({
            "z_field": 1,
            "a_field": 2,
            "m_field": 3
        });

        let canonical = canonicalize_manifest(&manifest).unwrap();
        let canonical_str = String::from_utf8(canonical).unwrap();

        let a_pos = canonical_str.find("a_field").unwrap();
        let m_pos = canonical_str.find("m_field").unwrap();
        let z_pos = canonical_str.find("z_field").unwrap();

        assert!(a_pos < m_pos);
        assert!(m_pos < z_pos);
    }

    /// Regression for a fuzzer-found canonicalization defect.
    ///
    /// `serde_json`'s default float parser is lossy by up to 1 ULP, so a
    /// high-precision number could canonicalize to bytes that no longer parsed
    /// back to the same `f64`. Canonicalization is the input to signing, so it
    /// has to be a fixed point.
    ///
    /// Crash input: `rust/fuzz/corpus/manifest_canonicalization/big_integer_precision`.
    #[test]
    fn manifest_canonicalization_is_idempotent_for_high_precision_numbers() {
        let raw =
            br#"{"array":[999999999999999999999999999999999999999999999],"ted":{"Ab":2,"a":1}}"#;
        let manifest: serde_json::Value = serde_json::from_slice(raw).unwrap();

        let first = String::from_utf8(canonicalize_manifest(&manifest).unwrap()).unwrap();
        let reparsed: serde_json::Value = serde_json::from_str(&first).unwrap();
        let second = String::from_utf8(canonicalize_manifest(&reparsed).unwrap()).unwrap();

        assert_eq!(
            first, second,
            "canonical form must survive a JSON round trip"
        );
        assert_eq!(first, r#"{"array":[1e+45],"ted":{"Ab":2,"a":1}}"#);
    }

    /// The practical consequence of the defect above: a signature computed
    /// before the manifest went over the wire had to still verify after the
    /// receiver reparsed it.
    #[test]
    fn signature_survives_json_round_trip_for_high_precision_numbers() {
        let raw = br#"{"bundle":{"id":"test"},"weights":[999999999999999999999999999999999999999999999,0.1]}"#;
        let manifest: serde_json::Value = serde_json::from_slice(raw).unwrap();

        let signing_key = SigningKey::from_bytes(&[7u8; 32]);
        let public_key = signing_key.verifying_key().to_bytes();
        let signature = sign_manifest(&manifest, &signing_key.to_bytes()).unwrap();

        // Serialize as it would go over the wire, then reparse as a receiver would.
        let wire = serde_json::to_vec(&manifest).unwrap();
        let received: serde_json::Value = serde_json::from_slice(&wire).unwrap();

        assert!(verify_manifest_signature(&received, &public_key, &signature).unwrap());
    }

    #[test]
    fn manifest_canonicalization_uses_utf16_key_order() {
        // RFC 8785 sorts property names by UTF-16 code units. U+10000 starts
        // with D800 and therefore precedes U+E000, even though scalar-value
        // and UTF-8 byte ordering put U+E000 first.
        let manifest = serde_json::json!({
            "\u{e000}": 1,
            "\u{10000}": 2,
        });

        assert_eq!(
            canonicalize_manifest(&manifest).unwrap(),
            "{\"\u{10000}\":2,\"\u{e000}\":1}".as_bytes()
        );
    }

    #[test]
    fn manifest_canonicalization_uses_ecmascript_numbers() {
        let manifest = serde_json::json!({
            "numbers": [333_333_333.333_333_3_f64, 1e30_f64, 4.50_f64, 2e-3_f64, 1e-27_f64]
        });

        assert_eq!(
            canonicalize_manifest(&manifest).unwrap(),
            br#"{"numbers":[333333333.3333333,1e+30,4.5,0.002,1e-27]}"#
        );
    }

    #[test]
    fn verify_bundle_valid() {
        let content = "# My Constitution\n\nBe kind.";
        let hash = compute_content_hash(content).unwrap();
        let manifest = serde_json::json!({
            "bundle": {
                "id": "test-bundle",
                "content_hash": hash,
            }
        });

        let result = verify_bundle(&serde_json::to_string(&manifest).unwrap(), content).unwrap();
        assert!(result.is_valid());
    }

    #[test]
    fn verify_bundle_tampered() {
        let hash = compute_content_hash("original content").unwrap();
        let manifest = serde_json::json!({
            "bundle": {
                "id": "test-bundle",
                "content_hash": hash,
            }
        });

        let result = verify_bundle(
            &serde_json::to_string(&manifest).unwrap(),
            "tampered content",
        )
        .unwrap();
        assert!(!result.is_valid());
        assert_eq!(result.code, VerificationCode::HashMismatch);
    }

    #[test]
    fn bundle_verification_rejects_duplicate_json_keys() {
        let content = "hello";
        let hash = compute_content_hash(content).unwrap();
        let manifest = format!(
            r#"{{"bundle":{{"content_hash":"{hash}"}},"bundle":{{"content_hash":"{hash}"}}}}"#
        );

        assert!(matches!(
            verify_bundle(&manifest, content),
            Err(VcpError::JsonError(message)) if message.contains("duplicate JSON object key")
        ));
    }

    // ── Ed25519 signing tests ───────────────────────────────

    /// Helper: generate a deterministic Ed25519 keypair from a seed byte.
    fn test_keypair(seed: u8) -> (SigningKey, VerifyingKey) {
        let signing_key = SigningKey::from_bytes(&[seed; 32]);
        let verifying_key = signing_key.verifying_key();
        (signing_key, verifying_key)
    }

    #[test]
    fn sign_and_verify_manifest_roundtrip() {
        let (sk, vk) = test_keypair(1);
        let manifest = serde_json::json!({
            "vcp_version": "2.0",
            "bundle": {"id": "test-bundle", "content_hash": "sha256:abc123"}
        });

        let sig = sign_manifest(&manifest, &sk.to_bytes()).unwrap();
        let valid = verify_manifest_signature(&manifest, &vk.to_bytes(), &sig).unwrap();
        assert!(valid, "signature should verify against correct public key");
    }

    #[test]
    fn verify_rejects_wrong_public_key() {
        let (sk, _vk) = test_keypair(1);
        let (_sk2, wrong_vk) = test_keypair(2);
        let manifest = serde_json::json!({"bundle": {"id": "test"}});

        let sig = sign_manifest(&manifest, &sk.to_bytes()).unwrap();
        let valid = verify_manifest_signature(&manifest, &wrong_vk.to_bytes(), &sig).unwrap();
        assert!(!valid, "signature should not verify with wrong public key");
    }

    #[test]
    fn verify_rejects_tampered_manifest() {
        let (sk, vk) = test_keypair(3);
        let manifest = serde_json::json!({"bundle": {"id": "original"}});
        let sig = sign_manifest(&manifest, &sk.to_bytes()).unwrap();

        let tampered = serde_json::json!({"bundle": {"id": "tampered"}});
        let valid = verify_manifest_signature(&tampered, &vk.to_bytes(), &sig).unwrap();
        assert!(!valid, "signature should fail on tampered manifest");
    }

    #[test]
    fn sign_excludes_signature_field() {
        let (sk, vk) = test_keypair(4);

        // Sign manifest without signature field.
        let manifest_no_sig = serde_json::json!({
            "vcp_version": "2.0",
            "bundle": {"id": "test"}
        });
        let sig = sign_manifest(&manifest_no_sig, &sk.to_bytes()).unwrap();

        // Verification should pass even if the manifest now contains a signature field,
        // because canonicalize_manifest strips it.
        let manifest_with_sig = serde_json::json!({
            "vcp_version": "2.0",
            "bundle": {"id": "test"},
            "signature": {"algorithm": "ed25519", "value": sig.clone()}
        });
        let valid = verify_manifest_signature(&manifest_with_sig, &vk.to_bytes(), &sig).unwrap();
        assert!(
            valid,
            "signature field should be excluded during verification"
        );
    }

    #[test]
    fn verify_accepts_base64_prefix() {
        let (sk, vk) = test_keypair(5);
        let manifest = serde_json::json!({"bundle": {"id": "prefix-test"}});
        let sig = sign_manifest(&manifest, &sk.to_bytes()).unwrap();

        // Verify with "base64:" prefix (Python SDK convention).
        let prefixed = format!("base64:{sig}");
        let valid = verify_manifest_signature(&manifest, &vk.to_bytes(), &prefixed).unwrap();
        assert!(valid, "should accept base64: prefixed signature");
    }

    #[test]
    fn sign_rejects_wrong_key_length() {
        let manifest = serde_json::json!({"bundle": {"id": "test"}});
        let short_key = [0u8; 16];
        let result = sign_manifest(&manifest, &short_key);
        assert!(result.is_err());
        assert!(
            result.unwrap_err().to_string().contains("32 bytes"),
            "error should mention expected key length"
        );
    }

    #[test]
    fn verify_rejects_wrong_key_length() {
        let manifest = serde_json::json!({"bundle": {"id": "test"}});
        let short_key = [0u8; 16];
        let result = verify_manifest_signature(&manifest, &short_key, "AAAA");
        assert!(result.is_err());
    }

    #[test]
    fn verify_rejects_invalid_base64() {
        let manifest = serde_json::json!({"bundle": {"id": "test"}});
        let key = [0u8; 32];
        let result = verify_manifest_signature(&manifest, &key, "not-valid-base64!!!");
        assert!(result.is_err());
    }

    #[test]
    fn sign_deterministic_for_same_key_and_manifest() {
        let (sk, _vk) = test_keypair(6);
        let manifest = serde_json::json!({
            "vcp_version": "2.0",
            "bundle": {"id": "deterministic-test", "version": "1.0.0"}
        });

        let sig1 = sign_manifest(&manifest, &sk.to_bytes()).unwrap();
        let sig2 = sign_manifest(&manifest, &sk.to_bytes()).unwrap();
        assert_eq!(
            sig1, sig2,
            "Ed25519 signing should be deterministic for same input"
        );
    }
}