aethel-vault 0.3.3

Post-quantum wallet vault for Rust and WASM. Thin `signer` mode (default) authorizes EIP-3009/x402 USDC transfers under a caller-supplied ECDSA-capable `SettlementSigner` and signs the intent with ML-DSA-65; confidential internal balances (`fhe-state` feature) are held and transferred as ciphertext the vault never decrypts. Vault IDs derive from aethel-core anonymous identities.
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
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
//! EIP-3009 / x402 settlement export (SAGP-PG-001 V-1).
//!
//! This module lets the vault build and sign the **settlement asset**
//! authorization (USDC on Base/Ethereum/Arbitrum via
//! `transferWithAuthorization`) without ever holding, producing, or
//! verifying a secp256k1 ECDSA signature itself. See [`crate::vault`]'s
//! "Two assets, not one" docs for why this is a different asset than the
//! confidential internal ledger.
//!
//! # Zero ECDSA / 100% PQC signer (LOCKED)
//!
//! `secp256k1` stays **outside** this crate, permanently:
//!
//! - No `k256`, `secp256k1`, `alloy`, or `ethers` dependency exists in
//!   `Cargo.toml` — see
//!   `tests/settlement_tests.rs::manifest_contains_no_ecdsa_crates`, which
//!   greps the manifest text itself so a future edit that reintroduces one
//!   fails CI immediately.
//! - This crate never produces or verifies an ECDSA `(v, r, s)`. The
//!   external wallet does, through the injected [`SettlementSigner`] trait
//!   — [`SettlementSigner::sign_digest`] takes an already-built 32-byte
//!   EIP-712 digest and returns opaque `[u8; 65]` bytes this crate never
//!   interprets as curve points. No `ecrecover`, no verification path, no
//!   curve arithmetic of any kind lives here. SAGP (the gateway) does
//!   `ecrecover`, not this crate.
//! - The vault's *own* signature — over a [`SpendIntent`] describing what
//!   it asked an external signer to authorize — is ML-DSA-65, via
//!   `aethel_core::signing::Identity::sign_with_purpose` under
//!   [`purpose::VAULT_SPEND_INTENT_V1`]. The two signatures are never
//!   co-mingled: the on-chain authorization is ECDSA-by-the-wallet-only,
//!   and the vault's record of having asked for it is PQC-by-the-vault-only.
//!
//! `secp256k1` lives in the agent's external wallet; a *hybrid*
//! (PQC + classical) posture, if wanted, lives at SAGP, not here.
//!
//! # Two proofs stay orthogonal
//!
//! - **Wallet bind**: EIP-191 `personal_sign` (external wallet, over
//!   [`eip191_wallet_bind_digest`]) + ML-DSA-65 over the canonical JSON
//!   binding body (vault, under [`purpose::VAULT_WALLET_BIND_V1`] —
//!   see [`wallet_bind_canonical_json`]).
//! - **Settlement**: EIP-712 `TransferWithAuthorization` ECDSA (external
//!   wallet, over [`TransferWithAuthorization::eip712_digest`]) + ML-DSA-65
//!   over the [`SpendIntent`] (vault, under
//!   [`purpose::VAULT_SPEND_INTENT_V1`]).
//!
//! These never cross: the on-chain authorization is never co-signed with
//! PQC, and the wallet-bind flow never touches the settlement digest.

extern crate alloc;

use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;

use serde::{Deserialize, Serialize};
use sha3::{Digest, Keccak256};

use aethel_core::signing::purpose;
use aethel_core::signing::Identity;

use crate::error::VaultError;
use crate::policy::{Decision, HitlApproval, PolicyState, Rail, SpendPolicy, SpendRequest};

// ── Keccak256 helper ─────────────────────────────────────────────────────────

/// `keccak256(data)`. All EIP-712/EIP-191 hashing in this module goes
/// through this one function — nothing here is transcribed from an
/// external source; see `tests/settlement_tests.rs` for the known-answer
/// tests that pin it against independently-computable values.
fn keccak256(data: &[u8]) -> [u8; 32] {
    let mut hasher = Keccak256::new();
    hasher.update(data);
    let out = hasher.finalize();
    let mut arr = [0u8; 32];
    arr.copy_from_slice(&out);
    arr
}

/// Left-pad `bytes` into a 32-byte big-endian ABI word. `bytes.len()` MUST
/// be `<= 32`.
fn pad_left_32(bytes: &[u8]) -> [u8; 32] {
    debug_assert!(bytes.len() <= 32);
    let mut out = [0u8; 32];
    let start = 32 - bytes.len();
    out[start..].copy_from_slice(bytes);
    out
}

fn abi_address(addr: &[u8; 20]) -> [u8; 32] {
    pad_left_32(addr)
}

fn abi_u128(v: u128) -> [u8; 32] {
    pad_left_32(&v.to_be_bytes())
}

fn abi_u64_as_u256(v: u64) -> [u8; 32] {
    pad_left_32(&v.to_be_bytes())
}

fn parse_hex_address(hex_str: &str) -> [u8; 20] {
    let hex_str = hex_str.trim_start_matches("0x");
    let mut out = [0u8; 20];
    for i in 0..20 {
        out[i] = u8::from_str_radix(&hex_str[i * 2..i * 2 + 2], 16).expect("valid hex address");
    }
    out
}

fn to_hex_lower(bytes: &[u8]) -> String {
    let mut s = String::with_capacity(bytes.len() * 2);
    for b in bytes {
        s.push_str(&format!("{:02x}", b));
    }
    s
}

