Source file tx_rollup.ml
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
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
module Constants = Constants
module Types = Types
module Utils = Utils
open Utils
open Plompiler
module HashPV = Anemoi128
module MerklePV = Gadget.Merkle (HashPV)
module SchnorrPV = Plompiler.Schnorr (HashPV)
module Hash = HashPV.P
module Merkle = MerklePV.P
module Schnorr = SchnorrPV.P
module Curve = Mec.Curve.Jubjub.AffineEdwards
module P = struct
open Types.P
open Constants
let compression_bound (values : unit Bounded.t list) =
let values = (values :> (Z.t * Z.t) list) in
List.fold_left Z.mul Z.one (List.map snd values)
let compress (values : unit Bounded.t list) =
assert (compression_bound values < S.order) ;
let values = (values :> (Z.t * Z.t) list) in
List.fold_left
(fun acc (v, v_bound) -> Z.(v + (acc * v_bound)))
(fst @@ List.hd values)
(List.tl values)
let scalar_of_account (acc : account) =
let u = Curve.get_u_coordinate acc.pk |> of_bls_scalar in
let compressed = compress Bounded.[f acc.tez_balance; f acc.cnt] in
let h = Hash.direct ~input_length:2 [|u; S.of_z compressed|] in
S.add h acc.tickets_root
let scalar_of_leaf (l : leaf) =
let compressed = compress Bounded.[f l.pos; f l.ticket.amount] in
Hash.direct ~input_length:2 [|l.ticket.id; S.of_z compressed|]
let default_leaf pos =
{
pos = Bounded.make ~bound:Bound.max_nb_leaves (Z.of_int pos);
ticket = Dummy.ticket_balance;
}
let empty_ticket_tree start_pos =
let size = max_nb_tickets in
let leaves = Array.init size (fun i -> default_leaf (i + start_pos)) in
( leaves,
Merkle.generate_tree
~leaves:(Array.map scalar_of_leaf leaves)
tickets_depth )
let default_account acc_index =
let start_pos = max_nb_tickets * acc_index in
let leaves, ticket_tree = empty_ticket_tree start_pos in
let tickets_root = Merkle.root ticket_tree in
( {
pk = Curve.one;
tez_balance = Bounded.make ~bound:Bound.max_balance Z.zero;
cnt = Bounded.make ~bound:Bound.max_counter Z.zero;
tickets_root;
},
leaves,
ticket_tree )
let get_account :
int ->
(account * leaf array * Merkle.tree) IMap.t ->
account * leaf array * Merkle.tree =
fun i accs ->
IMap.find_opt i accs |> Option.value ~default:(default_account i)
let random_leaf pos =
let id = S.random () in
let amount = Bounded.random Bound.max_balance in
{pos; ticket = {id; amount}}
let random_ticket_tree start_pos =
let size = max_nb_tickets in
let leaves =
Array.init size (fun i ->
random_leaf
(Bounded.make ~bound:Bound.max_nb_leaves
@@ Z.of_int @@ (i + start_pos)))
in
( Merkle.generate_tree
~leaves:(Array.map scalar_of_leaf leaves)
tickets_depth,
leaves )
let random_account sks i =
let open Bound in
let tez_balance = Bounded.make Z.(v max_balance / two) ~bound:max_balance in
let pk = Schnorr.neuterize sks.(i) in
let cnt = Bounded.random max_counter in
let start_pos = max_nb_tickets * i in
let ticket_tree, leaves = random_ticket_tree start_pos in
let tickets_root = Merkle.root ticket_tree in
({tez_balance; pk; cnt; tickets_root}, leaves, ticket_tree)
let random_state sks () =
let size = 1 + random_int (max_nb_accounts - 1) in
let next_index = random_int max_nb_accounts in
let next_position = max_nb_tickets * next_index in
let indices = List.init size (fun _ -> random_int max_nb_accounts) in
let indices = List.filter (fun i -> next_index <> i) indices in
let accs_list = List.map (random_account sks) indices in
let indexed_accounts = List.combine indices accs_list in
let accounts = IMap.of_seq (List.to_seq indexed_accounts) in
let account_scalars =
Array.init max_nb_accounts (fun i ->
scalar_of_account
(let x, _, _ = default_account i in
x))
in
let () =
List.iter
(fun (i, (acc, _, _)) -> account_scalars.(i) <- scalar_of_account acc)
indexed_accounts
in
let accounts_tree =
Merkle.generate_tree ~leaves:account_scalars accounts_depth
in
{accounts; accounts_tree; next_position}
let empty_state () =
let accounts = Array.init max_nb_accounts (fun i -> default_account i) in
let accounts_tree =
Merkle.generate_tree
~leaves:(Array.map (fun (a, _, _) -> scalar_of_account a) accounts)
accounts_depth
in
let accounts = Array.mapi (fun i x -> (i, x)) accounts in
let accounts = IMap.of_seq @@ Array.to_seq accounts in
{accounts; accounts_tree; next_position = 0}
let make_state (bals : (Schnorr.pk * Z.t * balance ticket array) list) =
let open Bound in
let s = empty_state () in
let _, accounts, accounts_tree =
List.fold_left
(fun (i, accounts, accounts_tree) (pk, tez_bal, tickets) ->
let acc, leaves, _tree = get_account i accounts in
let leaves =
Array.mapi
(fun i {pos; ticket} ->
let ticket =
try tickets.(i) with Invalid_argument _ -> ticket
in
{pos; ticket})
leaves
in
let tree =
Merkle.generate_tree
~leaves:(Array.map scalar_of_leaf leaves)
tickets_depth
in
let acc =
{
acc with
tez_balance = Bounded.make ~bound:max_balance tez_bal;
pk;
tickets_root = Merkle.root tree;
}
in
let accounts = IMap.add i (acc, leaves, tree) accounts in
let accounts_tree =
Merkle.update_tree
~input_length:2
accounts_tree
i
(scalar_of_account acc)
in
(i + 1, accounts, accounts_tree))
(0, s.accounts, s.accounts_tree)
bals
in
{accounts; accounts_tree; next_position = List.length bals * max_nb_tickets}
let coerce (type a) (x : a Bounded.t) = fst (x : a Bounded.t :> Z.t * Z.t)
let hash_op (t : unsigned_tx) =
let module Curve = Mec.Curve.Jubjub.AffineEdwards in
let module S = Bls12_381.Fr in
match t with
| Transfer {; payload = {src; dst; fee; amount; cnt}} ->
let compressed_msg =
compress
Bounded.
[
f header.op_code;
f header.price.amount;
f src;
f dst;
f fee;
f amount.amount;
f cnt;
]
in
Hash.direct
~input_length:4
[|
scalar_of_bytes header.l1_dst;
S.of_z compressed_msg;
header.price.id;
amount.id;
|]
| Create {; payload = {pk; fee}} ->
let compressed_msg =
compress Bounded.[f header.op_code; f header.price.amount; f fee]
in
let pk_x, pk_y = affine_to_point pk in
Hash.direct
~input_length:5
[|
scalar_of_bytes header.l1_dst;
S.of_z compressed_msg;
pk_x;
pk_y;
header.price.id;
|]
| Credit {; payload = {dst; amount; cnt}} ->
let compressed_msg =
compress
Bounded.
[
f header.op_code;
f header.price.amount;
f dst;
f amount.amount;
f cnt;
]
in
Hash.direct
~input_length:4
[|
scalar_of_bytes header.l1_dst;
S.of_z compressed_msg;
header.price.id;
amount.id;
|]
| Debit {; payload = {src; amount; cnt}} ->
let compressed_msg =
compress
Bounded.
[
f header.op_code;
f header.price.amount;
f src;
f amount.amount;
f cnt;
]
in
Hash.direct
~input_length:4
[|
scalar_of_bytes header.l1_dst;
S.of_z compressed_msg;
header.price.id;
amount.id;
|]
let sign_op sk (t : unsigned_tx) : tx =
let module Curve = Mec.Curve.Jubjub.AffineEdwards in
let module S = Bls12_381.Fr in
let msg = hash_op t in
match t with
| Transfer {; payload = {src; dst; fee; amount; cnt}} ->
let signature =
let random = Curve.Scalar.random () in
Schnorr.sign ~compressed:true sk msg random
in
Transfer
{header; payload = {msg = {src; dst; fee; amount; cnt}; signature}}
| Create {; payload = {pk; fee}} ->
let signature =
let random = Curve.Scalar.random () in
Schnorr.sign ~compressed:true sk msg random
in
Create {header; payload = {msg = {pk; fee}; signature}}
| Debit {; payload = {src; amount; cnt}} ->
let signature =
let random = Curve.Scalar.random () in
Schnorr.sign ~compressed:true sk msg random
in
Debit {header; payload = {msg = {src; amount; cnt}; signature}}
| Credit t -> Credit t
let preprocess_operation :
state -> tx -> tezos_zkru -> state * tx * tx_storage option =
fun s tx rollup_id ->
match tx with
| Transfer
({; payload = {msg = {cnt; src; dst; amount; fee}; signature}} as
op) ->
let msg =
hash_op @@ Transfer {header; payload = {cnt; src; dst; amount; fee}}
in
let well_formed =
Bounded.(
check cnt && check src && check dst && check amount.amount
&& check fee)
in
if not well_formed then (s, Transfer op, None)
else
let src_index = Z.to_int (coerce src) / Constants.max_nb_tickets in
let src_offset = Z.to_int (coerce src) mod Constants.max_nb_tickets in
let dst_index = Z.to_int (coerce dst) / Constants.max_nb_tickets in
let dst_offset = Z.to_int (coerce dst) mod Constants.max_nb_tickets in
let src_account, src_leaves, src_tree =
get_account src_index s.accounts
in
let src_leaf = src_leaves.(src_offset) in
let is_tez = amount.id = Constants.tez_id in
let ticket_amount =
if is_tez then Bounded.make ~bound:Constants.Bound.max_amount Z.zero
else amount.amount
in
let tez_transfer_amount =
if is_tez then amount.amount
else Bounded.make ~bound:Constants.Bound.max_amount Z.zero
in
let tez_amount =
Bounded.add_left ~unsafe:true tez_transfer_amount fee
in
let new_tez_amount_src =
Bounded.(sub_left ~unsafe:true src_account.tez_balance tez_amount)
in
let new_ticket_amount_src =
Bounded.(sub_left ~unsafe:true src_leaf.ticket.amount ticket_amount)
in
let dst_account, dst_leaves, _dst_tree =
if dst_index = src_index then (
let new_ticket_src =
{src_leaf.ticket with amount = new_ticket_amount_src}
in
let new_leaf_src = {src_leaf with ticket = new_ticket_src} in
src_leaves.(src_offset) <- new_leaf_src ;
let src_tree =
Merkle.update_tree
~input_length:2
src_tree
src_offset
(scalar_of_leaf new_leaf_src)
in
let new_cnt_src = Bounded.succ ~unsafe:true src_account.cnt in
let new_acc_src =
{
src_account with
tez_balance = new_tez_amount_src;
cnt = new_cnt_src;
tickets_root = Merkle.root src_tree;
}
in
(new_acc_src, src_leaves, src_tree))
else get_account dst_index s.accounts
in
let dst_leaf = dst_leaves.(dst_offset) in
let new_tez_amount_dst =
Bounded.(
add_left ~unsafe:true dst_account.tez_balance tez_transfer_amount)
in
let new_ticket_amount_dst =
Bounded.(add_left ~unsafe:true dst_leaf.ticket.amount ticket_amount)
in
let check_counter = src_account.cnt < cnt in
let check_signature =
Schnorr.verify
~compressed:true
~msg
~pk:src_account.pk
~signature
()
in
let check_balances =
Z.(coerce new_tez_amount_src >= zero)
&& Z.(coerce new_ticket_amount_src >= zero)
&& Bounded.check new_tez_amount_dst
&& Bounded.check new_ticket_amount_dst
in
let check_rollup_id = rollup_id = header.rollup_id in
let check_ticket_ids =
let check_src = is_tez || amount.id = src_leaf.ticket.id in
let check_dst =
is_tez
|| Z.(Bounded.v @@ dst_leaf.ticket.amount = zero)
|| amount.id = dst_leaf.ticket.id
in
check_src && check_dst
in
let check_dst_pk = dst_account.pk <> Curve.one in
let check_price = Z.(zero = Bounded.v header.price.amount) in
let valid =
check_counter && check_signature && check_balances && check_dst_pk
&& check_ticket_ids && check_price && check_rollup_id
in
let src_proof, s =
let _, path = Merkle.proof_path src_index s.accounts_tree in
let s =
if valid then (
let new_ticket_src =
{src_leaf.ticket with amount = new_ticket_amount_src}
in
let new_leaf_src = {src_leaf with ticket = new_ticket_src} in
src_leaves.(src_offset) <- new_leaf_src ;
let src_tree =
Merkle.update_tree
~input_length:2
src_tree
src_offset
(scalar_of_leaf new_leaf_src)
in
let new_cnt_src = Bounded.succ ~unsafe:true src_account.cnt in
let new_acc_src =
{
src_account with
tez_balance = new_tez_amount_src;
cnt = new_cnt_src;
tickets_root = Merkle.root src_tree;
}
in
let accounts =
IMap.add
src_index
(new_acc_src, src_leaves, src_tree)
s.accounts
in
let accounts_tree =
Merkle.update_tree
~input_length:2
s.accounts_tree
src_index
(scalar_of_account new_acc_src)
in
{s with accounts_tree; accounts})
else s
in
let root = Merkle.root s.accounts_tree in
({path; root}, s)
in
let acc_after_src, leaves_after_src, tree_after_src =
get_account src_index s.accounts
in
let leaf_after_src = leaves_after_src.(src_offset) in
let _, src_leaf_path = Merkle.proof_path src_offset tree_after_src in
let src =
{
account =
{before = src_account; after = acc_after_src; proof = src_proof};
leaf =
{
before = src_leaf;
after = leaf_after_src;
path = src_leaf_path;
};
}
in
let dst_account, dst_leaves, dst_tree =
get_account dst_index s.accounts
in
let dst_proof, s =
let _, path = Merkle.proof_path dst_index s.accounts_tree in
let s =
if valid then (
let new_ticket_dst =
{
id = (if is_tez then dst_leaf.ticket.id else amount.id);
amount = new_ticket_amount_dst;
}
in
let new_leaf_dst = {dst_leaf with ticket = new_ticket_dst} in
dst_leaves.(dst_offset) <- new_leaf_dst ;
let dst_tree =
Merkle.update_tree
~input_length:2
dst_tree
dst_offset
(scalar_of_leaf new_leaf_dst)
in
let new_acc_dst =
{
dst_account with
tez_balance = new_tez_amount_dst;
tickets_root = Merkle.root dst_tree;
}
in
let accounts =
IMap.add
dst_index
(new_acc_dst, dst_leaves, dst_tree)
s.accounts
in
let accounts_tree =
Merkle.update_tree
~input_length:2
s.accounts_tree
dst_index
(scalar_of_account new_acc_dst)
in
{s with accounts_tree; accounts})
else s
in
let root = Merkle.root s.accounts_tree in
({path; root}, s)
in
let acc_after_dst, leaves_after_dst, tree_after_dst =
get_account dst_index s.accounts
in
let leaf_after_dst = leaves_after_dst.(dst_offset) in
let _, dst_leaf_path = Merkle.proof_path dst_offset tree_after_dst in
let dst =
{
account =
{before = dst_account; after = acc_after_dst; proof = dst_proof};
leaf =
{
before = dst_leaf;
after = leaf_after_dst;
path = dst_leaf_path;
};
}
in
(s, Transfer op, Some (Transfer {src; dst; valid}))
| Create ({; payload = {msg = {pk; fee}; signature}} as op) ->
let msg = hash_op @@ Create {header; payload = {pk; fee}} in
let well_formed = Bounded.(check fee) in
if not well_formed then (s, Create op, None)
else
let dst_pos = s.next_position in
let dst_index = dst_pos / Constants.max_nb_tickets in
let dst_offset = dst_pos mod Constants.max_nb_tickets in
assert (dst_offset = 0) ;
let next_empty_pos = dst_pos + Constants.max_nb_tickets in
let next_empty_index = dst_index + 1 in
let next_empty_offset = 0 in
let s = {s with next_position = next_empty_pos} in
let dst_account, dst_leaves, dst_tree =
get_account dst_index s.accounts
in
let next_empty_account, next_empty_leaves, next_empty_tree =
get_account (dst_index + 1) s.accounts
in
let check_next_is_empty = next_empty_account.pk = Curve.one in
let check_rollup_id = rollup_id = header.rollup_id in
let check_signature =
Schnorr.verify ~compressed:true ~msg ~pk ~signature ()
in
let check_price =
Bounded.v header.price.amount = Bounded.v fee
&& header.price.id = Constants.tez_id
in
let valid =
check_signature && check_next_is_empty && check_price
&& check_rollup_id
in
let next_empty_proof, s =
let _, path = Merkle.proof_path next_empty_index s.accounts_tree in
let root = Merkle.root s.accounts_tree in
({path; root}, s)
in
let dst_proof, s =
let _, path = Merkle.proof_path dst_index s.accounts_tree in
let s =
if valid then
let new_acc_dst = {dst_account with pk} in
let accounts =
IMap.add
dst_index
(new_acc_dst, dst_leaves, dst_tree)
s.accounts
in
let accounts_tree =
Merkle.update_tree
~input_length:2
s.accounts_tree
dst_index
(scalar_of_account new_acc_dst)
in
{s with accounts_tree; accounts}
else s
in
let root = Merkle.root s.accounts_tree in
({path; root}, s)
in
let acc_after_dst, _, _ = get_account dst_index s.accounts in
let dst_leaf = dst_leaves.(dst_offset) in
let _, dst_leaf_path = Merkle.proof_path dst_offset dst_tree in
let dst =
{
account =
{before = dst_account; after = acc_after_dst; proof = dst_proof};
leaf = {before = dst_leaf; after = dst_leaf; path = dst_leaf_path};
}
in
let next_empty_leaf = next_empty_leaves.(next_empty_offset) in
let _, ne_leaf_path =
Merkle.proof_path next_empty_offset next_empty_tree
in
let next_empty =
{
account =
{
before = next_empty_account;
after = next_empty_account;
proof = next_empty_proof;
};
leaf =
{
before = next_empty_leaf;
after = next_empty_leaf;
path = ne_leaf_path;
};
}
in
(s, Create op, Some (Create {dst; next_empty; valid}))
| Credit ({; payload = {cnt; dst; amount}} as op) ->
let well_formed =
Bounded.(check cnt && check dst && check amount.amount)
in
if not well_formed then (s, Credit op, None)
else
let dst_index = Z.to_int (coerce dst) / Constants.max_nb_tickets in
let dst_offset = Z.to_int (coerce dst) mod Constants.max_nb_tickets in
let is_tez = amount.id = Constants.tez_id in
let ticket_amount =
if is_tez then Bounded.make ~bound:Constants.Bound.max_amount Z.zero
else amount.amount
in
let tez_amount =
if is_tez then amount.amount
else Bounded.make ~bound:Constants.Bound.max_amount Z.zero
in
let dst_account, dst_leaves, dst_tree =
get_account dst_index s.accounts
in
let dst_leaf = dst_leaves.(dst_offset) in
let new_tez_amount_dst =
Bounded.(add_left ~unsafe:true dst_account.tez_balance tez_amount)
in
let new_ticket_amount_dst =
Bounded.(add_left ~unsafe:true dst_leaf.ticket.amount ticket_amount)
in
let check_counter = dst_account.cnt < cnt in
let check_balances =
Bounded.check new_tez_amount_dst
&& Bounded.check new_ticket_amount_dst
in
let check_rollup_id = rollup_id = header.rollup_id in
let check_ticket_ids =
is_tez
|| Z.(Bounded.v @@ dst_leaf.ticket.amount = zero)
|| amount.id = dst_leaf.ticket.id
in
let check_price =
Bounded.v header.price.amount = Bounded.v amount.amount
&& header.price.id = amount.id
in
let check_dst_pk = dst_account.pk <> Curve.one in
let valid =
check_counter && check_dst_pk && check_balances && check_ticket_ids
&& check_price && check_rollup_id
in
let dst_proof, s =
let _, path = Merkle.proof_path dst_index s.accounts_tree in
let s =
if valid then (
let new_ticket_dst =
{id = amount.id; amount = new_ticket_amount_dst}
in
let new_leaf_dst = {dst_leaf with ticket = new_ticket_dst} in
dst_leaves.(dst_offset) <- new_leaf_dst ;
let dst_tree =
Merkle.update_tree
~input_length:2
dst_tree
dst_offset
(scalar_of_leaf new_leaf_dst)
in
let new_acc_dst =
{
dst_account with
tez_balance = new_tez_amount_dst;
cnt;
tickets_root = Merkle.root dst_tree;
}
in
let accounts =
IMap.add
dst_index
(new_acc_dst, dst_leaves, dst_tree)
s.accounts
in
let accounts_tree =
Merkle.update_tree
~input_length:2
s.accounts_tree
dst_index
(scalar_of_account new_acc_dst)
in
{s with accounts_tree; accounts})
else s
in
let root = Merkle.root s.accounts_tree in
({path; root}, s)
in
let acc_after_dst, leaves_after_dst, tree_after_dst =
get_account dst_index s.accounts
in
let leaf_after_dst = leaves_after_dst.(dst_offset) in
let _, dst_leaf_path = Merkle.proof_path dst_offset tree_after_dst in
let dst =
{
account =
{before = dst_account; after = acc_after_dst; proof = dst_proof};
leaf =
{
before = dst_leaf;
after = leaf_after_dst;
path = dst_leaf_path;
};
}
in
(s, Credit op, Some (Credit {dst; valid}))
| Debit ({; payload = {msg = {cnt; src; amount}; signature}} as op) ->
let msg = hash_op @@ Debit {header; payload = {cnt; src; amount}} in
let well_formed =
Bounded.(check cnt && check src && check amount.amount)
in
if not well_formed then (s, Debit op, None)
else
let src_index = Z.to_int (coerce src) / Constants.max_nb_tickets in
let src_offset = Z.to_int (coerce src) mod Constants.max_nb_tickets in
let src_account, src_leaves, src_tree =
get_account src_index s.accounts
in
let src_leaf = src_leaves.(src_offset) in
let is_tez = amount.id = Constants.tez_id in
let ticket_amount =
if is_tez then Bounded.make ~bound:Constants.Bound.max_amount Z.zero
else amount.amount
in
let tez_amount =
if is_tez then amount.amount
else Bounded.make ~bound:Constants.Bound.max_amount Z.zero
in
let new_tez_amount_src =
Bounded.(sub_left ~unsafe:true src_account.tez_balance tez_amount)
in
let new_ticket_amount_src =
Bounded.(sub_left ~unsafe:true src_leaf.ticket.amount ticket_amount)
in
let check_counter = src_account.cnt < cnt in
let check_signature =
Schnorr.verify
~compressed:true
~msg
~pk:src_account.pk
~signature
()
in
let check_balances =
Z.(coerce new_tez_amount_src >= zero)
&& Z.(coerce new_ticket_amount_src >= zero)
in
let check_rollup_id = rollup_id = header.rollup_id in
let check_ticket_ids = is_tez || amount.id = src_leaf.ticket.id in
let check_price =
Bounded.v header.price.amount = Bounded.v amount.amount
&& header.price.id = amount.id
in
let valid =
check_counter && check_signature && check_balances
&& check_ticket_ids && check_price && check_rollup_id
in
let src_proof, s =
let _, path = Merkle.proof_path src_index s.accounts_tree in
let s =
if valid then (
let new_ticket_src =
{src_leaf.ticket with amount = new_ticket_amount_src}
in
let new_leaf_src = {src_leaf with ticket = new_ticket_src} in
src_leaves.(src_offset) <- new_leaf_src ;
let src_tree =
Merkle.update_tree
~input_length:2
src_tree
src_offset
(scalar_of_leaf new_leaf_src)
in
let new_cnt_src = Bounded.succ ~unsafe:true src_account.cnt in
let new_acc_src =
{
src_account with
tez_balance = new_tez_amount_src;
cnt = new_cnt_src;
tickets_root = Merkle.root src_tree;
}
in
let accounts =
IMap.add
src_index
(new_acc_src, src_leaves, src_tree)
s.accounts
in
let accounts_tree =
Merkle.update_tree
~input_length:2
s.accounts_tree
src_index
(scalar_of_account new_acc_src)
in
{s with accounts_tree; accounts})
else s
in
let root = Merkle.root s.accounts_tree in
({path; root}, s)
in
let acc_after_src, leaves_after_src, tree_after_src =
get_account src_index s.accounts
in
let leaf_after_src = leaves_after_src.(src_offset) in
let _, src_leaf_path = Merkle.proof_path src_offset tree_after_src in
let src =
{
account =
{before = src_account; after = acc_after_src; proof = src_proof};
leaf =
{
before = src_leaf;
after = leaf_after_src;
path = src_leaf_path;
};
}
in
(s, Debit op, Some (Debit {src; valid}))
let get_validity tx_s : bool =
match tx_s with
| Some (Transfer t_s) -> t_s.valid
| Some (Create t_s) -> t_s.valid
| Some (Credit t_s) -> t_s.valid
| Some (Debit t_s) -> t_s.valid
| None -> false
let tx_fee (op : tx) op_s =
let z = Bounded.make ~bound:Constants.Bound.max_fee Z.zero in
match (op, get_validity op_s) with
| Transfer tx, true -> tx.payload.msg.fee
| Create tx, true -> tx.payload.msg.fee
| _ -> z
let preprocess_private_batch (s : state) ops rollup_id =
let s, ops, ops_s, fees =
List.fold_left
(fun (s, ops, ops_s, acc_fee) op ->
let s, tx, tx_s = preprocess_operation s op rollup_id in
let fee = tx_fee tx tx_s in
let op = match tx with Transfer op -> op | _ -> assert false in
let op_s =
match tx_s with Some (Transfer op_s) -> op_s | _ -> assert false
in
( s,
op :: ops,
op_s :: ops_s,
Bounded.add_left ~unsafe:true acc_fee fee ))
(s, [], [], Bounded.make ~bound:Constants.Bound.max_amount Z.zero)
ops
in
let ops, ops_s = (List.rev ops, List.rev ops_s) in
(s, ops, ops_s, fees)
type generate_op_result = {
tx : tx;
tx_s : tx_storage;
fee : fee Bounded.t;
hash : S.t;
exit_validity : bool;
}
let generate_transaction :
?src_pos:Z.t ->
?dst_pos:Z.t ->
?amount:amount ticket ->
?fee:Z.t ->
?cnt:Z.t ->
?valid:bool ->
?unsafe:bool ->
sks:Schnorr.sk array ->
state ->
generate_op_result * state =
fun ?src_pos
?dst_pos
?amount
?fee
?cnt
?(valid = true)
?(unsafe = false)
~sks
s ->
let open Bound in
let unpack_optional ~bound ?opts ?(maxv = Bound.v bound) arg =
match (arg, opts) with
| Some v, _ -> Bounded.make ~unsafe ~bound v
| None, Some opts ->
let len_opts = List.length opts in
assert (len_opts > 0) ;
let i = random_int len_opts in
Bounded.make ~unsafe ~bound @@ List.nth opts i
| _ -> Bounded.random ~maxv bound
in
let open_positions =
List.of_seq
@@ Seq.map (fun (i, _) -> Z.of_int @@ (max_nb_tickets * i))
@@ IMap.to_seq s.accounts
in
let src_pos =
unpack_optional ~bound:max_nb_leaves ~opts:open_positions src_pos
in
let src_pos_i = Z.to_int @@ Bounded.v src_pos in
let src_index = src_pos_i / max_nb_tickets in
let src_offset = src_pos_i mod max_nb_tickets in
let dst_pos =
unpack_optional ~bound:max_nb_leaves ~opts:open_positions dst_pos
in
let dst_pos_i = Z.to_int @@ Bounded.v dst_pos in
let dst_index = dst_pos_i / max_nb_tickets in
let dst_offset = dst_pos_i mod max_nb_tickets in
let sk_src = sks.(src_index) in
let src_acc, src_leaves, _src_tree = get_account src_index s.accounts in
let dst_acc, dst_leaves, _dst_tree = get_account dst_index s.accounts in
let src_leaf = src_leaves.(src_offset) in
let dst_leaf = dst_leaves.(dst_offset) in
let cnt =
Option.(
value
~default:src_acc.cnt
(map (Bounded.make ~unsafe ~bound:max_counter) cnt))
in
let cnt = Bounded.succ cnt in
let fee =
unpack_optional
~bound:max_fee
~maxv:Z.(min (Bounded.v src_acc.tez_balance) (Bound.v max_fee))
fee
in
let amount_id =
Option.(value ~default:tez_id (map (fun {id; _} -> id) amount))
in
let is_tez = amount_id = tez_id in
let max_src_amount =
if is_tez then
Z.(min Bounded.(v src_acc.tez_balance - v fee) (Bound.v max_amount))
else Z.(min Bounded.(v src_leaf.ticket.amount) (Bound.v max_amount))
in
let max_dst_amount =
let bal =
if is_tez then dst_acc.tez_balance else dst_leaf.ticket.amount
in
Z.(Bound.v max_balance - Bounded.v bal)
in
let amount_amount =
unpack_optional
~bound:max_amount
~maxv:Z.(min max_src_amount max_dst_amount)
(Option.map (fun {amount; id = _id} -> Bounded.v amount) amount)
in
let amount = {id = amount_id; amount = amount_amount} in
let = Dummy.header in
let unsigned_payload = {cnt; src = src_pos; dst = dst_pos; amount; fee} in
let op = sign_op sk_src (Transfer {header; payload = unsigned_payload}) in
let msg = hash_op (Transfer {header; payload = unsigned_payload}) in
let s, tx, tx_s = preprocess_operation s op header.rollup_id in
let tx_s = Option.get tx_s in
( {
tx;
tx_s;
fee =
(if valid then fee else Bounded.make ~unsafe ~bound:max_fee Z.zero);
hash = msg;
exit_validity = false;
},
s )
let generate_transactions :
?src_pos:Z.t ->
?dst_pos:Z.t ->
?amount:amount ticket ->
?fee:Z.t ->
?cnt:Z.t ->
?valid:bool ->
?unsafe:bool ->
nb_batches:int ->
batch_size:int ->
sks:Schnorr.sk array ->
state ->
(generate_op_result list * state) list =
fun ?src_pos
?dst_pos
?amount
?fee
?cnt
?(valid = true)
?(unsafe = false)
~nb_batches
~batch_size
~sks
state ->
let make_batch state =
let batch, state =
List.fold_left
(fun (txs, state) _ ->
let tx, state =
generate_transaction
?src_pos
?dst_pos
?amount
?fee
?cnt
~valid
~unsafe
~sks
state
in
(tx :: txs, state))
([], state)
(List.init batch_size Fun.id)
in
(List.rev batch, state)
in
let batches, _ =
List.fold_left
(fun (batches, state) _ ->
let batch, state = make_batch state in
((batch, state) :: batches, state))
([], state)
(List.init nb_batches Fun.id)
in
List.rev batches
end
module V (L : LIB) = struct
module Hash = HashPV.V (L)
module Plompiler_Curve = JubjubEdwards (L)
module Schnorr = SchnorrPV.V (L)
module Merkle = MerklePV.V (L)
open L
module T = Types.V (L)
open T
module Encodings = Types.Encodings (L)
let compression_bound (values : unit Bounded_u.t list) =
let values = (values :> (scalar repr * Z.t) list) in
List.fold_left Z.mul Z.one (List.map snd values)
let monadic_compress (values : unit Bounded_u.t list) =
assert (compression_bound values < S.order) ;
let values = (values :> (scalar repr * Z.t) list) in
foldM
(fun acc (v, v_bound) -> Num.add ~ql:(S.of_z v_bound) acc v)
(fst @@ List.hd values)
(List.tl values)
let assert_merkle_proof x path root =
let* b = Merkle.merkle_proof path x root in
Bool.assert_true b
let hash_leaf (l : leaf_u) =
let* compressed = monadic_compress Bounded_u.[f l.pos; f l.ticket.amount] in
Hash.digest ~input_length:2 (to_list [l.ticket.id; compressed])
let hash_account (acc : account_u) =
let pk_x, _pk_y = of_pair acc.pk in
let* compressed =
monadic_compress Bounded_u.[f acc.tez_balance; f acc.cnt]
in
let* h = Hash.digest ~input_length:2 (to_list [pk_x; compressed]) in
Num.add h acc.tickets_root
let assert_tree_proofs (acc : account_u) (leaf : leaf_u) path_acc path_leaf
root =
let* scalar_acc = hash_account acc in
assert_merkle_proof scalar_acc path_acc root
>* let* scalar_leaf = hash_leaf leaf in
assert_merkle_proof scalar_leaf path_leaf acc.tickets_root
let coerce (type a) (x : a Bounded_u.t) =
fst (x : a Bounded_u.t :> scalar repr * Z.t)
let check_eq_account (a : account_u) (b : account_u) =
with_bool_check (equal a.pk b.pk)
>* with_bool_check (equal (coerce a.tez_balance) (coerce b.tez_balance))
>* with_bool_check (equal (coerce a.cnt) (coerce b.cnt))
>* with_bool_check (equal a.tickets_root b.tickets_root)
let check_eq_leaf (a : leaf_u) (b : leaf_u) =
with_bool_check (equal (coerce a.pos) (coerce b.pos))
>* with_bool_check (equal a.ticket.id b.ticket.id)
>* with_bool_check (equal (coerce a.ticket.amount) (coerce b.ticket.amount))
let predicate_fees ~old_root ~old_next_pos ~new_root ~new_next_pos ~fees
operator =
let safety = Encodings.Bounded_e.Unsafe in
let* old_root = input ~kind:`Public @@ Input.scalar old_root in
let* old_next_pos =
input ~kind:`Public @@ Encodings.(pos_encoding ~safety).input old_next_pos
in
let* new_root = input ~kind:`Public @@ Input.scalar new_root in
let* new_next_pos =
input ~kind:`Public @@ Encodings.(pos_encoding ~safety).input new_next_pos
in
let* fees =
input ~kind:`Public @@ Encodings.((amount_encoding ~safety).input) fees
in
let* operator =
input @@ Encodings.account_tree_el_encoding.input operator
in
let* generator =
Plompiler_Curve.(input_point @@ affine_to_point Curve.one)
in
let fees = Encodings.((amount_encoding ~safety).decode) fees in
let operator = Encodings.account_tree_el_encoding.decode operator in
assert_equal old_next_pos new_next_pos
>* let* before_s = hash_account operator.before in
let* after_s = hash_account operator.after in
assert_merkle_proof before_s operator.proof.path old_root
>* assert_merkle_proof after_s operator.proof.path new_root
>* let* new_bl_operator =
Bounded_u.add_left operator.before.tez_balance fees
in
let new_acc_operator =
{operator.before with tez_balance = new_bl_operator}
in
check_eq_account new_acc_operator operator.after
>*
let x_pk = of_pair operator.before.pk |> fst in
let x_g = of_pair generator |> fst in
let* diff = Num.add x_pk ~qr:S.mone x_g in
with_bool_check (Num.is_not_zero diff)
let hash_op = function
| `Transfer (tx : transfer_u) ->
let* compressed =
monadic_compress
Bounded_u.
[
f tx.header.op_code;
f tx.header.price.amount;
f tx.payload.msg.src;
f tx.payload.msg.dst;
f tx.payload.msg.fee;
f tx.payload.msg.amount.amount;
f tx.payload.msg.cnt;
]
in
Hash.digest
~input_length:4
(to_list
[
tx.header.l1_dst;
compressed;
tx.header.price.id;
tx.payload.msg.amount.id;
])
| `Create (tx : create_u) ->
let* compressed =
monadic_compress
Bounded_u.
[
f tx.header.op_code;
f tx.header.price.amount;
f tx.payload.msg.fee;
]
in
let x_pk, y_pk = of_pair tx.payload.msg.pk in
Hash.digest
~input_length:5
(to_list
[tx.header.l1_dst; compressed; x_pk; y_pk; tx.header.price.id])
| `Credit (tx : credit_u) ->
let* compressed =
monadic_compress
Bounded_u.
[
f tx.header.op_code;
f tx.header.price.amount;
f tx.payload.dst;
f tx.payload.amount.amount;
f tx.payload.cnt;
]
in
Hash.digest
~input_length:4
(to_list
[
tx.header.l1_dst;
compressed;
tx.header.price.id;
tx.payload.amount.id;
])
| `Debit (tx : debit_u) ->
let* compressed =
monadic_compress
Bounded_u.
[
f tx.header.op_code;
f tx.header.price.amount;
f tx.payload.msg.src;
f tx.payload.msg.amount.amount;
f tx.payload.msg.cnt;
]
in
Hash.digest
~input_length:4
(to_list
[
tx.header.l1_dst;
compressed;
tx.header.price.id;
tx.payload.msg.amount.id;
])
let expected_op_code : Types.P.tx -> S.t = function
| Types.P.Transfer _ -> S.zero
| Types.P.Create _ -> S.one
| Types.P.Credit _ -> S.of_int 2
| Types.P.Debit _ -> S.of_int 3
let get_op_code : Types.P.tx -> Z.t =
let open Types.P.Bounded in
function
| Types.P.Transfer tx -> v tx.header.op_code
| Types.P.Create tx -> v tx.header.op_code
| Types.P.Credit tx -> v tx.header.op_code
| Types.P.Debit tx -> v tx.header.op_code
let predicate_ill_formed ~old_root ~old_next_pos ~new_root ~new_next_pos ~fee
~exit_validity ~rollup_id (t : Types.P.tx) =
let safety = Encodings.Bounded_e.Safe in
let* old_root = input ~kind:`Public @@ Input.scalar old_root in
let* old_next_pos =
input ~kind:`Public @@ Encodings.(pos_encoding ~safety).input old_next_pos
in
let* new_root = input ~kind:`Public @@ Input.scalar new_root in
let* new_next_pos =
input ~kind:`Public @@ Encodings.(pos_encoding ~safety).input new_next_pos
in
let* fee =
input ~kind:`Public @@ Encodings.((fee_encoding ~safety).input) fee
in
let fee = Encodings.((fee_encoding ~safety).decode) fee in
let* exit_validity = input ~kind:`Public @@ Input.bool exit_validity in
let* _rollup_id =
input ~kind:`Public @@ Encodings.(tezos_zkru_encoding.input) rollup_id
in
Bool.assert_false (unsafe_bool_of_scalar @@ coerce fee)
>* assert_equal old_root new_root
>* assert_equal old_next_pos new_next_pos
>*
match t with
| Types.P.Transfer tx ->
let* tx =
input ~kind:`Public
@@ Encodings.((transfer_encoding ~safety).input) tx
in
let tx = Encodings.((transfer_encoding ~safety).decode) tx in
Num.assert_eq_const (coerce tx.header.op_code) (expected_op_code t)
>* let* b_tx = get_checks_wire in
Bool.assert_false b_tx
| Types.P.Create tx ->
let* tx =
input ~kind:`Public @@ Encodings.((create_encoding ~safety).input) tx
in
let tx = Encodings.((create_encoding ~safety).decode) tx in
Num.assert_eq_const (coerce tx.header.op_code) (expected_op_code t)
>* Bool.assert_true exit_validity
>* let* b_tx = get_checks_wire in
Bool.assert_false b_tx
| Types.P.Credit tx ->
let* tx =
input ~kind:`Public @@ Encodings.(credit_encoding ~safety).input tx
in
let tx = Encodings.((credit_encoding ~safety).decode) tx in
Num.assert_eq_const (coerce tx.header.op_code) (expected_op_code t)
>* Bool.assert_true exit_validity
>* let* b_tx = get_checks_wire in
Bool.assert_false b_tx
| Types.P.Debit tx ->
let* tx =
input ~kind:`Public @@ Encodings.(debit_encoding ~safety).input tx
in
let tx = Encodings.((debit_encoding ~safety).decode) tx in
Num.assert_eq_const (coerce tx.header.op_code) (expected_op_code t)
>* Bool.assert_false exit_validity
>* let* b_tx = get_checks_wire in
Bool.assert_false b_tx
let transfer_circuit ~op_code ~old_root ~old_next_pos ~rollup_id ~generator
(tx : transfer_u) (tx_s : transfer_storage_u) =
Num.assert_eq_const (coerce tx.header.op_code) op_code
>*
assert_tree_proofs
tx_s.src.account.before
tx_s.src.leaf.before
tx_s.src.account.proof.path
tx_s.src.leaf.path
old_root
>*
assert_tree_proofs
tx_s.dst.account.before
tx_s.dst.leaf.before
tx_s.dst.account.proof.path
tx_s.dst.leaf.path
tx_s.src.account.proof.root
>*
assert_tree_proofs
tx_s.src.account.after
tx_s.src.leaf.after
tx_s.src.account.proof.path
tx_s.src.leaf.path
tx_s.src.account.proof.root
>*
assert_tree_proofs
tx_s.dst.account.after
tx_s.dst.leaf.after
tx_s.dst.account.proof.path
tx_s.dst.leaf.path
tx_s.dst.account.proof.root
>*
assert_equal (coerce tx.payload.msg.src) (coerce tx_s.src.leaf.before.pos)
>* assert_equal
(coerce tx.payload.msg.dst)
(coerce tx_s.dst.leaf.before.pos)
>* with_bool_check (equal rollup_id tx.header.rollup_id)
>*
let* is_tez = Num.is_eq_const tx.payload.msg.amount.id Constants.tez_id in
let* dst_bal_is_0 =
Num.is_zero @@ coerce tx_s.dst.leaf.before.ticket.amount
in
let* equal_src =
equal tx.payload.msg.amount.id tx_s.src.leaf.before.ticket.id
in
with_bool_check (Bool.bor is_tez equal_src)
>* let* is_tez_or_bal_0 = Bool.bor is_tez dst_bal_is_0 in
let* equal_dst =
equal tx.payload.msg.amount.id tx_s.dst.leaf.before.ticket.id
in
with_bool_check (Bool.bor is_tez_or_bal_0 equal_dst)
>*
let* z = Num.zero in
let* ticket_amount =
Bool.ifthenelse is_tez z (coerce tx.payload.msg.amount.amount)
in
let ticket_amount =
Bounded_u.make_unsafe ~bound:Constants.Bound.max_amount ticket_amount
in
let* new_ticket_amnt_src =
Bounded_u.sub_left tx_s.src.leaf.before.ticket.amount ticket_amount
in
let new_ticket_src =
{id = tx_s.src.leaf.before.ticket.id; amount = new_ticket_amnt_src}
in
let new_leaf_src = {tx_s.src.leaf.before with ticket = new_ticket_src} in
let* new_ticket_amnt_dst =
Bounded_u.add_left tx_s.dst.leaf.before.ticket.amount ticket_amount
in
let* new_ticket_id_dst =
Bool.ifthenelse
is_tez
tx_s.dst.leaf.before.ticket.id
tx.payload.msg.amount.id
in
let new_ticket_dst =
{id = new_ticket_id_dst; amount = new_ticket_amnt_dst}
in
let new_leaf_dst = {tx_s.dst.leaf.before with ticket = new_ticket_dst} in
check_eq_leaf new_leaf_src tx_s.src.leaf.after
>* check_eq_leaf new_leaf_dst tx_s.dst.leaf.after
>*
let* tez_transfer_amount =
Bool.ifthenelse is_tez (coerce tx.payload.msg.amount.amount) z
in
let tez_transfer_amount =
Bounded_u.make_unsafe
~bound:Constants.Bound.max_amount
tez_transfer_amount
in
let* tez_amount =
Bounded_u.add_left tez_transfer_amount tx.payload.msg.fee
in
let* new_tez_bal_src =
Bounded_u.sub_left tx_s.src.account.before.tez_balance tez_amount
in
let new_acc_src =
{
tx_s.src.account.before with
tez_balance = new_tez_bal_src;
cnt = tx.payload.msg.cnt;
tickets_root = tx_s.src.account.after.tickets_root;
}
in
let* new_tez_bal_dst =
Bounded_u.add_left
tx_s.dst.account.before.tez_balance
tez_transfer_amount
in
let new_acc_dst =
{
tx_s.dst.account.before with
tickets_root = tx_s.dst.account.after.tickets_root;
tez_balance = new_tez_bal_dst;
}
in
check_eq_account new_acc_src tx_s.src.account.after
>* check_eq_account new_acc_dst tx_s.dst.account.after
>*
let* expected_cnt = Bounded_u.succ tx_s.src.account.before.cnt in
with_bool_check (equal (coerce expected_cnt) (coerce tx.payload.msg.cnt))
>*
with_bool_check (Num.is_zero @@ coerce tx.header.price.amount)
>*
let x_pk = of_pair tx_s.dst.account.before.pk |> fst in
let x_g = of_pair generator |> fst in
let* diff = Num.add x_pk ~qr:S.mone x_g in
with_bool_check (Num.is_not_zero diff)
>*
let* msg = hash_op (`Transfer tx) in
with_bool_check
(Schnorr.verify
~compressed:true
~g:generator
~msg
~pk:tx_s.src.account.before.pk
~signature:tx.payload.signature
())
>* let* b_tx = get_checks_wire in
let* expected_fee =
Bool.ifthenelse b_tx (coerce tx.payload.msg.fee) z
in
let* root_next =
Bool.ifthenelse b_tx tx_s.dst.account.proof.root old_root
in
assert_equal b_tx tx_s.valid
>* ret
( root_next,
old_next_pos,
Bounded_u.make_unsafe
~bound:Constants.Bound.max_fee
expected_fee )
let predicate_op ?(public = true) ~old_root ~old_next_pos ~new_root
~new_next_pos ~fee ~exit_validity ~rollup_id (t : Types.P.tx)
(t_storage : Types.P.tx_storage) =
let safety = Encodings.Bounded_e.Unsafe in
let* old_root = input ~kind:`Public @@ Input.scalar old_root in
let* old_next_pos =
input ~kind:`Public @@ Encodings.(pos_encoding ~safety).input old_next_pos
in
let* new_root = input ~kind:`Public @@ Input.scalar new_root in
let* new_next_pos =
input ~kind:`Public @@ Encodings.(pos_encoding ~safety).input new_next_pos
in
let* fee =
input ~kind:`Public @@ Encodings.((fee_encoding ~safety).input) fee
in
let fee = Encodings.((fee_encoding ~safety).decode) fee in
let* exit_validity = input ~kind:`Public @@ Input.bool exit_validity in
let* rollup_id =
input ~kind:`Public @@ Encodings.(tezos_zkru_encoding.input) rollup_id
in
match (t, t_storage) with
| Transfer tx, Transfer tx_s ->
let kind = if public then `Public else `Private in
let* tx =
input ~kind @@ Encodings.((transfer_encoding ~safety).input) tx
in
let tx = Encodings.((transfer_encoding ~safety).decode) tx in
let* tx_s = input @@ Encodings.(transfer_storage_encoding.input) tx_s in
let tx_s = Encodings.(transfer_storage_encoding.decode) tx_s in
let* generator =
Plompiler_Curve.(input_point @@ affine_to_point Curve.one)
in
let* root_next, next_pos_next, computed_fee =
transfer_circuit
~op_code:(expected_op_code t)
~old_root
~old_next_pos
~rollup_id
~generator
tx
tx_s
in
assert_equal root_next new_root
>* assert_equal next_pos_next new_next_pos
>* assert_equal (coerce computed_fee) (coerce fee)
| Create tx, Create tx_s ->
assert public ;
let* tx =
input ~kind:`Public @@ Encodings.((create_encoding ~safety).input) tx
in
let tx = Encodings.((create_encoding ~safety).decode) tx in
let* tx_s = input @@ Encodings.(create_storage_encoding.input) tx_s in
let tx_s = Encodings.(create_storage_encoding.decode) tx_s in
let* generator =
Plompiler_Curve.(input_point @@ affine_to_point Curve.one)
in
Num.assert_eq_const (coerce tx.header.op_code) (expected_op_code t)
>* let* dst_account_before_s = hash_account tx_s.dst.account.before in
let* dst_account_after_s = hash_account tx_s.dst.account.after in
let* next_empty_account_s =
hash_account tx_s.next_empty.account.before
in
assert_merkle_proof
dst_account_before_s
tx_s.dst.account.proof.path
old_root
>* assert_merkle_proof
dst_account_after_s
tx_s.dst.account.proof.path
tx_s.dst.account.proof.root
>* assert_merkle_proof
next_empty_account_s
tx_s.next_empty.account.proof.path
old_root
>*
assert_equal old_next_pos (coerce tx_s.dst.leaf.after.pos)
>* assert_equal new_next_pos (coerce tx_s.next_empty.leaf.before.pos)
>*
let x_pk = of_pair tx_s.next_empty.account.before.pk |> fst in
let x_g = of_pair generator |> fst in
let* diff = Num.add x_pk ~qr:S.mone x_g in
with_bool_check (Num.is_zero diff)
>*
let x_pk = of_pair tx_s.dst.account.before.pk |> fst in
let x_g = of_pair generator |> fst in
let* diff = Num.add x_pk ~qr:S.mone x_g in
with_bool_check (Num.is_zero diff)
>*
let new_acc_dst =
{tx_s.dst.account.before with pk = tx.payload.msg.pk}
in
check_eq_account new_acc_dst tx_s.dst.account.after
>* with_bool_check (equal rollup_id tx.header.rollup_id)
>*
with_bool_check (Num.is_eq_const tx.header.price.id Constants.tez_id)
>* with_bool_check
(equal
(coerce tx.header.price.amount)
(coerce tx.payload.msg.fee))
>* with_bool_check
(Num.is_eq_const
(coerce tx.payload.msg.fee)
(S.of_z Constants.create_fee))
>*
let* msg = hash_op (`Create tx) in
with_bool_check
(Schnorr.verify
~compressed:true
~g:generator
~msg
~pk:tx.payload.msg.pk
~signature:tx.payload.signature
())
>* let* b_tx = get_checks_wire in
let* z = Num.zero in
let* expected_fee =
Bool.ifthenelse b_tx (coerce tx.payload.msg.fee) z
in
assert_equal (coerce fee) expected_fee
>* let* root_next =
Bool.ifthenelse b_tx tx_s.dst.account.proof.root old_root
in
assert_equal b_tx tx_s.valid
>* let* not_valid = Bool.bnot b_tx in
assert_equal not_valid exit_validity
>* assert_equal root_next new_root
| Credit tx, Credit tx_s ->
assert public ;
let* tx =
input ~kind:`Public @@ Encodings.(credit_encoding ~safety).input tx
in
let tx = Encodings.(credit_encoding ~safety).decode tx in
let* tx_s = input @@ Encodings.(credit_storage_encoding.input) tx_s in
let tx_s = Encodings.(credit_storage_encoding.decode) tx_s in
let* generator =
Plompiler_Curve.(input_point @@ affine_to_point Curve.one)
in
assert_equal old_next_pos new_next_pos
>* Num.assert_eq_const (coerce tx.header.op_code) (expected_op_code t)
>*
assert_tree_proofs
tx_s.dst.account.before
tx_s.dst.leaf.before
tx_s.dst.account.proof.path
tx_s.dst.leaf.path
old_root
>*
assert_tree_proofs
tx_s.dst.account.after
tx_s.dst.leaf.after
tx_s.dst.account.proof.path
tx_s.dst.leaf.path
tx_s.dst.account.proof.root
>*
assert_equal (coerce tx.payload.dst) (coerce tx_s.dst.leaf.before.pos)
>* Bool.assert_false (unsafe_bool_of_scalar @@ coerce fee)
>* with_bool_check (equal rollup_id tx.header.rollup_id)
>* let* is_tez =
Num.is_eq_const tx.payload.amount.id Constants.tez_id
in
let* eq_id =
equal tx.payload.amount.id tx_s.dst.leaf.before.ticket.id
in
let* is_tez_or_eq_id = Bool.bor is_tez eq_id in
let* bal_0 =
Num.is_zero (coerce tx_s.dst.leaf.before.ticket.amount)
in
with_bool_check (Bool.bor is_tez_or_eq_id bal_0)
>*
let* z = Num.zero in
let* ticket_amount =
Bool.ifthenelse is_tez z (coerce tx.payload.amount.amount)
in
let ticket_amount =
Bounded_u.make_unsafe
~bound:Constants.Bound.max_amount
ticket_amount
in
let* new_ticket_amnt_dst =
Bounded_u.add_left tx_s.dst.leaf.before.ticket.amount ticket_amount
in
let* new_ticket_id_dst =
Bool.ifthenelse
is_tez
tx_s.dst.leaf.before.ticket.id
tx.payload.amount.id
in
let new_ticket_dst =
{id = new_ticket_id_dst; amount = new_ticket_amnt_dst}
in
let new_leaf_dst =
{tx_s.dst.leaf.before with ticket = new_ticket_dst}
in
let* tez_credit_amount =
Bool.ifthenelse is_tez (coerce tx.payload.amount.amount) z
in
let tez_credit_amount =
Bounded_u.make_unsafe
~bound:Constants.Bound.max_amount
tez_credit_amount
in
let* new_tez_bal_dst =
Bounded_u.add_left
tx_s.dst.account.before.tez_balance
tez_credit_amount
in
let new_acc_dst =
{
tx_s.dst.account.before with
tez_balance = new_tez_bal_dst;
cnt = tx.payload.cnt;
tickets_root = tx_s.dst.account.after.tickets_root;
}
in
check_eq_leaf new_leaf_dst tx_s.dst.leaf.after
>* check_eq_account new_acc_dst tx_s.dst.account.after
>*
let* expected_cnt = Bounded_u.succ tx_s.dst.account.before.cnt in
with_bool_check (equal (coerce expected_cnt) (coerce tx.payload.cnt))
>*
let x_pk = of_pair tx_s.dst.account.before.pk |> fst in
let x_g = of_pair generator |> fst in
let* diff = Num.add x_pk ~qr:S.mone x_g in
with_bool_check (Num.is_not_zero diff)
>*
with_bool_check (equal tx.header.price.id tx.payload.amount.id)
>* with_bool_check
(equal
(coerce tx.header.price.amount)
(coerce tx.payload.amount.amount))
>* let* b_tx = get_checks_wire in
let* root_next =
Bool.ifthenelse b_tx tx_s.dst.account.proof.root old_root
in
assert_equal b_tx tx_s.valid
>* let* not_valid = Bool.bnot b_tx in
assert_equal not_valid exit_validity
>* assert_equal root_next new_root
| Debit tx, Debit tx_s ->
assert public ;
let* tx =
input ~kind:`Public @@ Encodings.(debit_encoding ~safety).input tx
in
let tx = Encodings.(debit_encoding ~safety).decode tx in
let* tx_s = input @@ Encodings.(debit_storage_encoding.input) tx_s in
let tx_s = Encodings.(debit_storage_encoding.decode) tx_s in
let* generator =
Plompiler_Curve.(input_point @@ affine_to_point Curve.one)
in
assert_equal old_next_pos new_next_pos
>* Num.assert_eq_const (coerce tx.header.op_code) (expected_op_code t)
>*
assert_tree_proofs
tx_s.src.account.before
tx_s.src.leaf.before
tx_s.src.account.proof.path
tx_s.src.leaf.path
old_root
>*
assert_tree_proofs
tx_s.src.account.after
tx_s.src.leaf.after
tx_s.src.account.proof.path
tx_s.src.leaf.path
tx_s.src.account.proof.root
>*
assert_equal
(coerce tx.payload.msg.src)
(coerce tx_s.src.leaf.before.pos)
>* Bool.assert_false (unsafe_bool_of_scalar @@ coerce fee)
>* with_bool_check (equal rollup_id tx.header.rollup_id)
>* let* is_tez =
Num.is_eq_const tx.payload.msg.amount.id Constants.tez_id
in
let* eq_id =
equal tx.payload.msg.amount.id tx_s.src.leaf.before.ticket.id
in
with_bool_check (Bool.bor is_tez eq_id)
>*
let* z = Num.zero in
let* ticket_amount =
Bool.ifthenelse is_tez z (coerce tx.payload.msg.amount.amount)
in
let ticket_amount =
Bounded_u.make_unsafe
~bound:Constants.Bound.max_amount
ticket_amount
in
let* new_ticket_amnt_src =
Bounded_u.sub_left tx_s.src.leaf.before.ticket.amount ticket_amount
in
let new_ticket_src =
{id = tx.payload.msg.amount.id; amount = new_ticket_amnt_src}
in
let new_leaf_src =
{tx_s.src.leaf.before with ticket = new_ticket_src}
in
let* tez_debit_amount =
Bool.ifthenelse is_tez (coerce tx.payload.msg.amount.amount) z
in
let tez_debit_amount =
Bounded_u.make_unsafe
~bound:Constants.Bound.max_amount
tez_debit_amount
in
let* new_tez_bal_src =
Bounded_u.sub_left
tx_s.src.account.before.tez_balance
tez_debit_amount
in
let new_acc_src =
{
tx_s.src.account.before with
tez_balance = new_tez_bal_src;
cnt = tx.payload.msg.cnt;
tickets_root = tx_s.src.account.after.tickets_root;
}
in
check_eq_leaf new_leaf_src tx_s.src.leaf.after
>* check_eq_account new_acc_src tx_s.src.account.after
>*
let* expected_cnt = Bounded_u.succ tx_s.src.account.before.cnt in
with_bool_check
(equal (coerce expected_cnt) (coerce tx.payload.msg.cnt))
>*
with_bool_check (equal tx.header.price.id tx.payload.msg.amount.id)
>* with_bool_check
(equal
(coerce tx.header.price.amount)
(coerce tx.payload.msg.amount.amount))
>*
let* msg = hash_op (`Debit tx) in
with_bool_check
(Schnorr.verify
~compressed:true
~g:generator
~msg
~pk:tx_s.src.account.before.pk
~signature:tx.payload.signature
())
>* let* b_tx = get_checks_wire in
let* root_next =
Bool.ifthenelse b_tx tx_s.src.account.proof.root old_root
in
assert_equal b_tx tx_s.valid
>* assert_equal b_tx exit_validity
>* assert_equal root_next new_root
| _ -> assert false
let predicate_private_batch ~old_root ~old_next_pos ~new_root ~new_next_pos
~fees ~rollup_id (ops : Types.P.transfer list)
(ops_s : Types.P.transfer_storage list) =
assert (List.compare_lengths ops ops_s = 0) ;
let safety = Encodings.Bounded_e.Unsafe in
let* old_root = input ~kind:`Public @@ Input.scalar old_root in
let* old_next_pos =
input ~kind:`Public @@ Encodings.(pos_encoding ~safety).input old_next_pos
in
let* new_root = input ~kind:`Public @@ Input.scalar new_root in
let* new_next_pos =
input ~kind:`Public @@ Encodings.(pos_encoding ~safety).input new_next_pos
in
let* fees =
input ~kind:`Public @@ Encodings.((amount_encoding ~safety).input) fees
in
let fees = Encodings.((amount_encoding ~safety).decode) fees in
let* rollup_id =
input ~kind:`Public @@ Encodings.(tezos_zkru_encoding.input) rollup_id
in
let* ops =
mapM
(fun tx -> input @@ Encodings.((transfer_encoding ~safety).input tx))
ops
in
let ops = List.map Encodings.((transfer_encoding ~safety).decode) ops in
let* ops_s =
mapM
(fun tx_s -> input @@ Encodings.(transfer_storage_encoding.input tx_s))
ops_s
in
let ops_s = List.map Encodings.(transfer_storage_encoding.decode) ops_s in
let* generator =
Plompiler_Curve.(input_point @@ affine_to_point Curve.one)
in
let op_code = S.zero in
let* z = Num.zero in
let z = Bounded_u.make_unsafe ~bound:Constants.Bound.max_amount z in
let* computed_root, computed_fees =
fold2M
(fun (computed_root, computed_fees) op op_s ->
let* computed_root, _, fee =
transfer_circuit
~op_code
~old_root:computed_root
~old_next_pos
~rollup_id
~generator
op
op_s
in
let* computed_fees =
Bounded_u.add_left ~unsafe:true computed_fees fee
in
ret (computed_root, computed_fees))
(old_root, z)
ops
ops_s
in
assert_equal computed_root new_root
>* assert_equal old_next_pos new_next_pos
>* assert_equal (coerce computed_fees) (coerce fees)
end
module PI_parameters_predicate_private_batch = struct
module L = LibCircuit
type acc = {
root : L.scalar L.repr;
pos : L.scalar L.repr;
total_fees : L.scalar L.repr;
}
let inner_elt pi_list =
match pi_list with
| [old_root; old_next_pos; new_root; new_next_pos; fees; rollup_id] ->
(old_root, old_next_pos, new_root, new_next_pos, fees, rollup_id)
| _ -> failwith "invalid inner_pi format."
let outer_elt pi_list =
match pi_list with
| [old_root; new_root; total_fees; rollup_id] ->
(old_root, new_root, total_fees, rollup_id)
| _ -> failwith "invalid outer_pi format."
let nb_inner = 6
let nb_outer = 4
let check ~switches ~outer ~inner =
let open L in
let init, first_root, init_rollup_id =
let first_root, _old_next_pos, new_root, new_next_pos, fees, rollup_id =
inner_elt (List.hd inner)
in
( ({root = new_root; pos = new_next_pos; total_fees = fees}, []),
first_root,
rollup_id )
in
let old_root, new_root, total_fees, outer_rollup_id = outer_elt outer in
let* acc, inner_checks =
fold2M
(fun (acc, checks) pi_list switch ->
let* n_switch = Bool.bnot switch in
let old_root, old_next_pos, new_root, new_next_pos, fees, rollup_id =
inner_elt pi_list
in
let* check_old_pos =
let* res = equal acc.pos old_next_pos in
Bool.bor n_switch res
in
let* check_roots =
let* res = equal acc.root old_root in
Bool.bor n_switch res
in
let* check_id =
let* res = equal outer_rollup_id rollup_id in
Bool.bor n_switch res
in
let* total_fees =
let* fees = Num.mul (scalar_of_bool switch) fees in
Num.add acc.total_fees fees
in
let checks = [check_old_pos; check_roots; check_id] @ checks in
let* root = Bool.ifthenelse switch new_root acc.root in
let* pos = Bool.ifthenelse switch new_next_pos acc.pos in
ret ({root; pos; total_fees}, checks))
init
(List.tl inner)
(List.tl switches)
in
let* check_fees = equal total_fees acc.total_fees in
let* check_first_root = equal old_root first_root in
let* check_last_root = equal new_root acc.root in
let* check_fst_rollup_id = equal outer_rollup_id init_rollup_id in
Bool.band_list
([check_fees; check_first_root; check_last_root; check_fst_rollup_id]
@ inner_checks)
let outer_of_inner inner =
let old_root, _, _, _, first_fees, rollup_id = inner_elt (List.hd inner) in
let new_root, total_fees =
List.fold_left
(fun (_, acc_fees) pi ->
let _, _, new_root, _, fees, _ = inner_elt pi in
let acc_fees = Kzg.Bls.Scalar.(acc_fees + fees) in
(new_root, acc_fees))
(old_root, first_fees)
(List.tl inner)
in
[old_root; new_root; total_fees; rollup_id]
end