summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/latex/cryptocode/cryptocode.tex
blob: 3dd048a2aee291a082ec730bd31e84480cfc08a4 (plain)
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
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
 %% Copyright 2015 Arno Mittelbach
  %
  % This work may be distributed and/or modified under the
  % conditions of the LaTeX Project Public License, either version 1.3
  % of this license or (at your option) any later version.
  % The latest version of this license is in
  %   http://www.latex-project.org/lppl.txt
  % and version 1.3 or later is part of all distributions of LaTeX
  % version 2005/12/01 or later.
  %
  % This work has the LPPL maintenance status `maintained'.
  % 
  % The Current Maintainer of this work is Arno Mittelbach.
  %
  % This work consists of the files cryptocode.tex and cryptocode.sty

\documentclass[a4paper]{report}

% more than one optional parameter
\usepackage[usenames]{xcolor}
\usepackage{hyperref}
\usepackage[
	n,
	operators,
	advantage,
	sets,
	adversary,
	landau,
	probability,
	notions,	
	logic,
	ff,
	mm,
	primitives,
	events,
	complexity,
	asymptotics,
	keys]{cryptocode}


\usepackage{csquotes}
\usepackage{fullpage}
\usepackage{dashbox}
\usepackage{todonotes}

\usepackage{url}


\usetikzlibrary{shapes.callouts}
\usetikzlibrary{ocgx}

\usepackage{listings}

\usepackage{trace}

\usepackage{makeidx}
\makeindex


\author{Arno Mittelbach\\
\texttt{mail@arno-mittelbach.de}}
\title{\Huge Cryptocode \\ \large\textsc{Typesetting Cryptography}}

\definecolor{mygreen}{rgb}{0,0.6,0}
\definecolor{mygray}{rgb}{0.5,0.5,0.5}
\definecolor{mymauve}{rgb}{0.58,0,0.82}

\lstset{language=[LaTeX]TeX,
 backgroundcolor=\color{white},   % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}
  basicstyle=\footnotesize,        % the size of the fonts that are used for the code
  breakatwhitespace=false,         % sets if automatic breaks should only happen at whitespace
  breaklines=true,                 % sets automatic line breaking
  captionpos=b,                    % sets the caption-position to bottom
  commentstyle=\color{mygreen},    % comment style
  deletekeywords={...},            % if you want to delete keywords from the given language
  escapeinside={\%*}{*)},          % if you want to add LaTeX within your code
  extendedchars=true,              % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8
  frame=single,                    % adds a frame around the code
  keepspaces=true,                 % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)
  keywordstyle=\color{blue},       % keyword style
  language=Octave,                 % the language of the code
  morekeywords={*,...},            % if you want to add more keywords to the set
  numbers=left,                    % where to put the line-numbers; possible values are (none, left, right)
  numbersep=5pt,                   % how far the line-numbers are from the code
  numberstyle=\tiny\color{mygray}, % the style that is used for the line-numbers
  rulecolor=\color{black},         % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (green here))
  showspaces=false,                % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'
  showstringspaces=false,          % underline spaces within strings only
  showtabs=false,                  % show tabs within strings adding particular underscores
  stepnumber=2,                    % the step between two line-numbers. If it's 1, each line will be numbered
  stringstyle=\color{mymauve},     % string literal style
  tabsize=2,                       % sets default tabsize to 2 spaces
  title=\lstname        }  

\begin{document}
%\pcfixhyperref

\maketitle




\begin{abstract}
\thispagestyle{empty}
The cryptocode package is targeted at cryptographers typesetting their results in \LaTeX. 
It provides various predefined commands for different topics in cryptography.
In particular it provides an easy interface to write pseudocode, protocols, game based proofs 
and draw black-box reductions.


\vspace{2em}
\begin{center}
\fbox{
\pseudocode{%
\textbf{ Alice} \< \< \textbf{ Bob}  \\[0.5\baselineskip][\hline]
 \<\< \\[-0.5\baselineskip]
x \sample \ZZ_q \< \< \\
 X \gets g^x \<\< \\
 \< \sendmessageright*{\GG,q,g,X} \< \\
 \<\< y \sample \ZZ_q \\
 \<\< Y \gets g^y \\
 \< \sendmessageleft*{Y} \< \\
 \key_A \gets Y^x \<\< \key_B \gets X^y }
}
\end{center}



\end{abstract}
\newpage

\pagenumbering{arabic}

\tableofcontents
\thispagestyle{empty}
\newpage

\chapter{Cryptocode by Example}

Cryptocode is a \LaTeX package to ease the writing of cryptographic papers. It provides mechanisms
for writing pseudocode, protocols, game-based proofs and black-box reductions. In addition it comes
with a large number of predefined commands. In this chapter we present the various features of 
cryptocode by giving small examples. But first, let's load the package

\begin{lstlisting}
\usepackage[
	n,
	advantage,
	operators,
	sets,
	adversary,
	landau,
	probability,
	notions,
	logic,
	ff,
	mm,
	primitives,
	events,
	complexity,
	asymptotics,
	keys
	]{cryptocode}
\end{lstlisting}

Note that all the options refer to a set of commands. That is, without any options cryptocode will provide the mechanisms
for writing pseudocode, protocols, game-based proofs and black-box reductions but not define additional commands,
such as \lstinline$\pk$ or \lstinline$\sk$ (for typesetting public and private/secret keys) which are part of the keys option.
We discuss the various options and associated commands in Chapter~\ref{chap:commands}.


\section{Pseudocode}
The cryptocode package tries to make writing pseudocode easy and enjoyable. The
\lstinline$\pseudocode$ command takes a single parameter where you can start writing
code in mathmode using \lstinline{\\} as line breaks. Following is an $\indcpa$ game
definition using various commands from cryptocode to ease writing keys (\lstinline{\pk,\sk}),
sampling (\lstinline{\sample}), and more:
\begin{center}
\fbox{\pseudocode[linenumbering,syntaxhighlight=auto]{%
  b \sample \bin  \\
  (\pk,\sk) \sample \kgen (\secparam)  \\
  (\state,m_0,m_1) \sample \adv(\secparam, \pk, c)   \\
  c \sample \enc(\pk,m_b)  \\
  b' \sample \adv(\secparam, \pk, c, \state)  \\
  return b = b' }}
\end{center}


The above code is generated by (the code is actually wrapped in an \lstinline$fbox$).
\begin{lstlisting}
\pseudocode[linenumbering,syntaxhighlight=auto]{%
  b \sample \bin  \\
  (\pk,\sk) \sample \kgen (\secparam)  \\
  (\state,m_0,m_1) \sample \adv(\secparam, \pk, c)   \\
  c \sample \enc(\pk,m_b)  \\
  b' \sample \adv(\secparam, \pk, c, \state)  \\
  return b = b' }
\end{lstlisting}
The pseudocode command thus takes a single mandatory argument (the code) plus an optional argument
which allows you to specify options in a key=value fashion. In the above example we used the linenumbering
option (which not surprisingly adds line numbers to the code) as well as the syntaxhighlighting option which
highlights certain keywords (in the example it is responsible for setting \enquote{return} as \pseudocodeconstant[]{return}).

It is easy to define a heading for your code. Either specify the header using the option \enquote{head} or use 
the \lstinline$\procedure$ command which takes an additional argument to specify the headline.
\begin{center}
\fbox{\procedure[linenumbering]{$\indcpa_\enc^\adv$}{%
  b \sample \bin  \\
  (\pk,\sk) \sample \kgen (\secparam)  \\
  (\state,m_0,m_1) \sample \adv(\secparam, \pk, c)   \\
  c \sample \enc(\pk,m_b)  \\
  b' \sample \adv(\secparam, \pk, c, \state) \\
  \pcreturn b = b' }}
\end{center}
\begin{lstlisting}
\procedure[linenumbering]{$\indcpa_\enc^\adv$}{%
  b \sample \bin  \\
  (\pk,\sk) \sample \kgen (\secparam)  \\
  (\state,m_0,m_1) \sample \adv(\secparam, \pk, c)   \\
  c \sample \enc(\pk,m_b)  \\
  b' \sample \adv(\secparam, \pk, c, \state) \\
  \pcreturn b = b' }
\end{lstlisting}
Here in the example we have not turned on the automatic syntax highlighting but used the command \lstinline$\pcreturn$
to highlight the return statement. Besides \lstinline$\pcreturn$ there are a variant of predefined \enquote{PseudocodeConstants}
such as \lstinline$\pcfor$, \lstinline$\pcif$, etc.

There is a lot more that we will discuss in detail in Chapter~\ref{chap:pseudocode}. Here, for example
is the same code with an overlay explanation and a division of the pseudocode.
\begin{center}
\fbox{
\begin{pcimage}
\procedure[linenumbering]{$\indcpa_\enc^\adv$}{%
	 b \sample \bin  \\
	 (\pk,\sk) \sample \kgen (\secparam)\pcnode{kgen}  \pclb
	\pcintertext[dotted]{Setup Completed} 
	 (m_0,m_1) \sample \adv(\secparam, \pk, c)  \\
	  c \sample \enc(\pk,m_b)  \\
	  b' \sample \adv(\secparam, \pk, c, \state) \\
	  \pcreturn b = b' }

\pcdraw{
  \node[rectangle callout,callout absolute pointer=(kgen),fill=orange]
      at ([shift={(+3,-1)}]kgen) {
		\begin{varwidth}{3cm}
			$\kgen(\secparam)$ samples a public key $\pk$ and a private key $\sk$.
		\end{varwidth}
  };
}
\end{pcimage}
}
\end{center}

\begin{lstlisting}
\begin{pcimage}
\procedure[linenumbering]{$\indcpa_\enc^\adv$}{%
	 b \sample \bin  \\
	 (\pk,\sk) \sample \kgen (\secparam)\pcnode{kgen}  \pclb
	\pcintertext[dotted]{Setup Completed} 
	 (m_0,m_1) \sample \adv(\secparam, \pk, c)  \\
	  c \sample \enc(\pk,m_b)  \\
	  b' \sample \adv(\secparam, \pk, c, \state) \\
	  \pcreturn b = b' }

\pcdraw{
  \node[rectangle callout,callout absolute pointer=(kgen),fill=orange]
      at ([shift={(+3,-1)}]kgen) {
		\begin{varwidth}{3cm}
			$\kgen(\secparam)$ samples a public key $\pk$ and a private key $\sk$.
		\end{varwidth}
  };
}
\end{pcimage}
\end{lstlisting}

\section{Columns}
The \lstinline$\pseudocode$ and \lstinline$\procedure$ commands allow the usage of multiple columns.
You switch to a new column by inserting a \lstinline$\>$. This is similar to using an \lstinline$align$
environment and placing a tabbing \& character.\footnote{In fact, the \emph{pseudocode} command
is based on amsmath's flalign environment.}
\begin{center}
\fbox{%
\pseudocode{%
 \textbf{First} \> \textbf{Second} \> \textbf{Third} \> \textbf{Fourth} \\
  b \sample \bin \> b \sample \bin \> b \sample \bin \> b \sample \bin}
}
\end{center}
\begin{lstlisting}
\pseudocode{%
 \textbf{First} \> \textbf{Second} \> \textbf{Third} \> \textbf{Fourth} \\
  b \sample \bin \> b \sample \bin \> b \sample \bin \> b \sample \bin}
\end{lstlisting}
As you can see the first column is left aligned the second right, the third left and so forth.
In order to get only left aligned columns you could thus simply always skip a column by
using \lstinline$\>\>$. You can also use \lstinline$\<$ a shorthand for \lstinline$\>\>$.
\begin{center}
\fbox{%
\pseudocode{%
 \textbf{First} \< \textbf{Second} \< \textbf{Third} \< \textbf{Fourth} \\
  b \sample \bin \< b \sample \bin \< b \sample \bin \< b \sample \bin}
}
\end{center}

\section{Protocols}
Using columns makes it easy to write even complex protocols. Following is a simple
three party protocol
\begin{center}
\fbox{%
\pseudocode{%
 \textbf{Alice} \< \< \textbf{Bob} \< \< \textbf{Charlie} \\[][\hline]
 \text{work} \< \< \< \< \\
 \< \sendmessageright{top=Work result,topstyle=red} \< \< \< \\
 \< \< \text{work} \< \< \\
 \< \< \<  \sendmessageright{top=Work result,bottom=Bottom message} \< \\
 \< \<  \< \< \text{work} \\
 \< \sendmessageleftx{8}{\text{A long message for Alice}} \< \\
 \text{finalize} \< \< \< \< }
}
\end{center}
\begin{lstlisting}
\pseudocode{%
 \textbf{Alice} \< \< \textbf{Bob} \< \< \textbf{Charlie} \\[][\hline]
 \text{work} \< \< \< \< \\
 \< \sendmessageright{top=Work result,topstyle=red} \< \< \< \\
 \< \< \text{work} \< \< \\
 \< \< \<  \sendmessageright{top=Work result,bottom=Bottom message} \< \\
 \< \<  \< \< \text{work} \\
 \< \sendmessageleftx{8}{\text{A long message for Alice}} \< \\
 \text{finalize} \< \< \< \< }
\end{lstlisting}
The commands \lstinline$\sendmessageright$ and \lstinline$\sendmessageleft$ are very flexible and
allow to style the sending of messages in various ways. Also note the \lstinline$\\[][\hline]$ at the end of the
first line. Here the first optional argument allows us to specify the lineheight (similarly to the behavior in an
align environment). The second optional argument allows us to, for example, draw a horizontal line.

In multi player protocols such as the one
above the commands  \lstinline$\sendmessagerightx$ and \lstinline$\sendmessageleftx$ (note the x at the end)
allow to send messages over multiple columns. In the example, as we were using \lstinline$\<$ the
final message thus spans 8 columns. 

For basic protocols you might also utilize the \lstinline$\sendmessageright*$ and \lstinline$\sendmessageleft*$
commands which simply take a message which is displayed.
\begin{center}
\fbox{%
\pseudocode{%
\textbf{ Alice} \< \< \textbf{ Bob}  \\[0.5\baselineskip][\hline]
 \<\< \\[-0.5\baselineskip]
x \sample \ZZ_q \< \< \\
 X \gets g^x \<\< \\
 \< \sendmessageright*{\GG,q,g,X} \< \\
 \<\< y \sample \ZZ_q \\
 \<\< Y \gets g^y \\
 \< \sendmessageleft*{Y} \< \\
 \key_A \gets Y^x \<\< \key_B \gets X^y 
}
}
\end{center}
\begin{lstlisting}
\pseudocode{%
\textbf{ Alice} \< \< \textbf{ Bob}  \\[0.5\baselineskip][\hline]
 \<\< \\[-0.5\baselineskip]
x \sample \ZZ_q \< \< \\
 X \gets g^x \<\< \\
 \< \sendmessageright*{\GG,q,g,X} \< \\
 \<\< y \sample \ZZ_q \\
 \<\< Y \gets g^y \\
 \< \sendmessageleft*{Y} \< \\
 \key_A \gets Y^x \<\< \key_B \gets X^y }
\end{lstlisting}
We will discuss protocols in greater detail in Chapter~\ref{chap:protocols}.

\section{Game-based Proofs}
Cryptocode supports authors in visualizing game-based proofs. It defines an environment
\lstinline$gameproof$ which allows to wrap a number of game procedures displaying helpful
information as to what changes from game to game, and to what each step is reduced.

\vspace{2\baselineskip}
\begin{center}
\begin{gameproof}
\gameprocedure[linenumbering,mode=text]{%
 Step 1 \\
 Step 2 \\
 Step 3  
 }
\gameprocedure[mode=text]{%
   Step 1  \\
   \gamechange{Step 2 is different} \\
   Step 3
 }
\addgamehop{1}{2}{hint={\footnotesize some hint}}
\end{gameproof}
\end{center}
\begin{lstlisting}
\begin{gameproof}
\gameprocedure[linenumbering,mode=text]{%
 Step 1 \\
 Step 2 \\
 Step 3  
 }
\gameprocedure[mode=text]{%
   Step 1  \\
   \gamechange{Step 2 is different} \\
   Step 3
 }
\addgamehop{1}{2}{hint={\footnotesize some hint}}
\end{gameproof}
\end{lstlisting}
Note that we made use of the option \enquote{mode=text} in the above example which tells the underlying pseudocode 
command to not work in math mode but in plain text mode. We'll discuss how to visualize game-based proofs in Chapter~\ref{chap:gbproofs}.

\section{Black-box Reductions}
Cryptocode provides a strucured syntax to visualize black-box reductions. Basically cryptocode
provides an environment to draw boxes that may have oracles and that can be communicated with.
Cryptocode makes heavy use of TIKZ (\url{https://www.ctan.org/pkg/pgf}) for this, which gives you 
quite some control over how things should look like. Additionally, as you can specify node names
(for example the outer box in the next example is called \enquote{A}) you can easily extend the pictures
by using plain TIKZ commands.
\begin{bbrenv}{A}
	\begin{bbrbox}[name=Reduction]
	\pseudocode{
		\text{Do something} \\
		\text{Step 2} 
	}

	\begin{bbrenv}{B}
		\begin{bbrbox}[name=Adversary,minheight=4cm]
		\end{bbrbox}

		\bbrmsgto{top=$m$}
		\bbrmsgfrom{top=$\sigma$}
		\bbrmsgtxt{\pseudocode{%
			\text{more work} 
		}}
		\bbrmsgto{top=$m$}
		\bbrmsgfrom{top=$\sigma$}

		\bbrqryto{side=$m$}
		\bbrqryfrom{side=$b$}
	\end{bbrenv}

	\pseudocode{
		\text{finalize} 
	}	

	\end{bbrbox}
	\bbrinput{input}
	\bbroutput{output}

	\begin{bbroracle}{OraA}
		\begin{bbrbox}[name=Oracle 1,minheight=1cm]
		\end{bbrbox}
	\end{bbroracle}
	\bbroraclequeryto{top=$m$}
	\bbroraclequeryfrom{top=$b$}

	\begin{bbroracle}{OraB}
		\begin{bbrbox}[name=Oracle 2,minheight=1cm]
		\end{bbrbox}
	\end{bbroracle}
	\bbroraclequeryto{top=$m$}
	\bbroraclequeryfrom{top=$b$}
\end{bbrenv}
\begin{lstlisting}
\begin{bbrenv}{A}
	\begin{bbrbox}[name=Reduction]
	\pseudocode{
		\text{Do something} \\
		\text{Step 2} 
	}

	\begin{bbrenv}{B}
		\begin{bbrbox}[name=Adversary,minheight=4cm]
		\end{bbrbox}

		\bbrmsgto{top=$m$}
		\bbrmsgfrom{top=$\sigma$}
		\bbrmsgtxt{\pseudocode{%
			\text{more work} 
		}}
		\bbrmsgto{top=$m$}
		\bbrmsgfrom{top=$\sigma$}

		\bbrqryto{side=$m$}
		\bbrqryfrom{side=$b$}
	\end{bbrenv}

	\pseudocode{
		\text{finalize} 
	}	

	\end{bbrbox}
	\bbrinput{input}
	\bbroutput{output}

	\begin{bbroracle}{OraA}
		\begin{bbrbox}[name=Oracle 1,minheight=1cm]
		\end{bbrbox}
	\end{bbroracle}
	\bbroraclequeryto{top=$m$}
	\bbroraclequeryfrom{top=$b$}

	\begin{bbroracle}{OraB}
		\begin{bbrbox}[name=Oracle 2,minheight=1cm]
		\end{bbrbox}
	\end{bbroracle}
	\bbroraclequeryto{top=$m$}
	\bbroraclequeryfrom{top=$b$}
\end{bbrenv}
\end{lstlisting}
We'll discuss the details in Chapter~\ref{chap:bbr}.

\chapter{Cryptographic Notation}
In this section we'll discuss the various commands for notation that can be loaded via package options.
\begin{lstlisting}
\usepackage[
	n,
	advantage,
	operators,
	sets,
	adversary,
	landau,
	probability,
	notions,
	logic,
	ff,
	mm,
	primitives,
	events,
	complexity,
	asymptotics,
	keys
	]{cryptocode}
\end{lstlisting}

\textbf{Remark. } The commands defined so far are far from complete and are currently mostly targeted at what I needed in my
papers (especially once you get to cryptographic notions and primitives). So please if you feel that something should be added
drop me an email.

\section{Security Parameter}
In cryptography we make use of a security parameter which is usually written as $1^n$ or $1^\lambda$. The cryptocode
package, when loading either option \enquote{n} or option \enquote{lambda} will define the commands
\begin{lstlisting}
	\secpar
	\secparam
\end{lstlisting}
The first command provides the \enquote{letter}, i.e., either $n$ or $\lambda$, whereas \lstinline$\secparam$ points
to $1^\secpar$.

\section{Advantage Terms}
Load the package option ``advantage'' in order to define the command \lstinline$\advantage$ used to specify advantage terms such as:
\[
\advantage{prf}{\adv,\prf} = \negl
\]
\begin{lstlisting}
	\advantage{prf}{\adv,\prf} = \negl
\end{lstlisting}
Specify an optional third parameter to replace the $(\secpar)$. 

\section{Math Operators}
The \enquote{operators} option provides the following list of commands:

\begin{center}
\begin{tabular}{l l l l}
\textbf{Command} & \textbf{Description} & \textbf{Result} & \textbf{Example}  \\\hline
\lstinline$\sample$ & Sampling from a distribution, or running a randomized procedure & $\sample$ & $b \sample \bin$ \\
\lstinline$\floor$ & Rounding down & $\floor{42.5}$ &  \\
\lstinline$\ceil$ & Rounding up & $\ceil{41.5}$ &  \\
\lstinline$\Angle$ & Vector product & $\Angle{x,y}$ &  \\
\lstinline$\abs$ & Absolute number & $\abs{42.9}$ &  \\
\lstinline$\norm$ & Norm & $\norm{x}$ & \\
\lstinline$\concat$ & Verbose concatenation (I usually prefer simply \lstinline$\|$) & $\concat$ & $x \gets a\concat b$ \\
\lstinline$\emptystring$ & The empty string & $\emptystring$ & $x \gets \emptystring$ 
\end{tabular}
\end{center}

\section{Adversaries}
The \enquote{adversary} option provides the following list of commands:

\begin{center}
\begin{tabular}{l l l}
\textbf{Command} & \textbf{Description} & \textbf{Result} \\\hline
\lstinline$\adv$ & Adversary & $\adv$  \\
\lstinline$\bdv$ & Adversary & $\bdv$  \\
\lstinline$\cdv$ & Adversary & $\cdv$  \\
\lstinline$\ddv$ & Adversary & $\ddv$  \\
\lstinline$\mdv$ & Adversary & $\mdv$  \\
\lstinline$\pdv$ & Adversary & $\pdv$  \\
\lstinline$\sdv$ & Adversary & $\sdv$  
\end{tabular}
\end{center}

The style in which an adversary is rendered is controlled via 
\begin{lstlisting}
\renewcommand{\pcadvstyle}[1]{\mathcal{#1}}
\end{lstlisting}


\section{Landau}
The \enquote{landau} option provides the following list of commands:

\begin{center}
\begin{tabular}{l l l}
\textbf{Command} & \textbf{Description} & \textbf{Result} \\\hline
\lstinline$\bigO{n^2}$ & Big O notation & $\bigO{n^2}$  \\
\lstinline$\smallO{n^2}$ & small o notation & $\smallO{n^2}$  \\
\lstinline$\bigOmega{n^2}$ & Big Omega notation & $\bigOmega{n^2}$  \\
\lstinline$\bigsmallO{n^2}$ & Big and small O notation & $\bigsmallO{n^2}$  
\end{tabular}
\end{center}


\section{Probabilities}
The \enquote{probability} option provides commands for writing probabilities. Use
\begin{lstlisting}
\prob{X=x}
\probsub{x\sample{\bin^n}}{x=5}
\condprob{X=x}{A=b}
\condprobsub{x\sample{\bin^n}}{x=5}{A=b}
\end{lstlisting}
to write basic probabilities, probabilities with explicit probability spaces and conditional probabilities.
\begin{align*}
& \prob{X=x} \\
& \probsub{x\sample{\bin^n}}{X=x} \\
& \condprob{X=x}{A=b} \\
& \condprobsub{x\sample{\bin^n}}{x=5}{A=b}
\end{align*}
You can control the probability symbol (Pr) by redefining
\begin{lstlisting}
\renewcommand{\probname}{Pr}
\end{lstlisting}

For expectations you can use
\begin{lstlisting}
\expect{X}
\expsub{x,y\sample\set{1,\ldots,6}}{x+y}
\condexp{X+Y}{Y>3}
\condexpsub{x,y\sample\set{1,\ldots,6}}{x+y}{y>3}
\end{lstlisting}
yielding
\begin{align*}
& \expect{X} \\
& \expsub{x,y\sample\set{1,\ldots,6}}{x+y} \\
& \condexp{X+Y}{Y>3} \\
& \condexpsub{x,y\sample\set{1,\ldots,6}}{x+y}{y>3} 
\end{align*}
You can control the expactation symbol ($\mathbb{E}$) by redefining
\begin{lstlisting}
\renewcommand{\expectationname}{\ensuremath{\mathbb{E}}}
\end{lstlisting}

The support $\supp{X}$ of a random variable $X$ can be written as
\begin{lstlisting}
\supp{X}
\end{lstlisting}
where again the name can be controlled via
\begin{lstlisting}
\renewcommand{\supportname}{Supp}
\end{lstlisting}

For denoting entropy and min-entropy use
\begin{lstlisting}
\entropy{X}
\minentropy{X}
\condminentropy{X}{Y=5}
\end{lstlisting}
This yields 
\begin{align*}
& \entropy{X} \\
& \minentropy{X} \\
& \condminentropy{X}{Y=5}
\end{align*}

\section{Sets}
The \enquote{sets} option provides commands for basic mathematical sets.
You can write sets and sequences as
\begin{lstlisting}
\set{1, \ldots, 10}
\sequence{1, \ldots, 10}
\end{lstlisting}
which is typeset as
\begin{align*}
& \set{1, \ldots, 10} \\
& \sequence{1, \ldots, 10}
\end{align*}

In addation the following commands are provided
\begin{center}
\begin{tabular}{l l l}
\textbf{Command} & \textbf{Description} & \textbf{Result} \\\hline
\lstinline$\bin$ & The set containing 0 and 1 & $\bin$  \\
\lstinline$\NN$ & Natural numbers & $\NN$  \\
\lstinline$\ZZ$ & Integers & $\ZZ$  \\
\lstinline$\QQ$ & Rational numbers & $\QQ$  \\
\lstinline$\RR$ & Reals & $\RR$  \\
\lstinline$\PP$ & & $\PP$  \\
\lstinline$\FF$ & & $\FF$  \\
\end{tabular}
\end{center}

\section{Crypto Notions}
The \enquote{notions} option provides the following list of commands:

\begin{center}
\begin{tabular}{l l l}
\textbf{Command} & \textbf{Description} & \textbf{Result} \\\hline
\lstinline$\indcpa$ & IND-CPA security for encryption schemes & $\indcpa$  \\
\lstinline$\indcpa$ & IND-CCA security for encryption schemes & $\indcca$  \\
\lstinline$\indcpai$ & IND-CCA1 security for encryption schemes & $\indccai$  \\
\lstinline$\indcpaii$ &  IND-CCA2 security for encryption schemes & $\indccaii$  \\
\lstinline$\priv$ & PRIV security for deterministic public-key encryption schemes & $\priv$  \\
\lstinline$\ind$ & IND security (for deterministic public-key encryption schemes) & $\ind$  \\
\lstinline$\prvcda$ & PRV-CDA security (for deterministic public-key encryption schemes)& $\prvcda$  \\
\lstinline$\prvrcda$ & PRV\$-CDA security (for deterministic public-key encryption schemes) & $\prvrcda$  \\
\lstinline$\kiae$ & Key independent authenticated encryption & $\kiae$  \\
\lstinline$\kdae$ & Key dependent authenticated encryption & $\kdae$  \\
\lstinline$\mle$ & Message locked encryption & $\mle$  \\
\lstinline$\uce$ & Universal computational extractors & $\uce$  \\
\end{tabular}
\end{center}

The style in which notions are displayed can be controlled via redefining
\begin{lstlisting}
\renewcommand{\pcnotionstyle}[1]{\ensuremath{\mathrm{#1}}}
\end{lstlisting}

\section{Logic}
The \enquote{logic} option provides the following list of commands:

\begin{center}
\begin{tabular}{l l l}
\textbf{Command} & \textbf{Description} & \textbf{Result} \\\hline
\lstinline$\AND$ & Logical AND & $\AND$  \\
\lstinline$\OR$ & Logical OR & $\OR$  \\
\lstinline$\NOT$ & not & $\NOT$  \\
\lstinline$\xor$ & exclusive or & $\xor$  \\
\lstinline$\false$ & false & $\false$  \\
\lstinline$\true$ & true & $\true$  
\end{tabular}
\end{center}


% Function Families
\section{Function Families}
The \enquote{ff} option provides the following list of commands:

\begin{center}
\begin{tabular}{l l l}
\textbf{Command} & \textbf{Description} & \textbf{Result} \\\hline
\lstinline$\kgen$ & Key generation & $\kgen$  \\
\lstinline$\pgen$ & Parameter generation & $\pgen$  \\
\lstinline$\eval$ & Evaluation & $\eval$  \\
\lstinline$\il$ & Input length & $\il$  \\
\lstinline$\ol$ & Output length & $\ol$  \\
\lstinline$\kl$ & Key length & $\kl$  \\
\lstinline$\nl$ & Nonce length & $\nl$  \\
\lstinline$\rl$ & Randomness length & $\rl$  
\end{tabular}
\end{center}

The style in which these are displayed can be controlled via redefining
\begin{lstlisting}
\renewcommand{\pcalgostyle}[1]{\ensuremath{\mathsf{#1}}}
\end{lstlisting}


% Machine Model
\section{Machine Model}
The \enquote{mm} option provides the following list of commands:

\begin{center}
\begin{tabular}{l l l}
\textbf{Command} & \textbf{Description} & \textbf{Result} \\\hline
\lstinline$\CRKT$ & A circuit & $\CRKT$  \\
\lstinline$\TM$ & A Turing machine & $\TM$  \\
\lstinline$\PROG$ & A program & $\PROG$  \\
\lstinline$\uTM$ & A universal Turing machine & $\uTM$  \\
\lstinline$\uC$ & A universal Circuit & $\uC$  \\
\lstinline$\uP$ & A universal Program & $\uP$  \\
\lstinline$\tmtime$ & Time (of a TM) & $\tmtime$  \\
\lstinline$\ppt$ & Probabilistic polynomial time & $\ppt$  
\end{tabular}
\end{center}

The style in which these are displayed can be controlled via redefining
\begin{lstlisting}
\renewcommand{\pcmachinemodelstyle}[1]{\ensuremath{\mathsf{#1}}}
\end{lstlisting}

\section{Crypto Primitives}
The \enquote{primitives} option provides the following list of commands:


\begin{center}
\begin{tabular}{l l l}
\textbf{Command} & \textbf{Description} & \textbf{Result} \\\hline
\lstinline$\prover$ & Proover & $\prover$  \\
\lstinline$\verifier$ & Verifier & $\verifier$  \\
\lstinline$\nizk$ & Non interactie zero knowledge & $\nizk$  \\
\lstinline$\hash$ & A hash function & $\hash$  \\
\lstinline$\gash$ & A hash function& $\gash$  \\
\lstinline$\fash$ & A hash function & $\fash$  \\
\lstinline$\enc$ & Encryption  & $\enc$  \\
\lstinline$\dec$ & Decryption & $\dec$  \\
\lstinline$\sig$ & Signing & $\sig$  \\
\lstinline$\verify$ & Verifying & $\verify$  \\
\lstinline$\obf$ & Obfuscation & $\obf$  \\
\lstinline$\iO$ & Indistinguishability obfuscation & $\iO$  \\
\lstinline$\diO$ & Differing inputs obfuscation & $\diO$  \\
\lstinline$\mac$ & Message authentication & $\mac$  \\
\lstinline$\puncture$ & Puncturing & $\puncture$  \\
\lstinline$\source$ & A source & $\source$  \\
\lstinline$\predictor$ & A predictor & $\predictor$  \\
\lstinline$\sam$ & A sampler & $\sam$  \\
\lstinline$\distinguisher$ & A distinguisher & $\distinguisher$  \\
\lstinline$\dist$ & A distinguisher& $\dist$  \\
\lstinline$\simulator$ & A simulator & $\simulator$  \\
\lstinline$\ext$ & An extractor & $\ext$  
\end{tabular}
\end{center}

The style in which these are displayed can be controlled via redefining
\begin{lstlisting}
\renewcommand{\pcalgostyle}[1]{\ensuremath{\mathsf{#1}}}
\end{lstlisting}

\section{Events}
The \enquote{events} option provides the following list of commands. 

To classify an event use
\begin{lstlisting}
\event{Event}
\nevent{Event}
\end{lstlisting}
where the second is meant as the negation. These are typset as
\begin{align*}
&\event{Event}\\
&\nevent{Event}
\end{align*}

For bad events, use \lstinline$\bad$ ($\bad$).

\section{Complexity}
The \enquote{complexity} option provides the following list of commands:

\begin{center}
\begin{tabular}{l l l}
\textbf{Command} & \textbf{Result} \\\hline
\lstinline$\npol$ &  $\npol$  \\
\lstinline$\conpol$ &  $\conpol$  \\
\lstinline$\pol$ &  $\pol$  \\
\lstinline$\bpp$ & $\bpp$  \\
\lstinline$\ppoly$ & $\ppoly$  \\
\lstinline$\NC{1}$ &  $\NC1$  \\
\lstinline$\AC{1}$ & $\AC1$  \\
\lstinline$\TC{1}$ & $\TC1$ \\
\lstinline$\AM$ &  $\AM$  \\
\lstinline$\coAM$ &  $\coAM$  
\end{tabular}
\end{center}

The style in which these are displayed can be controlled via redefining
\begin{lstlisting}
\renewcommand{\pccomplexitystyle}[1]{\ensuremath{\mathsf{#1}}}
\end{lstlisting}

\section{Asymptotics}
The \enquote{asymptotics} option provides the following list of commands:

\begin{center}
\begin{tabular}{l l p{7.5cm}}
\textbf{Command} & \textbf{Description} & \textbf{Result} \\\hline
\lstinline$\negl$ & A negligible function & $\negl$ (takes an optional argument \lstinline$\negl[a]$ ($\negl[a]$). Write \lstinline$\negl[]$ for $\negl[]$.)  \\
\lstinline$\poly$ &  A polynomial & $\poly$ (takes an optional argument \lstinline$\poly[a]$ ($\poly[a]$). Write \lstinline$\poly[]$ for $\poly[]$.) \\
\lstinline$\pp$ & some polynomial $^\pp$ & $\pp$  \\
\lstinline$\qq$ & some polynomial $^\qq$ &  $\qq$  \\
\end{tabular}
\end{center}

The style in which these are displayed can be controlled via redefining
\begin{lstlisting}
\renewcommand{\pcpolynomialstyle}[1]{\ensuremath{\mathrm{#1}}}
\end{lstlisting}

\section{Keys}
The \enquote{keys} option provides the following list of commands:


\begin{center}
\begin{tabular}{l l l}
\textbf{Command} & \textbf{Description} & \textbf{Result} \\\hline
\lstinline$\pk$ &public key & $\npol$  \\
\lstinline$\vk$ &  verification key& $\vk$  \\
\lstinline$\sk$ & secret key&  $\sk$  \\
\lstinline$\key$ & a plain key& $\key$  \\
\lstinline$\hk$ & hash key&  $\hk$  \\
\lstinline$\gk$ &gash key & $\gk$  \\
\lstinline$\fk$ & function key & $\fk$  
\end{tabular}
\end{center}

The style in which these are displayed can be controlled via redefining
\begin{lstlisting}
\renewcommand{\pckeystyle}[1]{\ensuremath{\mathsf{#1}}}
\end{lstlisting}


\chapter{Pseudocode}
\label{chap:pseudocode}

In this chapter we discuss how to write pseudocode with the cryptocode library.

\section{Basics}

The cryptocode package provides the command \emph{pseudocode} in order to write simple cryptostyle algorithms.
Consider the following definition of an IND-CPA game
\begin{center}
\fbox{%
\pseudocode{%
   b \sample \bin  \\
   (\pk,\sk) \sample \kgen (\secparam)  \\
   (m_0,m_1) \sample \adv(\secparam, \pk, c)  \\
   c \sample \enc(\pk,m_b)  \\
   b' \sample \adv(\secparam, \pk, c)  \\
   \pcreturn b = b' }
}
\end{center}
which is generated as
\begin{lstlisting}
\pseudocode{%
   b \sample \bin  \\
   (\pk,\sk) \sample \kgen (\secparam)  \\
   (m_0,m_1) \sample \adv(\secparam, \pk, c)  \\
   c \sample \enc(\pk,m_b)  \\
   b' \sample \adv(\secparam, \pk, c)  \\
   \pcreturn b = b' }
\end{lstlisting}
As you can see the pseudocode command provides a math based environment where you can simply start typing your pseudocode
separating lines by \lstinline$\\$.


\paragraph{Boxed appearance}
Although most examples here appear centered and boxed this is not directly part of the pseudocode package but due to the examples being 
typeset as
\begin{lstlisting}
\begin{center}
\fbox{%
Code
}
\end{center}
\end{lstlisting}


\subsection{Customizing Pseudocode}
Besides the mandatory argument the \lstinline{\pseudocode} command can take an optional argument which consists of a list of key=value pairs
separated by commas (,).
\begin{lstlisting}
\pseudocode[options]{body}
\end{lstlisting}
The following keys are available:
\begin{description}
\item[head]
A header for the code
\item[width]
An exact width. If no width is specified, cryptocode tries to automatically compute the correct width.
\item[lnstart]
The starting line number when using line numbering.
\item[lnstartright]
The starting line number for right aligned line numberswhen using line numbering.
\item[linenumbering]
Enables line numbering.
\item[syntaxhighlight]
When set to \enquote{auto} cryptocode will attempt to automatically hightlight keywords such as \enquote{for}, \enquote{foreach}
and \enquote{return}
\item[keywords]
Provide a comma separated list of keywords for automatic syntax highlighting.
\item[addkeywords]
Provide additional keywords for automatic syntax highlighting.
\item[mode]
When set to text pseudocode will not start in math mode but in text mode.
\item[xshift]
Allows horizontal shifting
\end{description}
The following code 
\begin{lstlisting}
\pseudocode[linenumbering,syntaxhighlight=auto,head=Header]{ return null }
\end{lstlisting}
creates 
\begin{center}
\fbox{\pseudocode[linenumbering,syntaxhighlight=auto,head=Header]{ return null }}
\end{center}



\subsection{Indentation}
\index{indentation}
\index{pcind}
\index{t}

In order to indent code use \lstinline$\pcind$ or short \lstinline$\t$. You can also usecustomized spacing such as \lstinline$\quad$ or \lstinline$\hspace$
when using the pseudocode command in math mode.
\begin{center}
\fbox{%
\pseudocode{%
   \pcfor i = 1..10 \pcdo  \\
   \pcind T[i] \sample \bin^n \\
   \pcfor i = 1..10 \pcdo  \\
   \t T[i] \sample \bin^n }
}
\end{center}
which is generated as
\begin{lstlisting}
\pseudocode{%
   \pcfor i = 1..10 \pcdo  \\
   \pcind T[i] \sample \bin^n  \\
   \pcfor i = 1..10 \pcdo  \\
   \t T[i] \sample \bin^n }
\end{lstlisting}
You can specify multiple levels via the optional first argument
\begin{lstlisting}
\pcind[level]
\end{lstlisting}
\begin{center}
\fbox{%
\pseudocode{%
   \pcfor i = 1..10 \pcdo  \\
   \pcind T[i] \sample \bin^n  \\
   \pcind\pcind T[i] \sample \bin^n \\
   \pcind[3] T[i] \sample \bin^n \\
   \pcind[4] T[i] \sample \bin^n \\
   \pcind[5] T[i] \sample \bin^n }
}
\end{center}
\begin{lstlisting}
\pseudocode{%
   \pcfor i = 1..10 \pcdo  \\
   \pcind T[i] \sample \bin^n  \\
   \pcind\pcind T[i] \sample \bin^n \\
   \pcind[3] T[i] \sample \bin^n \\
   \pcind[4] T[i] \sample \bin^n \\
   \pcind[5] T[i] \sample \bin^n }
\end{lstlisting}

\index{pcindentname}
You can customize the indentation shortcut by redefining
\begin{lstlisting}
\renewcommand{\pcindentname}{t}
\end{lstlisting}
 
\subsection{Textmode}
\index{text mode}

By default pseudocode enables \LaTeX' math mode. You can change this behavior and tell the pseudocode command to interpret the content
in text mode by setting the option \enquote{mode=text}.
\begin{center}
\fbox{\pseudocode[mode=text]{%
This is \\
\t simply text}}
\end{center}
\begin{lstlisting}
\pseudocode[mode=text]{%
This is \\
\t simply text}
\end{lstlisting}

\subsection{Syntax Highlighting}

\index{pccontinue}
\index{pccomment}
\index{pcdo}
\index{pcdone}
\index{pcelse}
\index{pcfi}
\index{pcforeach}
\index{pcglobvar}
\index{pcif}
\index{pcin}
\index{pcnew}
\index{pcnull}
\index{pcparse}
\index{pcrepeat}
\index{pcreturn}
\index{pcthen}
\index{pctrue}
\index{pcwhile}


In the above examples we have used commands \lstinline$\pcreturn$ and \lstinline$\pcfor$ to highlight certain keywords.
Besides the \emph{pcreturn}, \emph{pcfor} and \emph{pcdo} (where the pc stands for pseudocode) that were used in the above 
examples the package defines the following set of constants:

\begin{center}
\begin{tabular}{lll}
 \textbf{name} & \textbf{usage} & \textbf{outcome} \\ 
 pccontinue & \lstinline$\pccontinue$ & \pccontinue \\
 pccomment & \lstinline$\pccomment{comment}$ & \pccomment{comment} \\
 pcdo & \lstinline$\pcdo$ & \pcdo \\
 pcdone & \lstinline$\pcdone$ & \pcdone \\
 pcfalse & \lstinline$\pcfalse$ & \pcfalse \\
 pcelse & \lstinline$\pcelse$ & \pcelse \\
 pcfi & \lstinline$\pcfi$ & \pcfi \\
 pcfor  & \lstinline$\pcfor$ & \pcfor \\
 pcforeach  & \lstinline$\pcforeach$ & \pcforeach \\
 pcglobvar & \lstinline$\pcglobvar$ & \pcglobvar \\
 pcif & \lstinline$\pcif$ & \pcif \\
 pcin & \lstinline$\pcin$ & \pcin \\
 pcnew & \lstinline$\pcnew$ & \pcnew \\
 pcnull & \lstinline$\pcnull$ & \pcnull \\
 pcparse & \lstinline$\pcparse$ & \pcparse \\
 pcrepeat & \lstinline$\pcrepeat{10}$ & \pcrepeat{10} \\
 pcuntil & \lstinline$\pcuntil$ & \pcuntil \\
 pcreturn & \lstinline$\pcreturn$ & \pcreturn \\
 pcthen & \lstinline$\pcthen$ & \pcthen \\
 pctrue & \lstinline$\pctrue$ & \pctrue \\
 pcwhile & \lstinline$\pcwhile$ & \pcwhile 
\end{tabular}
\end{center}
Note that \lstinline{\pcdo}, \lstinline{\pcin} and \lstinline{\pcthen} have a leading space. This is due to their usual usage scenarios
such as
\begin{center}
\pseudocode{\pcfor i \pcin \{1,\ldots,10\}}
\end{center}
Furthermore all constants have a trailing space. This can be removed by adding the optional parameter [] such as
\begin{center}
\pseudocode{\pcfor i \pcin[] \{1,\ldots,10\}}
\end{center}
\begin{lstlisting}
\pseudocode{\pcfor i \pcin[] \{1,\ldots,10\}}
\end{lstlisting}

\index{pseudocodeconstant}
In order to change the font you can overwrite the command \lstinline{\pseudocodeconstant} which is defined as
\begin{lstlisting}
\newcommand{\pseudocodeconstant}[2][\ ]{\ensuremath{\mathbf{#2}}#1}
\end{lstlisting}

\subsubsection{Automatic Syntax Highlighting}
The pseudocode command comes with an experimental feature to automatically highlight keywords. This can be activated via
the option \enquote{syntaxhighlight=auto}. The preset list of keywords it looks for are
\begin{lstlisting}
for ,foreach ,return ,{ do }, in ,new ,if ,null ,null,true ,true,until ,{ to },false ,false,{ then },repeat ,else ,done ,done,fi 
\end{lstlisting}
Note that the keywords are matched with spaces and note the grouping for trailing spaces. That is, the \enquote{ do } keyword won't match within the string \enquote{don't}.
Via the option \enquote{keywords} you can provide a custom list of keywords.
Thus the following bubblesort variant (taken from \url{http://en.wikipedia.org/wiki/Bubble_sort}) 
\begin{center}
\fbox{\procedure[syntaxhighlight=auto]{Bubblesort(A : list of items)}{
   n \gets \mathsf{length}(A) \\
   repeat \\
   \t s \gets false \\
   \t  for i = 1 to n-1 do \\
   \t\t  \pccomment{if this pair is out of order} \\
   \t\t   if A[i-1] > A[i] then \\
   \t\t\t \pccomment{swap them and remember something changed} \\
   \t\t\t     \mathsf{swap}( A[i-1], A[i] ) \\
   \t\t\t       s \gets true \\
   until \neg s }
}
\end{center}
can be typeset as
\begin{lstlisting}
\procedure[syntaxhighlight=auto]{Bubblesort(A : list of items)}{
   n \gets \mathsf{length}(A) \\
   repeat \\
   \t s \gets false \\
   \t  for i = 1 to n-1 do \\
   \t\t  \pccomment{if this pair is out of order} \\
   \t\t   if A[i-1] > A[i] then \\
   \t\t\t \pccomment{swap them and remember something changed} \\
   \t\t\t     \mathsf{swap}( A[i-1], A[i] ) \\
   \t\t\t       s \gets true \\
   until \neg s }
\end{lstlisting}

You can also define additional keywords using the \enquote{addkeywords} option. This would allow ous to specify length and swap in the above example.
We can also overwrite how a single keyword is set by defining the command \lstinline$dohighlightKEYWORD$. Thus to set swap and length in \lstinline$\mathsf$
we could define
\begin{lstlisting}
\newcommand{\dohighlightswap}[1]{\mathsf{#1}}
\newcommand{\dohighlightlengt}[1]{\mathsf{#1}}
\end{lstlisting}
and then 
\begin{lstlisting}
\procedure[syntaxhighlight=auto,addkeywords={swap,length}]{Bubblesort(A : list of items)}{
   n \gets length(A) \\
   repeat \\
   \t s \gets false \\
   \t  for i = 1 to n-1 do \\
   \t\t  \pccomment{if this pair is out of order} \\
   \t\t   if A[i-1] > A[i] then \\
   \t\t\t \pccomment{swap them and remember something changed} \\
   \t\t\t     swap( A[i-1], A[i] ) \\
   \t\t\t       s \gets true \\
   until \neg s }
\end{lstlisting}

\subsection{Predefined Headings}
\index{procedure}
\index{mainprocedure}
\index{circuit}
\index{program}

Besides the  \lstinline{\pseudocode}  command the commands  \lstinline{\procedure},  \lstinline{\mainprocedure},  \lstinline{\circuit}  and  \lstinline{\program}  
provide easy access to generate code with common headers. They all take two mandatory arguments and an optional argument.
\begin{lstlisting}
\procedure[options]{Header}{Body}
\end{lstlisting}

\subsubsection{Examples}
\begin{center}
\fbox{%
\procedure{$\indcpa_\enc^\adv$}{%
   b \sample \bin  \\
   (\pk,\sk) \sample \kgen(\secparam)  \\
   (m_0,m_1) \sample \adv(\secparam, \pk, c)  \\
   c \sample \enc(\pk,m_b)  \\
   b' \sample \adv(\secparam, \pk, c)  \\
   \pcreturn b = b' }
}
\end{center}
which is generated as
\begin{lstlisting}
\procedure{$\indcpa_\enc^\adv$}{%
   b \sample \bin  \\
   (\pk,\sk) \sample \kgen(\secparam)  \\
   (m_0,m_1) \sample \adv(\secparam, \pk, c)  \\
   c \sample \enc(\pk,m_b)  \\
   b' \sample \adv(\secparam, \pk, c)  \\
   \pcreturn b = b' }
\end{lstlisting}

And the same with  \lstinline{\mainprocedure},  \lstinline{\circuit},  \lstinline{\program} and \lstinline{\algorithm}:

\begin{center}
\begin{pcvstack}
\begin{pchstack}
\mainprocedure{$\indcpa_\enc^\adv$}{%
   b \sample \bin  \\
   (\pk,\sk) \sample \kgen(\secparam)  \\
   (m_0,m_1) \sample \adv(\secparam, \pk, c)  \\
   c \sample \enc(\pk,m_b)  \\
   b' \sample \adv(\secparam, \pk, c)  \\
   \pcreturn b = b' }
\hspace{2cm}
\circuit{$\indcpa_\enc^\adv$}{%
   b \sample \bin  \\
   (\pk,\sk) \sample \kgen(\secparam)  \\
   (m_0,m_1) \sample \adv(\secparam, \pk, c)  \\
   c \sample \enc(\pk,m_b)  \\
   b' \sample \adv(\secparam, \pk, c)  \\
   \pcreturn b = b' }
\end{pchstack}

\vspace{0.5cm}

\begin{pchstack}
\program{$\indcpa_\enc^\adv$}{%
   b \sample \bin  \\
   (\pk,\sk) \sample \kgen(\secparam)  \\
   (m_0,m_1) \sample \adv(\secparam, \pk, c)  \\
   c \sample \enc(\pk,m_b)  \\
   b' \sample \adv(\secparam, \pk, c)  \\
   \pcreturn b = b' }
\hspace{2cm}
\algorithm{$\indcpa_\enc^\adv$}{%
   b \sample \bin  \\
   (\pk,\sk) \sample \kgen(\secparam)  \\
   (m_0,m_1) \sample \adv(\secparam, \pk, c)  \\
   c \sample \enc(\pk,m_b)  \\
   b' \sample \adv(\secparam, \pk, c)  \\
   \pcreturn b = b' }
\end{pchstack}
\end{pcvstack}
\end{center}


The prefixes (Main, Circ, etc.) can be controlled via the commands:

\begin{center}
\begin{tabular}{ll}
\textbf{Command} & \textbf{Default} \\
\lstinline$\pcmainname$ & \lstinline$\textsc{Main }$ \\
\lstinline$\pcalgorithmname$ & \lstinline$\textsc{Algo. }$\\
\lstinline$\pccircuitname$ & \lstinline$\textsc{Circ. }$\\
\lstinline$\pcprogramname$ & \lstinline$\textsc{Prog. }$\\
\lstinline$\pcprotocolname$ & \lstinline$\textsc{Protocol }$
\end{tabular}
\end{center}


\section{Line Numbering}
\index{line numbering}
\index{pcln}
\index{pclnr}
\index{pcrln}
The pseudocode command allows to insert line numbers into pseudocode. You can either manually control linenumbering or simply turn on the option
\enquote{linenumbering}.
\begin{center}
\fbox{%
\procedure[linenumbering]{$\indcpa_\enc^\adv$}{%
  b \sample \bin \\
 (\pk,\sk) \sample \kgen\secparam)  \\
  \label{tmp:line:label}   (m_0,m_1) \sample \adv(\secparam, \pk, c)  \\
  c \sample \enc(\pk,m_b)  \\
  b' \sample \adv(\secparam, \pk, c)  \\
 \pcreturn b = b' }
}
\end{center}
is generated by 
\begin{lstlisting}
\procedure[linenumbering]{$\indcpa_\enc^\adv$}{%
  b \sample \bin \\
 (\pk,\sk) \sample \kgen\secparam)  \\
  \label{tmp:line:label}   (m_0,m_1) \sample \adv(\secparam, \pk, c)  \\
  c \sample \enc(\pk,m_b)  \\
  b' \sample \adv(\secparam, \pk, c)  \\
 \pcreturn b = b' }
\end{lstlisting}
Note how you can use labels such as \lstinline$\label{tmp:line:label}$ which now points to \ref{tmp:line:label}.

\subsection{Manually Inserting Linenumbers}
In order to manually insert line numbers use the command \lstinline$\pcln$.
\begin{center}
\fbox{%
\procedure{$\indcpa_\enc^\adv$}{%
 \pcln  b \sample \bin \\
 \pcln   (\pk,\sk) \sample \kgen\secparam)  \\
 \pcln\label{tmp:line:label2}   (m_0,m_1) \sample \adv(\secparam, \pk, c)  \\
 \pcln   c \sample \enc(\pk,m_b)  \\
 \pcln   b' \sample \adv(\secparam, \pk, c)  \\
 \pcln   \pcreturn b = b' }
}
\end{center}
is generated by 
\begin{lstlisting}
\procedure{$\indcpa_\enc^\adv$}{%
 \pcln  b \sample \bin \\
 \pcln   (\pk,\sk) \sample \kgen\secparam)  \\
 \pcln\label{tmp:line:label2}   (m_0,m_1) \sample \adv(\secparam, \pk, c)  \\
 \pcln   c \sample \enc(\pk,m_b)  \\
 \pcln   b' \sample \adv(\secparam, \pk, c)  \\
 \pcln   \pcreturn b = b' }
\end{lstlisting}
Note that the label \emph{tmp:line:label2} now points to line number \ref{tmp:line:label2}.



\subsection{Start Values}
You can specify the start value (-1) of the counter by setting the option \enquote{lnstart}.
\begin{lstlisting}
\procedure[lnstart=10,linenumbering]{Header}{Body}
\end{lstlisting}
\begin{center}
\fbox{%
\procedure[lnstart=10,linenumbering]{$\indcpa_\enc^\adv$}{%
  b \sample \bin \\
   (\pk,\sk) \sample \kgen\secparam)  \\
   (m_0,m_1) \sample \adv(\secparam, \pk, c)  \\
   c \sample \enc(\pk,m_b)  \\
   b' \sample \adv(\secparam, \pk, c)  \\
   \pcreturn b = b' }
}
\end{center}

\subsection{Separators}
\index{pclnseparator}
The commands \lstinline$\pclnseparator$ defines the separator between the pseudocode and the line numbering. By
default the left separator is set to (:) colon. Also see Section~\ref{sec:protocol:separator}.


\section{Subprocedures}
\index{subprocedure}
The pseudocode package allows the typesetting of sub procedures such as

\begin{center}
\procedure[linenumbering]{$\indcpa_\enc^\adv$}{%
   b \sample \bin  \\
   (\pk,\sk) \sample \kgen(\secparam)  \\
   (m_0,m_1) \sample \begin{subprocedure}%
 \dbox{\procedure{$\adv(\secparam, \pk, c)$}{%
  	 \text{Step 1}  \\
  	 \text{Step 2}  \\
	 \pcreturn m_0, m_1 }}
  \end{subprocedure}  \\
   c \sample \enc(\pk,m_b)  \\
   b' \sample \adv(\secparam, \pk, c)  \\
   \pcreturn b = b' }
 \end{center}
 
To create a subprocedure use the \emph{subprocedure} environment. The above example is generated via
 \begin{lstlisting}
\procedure[linenumbering]{$\indcpa_\enc^\adv$}{%
   b \sample \bin  \\
   (\pk,\sk) \sample \kgen(\secparam)  \\
   (m_0,m_1) \sample \begin{subprocedure}%
 \dbox{\procedure{$\adv(\secparam, \pk, c)$}{%
  	 \text{Step 1}  \\
  	 \text{Step 2}  \\
	 \pcreturn m_0, m_1 }}
  \end{subprocedure}  \\
   c \sample \enc(\pk,m_b)  \\
   b' \sample \adv(\secparam, \pk, c)  \\
   \pcreturn b = b' }
\end{lstlisting}
Here the dbox command (from the dashbox package) is used to generate a dashed box around the sub procedure. 

\subsection{Numbering in Subprocedures}
Subprocedures as normal pseudocode allow you to create line numbers. By default the line numbering starts with 1 in a subprocedure while
ensuring that the outer numbering remains intact. Also note that the linenumbering on the outer procedure in the above example is inherited
by the subprocedure. For more control, either use manual numbering or set the option \enquote{linenumbering=off} on the subprocedure.

\begin{center}
\procedure{$\indcpa_\enc^\adv$}{%
  \pcln  b \sample \bin  \\
  \pcln  (\pk,\sk) \sample \kgen(\secparam)  \\
  \pcln  (m_0,m_1) \sample \begin{subprocedure}%
 \dbox{\procedure{$\adv(\secparam, \pk, c)$}{%
  	\pcln \text{Step 1}  \\
  	\pcln \text{Step 2}  \\
	\pcln \pcreturn m_0, m_1 }}
  \end{subprocedure}  \\
 \pcln   c \sample \enc(\pk,m_b)  \\
 \pcln   b' \sample \adv(\secparam, \pk, c)  \\
 \pcln   \pcreturn b = b' }
 \end{center}
  \begin{lstlisting}
\procedure{$\indcpa_\enc^\adv$}{%
  \pcln  b \sample \bin  \\
  \pcln  (\pk,\sk) \sample \kgen(\secparam)  \\
  \pcln  (m_0,m_1) \sample \begin{subprocedure}%
  \dbox{\procedure{$\adv(\secparam, \pk, c)$}{%
  	\pcln \text{Step 1}  \\
  	\pcln \text{Step 2}  \\
	\pcln \pcreturn m_0, m_1 }}
  \end{subprocedure}  \\
 \pcln   c \sample \enc(\pk,m_b)  \\
 \pcln   b' \sample \adv(\secparam, \pk, c)  \\
 \pcln   \pcreturn b = b' }
\end{lstlisting}


\section{Stacking Procedures}
You can stack procedures horizontally or vertically using the environments ``pchstack'' and ``pcvstack''. 
\begin{lstlisting}
\begin{pchstack}[center] body \end{pchstack}
\begin{pcvstack}[center] body \end{pcvstack}
\end{lstlisting}

The following example displays two procedures next to one another. As a spacing between two horizontally outlined procedures
use \lstinline{\pchspace} which takes an optional length as a parameter.

\begin{pchstack}[center]
\procedure{$\indcpa_\enc^\adv$}{%
  \pcln  b \sample \bin  \\
  \pcln  (\pk,\sk) \sample \kgen(\secparam)  \\
  \pcln  (m_0,m_1) \sample \adv^O(\secparam, \pk)  \\
 \pcln   c \sample \enc(\pk,m_b)  \\
 \pcln   b' \sample \adv(\secparam, \pk, c)  \\
 \pcln   \pcreturn b = b' }

\pchspace

 \procedure{Oracle $O$}{%
  \pcln  \text{line 1}  \\
  \pcln  \text{line 2}  
}
\end{pchstack}

\begin{lstlisting}
\begin{pchstack}[center]
\procedure{$\indcpa_\enc^\adv$}{%
  \pcln  b \sample \bin  \\
  \pcln  (\pk,\sk) \sample \kgen(\secparam)  \\
  \pcln  (m_0,m_1) \sample \adv^O(\secparam, \pk)  \\
 \pcln   c \sample \enc(\pk,m_b)  \\
 \pcln   b' \sample \adv(\secparam, \pk, c)  \\
 \pcln   \pcreturn b = b' }

\pchspace

 \procedure{Oracle $O$}{%
  \pcln  \text{line 1}  \\
  \pcln  \text{line 2}  
}
\end{pchstack}
\end{lstlisting}


Similarly you can stack two procedures vertically using the \enquote{pcvstack} environment. As a spacing between two vertically stacked procedures
use \lstinline{\pcvspace} which takes an optional length as a parameter.


\begin{pcvstack}[center]
\procedure{$\indcpa_\enc^\adv$}{%
  \pcln  b \sample \bin  \\
  \pcln  (\pk,\sk) \sample \kgen(\secparam)  \\
  \pcln  (m_0,m_1) \sample \adv^O(\secparam, \pk)  \\
 \pcln   c \sample \enc(\pk,m_b)  \\
 \pcln   b' \sample \adv(\secparam, \pk, c)  \\
 \pcln   \pcreturn b = b' }

\pcvspace

 \procedure{Oracle $O$}{%
  \pcln  \text{line 1}  \\
  \pcln  \text{line 2}  
}
\end{pcvstack}


\begin{lstlisting}
\begin{pcvstack}[center]
\procedure{$\indcpa_\enc^\adv$}{%
  \pcln  b \sample \bin  \\
  \pcln  (\pk,\sk) \sample \kgen(\secparam)  \\
  \pcln  (m_0,m_1) \sample \adv^O(\secparam, \pk)  \\
 \pcln   c \sample \enc(\pk,m_b)  \\
 \pcln   b' \sample \adv(\secparam, \pk, c)  \\
 \pcln   \pcreturn b = b' }

\pcvspace

 \procedure{Oracle $O$}{%
  \pcln  \text{line 1}  \\
  \pcln  \text{line 2}  
}
\end{pcvstack}
\end{lstlisting}


Horizontal and vertical stacking can be combined
\begin{pchstack}[center]
\begin{pcvstack}
\procedure{$\indcpa_\enc^\adv$}{%
  \pcln  b \sample \bin  \\
  \pcln  (\pk,\sk) \sample \kgen(\secparam)  \\
  \pcln  (m_0,m_1) \sample \adv^{O,H_1,H_2}(\secparam, \pk)  \\
 \pcln   c \sample \enc(\pk,m_b)  \\
 \pcln   b' \sample \adv(\secparam, \pk, c)  \\
 \pcln   \pcreturn b = b' }

\pcvspace

\begin{pchstack}
 \procedure{Oracle $O$}{%
  \pcln  \text{line 1}  \\
  \pcln  \text{line 2}  
}

 \procedure{Oracle $H_1$}{%
  \pcln  \text{line 1}  \\
  \pcln  \text{line 2}  
}

 \procedure{Oracle $H_2$}{%
  \pcln  \text{line 1}  \\
  \pcln  \text{line 2}  
}
\end{pchstack}
\end{pcvstack}

\pchspace

\procedure{$\indcpa_\enc^\adv$}{%
  \pcln  b \sample \bin  \\
  \pcln  (\pk,\sk) \sample \kgen(\secparam)  \\
  \pcln  (m_0,m_1) \sample \adv^O(\secparam, \pk)  \\
 \pcln   c \sample \enc(\pk,m_b)  \\
 \pcln   b' \sample \adv(\secparam, \pk, c)  \\
 \pcln   \pcreturn b = b' }

\end{pchstack}


\begin{lstlisting}
\begin{pchstack}[center]
\begin{pcvstack}
\procedure{$\indcpa_\enc^\adv$}{%
  \pcln  b \sample \bin  \\
  \pcln  (\pk,\sk) \sample \kgen(\secparam)  \\
  \pcln  (m_0,m_1) \sample \adv^{O,H_1,H_2}(\secparam, \pk)  \\
 \pcln   c \sample \enc(\pk,m_b)  \\
 \pcln   b' \sample \adv(\secparam, \pk, c)  \\
 \pcln   \pcreturn b = b' }

\pcvspace

\begin{pchstack}
 \procedure{Oracle $O$}{%
  \pcln  \text{line 1}  \\
  \pcln  \text{line 2}  
}

 \procedure{Oracle $H_1$}{%
  \pcln  \text{line 1}  \\
  \pcln  \text{line 2}  
}

 \procedure{Oracle $H_2$}{%
  \pcln  \text{line 1}  \\
  \pcln  \text{line 2}  
}
\end{pchstack}
\end{pcvstack}

\pchspace

\procedure{$\indcpa_\enc^\adv$}{%
  \pcln  b \sample \bin  \\
  \pcln  (\pk,\sk) \sample \kgen(\secparam)  \\
  \pcln  (m_0,m_1) \sample \adv^O(\secparam, \pk)  \\
 \pcln   c \sample \enc(\pk,m_b)  \\
 \pcln   b' \sample \adv(\secparam, \pk, c)  \\
 \pcln   \pcreturn b = b' }

\end{pchstack}
\end{lstlisting}

\section{Divisions and Linebreaks}
\index{linebreaks}
\index{hline}
Within the pseudocode command you generate linebreaks as $\\$. In order to specify the linewidth you can add an optional argument
\begin{lstlisting}
\\[height]
\end{lstlisting}
Furthermore, you can add, for example a horizontal line by using the second optional argument and write
\begin{lstlisting}
\\[][\hline]
\end{lstlisting}

\begin{center}
\fbox{
\procedure{$\indcpa_\enc^\adv$}{%
  \pcln  b \sample \bin  \\[2\baselineskip][\hline\hline]
  \pcln  (\pk,\sk) \sample \kgen(\secparam)  \\
  \pcln  (m_0,m_1) \sample \adv^O(\secparam, \pk)  \\
 \pcln   c \sample \enc(\pk,m_b)  \\
 \pcln   b' \sample \adv(\secparam, \pk, c)  \\
 \pcln   \pcreturn b = b' }
}
\end{center}

\begin{lstlisting}
\procedure{$\indcpa_\enc^\adv$}{%
  \pcln  b \sample \bin  \\[2\baselineskip][\hline\hline]
  \pcln  (\pk,\sk) \sample \kgen(\secparam)  \\
  \pcln  (m_0,m_1) \sample \adv^O(\secparam, \pk)  \\
 \pcln   c \sample \enc(\pk,m_b)  \\
 \pcln   b' \sample \adv(\secparam, \pk, c)  \\
 \pcln   \pcreturn b = b' }
\end{lstlisting}


\section{Fancy Code}

Consider the \indcpa\ game. Here we have a single adversary $\adv$ that is called twice, first to output two messages then
given the ciphertext of one of the messages to \enquote{guess} which one was encrypted. Often this is not visualized.
Sometimes an additional state $\state$ is passed as we have in the following example on the left. On the right,
we visualize the same thing in a bit more fancy way.

\begin{center}

\pchstack{

\procedure{$\indcpa_\enc^\adv$}{%
  \pcln  b \sample \bin  \\
  \pcln  (\pk,\sk) \sample \kgen (\secparam)  \\
  \pcln  (\state,m_0,m_1) \sample \adv(\secparam, \pk, c) \pcnode{start}  \\
 \pcln   c \sample \enc(\pk,m_b)  \\
 \pcln   b' \sample \adv(\secparam, \pk, c, \state) \pcnode{end} \\
 \pcln   \pcreturn b = b' }


\pchspace[1cm]

\begin{pcimage}
\procedure{$\indcpa_\enc^\adv$}{%
	\pcln  b \sample \bin  \\
	\pcln  (\pk,\sk) \sample \kgen (\secparam)  \\
	\pcln  (m_0,m_1) \sample \adv(\secparam, \pk, c) \pcnode{start}  \\
	\pcln   c \sample \enc(\pk,m_b)  \\
	\pcln   b' \sample \adv(\secparam, \pk, c, \state) \pcnode{end} \\
	\pcln   \pcreturn b = b' }
	
\pcdraw{
	\path[->] (start) edge[bend left=50] node[right]{$\state$} (start|-end);
}
\end{pcimage}

}
\end{center}

The image on the right is generated by:
\begin{lstlisting}
\begin{pcimage}
\procedure{$\indcpa_\enc^\adv$}{%
	\pcln  b \sample \bin  \\
	\pcln  (\pk,\sk) \sample \kgen (\secparam)  \\
	\pcln  (m_0,m_1) \sample \adv(\secparam, \pk, c) \pcnode{start}  \\
	\pcln   c \sample \enc(\pk,m_b)  \\
	\pcln   b' \sample \adv(\secparam, \pk, c, \state) \pcnode{end} \\
	\pcln   \pcreturn b = b' }
	
\pcdraw{
	\path[->] (start) edge[bend left=50] node[right]{$\state$} (start|-end);
}
\end{pcimage}
\end{lstlisting}

In order to achieve the above effect cryptocode utilizes TIKZ underneath. The
\lstinline$pcnode$ command generates TIKZ nodes and additionally we wrapped the 
pseudocode (or procedure) command in an \lstinline$\begin{pcimage}\end{pcimage}$ environment
which allows us to utilize these nodes later, for example using the  \lstinline$\pcdraw$
command. We can achieve a similar effect without an additional pcimage environment as

\begin{lstlisting}
\procedure{$\indcpa_\enc^\adv$}{%
	\pcln  b \sample \bin  \\
	\pcln  (\pk,\sk) \sample \kgen (\secparam)  \\
	\pcln  (m_0,m_1) \sample \adv(\secparam, \pk, c) \pcnode{start}  \\
	\pcln   c \sample \enc(\pk,m_b)  \\
	\pcln   b' \sample \adv(\secparam, \pk, c, \state) \pcnode{end}[draw={
		\path[->] (start) edge[bend left=50] node[right]{$\state$} (start|-end);
	}] \\
	\pcln   \pcreturn b = b' }
\end{lstlisting}

\subsection{Example: Explain your Code}
As an exmaple of what you can do with this, let us put an explanation to a line of the code.

\begin{center}
\begin{pcimage}
\procedure{$\indcpa_\enc^\adv$}{%
	\pcln  b \sample \bin  \\
	\pcln  (\pk,\sk) \sample \kgen (\secparam)\pcnode{kgen}  \\
	\pcln  (m_0,m_1) \sample \adv(\secparam, \pk, c)  \\
	\pcln   c \sample \enc(\pk,m_b)  \\
	\pcln   b' \sample \adv(\secparam, \pk, c, \state) \\
	\pcln   \pcreturn b = b' }

\pcdraw{
  \node[rectangle callout,callout absolute pointer=(kgen),fill=orange]
      at ([shift={(+3,+1)}]kgen) {
		\begin{varwidth}{3cm}
			$\kgen(\secparam)$ samples a public key $\pk$ and a private key $\sk$.
		\end{varwidth}
  };
}
\end{pcimage}
\end{center}

\begin{lstlisting}
\begin{center}
\begin{pcimage}
\procedure{$\indcpa_\enc^\adv$}{%
	\pcln  b \sample \bin  \\
	\pcln  (\pk,\sk) \sample \kgen (\secparam)\pcnode{kgen}  \\
	\pcln  (m_0,m_1) \sample \adv(\secparam, \pk, c)  \\
	\pcln   c \sample \enc(\pk,m_b)  \\
	\pcln   b' \sample \adv(\secparam, \pk, c, \state) \\
	\pcln   \pcreturn b = b' }

\pcdraw{
  \node[rectangle callout,callout absolute pointer=(kgen),fill=orange]
      at ([shift={(+3,+1)}]kgen) {
		\begin{varwidth}{3cm}
			$\kgen(\secparam)$ samples a public key $\pk$ and a private key $\sk$.
		\end{varwidth}
  };
}
\end{pcimage}
\end{center}
\end{lstlisting}

Using the \emph{ocgx} package (\url{https://www.ctan.org/pkg/ocgx}) we could even make the
$\kgen$ command clickable:

\begin{center}
\begin{pcimage}
\procedure{$\indcpa_\enc^\adv$}{%
	\pcln  b \sample \bin  \\
	\pcln  (\pk,\sk) \sample \switchocg{kgen}{\kgen (\secparam)}\pcnode{kgen}  \\
	\pcln  (m_0,m_1) \sample \adv(\secparam, \pk, c)  \\
	\pcln   c \sample \enc(\pk,m_b)  \\
	\pcln   b' \sample \adv(\secparam, \pk, c, \state) \\
	\pcln   \pcreturn b = b' }

\pcdraw{
  \begin{scope}[ocg={ref=kgen,status=invisible,name=Key Generation Explanation}]
  \node[rectangle callout,callout absolute pointer=(kgen),fill=orange]
      at ([shift={(+3,+1)}]kgen) {
		\begin{varwidth}{3cm}
			$\kgen(\secparam)$ samples a public key $\pk$ and a private key $\sk$.
		\end{varwidth}
  };
  \end{scope}
}
\end{pcimage}
\end{center}

(Click on $\kgen$ to see the magic.)

\begin{lstlisting}
\begin{center}
\begin{pcimage}
\procedure{$\indcpa_\enc^\adv$}{%
	\pcln  b \sample \bin  \\
	\pcln  (\pk,\sk) \sample \switchocg{kgen}{\kgen (\secparam)}\pcnode{kgen}  \\
	\pcln  (m_0,m_1) \sample \adv(\secparam, \pk, c)  \\
	\pcln   c \sample \enc(\pk,m_b)  \\
	\pcln   b' \sample \adv(\secparam, \pk, c, \state) \\
	\pcln   \pcreturn b = b' }

\pcdraw{
  \begin{scope}[ocg={ref=kgen,status=invisible,name=Key Generation Explanation}]
  \node[rectangle callout,callout absolute pointer=(kgen),fill=orange]
      at ([shift={(+3,+1)}]kgen) {
		\begin{varwidth}{3cm}
			$\kgen(\secparam)$ samples a public key $\pk$ and a private key $\sk$.
		\end{varwidth}
  };
  \end{scope}
}
\end{pcimage}
\end{center}
\end{lstlisting}


\chapter{Tabbing Mode}
\label{chap:tabbing}
\index{Tabbing Mode}
\index{mintabmode}
In the following chapter we discuss how to create multiple columns within a \lstinline$pseudocode$ command.
Within a \lstinline$pseudocode$ command you can switch to a new column by inserting a \lstinline$\>$. 
This is similar to using an \lstinline$align$ environment and placing a tabbing \& character. Also,
similarly to using \lstinline$align$ you need to ensure that the number of \lstinline$\>$ are identical
on each line.
\begin{center}
\fbox{%
\pseudocode{%
 \textbf{First} \> \textbf{Second} \> \textbf{Third} \> \textbf{Fourth} \\
  b \sample \bin \> b \sample \bin \> b \sample \bin \> b \sample \bin}
}
\end{center}
\begin{lstlisting}
\pseudocode{%
 \textbf{First} \> \textbf{Second} \> \textbf{Third} \> \textbf{Fourth} \\
  b \sample \bin \> b \sample \bin \> b \sample \bin \> b \sample \bin}
\end{lstlisting}
As you can see the first column is left aligned the second right, the third left and so forth.
In order to get only left aligned columns you could thus simply always skip a column by
using \lstinline$\>\>$. You can also use \lstinline$\<$ a shorthand for \lstinline$\>\>$.
\begin{center}
\fbox{%
\pseudocode{%
 \textbf{First} \< \textbf{Second} \< \textbf{Third} \< \textbf{Fourth} \\
  b \sample \bin \< b \sample \bin \< b \sample \bin \< b \sample \bin}
}
\end{center}
\begin{lstlisting}
\pseudocode{%
 \textbf{First} \< \textbf{Second} \< \textbf{Third} \< \textbf{Fourth} \\
  b \sample \bin \< b \sample \bin \< b \sample \bin \< b \sample \bin}
\end{lstlisting}

This is basically all you need to know in order to go on to writing protocols with the
cryptocode package. So unless you want to know a bit more about tabbing (switching columns)
and learn some of the internals, feel free to proceed to Chapter~\ref{chap:protocols}.


\section{Tabbing in Detail}
At the heart of the pseudocode package is an align (or rather a flalign*) environment which allows you to use basic math writing. Usually an align (or flalign) environment uses \& as tabbing characters. The pseudocode comes in two modes the first of which changes the default align behavior. That is, it automatically adds a tabbing character to the beginning and end of each line and changes the tabbing character to \lstinline$\>$. This mode is called mintabmode and is active by default.

In mintabmode in order to make use of extra columns in the align environment (which we will use shortly in order to write protocols) you can use \lstinline$\>$ as you would use \& normally. But, don't forget that there is an alignment tab already placed at the beginning and end of each line. So the following example
\begin{center}
\fbox{%
\pseudocode{%
 \textbf{Alice} \> \> \textbf{Bob}  \\
 b \sample \bin \> \> \\
 \> \xrightarrow{\text{send over } b} \>  \\
 \> \> \text{do something}}
}
\end{center}
is generated by
\begin{lstlisting}
\pseudocode{%
 \textbf{Alice} \> \> \textbf{Bob}  \\
 b \sample \bin \> \> \\
 \> \xrightarrow{\text{send over } b} \>  \\
 \> \> \text{do something}}
\end{lstlisting}

In Chapter~\ref{chap:protocols} we'll discuss how to write protocols in detail.
The next two sections are rather technical, so feel free to skip them.


\subsection{Overriding The Tabbing Character}
\index{pctabname}
If you don't like \lstinline$\>$ as the tabbing character you can choose a custom command by overwriting \lstinline$\pctabname$.
For example
\begin{lstlisting}
\renewcommand{\pctabname}{\myTab}

\pseudocode{%
 \textbf{Alice} \myTab \myTab \textbf{Bob}  \\
 b \sample \bin \myTab \myTab \\
 \myTab \xrightarrow{\text{send over } b} \myTab  \\
 \myTab \myTab \text{do something}}
\end{lstlisting}

\subsection{Custom Line Spacing and Horizontal Rules}
\index{pclb}
As explained underlying the pseudocode command is an flalign environment. This would allow the use
of \lstinline$\\[spacing]$ to specifiy the spacing between two lines or of \lstinline$[\\\hline]$ 
to insert a horizontal rule. In order to achieve the same effect within the pseudocode command you
can use \lstinline$\\{[spacing][\hline]}$. You can also use \lstinline$\pclb$ to get a line break which
does not insert the additional alignment characters. 

\chapter{Protocols}
\label{chap:protocols}

The pseudocode package can also be used to write protocols such as
\begin{center}
\fbox{%
\protocol{My Protocol}{%
 \textbf{Alice} \> \> \textbf{Bob}  \\
 b \sample \bin \> \> \\
 \> \xrightarrow{\text{send over } b} \>  \\
 \> \> \text{do something}    \\
 \> \xleftarrow{\text{send over sth. else}} \>   \\
 \text{finalize} \> \>}
 }
\end{center}
which uses the tabbing feature of align and is generated as
\begin{lstlisting}
\protocol{My Protocol}{%
 \textbf{Alice} \> \> \textbf{Bob}  \\
 b \sample \bin \> \> \\
 \> \xrightarrow{\text{send over } b} \>  \\
 \> \> \text{do something}    \\
 \> \xleftarrow{\text{send over sth. else}} \>   \\
 \text{finalize} \> \>}
}
\end{lstlisting}
In order to get nicer message arrows use the commands \lstinline$\sendmessageright*{message}$ and \lstinline$\sendmessageleft*{message}$.
Both take an additional optional argument specifying the length of the arrow and both are run
in math mode.
\begin{lstlisting}
\sendmessageright*[3.5cm]{message}
\sendmessageleft*[3.5cm]{message}
\end{lstlisting}


\begin{center}
\fbox{%
\protocol{My Protocol}{%
 \textbf{Alice} \> \> \textbf{Bob}  \\
 b \sample \bin \> \> \\
 \> \sendmessageright*{\text{send over } b} \>  \\
 \> \> \text{do something}    \\
 \> \sendmessageleft*{\text{send over sth. else}} \>   \\
 \text{finalize} \> \> }
 }
\end{center}
\begin{lstlisting}
\protocol{My Protocol}{%
 \textbf{Alice} \> \> \textbf{Bob}  \\
 b \sample \bin \> \> \\
 \> \sendmessageright*{\text{send over } b} \>  \\
 \> \> \text{do something}    \\
 \> \sendmessageleft*{\text{send over sth. else}} \>   \\
 \text{finalize} \> \> }
\end{lstlisting}

Besides the starred version there is also the unstarred version which allows more flexibility. Note that a crucial difference between
the starred and unstarred versions are that  \lstinline$\sendmessageleft*{message}$ wraps an aligned environment around the message.
\begin{center}
\protocol{My Protocol}{%
 \textbf{Alice} \> \> \textbf{Bob}  \\
 b \sample \bin \> \> \\
 \> \sendmessageright{centercol=3,top=send over $b$,bottom=Text below,topstyle={draw,solid,yshift=0.25cm},style={dashed}} \>  \\
 \> \> \text{do something}    \\
 \> \sendmessageleft{length=8cm,top=send over sth. else} \>   \\
 \text{finalize} \> \> }
\end{center}
\begin{lstlisting}
\protocol{My Protocol}{%
 \textbf{Alice} \> \> \textbf{Bob}  \\
 b \sample \bin \> \> \\
 \> \sendmessageright{centercol=3,top=send over $b$,bottom=Text below,topstyle={draw,solid,yshift=0.25cm},style={dashed}} \>  \\
 \> \> \text{do something}    \\
 \> \sendmessageleft{length=8cm,top=send over sth. else} \>   \\
 \text{finalize} \> \> }
\end{lstlisting}
The unstarred commands take key-value pairs. The following keys are available:
\begin{description}
\item[top]
The content to display on top of the arrow. 
\item[bottom]
The content to display below the arrow.
\item[left]
The content to display on the left of the arrow.
\item[right]
The content to display on the right of the arrow.
\item[topstyle]
The TIKZ style to be used for the top node.
\item[bottomstyle]
The TIKZ style to be used for the bottom node.
\item[rightstyle]
The TIKZ style to be used for the right node.
\item[leftstyle]
The TIKZ style to be used for the left node.
\item[length]
The length of the arrow.
\item[style]
The style of the arrow.
\item[width]
The width of the column
\item[centercol]
Can be used to ensure that the message is displayed in the center. This should be set to the
column index. In the above example, the message column is the third column (note that there is
a column left of alice that  is automatically inserted.).
\end{description}


\section{Tabbing}
When typesetting protocols you might find that using two tabs instead of a single tab usually provides
a better result as this ensures that all columns are left aligned. 
For this you can use \lstinline$\<$ instead of \lstinline$\>$ (see Chapter~\ref{chap:tabbing}).

Following is once more the example from before but now with double tapping. 
\begin{center}
\fbox{%
\protocol{My Protocol}{%
 \textbf{Alice} \< \< \textbf{Bob}  \\
 b \sample \bin \< \< \\
 \< \sendmessageright*{\text{send over } b} \<  \\
 \< \< \text{do something}    \\
 \< \sendmessageleft*{\text{send over sth. else}} \<   \\
 \text{finalize} \< \< }
 }
\end{center}

\section{Multiline Messages}
Using the send message commands you can easily generate multiline messages as the command
wraps an \emph{aligned} environment around the message.
\begin{center}
\fbox{%
\protocol{My Protocol}{%
 \textbf{Alice} \< \< \textbf{Bob}  \\
 b \sample \bin \< \< \\
 \< \sendmessageright*{\text{send over } b\\ \text{second line}} \<  \\
 \< \< \text{do something}    \\
 \< \sendmessageleft*{&\text{left aligned}\\ &\text{multiline message}} \<   \\
 \text{finalize} \< \<}
 }
\end{center}
\begin{lstlisting}
\protocol{My Protocol}{%
 \textbf{Alice} \< \< \textbf{Bob}  \\
 b \sample \bin \< \< \\
 \< \sendmessageright*{\text{send over } b\\ \text{second line}} \<  \\
 \< \< \text{do something}    \\
 \< \sendmessageleft*{&\text{left aligned}\\ &\text{multiline message}} \<   \\
 \text{finalize} \< \<}
\end{lstlisting}



\subsection{Multiplayer Protocols}

You are not limited to two players. In order to send messages skipping players use \lstinline$\sendmessagerightx$ and \lstinline$\sendmessageleftx$.
\begin{lstlisting}
\sendmessagerightx[width]{columnspan}{Text}
\sendmessageleftx[width]{columnspan}{Text}
\end{lstlisting}

\begin{center}
\fbox{%
\protocol{Multiparty Protocol}{%
 \textbf{Alice} \< \< \textbf{Bob} \< \< \textbf{Charlie} \\
 \text{work} \< \< \< \< \\
 \< \sendmessageright{top=Work result} \< \< \< \\
 \< \< \text{work} \< \< \\
 \< \< \<  \sendmessageright{top=Work result,bottom=Bottom message} \< \\
 \< \<  \< \< \text{work} \\
 \< \sendmessageleftx[7cm]{8}{\text{A long message for Alice}} \< \\
 \text{finalize} \< \< \< \< }
}
\end{center}

\begin{lstlisting}
\begin{center}
\protocol{Multiparty Protocol}{%
 \textbf{Alice} \< \< \textbf{Bob} \< \< \textbf{Charlie} \\
 \text{work} \< \< \< \< \\
 \< \sendmessageright{top=Work result} \< \< \< \\
 \< \< \text{work} \< \< \\
 \< \< \<  \sendmessageright{top=Work result,bottom=Bottom message} \< \\
 \< \<  \< \< \text{work} \\
 \< \sendmessageleftx[7cm]{8}{\text{A long message for Alice}} \< \\
 \text{finalize} \< \< \< \< }
\end{lstlisting}
Note that for the last message from Charlie to Alice we needed to specify the number of passed
over colums (\lstinline$\sendmessageleftx[7cm]{8}{message}$). As we were passing
4 \lstinline$\<$ where each creates 2 columns, the total was 8 columns.


\subsection{Divisions}
You can use \lstinline$\pcintertext$ in order to divide protocols (or other pseudocode for that matter).
\begin{lstlisting}
\pcintertext[dotted|center]{Division Text}
\end{lstlisting}
Note that in order to use the \lstinline$\pcintertext$ you need to use \lstinline$\pclb$ as the line break for the line
before. Also see Chapter~\ref{chap:tabbing}.

\begin{center}
\fbox{%
\protocol{My Protocol}{%
\textbf{Alice} \< \< \textbf{Bob}  \\
 b \sample \bin \< \<  \pclb
 \pcintertext[dotted]{Some Division} \\
 \< \sendmessageright*{\text{send over } b} \<  \\
 \< \< \text{do something} \pclb
 \pcintertext[dotted]{Another Division} \\
 \< \sendmessageleft*{\text{message}} \<   \\
 \text{finalize} \< \< }
}
\end{center}

\begin{lstlisting}
\protocol{My Protocol}{%
\textbf{Alice} \< \< \textbf{Bob}  \\
 b \sample \bin \< \<  \pclb
 \pcintertext[dotted]{Some Division} \\
 \< \sendmessageright*{\text{send over } b} \<  \\
 \< \< \text{do something} \pclb
 \pcintertext[dotted]{Another Division} \\
 \< \sendmessageleft*{\text{message}} \<   \\
 \text{finalize} \< \< }
\end{lstlisting}



\section{Line Numbering in Protocols}
Protocols can be numbered similarly to plain pseudocode. Additionally to the \lstinline$\pcln$ there are the commands \lstinline$\pclnr$ and \lstinline$\pcrln$. The first
allows you to right align line numbers but uses the same counter as \lstinline$\pcln$. The second uses a different counter.

\begin{center}
\fbox{%
\protocol{My Protocol}{%
\pcln  \textbf{Alice} \< \< \textbf{Bob} \< \\
\pcln b \sample \bin \< \< \< \\
 \< \sendmessageright*{\text{send over } b} \< \< \pclnr\\
 \< \< \text{do something} \<  \pclnr \\
 \< \sendmessageleft*{\text{send over sth. else}} \<\<  \pclnr \\
\pcln \text{finalize} \< \< \<}
}
\end{center}
Which is generated as
\begin{lstlisting}
\protocol{My Protocol}{%
\pcln  \textbf{Alice} \< \< \textbf{Bob} \< \\
\pcln b \sample \bin \< \< \< \\
 \< \sendmessageright*{\text{send over } b} \< \< \pclnr\\
 \< \< \text{do something} \<  \pclnr \\
 \< \sendmessageleft*{\text{send over sth. else}} \<\<  \pclnr \\
\pcln \text{finalize} \< \< \<}
\end{lstlisting}



And using \lstinline$\pcrln$:
\begin{center}
\fbox{%
\protocol{My Protocol}{%
\pcln  \textbf{Alice} \< \< \textbf{Bob} \<  \\
\pcln b \sample \bin \< \< \< \\
 \< \sendmessageright*{\text{send over } b} \<\<  \pcrln\\
 \< \< \text{do something}   \< \pcrln \\
 \< \sendmessageleft*{\text{send over sth. else}} \<  \< \pcrln \\
\pcln \text{finalize} \< \< \< }
}
\end{center}
Which is generated as
\begin{lstlisting}
\protocol{My Protocol}{%
\pcln  \textbf{Alice} \< \< \textbf{Bob}  \\
\pcln b \sample \bin \< \< \\
\< \sendmessageright*{\text{send over } b} \<  \pcrln\\
\< \< \text{do something}   \pcrln \\
\< \sendmessageleft*{\text{send over sth. else}} \<  \pcrln \\
\pcln \text{finalize} \< \< }
\end{lstlisting}




\subsection{Separators}
\label{sec:protocol:separator}
The commands \lstinline$\pclnseparator$ and \lstinline$\pcrlnseparator$ define the separators between the pseudocode and line numbering. By default the left separator is set to (:) colon and the right separator is set to a space of 3 pt.


\section{Sub Protocols}

Use the \enquote{subprocedure} function also to create sub protocols. 

\begin{center}
\fbox{%
\protocol{My Protocol}{%
 \textbf{Alice} \< \< \textbf{Bob}  \\
 b \sample \bin \< \< \\
 \< \sendmessageright*{\text{send over } b} \<  \\
 \< \< \text{do something}    \\
 \<\< \dbox{\begin{subprocedure}\pseudocode{
 \< \< \textbf{Charlie}  \\
\text{something more} \< \<  \\
 \< \sendmessageright*[1.5cm]{\text{message}} \<  \\
 \< \< \text{some processing}  \\
 \< \< \text{more processing}  \\ 
 \< \sendmessageleft*[1.5cm]{\text{message}} \<  \\
 \< \sendmessageright*[1.5cm]{\text{message}} \<  \\
 \< \sendmessageleft*[1.5cm]{\text{message}} \<  \\
}\end{subprocedure}}  \\
 \< \sendmessageleft*{\text{send over sth. else}} \<   \\
 \text{finalize} \< \< }
}
\end{center}


  \begin{lstlisting}
\protocol{My Protocol}{%
 \textbf{Alice} \< \< \textbf{Bob}  \\
 b \sample \bin \< \< \\
 \< \sendmessageright*{\text{send over } b} \<  \\
 \< \< \text{do something}    \\
 \<\< \dbox{\begin{subprocedure}\pseudocode{
 \< \< \textbf{Charlie}  \\
\text{something more} \< \<  \\
 \< \sendmessageright*[1.5cm]{\text{message}} \<  \\
 \< \< \text{some processing}  \\
 \< \< \text{more processing}  \\ 
 \< \sendmessageleft*[1.5cm]{\text{message}} \<  \\
 \< \sendmessageright*[1.5cm]{\text{message}} \<  \\
 \< \sendmessageleft*[1.5cm]{\text{message}} \<  \\
}\end{subprocedure}}  \\
 \< \sendmessageleft*{\text{send over sth. else}} \<   \\
 \text{finalize} \< \< }
\end{lstlisting}






\chapter{Game Based Proofs}
\label{chap:gbproofs}

\section{Basics}
\index{gameproof}
\index{gameprocedure}

Besides displaying pseudocode the package also comes with commands to display game based proofs. A proof
is wrapped in the \emph{gameproof} environment. 
\begin{lstlisting}
\begin{gameproof}
proof goes here
\end{gameproof}
\end{lstlisting}
Within the proof environment you can use the command \lstinline$\gameprocedure$ which works similarly to the pseudocode
command and produces a heading of the form $\pcgamename_{\text{counter}}$ where counter is a consecutive counter.
Thus, we can create the following setup
\begin{center}
\begin{gameproof}
\gameprocedure[linenumbering,mode=text]{%
 Step 1  \\
 Step 2  
 }
\gameprocedure[mode=text]{%
 Step 1  \\
 Step 2  
 }
\end{gameproof}
\end{center}
by using
\begin{lstlisting}
\begin{gameproof}
\gameprocedure[linenumbering,mode=text]{%
 Step 1  \\
 Step 2  
 }
\gameprocedure[mode=text]{%
 Step 1  \\
 Step 2  
 }
\end{gameproof}
\end{lstlisting}



\subsection{Highlight Changes}
\index{gamechange}
In order to highlight changes from one game to the next use \lstinline$\gamechange$.
\begin{center}
\begin{gameproof}
\gameprocedure[linenumbering,mode=text]{%
 Step 1  \\
 Step 2  
 }
\gameprocedure[mode=text]{%
 Step 1  \\
 \gamechange{Step 2}
 }
\end{gameproof}
\end{center}
\begin{lstlisting}
\begin{gameproof}
\gameprocedure[linenumbering,mode=text]{%
 Step 1  \\
 Step 2  
 }
\gameprocedure[mode=text]{%
 Step 1  \\
 \gamechange{Step 2}
 }
\end{gameproof}
\end{lstlisting}

\subsection{Boxed games}
\index{tbxgameprocedure}
Use \lstinline$\tbxgameprocedure$ in order to create two consecutive games where the second game is \emph{boxed}. Use
\lstinline$\pcbox$ to create boxed statements.
\begin{center}
\begin{gameproof}
\gameprocedure{%
 \pcln  \text{Step 1}  \\
 \pcln  \text{Step 2}  
 }
\tbxgameprocedure{%
   \text{Step 1}; \pcbox{\text{Alternative step 1}}  \\
   \gamechange{\text{Step 2 is different}}  
 }
\gameprocedure{%
  \text{Step 1}  \\
  \text{\gamechange{Step 2}}  
 } 
\end{gameproof}
\end{center}

\begin{lstlisting}
\begin{gameproof}
\gameprocedure{%
 \pcln  \text{Step 1}  \\
 \pcln  \text{Step 2}  
 }
\tbxgameprocedure{%
   \text{Step 1}; \pcbox{\text{Alternative step 1}}  \\
   \gamechange{\text{Step 2 is different}}  
 }
\gameprocedure{%
 \pcln  \text{Step 1}  \\
 \pcln  \text{\gamechange{Step 2}}  
 } 
\end{gameproof}
\end{lstlisting}

\subsection{Reduction Hints}
\index{addgamehop}
In a game based proof in order to go from one game to the next we usually give a reduction, for example, we show that
the difference between two games is bound by the security of some pseudorandom generator $\mathsf{PRG}$. To give a hint
within the pseudocode that the difference between two games is down to ``something'' you can use the \lstinline$\addgamehop$
command.
\begin{lstlisting}
\addgamehop{startgame}{endgame}{options}
\end{lstlisting}
Here options allows you to specify the hint as well as the style. The following options are available
\begin{description}
\item[hint]
The hint text
\item[nodestyle]
A TIKZ style to be used for the node.
\item[pathstyle]
A TIKZ style to be used for the path.
\item[edgestyle]
A TIKZ style to be used for the edge. This defaults to \enquote{bend left}.
\end{description}

\begin{center}
\begin{gameproof}
\gameprocedure{%
 \pcln  \text{Step 1}  \\
 \pcln  \text{Step 2}  
 }
\gameprocedure{%
   \text{Step 1}  \\
   \gamechange{\text{Step 2 is different}}  
 }
\addgamehop{1}{2}{hint=\footnotesize some hint,nodestyle=red}
\end{gameproof}
\end{center}
\begin{lstlisting}
\begin{gameproof}
\gameprocedure{%
 \pcln  \text{Step 1}  \\
 \pcln  \text{Step 2}  
 }
\gameprocedure{%
   \text{Step 1}  \\
   \gamechange{\text{Step 2 is different}}  
 }
\addgamehop{1}{2}{hint=\footnotesize some hint,nodestyle=red}
\end{gameproof}
\end{lstlisting}
The edgestyle allows you to specify how the hint is displayed. If you, for example want a straight line, rather than the curved arrow simply
use 
\begin{lstlisting}
\addgamehop{1}{2}{hint=\footnotesize some hint,edgestyle=}
\end{lstlisting}



If game proofs do not fit into a single picture you can specify start and end hints using the commands
\begin{lstlisting}
\addstartgamehop[first game]{options}
\addendgamehop[last game]{options}
\end{lstlisting}

\begin{center}
\begin{gameproof}
\gameprocedure{%
 \pcln  \text{Step 1}  \\
 \pcln  \text{Step 2}  
 }
\gameprocedure{%
   \text{Step 1}  \\
   \gamechange{\text{Step 2 is different}}  
 }
 \addstartgamehop{hint=\footnotesize some hint,edgestyle=}
\addgamehop{1}{2}{hint=\footnotesize some hint}
 \addendgamehop{hint=\footnotesize some outgoing hint,edgestyle=}
\end{gameproof}
\end{center}


\begin{lstlisting}
\begin{gameproof}
\gameprocedure{%
 \pcln  \text{Step 1}  \\
 \pcln  \text{Step 2}  
 }
\gameprocedure{%
   \text{Step 1}  \\
   \gamechange{\text{Step 2 is different}}  
 }
 \addstartgamehop{hint=\footnotesize some hint,edgestyle=}
\addgamehop{1}{2}{hint=\footnotesize some hint}
 \addendgamehop{hint=\footnotesize some outgoing hint,edgestyle=}
\end{gameproof}
\end{lstlisting}

\subsection{Numbering and Names}
By default the \emph{gameproof} environment starts to count from 1 onwards. Its optional parameters allow you to specify a custom
name for your game and the starting number.
\begin{lstlisting}
\begin{gameproof}[options]
\end{lstlisting}

\begin{center}
\begin{gameproof}[nr=5,name=\mathsf{MyGame},arg=(1^n)]
\gameprocedure{%
 \pcln  \text{Step 1}  \\
 \pcln  \text{Step 2}  
 }
\gameprocedure{%
   \text{Step 1}  \\
   \gamechange{\text{Step 2 is different}}  
 }
\addstartgamehop{hint=\footnotesize some ingoing hint}
\addgamehop{6}{7}{hint=\footnotesize some hint}
 \addendgamehop{hint=\footnotesize some outgoing hint}
\end{gameproof}
\end{center}

\begin{lstlisting}
\begin{gameproof}[nr=5,name=$\mathsf{MyGame}$,arg=$(1^n)$]
\gameprocedure{%
 \pcln  \text{Step 1}  \\
 \pcln  \text{Step 2}  
 }
\gameprocedure{%
   \text{Step 1}  \\
   \gamechange{\text{Step 2 is different}}  
 }
\addstartgamehop{hint=\footnotesize some ingoing hint}
\addgamehop{6}{7}{hint=\footnotesize some hint}
 \addendgamehop{hint=\footnotesize some outgoing hint}
\end{gameproof}
\end{lstlisting}


\subsection{Default Name and Argument}
The default name and argument are controlled via the commands \lstinline$\pcgamename$ and \lstinline$\gameprocedurearg$.

\begin{center}
\begin{tabular}{ll}
\textbf{Command} & \textbf{Default} \\
\lstinline$\pcgamename$ & \lstinline$\mathsf{Game}$ \\
\lstinline$\gameprocedurearg$ & \lstinline$(\secpar)$
\end{tabular}
\end{center}


\subsection{Two Directional Games}
\index{bxgameprocedure}
\index{addloopgamehop}
You can use the \lstinline$\bxgameprocedure$ to generate games for going in two directions. Use the \lstinline$\addloopgamehop$
to add the gamehop in the middle.

\vspace{1cm}
\begin{center}
\begin{gameproof}
\bxgameprocedure{4}{%
 \pcln  \text{Step 1}; \pcbox{Alternative} \\
 \pcln  \text{Step 2}  
 }
\bxgameprocedure{3}{%
   \text{Step 1}; \pcbox{Alternative}  \\
   \gamechange{\text{Step 2 is different}}  
 }
 \addstartgamehop{hint=\footnotesize some hint,edgestyle=}
\addgamehop{1}{2}{hint=\footnotesize some hint,edgestyle=}
 \addloopgamehop{hint=\footnotesize some loop hint}
 \addgamehop{2}{1}{hint=\footnotesize some hint}
\end{gameproof}
\end{center}


\begin{lstlisting}
\begin{gameproof}
\bxgameprocedure{4}{%
 \pcln  \text{Step 1}; \pcbox{Alternative} \\
 \pcln  \text{Step 2}  
 }
\bxgameprocedure{3}{%
   \text{Step 1}; \pcbox{Alternative}  \\
   \gamechange{\text{Step 2 is different}}  
 }
 \addstartgamehop{hint=\footnotesize some hint,edgestyle=}
\addgamehop{1}{2}{hint=\footnotesize some hint,edgestyle=}
 \addloopgamehop{hint=\footnotesize some loop hint}
 \addgamehop{2}{1}{hint=\footnotesize some hint}
\end{gameproof}
\end{lstlisting}

\chapter{Black-box Reductions}
\label{chap:bbr}
\index{bbrenv}
\index{bbrbox}
\index{bbrinput}
\index{bbroutput}

The cryptocode package comes with support for drawing basic black box reductions. A reduction is
always of the following form.

\begin{lstlisting}
\begin{bbrenv}{A}
\begin{bbrbox}[name=Box Name]
% The Box's content
\end{bbrbox}
% Commands to display communication, input output etc
\end{bbrenv}
\end{lstlisting}
That is, a \enquote{bbrenv} (where bbr is short for black-box reduction) environment which takes a single
\enquote{bbrbox} environment and some additional commands. 

The following is a simple example drawing one (black)box with some code and input output:
\begin{bbrenv}{A}
	\begin{bbrbox}[name=Box Name]
	\pseudocode{
		\text{step 1} \\
		\text{step 2} \\
		\pcfor \text{some condition} \pcdo \\
		\pcind\text{step 3} 
	}
	\end{bbrbox}
	\bbrinput{input}
	\bbroutput{output}
\end{bbrenv}
This box is generated as
\begin{lstlisting}
\begin{bbrenv}{A}
	\begin{bbrbox}[name=Box Name]
	\pseudocode{
		\text{step 1} \\
		\text{step 2} \\
		\pcfor \text{some condition} \pcdo \\
		\pcind\text{step 3} 
	}
	\end{bbrbox}
	\bbrinput{input}
	\bbroutput{output}
\end{bbrenv}
\end{lstlisting}

The commands \lstinline$bbrinput$ and \lstinline$bbroutput$ allow to specify input and output for the latest
''bbrenv´´ environment. The single argument to the bbrenv environment needs to specify a unique identifier
(unique for the current reduction). This id is used as an internal TIKZ node name (\url{http://www.ctan.org/tex-archive/graphics/pgf/}).
\begin{lstlisting}
\begin{bbrenv}{UNIQUE IDENTIFIER}
\end{lstlisting}
As we are drawing a TIKZ image, note that we can easily later customize the image using the labels that we
have specified on the way. 
\begin{bbrenv}{A}
	\begin{bbrbox}[name=Box Name]
	\pseudocode{
		\text{step 1} \\
		\text{step 2} \\
		\pcfor \text{some condition} \pcdo \\
		\pcind\text{step 3} 
	}
	\end{bbrbox}
	\bbrinput{input}
	\bbroutput{output}

	\filldraw[fill=blue] (A.north) circle (4pt);
	\filldraw[fill=blue] (A.west) circle (4pt);
	\filldraw[fill=blue] (A.east) circle (4pt);
	\filldraw[fill=blue] (A.south) circle (4pt);
\end{bbrenv}
\begin{lstlisting}
\begin{bbrenv}{A}
	\begin{bbrbox}[name=Box Name]
	\pseudocode{
		\text{step 1} \\
		\text{step 2} \\
		\pcfor \text{some condition} \pcdo \\
		\pcind\text{step 3} 
	}
	\end{bbrbox}
	\bbrinput{input}
	\bbroutput{output}

	\filldraw[fill=blue] (A.north) circle (4pt);
	\filldraw[fill=blue] (A.west) circle (4pt);
	\filldraw[fill=blue] (A.east) circle (4pt);
	\filldraw[fill=blue] (A.south) circle (4pt);
\end{bbrenv}
\end{lstlisting}

The \enquote{bbrbox} takes as single argument a comma separated list of key value pairs. In the example we have used
\begin{lstlisting}
name=Box Name
\end{lstlisting}
to specify the label. The following options are available

\begin{tabular}{ll}
Option & Description \\ \hline
name & Specifies the box' label \\
minheight & The minimal height \\
xshift & Allows horizontal positioning \\
style & allows to customize the node
\end{tabular}


\section{Nesting of Boxes}
Boxes can be nested. For this simply insert a bbrenv (together with a single bbrbox) environment into an
existing bbrbox.
\begin{bbrenv}{A}
	\begin{bbrbox}[name=Box Name]
	\pseudocode{
		\text{step 1} \\
		\text{step 2} \\
		\pcfor \text{some condition} \pcdo \\
		\pcind\text{step 3} 
	}

	\begin{bbrenv}{B}
		\begin{bbrbox}[name=Inner Box]
		\pseudocode{
			\text{inner step 1} \\
			\text{inner step 2} \\
		}
		\end{bbrbox}
	\end{bbrenv}

	\pseudocode{
		\text{step 4} \\
		\text{step 5} \\
	}
	\end{bbrbox}
	\bbrinput{input}
	\bbroutput{output}
\end{bbrenv}

\begin{lstlisting}
\begin{bbrenv}{A}
	\begin{bbrbox}[name=Box Name]
	\pseudocode{
		\text{step 1} \\
		\text{step 2} \\
		\pcfor \text{some condition} \pcdo \\
		\pcind\text{step 3} 
	}

	\begin{bbrenv}{B}
		\begin{bbrbox}[name=Inner Box]
		\pseudocode{
			\text{inner step 1} \\
			\text{inner step 2} \\
		}
		\end{bbrbox}
	\end{bbrenv}

	\pseudocode{
		\text{step 4} \\
		\text{step 5} \\
	}
	\end{bbrbox}
	\bbrinput{input}
	\bbroutput{output}
\end{bbrenv}
\end{lstlisting}

\section{Messages and Queries}
\index{bbrmsgto}
\index{bbrmsgfrom}
\index{bbrqryto}
\index{bbrqryfrom}

You can send messages and queries to boxes. For this use the commands 
\begin{lstlisting}
\bbrmsgto{options}
\bbrmsgfrom{options}
\bbrqryto{options}
\bbrqryfrom{options}
\end{lstlisting}
By convention messages are on the left of boxes and queries on the right. Commands ending on to
make an arrow to the right while commands ending on from make an arrow to the left. The \emph{options}
define how the message is drawn and consists of a key-value pairs separated by \enquote{,}.

For example, to draw a message with a label on top and on the side use
\begin{lstlisting}
\bbrmsgto{top=Top Label, side=Side Label}
\end{lstlisting}
If your label contains a \enquote{,} (comma), then group the label in $\{\}$ (curly brackets).
\begin{lstlisting}
\bbrmsgto{top=Top Label, side={Side, Label}}
\end{lstlisting}

Following is a complete example. Notice that cryptocode takes care of the vertical positioning.

\begin{bbrenv}{A}
	\begin{bbrbox}[name=Box Name]
	\pseudocode{
		\text{step 1} \\
		\text{step 2} \\
		\pcfor \text{some condition} \pcdo \\
		\pcind\text{step 3} 
	}

	\begin{bbrenv}{B}
		\begin{bbrbox}[name=Inner Box]
		\pseudocode{
			\text{inner step 1} \\
			\text{inner step 2} \\
		}
		\end{bbrbox}
		
		\bbrmsgto{top={$m_0,m_1$}}
		\bbrmsgfrom{top=$q$}


		\bbrqryto{top={$m_0,m_1$}}
		\bbrqryfrom{top=$q$}

	\end{bbrenv}

	\pseudocode{
		\text{step 4} \\
		\text{step 5} \\
	}
	\end{bbrbox}
	\bbrinput{input}
	\bbroutput{output}
\end{bbrenv}

\begin{lstlisting}
\begin{bbrenv}{A}
	\begin{bbrbox}[name=Box Name]
	\pseudocode{
		\text{step 1} \\
		\text{step 2} \\
		\pcfor \text{some condition} \pcdo \\
		\pcind\text{step 3} 
	}

	\begin{bbrenv}{B}
		\begin{bbrbox}[name=Inner Box]
		\pseudocode{
			\text{inner step 1} \\
			\text{inner step 2} \\
		}
		\end{bbrbox}
		
		\bbrmsgto{top={$m_0,m_1$}}
		\bbrmsgfrom{top=$q$}


		\bbrqryto{top={$m_0,m_1$}}
		\bbrqryfrom{top=$q$}

	\end{bbrenv}

	\pseudocode{
		\text{step 4} \\
		\text{step 5} \\
	}
	\end{bbrbox}
	\bbrinput{input}
	\bbroutput{output}
\end{bbrenv}
\end{lstlisting}

\subsection{Options}
Besides specifying labels for top, side and bottom you can further specify how cryptocode renders the message.
Remember that tnderneath the reduction commands is a TIKZ image (\url{http://www.ctan.org/tex-archive/graphics/pgf/}).
For each label position (top, side, bottom) a node is generated. You can provide additional properties for
this node using the options:
\begin{itemize}
\item topstyle
\item sidestyle
\item bottomstyle
\end{itemize}
You can additionally provide custom names for the nodes for later reference using
\begin{itemize}
\item topname
\item sidename
\item osidename
\item bottomname
\end{itemize}
The \enquote{osidename} allows you to provide a name for the \enquote{other side}. 

Via the option \enquote{length} you can specifiy the length of the arrow.


\begin{bbrenv}{A}
	\begin{bbrbox}[name=Box Name]
	\pseudocode{
		\text{step 1} \\
		\text{step 2} \\
		\pcfor \text{some condition} \pcdo \\
		\pcind\text{step 3} 
	}

	\begin{bbrenv}{B}
		\begin{bbrbox}[name=Inner Box]
		\pseudocode{
			\text{inner step 1} \\
			\text{inner step 2} \\
		}
		\end{bbrbox}
		
		\bbrmsgto{top={$m_0,m_1$},side=Side Label, bottom=$b$, length=2cm,
				  topstyle={draw, solid}, sidestyle={red}, bottomstyle={draw, dashed}}

	\end{bbrenv}

	\pseudocode{
		\text{step 4} \\
		\text{step 5} \\
	}
	\end{bbrbox}
	\bbrinput{input}
	\bbroutput{output}
\end{bbrenv}

\begin{lstlisting}
\begin{bbrenv}{A}
	\begin{bbrbox}[name=Box Name]
	\pseudocode{
		\text{step 1} \\
		\text{step 2} \\
		\pcfor \text{some condition} \pcdo \\
		\pcind\text{step 3} 
	}

	\begin{bbrenv}{B}
		\begin{bbrbox}[name=Inner Box]
		\pseudocode{
			\text{inner step 1} \\
			\text{inner step 2} \\
		}
		\end{bbrbox}
		
		\bbrmsgto{top={$m_0,m_1$},side=Side Label, bottom=$b$, length=2cm,
				  topstyle={draw, solid}, sidestyle={red}, bottomstyle={draw, dashed}}

	\end{bbrenv}

	\pseudocode{
		\text{step 4} \\
		\text{step 5} \\
	}
	\end{bbrbox}
	\bbrinput{input}
	\bbroutput{output}
\end{bbrenv}
\end{lstlisting}

\subsection{Loops}
\index{bbrloop}
Often an adversary may send poly many queries to an oracle, or a reduction sends many queries to an adversary.
Consider the following setting

\begin{bbrenv}{A}
	\begin{bbrbox}[name=Reduction]
	\pseudocode{
		\text{Do something} 
	}

	\begin{bbrenv}{B}

		\begin{bbrbox}[name=Adversary,minheight=3cm,xshift=4cm]

		\end{bbrbox}
		
		\bbrmsgto{top=$m$,sidename=BeginLoop}
		\bbrmsgfrom{top=$\sigma$,sidename=EndLoop}

	\end{bbrenv}

	\end{bbrbox}
	\bbrinput{input}
	\bbroutput{output}
\end{bbrenv}

\begin{lstlisting}
\begin{bbrenv}{A}
	\begin{bbrbox}[name=Reduction]
	\pseudocode{
		\text{Do something} 
	}

	\begin{bbrenv}{B}

		\begin{bbrbox}[name=Adversary,minheight=3cm,xshift=4cm]

		\end{bbrbox}
		
		\bbrmsgto{top=$m$}
		\bbrmsgfrom{top=$\sigma$}

	\end{bbrenv}

	\end{bbrbox}
	\bbrinput{input}
	\bbroutput{output}
\end{bbrenv}
\end{lstlisting}

First note that by specifying the minheight and xshift option we shifted the adversary box a bit to the right
and enlarged its box. Further we specified custom names for the node on the  side of the two messages. We
can now use the \lstinline$bbrloop$ command to visualize that these two messages are exchanged $q$ many times
\begin{lstlisting}
\bbrloop{BeginLoop}{EndLoop}{center=$q$}
\end{lstlisting}
The \lstinline$bbrloop$ command takes two node names and a config which allows you to specify if the label is
to be shown on the left, center or right. Here is the result.
\begin{bbrenv}{A}
	\begin{bbrbox}[name=Reduction]
	\pseudocode{
		\text{Do something} 
	}

	\begin{bbrenv}{B}

		\begin{bbrbox}[name=Adversary,minheight=3cm,xshift=4cm]

		\end{bbrbox}
		
		\bbrmsgto{top=$m$,sidename=BeginLoop}
		\bbrmsgfrom{top=$\sigma$,sidename=EndLoop}
		\bbrloop{BeginLoop}{EndLoop}{center=$q$}

	\end{bbrenv}

	\end{bbrbox}
	\bbrinput{input}
	\bbroutput{output}
\end{bbrenv}

\begin{lstlisting}
\begin{bbrenv}{A}
	\begin{bbrbox}[name=Reduction]
	\pseudocode{
		\text{Do something} 
	}

	\begin{bbrenv}{B}

		\begin{bbrbox}[name=Adversary,minheight=3cm,xshift=4cm]

		\end{bbrbox}
		
		\bbrmsgto{top=$m$,sidename=BeginLoop}
		\bbrmsgfrom{top=$\sigma$,sidename=EndLoop}
		\bbrloop{BeginLoop}{EndLoop}{center=$q$}

	\end{bbrenv}

	\end{bbrbox}
	\bbrinput{input}
	\bbroutput{output}
\end{bbrenv}
\end{lstlisting}

\subsection{Add Space}
\index{bbrmsgspace}
If the spacing between messages is not sufficient you can use the \lstinline$bbrmsgspace$ and
\lstinline$bbrqryspace$ commands to add additional space.
\begin{bbrenv}{A}
	\begin{bbrbox}[name=Reduction]
	\pseudocode{
		\text{Do something} 
	}

	\begin{bbrenv}{B}

		\begin{bbrbox}[name=Adversary,minheight=3cm,xshift=4cm]

		\end{bbrbox}
		
		\bbrmsgto{top=$m$}
		\bbrmsgspace{1.5cm}
		\bbrmsgfrom{top=$\sigma$}

		\bbrqryto{top=$m$}
		\bbrqryspace{1cm}
		\bbrqryfrom{top=$\sigma$}

	\end{bbrenv}

	\end{bbrbox}
	\bbrinput{input}
	\bbroutput{output}
\end{bbrenv}

\begin{lstlisting}
\begin{bbrenv}{A}
	\begin{bbrbox}[name=Reduction]
	\pseudocode{
		\text{Do something} 
	}

	\begin{bbrenv}{B}

		\begin{bbrbox}[name=Adversary,minheight=3cm,xshift=4cm]

		\end{bbrbox}
		
		\bbrmsgto{top=$m$}
		\bbrmsgspace{1.5cm}
		\bbrmsgfrom{top=$\sigma$}

		\bbrqryto{top=$m$}
		\bbrqryspace{1cm}
		\bbrqryfrom{top=$\sigma$}

	\end{bbrenv}

	\end{bbrbox}
	\bbrinput{input}
	\bbroutput{output}
\end{bbrenv}
\end{lstlisting}


\subsection{Intertext}
\index{bbrmsgtxt}
\index{bbrqrytxt}

If your reduction needs to do some extra work between queries use the \lstinline$\bbrmsgtxt$
and \lstinline$\bbrqrytxt$ commands.
\begin{lstlisting}
\bbrmsgtxt[options]{Text}
\bbrqrytxt[options]{Text}
\end{lstlisting}
\begin{bbrenv}{A}
	\begin{bbrbox}[name=Reduction]
	\pseudocode{
		\text{Do something} 
	}

	\begin{bbrenv}{B}

		\begin{bbrbox}[name=Adversary,minheight=3cm,xshift=4cm]

		\end{bbrbox}
		
		\bbrmsgto{top=$m$}
		\bbrmsgtxt{\pseudocode{
			do \\
			some \\
			work
		}}
		\bbrmsgfrom{top=$\sigma$}

		\bbrqryto{top=$m$}
		\bbrqrytxt[beforeskip=0.5cm,nodestyle={draw,dashed},xshift=2cm]{\pseudocode{
			do \\
			some \\
			work
		}}
		\bbrqryfrom{top=$\sigma$}

	\end{bbrenv}

	\end{bbrbox}
	\bbrinput{input}
	\bbroutput{output}
\end{bbrenv}

\begin{lstlisting}
\begin{bbrenv}{A}
	\begin{bbrbox}[name=Reduction]
	\pseudocode{
		\text{Do something} 
	}

	\begin{bbrenv}{B}

		\begin{bbrbox}[name=Adversary,minheight=3cm,xshift=4cm]

		\end{bbrbox}
		
		\bbrmsgto{top=$m$}
		\bbrmsgtxt{\pseudocode{
			do \\
			some \\
			work
		}}
		\bbrmsgfrom{top=$\sigma$}

		\bbrqryto{top=$m$}
		\bbrqrytxt[beforeskip=0.5cm,nodestyle={draw,dashed},xshift=2cm]{\pseudocode{
			do \\
			some \\
			work
		}}
		\bbrqryfrom{top=$\sigma$}

	\end{bbrenv}

	\end{bbrbox}
	\bbrinput{input}
	\bbroutput{output}
\end{bbrenv}
\end{lstlisting}

\section{Oracles}
\index{bbroracle}
Each box can have one or more oracles which are drawn on the right hand side of the box. An oracle
is created similarly to a \emph{bbrenv} environment using the \emph{bbroracle} environment. Oracles
go behind the single \emph{bbrbox} environment within an \emph{bbrenv} enviornment.

\begin{bbrenv}{A}
	\begin{bbrbox}[name=Reduction]
	\pseudocode{
		\text{Do something} 
	}

	\begin{bbrenv}{B}
		\begin{bbrbox}[name=Adversary,minheight=3cm,xshift=4cm]
		\end{bbrbox}

	\end{bbrenv}

	\end{bbrbox}
	\bbrinput{input}
	\bbroutput{output}

	\begin{bbroracle}{OraA}
		\begin{bbrbox}[name=Oracle 1]
		\end{bbrbox}
	\end{bbroracle}

	\begin{bbroracle}{OraB}
		\begin{bbrbox}[name=Oracle 2]
		\end{bbrbox}
	\end{bbroracle}
\end{bbrenv}


\begin{lstlisting}
\begin{bbrenv}{A}
	\begin{bbrbox}[name=Reduction]
	\pseudocode{
		\text{Do something} 
	}

	\begin{bbrenv}{B}
		\begin{bbrbox}[name=Adversary,minheight=3cm,xshift=4cm]
		\end{bbrbox}

	\end{bbrenv}

	\end{bbrbox}
	\bbrinput{input}
	\bbroutput{output}

	\begin{bbroracle}{OraA}
		\begin{bbrbox}[name=Oracle 1]
		\end{bbrbox}
	\end{bbroracle}

	\begin{bbroracle}{OraB}
		\begin{bbrbox}[name=Oracle 2]
		\end{bbrbox}
	\end{bbroracle}
\end{bbrenv}
\end{lstlisting}

\subsection{Communicating with Oracles}
\index{bbroraclequeryfrom}
\index{bbroraclequeryto}
As oracles use the \emph{bbrbox} environment we can directly use the established ways to send messages and
queries to oracles. In addition you can use the \lstinline$\bbroraclequeryfrom$ and \lstinline$\bbroraclequeryto$.
\begin{lstlisting}
\bbroraclequeryfrom{options}
\bbroraclequeryto{options}
\end{lstlisting}
Here options allow you to specify where the label goes (top, bottom). In addition you can use
\lstinline$\bbroracleqryspace$ to generate extra space between oracle messages. Note
that oracle messages need to be added after the closing \lstinline$\end{bbroracle}$ command.

\begin{bbrenv}{A}
	\begin{bbrbox}[name=Reduction]
	\pseudocode{
		\text{Do something} 
	}

	\begin{bbrenv}{B}
		\begin{bbrbox}[name=Adversary,minheight=3cm,xshift=4cm]
		\end{bbrbox}

	\end{bbrenv}

	\end{bbrbox}
	\bbrinput{input}
	\bbroutput{output}

	\begin{bbroracle}{OraA}
		\begin{bbrbox}[name=Oracle 1,minheight=1cm]
		\end{bbrbox}
	\end{bbroracle}
	\bbroraclequeryfrom{top=$m$}
	\bbroraclequeryto{top=$b$}

	\begin{bbroracle}{OraB}
		\begin{bbrbox}[name=Oracle 2,minheight=1cm]
		\end{bbrbox}
	\end{bbroracle}
	\bbroraclequeryfrom{bottom=$m$}
	\bbroraclequeryto{bottom=$b$}
\end{bbrenv}

\begin{lstlisting}
\begin{bbrenv}{A}
	\begin{bbrbox}[name=Reduction]
	\pseudocode{
		\text{Do something} 
	}

	\begin{bbrenv}{B}
		\begin{bbrbox}[name=Adversary,minheight=3cm,xshift=4cm]
		\end{bbrbox}

	\end{bbrenv}

	\end{bbrbox}
	\bbrinput{input}
	\bbroutput{output}

	\begin{bbroracle}{OraA}
		\begin{bbrbox}[name=Oracle 1,minheight=1cm]
		\end{bbrbox}
	\end{bbroracle}
	\bbroraclequeryfrom{top=$m$}
	\bbroraclequeryto{top=$b$}

	\begin{bbroracle}{OraB}
		\begin{bbrbox}[name=Oracle 2,minheight=1cm]
		\end{bbrbox}
	\end{bbroracle}
	\bbroraclequeryfrom{bottom=$m$}
	\bbroraclequeryto{bottom=$b$}
\end{bbrenv}
\end{lstlisting}

%\chapter{Known Issues}
%
%\section{Hyperref}
%\label{sec:hyperref}
%\index{pcfixhyperref}
%Referencing line numbers breaks the hyperref package. In order to fix this call
%\begin{lstlisting}
%\pcfixhyperref
%\end{lstlisting}
%after \lstinline$\begin{document}$.

\printindex



\end{document}