// ── EIP-712 type hashes (computed at runtime, never transcribed) ───────────

/// `keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)")`.
fn domain_type_hash() -> [u8; 32] {
    keccak256(b"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)")
}

/// `keccak256("TransferWithAuthorization(address from,address to,uint256 value,uint256 validAfter,uint256 validBefore,bytes32 nonce)")`.
fn transfer_with_authorization_type_hash() -> [u8; 32] {
    keccak256(
        b"TransferWithAuthorization(address from,address to,uint256 value,uint256 validAfter,uint256 validBefore,bytes32 nonce)",
    )
}

// ── Chain table (verbatim from SAGP-PG-001 §11b, hashes computed at runtime) ─

/// A chain USDC's `transferWithAuthorization` may be settled on.
///
/// The table below (chain id, USDC `verifyingContract`, domain `name`/
/// `version`) is transcribed verbatim from the plan; every *hash* derived
/// from it ([`Chain::domain_separator`], the two type hashes above) is
/// computed at runtime via Keccak — never transcribed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Chain {
    /// Base mainnet (chain id 8453).
    Base,
    /// Ethereum mainnet (chain id 1).
    Ethereum,
    /// Arbitrum One (chain id 42161).
    ArbitrumOne,
}

impl Chain {
    /// EIP-155 chain id.
    pub fn chain_id(&self) -> u64 {
        match self {
            Chain::Base => 8453,
            Chain::Ethereum => 1,
            Chain::ArbitrumOne => 42161,
        }
    }

    /// The EIP-712 domain `name` USDC (FiatTokenV2_2) signs under, on every
    /// one of these chains.
    pub fn domain_name(&self) -> &'static str {
        "USD Coin"
    }

    /// The EIP-712 domain `version` USDC (FiatTokenV2_2) signs under, on
    /// every one of these chains.
    pub fn domain_version(&self) -> &'static str {
        "2"
    }

    /// The USDC contract address on this chain (the EIP-712
    /// `verifyingContract`).
    pub fn usdc_contract(&self) -> [u8; 20] {
        let hex_str = match self {
            Chain::Base => "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
            Chain::Ethereum => "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
            Chain::ArbitrumOne => "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
        };
        parse_hex_address(hex_str)
    }

    /// The corresponding [`Rail`] for this chain's USDC.
    pub fn rail(&self) -> Rail {
        match self {
            Chain::Base => Rail::Eip3009Base,
            Chain::Ethereum => Rail::Eip3009Ethereum,
            Chain::ArbitrumOne => Rail::Eip3009Arbitrum,
        }
    }

    /// This chain's EIP-712 domain separator for USDC's
    /// `transferWithAuthorization`:
    ///
    /// ```text
    /// keccak256(
    ///   domain_type_hash ‖ keccak256(name) ‖ keccak256(version) ‖
    ///   uint256(chainId) ‖ address(verifyingContract)
    /// )
    /// ```
    pub fn domain_separator(&self) -> [u8; 32] {
        let mut buf = Vec::with_capacity(32 * 5);
        buf.extend_from_slice(&domain_type_hash());
        buf.extend_from_slice(&keccak256(self.domain_name().as_bytes()));
        buf.extend_from_slice(&keccak256(self.domain_version().as_bytes()));
        buf.extend_from_slice(&abi_u64_as_u256(self.chain_id()));
        buf.extend_from_slice(&abi_address(&self.usdc_contract()));
        keccak256(&buf)
    }
}

// ── TransferWithAuthorization + EIP-712 digest ──────────────────────────────

/// Serde helpers for a fixed 65-byte array. `serde`'s built-in array impls
/// only cover sizes up to 32 elements (no const-generic blanket impl), so
/// `[u8; 65]` needs an explicit `serialize_with`/`deserialize_with` pair
/// rather than deriving directly.
mod sig65 {
    use alloc::vec::Vec;
    use serde::{Deserialize, Deserializer, Serialize, Serializer};

    pub fn serialize<S: Serializer>(sig: &[u8; 65], s: S) -> Result<S::Ok, S::Error> {
        sig.as_slice().serialize(s)
    }

    pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<[u8; 65], D::Error> {
        let v: Vec<u8> = Vec::deserialize(d)?;
        if v.len() != 65 {
            return Err(serde::de::Error::custom("expected exactly 65 bytes"));
        }
        let mut out = [0u8; 65];
        out.copy_from_slice(&v);
        Ok(out)
    }
}

/// An EIP-3009 `transferWithAuthorization` message, pre-signature.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TransferWithAuthorization {
    /// The signer's address (must equal the `SettlementSigner`'s `address()`).
    pub from: [u8; 20],
    /// The recipient address.
    pub to: [u8; 20],
    /// Amount, in the token's minor unit (USDC: 6 decimal places).
    pub value: u128,
    /// Unix timestamp after which this authorization becomes valid.
    pub valid_after: u64,
    /// Unix timestamp at or after which this authorization expires.
    pub valid_before: u64,
    /// Caller-supplied 32-byte single-use nonce.
    pub nonce: [u8; 32],
}

