crypto: Allow changing the symbol for randomness, e.g., to `$'.
[mdwtools] / crypto.dtx
1 % \begin{meta-comment}
2 %
3 % $Id: crypto.dtx,v 1.2 2003/09/05 16:13:14 mdw Exp $
4 %
5 % Typesetting crypto papers
6 %
7 % (c) 2001 Mark Wooding
8 %
9 %----- Revision history -----------------------------------------------------
10 %
11 % $Log: crypto.dtx,v $
12 % Revision 1.2 2003/09/05 16:13:14 mdw
13 % Various minor fettlings. Fix paragraph formatting after programs. More
14 % Things.
15 %
16 % Revision 1.1 2002/02/03 20:49:03 mdw
17 % Checkin for new build system.
18 %
19 %
20 % \end{meta-comment}
21 %
22 % \begin{meta-comment} <general public licence>
23 %%
24 %% crypto package -- useful macros for typesetting crypto papers
25 %% Copyright (c) 2001 Mark Wooding
26 %%
27 %% This program is free software; you can redistribute it and/or modify
28 %% it under the terms of the GNU General Public License as published by
29 %% the Free Software Foundation; either version 2 of the License, or
30 %% (at your option) any later version.
31 %%
32 %% This program is distributed in the hope that it will be useful,
33 %% but WITHOUT ANY WARRANTY; without even the implied warranty of
34 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 %% GNU General Public License for more details.
36 %%
37 %% You should have received a copy of the GNU General Public License
38 %% along with this program; if not, write to the Free Software Foundation,
39 %% Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
40 % \end{meta-comment}
41 %
42 % \begin{meta-comment} <Package preambles>
43 %<+package>\NeedsTeXFormat{LaTeX2e}
44 %<+package>\ProvidesPackage{crypto}
45 %<+package> [2001/09/16 1.0 Crypto typesetting]
46 % \end{meta-comment}
47 %
48 % \CheckSum{249}
49 %% \CharacterTable
50 %% {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
51 %% Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
52 %% Digits \0\1\2\3\4\5\6\7\8\9
53 %% Exclamation \! Double quote \" Hash (number) \#
54 %% Dollar \$ Percent \% Ampersand \&
55 %% Acute accent \' Left paren \( Right paren \)
56 %% Asterisk \* Plus \+ Comma \,
57 %% Minus \- Point \. Solidus \/
58 %% Colon \: Semicolon \; Less than \<
59 %% Equals \= Greater than \> Question mark \?
60 %% Commercial at \@ Left bracket \[ Backslash \\
61 %% Right bracket \] Circumflex \^ Underscore \_
62 %% Grave accent \` Left brace \{ Vertical bar \|
63 %% Right brace \} Tilde \~}
64 %%
65 %
66 % \begin{meta-comment}
67 %
68 %<*driver>
69 \input{mdwtools}
70 \describespackage{crypto}
71 \mdwdoc
72 %</driver>
73 %
74 % \end{meta-comment}
75 %
76 %^^A-------------------------------------------------------------------------
77 % \section{User guide}
78 %
79 % \subsection{Algorithm typesetting}
80 %
81 % A lot of provable-security papers need to be able to typeset algorithms
82 % describing adversaries, schemes, oracle behaviour, etc. There is a
83 % (relatively) standard format for doing this which we support.
84 %
85 % \DescribeEnv{program}
86 % The \env{program} environment provides handy notation for describing
87 % algorithms formally. It gives a \env{tabbing} environment, so that things
88 % can be laid out nicely, and allows fragments of algorithms to be laid out
89 % in columns or rows, with separating rules.
90 %
91 % \DescribeMacro\next
92 % Within the \env{program} environment, the |\next| command stops typesetting
93 % the current column, typesets a vertical separator rule, and starts a new
94 % column. Adjacent columns are spaced out evenly across the page, with equal
95 % space around the rules rules and at the current margins. This means that
96 % the rules don't line up, but it still seems to provide a pleasing effect.
97 %
98 % \DescribeMacro\newline
99 % The |\newline| macro begins a new row of algorithm typesetting. A page
100 % break is possible at a |\newline|.
101 %
102 % \DescribeMacro\kw
103 % A number of standard keywords are available, as shown in
104 % table~\ref{tab:kw}. The typsetting of these is done by the |\kw| command,
105 % which usually sets its argument in text bold face, but can be redefined.
106 % The standard definition uses |\xspace| so that you don't need to remember
107 % to say \verb*+\ + after a keyword command.
108 % \begin{table}
109 % \centering
110 % \def\row#1{\texttt{\string#1} & #1 \\}
111 % \begin{tabular}{ll}
112 % \textbf{Command} & \textbf{Keyword} \\
113 % \row\RETURN
114 % \row\IF
115 % \row\THEN
116 % \row\ELSE
117 % \row\REPEAT
118 % \row\WHILE
119 % \row\UNTIL
120 % \row\FOREVER
121 % \row\DO
122 % \row\FOR
123 % \row\FOREACH
124 % \row\FROM
125 % \row\IN
126 % \row\TO
127 % \row\ABORT
128 % \row\PARSE
129 % \row\NEW
130 % \row\AS
131 % \end{tabular}
132 % \caption{Keywords available for algorithm typesetting}
133 % \label{tab:kw}
134 % \end{table}
135 %
136 % \DescribeMacro\ind
137 % Within a \env{program} environment, the |\ind| command shunts the indent
138 % level 1\,em to the right.
139 %
140 % \DescribeMacro\gets
141 % \DescribeMacro\getsr
142 % \DescribeMacro\inr
143 % Assignment can be represented using the standard command |\gets|, which
144 % typesets a left-pointing arrow `$\gets$'. Random sampling -- the selection
145 % of a random element from a set or probability distribution -- can be
146 % represented using the new command |\getsr|, which typesets an arrow with a
147 % little `R' above it `$\getsr$'. Random membership -- showing that
148 % something is a random variable with some distribution -- can be represented
149 % using the |\inr| command, which just typesets an $\in$ sign with a
150 % subscript `R': `$\inr$'.
151 %
152 % Should one wish, one can use a different character than `R' to denote
153 % randomness. Some authors use `\$', for example. I know of one
154 % (cheapskate?) author who has used `\rlap/c'. Redefining the |\random|
155 % command lets you do this. For example, you can say
156 % |\newcommand{\random}{\$}| should you so wish.
157 %
158 % \DescribeMacro\id
159 % Long identifiers can be typeset using the |\id| command. giving the
160 % identifier name as an argument. The |\id| command is only valid in maths
161 % mode. As currently set up, |\id| sets its argument in \emph{text} italics;
162 % this seems to look better in documents which use a PostScript body face and
163 % Computer Modern for maths.
164 %
165 % \DescribeMacro\Xid
166 % It's handy to be able to glue a bit of (possibly fancy) maths typesetting
167 % to an identifier, e.g., to construct $\Xid{H'}{list}$, or
168 % $\Xid{\mathcal{E}}{CTR$\$$}^F$. This is done using
169 % \syntax{"\\Xid{"<maths>"}{"<text>"}"}. The two bits are joined by a text
170 % hyphen `-'.
171 %
172 % \DescribeMacro\cookie
173 % Sometimes textual names are used for special `symbols', which have meaning
174 % to algorithms, e.g., the symbols $\cookie{find}$ and $\cookie{guess}$ in
175 % the standard indistinguishability game. These can be typeset using the
176 % |\cookie| command.
177 %
178 % \subsection{Other stuff}
179 %
180 % \DescribeMacro\Thing
181 % In the quantifiable-security world, there are standard symbols for
182 % advantage, success probability, insecurity, etc. The generic `style hook'
183 % for these is \syntax{"\\Thing{"<name>"}{"<notion>"}{"scheme"}"}, which
184 % typesets $\Thing{name}{notion}{scheme}$. It helps a lot if you have the
185 % \package{amstext} package loaded.
186 %
187 % \DescribeMacro\Succ
188 % \DescribeMacro\Adv
189 % \DescribeMacro\InSec
190 % \DescribeMacro\Expt
191 % \DescribeMacro\Game
192 % \begin{synshorts}
193 % Some standard `things' are provided: "\\Succ{"<notion>"}{"<scheme>"}",
194 % "\\Adv{"<notion>"}{"<scheme>"}", "\\InSec{"<notion>"}",
195 % "\\Expt{"<notion>"}{"<scheme>"}", and "\\Game{"<notion>"}{"<scheme>"}".
196 % \end{synshorts}
197 %
198 % \DescribeMacro\G
199 % In proofs which proceed by varying the rules of the game played by the
200 % adversary and bounding the probability of it noticing at each step, game
201 % names are usually typeset as $\G n$ for small numbers $n$. The command
202 % \syntax{"\\G{"<n>"}"} command does this typesetting. There's an optional
203 % argument, which is a symbol to write instead of `G'.
204 %
205 % \DescribeMacro\Func
206 % \DescribeMacro\Perm
207 % When dealing with finite PRFs and PRPs, we need to talk about the set of
208 % \emph{all} functions (or permutations) over particular sets, usually
209 % $n$-vectors of bits. The macros \syntax{"\\Func{"<l>"}{"<L>"}"} and
210 % \syntax{"\\Perm{"<L>"}"} typeset $\Func{l}{L}$ and $\Perm{L}$ respectively,
211 % and are intended to denote the sets of all functions $F\colon \{0, 1\}^l
212 % \to \{0, 1\}^L$ and all permutations $\Pi\colon \{0, 1\}^L \to \{0, 1\}^L$
213 % respectively.
214 %
215 % \DescribeMacro\PKCS
216 % Finally, the |\PKCS| macro typesets `\PKCS{$n$}', allowing you to name RSA
217 % Security Inc.'s Public Key Cryptography Standards in a relatively nice way.
218 %
219 % \implementation
220 %
221 %
222 %^^A-------------------------------------------------------------------------
223 % \section{Implementation}
224 %
225 % We need David Carlisle's handy \package{xspace} package and the AMS |\text|
226 % command.
227 %
228 % \begin{macrocode}
229 %<*package>
230 \RequirePackage{amstext}
231 \RequirePackage{xspace}
232 % \end{macrocode}
233 %
234 % \subsection{Algorithm typsetting}
235 %
236 % \begin{macro}{\cookie}
237 % \begin{macro}{\kw}
238 % \begin{macro}{\id}
239 %
240 % First, some style issues. Note the |\xspace| at the end of |\kw|.
241 %
242 % \begin{macrocode}
243 \def\cookie#1{\text{\normalfont\sffamily\/#1\/}}
244 \def\kw#1{\text{\normalfont\bfseries\/#1\/}\xspace}
245 \def\id#1{\text{\normalfont\itshape\/#1\/}}
246 % \end{macrocode}
247 %
248 % \end{macro}
249 % \end{macro}
250 % \end{macro}
251 %
252 % \begin{macro}{\getsr}
253 % \begin{macro}{\inr}
254 %
255 % The symbols for random selection and membership are fairly easy. The `R'
256 % over $\getsr$ is actually in scriptscript style, because that seems to look
257 % nicer.
258 %
259 % \begin{macrocode}
260 \providecommand\random{R}
261 \def\inr{\mathrel{\in_{\random}}}
262 \def\getsr{\mathrel{\mathop{\gets}\limits^{\scriptscriptstyle\random}}}
263 % \end{macrocode}
264 %
265 % \end{macro}
266 % \end{macro}
267 %
268 % \begin{macro}{\Xid}
269 %
270 % The compound identifiers set by |\Xid| are easy.
271 %
272 % \begin{macrocode}
273 \def\Xid#1#2{\id{$#1$-#2}}
274 % \end{macrocode}
275 %
276 % \end{macro}
277 %
278 % Now for the various keywords. These are trivial, but useful.
279 %
280 % \begin{macrocode}
281 \def\RETURN{\kw{return}}
282 \def\IF{\kw{if}}
283 \def\THEN{\kw{then}}
284 \def\ELSE{\kw{else}}
285 \def\REPEAT{\kw{repeat}}
286 \def\WHILE{\kw{while}}
287 \def\UNTIL{\kw{until}}
288 \def\FOREVER{\kw{forever}}
289 \def\DO{\kw{do}}
290 \def\FOR{\kw{for}}
291 \def\FOREACH{\kw{for\,each}}
292 \def\FROM{\kw{from}}
293 \def\IN{\kw{in}}
294 \def\TO{\kw{to}}
295 \def\ABORT{\kw{abort}}
296 \def\PARSE{\kw{parse}}
297 \def\AS{\kw{as}}
298 \def\NEW{\ifmmode\mathop{\kw{new}}\else\kw{new}\fi}
299 % \end{macrocode}
300 %
301 % \begin{environment}{program}
302 % \begin{macro}{\next}
303 % \begin{macro}{\newline}
304 % \begin{macro}{\ind}
305 %
306 % Now for the \env{program} environment and its associated twiddling. This
307 % is actually a little fiddly.
308 %
309 % At the beginning, if we're in vertical mode -- i.e., there was a paragraph
310 % break before the start of the environment -- then remember this, because it
311 % affects the typesetting at the end. Set up |\next| and |\newline| in terms
312 % of the underlying machinery, and start a row of algorithm.
313 %
314 % \begin{macrocode}
315 \def\program{%
316 \normalfont%
317 \@tempswatrue\ifvmode\@tempswafalse\fi%
318 \def\next{\program@end\vrule\program@begin}%
319 \def\newline{\program@endline\medskip\program@startline}%
320 \def\ind{\quad\=\+\kill}%
321 \ifdim\topsep<\parskip\topsep\parskip\fi%
322 \ifdim\@topsepadd<\z@\@topsepadd\z@\fi%
323 \begingroup\trivlist%
324 \advance\@topsep-\parskip\advance\@topsepadd-\parskip\item%
325 \program@startline%
326 }
327 % \end{macrocode}
328 %
329 % Ending the environment is easy-ish. We stop the current row and leave a
330 % gap, matching the one that |\poem@startline| adds automatically. If we
331 % were initially in horizontal mode, then don't indent the next paragraph,
332 % and ignore spaces after the |\end{program}| command.
333 %
334 % \begin{macrocode}
335 \def\endprogram{%
336 \program@endline\endtrivlist\endgroup%
337 \if@tempswa\@endparenv\fi\@ignoretrue%
338 }
339 % \end{macrocode}
340 %
341 % Now for the guts of all of this. First of all, we turn to the typesetting
342 % of a column, which is just hfil glue, a \env{minipage} with zero width and
343 % a \env{tabbing} environment. The first tab is already set 1\,em in from
344 % the margin. We use \env{minipage} to set up the list parameters correctly
345 % and manage the initial and final spacing. The zero width is OK because
346 % \env{tabbing} sets a list of hboxes rather than using outer horizontal
347 % mode, so the |\hsize| is irrelevant.
348 %
349 % \begin{macrocode}
350 \def\program@begin{%
351 \begingroup%
352 \hfil%
353 \minipage[t]\z@%
354 \topsep\z@%
355 \itemsep\z@%
356 \parskip\z@\parsep\z@%
357 \partopsep\z@%
358 \tabbing%
359 % \end{macrocode}
360 %
361 % This is rather messy. The |\item| from the \env{trivlist} messes up the
362 % spacing. We remove the box, and fix |\prevdepth| to ensure that there's no
363 % glue at the top.
364 %
365 % \begin{macrocode}
366 \quad\=dummy\\%
367 \@stopfield%
368 \begingroup%
369 \setbox\z@\lastbox\unskip\unskip\unskip\setbox\z@\lastbox\unskip%
370 \endgroup%
371 \prevdepth-\@m\p@%
372 \@startfield\strut\ignorespaces%
373 }
374 % \end{macrocode}
375 %
376 % Ending a program has no discernable subtlety.
377 %
378 % \begin{macrocode}
379 \def\program@end{%
380 \endtabbing%
381 \endminipage%
382 \hfil%
383 \endgroup%
384 }
385 % \end{macrocode}
386 %
387 % Finally, the row setting is fairly easy. We have to ensure that we obey
388 % the prevailing list parameters.
389 %
390 % \begin{macrocode}
391 \def\program@startline{%
392 \moveright\@totalleftmargin%
393 \hb@xt@\linewidth\bgroup%
394 \program@begin%
395 }
396 \def\program@endline{%
397 \program@end%
398 \egroup%
399 }
400 % \end{macrocode}
401 %
402 % \end{macro}
403 % \end{macro}
404 % \end{macro}
405 % \end{environment}
406 %
407 % \subsection{Other stuff}
408 %
409 % \begin{macro}{\Thing}
410 % \begin{macro}{\Succ}
411 % \begin{macro}{\Adv}
412 % \begin{macro}{\InSec}
413 % \begin{macro}{\Expt}
414 % \begin{macro}{\Game}
415 %
416 % Typesetting |\Thing| is easy. This acts as a style hook for the rest of
417 % these things.
418 %
419 % \begin{macrocode}
420 \def\Thing#1#2#3{\text{\normalfont\bfseries#1}^{\text{\normalfont#2}}_{#3}}
421 % \end{macrocode}
422 %
423 % And now here they are.
424 %
425 % \begin{macrocode}
426 \def\Succ{\Thing{Succ}}
427 \def\Adv{\Thing{Adv}}
428 \def\InSec#1{\Thing{InSec}{#1}{}}
429 \def\Expt{\Thing{Expt}}
430 \def\Game{\Thing{Game}}
431 % \end{macrocode}
432 %
433 % \end{macro}
434 % \end{macro}
435 % \end{macro}
436 % \end{macro}
437 % \end{macro}
438 % \end{macro}
439 %
440 % \begin{macro}{\G}
441 %
442 % The name of a game is typeset simply as
443 %
444 % \begin{macrocode}
445 \newcommand\G[2][G]{\mathbf{#1}_{#2}}
446 % \end{macrocode}
447 %
448 % \end{macro}
449 %
450 % \begin{macro}{\Func}
451 % \begin{macro}{\Perm}
452 %
453 % The finite sets of functions and permutations are just a style choice. We
454 % choose to buck the standard trends and use caligraphic letters.
455 %
456 % \begin{macrocode}
457 \def\Func#1#2{\mathcal{F}^{#1,#2}}
458 \def\Perm#1{\mathcal{P}^{#1}}
459 % \end{macrocode}
460 %
461 % \end{macro}
462 % \end{macro}
463 %
464 % \begin{macro}{\PKCS}
465 %
466 % Finally, I find that \PKCS{$n$} looks best typeset like this:
467 %
468 % \begin{macrocode}
469 \def\PKCS#1{PKCS\,\##1}
470 % \end{macrocode}
471 %
472 % \end{macro}
473 %
474 % \vskip\parskip\vbox{ ^^A The best way I could find of keeping this lot
475 % ^^A together, I'm afraid.
476 % That's all there is. Byebye.
477 %
478 % \begin{macrocode}
479 %</package>
480 % \end{macrocode}
481 % \nopagebreak
482 %
483 % \hfill Mark Wooding, \today
484 % }
485 % \Finale
486 %
487 \endinput