-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprotocol.sty
353 lines (306 loc) · 12 KB
/
protocol.sty
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
%----------------------------------------------------------------------
% this is: protocol.sty v1.3 93/09/06 Peter de Rooij
%----------------------------------------------------------------------
% Modified by Juan Garay to support broken arrows
% Modified by Mihir to add a new macro protocolm which has an extra
% left hand side column for flow names. July 7, 1995.
%----------------------------------------------------------------------
% ARROWFILL WITH \textstyle SUPERSCRIPT.
% use in math-mode:
% \sends{a^b} makes a `rightarrowfill' with $a^b$ on top.
% \Sends[n]{a^b} idem but spans n columns
% \receives{a^b}, \Receives[n]{a^b} idem for leftarrowfill
% \exchange{a^b}, \Exchange[n]{a^b} idem for lefrightarrowfill
%
% \bsends (resp., \bSends) and \breceives (resp., \bReceives) produce
% broken-line versions of sends & receives
% \dsends (resp., \dSends) and \dreceives (resp., \dReceives) produce
% double-line versions of sends & receives
% \tsends (resp., \tSends) and \treceives (resp., \tReceives) produce
% triple-line versions of sends & receives
%----------------------------------------------------------------------
\newcommand{\Pdash}{\begin{picture}(6,4)
\thinlines\put(0,2){\line(1,0){6}}\end{picture}}
\newcommand{\Pdotteddash}{\begin{picture}(10,4)
\thinlines\put(0,2){\line(1,0){5}}\end{picture}}
\newcommand{\Prightarrow}{\begin{picture}(6,4)
\thinlines\put(0,2){\vector(1,0){6}}\end{picture}}
\newcommand{\Pleftarrow}{\begin{picture}(6,4)
\thinlines\put(6,2){\vector(-1,0){6}}\end{picture}}
\newcommand{\Pdoubledash}{\begin{picture}(6,10)
\thinlines\put(0,2){\line(1,0){6}}
\put(0,8){\line(1,0){6}}
\end{picture}}
\newcommand{\Pdoubleleftarrow}{\begin{picture}(12,10)
\thinlines\put(5,2){\line(1,0){8}}
\put(5,8){\line(1,0){8}}
\put(0,5){\line(3,2){10}}
\put(0,5){\line(3,-2){10}}
\end{picture}}
\newcommand{\Pdoublerightarrow}{\begin{picture}(12,10)
\thinlines\put(7,2){\line(-1,0){8}}
\put(7,8){\line(-1,0){8}}
\put(12,5){\line(-3,2){10}}
\put(12,5){\line(-3,-2){10}}
\end{picture}}
\newcommand{\Ptripledash}{\begin{picture}(6,8)
\thinlines\put(0,2){\line(1,0){6}}
\put(0,4){\line(1,0){6}}
\put(0,6){\line(1,0){6}}
\end{picture}}
\newcommand{\Ptriplerightarrow}{\begin{picture}(12,8)
\thinlines\put(0,2){\vector(1,0){12}}
\thinlines\put(12,4){\line(-1,0){12}}
\thinlines\put(0,6){\vector(1,0){12}}
\end{picture}}
\newcommand{\Ptripleleftmidarrow}{\begin{picture}(12,8)
\thinlines\put(0,2){\line(1,0){12}}
\thinlines\put(12,4){\vector(-1,0){12}}
\thinlines\put(0,6){\line(1,0){12}}
\end{picture}}
\newcommand{\Ptripleleftarrow}{\begin{picture}(12,8)
\thinlines\put(12,2){\vector(-1,0){12}}
\thinlines\put(0,4){\line(1,0){12}}
\thinlines\put(12,6){\vector(-1,0){12}}
\end{picture}}
\newcommand{\Ptriplerightmidarrow}{\begin{picture}(12,8)
\thinlines\put(12,2){\line(-1,0){12}}
\thinlines\put(0,4){\vector(1,0){12}}
\thinlines\put(12,6){\line(-1,0){12}}
\end{picture}}
% \newcommand{\Ptwowaysuperarrow}{\begin{picture}(74,4)
% \thinlines\put(72,2){\vector(-1,0){72}}
% \thinlines\put(2,2){\vector(1,0){72}}\end{picture}}
% shorthand for lowering the arrows
\def\@r@al{\raise\arrowlower}
% rulefill made up of lowered $-$'s
\def\@lrulefill{\cleaders\hbox{$\mkern-2mu\@r@al\hbox{\Pdash}\mkern-2mu$}\hfill}
% broken rulefill made up of lowered $-~$'s
\def\@blrulefill{\cleaders\hbox{$\mkern-2mu\@r@al\hbox{\Pdotteddash}\mkern-2mu$}\hfill}
%\def\@blrulefill{\cleaders\hbox{$\mkern-2mu\@r@al\hbox{\rule{0.1cm}{0.1cm}}\mkern-2mu$}\hfill}
% double rulefill made up of lowered $=$'s
\def\@dlrulefill{\cleaders\hbox{$\mkern-3mu\@r@al\hbox{\Pdoubledash}\mkern-3mu$}\hfill}
% triple rulefill made up of lowered triple dashes
\def\@tlrulefill{\cleaders\hbox{$\mkern-3mu\@r@al\hbox{\Ptripledash}\mkern-3mu$}\hfill}
% end of rule (overlaps with arrowheads):
\def\@lendofrule{\@r@al\hbox{\Pdash}\mkern-4mu}
\def\@rendofrule{\mkern-4mu\@r@al\hbox{\Pdash}}
% double end of rule (overlaps with arrowheads):
\def\@dlendofrule{\@r@al\hbox{\Pdoubledash}\mkern-4mu}
\def\@drendofrule{\mkern-4mu\@r@al\hbox{\Pdoubledash}}
% triple end of rule (overlaps with arrowheads):
\def\@tlendofrule{\@r@al\hbox{\Ptripledash}\mkern-4mu}
\def\@trendofrule{\mkern-4mu\@r@al\hbox{\Ptripledash}}
% ends of arrows:
\def\@rafill{\mkern-6mu\@lrulefill\@rendofrule\@r@al\llap{\Prightarrow}}
\def\@lafill{\m@th\@r@al\rlap{\Pleftarrow}\@lendofrule\@lrulefill\mkern-6mu}
\def\@rdfill{\mkern-6mu\@lrulefill\@rendofrule}
\def\@ldfill{\m@th\@lendofrule\@lrulefill\mkern-6mu}
% ends of broken arrows:
\def\@brafill{\mkern-6mu\@blrulefill\@rendofrule\@r@al\llap{\Prightarrow}}
\def\@blafill{\m@th\@r@al\rlap{\Pleftarrow}\@lendofrule\@blrulefill\mkern-6mu}
\def\@brdfill{\mkern-6mu\@blrulefill\@rendofrule}
\def\@bldfill{\m@th\@lendofrule\@blrulefill\mkern-6mu}
% ends of double arrows:
\def\@drafill{\mkern-6mu\@dlrulefill\@r@al\hbox{\Pdoublerightarrow}}
\def\@dlafill{\m@th\@r@al\hbox{\Pdoubleleftarrow}\@dlrulefill\mkern-6mu}
\def\@drdfill{\mkern-6mu\@dlrulefill\@drendofrule}
\def\@dldfill{\m@th\@dlendofrule\@dlrulefill\mkern-6mu}
% \def\@drafill{\mkern-6mu\@dlrulefill\@drendofrule\@r@al\llap{\Pdoublerightarrow}}
% \def\@dlafill{\m@th\@r@al\rlap{\Pdoubleleftarrow}\@dlendofrule\@dlrulefill\mkern-6mu}
% ends of triple arrows:
\def\@trafill{\mkern-6mu\@tlrulefill\@r@al\hbox{\Ptriplerightarrow}}
\def\@tlafill{\m@th\@r@al\hbox{\Ptripleleftarrow}\@tlrulefill\mkern-6mu}
\def\@trdfill{\mkern-6mu\@tlrulefill\@r@al\hbox{\Ptriplerightmidarrow}}
\def\@tldfill{\m@th\@r@al\hbox{\Ptripleleftmidarrow}\@tlrulefill\mkern-6mu}
%\def\@trdfill{\mkern-6mu\@tlrulefill\@trendofrule}
%\def\@tldfill{\m@th\@tlendofrule\@tlrulefill\mkern-6mu}
% ends of thick arrows:
%\def\@brafill{\mkern-6mu\@blrulefill\@rendofrule\@r@al\llap{$\rightarrow$}}
%\def\@blafill{\m@th\@r@al\rlap{$\lhd$}\@lendofrule\@blrulefill\mkern-6mu}
%\def\@brdfill{\mkern-6mu\@blrulefill\@rendofrule}
%\def\@bldfill{\m@th\@lendofrule\@blrulefill\mkern-6mu}
\newdimen\minarrowwidth \minarrowwidth=0pt
\newdimen\arrowwidth
\newdimen\arrowlower \arrowlower=-4pt
\def\@showmessage#1{\setbox\@tempboxa\hbox{$#1$}
\ifdim\wd\@tempboxa<\minarrowwidth
\arrowwidth\minarrowwidth
\else
\arrowwidth\wd\@tempboxa
\fi
\hbox{$\m@th\displaystyle\mathop{\hbox to \arrowwidth
{$\m@th\@lrulefill$}}\limits^{\hfill\box\@tempboxa\hfill}$}}
% for broken arrows
\def\@bshowmessage#1{\setbox\@tempboxa\hbox{$#1$}
\ifdim\wd\@tempboxa<\minarrowwidth
\arrowwidth\minarrowwidth
\else
\arrowwidth\wd\@tempboxa
\fi
\hbox{$\m@th\displaystyle\mathop{\hbox to \arrowwidth
{$\m@th\@blrulefill$}}\limits^{\hfill\box\@tempboxa\hfill}$}}
% for double arrows
\def\@dshowmessage#1{\setbox\@tempboxa\hbox{$#1$}
\ifdim\wd\@tempboxa<\minarrowwidth
\arrowwidth\minarrowwidth
\else
\arrowwidth\wd\@tempboxa
\fi
\hbox{$\m@th\displaystyle\mathop{\hbox to \arrowwidth
{$\m@th\@dlrulefill$}}\limits^{\hfill\box\@tempboxa\hfill}$}}
% for triple arrows
\def\@tshowmessage#1{\setbox\@tempboxa\hbox{$#1$}
\ifdim\wd\@tempboxa<\minarrowwidth
\arrowwidth\minarrowwidth
\else
\arrowwidth\wd\@tempboxa
\fi
\hbox{$\m@th\displaystyle\mathop{\hbox to \arrowwidth
{$\m@th\@tlrulefill$}}\limits^{\hfill\box\@tempboxa\hfill}$}}
\def\sends{\@sends}
\def\Sends[#1]#2{\multispan{#1}$\@sends{#2}$}
\def\@sends#1{\@ldfill\@showmessage{#1}\@rafill}
% broken send
\def\bsends{\@bsends}
\def\bSends[#1]#2{\multispan{#1}$\@bsends{#2}$}
\def\@bsends#1{\@bldfill\@bshowmessage{#1}\@brafill}
% double send
\def\dsends{\@dsends}
\def\dSends[#1]#2{\multispan{#1}$\@dsends{#2}$}
\def\@dsends#1{\@dldfill\@dshowmessage{#1}\@drafill}
% triple send
\def\tsends{\@tsends}
\def\tSends[#1]#2{\multispan{#1}$\@tsends{#2}$}
\def\@tsends#1{\@tldfill\@tshowmessage{#1}\@trafill}
\def\receives{\@receives}
\def\Receives[#1]#2{\multispan{#1}$\@receives{#2}$}
\def\@receives#1{\@lafill\@showmessage{#1}\@rdfill}
% broken receive
\def\breceives{\@breceives}
\def\bReceives[#1]#2{\multispan{#1}$\@breceives{#2}$}
\def\@breceives#1{\@blafill\@bshowmessage{#1}\@brdfill}
% double receive
\def\dreceives{\@dreceives}
\def\dReceives[#1]#2{\multispan{#1}$\@dreceives{#2}$}
\def\@dreceives#1{\@dlafill\@dshowmessage{#1}\@drdfill}
% triple receive
\def\treceives{\@treceives}
\def\tReceives[#1]#2{\multispan{#1}$\@treceives{#2}$}
\def\@treceives#1{\@tlafill\@tshowmessage{#1}\@trdfill}
\def\exchange{\@exchange}
\def\Exchange[#1]#2{\multispan{#1}$\@exchange{#2}$}
\def\@exchange#1{\@lafill\@showmessage{#1}\@rafill}
%----------------------------------------------------------------------
% NARROWING COLUMNS
% \narrowcol{ARG} produces ARG centered in an \hbox that is 2em too
% narrow for its contents. ARG is still in math-mode!
% \Narrowcol does the same with the double amount of shrinking.
% \phantomcol does the same with a column of zero width
% BUG: cannot be used for \send and relatives!
\def\narrowcol#1{\hbox{\hskip-1em$#1$\hskip-1em}}
\def\Narrowcol#1{\hbox{\hskip-2em$#1$\hskip-2em}}
\def\phantomcol#1{\hbox to 0pt{\hss$#1$\hss}}
%----------------------------------------------------------------------
% A PROTOCOL ENVIRONMENT.
% args: #1: number of parties
% essentially, an array with # columns depending on the argument.
% furthermore, a macro \participants with the correct number of args
% is defined. \participants{A}{B}[{C}...] puts A resp. B (and C...) centered
% over columns 1 resp. 3 (resp. 5...)
% BUG: PARTICIPANTS CAN BE USED ONLY ONCE PER PROTOCOL!
% \protocolheader{#1} puts #1 in an \hbox centered over the entire
% array.
%
% use:
% \begin{protocol}{2}
% \protocolheader{HEADER}\\
% \participants{Alice}{Bob}\\
% y := x^2\\
% & \sends{y}\\
% & & \mbox{verify if $y=x^2$}\\
% & \receives{\mbox{OK!}}\\
% \mbox{be happy}
% \end{protocol}
% yields:
% HEADER
%
% Alice Bob
%
% y=x^2
% y
% ------>
% verify if $y=x^2$
% OK!
% <------
% be happy
%
\newcount\@ncols \@ncols=3
\newcount\@nccols \@nccols=1
\newcount\@nparties \@nparties=2
\newcount\@npartic
\def\protocolheader#1{\multicolumn{\the\@ncols}{c}{\hbox{#1}}\\}
\def\participants{% initialize:
\noalign{\global\@npartic\@nparties\gdef\@more{}}
% and do the real stuff:
\@participants}
\def\@participants#1{% print next participant:
\@more\multicolumn{1}{c}{\hbox{#1}}
% next time, first put in &&:
\gdef\@more{&&}
\global\advance\@npartic by \m@ne
% if there's more to do:
\ifnum\@npartic>0 \let\next\@participants
\else\let\next\\\fi\next
}
\newenvironment{protocol}[1]{% arg: # of parties
\relax\ifnum #1 < 2
\typeout{Only n-party protocols for n>1 implemented!}
\typeout{I'll assume n=2.}
\@nparties=2
\else
\@nparties=#1
\fi
% # of cols = 2 * (# of parties) - 1
\@ncols = \@nparties
\multiply \@ncols by 2
\advance \@ncols by -1
% # of centered cols = 2 * (# of parties) - 3
\@nccols = \@ncols
\advance \@nccols by -2
% arrowwidth based on # of columns:
\minarrowwidth = .5\textwidth
\divide\minarrowwidth by \@ncols
% lower all arrows halfway down to baseline:
\setbox\@tempboxa\hbox{$\leftarrow$}
\arrowlower-.5\ht\@tempboxa
$\begin{array}{r*{\@nccols}{c}l}
}{\end{array}$}
\newenvironment{protocolm}[1]{% arg: # of parties
\relax\ifnum #1 < 2
\typeout{Only n-party protocols for n>1 implemented!}
\typeout{I'll assume n=2.}
\@nparties=2
\else
\@nparties=#1
\fi
% # of cols = 2 * (# of parties) - 1
\@ncols = \@nparties
\multiply \@ncols by 2
\advance \@ncols by -1
% # of centered cols = 2 * (# of parties) - 3
\@nccols = \@ncols
\advance \@nccols by -2
% arrowwidth based on # of columns:
\minarrowwidth = .5\textwidth
\divide\minarrowwidth by \@ncols
% lower all arrows halfway down to baseline:
\setbox\@tempboxa\hbox{$\leftarrow$}
\arrowlower-.5\ht\@tempboxa
$\begin{array}{lr*{\@nccols}{c}l}
}{\end{array}$}
\endinput
--
Peter de Rooij <P.J.N.deRooij@research.ptt.nl>
It's better to regret something you have done,
than to regret something you haven't done. -- Butthole Surfers