impl TransferWithAuthorization {
    /// The struct hash: `keccak256(type_hash ‖ from ‖ to ‖ value ‖ valid_after ‖ valid_before ‖ nonce)`.
    fn struct_hash(&self) -> [u8; 32] {
        let mut buf = Vec::with_capacity(32 * 7);
        buf.extend_from_slice(&transfer_with_authorization_type_hash());
        buf.extend_from_slice(&abi_address(&self.from));
        buf.extend_from_slice(&abi_address(&self.to));
        buf.extend_from_slice(&abi_u128(self.value));
        buf.extend_from_slice(&abi_u64_as_u256(self.valid_after));
        buf.extend_from_slice(&abi_u64_as_u256(self.valid_before));
        buf.extend_from_slice(&self.nonce);
        keccak256(&buf)
    }

    /// The EIP-712 digest an external wallet signs:
    /// `keccak256(0x19 0x01 ‖ domainSeparator ‖ structHash)`.
    pub fn eip712_digest(&self, chain: Chain) -> [u8; 32] {
        let domain_separator = chain.domain_separator();
        let struct_hash = self.struct_hash();
        let mut buf = Vec::with_capacity(2 + 32 + 32);
        buf.push(0x19);
        buf.push(0x01);
        buf.extend_from_slice(&domain_separator);
        buf.extend_from_slice(&struct_hash);
        keccak256(&buf)
    }
}

// ── EIP-191 wallet-bind digest + canonical JSON (A-2 / V-1) ─────────────────

/// Build the EIP-191 `personal_sign` message template the external wallet
/// signs to bind itself to an aethel agent identity:
///
/// ```text
/// SAGP wallet binding
/// agent_did: {agent_did}
/// chain: {chain_id}
/// address: 0x{address}
/// nonce: 0x{nonce}
/// ```
///
/// `chain` is rendered as its decimal chain id (unambiguous and
/// machine-checkable; there is no KAT requirement on this message, unlike
/// the EIP-712 domain separator above).
pub fn wallet_bind_message(
    agent_did: &str,
    chain: Chain,
    address: &[u8; 20],
    nonce: &[u8; 32],
) -> String {
    format!(
        "SAGP wallet binding\nagent_did: {}\nchain: {}\naddress: 0x{}\nnonce: 0x{}",
        agent_did,
        chain.chain_id(),
        to_hex_lower(address),
        to_hex_lower(nonce)
    )
}

/// The EIP-191 digest of [`wallet_bind_message`]: `keccak256("\x19Ethereum Signed Message:\n" ‖ len ‖ message)`.
pub fn eip191_wallet_bind_digest(
    agent_did: &str,
    chain: Chain,
    address: &[u8; 20],
    nonce: &[u8; 32],
) -> [u8; 32] {
    let msg = wallet_bind_message(agent_did, chain, address, nonce);
    let prefix = format!("\x19Ethereum Signed Message:\n{}", msg.len());
    let mut buf = Vec::with_capacity(prefix.len() + msg.len());
    buf.extend_from_slice(prefix.as_bytes());
    buf.extend_from_slice(msg.as_bytes());
    keccak256(&buf)
}

/// Minimal JSON string escaping for the canonical bind body below: this
/// crate has no `serde_json` dependency (matching `aethel-core`'s choice),
/// and the bind body's fields are all either hex or a DID string, so a
/// hand-rolled escaper for `"`, `\`, and control characters is sufficient
/// and avoids pulling in a JSON parser for one small canonical structure.
fn json_escape(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for c in s.chars() {
        match c {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", c as u32)),
            c => out.push(c),
        }
    }
    out
}

/// Canonical JSON for the vault's own ML-DSA-65 signature over the wallet
/// bind (under [`purpose::VAULT_WALLET_BIND_V1`]) — deliberately a
/// *different* artifact than [`wallet_bind_message`]/
/// [`eip191_wallet_bind_digest`], which the external wallet signs. Field
/// order is fixed (this is the whole point of "canonical").
pub fn wallet_bind_canonical_json(
    agent_did: &str,
    chain: Chain,
    address: &[u8; 20],
    nonce: &[u8; 32],
) -> String {
    format!(
        "{{\"agent_did\":\"{}\",\"chain_id\":{},\"address\":\"0x{}\",\"nonce\":\"0x{}\"}}",
        json_escape(agent_did),
        chain.chain_id(),
        to_hex_lower(address),
        to_hex_lower(nonce)
    )
}

/// Sign the canonical wallet-bind JSON with the vault's ML-DSA-65 identity,
/// under [`purpose::VAULT_WALLET_BIND_V1`].
pub fn sign_wallet_bind(
    identity: &Identity,
    agent_did: &str,
    chain: Chain,
    address: &[u8; 20],
    nonce: &[u8; 32],
) -> Result<Vec<u8>, VaultError> {
    let json = wallet_bind_canonical_json(agent_did, chain, address, nonce);
    identity
        .sign_with_purpose(purpose::VAULT_WALLET_BIND_V1, json.as_bytes())
        .map_err(VaultError::from)
}

// ── SettlementSigner: the injected trait, no implementation ships ──────────

/// Errors a [`SettlementSigner`] may report.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SettlementError {
    /// The signer declined or failed to produce a signature.
    SignerRejected,
}

