Skip to content

Instantly share code, notes, and snippets.

@jackdoerner
Last active January 13, 2021 21:02
Show Gist options
  • Save jackdoerner/115e262f9f9f840e05d8ff896ccba234 to your computer and use it in GitHub Desktop.
Save jackdoerner/115e262f9f9f840e05d8ff896ccba234 to your computer and use it in GitHub Desktop.
A short snippet of LaTeX for typesetting hybrid proofs in crypto papers
% hybrids.tex
% A short snippet of LaTeX for typesetting hybrid proofs in crypto papers
% Copyright (c) 2021 Jack Doerner
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to deal
% in the Software without restriction, including without limitation the rights
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
% copies of the Software, and to permit persons to whom the Software is
% furnished to do so, subject to the following conditions:
% The above copyright notice and this permission notice shall be included in
% all copies or substantial portions of the Software.
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
% THE SOFTWARE.
\usepackage{hyperref, amsthm, xspace}
\newcounter{realfrsthybrid}
\newcounter{realcurrhybrid}
\setcounter{realcurrhybrid}{0}
\newcounter{realprevhybrid}
\setcounter{realprevhybrid}{-1}
\newcounter{currhybrid}
\newcounter{prevhybrid}
\newcommand{\inithybrids}[1]{\stepcounter{realprevhybrid}\refstepcounter{realcurrhybrid}\setcounter{realfrsthybrid}{\arabic{realcurrhybrid}}\setcounter{prevhybrid}{#1}\addtocounter{prevhybrid}{-1}\setcounter{currhybrid}{#1}}
\newcommand{\stephybrids}{\stepcounter{realprevhybrid}\refstepcounter{realcurrhybrid}\stepcounter{prevhybrid}\stepcounter{currhybrid}\ifnum\value{currhybrid}=0{\setcounter{realfrsthybrid}{\arabic{realcurrhybrid}}}\fi}
\newcommand{\linkthishybrid}{\phantomsection\label{hybauto:\arabic{realcurrhybrid}}\thishybrid}
\newcommand{\hyb}{\ensuremath{\mathcal{H}}\xspace}
\newcommand{\thishybrid}{\ensuremath{\hyperref[hybauto:\arabic{realcurrhybrid}]{\hyb_{\arabic{currhybrid}}}}\xspace}
\newcommand{\firsthybrid}{\ensuremath{\hyperref[hybauto:\arabic{realfrsthybrid}]{\hyb_0}}\xspace}
\newcommand{\prevhybrid}{\ensuremath{\hyperref[hybauto:\arabic{realprevhybrid}]{\hyb_{\arabic{prevhybrid}}}}\xspace}
\newtheoremstyle{hybrid}% name of the style to be used
{1em}% measure of space to leave above the theorem. E.g.: 3pt
{1em}% measure of space to leave below the theorem. E.g.: 3pt
{}% name of font to use in the body of the theorem
{0pt}% measure of space to indent
{\bfseries}% name of head font
{}% punctuation between head and body
{0pt}% space after theorem head; " " = normal interword space
{\thmname{#1}}
\theoremstyle{hybrid}
\newtheorem*{hybrid}{\stephybrids{}Hybrid $\hyperref[hybauto:\arabic{realcurrhybrid}]{\hyb_{\arabic{currhybrid}}.}$ \label{hybauto:\arabic{realcurrhybrid}}}
\documentclass{article}
\input{hybrids}
\title{A Little Bit of \LaTeX{} to Make\\Typesetting Hybrid Proofs Easier}
\author{Jack Doerner}
\date{}
\begin{document}
\maketitle
\inithybrids{0}
In order to begin a sequence of hybrids, we write \verb!\inithybrids{0}!, which sets the number of the first hybrid in the sequence to be 0. Once this is done, the macro \verb!\linkthishybrid! will render the name of the current hybrid and create a link to the spot where it is rendered. This is useful for defining the distribution of a hybrid mathematically:
\[
\linkthishybrid = \textrm{Some Distrubtion}
\]
Note that \verb!\linkthishybrid! can only be used \emph{once} per hybrid. We can reference this hybrid via \verb!\thishybrid!, which links back to the definition, like so: \thishybrid. When we're ready to move to the next hybrid, we call \verb!\stephybrids!, and then the next hybrid can be defined.
\stephybrids
\[
\linkthishybrid = \textrm{A Different Distribution}
\]
Now \verb!\thishybrid! renders and links to \thishybrid, and a second macro, \verb!\prevhybrid!, renders the name of \prevhybrid, which we defined before. This allows us to write that $\thishybrid \approx \prevhybrid$, for example. Often we will want to \emph{describe} a hybrid distribution, instead of defining it mathematically. For this, we have the \emph{hybrid environment}, which we can access via the paired delimeters \verb!\begin{hybrid}! and \verb!\end{hybrid}!. When we open the environment, the hybrid number is steped and a link is created automatically, which points to the spot where the description begins.
\begin{hybrid}\label{sequence_one_hybrid_three}
This is the description of \thishybrid. As before, we can reference the last hybrid, \prevhybrid. Furthermore, the macro \verb!\firsthybrid! allows us to talk about the first hybrid in the sequence, \firsthybrid.
\end{hybrid}
Even after the hybrid environment is closed, and the description of \thishybrid is finished, we can still use the macros to refer to \thishybrid, \prevhybrid, and \firsthybrid, which is useful if we would like to write some further analysis, for example.
\begin{hybrid}
One more hybrid for good measure. If everything has gone right, we should have \verb!\thishybrid \approx \firsthybrid!, which is to say $\thishybrid \approx \firsthybrid$.
\end{hybrid}
\eject
If we need multiple hybrid proofs in the same paper, we can reinitialize the sequence of hybrids. If we want to use a hybrid environment to describe the $0^\mathrm{th}$ hybrid, the we must type \verb!\inithybrids{-1}!.
\inithybrids{-1}
\begin{hybrid}
The first hybrid in our new sequence.
\end{hybrid}
\begin{hybrid}
The second hybrid in our new sequence.
\end{hybrid}
\begin{hybrid}
As before, we can reference the first (\firsthybrid), previous (\prevhybrid), and current (\thishybrid) hybrids, but now our macros link to the appropriate places in the current sequence, instead of the original sequence. If for some reason we wish to reference a particular hybrid in a different sequence, we can manually add a label to its body, and then reference that label here with a \verb!\hyperref!. For instance, we can link to the \hyperref[sequence_one_hybrid_three]{second hybrid of the first sequence}.
\end{hybrid}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment