diff options
author | Karl Berry <karl@freefriends.org> | 2015-03-28 21:41:27 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2015-03-28 21:41:27 +0000 |
commit | 66d4bb2ab2031db51d7818a691dc267fd3991d79 (patch) | |
tree | 52d3a6e71c87eeca3dffa016cd60025ae56eb7c8 | |
parent | 719d9d5c5bf005dd81b357ada574bf794b34a7d2 (diff) |
cryptocode (28mar15)
git-svn-id: svn://tug.org/texlive/trunk@36659 c570f23f-e606-0410-a88d-b1316a301751
-rw-r--r-- | Master/texmf-dist/doc/latex/cryptocode/cryptocode.pdf | bin | 810963 -> 623560 bytes | |||
-rw-r--r-- | Master/texmf-dist/doc/latex/cryptocode/cryptocode.tex | 824 | ||||
-rw-r--r-- | Master/texmf-dist/tex/latex/cryptocode/cryptocode.sty | 418 |
3 files changed, 871 insertions, 371 deletions
diff --git a/Master/texmf-dist/doc/latex/cryptocode/cryptocode.pdf b/Master/texmf-dist/doc/latex/cryptocode/cryptocode.pdf Binary files differindex 802ec5b0226..602430e15a0 100644 --- a/Master/texmf-dist/doc/latex/cryptocode/cryptocode.pdf +++ b/Master/texmf-dist/doc/latex/cryptocode/cryptocode.pdf diff --git a/Master/texmf-dist/doc/latex/cryptocode/cryptocode.tex b/Master/texmf-dist/doc/latex/cryptocode/cryptocode.tex index 3dd048a2aee..35c85948c70 100644 --- a/Master/texmf-dist/doc/latex/cryptocode/cryptocode.tex +++ b/Master/texmf-dist/doc/latex/cryptocode/cryptocode.tex @@ -47,39 +47,42 @@ \usetikzlibrary{shapes.callouts} -\usetikzlibrary{ocgx} \usepackage{listings} \usepackage{trace} \usepackage{makeidx} +\usepackage{mdframed} + \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{mygray}{rgb}{0.1,0.1,0.1} \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 + backgroundcolor=\color{lightgray}, % choose the background color; you must add \usepackage{color} or \usepackage{xcolor} + rulecolor=\color{lightgray}, + basicstyle=\scriptsize, % 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 @@ -87,13 +90,15 @@ 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 + stepnumber=1, % the step between two line-numbers. If it's 1, each line will be numbered stringstyle=\color{mymauve}, % string literal style + frameround=ftff, + belowskip=0em, + aboveskip=1em, tabsize=2, % sets default tabsize to 2 spaces title=\lstname } \begin{document} -%\pcfixhyperref \maketitle @@ -125,16 +130,16 @@ x \sample \ZZ_q \< \< \\ \end{center} - \end{abstract} \newpage -\pagenumbering{arabic} +\pagenumbering{roman} \tableofcontents \thispagestyle{empty} \newpage +\pagenumbering{arabic} \chapter{Cryptocode by Example} Cryptocode is a \LaTeX package to ease the writing of cryptographic papers. It provides mechanisms @@ -199,7 +204,7 @@ The above code is generated by (the code is actually wrapped in an \lstinline$fb 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}). +highlights certain keywords (in the example it is responsible for setting \enquote{return} as \highlightkeyword[]{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. @@ -222,8 +227,8 @@ the \lstinline$\procedure$ command which takes an additional argument to specify \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. +to highlight the return statement. Besides \lstinline$\pcreturn$ there are a variant of predefined \enquote{keywords} +such as \lstinline$\pcfor$, \lstinline$\pcif$, etc. (all prefixed with pc) 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. @@ -273,6 +278,8 @@ is the same code with an overlay explanation and a division of the pseudocode. \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$ @@ -510,6 +517,8 @@ by using plain TIKZ commands. We'll discuss the details in Chapter~\ref{chap:bbr}. \chapter{Cryptographic Notation} +\label{chap:commands} +\index{package options} In this section we'll discuss the various commands for notation that can be loaded via package options. \begin{lstlisting} \usepackage[ @@ -537,6 +546,8 @@ papers (especially once you get to cryptographic notions and primitives). So ple drop me an email. \section{Security Parameter} +\index{security parameter|see {package options}} +\index{package options!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} @@ -555,25 +566,38 @@ Load the package option ``advantage'' in order to define the command \lstinline$ \advantage{prf}{\adv,\prf} = \negl \end{lstlisting} Specify an optional third parameter to replace the $(\secpar)$. +\begin{lstlisting} + \advantage{prf}{\adv,\prf}[(arg)] +\end{lstlisting} +In order to redefine the styles in which superscript and subscript are set redefine +\begin{lstlisting} + \renewcommand{\pcadvantagesuperstyle}[1]{\mathrm{\MakeLowercase{#1}}} + \renewcommand{\pcadvantagesubstyle}[1]{#1} +\end{lstlisting} \section{Math Operators} +\index{operators|see {package options}} +\index{package options!operators} +\index{math operators|see {package options}} The \enquote{operators} option provides the following list of commands: \begin{center} -\begin{tabular}{l l l l} +\begin{tabular}{l p{6cm} 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$\floor{42.5}$ & Rounding down & $\floor{42.5}$ & \\ +\lstinline$\ceil{41.5}$ & Rounding up & $\ceil{41.5}$ & \\ +\lstinline$\Angle{x,y}$ & Vector product & $\Angle{x,y}$ & \\ +\lstinline$\abs{42.9}$ & Absolute number & $\abs{42.9}$ & \\ +\lstinline$\norm{x}$ & 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} +\index{adversary|see {package options}} +\index{package options!adversary} The \enquote{adversary} option provides the following list of commands: \begin{center} @@ -596,6 +620,8 @@ The style in which an adversary is rendered is controlled via \section{Landau} +\index{Landau|see {package options}} +\index{package options!Landau} The \enquote{landau} option provides the following list of commands: \begin{center} @@ -610,6 +636,8 @@ The \enquote{landau} option provides the following list of commands: \section{Probabilities} +\index{probability|see {package options}} +\index{package options!probabilities} The \enquote{probability} option provides commands for writing probabilities. Use \begin{lstlisting} \prob{X=x} @@ -671,6 +699,8 @@ This yields \end{align*} \section{Sets} +\index{sets|see {package options}} +\index{package options!sets} The \enquote{sets} option provides commands for basic mathematical sets. You can write sets and sequences as \begin{lstlisting} @@ -691,6 +721,7 @@ In addation the following commands are provided \lstinline$\NN$ & Natural numbers & $\NN$ \\ \lstinline$\ZZ$ & Integers & $\ZZ$ \\ \lstinline$\QQ$ & Rational numbers & $\QQ$ \\ +\lstinline$\CC$ & Complex numbers & $\CC$ \\ \lstinline$\RR$ & Reals & $\RR$ \\ \lstinline$\PP$ & & $\PP$ \\ \lstinline$\FF$ & & $\FF$ \\ @@ -698,15 +729,18 @@ In addation the following commands are provided \end{center} \section{Crypto Notions} +\index{notions|see {package options}} +\index{package options!notions} +\index{Crypto notions|see {package options}} 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$\indcca$ & IND-CCA security for encryption schemes & $\indcca$ \\ +\lstinline$\indccai$ & IND-CCA1 security for encryption schemes & $\indccai$ \\ +\lstinline$\indccaii$ & 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$ \\ @@ -724,6 +758,8 @@ The style in which notions are displayed can be controlled via redefining \end{lstlisting} \section{Logic} +\index{logic|see {package options}} +\index{package options!logic} The \enquote{logic} option provides the following list of commands: \begin{center} @@ -741,6 +777,9 @@ The \enquote{logic} option provides the following list of commands: % Function Families \section{Function Families} +\index{ff|see {package options}} +\index{package options!ff} +\index{function families|see {package options}} The \enquote{ff} option provides the following list of commands: \begin{center} @@ -765,6 +804,9 @@ The style in which these are displayed can be controlled via redefining % Machine Model \section{Machine Model} +\index{machine model|see {package options}} +\index{package options!mm} +\index{mm|see {package options}} The \enquote{mm} option provides the following list of commands: \begin{center} @@ -787,6 +829,9 @@ The style in which these are displayed can be controlled via redefining \end{lstlisting} \section{Crypto Primitives} +\index{primitives|see {package options}} +\index{package options!primitives} +\index{Crypto primitives|see {package options}} The \enquote{primitives} option provides the following list of commands: @@ -824,6 +869,8 @@ The style in which these are displayed can be controlled via redefining \end{lstlisting} \section{Events} +\index{events|see {package options}} +\index{package options!events} The \enquote{events} option provides the following list of commands. To classify an event use @@ -840,6 +887,8 @@ where the second is meant as the negation. These are typset as For bad events, use \lstinline$\bad$ ($\bad$). \section{Complexity} +\index{complexity|see {package options}} +\index{package options!complexity} The \enquote{complexity} option provides the following list of commands: \begin{center} @@ -864,6 +913,8 @@ The style in which these are displayed can be controlled via redefining \end{lstlisting} \section{Asymptotics} +\index{asymptotics|see {package options}} +\index{package options!asymptotics} The \enquote{asymptotics} option provides the following list of commands: \begin{center} @@ -871,8 +922,8 @@ The \enquote{asymptotics} option provides the following list of commands: \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$ \\ +\lstinline$\pp$ & some polynomial $\pp$ & $\pp$ \\ +\lstinline$\qq$ & some polynomial $\qq$ & $\qq$ \\ \end{tabular} \end{center} @@ -882,13 +933,15 @@ The style in which these are displayed can be controlled via redefining \end{lstlisting} \section{Keys} +\index{keys|see {package options}} +\index{package options!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$\pk$ &public key & $\pk$ \\ \lstinline$\vk$ & verification key& $\vk$ \\ \lstinline$\sk$ & secret key& $\sk$ \\ \lstinline$\key$ & a plain key& $\key$ \\ @@ -910,6 +963,7 @@ The style in which these are displayed can be controlled via redefining In this chapter we discuss how to write pseudocode with the cryptocode library. \section{Basics} +\index{\textbackslash pseudocode} The cryptocode package provides the command \emph{pseudocode} in order to write simple cryptostyle algorithms. Consider the following definition of an IND-CPA game @@ -951,6 +1005,24 @@ Code \subsection{Customizing Pseudocode} +\index{lnstart} +\index{head} +\index{lnstartright} +\index{linenumbering} +\index{syntaxhighlight} +\index{keywords} +\index{keywordsindent} +\index{keywordsunindent} +\index{keywordsuninindent} +\index{addkeywords} +\index{altkeywords} +\index{mode} +\index{space} +\index{xshift} +\index{colsep} +\index{addtolength} +\index{nodraft} + 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} @@ -972,13 +1044,33 @@ Enables line numbering. 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. +Provide a comma separated list of keywords for automatic syntax highlighting. To customize the behavior of automatic spacing +you can provide keywords as +\begin{description} +\item[keywordsindent] +After seeing this keyword all following lines will be indented one extra level. +\item[keywordsunindent] +After seeing this keyword the current and all following lines will be unindented one extra level. +\item[keywordsuninindent] +After seeing this keyword the current line will be unindented one level. +\end{description} \item[addkeywords] Provide additional keywords for automatic syntax highlighting. +\item[altkeywords] +Provide a second list of keywords for automatic syntax highlighting that are highlighted differently. \item[mode] When set to text pseudocode will not start in math mode but in text mode. +\item[space] +Allows you to enable automatic spacing mode. If set to \enquote{keep} the spaces in the input are preserved. +If set to \enquote{auto} it will try to detect spacing according to keywords such as \enquote{if} and \enquote{fi}. \item[xshift] Allows horizontal shifting +\item[colsep] +Defines the space between columns. +\item[addtolength] +Is added to the automatically computed width of the pseudocode (which does not take colsep into account). +\item[nodraft] +Forces syntax highlighting also in draft mode. \end{description} The following code \begin{lstlisting} @@ -989,12 +1081,56 @@ creates \fbox{\pseudocode[linenumbering,syntaxhighlight=auto,head=Header]{ return null }} \end{center} +\subsection{Customized Pseudocode Commands} +\index{\textbackslash procedure} +\index{\textbackslash createpseudocodecommand} +\index{\textbackslash createprocedurecommand} + +Besides the \lstinline{\pseudocode} command the command \lstinline{\procedure} +provides easy access to generate code with a header. It takes the following form +\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} + +You can define customized pseudocode commands with either take one optional argument and two mandatory arguments (as the procedure command) +or one optional and one mandatory argument (as the pseudocode command). The following +\begin{lstlisting} +\createprocedurecommand{mypseudocode}{}{}{linenumbering} +\createpseudocodecommand{myheadlesscmd}{}{}{linenumbering} +\end{lstlisting} +creates the commands \lstinline$\mypseudocode$ and \lstinline$\myheadlesscmd$ with line numbering always enabled. The first command +has an identical interface as the \lstinline$\pseudocode$ command, the second has an interface as the \lstinline$\procedure$ command. +The second and third argument that we kept empty when generating the commands allows us to specify commands that are executed at the +very beginning when the command is called (argument 2) and a prefix for the header. -\subsection{Indentation} +\section{Indentation} \index{indentation} -\index{pcind} -\index{t} +\index{\textbackslash pcind} +\index{\textbackslash 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. @@ -1040,13 +1176,94 @@ You can specify multiple levels via the optional first argument \pcind[5] T[i] \sample \bin^n } \end{lstlisting} -\index{pcindentname} +\index{\textbackslash pcindentname} You can customize the indentation shortcut by redefining \begin{lstlisting} \renewcommand{\pcindentname}{t} \end{lstlisting} + +\subsubsection{Automatic Indentation} +The pseudocode command comes with an option \enquote{space=auto} which tries to detect the correct indentation from the use of +keywords. When it sees one of the following keywords +\begin{lstlisting} +\pcif, \pcfor, \pcwhile, \pcrepeat, \pcforeach +\end{lstlisting} +it will increase the indentation starting from the next line. It will again remove the indentation on seeing +\begin{lstlisting} +\pcfi, \pcendif, \pcendfor, \pcendwhile, \pcuntil, \pcendforeach +\end{lstlisting} +Additionally, on seeing +\begin{lstlisting} +\pcelse, \pcelseif +\end{lstlisting} +it will remove the indentation for that particular line. Thus the following +\begin{center} +\fbox{\pseudocode[space=auto]{% +\pcfor a \in [10] \pcdo \\ + \pcfor a \in [10] \pcdo \\ + \pcfor a \in [10] \pcdo \\ + \pcif a = b \pcthen \\ + \text{some operation} \\ + \pcelseif a = c \pcthen \\ + \text{some operation} \\ + \pcelse \\ + \text{some default operation} \\ + \pcfi \\ + \pcendfor \\ + \pcendfor \\ +\pcendfor \\ +\pcreturn a}} +\end{center} +can be obtained by: +\begin{lstlisting} +\pseudocode[space=auto]{% +\pcfor a \in [10] \pcdo \\ + \pcfor a \in [10] \pcdo \\ + \pcfor a \in [10] \pcdo \\ + \pcif a = b \pcthen \\ + \text{some operation} \\ + \pcelseif a = c \pcthen \\ + \text{some operation} \\ + \pcelse \\ + \text{some default operation} \\ + \pcfi \\ + \pcendfor \\ + \pcendfor \\ +\pcendfor \\ +\pcreturn a} +\end{lstlisting} +Note that the manual indentation in the above example is not necessary for the outcome. Further note that the same +works when using automatic syntax highlighting (see Section~\ref{sec:syntaxhighlight}). + +\subsubsection{Keep Input Indentation} +\label{sec:keepindent} +The pseudocode package comes with an experimental feature that preserves the spacing in the input. This can be enabled with +the option \enquote{space=keep}. Thus the above can also be written as +\begin{center} +\pseudocode[space=keep]{% +\pcfor i = 1..10 \pcdo \\ + T[i] \sample \bin^n \\ + T[i] \sample \bin^n \\ + T[i] \sample \bin^n \\ + T[i] \sample \bin^n \\ + T[i] \sample \bin^n } +\end{center} +\begin{lstlisting} +\pseudocode[space=keep]{% +\pcfor i = 1..10 \pcdo \\ + T[i] \sample \bin^n \\ + T[i] \sample \bin^n \\ + T[i] \sample \bin^n \\ + T[i] \sample \bin^n \\ + T[i] \sample \bin^n } +\end{lstlisting} +Note that automatic spacing only works when the \lstinline$\pseudocode$ command is not wrapped within another command. +Thus in order to get a frame box \lstinline$\fbox{\pseudocode[space=keep]{code}}$ will not work but you would need to use +an environment such as one offered by the \emph{mdframed} package ((\url{https://www.ctan.org/pkg/mdframed}). Also see +Section~\ref{sec:keepindent-problem}. + -\subsection{Textmode} +\section{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 @@ -1062,26 +1279,32 @@ 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} +\section{Syntax Highlighting} +\label{sec:syntaxhighlight} + +\index{\textbackslash pccontinue} +\index{\textbackslash pccomment} +\index{\textbackslash pcdo} +\index{\textbackslash pcdone} +\index{\textbackslash pcelse} +\index{\textbackslash pcelseif} +\index{\textbackslash pcfi} +\index{\textbackslash pcendif} +\index{\textbackslash pcforeach} +\index{\textbackslash pcendforeach} +\index{\textbackslash pcglobvar} +\index{\textbackslash pcif} +\index{\textbackslash pcin} +\index{\textbackslash pcnew} +\index{\textbackslash pcnull} +\index{\textbackslash pcparse} +\index{\textbackslash pcrepeat} +\index{\textbackslash pcuntil} +\index{\textbackslash pcreturn} +\index{\textbackslash pcthen} +\index{\textbackslash pctrue} +\index{\textbackslash pcwhile} +\index{\textbackslash pcendwhile} In the above examples we have used commands \lstinline$\pcreturn$ and \lstinline$\pcfor$ to highlight certain keywords. @@ -1096,12 +1319,16 @@ examples the package defines the following set of constants: pcdo & \lstinline$\pcdo$ & \pcdo \\ pcdone & \lstinline$\pcdone$ & \pcdone \\ pcfalse & \lstinline$\pcfalse$ & \pcfalse \\ - pcelse & \lstinline$\pcelse$ & \pcelse \\ + pcif & \lstinline$\pcif$ & \pcif \\ pcfi & \lstinline$\pcfi$ & \pcfi \\ + pcendif & \lstinline$\pcendif$ & \pcendif \\ + pcelse & \lstinline$\pcelse$ & \pcelse \\ + pcelseif & \lstinline$\pcelseif$ & \pcelseif \\ pcfor & \lstinline$\pcfor$ & \pcfor \\ + pcendfor & \lstinline$\pcendfor$ & \pcendfor \\ pcforeach & \lstinline$\pcforeach$ & \pcforeach \\ + pcendforeach & \lstinline$\pcendforeach$ & \pcendforeach \\ pcglobvar & \lstinline$\pcglobvar$ & \pcglobvar \\ - pcif & \lstinline$\pcif$ & \pcif \\ pcin & \lstinline$\pcin$ & \pcin \\ pcnew & \lstinline$\pcnew$ & \pcnew \\ pcnull & \lstinline$\pcnull$ & \pcnull \\ @@ -1111,7 +1338,8 @@ examples the package defines the following set of constants: pcreturn & \lstinline$\pcreturn$ & \pcreturn \\ pcthen & \lstinline$\pcthen$ & \pcthen \\ pctrue & \lstinline$\pctrue$ & \pctrue \\ - pcwhile & \lstinline$\pcwhile$ & \pcwhile + pcwhile & \lstinline$\pcwhile$ & \pcwhile \\ + pcendwhile & \lstinline$\pcendwhile$ & \pcendwhile \\ \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 @@ -1127,10 +1355,12 @@ Furthermore all constants have a trailing space. This can be removed by adding t \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 +\index{highlight keywords} +\index{emphasize|see highlight keywords} +\index{\textbackslash highlightkeyword} +In order to change the font you can overwrite the command \lstinline{\highlightkeyword} which is defined as \begin{lstlisting} -\newcommand{\pseudocodeconstant}[2][\ ]{\ensuremath{\mathbf{#2}}#1} +\newcommand{\highlightkeyword}[2][\ ]{\ensuremath{\mathbf{#2}}#1} \end{lstlisting} \subsubsection{Automatic Syntax Highlighting} @@ -1171,128 +1401,126 @@ can be typeset as 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} +\index{addkeywords} +You can also define additional keywords using the \enquote{addkeywords} option. This would allow us to specify \enquote{length} and \enquote{swap} in the above example. Combined with automatic spacing we could thus get +\begin{center} +\procedure[space=keep,syntaxhighlight=auto,addkeywords={swap,length}]{Bubblesort(A : list of items)}{ +n \gets length(A) \\ +repeat \\ + s \gets false \\ + for i=1 to n-1 do \\ + \pccomment{if this pair is out of order} \\ + if A[i-1]>A[i] then \\ + \pccomment{swap them and remember something changed} \\ + swap(A[i-1], A[i]) \\ + s \gets true \\ +until \neg{}s } +\end{center} +Simply by writing (note the \lstinline$\neg{}$ in order to not have a space before $s$): +\begin{lstlisting} +\procedure[space=keep,syntaxhighlight=auto,addkeywords={swap,length}]{Bubblesort(A : list of items)}{ +n \gets length(A) \\ +repeat \\ + s \gets false \\ + for i=1 to n-1 do \\ + \pccomment{if this pair is out of order} \\ + if A[i-1]>A[i] then \\ + \pccomment{swap them and remember something changed} \\ + swap(A[i-1], A[i]) \\ + s \gets true \\ +until \neg{}s } +\end{lstlisting} +Also note that a simple \lstinline$\fbox$ around the above \lstinline$\procedure$ command has the effect that the automatic spacing fails. +For this also see Section~\ref{sec:keepindent-problem}. As an alternative we could use automatic spacing and insert \enquote{group end} +keywords such as \enquote{fi}: +\begin{center} +\fbox{\procedure[space=auto,syntaxhighlight=auto,addkeywords={swap,length}]{Bubblesort(A : list of items)}{ +n \gets length(A) \\ +repeat \\ + s \gets false \\ + for i=1 to n-1 do \\ + \pccomment{assuming this pair is out of order} \\ + if A[i-1]>A[i] then \\ + \pccomment{swap them and remember something changed} \\ + swap(A[i-1], A[i]) \\ + s \gets true \\ + endif \\ + endfor \\ +until \neg s }} +\end{center} +The last example is generated as (note that here fbox is fine.) +\begin{lstlisting} +\fbox{\procedure[space=auto,syntaxhighlight=auto,addkeywords={swap,length}]{Bubblesort(A : list of items)}{ +n \gets length(A) \\ +repeat \\ + s \gets false \\ + for i=1 to n-1 do \\ + \pccomment{assuming this pair is out of order} \\ + if A[i-1]>A[i] then \\ + \pccomment{swap them and remember something changed} \\ + swap(A[i-1], A[i]) \\ + s \gets true \\ + endif \\ + endfor \\ +until \neg s }} +\end{lstlisting} + +\subsection{Alternative Keywords} +\index{altkeywords} +There is a second keyword list that you can add keywords to which are highlighted not via \lstinline$\highlightkeyword$ but via \lstinline$\highlightaltkeyword$ +where alt stands for alternate. This allows you to have two different keyword styles which are by default defined as +\begin{lstlisting} +\newcommand{\highlightkeyword}[2][\ ]{\ensuremath{\mathbf{#2}}#1} +\newcommand{\highlightaltkeyword}[1]{\ensuremath{\mathsf{#1}}} +\end{lstlisting} +This allows you to rewrite the above example and highlight the different nature of swap and length. +\begin{center} +\fbox{\procedure[space=auto,syntaxhighlight=auto,altkeywords={swap,length}]{Bubblesort(A : list of items)}{ +n \gets length(A) \\ +repeat \\ + s \gets false \\ + for i=1 to n-1 do \\ + \pccomment{assuming this pair is out of order} \\ + if A[i-1]>A[i] then \\ + \pccomment{swap them and remember something changed} \\ + swap(A[i-1], A[i]) \\ + s \gets true \\ + endif \\ + endfor \\ +until \neg s }} +\end{center} +\begin{lstlisting} +\procedure[space=auto,syntaxhighlight=auto,addkeywords={swap,length}]{Bubblesort(A : list of items)}{ +n \gets length(A) \\ +repeat \\ + s \gets false \\ + for i=1 to n-1 do \\ + \pccomment{assuming this pair is out of order} \\ + if A[i-1]>A[i] then \\ + \pccomment{swap them and remember something changed} \\ + swap(A[i-1], A[i]) \\ + s \gets true \\ + endif \\ + endfor \\ +until \neg s } +\end{lstlisting} + +\subsection{Draft Mode} +\index{nodraft} +\index{performance} +\index{draft mode} +Automatic syntax highlighting is a somewhat expensive operation as it requires several rounds of regular expression matching. +In order to speed up compilation the pseudocode command will not attempt automatic highlighting when the document is in draft mode. +When in draft mode and you want to force a specific instance of \lstinline$\pseudocode$ to render the code with automatic syntax +highlighting you can use the option \lstinline$nodraft$. \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 +\index{\textbackslash pcln} +\index{\textbackslash pclnr} +\index{\textbackslash pcrln} +The pseudocode command allows to insert line numbers into pseudocode. You can either manually control line numbering or simply turn on the option \enquote{linenumbering}. \begin{center} \fbox{% @@ -1317,7 +1545,7 @@ is generated by \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} +\subsection{Manually Inserting Line Numbers} In order to manually insert line numbers use the command \lstinline$\pcln$. \begin{center} \fbox{% @@ -1362,7 +1590,7 @@ You can specify the start value (-1) of the counter by setting the option \enquo \end{center} \subsection{Separators} -\index{pclnseparator} +\index{\textbackslash 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}. @@ -1439,7 +1667,7 @@ by the subprocedure. For more control, either use manual numbering or set the op \section{Stacking Procedures} -You can stack procedures horizontally or vertically using the environments ``pchstack'' and ``pcvstack''. +You can stack procedures horizontally or vertically using the environments \enquote{pchstack} and \enquote{pcvstack}. \begin{lstlisting} \begin{pchstack}[center] body \end{pchstack} \begin{pcvstack}[center] body \end{pcvstack} @@ -1617,7 +1845,7 @@ Horizontal and vertical stacking can be combined \section{Divisions and Linebreaks} \index{linebreaks} -\index{hline} +\index{\textbackslash hline} Within the pseudocode command you generate linebreaks as $\\$. In order to specify the linewidth you can add an optional argument \begin{lstlisting} \\[height] @@ -1650,7 +1878,7 @@ Furthermore, you can add, for example a horizontal line by using the second opti \end{lstlisting} -\section{Fancy Code} +\section{Fancy Code with Overlays} 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. @@ -1771,68 +1999,17 @@ As an exmaple of what you can do with this, let us put an explanation to a line \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{\&} \index{Tabbing Mode} -\index{mintabmode} +\index{\textbackslash $"<$} +\index{\textbackslash $">$} 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 +This is similar to using an \lstinline$align$ environment and placing a tabbing character (\&). Also, +similarly to using \lstinline$align$ you should ensure that the number of \lstinline$\>$ are identical on each line. \begin{center} \fbox{% @@ -1862,6 +2039,26 @@ using \lstinline$\>\>$. You can also use \lstinline$\<$ a shorthand for \lstinli b \sample \bin \< b \sample \bin \< b \sample \bin \< b \sample \bin} \end{lstlisting} +\paragraph{Column Spacing} +\index{colsep} +\index{addtolength} +You can control the space between columns using the option \enquote{colsep=2em}. Note that when doing so you should +additionally use \enquote{addtolength=5em} (where 5em depends on the number of columns) in order to avoid having +overfull hboxes. +\begin{center} +\fbox{% +\pseudocode[colsep=1em,addtolength=10em]{% + \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{% +\pseudocode[colsep=1em,addtolength=10em]{% + \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}. @@ -1894,7 +2091,7 @@ The next two sections are rather technical, so feel free to skip them. \subsection{Overriding The Tabbing Character} -\index{pctabname} +\index{\textbackslash 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} @@ -1908,11 +2105,11 @@ For example \end{lstlisting} \subsection{Custom Line Spacing and Horizontal Rules} -\index{pclb} +\index{\textbackslash 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 +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} @@ -1921,7 +2118,7 @@ does not insert the additional alignment characters. The pseudocode package can also be used to write protocols such as \begin{center} \fbox{% -\protocol{My Protocol}{% +\procedure{My Protocol}{% \textbf{Alice} \> \> \textbf{Bob} \\ b \sample \bin \> \> \\ \> \xrightarrow{\text{send over } b} \> \\ @@ -1932,7 +2129,7 @@ The pseudocode package can also be used to write protocols such as \end{center} which uses the tabbing feature of align and is generated as \begin{lstlisting} -\protocol{My Protocol}{% +\procedure{My Protocol}{% \textbf{Alice} \> \> \textbf{Bob} \\ b \sample \bin \> \> \\ \> \xrightarrow{\text{send over } b} \> \\ @@ -1941,6 +2138,11 @@ which uses the tabbing feature of align and is generated as \text{finalize} \> \>} } \end{lstlisting} + +\index{\textbackslash sendmessageright*} +\index{\textbackslash sendmessageright} +\index{\textbackslash sendmessageleft*} +\index{\textbackslash sendmessageleft} 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. @@ -1952,7 +2154,7 @@ in math mode. \begin{center} \fbox{% -\protocol{My Protocol}{% +\procedure{My Protocol}{% \textbf{Alice} \> \> \textbf{Bob} \\ b \sample \bin \> \> \\ \> \sendmessageright*{\text{send over } b} \> \\ @@ -1962,7 +2164,7 @@ in math mode. } \end{center} \begin{lstlisting} -\protocol{My Protocol}{% +\procedure{My Protocol}{% \textbf{Alice} \> \> \textbf{Bob} \\ b \sample \bin \> \> \\ \> \sendmessageright*{\text{send over } b} \> \\ @@ -1974,7 +2176,7 @@ in math mode. 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}{% +\procedure{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}} \> \\ @@ -1983,7 +2185,7 @@ the starred and unstarred versions are that \lstinline$\sendmessageleft*{messag \text{finalize} \> \> } \end{center} \begin{lstlisting} -\protocol{My Protocol}{% +\procedure{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}} \> \\ @@ -2030,7 +2232,7 @@ For this you can use \lstinline$\<$ instead of \lstinline$\>$ (see Chapter~\ref{ Following is once more the example from before but now with double tapping. \begin{center} \fbox{% -\protocol{My Protocol}{% +\procedure{My Protocol}{% \textbf{Alice} \< \< \textbf{Bob} \\ b \sample \bin \< \< \\ \< \sendmessageright*{\text{send over } b} \< \\ @@ -2045,7 +2247,7 @@ Using the send message commands you can easily generate multiline messages as th wraps an \emph{aligned} environment around the message. \begin{center} \fbox{% -\protocol{My Protocol}{% +\procedure{My Protocol}{% \textbf{Alice} \< \< \textbf{Bob} \\ b \sample \bin \< \< \\ \< \sendmessageright*{\text{send over } b\\ \text{second line}} \< \\ @@ -2055,7 +2257,7 @@ wraps an \emph{aligned} environment around the message. } \end{center} \begin{lstlisting} -\protocol{My Protocol}{% +\procedure{My Protocol}{% \textbf{Alice} \< \< \textbf{Bob} \\ b \sample \bin \< \< \\ \< \sendmessageright*{\text{send over } b\\ \text{second line}} \< \\ @@ -2076,7 +2278,7 @@ You are not limited to two players. In order to send messages skipping players u \begin{center} \fbox{% -\protocol{Multiparty Protocol}{% +\procedure{Multiparty Protocol}{% \textbf{Alice} \< \< \textbf{Bob} \< \< \textbf{Charlie} \\ \text{work} \< \< \< \< \\ \< \sendmessageright{top=Work result} \< \< \< \\ @@ -2090,7 +2292,7 @@ You are not limited to two players. In order to send messages skipping players u \begin{lstlisting} \begin{center} -\protocol{Multiparty Protocol}{% +\procedure{Multiparty Protocol}{% \textbf{Alice} \< \< \textbf{Bob} \< \< \textbf{Charlie} \\ \text{work} \< \< \< \< \\ \< \sendmessageright{top=Work result} \< \< \< \\ @@ -2115,7 +2317,7 @@ before. Also see Chapter~\ref{chap:tabbing}. \begin{center} \fbox{% -\protocol{My Protocol}{% +\procedure{My Protocol}{% \textbf{Alice} \< \< \textbf{Bob} \\ b \sample \bin \< \< \pclb \pcintertext[dotted]{Some Division} \\ @@ -2128,7 +2330,7 @@ before. Also see Chapter~\ref{chap:tabbing}. \end{center} \begin{lstlisting} -\protocol{My Protocol}{% +\procedure{My Protocol}{% \textbf{Alice} \< \< \textbf{Bob} \\ b \sample \bin \< \< \pclb \pcintertext[dotted]{Some Division} \\ @@ -2147,7 +2349,7 @@ allows you to right align line numbers but uses the same counter as \lstinline$\ \begin{center} \fbox{% -\protocol{My Protocol}{% +\procedure{My Protocol}{% \pcln \textbf{Alice} \< \< \textbf{Bob} \< \\ \pcln b \sample \bin \< \< \< \\ \< \sendmessageright*{\text{send over } b} \< \< \pclnr\\ @@ -2158,7 +2360,7 @@ allows you to right align line numbers but uses the same counter as \lstinline$\ \end{center} Which is generated as \begin{lstlisting} -\protocol{My Protocol}{% +\procedure{My Protocol}{% \pcln \textbf{Alice} \< \< \textbf{Bob} \< \\ \pcln b \sample \bin \< \< \< \\ \< \sendmessageright*{\text{send over } b} \< \< \pclnr\\ @@ -2172,7 +2374,7 @@ Which is generated as And using \lstinline$\pcrln$: \begin{center} \fbox{% -\protocol{My Protocol}{% +\procedure{My Protocol}{% \pcln \textbf{Alice} \< \< \textbf{Bob} \< \\ \pcln b \sample \bin \< \< \< \\ \< \sendmessageright*{\text{send over } b} \<\< \pcrln\\ @@ -2183,7 +2385,7 @@ And using \lstinline$\pcrln$: \end{center} Which is generated as \begin{lstlisting} -\protocol{My Protocol}{% +\procedure{My Protocol}{% \pcln \textbf{Alice} \< \< \textbf{Bob} \\ \pcln b \sample \bin \< \< \\ \< \sendmessageright*{\text{send over } b} \< \pcrln\\ @@ -2206,12 +2408,12 @@ Use the \enquote{subprocedure} function also to create sub protocols. \begin{center} \fbox{% -\protocol{My Protocol}{% +\procedure{My Protocol}{% \textbf{Alice} \< \< \textbf{Bob} \\ b \sample \bin \< \< \\ \< \sendmessageright*{\text{send over } b} \< \\ \< \< \text{do something} \\ - \<\< \dbox{\begin{subprocedure}\pseudocode{ + \<\< \dbox{\begin{subprocedure}\procedure{Subprotocol}{ \< \< \textbf{Charlie} \\ \text{something more} \< \< \\ \< \sendmessageright*[1.5cm]{\text{message}} \< \\ @@ -2227,13 +2429,13 @@ Use the \enquote{subprocedure} function also to create sub protocols. \end{center} - \begin{lstlisting} -\protocol{My Protocol}{% +\begin{lstlisting} +\procedure{My Protocol}{% \textbf{Alice} \< \< \textbf{Bob} \\ b \sample \bin \< \< \\ \< \sendmessageright*{\text{send over } b} \< \\ \< \< \text{do something} \\ - \<\< \dbox{\begin{subprocedure}\pseudocode{ + \<\< \dbox{\begin{subprocedure}\procedure{Subprotocol}{ \< \< \textbf{Charlie} \\ \text{something more} \< \< \\ \< \sendmessageright*[1.5cm]{\text{message}} \< \\ @@ -2298,7 +2500,8 @@ by using \subsection{Highlight Changes} -\index{gamechange} +\index{highlight game change} +\index{\textbackslash gamechange} In order to highlight changes from one game to the next use \lstinline$\gamechange$. \begin{center} \begin{gameproof} @@ -2326,7 +2529,7 @@ In order to highlight changes from one game to the next use \lstinline$\gamechan \end{lstlisting} \subsection{Boxed games} -\index{tbxgameprocedure} +\index{\textbackslash 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} @@ -2364,7 +2567,7 @@ Use \lstinline$\tbxgameprocedure$ in order to create two consecutive games where \end{lstlisting} \subsection{Reduction Hints} -\index{addgamehop} +\index{\textbackslash 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$ @@ -2510,8 +2713,8 @@ The default name and argument are controlled via the commands \lstinline$\pcgame \subsection{Two Directional Games} -\index{bxgameprocedure} -\index{addloopgamehop} +\index{\textbackslash bxgameprocedure} +\index{\textbackslash 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. @@ -2555,8 +2758,8 @@ to add the gamehop in the middle. \label{chap:bbr} \index{bbrenv} \index{bbrbox} -\index{bbrinput} -\index{bbroutput} +\index{\textbackslash bbrinput} +\index{\textbackslash bbroutput} The cryptocode package comes with support for drawing basic black box reductions. A reduction is always of the following form. @@ -2652,13 +2855,15 @@ name=Box Name \end{lstlisting} to specify the label. The following options are available +\begin{center} \begin{tabular}{ll} -Option & Description \\ \hline +\textbf{Option} & \textbf{Description} \\ \hline name & Specifies the box' label \\ minheight & The minimal height \\ xshift & Allows horizontal positioning \\ style & allows to customize the node \end{tabular} +\end{center} \section{Nesting of Boxes} @@ -2721,10 +2926,10 @@ existing bbrbox. \end{lstlisting} \section{Messages and Queries} -\index{bbrmsgto} -\index{bbrmsgfrom} -\index{bbrqryto} -\index{bbrqryfrom} +\index{\textbackslash bbrmsgto} +\index{\textbackslash bbrmsgfrom} +\index{\textbackslash bbrqryto} +\index{\textbackslash bbrqryfrom} You can send messages and queries to boxes. For this use the commands \begin{lstlisting} @@ -2907,7 +3112,7 @@ Via the option \enquote{length} you can specifiy the length of the arrow. \end{lstlisting} \subsection{Loops} -\index{bbrloop} +\index{\textbackslash 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 @@ -3014,7 +3219,8 @@ to be shown on the left, center or right. Here is the result. \end{lstlisting} \subsection{Add Space} -\index{bbrmsgspace} +\index{\textbackslash bbrmsgspace} +\index{\textbackslash bbrqryspace} 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} @@ -3075,8 +3281,8 @@ If the spacing between messages is not sufficient you can use the \lstinline$bbr \subsection{Intertext} -\index{bbrmsgtxt} -\index{bbrqrytxt} +\index{\textbackslash bbrmsgtxt} +\index{\textbackslash bbrqrytxt} If your reduction needs to do some extra work between queries use the \lstinline$\bbrmsgtxt$ and \lstinline$\bbrqrytxt$ commands. @@ -3158,6 +3364,7 @@ and \lstinline$\bbrqrytxt$ commands. \section{Oracles} \index{bbroracle} +\index{distance} 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. @@ -3183,7 +3390,7 @@ go behind the single \emph{bbrbox} environment within an \emph{bbrenv} enviornme \end{bbrbox} \end{bbroracle} - \begin{bbroracle}{OraB} + \begin{bbroracle}{OraB}[distance=3cm] \begin{bbrbox}[name=Oracle 2] \end{bbrbox} \end{bbroracle} @@ -3218,10 +3425,12 @@ go behind the single \emph{bbrbox} environment within an \emph{bbrenv} enviornme \end{bbroracle} \end{bbrenv} \end{lstlisting} +Via the option \enquote{distance=length} you can control the horizontal position of the oracle. By default this value is set to 1cm. + \subsection{Communicating with Oracles} -\index{bbroraclequeryfrom} -\index{bbroraclequeryto} +\index{\textbackslash bbroraclequeryfrom} +\index{\textbackslash 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} @@ -3296,16 +3505,49 @@ that oracle messages need to be added after the closing \lstinline$\end{bbroracl \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}$. + +\chapter{Known Issues} + +\section{Pseudocode KeepSpacing within Commands} +\label{sec:keepindent-problem} +\index{\textbackslash fbox} +\index{framebox} +The \enquote{space=keep} option of pseudocode which should output spacing identical to that of the input +will fail, if the pseudocode command is called from within another command. An example is to +wrap the \lstinline$\pseudocode$ command with an \lstinline$\fbox$. As a workaround for generating frame boxes you should hence +use a package such as \emph{mdframed} (\url{https://www.ctan.org/pkg/mdframed}) which provides a frame environment. + +\begin{mdframed} +\pseudocode[space=keep,mode=text]{ Pseudocode with - spaces -} +\end{mdframed} +\begin{lstlisting} +\pseudocode[space=keep,mode=text]{ Pseudocode with - spaces -} +\end{lstlisting} + +As an alternative you could use a \emph{savebox} (in combination with the \lstinline$lrbox$ environment): + +\newsavebox{\mypcbox} +\begin{lrbox}{\mypcbox}% +\pseudocode[space=keep,mode=text]{ Pseudocode with - spaces -}% +\end{lrbox} +\fbox{\usebox{\mypcbox}} + +\begin{lstlisting} +\newsavebox{\mypcbox} +\begin{lrbox}{\mypcbox}% +\pseudocode[space=keep,mode=text]{ Pseudocode with - spaces -}% +\end{lrbox} +\fbox{\usebox{\mypcbox}} +\end{lstlisting} + +\section{AMSFonts} +\index{amsfonts} +\index{noamsfonts|see {package options}} +\index{package options!noamsfonts} +Some packages are not happy with the \enquote{amsfonts} package. Cryptocode will attempt to load amsfonts if it is loaded +with either the \enquote{sets} or the \enquote{probability} option. In order to not load amsfonts you can additionally add the +\enquote{noamsfonts} at the very end. Note that in this case you should ensure that the command \lstinline$\mathbb$ is defined +as this is used by most of the commands in \enquote{sets} and some of the commands in \enquote{probability}. \printindex diff --git a/Master/texmf-dist/tex/latex/cryptocode/cryptocode.sty b/Master/texmf-dist/tex/latex/cryptocode/cryptocode.sty index 1e1b843dfce..72fc9e90b12 100644 --- a/Master/texmf-dist/tex/latex/cryptocode/cryptocode.sty +++ b/Master/texmf-dist/tex/latex/cryptocode/cryptocode.sty @@ -21,6 +21,10 @@ \def\hi{Hello, this is Arno's crypto code package. } \let\myDate\date +\RequirePackage{amsmath} +\RequirePackage{mathtools} +%\usepackage{l3tl-analysis} % uncomment for debugging + %%% % option modes \newif\ifpc@orderofgrowth @@ -98,6 +102,7 @@ \providecommand\NN{\mathbb{N}} \providecommand\ZZ{\mathbb{Z}} + \providecommand\CC{\mathbb{C}} \providecommand\QQ{\mathbb{Q}} \providecommand\RR{\mathbb{R}} \providecommand\PP{\mathbb{P}} @@ -255,11 +260,9 @@ \ProcessOptions\relax %amsfonts -\RequirePackage{amsmath} \ifpc@amsfonts \RequirePackage{amsfonts} \fi -\RequirePackage{mathtools} \RequirePackage{xcolor} \RequirePackage{calc} \RequirePackage{tikz} @@ -271,6 +274,7 @@ \RequirePackage{forloop} \RequirePackage{array} \RequirePackage{xparse} +\RequirePackage{l3regex} \RequirePackage{pbox} \RequirePackage{varwidth} \RequirePackage{suffix} @@ -278,12 +282,13 @@ \RequirePackage{etex} \RequirePackage{etextools} \RequirePackage{environ} -\RequirePackage{xstring} -\RequirePackage{xspace} +%\RequirePackage{xspace} \RequirePackage{xkeyval} \ifpc@advantage - \newcommandx*{\advantage}[3][3=(\secpar)]{\ensuremath{\mathsf{Adv}^{\mathrm{\MakeLowercase{#1}}}_{#2}#3}} + \newcommand{\pcadvantagesuperstyle}[1]{\mathrm{\MakeLowercase{#1}}} + \newcommand{\pcadvantagesubstyle}[1]{#1} + \newcommandx*{\advantage}[3][3=(\secpar)]{\ensuremath{\mathsf{Adv}^{\pcadvantagesuperstyle{#1}}_{\pcadvantagesubstyle{#2}}#3}} \fi \ifpc@primitives @@ -293,7 +298,7 @@ \providecommand{\nizk}{\pcalgostyle{NIZK}} % hash - \providecommand{\hash}{\pcalgostyle{H}} + \renewcommand{\hash}{\pcalgostyle{H}} \providecommand{\gash}{\pcalgostyle{G}} \providecommand{\fash}{\pcalgostyle{F}} @@ -368,6 +373,9 @@ \settowidth{#1}{$\begin{aligned}#2\end{aligned}$} } +% check for draft mode +\def\@pc@ifdraft{\ifdim\overfullrule>\z@ + \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi} % run stuff in an empty box \newcommand{\@pcexecuteblindly}[1]{% @@ -414,6 +422,55 @@ %\global\let\label\relax } +\newcounter{@spacecounter} +\providecommand{\spacetoindent}{1} +\newenvironment{@withspaces} + {\obeyspaces\begingroup\lccode`~=` \lowercase{\endgroup\let~}\ } + {} + +%%%%%%%%%%%%%% +% a latex3 string substitution. +\ExplSyntaxOn +\tl_new:N \l_pc_strsub_input_tl +\tl_new:N \l_pc_strsub_search_tl +\tl_new:N \l_pc_strsub_replace_tl + +\NewDocumentCommand{\@pc@stringsubstitution}{mmm} + { + \tl_set:Nn \l_pc_strsub_input_tl { #1 } + \tl_set:Nn \l_pc_strsub_search_tl { #2 } + \tl_set:Nn \l_pc_strsub_replace_tl { #3 } +% \tl_show_analysis:N \l_pc_strsub_input_tl % uncomment for debugging +% \tl_show_analysis:N \l_pc_strsub_search_tl % uncomment for debugging +% \tl_show_analysis:N \l_pc_strsub_replace_tl % uncomment for debugging + \regex_replace_all:nnN + { (\W)\u{l_pc_strsub_search_tl} } %only match if keyword does not have a word character preceding + { \1\u{l_pc_strsub_replace_tl} } + \l_pc_strsub_input_tl + % \tl_show_analysis:N \l_tmpa_tl % uncomment for debugging + \tl_use:N \l_pc_strsub_input_tl + } + + % same as \@pc@stringsubstitution but without requiring the extra non word character + \NewDocumentCommand{\@pc@spacesubstitution}{mmm} + { + \tl_set:Nn \l_pc_strsub_input_tl { #1 } + \tl_set:Nn \l_pc_strsub_search_tl { #2 } + \tl_set:Nn \l_pc_strsub_replace_tl { #3 } +% \tl_show_analysis:N \l_pc_strsub_input_tl % uncomment for debugging +% \tl_show_analysis:N \l_pc_strsub_search_tl % uncomment for debugging +% \tl_show_analysis:N \l_pc_strsub_replace_tl % uncomment for debugging + \regex_replace_all:nnN + { \u{l_pc_strsub_search_tl} } + { \u{l_pc_strsub_replace_tl} } + \l_pc_strsub_input_tl + % \tl_show_analysis:N \l_tmpa_tl % uncomment for debugging + \tl_use:N \l_pc_strsub_input_tl + } + + +\ExplSyntaxOff + %%%%%%%% % line numbers %%%%%%%% @@ -472,7 +529,7 @@ \newlength{\@pc@alt@minipage@length} % backward games -\newif\ifbxgame +\newcommand{\@withinbxgame}{false} \newcommand{\@bxgameheader}{} @@ -482,7 +539,14 @@ \newlength{\@pc@length@tmp@width@vstack} -\newcommand{\@pc@beginnewline}{\@pc@and\@pseudocodelinenumber\@pc@modebegin} +\newcommand{\@pc@beginnewline}{% +\@pc@and\@pseudocodelinenumber% +%checkspace +\ifthenelse{\equal{\@pseudocodespace}{auto}}% +{\expandafter\pcind\expandafter[\value{@pc@indentationlevel}]}% +{}% +%beginmode +\@pc@modebegin} \newcommand{\@pc@and}{&} \newcommand{\@pc@and@wrap@end}{\@pc@modeend&} \newcommand{\@pc@and@wrap@start}{\@pc@beginnewline} @@ -501,8 +565,26 @@ \newcommand*\@pseudocodelnstart{0} \newcommand*\@pseudocodelnstartright{0} \newcommand*\@pseudocodesyntaxhighlighting{} -\newcommand*\@pseudocodekeywords{for ,foreach ,return ,{ do }, in ,new ,if ,null ,null,true ,true,until ,{ to },false ,false,{ then },repeat ,else ,done ,done,fi } +\newcommand*\@pseudocodenodraft{false} + +\newcommand*\@pseudocodecolsep{0em} +\newcommand*\@pseudocodeaddtolength{2pt} + +%%%%%%%%%%%%%% +% Define keywords for the automatic syntax highlighting +% the accompanying add provides additional keywords. +% The space version for automatic spacing +\newcommand*\@pseudocodekeywordsindent{for ,foreach ,if ,repeat ,while } +\newcommand*\@pseudocodekeywordsunindent{endfor,endforeach,fi,endif,until ,endwhile} +\newcommand*\@pseudocodekeywordsuninindent{else if,elseif, else} +\newcommand*\@pseudocodekeywords{return ,{ do }, in ,new ,null ,null,true ,true,{ to },false ,false,{ then },done ,done} \newcommand*\@pseudocodeaddkeywords{} +\newcommand*\@pseudocodealtkeywords{} +\begin{@withspaces} +\global\def\@pseudocodekeywordsspace{for,endfor,foreach,endforeach,return,do,in,new,if,null,true,until,to,false,then,repeat,else if,elseif,while,endwhile,else,done,fi,endif} +\end{@withspaces} + + \define@key{pseudocode}{head}[]{\renewcommand*\@pseudocodehead{#1}} \define@key{pseudocode}{width}[]{\renewcommand*\@pseudocodewidth{#1}} \define@key{pseudocode}{xshift}[]{\renewcommand*\@pseudocodexshift{#1}} @@ -511,6 +593,8 @@ \define@key{pseudocode}{afterskip}[]{\renewcommand*\@pseudocodeafterskip{#1}} \define@key{pseudocode}{lnstart}[0]{\renewcommand*\@pseudocodelnstart{#1}} \define@key{pseudocode}{lnstartright}[0]{\renewcommand*\@pseudocodelnstartright{#1}} +\define@key{pseudocode}{colsep}[0em]{\renewcommand*\@pseudocodecolsep{#1}} +\define@key{pseudocode}{addtolength}[2pt]{\renewcommand*\@pseudocodeaddtolength{#1}} \define@key{pseudocode}{mode}[math]{% \ifthenelse{\equal{#1}{text}}{% \renewcommand*\@pc@modebegin{\begin{varwidth}{\textwidth}% @@ -522,8 +606,13 @@ \renewcommand*\@pc@modeend{\end{varwidth}} }{}% } +\define@key{pseudocode}{nodraft}[true]{\renewcommand*\@pseudocodenodraft{#1}} \define@key{pseudocode}{keywords}[]{\renewcommand*\@pseudocodekeywords{#1}} +\define@key{pseudocode}{keywordsindent}[]{\renewcommand*\@pseudocodekeywordsindent{#1}} +\define@key{pseudocode}{keywordsunindent}[]{\renewcommand*\@pseudocodekeywordsunindent{#1}} +\define@key{pseudocode}{keywordsuninindent}[]{\renewcommand*\@pseudocodekeywordsuninindent{#1}} \define@key{pseudocode}{addkeywords}[]{\renewcommand*\@pseudocodeaddkeywords{#1}} +\define@key{pseudocode}{altkeywords}[]{\renewcommand*\@pseudocodealtkeywords{#1}} \define@key{pseudocode}{syntaxhighlight}[]{\renewcommand*\@pseudocodesyntaxhighlighting{#1}} \newcommand{\@pc@modebegin}{} @@ -532,35 +621,144 @@ \newcommand{\@pc@syntaxhighlight}[1]{% \ifthenelse{\equal{\@pseudocodesyntaxhighlighting}{auto}}{% -\saveexploremode\expandarg % suppress expansions made by xstring \def\@shtmp{#1}% first step -\edef\@tmpkeywords{\@pseudocodekeywords,\@pseudocodeaddkeywords}% +\ifthenelse{\equal{\@pseudocodespace}{keep}} + {\edef\@tmpkeywords{\@pseudocodekeywordsspace,\@pseudocodeaddkeywords}} + {\ifthenelse{\equal{\@pseudocodespace}{auto}} + {\edef\@tmpkeywords{\@pseudocodekeywords,\@pseudocodeaddkeywords}} + {\edef\@tmpkeywords{\@pseudocodekeywords,\@pseudocodekeywordsindent,\@pseudocodekeywordsunindent,\@pseudocodekeywordsuninindent,\@pseudocodeaddkeywords}}} \foreach \@pckw in \@tmpkeywords{% \ifthenelse{\equal{\@pckw}{}}{}{% -\StrSubstitute{\@pckw}{ }{PCXXMYBREAKSPACE}[\@pckwspace]% take care of spaces -\StrSubstitute{\@shtmp}{\@pckw}{\expandafter\@pc@highlight\expandafter{\@pckwspace}}[\@shtmpA]% -\global\let\@shtmp\@shtmpA% +% we are doing a simple strsub and storing the result (globally) in @shtmp +\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter + \gdef\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter + \@shtmp\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter + {\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter + \@pc@stringsubstitution\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter + {\expandafter\expandafter\expandafter\@shtmp\expandafter\expandafter\expandafter + }\expandafter\expandafter\expandafter{\expandafter\@pckw\expandafter}\expandafter{\expandafter\@pc@highlight\expandafter{\@pckw}}}% +}% alt keywords +}% +\foreach \@pckw in \@pseudocodealtkeywords{% +\ifthenelse{\equal{\@pckw}{}}{}{% +% we are doing a simple strsub and storing the result (globally) in @shtmp +\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter + \gdef\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter + \@shtmp\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter + {\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter + \@pc@stringsubstitution\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter + {\expandafter\expandafter\expandafter\@shtmp\expandafter\expandafter\expandafter + }\expandafter\expandafter\expandafter{\expandafter\@pckw\expandafter}\expandafter{\expandafter\@pc@althighlight\expandafter{\@pckw}}}% +}% +}% +%%%% +% if automatic spacing +\ifthenelse{\equal{\@pseudocodespace}{auto}} +{% +\foreach \@pckw in \@pseudocodekeywordsindent{% indentation keywords +\ifthenelse{\equal{\@pckw}{}}{}{% +% we are doing a simple strsub and storing the result (globally) in @shtmp +\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter + \gdef\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter + \@shtmp\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter + {\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter + \@pc@stringsubstitution\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter + {\expandafter\expandafter\expandafter\@shtmp\expandafter\expandafter\expandafter + }\expandafter\expandafter\expandafter{\expandafter\@pckw\expandafter}\expandafter{\expandafter\@pc@highlightindent\expandafter{\@pckw}}}% }}% -\restoreexploremode\@shtmp% -}{#1}% +\foreach \@pckw in \@pseudocodekeywordsunindent{% unindentation keywords +\ifthenelse{\equal{\@pckw}{}}{}{% +% we are doing a simple strsub and storing the result (globally) in @shtmp +\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter + \gdef\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter + \@shtmp\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter + {\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter + \@pc@stringsubstitution\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter + {\expandafter\expandafter\expandafter\@shtmp\expandafter\expandafter\expandafter + }\expandafter\expandafter\expandafter{\expandafter\@pckw\expandafter}\expandafter{\expandafter\@pc@highlightunindent\expandafter{\@pckw}}}% +}}% +\foreach \@pckw in \@pseudocodekeywordsuninindent{% uninindentation keywords +\ifthenelse{\equal{\@pckw}{}}{}{% +% we are doing a simple strsub and storing the result (globally) in @shtmp +\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter + \gdef\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter + \@shtmp\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter + {\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter + \@pc@stringsubstitution\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter + {\expandafter\expandafter\expandafter\@shtmp\expandafter\expandafter\expandafter + }\expandafter\expandafter\expandafter{\expandafter\@pckw\expandafter}\expandafter{\expandafter\@pc@highlightuninindent\expandafter{\@pckw}}}% +}}% +}{}% +% return result +\@shtmp% +}{#1}% nothing to highlight } \newcommand{\@pc@highlight}[1]{% -\StrSubstitute{#1}{PCXXMYBREAKSPACE}{}[\@pctmpname]% -\StrSubstitute{#1}{PCXXMYBREAKSPACE}{ }[\@pctmporg]% -\StrSubstitute{#1}{PCXXMYBREAKSPACE}{~}[\@pctmpres]% -\ifcsname dohighlight\@pctmpname\endcsname% -\csname dohighlight\@pctmpname\endcsname{\@pctmporg}% -\else\pseudocodeconstant[]{\@pctmpres}\fi% +\ifthenelse{\equal{\@pseudocodespace}{keep}} + {\highlightkeyword[]{#1}}% + {\highlightkeyword[]{\@pc@spacesubstitution{#1}{ }{~}}}% +} + +\newcommand{\@pc@highlightindent}[1]{% +\@pc@increaseindent\@pc@highlight{#1}% +} + +\newcommand{\@pc@highlightunindent}[1]{% +\@pc@decreaseindent\@pc@highlight{#1}% +} + +\newcommand{\@pc@highlightuninindent}[1]{% +\@pc@tmpdecreaseindent\@pc@highlight{#1}% +} + +\newcommand{\@pc@althighlight}[1]{% +\ifthenelse{\equal{\@pseudocodespace}{keep}} + {\highlightaltkeyword{#1}}% + {\highlightaltkeyword{\@pc@spacesubstitution{#1}{ }{~}}}% +} + +%%%%%%%%%%%%%%%%% +% Allow for spacing +\newcommand{\@withinspaces}{false}% +\newcommand{\@keepspaces}{% +\renewcommand{\@withinspaces}{true}\@withspaces% } +\newcommand{\@pc@endgroupafterpc}{} + +\newcommand*\@pseudocodespace{} +\define@key{pcspace}{space}[]{\ifthenelse{\equal{#1}{keep}}{\@keepspaces}{}\renewcommand*\@pseudocodespace{#1}} + +%%% automatic indentation +\newcounter{@pc@indentationlevel} +\newcommand{\@pc@increaseindent}{\addtocounter{@pc@indentationlevel}{1}} +\newcommand{\@pc@decreaseindent}{\ifthenelse{\equal{\@pseudocodespace}{auto}}{\pcind[-1]}{}\addtocounter{@pc@indentationlevel}{-1}} +\newcommand{\@pc@tmpdecreaseindent}{\ifthenelse{\equal{\@pseudocodespace}{auto}}{\pcind[-1]}{}} % store original halign \let\@pc@halign\halign% +%% Check if the pseudocode command is called with an optional argument +\providecommand{\pseudocode}{% +\begingroup% +\renewcommand{\@withinspaces}{false}% +\@ifnextchar[%] + {\@pseudocodeA}% + {\@pseudocode[]}% +} + +\def\@pseudocodeA[#1]{% +\setkeys*{pcspace}{#1}%test if there is a space assignment within the keys .. make the necessary arrangements and call the actual method +\@pseudocode[#1]% +} -\providecommand{\pseudocode}[2][]{% +\def\@pseudocode[#1]#2{% \begingroup% -\setkeys{pseudocode}{#1}% +\setkeys{pseudocode}[space]{#1}%ignore the space key. +% check draft mode and disable syntax highlighting +\@pc@ifdraft{\ifthenelse{\equal{\@pseudocodenodraft}{true}}{}{\renewcommand\@pseudocodesyntaxhighlighting{}}}{}% +% +% \addtocounter{@pc@global@pc@nestcnt}{1}% % allow for tikz usage \@pc@ensureremember% @@ -578,7 +776,8 @@ \fi% % create indent command \expandafter\let\csname \pcindentname\endcsname\pcind% -% wrap content if necessary +% +%store and wrap (do syntax highlighting) argument \renewcommand{\@pc@thecontent}{\@pc@and@wrap@start\@pc@syntaxhighlight{#2}\@pc@and@wrap@end}% % %take care of counters @@ -603,7 +802,7 @@ }{}% % %align column separation -\renewcommand*{\minalignsep}{0em}% +\renewcommand*{\minalignsep}{\@pseudocodecolsep}% % % if no width is set compute width and store in circuitlength \ifthenelse{\equal{\@pseudocodewidth}{}}{% @@ -619,11 +818,12 @@ \ifdim\@pc@alt@minipage@length>\@pc@minipage@length% \setlength{\@pc@minipage@length}{\@pc@alt@minipage@length}% \fi% -\addtolength{\@pc@minipage@length}{5pt}% +\addtolength{\@pc@minipage@length}{\@pseudocodeaddtolength}% }{\addtolength{\@pc@minipage@length}{\@pseudocodewidth}}% % reset counter \setcounter{pclinenumber}{\value{@pclinenumbertmp}}% \setcounter{pcrlinenumber}{\value{@pcrlinenumbertmp}}% +\setcounter{@pc@indentationlevel}{0}% % begin actual output % % @@ -641,6 +841,9 @@ \@pc@releaseremember% \addtocounter{@pc@global@pc@nestcnt}{-1}% \endgroup% +% close spacing and potentially a single group generated by the space tester +\ifthenelse{\equal{\@withinspaces}{true}}{\end@withspaces}{}% +\endgroup% } @@ -650,9 +853,11 @@ \ifthenelse{\equal{#1}{}}{\vspace{-0.8\baselineskip}\pcsize}{% \tikz{\gdef\i{\thepcgamecounter}% \node[anchor=base] (gamenode\i) {\hspace{-2pt}#1\vphantom{$\sum^A_{A_b}$}}; -\ifbxgame\node[draw,anchor=base, above=0.1cm of gamenode\i] (bgamenode\i) {\@bxgameheader\vphantom{$\sum^A_{A_b}$}};\fi % +\ifthenelse{\equal{\@withinbxgame}{true}} + {\node[draw,anchor=base, above=0.1cm of gamenode\i] (bgamenode\i) {\@bxgameheader\vphantom{$\sum^A_{A_b}$}};} + {}% }\vspace{-0.1\baselineskip}\hrule\small\vspace{-0.5\baselineskip}}% -\begin{flalign*} #2 \end{flalign*} +\begin{flalign*}#2\end{flalign*}% } @@ -660,7 +865,7 @@ \begingroup% \ifthenelse{\equal{#1}{}}{}{#1\vphantom{$\sum^A_{A_b}$}\hrule \vspace{0.2\baselineskip}}% \pcsubsize% -$\begin{aligned} #2 \end{aligned}$% +$\begin{aligned}#2\end{aligned}$% \endgroup% } @@ -815,7 +1020,16 @@ $\begin{aligned} #2 \end{aligned}$% \ifthenelse{\value{@bb@oracle@cnt}=1}{ \node[below right=0cm and \bbroracledistance of \@bb@lastbox.north east] (\bbroraclenodenameprefix#1) \bgroup }{ - \node[below=0.05cm of \bbroraclenodenameprefix \@bb@lastoracle.south west, anchor=north west] (\bbroraclenodenameprefix#1) \bgroup +% \node[below0.05cm of \bbroraclenodenameprefix \@bb@lastoracle.south west, anchor=north west] (\bbroraclenodenameprefix#1) \bgroup +% compute distance of top of last box to bottom of last oracle + \coordinate (@bbtmpcoord) at (\@bb@lastbox.north east); + \path (@bbtmpcoord); + \pgfgetlastxy{\XCoord}{\YCoordA} + \coordinate (@bbtmpcoord) at (\bbroraclenodenameprefix \@bb@lastoracle.south west); + \path (@bbtmpcoord); + \pgfgetlastxy{\XCoord}{\YCoordB} + \setlength{\@bb@tmplength@b}{\YCoordA-\YCoordB} + \node[below right=\@bb@tmplength@b and \bbroracledistance of \@bb@lastbox.north east] (\bbroraclenodenameprefix#1) \bgroup } \global\def\@bb@lastoracle{#1} \begin{bbrenv}{#1} @@ -1106,21 +1320,46 @@ $\begin{aligned} #2 \end{aligned}$% \addtocounter{@pcsubprogstep}{-1}} +%%%%% +% parameter reordering +\def\@pseudocodeB#1#2[#3]#4{\setkeys*{pcspace}{#2,#3}\@pseudocode[head={#1#4},#2,#3]} +\def\@pseudocodeC#1#2#3{\setkeys*{pcspace}{#2}\@pseudocode[head={#1#3},#2]} +%for no headers +\def\@pseudocodeE#1#2[#3]{\setkeys*{pcspace}{#2,#3}\@pseudocode[head={#1},#2,#3]} +\def\@pseudocodeF#1#2{\setkeys*{pcspace}{#2}\@pseudocode[head={#1},#2]} + +%%%%%%%%% +% Define pseudocode command: +% #1 name +% #2 code to execute after begingroup +% #3 head prefix +% #4 other config +\newcommand{\createprocedurecommand}[4]{ + \expandafter\gdef\csname #1\endcsname{% +\begingroup% +\renewcommand{\@withinspaces}{false}% +#2% +\@ifnextchar[%] + {\@pseudocodeB{#3}{#4}} + {\@pseudocodeC{#3}{#4}}% +}% +} -%%% some basic procedure commands -\newcommand{\pcmainname}{\textsc{Main }} -\newcommand{\pcalgorithmname}{\textsc{Algo. }} -\newcommand{\pccircuitname}{\textsc{Circ. }} -\newcommand{\pcprogramname}{\textsc{Prog. }} -\newcommand{\pcprotocolname}{\textsc{Protocol }} +\newcommand{\createpseudocodecommand}[4]{ + \expandafter\gdef\csname #1\endcsname{% +\begingroup% +\renewcommand{\@withinspaces}{false}% +#2% +\@ifnextchar[%] + {\@pseudocodeE{#3}{#4}} + {\@pseudocodeF{#3}{#4}}% +}% +} -\providecommand{\procedure}[3][]{\pseudocode[head={#2},#1]{#3}} -\providecommand{\mainprocedure}[3][]{\pseudocode[head={\pcmainname#2},#1]{#3}} -\providecommand{\algorithm}[3][]{\pseudocode[head={\pcalgorithmname#2},#1]{#3}} -\providecommand{\circuit}[3][]{\pseudocode[head={\pccircuitname#2},#1]{#3}} -\providecommand{\program}[3][]{\pseudocode[head={\pcprogramname#2},#1]{#3}} -\providecommand{\protocol}[3][]{\pseudocode[head={\pcprotocolname#2},#1]{#3}} +%%%%%% +% create procedure +\createprocedurecommand{procedure}{}{}{} %%% % send message @@ -1267,28 +1506,41 @@ $\begin{aligned} #2 \end{aligned}$% \newenvironment{gameproof}[1][]{% \begingroup% \setkeys{pcgameproof}{#1} -\bxgamefalse% \@pc@ensureremember% \setcounter{pcgamecounter}{\@pcgameproofgamenr}% \setcounter{pcstartgamecounter}{\@pcgameproofgamenr}\stepcounter{pcstartgamecounter}% }{\@pc@releaseremember\endgroup} -\newcommand{\gameprocedure}[2][]{\stepcounter{pcgamecounter}% -\pseudocode[head=\ensuremath{\pcgamename_{\thepcgamecounter}\gameprocedurearg},#1]{#2}} +\createpseudocodecommand{gameprocedure} + {\stepcounter{pcgamecounter}} + {\ensuremath{\pcgamename_{\thepcgamecounter}\gameprocedurearg}} + {} + +\def\@bxgame@pseudocodeA[#1]#2#3{\setkeys*{pcspace}{#1}\renewcommand{\@bxgameheader}{$\pcgamename_{#2}$\gameprocedurearg}% +\@pseudocode[head=\ensuremath{\pcgamename_{\thepcgamecounter}\gameprocedurearg},#1]{#3}} +\def\@bxgame@pseudocodeB#1#2{\renewcommand{\@bxgameheader}{$\pcgamename_{#1}$\gameprocedurearg}% +\@pseudocode[head=\ensuremath{\pcgamename_{\thepcgamecounter}\gameprocedurearg}]{#2}} -\newcommand{\bxgameprocedure}[3][]{% -\renewcommand{\@bxgameheader}{$\pcgamename_{#2}$\gameprocedurearg}% -\bxgametrue% +\newcommand{\bxgameprocedure}{ +\begingroup% +\renewcommand{\@withinspaces}{false}% +\renewcommand{\@withinbxgame}{true}% \stepcounter{pcgamecounter}% -\pseudocode[head=\ensuremath{\pcgamename_{\thepcgamecounter}\gameprocedurearg},#1]{#3}% -\bxgamefalse} +\@ifnextchar[%] + {\@bxgame@pseudocodeA} + {\@bxgame@pseudocodeB}% +} -\newcounter{@pc@game@tmpcnt@a} -\newcommand{\tbxgameprocedure}[2][]{\addtocounter{pcgamecounter}{1}% -\setcounter{@pc@game@tmpcnt@a}{\value{pcgamecounter}+1} -\pseudocode[head={\ensuremath{\pcgamename_{\thepcgamecounter}\gameprocedurearg}% -\ \setlength{\fboxsep}{3pt}\protect\fbox{\ensuremath{\pcgamename_{\thepcgamecounter}\gameprocedurearg}}},#1]{#2}% -\addtocounter{pcgamecounter}{1}} +%tbx top boxed +\createpseudocodecommand{tbxgameprocedure} + {\addtocounter{pcgamecounter}{1}% +\xdef\@pcfirstgamename{\thepcgamecounter}% +\addtocounter{pcgamecounter}{1}% +\xdef\@pcsecgamename{\thepcgamecounter}% +} + {\ensuremath{\pcgamename_{\@pcfirstgamename}\gameprocedurearg}% +\ \setlength{\fboxsep}{3pt}\protect\fbox{\ensuremath{\pcgamename_{\@pcsecgamename}\gameprocedurearg}}} +{} \newcommand*\@pcgamehopnodestyle{} @@ -1365,30 +1617,36 @@ $\begin{aligned} #2 \end{aligned}$% %%%%%%%% % basic pseudocode constants -\newcommand{\pseudocodeconstant}[2][\ ]{\ensuremath{\mathbf{#2}}#1} - -\newcommand{\pcglobvar}{\pseudocodeconstant{gbl}} -\newcommand{\pcnew}{\pseudocodeconstant{new}} -\newcommand{\pcwhile}{\pseudocodeconstant{while}} -\newcommandx*{\pcdo}[2][1=\ ,2=]{#1\pseudocodeconstant[#2]{do}} -\newcommand{\pcif}{\pseudocodeconstant{if}} -\newcommand{\pcelse}{\pseudocodeconstant{else}} -\newcommand{\pcfi}{\pseudocodeconstant{fi}} -\newcommandx*{\pcthen}[2][1=\ ,2=]{#1\pseudocodeconstant[#2]{then}} -\newcommand{\pcreturn}{\pseudocodeconstant{return}} -\newcommandx*{\pcin}[2][1=\ ,2=]{#1\pseudocodeconstant[#2]{in}} -\newcommand{\pcfor}{\pseudocodeconstant{for}} -\newcommand{\pcrepeat}[1]{\ensuremath{\pseudocodeconstant{repeat} #1\ \pseudocodeconstant{times}}} -\newcommand{\pcrepeatuntil}[2]{\ensuremath{\pseudocodeconstant{repeat}\ #1\ \pseudocodeconstant{until}\ #2}} -\newcommand{\pcforeach}{\pseudocodeconstant{for each}} -\newcommand{\pcuntil}{\pseudocodeconstant{until}} -\newcommand{\pccontinue}{\pseudocodeconstant{continue}} -\newcommand{\pcfalse}{\pseudocodeconstant{false}} -\newcommand{\pctrue}{\pseudocodeconstant{true}} -\newcommand{\pcnull}{\pseudocodeconstant{null}} +\newcommand{\highlightkeyword}[2][\ ]{\ensuremath{\mathbf{#2}}#1} +\newcommand{\highlightaltkeyword}[1]{\ensuremath{\mathsf{#1}}} + +\newcommand{\pcglobvar}{\highlightkeyword{gbl}} +\newcommand{\pcnew}{\highlightkeyword{new}} +\newcommand{\pcwhile}{\@pc@increaseindent\highlightkeyword{while}} +\newcommand{\pcendwhile}{\@pc@decreaseindent\highlightkeyword{endwhile}} +\newcommandx*{\pcdo}[2][1=\ ,2=]{#1\highlightkeyword[#2]{do}} +\newcommand{\pcif}{\@pc@increaseindent\highlightkeyword{if}} +\newcommand{\pcelse}{\@pc@tmpdecreaseindent\highlightkeyword{else}} +\newcommand{\pcelseif}{\@pc@tmpdecreaseindent\highlightkeyword{else if}} +\newcommand{\pcfi}{\@pc@decreaseindent\highlightkeyword{fi}} +\newcommand{\pcendif}{\@pc@decreaseindent\highlightkeyword{endif}} +\newcommand{\pcendfor}{\@pc@decreaseindent\highlightkeyword{endfor}} +\newcommandx*{\pcthen}[2][1=\ ,2=]{#1\highlightkeyword[#2]{then}} +\newcommand{\pcreturn}{\highlightkeyword{return}} +\newcommandx*{\pcin}[2][1=\ ,2=]{#1\highlightkeyword[#2]{in}} +\newcommand{\pcfor}{\@pc@increaseindent\highlightkeyword{for}} +\newcommand{\pcrepeat}[1]{\@pc@increaseindent\ensuremath{\highlightkeyword{repeat} #1\ \highlightkeyword{times}}} +\newcommand{\pcrepeatuntil}[2]{\ensuremath{\highlightkeyword{repeat}\ #1\ \highlightkeyword{until}\ #2}} +\newcommand{\pcforeach}{\@pc@increaseindent\highlightkeyword{foreach}} +\newcommand{\pcendforeach}{\@pc@decreaseindent\highlightkeyword{endforeach}} +\newcommand{\pcuntil}{\@pc@decreaseindent\highlightkeyword{until}} +\newcommand{\pccontinue}{\highlightkeyword{continue}} +\newcommand{\pcfalse}{\highlightkeyword{false}} +\newcommand{\pctrue}{\highlightkeyword{true}} +\newcommand{\pcnull}{\highlightkeyword{null}} \newcommand{\pccomment}[1]{{\mbox{/\!\!/ } \text{\scriptsize#1}}} -\newcommand{\pcdone}{\pseudocodeconstant{done}} -\newcommand{\pcparse}{\pseudocodeconstant{parse}} +\newcommand{\pcdone}{\highlightkeyword{done}} +\newcommand{\pcparse}{\highlightkeyword{parse}} %%% % highlighting |