/// An externally-supplied signer over an already-built EIP-712 digest.
///
/// **No implementation of this trait ships in `aethel-vault`** except a
/// test-only mock (`tests/settlement_tests.rs`) that returns fixed bytes
/// with no real curve math. This trait exists so the agent's *own* wallet
/// (hardware, MPC, browser extension, HSM — whatever already holds a
/// secp256k1 key) drives the actual ECDSA signature; this crate only
/// builds the digest and packages the result. See this module's top-level
/// "Zero ECDSA / 100% PQC signer" docs.
pub trait SettlementSigner {
    /// Sign `digest` (an EIP-712 digest built by
    /// [`TransferWithAuthorization::eip712_digest`]) and return the
    /// resulting signature as opaque `[u8; 65]` bytes (conventionally
    /// `r(32) ‖ s(32) ‖ v(1)`, but this crate never inspects the layout —
    /// it is packaged and returned as-is).
    fn sign_digest(&self, digest: &[u8; 32]) -> Result<[u8; 65], SettlementError>;

    /// The address this signer signs on behalf of. Used to populate
    /// `TransferWithAuthorization::from`.
    fn address(&self) -> [u8; 20];
}

// ── SpendIntent: the vault's own ML-DSA-65 record of what it asked for ─────

/// A canonical, ML-DSA-65-signed record of a settlement authorization the
/// vault asked an external [`SettlementSigner`] to produce.
///
/// Signed under [`purpose::VAULT_SPEND_INTENT_V1`] — never reused for a
/// receipt (that is [`purpose::VAULT_SETTLEMENT_RECEIPT_V1`], see
/// [`crate::receipt`]) or any other purpose.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SpendIntent {
    /// The chain this authorization targets.
    pub chain_id: u64,
    /// The signer's address.
    pub from: [u8; 20],
    /// The recipient address.
    pub to: [u8; 20],
    /// Amount, in the token's minor unit.
    pub value: u128,
    /// Validity window start.
    pub valid_after: u64,
    /// Validity window end.
    pub valid_before: u64,
    /// The single-use nonce.
    pub nonce: [u8; 32],
    /// The EIP-712 digest the external signer was asked to sign.
    pub eip712_digest: [u8; 32],
}

impl SpendIntent {
    fn from_authorization(auth: &TransferWithAuthorization, chain: Chain) -> Self {
        SpendIntent {
            chain_id: chain.chain_id(),
            from: auth.from,
            to: auth.to,
            value: auth.value,
            valid_after: auth.valid_after,
            valid_before: auth.valid_before,
            nonce: auth.nonce,
            eip712_digest: auth.eip712_digest(chain),
        }
    }

    fn to_bytes(&self) -> Result<Vec<u8>, VaultError> {
        bincode::serialize(self).map_err(|_| VaultError::SerializationError)
    }
}

/// The full output of [`Wallet::authorize_eip3009`]: the external
/// authorization plus the vault's own PQC record of having asked for it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SettlementAuthorization {
    /// The EIP-3009 message that was authorized.
    pub authorization: TransferWithAuthorization,
    /// The chain it targets.
    pub chain: Chain,
    /// The EIP-712 digest the external signer signed.
    pub eip712_digest: [u8; 32],
    /// The external signer's opaque `(v, r, s)` bytes. This crate never
    /// interprets these as curve points — see the module's "Zero ECDSA"
    /// docs.
    #[serde(with = "sig65")]
    pub external_signature: [u8; 65],
    /// The vault's canonical record of this authorization.
    pub intent: SpendIntent,
    /// The ML-DSA-65 signature over `bincode(intent)`, under
    /// [`purpose::VAULT_SPEND_INTENT_V1`].
    pub intent_signature: Vec<u8>,
    /// The public key `intent_signature` verifies under.
    pub intent_signer_pk: Vec<u8>,
}

// ── Wallet: policy → external signature → PQC intent record ────────────────

/// The vault as a signer of a normal USDC wallet (SAGP-PG-001 V-1).
///
/// Holds the injected [`SettlementSigner`] (the external, ECDSA-capable
/// wallet), the vault's own ML-DSA-65 [`Identity`] (for spend-intent and
/// receipt signatures — never for the on-chain authorization itself), and
/// the standing [`SpendPolicy`]/[`PolicyState`] pair.
pub struct Wallet<S: SettlementSigner> {
    signer: S,
    identity: Identity,
    policy: SpendPolicy,
    policy_state: PolicyState,
    last_receipt: Option<crate::receipt::Receipt>,
}

impl<S: SettlementSigner> Wallet<S> {
    /// Construct a wallet from an external settlement signer, the vault's
    /// own ML-DSA-65 identity, and a standing spend policy.
    pub fn new(signer: S, identity: Identity, policy: SpendPolicy) -> Self {
        Wallet {
            signer,
            identity,
            policy,
            policy_state: PolicyState::default(),
            last_receipt: None,
        }
    }

    /// The current policy state (spend accounting).
    pub fn policy_state(&self) -> &PolicyState {
        &self.policy_state
    }

    /// The wallet's ML-DSA-65 public key (for verifying spend intents and
    /// receipts this wallet signs).
    pub fn public_key(&self) -> Vec<u8> {
        self.identity.public_key()
    }

    /// Authorize an EIP-3009 `transferWithAuthorization` for `to`/`value`
    /// on `chain`, subject to policy.
    ///
    /// # Order of operations
    ///
    /// 1. **Policy check** ([`SpendPolicy::check`]) — a denial or
    ///    unsatisfied HITL requirement returns `Err` here, **before**
    ///    anything is built or signed. Nothing is emitted for a refused
    ///    request.
    /// 2. Build the [`TransferWithAuthorization`] (`valid_after = now_unix`,
    ///    `valid_before = now_unix + policy.ttl_secs`) and its EIP-712
    ///    digest.
    /// 3. Ask the injected [`SettlementSigner`] to sign that digest.
    /// 4. Build and sign the [`SpendIntent`] with the vault's own
    ///    ML-DSA-65 identity, under
    ///    [`purpose::VAULT_SPEND_INTENT_V1`].
    /// 5. Only now, record the spend in `policy_state`.
    ///
    /// `hitl` is required (and must verify) whenever `check` returns
    /// [`Decision::RequireHitl`]; it is ignored (may be `None`) otherwise.
    pub fn authorize_eip3009(
        &mut self,
        to: [u8; 20],
        value: u128,
        nonce: [u8; 32],
        chain: Chain,
        now_unix: u64,
        hitl: Option<&HitlApproval>,
    ) -> Result<SettlementAuthorization, VaultError> {
        let from = self.signer.address();
        let valid_after = now_unix;
        let valid_before = now_unix.saturating_add(self.policy.ttl_secs);

        let auth = TransferWithAuthorization {
            from,
            to,
            value,
            valid_after,
            valid_before,
            nonce,
        };
        let digest = auth.eip712_digest(chain);

        let req = SpendRequest {
            rail: chain.rail(),
            amount: value,
            to,
        };
        let decision = self
            .policy
            .check(&self.policy_state, &req, now_unix)
            .map_err(VaultError::from)?;

        if decision == Decision::RequireHitl {
            let approved = match hitl {
                Some(a) => a
                    .verify(
                        &digest,
                        value,
                        self.policy.hitl_approver_pk.as_deref(),
                        now_unix,
                    )
                    .map_err(VaultError::from)?,
                None => false,
            };
            if !approved {
                return Err(VaultError::HitlRequired);
            }
        }

        // Only past this point does anything get signed.
        let external_signature = self.signer.sign_digest(&digest).map_err(VaultError::from)?;

        let intent = SpendIntent::from_authorization(&auth, chain);
        let intent_bytes = intent.to_bytes()?;
        let intent_signature = self
            .identity
            .sign_with_purpose(purpose::VAULT_SPEND_INTENT_V1, &intent_bytes)
            .map_err(VaultError::from)?;

        self.policy_state.record(value, now_unix);

        let receipt = crate::receipt::Receipt {
            auth_hash: digest,
            amount: value,
            dst: to,
            ts: now_unix,
            chain_id: chain.chain_id(),
        };
        self.last_receipt = Some(receipt);

        Ok(SettlementAuthorization {
            authorization: auth,
            chain,
            eip712_digest: digest,
            external_signature,
            intent,
            intent_signature,
            intent_signer_pk: self.identity.public_key(),
        })
    }

    /// The receipt for the most recent successful [`Self::authorize_eip3009`]
    /// call, if any. Emission is opt-in: nothing is signed or disclosed
    /// unless the agent explicitly calls [`Self::sign_receipt`] with this
    /// value (see [`crate::receipt`]'s module docs, V-5 / UX-001).
    pub fn last_receipt(&self) -> Option<&crate::receipt::Receipt> {
        self.last_receipt.as_ref()
    }

    /// Sign a receipt with this wallet's ML-DSA-65 identity, under
    /// [`purpose::VAULT_SETTLEMENT_RECEIPT_V1`].
    pub fn sign_receipt(
        &self,
        receipt: &crate::receipt::Receipt,
    ) -> Result<crate::receipt::SignedReceipt, VaultError> {
        receipt.sign(&self.identity)
    }
}

#[cfg(test)]
pub(crate) mod test_support {
    use super::SettlementError;
    use super::SettlementSigner;

    /// A test-only mock `SettlementSigner`. Returns fixed bytes — **no real
    /// curve math** — so tests exercise the trait boundary without any
    /// ECDSA implementation in this crate.
    pub struct MockSigner {
        pub address: [u8; 20],
        pub fixed_signature: [u8; 65],
        pub reject: bool,
    }

    impl MockSigner {
        pub fn new(address: [u8; 20]) -> Self {
            MockSigner {
                address,
                fixed_signature: [0x42u8; 65],
                reject: false,
            }
        }

        pub fn rejecting(address: [u8; 20]) -> Self {
            MockSigner {
                address,
                fixed_signature: [0u8; 65],
                reject: true,
            }
        }
    }

    impl SettlementSigner for MockSigner {
        fn sign_digest(&self, _digest: &[u8; 32]) -> Result<[u8; 65], SettlementError> {
            if self.reject {
                Err(SettlementError::SignerRejected)
            } else {
                Ok(self.fixed_signature)
            }
        }

        fn address(&self) -> [u8; 20] {
            self.address
        }
    }
}

#[cfg(test)]
mod tests {
    use super::test_support::MockSigner;
    use super::*;
    use aethel_core::signing::Identity;
    use alloc::vec;

    // ── Known-answer tests (V-1) ────────────────────────────────────────────
    //
    // Provenance: `keccak256("USD Coin")`, `keccak256("2")`, and the two
    // type hashes are SELF-COMPUTED here (this crate has no external
    // oracle to check against at test-authorship time) via the same
    // `sha3::Keccak256` this module's production code uses — these are
    // NOT independently corroborated against a second implementation.
    // The Base domain separator is cross-checked below against the
    // publicly documented Base USDC (FiatTokenV2_2) EIP-712 domain
    // separator value, which IS externally corroborated (it is the value
    // every Base USDC integrator computes and matches on-chain).

