%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The Ascon encryption mode
%
% public domain (CC0 1.0 https://creativecommons.org/publicdomain/zero/1.0/)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\documentclass[11pt]{standalone}

\newif\ifsans
\newif\iftext
\newif\ifdetails

%%% CONFIGURATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% preferably use build.py

%\sanstrue  % for sans-serif fonts (slides, web)
\sansfalse  % for serif fonts (article)

\texttrue  % include phase description
%\textfalse  % no phase description

\detailsfalse % simplify (eg, 'const' instead of 'kab0'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\ifsans
\renewcommand*\familydefault{\sfdefault}
\usepackage{sfmath}
\fi

\usepackage{tikz}

\tikzset{sparsam/.style={inner sep=1pt}}
\tikzset{bitwidth/.style={above=-1pt, font=\tiny}}
\tikzset{next/.style={->, >=latex}}

\begin{document}
\begin{tikzpicture}[thick]
  \newcommand{\conc}{\ensuremath{\Vert}}
  \newcommand{\perm}[1]{ node[rectangle, rounded corners=3pt, minimum width=.5cm, minimum height=1.8cm, draw, sparsam] {$p^{#1}$}}
  \ifsans
    \newcommand{\xor}[1]{ node[circle, inner sep=-1.3pt, name={#1}] {\tikz{\draw[] (0,0) circle (3.75pt) +(3.75pt,0) -- +(-3.75pt,0) +(0,3.75pt) -- +(0,-3.75pt);}}}
  \else
    \newcommand{\xor}[1]{ node[circle, inner sep=-1.3pt, name={#1}] {$\oplus$}}
  \fi
  \newcommand{\bitwidth}{\tikz{\draw[-] (-2pt,-2pt) -- (2pt, 2pt);}}
  \newcommand{\rate}{.5cm}
  \newcommand{\msg}{.6cm}
  \newcommand{\phase}{1.7cm}
  \newcommand{\minnext}{.4cm}

  % --- init up to p^a ---
  \begin{scope}[xshift=0cm]
    \ifdetails
      \draw (0,\rate) node[left, sparsam] {$k \conc r \conc a \conc b \conc 0^*$};
      \draw (0,-\rate) node[left, sparsam] {$K \conc N$};

      \draw[next] (0,\rate) -- node {\bitwidth} node[bitwidth] {$r$} (.7,\rate);
      \draw[next] (0,-\rate) -- node {\bitwidth} node[bitwidth] {$c$} (.7,-\rate);
    \else
      \draw (0,0) node[left, sparsam] {$\mathrm{IV} \conc K \conc N$};
      \draw[next] (0,0) -- node {\bitwidth} node[bitwidth] {$320$} (.7,0);
    \fi

    \draw (.95,0) \perm{a};
  \end{scope}

  % --- init after p^a and auth A1 ---
  \begin{scope}[xshift=1.2cm]
    \draw (.4,-\rate) \xor{Ki};
    \draw[next] (0,-\rate) -- (Ki);
    \draw[next] (Ki) +(0,-\msg) node[below] {$0^* \conc K$ \hspace*{.3cm}} -- (Ki);
    \draw[next] (Ki) -- node[pos=0.6] {\bitwidth} node [pos=0.6, bitwidth] {$c$} +(1.3,0);

    \draw[dashdotted] (.8,1.5) -- (.8,-1.5);

    \draw (1.3,\rate) \xor{A1};
    \draw[next] (0,\rate) -- node[near start] {\bitwidth} node[near start, bitwidth] {$r$} (A1);
    \draw[next] (A1) +(0,\msg) node[above] {$A_1$} -- (A1);
    \draw[next] (A1) -- +(\minnext,0);

    \draw (1.95,0) \perm{b};
  \end{scope}

  % --- auth As ---
  \begin{scope}[xshift=3.4cm]
    \draw[dotted] (0,\rate) -- (\minnext,\rate)
                  (0,-\rate) -- (\minnext,-\rate);

    \draw (.9,\rate) \xor{As};
    \draw[next] (\minnext,\rate) -- (As);
    \draw[next] (As) +(0,\msg) node[above] {$A_s$} -- (As);
    \draw[next] (As) -- +(\minnext,0);

    \draw[next] (\minnext,-\rate) -- node {\bitwidth} node[bitwidth] {$c$} (1.3,-\rate);

    \draw (1.55,0) \perm{b};
  \end{scope}

  % --- enc P1 ---
  \begin{scope}[xshift=5.2cm]
    \draw (.4,-\rate) \xor{AuthPad};
    \draw[next] (0,-\rate) -- (AuthPad);
    \draw[next] (AuthPad) +(0,-\msg) node[below] {$0^* \conc 1$ \hspace*{.3cm}} -- (AuthPad);
    \draw[next] (AuthPad) -- node {\bitwidth} node [bitwidth] {$c$} +(1.7,0);

    \draw[dashdotted] (.8,1.5) -- (.8,-1.5);

    \draw (1.3,\rate) \xor{P1};
    \draw[next] (0,\rate) -- node[near start] {\bitwidth} node[near start, bitwidth] {$r$} (P1);
    \draw[next] (P1) +(0,\msg) node[above] {$P_1$} -- (P1);
    \draw[next] (P1) -- +(2*\minnext,0);
    \draw[next] (P1) ++(.5,0) -- +(0,\msg) node[above] {$C_1$};

    \draw (2.35,0) \perm{b};
  \end{scope}

  % --- enc Pt-1 ---
  \begin{scope}[xshift=7.8cm]
    \draw[dotted] (0,\rate) -- (\minnext,\rate)
                  (0,-\rate) -- (\minnext,-\rate);

    \draw[next] (\minnext,-\rate) -- node[pos=.4] {\bitwidth} node [pos=.4,bitwidth] {$c$} +(1.3,0);

    \draw (.9,\rate) \xor{Pt1};
    \draw[next] (\minnext,\rate) -- (Pt1);
    \draw[next] (Pt1) +(0,\msg) node[above] {$P_{t\!-\!1}$ \hspace*{.15cm}} -- (Pt1);
    \draw[next] (Pt1) -- +(.8,0);
    \draw[next] (Pt1) ++(.5,0) -- +(0,\msg) node[above] {\hspace*{.2cm} $C_{t\!-\!1}$};

    \draw (1.95,0) \perm{b};
  \end{scope}

  % --- enc Pt and finalize ---
  \begin{scope}[xshift=10.0cm]
    \draw (.5,\rate) \xor{Pt};
    \draw[next] (0,\rate) -- (Pt);
    \draw[next] (Pt) +(0,\msg) node[above] {$P_t$} -- (Pt);
    \draw[next] (Pt) ++(.5,0) -- +(0,\msg) node[above] {$C_t$};
    \draw[next] (Pt) -- node[pos=.7] {\bitwidth} node[pos=.7,bitwidth] {$r$} +(1.6,0);

    \draw[dashdotted] (1.3,1.5) -- (1.3,-1.5);

    \draw (1.7,-\rate) \xor{Kf};
    \draw[next] (Kf) +(0,-\msg) node[below] {\hspace*{.3cm} $K \conc 0^*$} -- (Kf);
    \draw[next] (0,-\rate) -- node[pos=.3] {\bitwidth} node[pos=.3,bitwidth] {$c$} (Kf);
    \draw[next] (Kf) -- (2.1,-\rate);

    \draw (2.35,0) \perm{a};

    \draw (3.3,-\rate) \xor{Kt};
    \draw[next] (Kt) +(0,-\msg) node[below] {$K$} -- (Kt);
    \draw[next] (2.6,-\rate) -- node[pos=.4] {\bitwidth} node[pos=.4,bitwidth] {$k$} (Kt);
    \draw (4.0,-\rate) node[name=T,sparsam] {$T$};
    \draw[next] (Kt) -- (T);
  \end{scope}

  % --- phase descriptions ---
  \iftext
    \draw (.5,-\rate-\phase) node {Initialization};
    \draw (4.0,-\rate-\phase) node {Associated Data};
    \draw (8.5,-\rate-\phase) node {Plaintext};
    \draw (12.8,-\rate-\phase) node {Finalization};
  \fi
\end{tikzpicture}
\end{document}
