truthlinked-mcp 0.1.2

On-chain MCP registry, agent policy, tool-call, and private-balance primitives for TruthLinked.
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
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
//! Agent Private Balance v2 - Full Confidential System
//!
//! ## Privacy model
//!
//! On-chain storage per agent cell:
//!   COMMITMENT   = blake3(balance_le16 || nonce_le16 || blake3(ciphertext)[..16])
//!                  - binds balance, nonce, AND ciphertext together
//!   CIPHER_LO/HI = AES-256-GCM(balance, owner_aes_key, random_nonce)
//!   TOTAL_DEPOSITED, TOTAL_WITHDRAWN, TOTAL_FEES - monotonic u128 counters
//!   STAKE_VERIFIED - u8 flag set at init, re-checked every circuit
//!
//! Deposit:  public TRTH → private balance. Amount revealed (it's a public tx).
//!           Balance before/after hidden. Commitment + ciphertext updated.
//!
//! Withdraw: private balance → public TRTH. Amount revealed.
//!           Balance before/after hidden.
//!
//! Confidential Transfer: amount AND balances hidden.
//!           A Winterfell STARK proof is submitted with the tx.
//!           The proof attests: old_balance - amount = new_balance,
//!           all three in [0, MAX_PRIVATE_BALANCE], using a public
//!           Pedersen-style commitment to amount (so verifier checks
//!           the commitment without learning the value).
//!
//! ## Staking gate
//!
//! Owner must have ≥ PRIVATE_BALANCE_MIN_STAKE TRTH staked at init time.
//! The stake requirement is re-checked on every circuit call.
//! If the owner unstakes below the threshold, all circuits revert.

use aes_gcm::{
    aead::{Aead, KeyInit},
    Aes256Gcm, Key, Nonce,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use truthlinked_core::pq_execution::AccountId;
use truthlinked_governance::params as gp;
use truthlinked_runtime::types::{CellUpdate, StateDiff};
use truthlinked_staking::StakingState;

use crate::McpStateView;

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

/// 100,000 TRTH in base units (ONE_TRTH = 1_000_000_000)
pub const PRIVATE_BALANCE_MIN_STAKE: u64 = 100_000 * 1_000_000_000u64;

pub const MAX_PRIVATE_BALANCE: u128 = (1u128 << 96) - 1;
pub const MAX_TRANSFER_AMOUNT: u128 = (1u128 << 64) - 1;
pub const MAX_FEE_AMOUNT: u128 = (1u128 << 32) - 1;

const AES_NONCE_LEN: usize = 12;
/// nonce(12) + ct(16) + tag(16) = 44 bytes
pub const CIPHERTEXT_LEN: usize = 44;

// ---------------------------------------------------------------------------
// Extended McpStateView - adds staking access
// ---------------------------------------------------------------------------

/// Extended view trait that includes staking state.
/// Implement this alongside McpStateView for private balance circuits.
pub trait PrivateBalanceStateView: McpStateView {
    fn staking(&self) -> &StakingState;
}

// ---------------------------------------------------------------------------
// Storage keys
// ---------------------------------------------------------------------------

pub mod pb_keys {
    use crate::registry_keys::blake3_key;

    /// Commitment: blake3(balance_le16 || nonce_le16 || ct_hash_lo16)
    pub const COMMITMENT: [u8; 32] = key(b"pb:c");
    /// AES-GCM ciphertext bytes 0..32
    pub const CIPHER_LO: [u8; 32] = key(b"pb:l");
    /// AES-GCM ciphertext bytes 32..44 (12 bytes used)
    pub const CIPHER_HI: [u8; 32] = key(b"pb:h");
    /// Commitment nonce (u128 LE in slot[0..16])
    pub const COMMIT_NONCE: [u8; 32] = key(b"pb:n");
    /// Owner AccountId
    pub const OWNER: [u8; 32] = key(b"pb:o");
    /// Agent AccountId
    pub const AGENT: [u8; 32] = key(b"pb:a");
    /// Locked flag: 1 = initialized
    pub const LOCKED: [u8; 32] = key(b"pb:L");
    /// Stake verified flag: 1 = stake was sufficient at init
    pub const STAKE_VERIFIED: [u8; 32] = key(b"pb:sv");
    /// Total deposited (u128 LE, monotonic)
    pub const TOTAL_DEPOSITED: [u8; 32] = key(b"pb:td");
    /// Total withdrawn (u128 LE, monotonic)
    pub const TOTAL_WITHDRAWN: [u8; 32] = key(b"pb:tw");
    /// Total fees deducted (u128 LE, monotonic)
    pub const TOTAL_FEES: [u8; 32] = key(b"pb:tf");
    /// Total confidential transfers out (u128 LE, monotonic)
    pub const TOTAL_CT_OUT: [u8; 32] = key(b"pb:to");

    /// Canonical cell address for an agent
    pub fn cell_for_agent(agent_id: &[u8; 32]) -> [u8; 32] {
        blake3_key(b"pb:cell:", agent_id)
    }

    const fn key(tag: &[u8]) -> [u8; 32] {
        let mut k = [0u8; 32];
        let mut i = 0;
        while i < tag.len() && i < 32 {
            k[i] = tag[i];
            i += 1;
        }
        k
    }
}

// ---------------------------------------------------------------------------
// Commitment (now binds ciphertext too)
// ---------------------------------------------------------------------------

/// Commitment = Rescue(balance || nonce || ct_hash_lo16), serialized as 32 bytes.
///
/// Private-balance state commitments intentionally use the same Rescue-Prime
/// digest format as the confidential-transfer STARK. That keeps init, deposit,
/// withdraw, and confidential transfer on one commitment scheme and avoids a
/// proof path that can never match cells created by the normal CLI flow.
pub fn compute_commitment(balance: u128, nonce: u128, ciphertext: &[u8]) -> [u8; 32] {
    let ct_hash = blake3::hash(ciphertext);
    let mut ct_hash_lo16 = [0u8; 16];
    ct_hash_lo16.copy_from_slice(&ct_hash.as_bytes()[..16]);
    let digest = crate::zk_transfer::rescue_commit(balance, nonce, &ct_hash_lo16);
    crate::zk_transfer::digest_to_bytes(&digest)
}

pub fn verify_commitment(
    commitment: &[u8; 32],
    balance: u128,
    nonce: u128,
    ciphertext: &[u8],
) -> Result<(), String> {
    let expected = compute_commitment(balance, nonce, ciphertext);
    if expected != *commitment {
        return Err("Commitment mismatch: balance, nonce, or ciphertext does not match".into());
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Encryption helpers
// ---------------------------------------------------------------------------

pub fn derive_aes_key(seed: &[u8]) -> [u8; 32] {
    blake3::derive_key("truthlinked:private_balance:aes256gcm:v2", seed)
}

pub fn encrypt_balance(
    balance: u128,
    aes_key: &[u8; 32],
    enc_nonce: &[u8; AES_NONCE_LEN],
) -> Result<Vec<u8>, String> {
    let cipher = Aes256Gcm::new(Key::<Aes256Gcm>::from_slice(aes_key));
    let ct = cipher
        .encrypt(Nonce::from_slice(enc_nonce), balance.to_le_bytes().as_ref())
        .map_err(|e| format!("Encryption failed: {e}"))?;
    let mut out = Vec::with_capacity(CIPHERTEXT_LEN);
    out.extend_from_slice(enc_nonce);
    out.extend_from_slice(&ct);
    Ok(out)
}

pub fn decrypt_balance(ciphertext: &[u8], aes_key: &[u8; 32]) -> Result<u128, String> {
    if ciphertext.len() != CIPHERTEXT_LEN {
        return Err(format!(
            "Ciphertext must be {CIPHERTEXT_LEN} bytes, got {}",
            ciphertext.len()
        ));
    }
    let cipher = Aes256Gcm::new(Key::<Aes256Gcm>::from_slice(aes_key));
    let pt = cipher
        .decrypt(
            Nonce::from_slice(&ciphertext[..AES_NONCE_LEN]),
            &ciphertext[AES_NONCE_LEN..],
        )
        .map_err(|_| "Decryption failed: wrong key or corrupted ciphertext".to_string())?;
    if pt.len() != 16 {
        return Err("Decrypted plaintext wrong length".into());
    }
    let mut buf = [0u8; 16];
    buf.copy_from_slice(&pt);
    Ok(u128::from_le_bytes(buf))
}

pub fn read_and_decrypt_balance(
    cell: &truthlinked_runtime::cells::CellAccount,
    aes_key: &[u8; 32],
) -> Result<u128, String> {
    let lo = cell
        .storage
        .get(&pb_keys::CIPHER_LO)
        .copied()
        .unwrap_or([0u8; 32]);
    let hi = cell
        .storage
        .get(&pb_keys::CIPHER_HI)
        .copied()
        .unwrap_or([0u8; 32]);
    let mut ct = Vec::with_capacity(CIPHERTEXT_LEN);
    ct.extend_from_slice(&lo);
    ct.extend_from_slice(&hi[..12]);
    decrypt_balance(&ct, aes_key)
}

// ---------------------------------------------------------------------------
// Storage packing
// ---------------------------------------------------------------------------

fn pack_ciphertext(ct: &[u8]) -> Result<([u8; 32], [u8; 32]), String> {
    if ct.len() != CIPHERTEXT_LEN {
        return Err(format!(
            "Ciphertext must be {CIPHERTEXT_LEN} bytes, got {}",
            ct.len()
        ));
    }
    let mut lo = [0u8; 32];
    let mut hi = [0u8; 32];
    lo.copy_from_slice(&ct[..32]);
    hi[..12].copy_from_slice(&ct[32..44]);
    Ok((lo, hi))
}

fn read_commit_nonce(cell: &truthlinked_runtime::cells::CellAccount) -> u128 {
    let s = cell
        .storage
        .get(&pb_keys::COMMIT_NONCE)
        .copied()
        .unwrap_or([0u8; 32]);
    let mut b = [0u8; 16];
    b.copy_from_slice(&s[..16]);
    u128::from_le_bytes(b)
}

fn read_u128_slot(cell: &truthlinked_runtime::cells::CellAccount, key: &[u8; 32]) -> u128 {
    let s = cell.storage.get(key).copied().unwrap_or([0u8; 32]);
    let mut b = [0u8; 16];
    b.copy_from_slice(&s[..16]);
    u128::from_le_bytes(b)
}

fn pack_u128(v: u128) -> [u8; 32] {
    let mut s = [0u8; 32];
    s[..16].copy_from_slice(&v.to_le_bytes());
    s
}

fn pack_nonce(n: &[u8; 16]) -> [u8; 32] {
    let mut s = [0u8; 32];
    s[..16].copy_from_slice(n);
    s
}

// ---------------------------------------------------------------------------
// Range checks
// ---------------------------------------------------------------------------

fn check_balance(v: u128, label: &str) -> Result<(), String> {
    if v > MAX_PRIVATE_BALANCE {
        return Err(format!("{label} ({v}) exceeds MAX_PRIVATE_BALANCE"));
    }
    Ok(())
}

fn check_amount(v: u128, label: &str) -> Result<(), String> {
    if v == 0 {
        return Err(format!("{label} must be > 0"));
    }
    if v > MAX_TRANSFER_AMOUNT {
        return Err(format!("{label} ({v}) exceeds MAX_TRANSFER_AMOUNT"));
    }
    Ok(())
}

fn check_fee(v: u128) -> Result<(), String> {
    if v == 0 {
        return Err("Fee must be > 0".into());
    }
    if v > MAX_FEE_AMOUNT {
        return Err(format!("Fee ({v}) exceeds MAX_FEE_AMOUNT"));
    }
    Ok(())
}

fn check_nonce(n: &[u8; 16]) -> Result<(), String> {
    if n == &[0u8; 16] {
        return Err("Commit nonce must be non-zero".into());
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Staking gate
// ---------------------------------------------------------------------------

/// Check that the owner has >= PRIVATE_BALANCE_MIN_STAKE staked.
/// Called at the top of every circuit.
fn check_stake_gate(staking: &StakingState, owner_pubkey_bytes: &[u8]) -> Result<(), String> {
    let stake = staking
        .validators
        .get(owner_pubkey_bytes)
        .map(|v| v.active_stake)
        .unwrap_or(0);
    if stake < PRIVATE_BALANCE_MIN_STAKE {
        return Err(format!(
            "Insufficient stake: owner has {} staked, minimum required is {} (100,000 TRTH)",
            stake, PRIVATE_BALANCE_MIN_STAKE
        ));
    }
    Ok(())
}

/// Resolve owner pubkey bytes from AccountRecord for stake lookup.
fn owner_pubkey<'a>(state: &'a impl McpStateView, owner: &AccountId) -> Result<Vec<u8>, String> {
    let acc = state
        .accounts()
        .get(owner)
        .ok_or("Owner account not found")?;
    if acc.pubkey_bytes.is_empty() {
        return Err("Owner account has no registered pubkey".into());
    }
    Ok(acc.pubkey_bytes.clone())
}

// ---------------------------------------------------------------------------
// Common cell guards (used by all circuits after init)
// ---------------------------------------------------------------------------

struct CellGuard<'a> {
    cell: &'a truthlinked_runtime::cells::CellAccount,
    stored_owner: AccountId,
    _stored_agent: AccountId,
    _on_chain_commitment: [u8; 32],
    old_nonce: u128,
}

fn load_and_guard<'a>(
    state: &'a impl McpStateView,
    cell_id: &AccountId,
    agent_id: &AccountId,
    sender: &AccountId,
    old_commitment: &[u8; 32],
) -> Result<CellGuard<'a>, String> {
    let cell = state
        .cells()
        .cells
        .get(cell_id)
        .ok_or_else(|| format!("Private balance cell {} not found", hex::encode(cell_id)))?;

    if cell
        .storage
        .get(&pb_keys::LOCKED)
        .map(|b| b[0])
        .unwrap_or(0)
        != 1
    {
        return Err("Cell not initialized".into());
    }

    let stored_owner = cell
        .storage
        .get(&pb_keys::OWNER)
        .copied()
        .unwrap_or([0u8; 32]);
    let stored_agent = cell
        .storage
        .get(&pb_keys::AGENT)
        .copied()
        .unwrap_or([0u8; 32]);

    if sender != &stored_owner && sender != &stored_agent {
        return Err("Unauthorized: sender is neither owner nor agent".into());
    }
    if agent_id != &stored_agent {
        return Err("agent_id does not match cell's registered agent".into());
    }

    let on_chain_commitment = cell
        .storage
        .get(&pb_keys::COMMITMENT)
        .copied()
        .unwrap_or([0u8; 32]);
    if old_commitment != &on_chain_commitment {
        return Err("old_commitment mismatch: stale or replayed transaction".into());
    }

    let old_nonce = read_commit_nonce(cell);

    Ok(CellGuard {
        cell,
        stored_owner,
        _stored_agent: stored_agent,
        _on_chain_commitment: on_chain_commitment,
        old_nonce,
    })
}

fn check_new_nonce(new_nonce_bytes: &[u8; 16], old_nonce: u128) -> Result<u128, String> {
    check_nonce(new_nonce_bytes)?;
    let mut b = [0u8; 16];
    b.copy_from_slice(new_nonce_bytes);
    let new_nonce = u128::from_le_bytes(b);
    if new_nonce == old_nonce {
        return Err("new_commit_nonce must differ from current nonce".into());
    }
    Ok(new_nonce)
}

// ---------------------------------------------------------------------------
// Intent enum
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PrivateBalanceIntent {
    /// Owner deploys private balance cell. Requires 100k TRTH staked.
    InitPrivateBalance {
        cell_id: AccountId,
        agent_id: AccountId,
        encrypted_balance: Vec<u8>,
        commitment: [u8; 32],
        commit_nonce: [u8; 16],
    },

    /// Deposit public TRTH into private balance.
    /// Amount is revealed (it's a public debit). Balance before/after hidden.
    Deposit {
        cell_id: AccountId,
        agent_id: AccountId,
        amount: u128,
        new_encrypted_balance: Vec<u8>,
        new_commitment: [u8; 32],
        new_commit_nonce: [u8; 16],
        old_commitment: [u8; 32],
    },

    /// Withdraw from private balance to public on-chain balance.
    /// Amount is revealed. Balance before/after hidden.
    Withdraw {
        cell_id: AccountId,
        agent_id: AccountId,
        amount: u128,
        recipient: AccountId,
        new_encrypted_balance: Vec<u8>,
        new_commitment: [u8; 32],
        new_commit_nonce: [u8; 16],
        old_commitment: [u8; 32],
    },

    /// Confidential transfer: amount and private balances stay hidden.
    ///
    /// The submitted STARK proof binds sender, recipient, and amount commitments
    /// while proving the private balance conservation equations inside the AIR.
    /// The recipient receives a new encrypted balance and commitment without
    /// revealing the transferred amount on-chain.
    ConfidentialTransfer {
        sender_cell_id: AccountId,
        sender_agent_id: AccountId,
        recipient_cell_id: AccountId,
        /// Commitment to the transfer amount (hides the amount)
        amount_commitment: [u8; 32],
        /// Winterfell STARK proof bytes
        stark_proof: Vec<u8>,
        /// Sender's new encrypted balance
        sender_new_encrypted: Vec<u8>,
        sender_new_commitment: [u8; 32],
        sender_new_commit_nonce: [u8; 16],
        sender_old_commitment: [u8; 32],
        /// Recipient's new encrypted balance
        recipient_new_encrypted: Vec<u8>,
        recipient_new_commitment: [u8; 32],
        recipient_new_commit_nonce: [u8; 16],
        recipient_old_commitment: [u8; 32],
    },

    /// Fee deduction by fee authority only.
    FeeDeduct {
        cell_id: AccountId,
        agent_id: AccountId,
        fee_amount: u128,
        fee_recipient: AccountId,
        new_encrypted_balance: Vec<u8>,
        new_commitment: [u8; 32],
        new_commit_nonce: [u8; 16],
        old_commitment: [u8; 32],
    },
}

// ---------------------------------------------------------------------------
// ZK circuits moved to zk_transfer module
use crate::zk_transfer::{bytes_to_digest, verify_ct_proof, CtPublicInputs};

// ---------------------------------------------------------------------------
// Circuit 1: InitPrivateBalance
// ---------------------------------------------------------------------------

pub fn circuit_init_private_balance(
    state: &impl PrivateBalanceStateView,
    sender: AccountId,
    intent: &PrivateBalanceIntent,
    timestamp: u64,
) -> Result<StateDiff, String> {
    let (cell_id, agent_id, encrypted_balance, commitment, commit_nonce) = match intent {
        PrivateBalanceIntent::InitPrivateBalance {
            cell_id,
            agent_id,
            encrypted_balance,
            commitment,
            commit_nonce,
        } => (
            cell_id,
            agent_id,
            encrypted_balance,
            commitment,
            commit_nonce,
        ),
        _ => return Err("Wrong intent".into()),
    };

    // --- Staking gate ---
    let pubkey = owner_pubkey(state, &sender)?;
    check_stake_gate(state.staking(), &pubkey)?;

    // --- Guards ---
    if sender == *agent_id {
        return Err("Owner and agent must be different accounts".into());
    }
    if state.cells().cells.contains_key(cell_id) {
        return Err(format!("Cell {} already exists", hex::encode(cell_id)));
    }
    let expected = pb_keys::cell_for_agent(agent_id);
    if *cell_id != expected {
        return Err(format!(
            "cell_id mismatch: expected {}",
            hex::encode(expected)
        ));
    }
    if commitment == &[0u8; 32] {
        return Err("Commitment must be non-zero".into());
    }
    check_nonce(commit_nonce)?;
    if encrypted_balance.len() != CIPHERTEXT_LEN {
        return Err(format!("encrypted_balance must be {CIPHERTEXT_LEN} bytes"));
    }

    let rent = gp::get_u128(gp::PARAM_STORAGE_RENT_LIFETIME_FEE);
    let sender_acc = state.accounts().get(&sender).ok_or("Sender not found")?;
    if sender_acc.balance < rent {
        return Err("Insufficient balance for rent".into());
    }

    let (cipher_lo, cipher_hi) = pack_ciphertext(encrypted_balance)?;

    let mut storage: HashMap<[u8; 32], [u8; 32]> = HashMap::new();
    storage.insert(pb_keys::COMMITMENT, *commitment);
    storage.insert(pb_keys::CIPHER_LO, cipher_lo);
    storage.insert(pb_keys::CIPHER_HI, cipher_hi);
    storage.insert(pb_keys::COMMIT_NONCE, pack_nonce(commit_nonce));
    storage.insert(pb_keys::OWNER, sender);
    storage.insert(pb_keys::AGENT, *agent_id);
    storage.insert(pb_keys::TOTAL_DEPOSITED, [0u8; 32]);
    storage.insert(pb_keys::TOTAL_WITHDRAWN, [0u8; 32]);
    storage.insert(pb_keys::TOTAL_FEES, [0u8; 32]);
    storage.insert(pb_keys::TOTAL_CT_OUT, [0u8; 32]);
    let mut locked = [0u8; 32];
    locked[0] = 1;
    storage.insert(pb_keys::LOCKED, locked);
    let mut sv = [0u8; 32];
    sv[0] = 1;
    storage.insert(pb_keys::STAKE_VERIFIED, sv);

    let manifest_hash =
        truthlinked_runtime::cells::CellAccount::compute_manifest_hash(&[], &[], &[], &[], &[]);

    let cell = truthlinked_runtime::cells::CellAccount {
        cell_id: *cell_id,
        owner: truthlinked_core::pq_execution::system_authority_id(),
        bytecode: vec![],
        storage,
        balance: 0,
        rent_deposit: rent,
        is_token: false,
        token_config: None,
        created_at: timestamp,
        upgraded_at: None,
        last_rent_paid_height: 0,
        rent_grace_blocks: gp::get_u64(gp::PARAM_STORAGE_RENT_GRACE_PERIOD_BLOCKS),
        pending_owner: None,
        is_immutable: false,
        declared_reads: vec![
            pb_keys::COMMITMENT,
            pb_keys::COMMIT_NONCE,
            pb_keys::OWNER,
            pb_keys::AGENT,
            pb_keys::LOCKED,
            pb_keys::STAKE_VERIFIED,
        ],
        declared_writes: vec![
            pb_keys::COMMITMENT,
            pb_keys::CIPHER_LO,
            pb_keys::CIPHER_HI,
            pb_keys::COMMIT_NONCE,
            pb_keys::TOTAL_DEPOSITED,
            pb_keys::TOTAL_WITHDRAWN,
            pb_keys::TOTAL_FEES,
            pb_keys::TOTAL_CT_OUT,
        ],
        commutative_keys: vec![],
        storage_key_specs: vec![],
        oracle_schema_ids: vec![],
        governance_proposal: None,
        manifest_version: 2,
        manifest_hash,
    };

    let mut diff = StateDiff::default();
    let mut sa = sender_acc.clone();
    sa.balance = sa
        .balance
        .checked_sub(rent)
        .ok_or("Balance underflow on rent")?;
    diff.account_updates.insert(sender, sa);
    diff.native_debits.push((sender, rent));
    diff.cell_updates.push(CellUpdate::Deploy {
        cell_id: *cell_id,
        cell,
    });
    diff.cu_fee = gp::get_u64(gp::PARAM_GAS_DEPLOY_CELL) as u128;
    Ok(diff)
}

// ---------------------------------------------------------------------------
// Circuit 2: Deposit
// ---------------------------------------------------------------------------

pub fn circuit_deposit(
    state: &impl PrivateBalanceStateView,
    sender: AccountId,
    intent: &PrivateBalanceIntent,
    _ts: u64,
) -> Result<StateDiff, String> {
    let (cell_id, agent_id, amount, new_enc, new_comm, new_nonce, old_comm) = match intent {
        PrivateBalanceIntent::Deposit {
            cell_id,
            agent_id,
            amount,
            new_encrypted_balance,
            new_commitment,
            new_commit_nonce,
            old_commitment,
        } => (
            cell_id,
            agent_id,
            *amount,
            new_encrypted_balance,
            new_commitment,
            new_commit_nonce,
            old_commitment,
        ),
        _ => return Err("Wrong intent".into()),
    };

    let g = load_and_guard(state, cell_id, agent_id, &sender, old_comm)?;

    // The owner stakes for the private balance cell; the authorized agent may submit withdrawals.
    let pubkey = owner_pubkey(state, &g.stored_owner)?;
    check_stake_gate(state.staking(), &pubkey)?;

    // Only owner can deposit (it's a public debit from their account)
    if sender != g.stored_owner {
        return Err("Only the owner can deposit into a private balance cell".into());
    }

    check_amount(amount, "deposit amount")?;

    let sender_acc = state.accounts().get(&sender).ok_or("Sender not found")?;
    if sender_acc.balance < amount {
        return Err("Insufficient on-chain balance for deposit".into());
    }

    check_new_nonce(new_nonce, g.old_nonce)?;
    if new_comm == &[0u8; 32] {
        return Err("new_commitment must be non-zero".into());
    }
    if new_enc.len() != CIPHERTEXT_LEN {
        return Err(format!(
            "new_encrypted_balance must be {CIPHERTEXT_LEN} bytes"
        ));
    }

    let old_deposited = read_u128_slot(g.cell, &pb_keys::TOTAL_DEPOSITED);
    let new_deposited = old_deposited
        .checked_add(amount)
        .ok_or("TOTAL_DEPOSITED overflow")?;
    check_balance(new_deposited, "total_deposited")?;

    let (cipher_lo, cipher_hi) = pack_ciphertext(new_enc)?;

    let mut diff = StateDiff::default();

    // Debit sender's on-chain balance
    let mut sa = sender_acc.clone();
    sa.balance = sa
        .balance
        .checked_sub(amount)
        .ok_or("Sender balance underflow")?;
    diff.account_updates.insert(sender, sa);
    diff.native_debits.push((sender, amount));

    diff.cell_updates.push(CellUpdate::StorageChange {
        cell_id: *cell_id,
        storage_diff: {
            let mut m = HashMap::new();
            m.insert(pb_keys::COMMITMENT, Some(*new_comm));
            m.insert(pb_keys::CIPHER_LO, Some(cipher_lo));
            m.insert(pb_keys::CIPHER_HI, Some(cipher_hi));
            m.insert(pb_keys::COMMIT_NONCE, Some(pack_nonce(new_nonce)));
            m.insert(pb_keys::TOTAL_DEPOSITED, Some(pack_u128(new_deposited)));
            m
        },
    });

    diff.cu_fee = gp::get_u64(gp::PARAM_GAS_TRANSFER) as u128;
    Ok(diff)
}

// ---------------------------------------------------------------------------
// Circuit 3: Withdraw
// ---------------------------------------------------------------------------

pub fn circuit_withdraw(
    state: &impl PrivateBalanceStateView,
    sender: AccountId,
    intent: &PrivateBalanceIntent,
    _ts: u64,
) -> Result<StateDiff, String> {
    let (cell_id, agent_id, amount, recipient, new_enc, new_comm, new_nonce, old_comm) =
        match intent {
            PrivateBalanceIntent::Withdraw {
                cell_id,
                agent_id,
                amount,
                recipient,
                new_encrypted_balance,
                new_commitment,
                new_commit_nonce,
                old_commitment,
            } => (
                cell_id,
                agent_id,
                *amount,
                recipient,
                new_encrypted_balance,
                new_commitment,
                new_commit_nonce,
                old_commitment,
            ),
            _ => return Err("Wrong intent".into()),
        };

    let g = load_and_guard(state, cell_id, agent_id, &sender, old_comm)?;

    // The owner stakes for the private balance cell; the authorized agent may submit withdrawals.
    let pubkey = owner_pubkey(state, &g.stored_owner)?;
    check_stake_gate(state.staking(), &pubkey)?;

    check_amount(amount, "withdrawal amount")?;
    check_new_nonce(new_nonce, g.old_nonce)?;
    if new_comm == &[0u8; 32] {
        return Err("new_commitment must be non-zero".into());
    }
    if new_enc.len() != CIPHERTEXT_LEN {
        return Err(format!(
            "new_encrypted_balance must be {CIPHERTEXT_LEN} bytes"
        ));
    }

    let recipient_acc = state
        .accounts()
        .get(recipient)
        .ok_or("Recipient not found")?;
    let new_recipient_balance = recipient_acc
        .balance
        .checked_add(amount)
        .ok_or("Recipient balance overflow")?;
    check_balance(new_recipient_balance, "recipient new balance")?;

    let old_withdrawn = read_u128_slot(g.cell, &pb_keys::TOTAL_WITHDRAWN);
    let new_withdrawn = old_withdrawn
        .checked_add(amount)
        .ok_or("TOTAL_WITHDRAWN overflow")?;
    check_balance(new_withdrawn, "total_withdrawn")?;

    let (cipher_lo, cipher_hi) = pack_ciphertext(new_enc)?;

    let mut diff = StateDiff::default();

    // Credit recipient on-chain
    let mut ra = recipient_acc.clone();
    ra.balance = new_recipient_balance;
    diff.account_updates.insert(*recipient, ra);
    diff.native_transfers.push((*recipient, amount));

    diff.cell_updates.push(CellUpdate::StorageChange {
        cell_id: *cell_id,
        storage_diff: {
            let mut m = HashMap::new();
            m.insert(pb_keys::COMMITMENT, Some(*new_comm));
            m.insert(pb_keys::CIPHER_LO, Some(cipher_lo));
            m.insert(pb_keys::CIPHER_HI, Some(cipher_hi));
            m.insert(pb_keys::COMMIT_NONCE, Some(pack_nonce(new_nonce)));
            m.insert(pb_keys::TOTAL_WITHDRAWN, Some(pack_u128(new_withdrawn)));
            m
        },
    });

    diff.cu_fee = gp::get_u64(gp::PARAM_GAS_TRANSFER) as u128;
    Ok(diff)
}

// ---------------------------------------------------------------------------
// Circuit 4: ConfidentialTransfer (ZK)
// ---------------------------------------------------------------------------

pub fn circuit_confidential_transfer(
    state: &impl PrivateBalanceStateView,
    sender: AccountId,
    intent: &PrivateBalanceIntent,
    _ts: u64,
) -> Result<StateDiff, String> {
    let (
        sender_cell_id,
        sender_agent_id,
        recipient_cell_id,
        amount_commitment,
        stark_proof,
        s_new_enc,
        s_new_comm,
        s_new_nonce,
        s_old_comm,
        r_new_enc,
        r_new_comm,
        r_new_nonce,
        r_old_comm,
    ) = match intent {
        PrivateBalanceIntent::ConfidentialTransfer {
            sender_cell_id,
            sender_agent_id,
            recipient_cell_id,
            amount_commitment,
            stark_proof,
            sender_new_encrypted,
            sender_new_commitment,
            sender_new_commit_nonce,
            sender_old_commitment,
            recipient_new_encrypted,
            recipient_new_commitment,
            recipient_new_commit_nonce,
            recipient_old_commitment,
        } => (
            sender_cell_id,
            sender_agent_id,
            recipient_cell_id,
            amount_commitment,
            stark_proof,
            sender_new_encrypted,
            sender_new_commitment,
            sender_new_commit_nonce,
            sender_old_commitment,
            recipient_new_encrypted,
            recipient_new_commitment,
            recipient_new_commit_nonce,
            recipient_old_commitment,
        ),
        _ => return Err("Wrong intent".into()),
    };

    // --- Load and guard sender cell ---
    let sg = load_and_guard(state, sender_cell_id, sender_agent_id, &sender, s_old_comm)?;

    // The owner stakes for the private balance cell; the authorized agent may relay the transfer.
    let pubkey = owner_pubkey(state, &sg.stored_owner)?;
    check_stake_gate(state.staking(), &pubkey)?;

    // --- Load recipient cell ---
    let r_cell = state.cells().cells.get(recipient_cell_id).ok_or_else(|| {
        format!(
            "Recipient cell {} not found",
            hex::encode(recipient_cell_id)
        )
    })?;
    if r_cell
        .storage
        .get(&pb_keys::LOCKED)
        .map(|b| b[0])
        .unwrap_or(0)
        != 1
    {
        return Err("Recipient cell not initialized".into());
    }
    let r_on_chain_comm = r_cell
        .storage
        .get(&pb_keys::COMMITMENT)
        .copied()
        .unwrap_or([0u8; 32]);
    if r_old_comm != &r_on_chain_comm {
        return Err("recipient_old_commitment mismatch".into());
    }
    let r_old_nonce = read_commit_nonce(r_cell);

    // --- Sender != recipient ---
    if sender_cell_id == recipient_cell_id {
        return Err("Sender and recipient cells must be different".into());
    }

    // --- Nonce checks ---
    check_new_nonce(s_new_nonce, sg.old_nonce)?;
    check_new_nonce(r_new_nonce, r_old_nonce)?;

    // --- Commitment non-zero ---
    if s_new_comm == &[0u8; 32] {
        return Err("sender new_commitment must be non-zero".into());
    }
    if r_new_comm == &[0u8; 32] {
        return Err("recipient new_commitment must be non-zero".into());
    }
    if amount_commitment == &[0u8; 32] {
        return Err("amount_commitment must be non-zero".into());
    }

    // --- Ciphertext lengths ---
    if s_new_enc.len() != CIPHERTEXT_LEN {
        return Err(format!(
            "sender new_encrypted must be {CIPHERTEXT_LEN} bytes"
        ));
    }
    if r_new_enc.len() != CIPHERTEXT_LEN {
        return Err(format!(
            "recipient new_encrypted must be {CIPHERTEXT_LEN} bytes"
        ));
    }

    // --- Proof size sanity (prevent DoS via huge proof) ---
    const MAX_PROOF_BYTES: usize = 512 * 1024; // 512 KB
    if stark_proof.is_empty() {
        return Err("STARK proof is empty".into());
    }
    if stark_proof.len() > MAX_PROOF_BYTES {
        return Err(format!(
            "STARK proof too large: {} bytes (max {MAX_PROOF_BYTES})",
            stark_proof.len()
        ));
    }

    // --- Verify STARK proof (full Rescue-Prime AIR) ---
    let pub_inputs = CtPublicInputs {
        s_old: bytes_to_digest(s_old_comm),
        s_new: bytes_to_digest(s_new_comm),
        r_old: bytes_to_digest(r_old_comm),
        r_new: bytes_to_digest(r_new_comm),
        amt: bytes_to_digest(amount_commitment),
    };
    verify_ct_proof(stark_proof, &pub_inputs)?;

    // --- Update TOTAL_CT_OUT (monotonic) ---
    // We do not know the amount (it's hidden), so we increment by 1 (tx count).
    // The actual amount is proven correct by the ZK proof.
    let old_ct_out = read_u128_slot(sg.cell, &pb_keys::TOTAL_CT_OUT);
    let new_ct_out = old_ct_out.checked_add(1).ok_or("TOTAL_CT_OUT overflow")?;

    let (s_cipher_lo, s_cipher_hi) = pack_ciphertext(s_new_enc)?;
    let (r_cipher_lo, r_cipher_hi) = pack_ciphertext(r_new_enc)?;

    let mut diff = StateDiff::default();

    // Update sender cell
    diff.cell_updates.push(CellUpdate::StorageChange {
        cell_id: *sender_cell_id,
        storage_diff: {
            let mut m = HashMap::new();
            m.insert(pb_keys::COMMITMENT, Some(*s_new_comm));
            m.insert(pb_keys::CIPHER_LO, Some(s_cipher_lo));
            m.insert(pb_keys::CIPHER_HI, Some(s_cipher_hi));
            m.insert(pb_keys::COMMIT_NONCE, Some(pack_nonce(s_new_nonce)));
            m.insert(pb_keys::TOTAL_CT_OUT, Some(pack_u128(new_ct_out)));
            m
        },
    });

    // Update recipient cell
    diff.cell_updates.push(CellUpdate::StorageChange {
        cell_id: *recipient_cell_id,
        storage_diff: {
            let mut m = HashMap::new();
            m.insert(pb_keys::COMMITMENT, Some(*r_new_comm));
            m.insert(pb_keys::CIPHER_LO, Some(r_cipher_lo));
            m.insert(pb_keys::CIPHER_HI, Some(r_cipher_hi));
            m.insert(pb_keys::COMMIT_NONCE, Some(pack_nonce(r_new_nonce)));
            m
        },
    });

    diff.cu_fee = gp::get_u64(gp::PARAM_GAS_TRANSFER) as u128 * 3;
    Ok(diff)
}

// ---------------------------------------------------------------------------
// Circuit 5: FeeDeduct
// ---------------------------------------------------------------------------

pub fn circuit_fee_deduct(
    state: &impl PrivateBalanceStateView,
    sender: AccountId,
    intent: &PrivateBalanceIntent,
    _ts: u64,
) -> Result<StateDiff, String> {
    let (cell_id, agent_id, fee_amount, fee_recipient, new_enc, new_comm, new_nonce, old_comm) =
        match intent {
            PrivateBalanceIntent::FeeDeduct {
                cell_id,
                agent_id,
                fee_amount,
                fee_recipient,
                new_encrypted_balance,
                new_commitment,
                new_commit_nonce,
                old_commitment,
            } => (
                cell_id,
                agent_id,
                *fee_amount,
                fee_recipient,
                new_encrypted_balance,
                new_commitment,
                new_commit_nonce,
                old_commitment,
            ),
            _ => return Err("Wrong intent".into()),
        };

    // Fee authority check
    let fee_authority = gp::get_bytes32(gp::PARAM_FEE_AUTHORITY);
    if sender != fee_authority {
        return Err("Only the fee authority can deduct private fees".into());
    }

    let g = load_and_guard(state, cell_id, agent_id, &sender, old_comm)?;

    check_fee(fee_amount)?;
    let gov_max = gp::get_u128(gp::PARAM_MAX_PRIVATE_FEE);
    if fee_amount > gov_max {
        return Err(format!("Fee {fee_amount} exceeds governance max {gov_max}"));
    }

    check_new_nonce(new_nonce, g.old_nonce)?;
    if new_comm == &[0u8; 32] {
        return Err("new_commitment must be non-zero".into());
    }
    if new_enc.len() != CIPHERTEXT_LEN {
        return Err(format!(
            "new_encrypted_balance must be {CIPHERTEXT_LEN} bytes"
        ));
    }

    let fee_rec_acc = state
        .accounts()
        .get(fee_recipient)
        .ok_or("Fee recipient not found")?;
    let new_fee_rec_bal = fee_rec_acc
        .balance
        .checked_add(fee_amount)
        .ok_or("Fee recipient overflow")?;
    check_balance(new_fee_rec_bal, "fee recipient balance")?;

    let old_fees = read_u128_slot(g.cell, &pb_keys::TOTAL_FEES);
    let new_fees = old_fees
        .checked_add(fee_amount)
        .ok_or("TOTAL_FEES overflow")?;
    check_balance(new_fees, "total_fees")?;

    let (cipher_lo, cipher_hi) = pack_ciphertext(new_enc)?;

    let mut diff = StateDiff::default();

    let mut fra = fee_rec_acc.clone();
    fra.balance = new_fee_rec_bal;
    diff.account_updates.insert(*fee_recipient, fra);

    diff.cell_updates.push(CellUpdate::StorageChange {
        cell_id: *cell_id,
        storage_diff: {
            let mut m = HashMap::new();
            m.insert(pb_keys::COMMITMENT, Some(*new_comm));
            m.insert(pb_keys::CIPHER_LO, Some(cipher_lo));
            m.insert(pb_keys::CIPHER_HI, Some(cipher_hi));
            m.insert(pb_keys::COMMIT_NONCE, Some(pack_nonce(new_nonce)));
            m.insert(pb_keys::TOTAL_FEES, Some(pack_u128(new_fees)));
            m
        },
    });

    diff.cu_fee = gp::get_u64(gp::PARAM_GAS_TRANSFER) as u128;
    Ok(diff)
}

// ---------------------------------------------------------------------------
// Conflict domain
// ---------------------------------------------------------------------------

pub fn conflict_domain(
    intent: &PrivateBalanceIntent,
) -> (
    Vec<truthlinked_runtime::compiler_aware::StorageKey>,
    Vec<truthlinked_runtime::compiler_aware::StorageKey>,
) {
    use truthlinked_runtime::compiler_aware::StorageKey;

    let cell_reads = |cid: &AccountId| {
        vec![
            StorageKey::CellStorage(*cid, pb_keys::COMMITMENT),
            StorageKey::CellStorage(*cid, pb_keys::COMMIT_NONCE),
            StorageKey::CellStorage(*cid, pb_keys::LOCKED),
            StorageKey::CellStorage(*cid, pb_keys::OWNER),
            StorageKey::CellStorage(*cid, pb_keys::AGENT),
        ]
    };
    let cell_writes = |cid: &AccountId| {
        vec![
            StorageKey::CellStorage(*cid, pb_keys::COMMITMENT),
            StorageKey::CellStorage(*cid, pb_keys::CIPHER_LO),
            StorageKey::CellStorage(*cid, pb_keys::CIPHER_HI),
            StorageKey::CellStorage(*cid, pb_keys::COMMIT_NONCE),
        ]
    };

    match intent {
        PrivateBalanceIntent::InitPrivateBalance { cell_id, .. } => (vec![], cell_writes(cell_id)),

        PrivateBalanceIntent::Deposit { cell_id, .. } => {
            let mut w = cell_writes(cell_id);
            w.push(StorageKey::CellStorage(*cell_id, pb_keys::TOTAL_DEPOSITED));
            (cell_reads(cell_id), w)
        }

        PrivateBalanceIntent::Withdraw { cell_id, .. } => {
            let mut w = cell_writes(cell_id);
            w.push(StorageKey::CellStorage(*cell_id, pb_keys::TOTAL_WITHDRAWN));
            (cell_reads(cell_id), w)
        }

        PrivateBalanceIntent::ConfidentialTransfer {
            sender_cell_id,
            recipient_cell_id,
            ..
        } => {
            let mut r = cell_reads(sender_cell_id);
            r.extend(cell_reads(recipient_cell_id));
            let mut w = cell_writes(sender_cell_id);
            w.extend(cell_writes(recipient_cell_id));
            w.push(StorageKey::CellStorage(
                *sender_cell_id,
                pb_keys::TOTAL_CT_OUT,
            ));
            (r, w)
        }

        PrivateBalanceIntent::FeeDeduct { cell_id, .. } => {
            let mut w = cell_writes(cell_id);
            w.push(StorageKey::CellStorage(*cell_id, pb_keys::TOTAL_FEES));
            (cell_reads(cell_id), w)
        }
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use im::HashMap as ImHashMap;
    use truthlinked_runtime::cells::CellState;
    use truthlinked_runtime::types::AccountRecord;
    use truthlinked_staking::{StakingState, ValidatorStake};

    struct TestState {
        cells: CellState,
        accounts: ImHashMap<AccountId, AccountRecord>,
        params: ImHashMap<[u8; 32], [u8; 32]>,
        staking: StakingState,
    }

    impl McpStateView for TestState {
        fn cells(&self) -> &CellState {
            &self.cells
        }
        fn accounts(&self) -> &ImHashMap<AccountId, AccountRecord> {
            &self.accounts
        }
    }

    impl PrivateBalanceStateView for TestState {
        fn staking(&self) -> &StakingState {
            &self.staking
        }
    }

    impl truthlinked_governance::params::ParamState for TestState {
        fn params(&self) -> &ImHashMap<[u8; 32], [u8; 32]> {
            &self.params
        }
        fn params_mut(&mut self) -> &mut ImHashMap<[u8; 32], [u8; 32]> {
            &mut self.params
        }
    }

    fn make_state() -> TestState {
        let mut s = TestState {
            cells: CellState::new(),
            accounts: ImHashMap::new(),
            params: ImHashMap::new(),
            staking: StakingState::new(),
        };
        truthlinked_governance::params::insert_genesis_params(&mut s);
        truthlinked_governance::params::rehydrate_from_state(&s);
        s
    }

    fn add_account(s: &mut TestState, id: AccountId, balance: u128) {
        s.accounts.insert(
            id,
            AccountRecord {
                pubkey_bytes: id.to_vec(), // pubkey = id bytes for test simplicity
                balance,
                compute_escrow_trth: 0,
                nonce: 0,
                nfts: vec![],
            },
        );
    }

    fn stake_owner(s: &mut TestState, owner: AccountId) {
        // pubkey_bytes = owner id bytes (set in add_account)
        s.staking.validators.insert(
            owner.to_vec(),
            ValidatorStake {
                active_stake: PRIVATE_BALANCE_MIN_STAKE,
                unbonding: vec![],
                jailed_until: None,
            },
        );
    }

    fn dummy_ct() -> Vec<u8> {
        vec![0xABu8; CIPHERTEXT_LEN]
    }
    fn dummy_comm() -> [u8; 32] {
        [0x01u8; 32]
    }
    fn dummy_nonce() -> [u8; 16] {
        [0x02u8; 16]
    }

    fn do_init(s: &mut TestState, owner: AccountId, agent: AccountId) -> AccountId {
        let cell_id = pb_keys::cell_for_agent(&agent);
        let rent = gp::get_u128(gp::PARAM_STORAGE_RENT_LIFETIME_FEE);
        add_account(s, owner, rent * 10 + 1_000_000_000_000);
        stake_owner(s, owner);

        let intent = PrivateBalanceIntent::InitPrivateBalance {
            cell_id,
            agent_id: agent,
            encrypted_balance: dummy_ct(),
            commitment: dummy_comm(),
            commit_nonce: dummy_nonce(),
        };
        let diff = circuit_init_private_balance(s, owner, &intent, 0).unwrap();
        for u in diff.cell_updates {
            if let CellUpdate::Deploy { cell_id: cid, cell } = u {
                s.cells.cells.insert(cid, cell);
            }
        }
        for (id, acc) in diff.account_updates {
            s.accounts.insert(id, acc);
        }
        cell_id
    }

    // --- Staking gate ---

    #[test]
    fn init_rejects_unstaked_owner() {
        let mut s = make_state();
        let owner = [1u8; 32];
        let agent = [2u8; 32];
        let rent = gp::get_u128(gp::PARAM_STORAGE_RENT_LIFETIME_FEE);
        add_account(&mut s, owner, rent * 10);
        // no stake_owner call

        let cell_id = pb_keys::cell_for_agent(&agent);
        let intent = PrivateBalanceIntent::InitPrivateBalance {
            cell_id,
            agent_id: agent,
            encrypted_balance: dummy_ct(),
            commitment: dummy_comm(),
            commit_nonce: dummy_nonce(),
        };
        let err = circuit_init_private_balance(&s, owner, &intent, 0).unwrap_err();
        assert!(err.contains("Insufficient stake"), "got: {err}");
    }

    #[test]
    fn deposit_rejects_unstaked_owner() {
        let mut s = make_state();
        let owner = [1u8; 32];
        let agent = [2u8; 32];
        let cell_id = do_init(&mut s, owner, agent);

        // Remove stake
        s.staking.validators.remove(&owner.to_vec());

        let intent = PrivateBalanceIntent::Deposit {
            cell_id,
            agent_id: agent,
            amount: 1000,
            new_encrypted_balance: dummy_ct(),
            new_commitment: [0x03u8; 32],
            new_commit_nonce: [0x05u8; 16],
            old_commitment: dummy_comm(),
        };
        let err = circuit_deposit(&s, owner, &intent, 0).unwrap_err();
        assert!(err.contains("Insufficient stake"), "got: {err}");
    }

    // --- Init guards ---

    #[test]
    fn init_rejects_self_register() {
        let mut s = make_state();
        let owner = [1u8; 32];
        let rent = gp::get_u128(gp::PARAM_STORAGE_RENT_LIFETIME_FEE);
        add_account(&mut s, owner, rent * 10);
        stake_owner(&mut s, owner);

        let cell_id = pb_keys::cell_for_agent(&owner);
        let intent = PrivateBalanceIntent::InitPrivateBalance {
            cell_id,
            agent_id: owner,
            encrypted_balance: dummy_ct(),
            commitment: dummy_comm(),
            commit_nonce: dummy_nonce(),
        };
        let err = circuit_init_private_balance(&s, owner, &intent, 0).unwrap_err();
        assert!(err.contains("different accounts"), "got: {err}");
    }

    #[test]
    fn init_rejects_wrong_cell_id() {
        let mut s = make_state();
        let owner = [1u8; 32];
        let agent = [2u8; 32];
        let rent = gp::get_u128(gp::PARAM_STORAGE_RENT_LIFETIME_FEE);
        add_account(&mut s, owner, rent * 10);
        stake_owner(&mut s, owner);

        let intent = PrivateBalanceIntent::InitPrivateBalance {
            cell_id: [0xFFu8; 32],
            agent_id: agent,
            encrypted_balance: dummy_ct(),
            commitment: dummy_comm(),
            commit_nonce: dummy_nonce(),
        };
        let err = circuit_init_private_balance(&s, owner, &intent, 0).unwrap_err();
        assert!(err.contains("cell_id mismatch"), "got: {err}");
    }

    #[test]
    fn init_succeeds() {
        let mut s = make_state();
        let owner = [1u8; 32];
        let agent = [2u8; 32];
        let cell_id = do_init(&mut s, owner, agent);
        assert!(s.cells.cells.contains_key(&cell_id));
        let cell = &s.cells.cells[&cell_id];
        assert_eq!(cell.storage[&pb_keys::LOCKED][0], 1);
        assert_eq!(cell.storage[&pb_keys::STAKE_VERIFIED][0], 1);
    }

    // --- Deposit ---

    #[test]
    fn deposit_rejects_zero_amount() {
        let mut s = make_state();
        let owner = [1u8; 32];
        let agent = [2u8; 32];
        let cell_id = do_init(&mut s, owner, agent);
        stake_owner(&mut s, owner);

        let intent = PrivateBalanceIntent::Deposit {
            cell_id,
            agent_id: agent,
            amount: 0,
            new_encrypted_balance: dummy_ct(),
            new_commitment: [0x03u8; 32],
            new_commit_nonce: [0x05u8; 16],
            old_commitment: dummy_comm(),
        };
        let err = circuit_deposit(&s, owner, &intent, 0).unwrap_err();
        assert!(err.contains("must be > 0"), "got: {err}");
    }

    #[test]
    fn deposit_rejects_stale_commitment() {
        let mut s = make_state();
        let owner = [1u8; 32];
        let agent = [2u8; 32];
        let cell_id = do_init(&mut s, owner, agent);
        stake_owner(&mut s, owner);

        let intent = PrivateBalanceIntent::Deposit {
            cell_id,
            agent_id: agent,
            amount: 1000,
            new_encrypted_balance: dummy_ct(),
            new_commitment: [0x03u8; 32],
            new_commit_nonce: [0x05u8; 16],
            old_commitment: [0xFFu8; 32], // wrong
        };
        let err = circuit_deposit(&s, owner, &intent, 0).unwrap_err();
        assert!(err.contains("old_commitment mismatch"), "got: {err}");
    }

    #[test]
    fn deposit_rejects_insufficient_balance() {
        let mut s = make_state();
        let owner = [1u8; 32];
        let agent = [2u8; 32];
        let cell_id = do_init(&mut s, owner, agent);
        stake_owner(&mut s, owner);
        // drain owner balance
        s.accounts.get_mut(&owner).unwrap().balance = 0;

        let intent = PrivateBalanceIntent::Deposit {
            cell_id,
            agent_id: agent,
            amount: 1_000_000,
            new_encrypted_balance: dummy_ct(),
            new_commitment: [0x03u8; 32],
            new_commit_nonce: [0x05u8; 16],
            old_commitment: dummy_comm(),
        };
        let err = circuit_deposit(&s, owner, &intent, 0).unwrap_err();
        assert!(err.contains("Insufficient"), "got: {err}");
    }

    #[test]
    fn deposit_succeeds_and_debits_sender() {
        let mut s = make_state();
        let owner = [1u8; 32];
        let agent = [2u8; 32];
        let cell_id = do_init(&mut s, owner, agent);
        stake_owner(&mut s, owner);
        let before = s.accounts[&owner].balance;

        let amount = 500_000_000u128;
        let intent = PrivateBalanceIntent::Deposit {
            cell_id,
            agent_id: agent,
            amount,
            new_encrypted_balance: dummy_ct(),
            new_commitment: [0x03u8; 32],
            new_commit_nonce: [0x05u8; 16],
            old_commitment: dummy_comm(),
        };
        let diff = circuit_deposit(&s, owner, &intent, 0).unwrap();
        let new_bal = diff.account_updates[&owner].balance;
        assert_eq!(new_bal, before - amount);
    }

    // --- Withdraw ---

    #[test]
    fn withdraw_rejects_same_nonce() {
        let mut s = make_state();
        let owner = [1u8; 32];
        let agent = [2u8; 32];
        let cell_id = do_init(&mut s, owner, agent);
        stake_owner(&mut s, owner);
        let recipient = [5u8; 32];
        add_account(&mut s, recipient, 0);

        let intent = PrivateBalanceIntent::Withdraw {
            cell_id,
            agent_id: agent,
            amount: 100,
            recipient,
            new_encrypted_balance: dummy_ct(),
            new_commitment: [0x03u8; 32],
            new_commit_nonce: dummy_nonce(), // same as init nonce
            old_commitment: dummy_comm(),
        };
        let err = circuit_withdraw(&s, owner, &intent, 0).unwrap_err();
        assert!(err.contains("nonce"), "got: {err}");
    }

    #[test]
    fn withdraw_credits_recipient() {
        let mut s = make_state();
        let owner = [1u8; 32];
        let agent = [2u8; 32];
        let cell_id = do_init(&mut s, owner, agent);
        stake_owner(&mut s, owner);
        let recipient = [5u8; 32];
        add_account(&mut s, recipient, 0);

        let amount = 999u128;
        let intent = PrivateBalanceIntent::Withdraw {
            cell_id,
            agent_id: agent,
            amount,
            recipient,
            new_encrypted_balance: dummy_ct(),
            new_commitment: [0x03u8; 32],
            new_commit_nonce: [0x05u8; 16],
            old_commitment: dummy_comm(),
        };
        let diff = circuit_withdraw(&s, owner, &intent, 0).unwrap();
        assert_eq!(diff.account_updates[&recipient].balance, amount);
    }

    // --- Encryption round-trip ---

    #[test]
    fn encrypt_decrypt_roundtrip() {
        let key = derive_aes_key(b"test_seed_32bytes_exactly_padded!");
        let balance = 123_456_789_000u128;
        let nonce = [0x42u8; AES_NONCE_LEN];
        let ct = encrypt_balance(balance, &key, &nonce).unwrap();
        assert_eq!(ct.len(), CIPHERTEXT_LEN);
        assert_eq!(decrypt_balance(&ct, &key).unwrap(), balance);
    }

    #[test]
    fn wrong_key_fails_decryption() {
        let key = derive_aes_key(b"correct_seed_32bytes_exactly____");
        let bad = derive_aes_key(b"wrong___seed_32bytes_exactly____");
        let ct = encrypt_balance(42, &key, &[0x11u8; AES_NONCE_LEN]).unwrap();
        assert!(decrypt_balance(&ct, &bad).is_err());
    }

    // --- Commitment binding ---

    #[test]
    fn commitment_binds_ciphertext() {
        let ct1 = vec![0xAAu8; CIPHERTEXT_LEN];
        let ct2 = vec![0xBBu8; CIPHERTEXT_LEN];
        let c1 = compute_commitment(100, 1, &ct1);
        let c2 = compute_commitment(100, 1, &ct2);
        assert_ne!(
            c1, c2,
            "Different ciphertexts must produce different commitments"
        );
    }

    #[test]
    fn commitment_verify_ok() {
        let ct = dummy_ct();
        let c = compute_commitment(500, 7, &ct);
        verify_commitment(&c, 500, 7, &ct).unwrap();
    }

    #[test]
    fn commitment_verify_wrong_balance_fails() {
        let ct = dummy_ct();
        let c = compute_commitment(500, 7, &ct);
        assert!(verify_commitment(&c, 501, 7, &ct).is_err());
    }

    // --- Range checks ---

    #[test]
    fn range_check_balance_overflow() {
        assert!(check_balance(MAX_PRIVATE_BALANCE + 1, "x").is_err());
    }

    #[test]
    fn range_check_amount_zero() {
        assert!(check_amount(0, "x").is_err());
    }

    #[test]
    fn range_check_amount_overflow() {
        assert!(check_amount(MAX_TRANSFER_AMOUNT + 1, "x").is_err());
    }

    #[test]
    fn range_check_fee_overflow() {
        assert!(check_fee(MAX_FEE_AMOUNT + 1).is_err());
    }
}