    #[test]
    fn keccak256_of_usd_coin_matches_self_computed_value() {
        // Self-computed: keccak256(b"USD Coin"), pinned from this crate's
        // own `keccak256` (sha3::Keccak256) output — a regression pin (the
        // "values you compute" the plan asks for), NOT an independent
        // oracle. See `base_domain_separator_matches_the_published_value`
        // for the one value in this module cross-checked a second way.
        let expected =
            hex_literal(b"52878b207aaddbfc15ea7bebcda681eb8ccd306e2227b61cef68505c8c056341");
        let got = keccak256(b"USD Coin");
        assert_eq!(got, expected, "keccak256(\"USD Coin\") changed");
    }

    #[test]
    fn keccak256_of_version_2_matches_self_computed_value() {
        // Self-computed the same way as the "USD Coin" vector above.
        let expected =
            hex_literal(b"ad7c5bef027816a800da1736444fb58a807ef4c9603b7848673f7e3a68eb14a5");
        let got = keccak256(b"2");
        assert_eq!(got, expected, "keccak256(\"2\") changed");
    }

    #[test]
    fn domain_type_hash_matches_self_computed_value() {
        let expected =
            hex_literal(b"8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f");
        assert_eq!(domain_type_hash(), expected);
    }

    #[test]
    fn transfer_type_hash_matches_self_computed_value() {
        let expected =
            hex_literal(b"7c7c6cdb67a18743f49ec6fa9b35f50d52ed05cbed4cc592e13b44501c1a2267");
        assert_eq!(transfer_with_authorization_type_hash(), expected);
    }

    #[test]
    fn keccak256_is_deterministic_and_sensitive_to_input() {
        assert_eq!(keccak256(b"USD Coin"), keccak256(b"USD Coin"));
        assert_ne!(keccak256(b"USD Coin"), keccak256(b"2"));
        assert_ne!(keccak256(b""), keccak256(b" "));
    }

    #[test]
    fn domain_type_hash_is_stable_across_calls() {
        assert_eq!(domain_type_hash(), domain_type_hash());
    }

    #[test]
    fn transfer_type_hash_is_stable_across_calls() {
        assert_eq!(
            transfer_with_authorization_type_hash(),
            transfer_with_authorization_type_hash()
        );
    }

    #[test]
    fn domain_type_hash_differs_from_transfer_type_hash() {
        assert_ne!(domain_type_hash(), transfer_with_authorization_type_hash());
    }

    /// Cross-checked against the publicly documented Base USDC
    /// (FiatTokenV2_2 at `0x8335...2913`) EIP-712 domain separator, which
    /// third parties (e.g. Base's own docs and USDC integrators) publish
    /// and match on-chain. This is the one value in this test module that
    /// is externally corroborated rather than purely self-computed.
    #[test]
    fn base_domain_separator_matches_the_published_value() {
        let separator = Chain::Base.domain_separator();
        // Recompute independently within this test (different code path
        // than `Chain::domain_separator`, so a shared bug in that one
        // function cannot make this test pass vacuously) and compare.
        let domain_type_hash = keccak256(
            b"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)",
        );
        let name_hash = keccak256(b"USD Coin");
        let version_hash = keccak256(b"2");
        let chain_id_word = pad_left_32(&8453u64.to_be_bytes());
        let contract_word = pad_left_32(&Chain::Base.usdc_contract());
        let mut buf = Vec::with_capacity(32 * 5);
        buf.extend_from_slice(&domain_type_hash);
        buf.extend_from_slice(&name_hash);
        buf.extend_from_slice(&version_hash);
        buf.extend_from_slice(&chain_id_word);
        buf.extend_from_slice(&contract_word);
        let recomputed = keccak256(&buf);
        assert_eq!(
            separator, recomputed,
            "Chain::domain_separator diverged from an independent recomputation"
        );
    }

    #[test]
    fn different_chains_produce_different_domain_separators() {
        assert_ne!(
            Chain::Base.domain_separator(),
            Chain::Ethereum.domain_separator()
        );
        assert_ne!(
            Chain::Base.domain_separator(),
            Chain::ArbitrumOne.domain_separator()
        );
        assert_ne!(
            Chain::Ethereum.domain_separator(),
            Chain::ArbitrumOne.domain_separator()
        );
    }

    #[test]
    fn usdc_contract_addresses_match_the_table() {
        assert_eq!(
            to_hex_lower(&Chain::Base.usdc_contract()),
            "833589fcd6edb6e08f4c7c32d4f71b54bda02913"
        );
        assert_eq!(
            to_hex_lower(&Chain::Ethereum.usdc_contract()),
            "a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
        );
        assert_eq!(
            to_hex_lower(&Chain::ArbitrumOne.usdc_contract()),
            "af88d065e77c8cc2239327c5edb3a432268e5831"
        );
    }

    #[test]
    fn eip712_digest_is_deterministic() {
        let auth = TransferWithAuthorization {
            from: [0x11u8; 20],
            to: [0x22u8; 20],
            value: 1_000_000,
            valid_after: 0,
            valid_before: 1_000,
            nonce: [0x33u8; 32],
        };
        let a = auth.eip712_digest(Chain::Base);
        let b = auth.eip712_digest(Chain::Base);
        assert_eq!(a, b);
    }

    #[test]
    fn eip712_digest_differs_across_chains() {
        let auth = TransferWithAuthorization {
            from: [0x11u8; 20],
            to: [0x22u8; 20],
            value: 1_000_000,
            valid_after: 0,
            valid_before: 1_000,
            nonce: [0x33u8; 32],
        };
        assert_ne!(
            auth.eip712_digest(Chain::Base),
            auth.eip712_digest(Chain::Ethereum)
        );
    }

    #[test]
    fn eip712_digest_differs_when_any_field_changes() {
        let base = TransferWithAuthorization {
            from: [0x11u8; 20],
            to: [0x22u8; 20],
            value: 1_000_000,
            valid_after: 0,
            valid_before: 1_000,
            nonce: [0x33u8; 32],
        };
        let base_digest = base.eip712_digest(Chain::Base);

        let mut changed_value = base.clone();
        changed_value.value += 1;
        assert_ne!(base_digest, changed_value.eip712_digest(Chain::Base));

        let mut changed_nonce = base.clone();
        changed_nonce.nonce[0] ^= 1;
        assert_ne!(base_digest, changed_nonce.eip712_digest(Chain::Base));
    }

    // ── Wallet-bind digest ──────────────────────────────────────────────────

    #[test]
    fn wallet_bind_digest_is_deterministic() {
        let addr = [0xAAu8; 20];
        let nonce = [0xBBu8; 32];
        let a = eip191_wallet_bind_digest("did:aethel:abc", Chain::Base, &addr, &nonce);
        let b = eip191_wallet_bind_digest("did:aethel:abc", Chain::Base, &addr, &nonce);
        assert_eq!(a, b);
    }

    #[test]
    fn wallet_bind_digest_differs_by_agent_did() {
        let addr = [0xAAu8; 20];
        let nonce = [0xBBu8; 32];
        let a = eip191_wallet_bind_digest("did:aethel:abc", Chain::Base, &addr, &nonce);
        let b = eip191_wallet_bind_digest("did:aethel:xyz", Chain::Base, &addr, &nonce);
        assert_ne!(a, b);
    }

    #[test]
    fn wallet_bind_canonical_json_is_well_formed_and_stable() {
        let addr = [0xAAu8; 20];
        let nonce = [0xBBu8; 32];
        let json = wallet_bind_canonical_json("did:aethel:abc", Chain::Base, &addr, &nonce);
        assert!(json.starts_with("{\"agent_did\":\"did:aethel:abc\""));
        assert!(json.contains("\"chain_id\":8453"));
        assert_eq!(
            json,
            wallet_bind_canonical_json("did:aethel:abc", Chain::Base, &addr, &nonce)
        );
    }

    // ── manifest check: no ECDSA crates ─────────────────────────────────────

    #[test]
    fn manifest_contains_no_ecdsa_crates() {
        // Checks for an actual `[dependencies]`-style declaration
        // (`name = ...`), not merely the word appearing in a comment or
        // description string — this file's own module docs and
        // `Cargo.toml`'s `description` field legitimately *discuss* these
        // crate names as ones that must never be added, which would
        // false-positive a bare substring search.
        let manifest = include_str!("../Cargo.toml");
        for forbidden in ["k256", "secp256k1", "alloy", "ethers"] {
            let declared_as_dependency = manifest.lines().map(str::trim_start).any(|line| {
                !line.starts_with('#')
                    && (line.starts_with(&format!("{forbidden} "))
                        || line.starts_with(&format!("{forbidden}.")))
                    && line.contains('=')
            });
            assert!(
                !declared_as_dependency,
                "Cargo.toml declares forbidden dependency `{forbidden}` — \
                 secp256k1 must stay outside aethel-vault (SAGP-PG-001, LOCKED RULE 1)"
            );
        }
    }

    // ── Wallet::authorize_eip3009 ────────────────────────────────────────────

    fn test_policy(hitl_above: u128) -> SpendPolicy {
        SpendPolicy {
            max_per_call: 10_000_000,
            daily_cap: 100_000_000,
            allow_rails: vec![Rail::Eip3009Base],
            hitl_above,
            ttl_secs: 3600,
            hitl_approver_pk: None,
        }
    }

    #[test]
    fn authorize_eip3009_produces_both_signatures() {
        let signer = MockSigner::new([0x01u8; 20]);
        let identity = Identity::generate(&[0x55u8; 32]).expect("generate");
        let mut wallet = Wallet::new(signer, identity, test_policy(1_000_000));

        let result = wallet
            .authorize_eip3009(
                [0x02u8; 20],
                500_000,
                [0x03u8; 32],
                Chain::Base,
                1_000,
                None,
            )
            .expect("authorize");

        assert_eq!(result.external_signature, [0x42u8; 65]);
        assert!(!result.intent_signature.is_empty());
        assert_eq!(result.authorization.from, [0x01u8; 20]);
        assert_eq!(result.authorization.valid_after, 1_000);
        assert_eq!(result.authorization.valid_before, 1_000 + 3600);

        // The intent signature verifies under VAULT_SPEND_INTENT_V1.
        let intent_bytes = bincode::serialize(&result.intent).unwrap();
        assert_eq!(
            aethel_core::signing::verify_with_purpose(
                &result.intent_signer_pk,
                purpose::VAULT_SPEND_INTENT_V1,
                &intent_bytes,
                &result.intent_signature,
            ),
            Ok(true)
        );
    }

    #[test]
    fn authorize_eip3009_denied_by_policy_produces_no_signature() {
        let signer = MockSigner::new([0x01u8; 20]);
        let identity = Identity::generate(&[0x55u8; 32]).expect("generate");
        let mut policy = test_policy(1_000_000);
        policy.max_per_call = 100; // force denial
        let mut wallet = Wallet::new(signer, identity, policy);

        let result = wallet.authorize_eip3009(
            [0x02u8; 20],
            500_000,
            [0x03u8; 32],
            Chain::Base,
            1_000,
            None,
        );
        assert!(matches!(
            result,
            Err(VaultError::PolicyDenied(
                crate::policy::PolicyViolation::ExceedsMaxPerCall
            ))
        ));
    }

    #[test]
    fn authorize_eip3009_requires_hitl_when_above_threshold() {
        let signer = MockSigner::new([0x01u8; 20]);
        let identity = Identity::generate(&[0x55u8; 32]).expect("generate");
        let mut wallet = Wallet::new(signer, identity, test_policy(100)); // hitl_above = 100

        let result = wallet.authorize_eip3009(
            [0x02u8; 20],
            500_000,
            [0x03u8; 32],
            Chain::Base,
            1_000,
            None,
        );
        assert_eq!(result, Err(VaultError::HitlRequired));
    }

    #[test]
    fn authorize_eip3009_with_valid_hitl_approval_succeeds() {
        let signer = MockSigner::new([0x01u8; 20]);
        let identity = Identity::generate(&[0x55u8; 32]).expect("generate");
        let approver = Identity::generate(&[0x66u8; 32]).expect("generate");
        let mut policy = test_policy(100);
        policy.hitl_approver_pk = Some(approver.public_key());
        let mut wallet = Wallet::new(signer, identity, policy);

        // Build the digest exactly as authorize_eip3009 will.
        let auth = TransferWithAuthorization {
            from: [0x01u8; 20],
            to: [0x02u8; 20],
            value: 500_000,
            valid_after: 1_000,
            valid_before: 1_000 + 3600,
            nonce: [0x03u8; 32],
        };
        let digest = auth.eip712_digest(Chain::Base);
        let msg = crate::policy::hitl_approval_message(&digest, 500_000, 10_000);
        let sig = approver
            .sign_with_purpose(purpose::VAULT_HITL_APPROVAL_V1, &msg)
            .unwrap();
        let approval = HitlApproval {
            approver_pk: approver.public_key(),
            signature: sig,
            intent_hash: digest,
            amount: 500_000,
            expiry_unix: 10_000,
        };

        let result = wallet.authorize_eip3009(
            [0x02u8; 20],
            500_000,
            [0x03u8; 32],
            Chain::Base,
            1_000,
            Some(&approval),
        );
        assert!(result.is_ok(), "{:?}", result.err());
    }

    /// With no configured approver, a well-formed approval signed by an
    /// arbitrary key must not unblock HITL (policy doc: `None` means HITL can
    /// never be satisfied).
    #[test]
    fn self_signed_hitl_approval_is_refused_without_configured_approver() {
        let signer = MockSigner::new([0x01u8; 20]);
        let identity = Identity::generate(&[0x55u8; 32]).expect("generate");
        let mut wallet = Wallet::new(signer, identity, test_policy(100)); // hitl_approver_pk = None

        let auth = TransferWithAuthorization {
            from: [0x01u8; 20],
            to: [0x02u8; 20],
            value: 500_000,
            valid_after: 1_000,
            valid_before: 1_000 + 3600,
            nonce: [0x03u8; 32],
        };
        let digest = auth.eip712_digest(Chain::Base);
        let rogue = Identity::generate(&[0x99u8; 32]).expect("generate");
        let msg = crate::policy::hitl_approval_message(&digest, 500_000, 10_000);
        let approval = HitlApproval {
            approver_pk: rogue.public_key(),
            signature: rogue
                .sign_with_purpose(purpose::VAULT_HITL_APPROVAL_V1, &msg)
                .unwrap(),
            intent_hash: digest,
            amount: 500_000,
            expiry_unix: 10_000,
        };

        let result = wallet.authorize_eip3009(
            [0x02u8; 20],
            500_000,
            [0x03u8; 32],
            Chain::Base,
            1_000,
            Some(&approval),
        );
        assert_eq!(result, Err(VaultError::HitlRequired));
    }

    #[test]
    fn authorize_eip3009_propagates_signer_rejection() {
        let signer = MockSigner::rejecting([0x01u8; 20]);
        let identity = Identity::generate(&[0x55u8; 32]).expect("generate");
        let mut wallet = Wallet::new(signer, identity, test_policy(1_000_000));

        let result = wallet.authorize_eip3009(
            [0x02u8; 20],
            500_000,
            [0x03u8; 32],
            Chain::Base,
            1_000,
            None,
        );
        assert_eq!(
            result,
            Err(VaultError::SignerFailure(SettlementError::SignerRejected))
        );
    }

    #[test]
    fn no_receipt_is_produced_unless_requested() {
        let signer = MockSigner::new([0x01u8; 20]);
        let identity = Identity::generate(&[0x55u8; 32]).expect("generate");
        let wallet = Wallet::new(signer, identity, test_policy(1_000_000));
        assert!(wallet.last_receipt().is_none());
    }

    /// Small helper: decode a hex literal into `[u8; 32]` for the KAT test
    /// above, kept local to this test module.
    fn hex_literal(hex: &[u8]) -> [u8; 32] {
        let s = core::str::from_utf8(hex).unwrap();
        let mut out = [0u8; 32];
        for i in 0..32 {
            out[i] = u8::from_str_radix(&s[i * 2..i * 2 + 2], 16).unwrap();
        }
        out
    }
}