# Using MathJax / $\TeX$ on the Cryptography site

Shortcut: see the community wiki answer below.

## Original question

I need both a reference and examples relevant to crypto for the TeX dialect we can use on crypto.stackexchange.com. I know there is tex.stackexchange.com but they do not even have the TeX extension enabled in their own site, nor have a tag for TeX-as-in-stackexchange, and I do not want to ask dumb questions.

Beside trial-and-error, plus reverse-engineering of other posts using "Show Math As TeX Commands", my lifebuoy is this TeX reference card (which is only very often online). But none of this tells me:

• how to make $$SHA-256(m)$$ good-looking;
• an easy reminder that integers are $$\mathbb N$$;
• if the product of integers a and b should be $$a \cdot b$$ or something else (somewhat I dislike $$ab$$).

Meta is now appropriate, since TeX is enabled, as apparent above.

Update: Paŭlo Ebermann linked to that sandbox with relevant examples. Mathurl is a nice sandbox with a few reminders, but lacks elaborate canned examples; at least now I can write $$\mathtt{SHA­1}(m)$$, but still that pesky $$-$$ resists. Also, an issue is that entering foo in mathurl shows what $$foo$$ gives on crypto.stackexchange.com, not what $foo$ gives.

• It looks like it is enabled on Math Stack Exchange meta, and they have a sandbox question there. If wanted, I could ask if we can enable this on crypto meta, too, for crypto-specific stuff. May 26, 2012 at 10:50
• The most complete MathJax Guide is on QuantumComputing.SE - In addition to the numerous examples offered at the bottom of the answer are a few excellent offsite links.
– Rob
Mar 14, 2019 at 2:49

MathJax is used to format mathematical expressions and symbols on cryptography site. You can use expressions within single dollar signs for inline math: $0101_2$ which gives and $$0101_2$$. Use double dollar signs for a centered block instead: $$\mathtt{CAFE}_{16}$$ gives

$$\mathtt{CAFE}_{16}$$

In the remainder of the text the dollar signs are not included to allow easy copying.

Braces / curly brackets {} are used to group expressions / symbols together within $$\mathrm\TeX$$. You need to use the backslash \ escape character to use one within an expression: \{\} gives you $$\{\}$$. Parentheses () and (square) brackets can be used as-is, for instance in a range $$[0, 10)$$ is just [0, 10).

The double backslash: \\ can be used to include a line break into a longer expression that is between double dollar signs.

The following sections will show common contents of math blocks. There are however other ways of finding out how to use $$\mathrm\TeX$$:

## Identifiers

We generally use $$K$$ for secret keys, $$P$$ for public keys and $$S$$ for private keys (private keys are also kept secret, but they are not shared).

The letter $$N$$ is generally used for the modulus while $$n$$ is used for the block size. $$k$$ is the key size.

Greek symbols are referenced by name, prefixed with a slash, for instance \alpha for $$\alpha$$ or \phi for $$\phi$$. Start with a capital to have the larger variant if available: \Phi for $$\Phi$$.

Multi-letter identifiers are written with \mathit; otherwise the letters are spaced as if for multiplication. Compare:

• E_k(message) $$E_k(message)$$
• E_k(\mathit{message}) $$E_k(\mathit{message})$$

there should be more whitespace in the first version (this might be more or less pronounced on your screen, and also depends on the characters within the identifier).

Identifiers in roman face are written with \mathrm. For example: k_{\mathrm{auth}}, k_{\mathrm{enc}} gives $$k_{\mathrm{auth}}, k_{\mathrm{enc}}$$.

## Undefined functions

Most cryptographic functions are undefined in the $$\mathrm\TeX$$ packages; it is however important to make sure they are not confused with identifiers.

Use \operatorname for well-known named functions like SHA-256, which should be set in roman face: \operatorname{SHA-256}(x) gives $$\operatorname{SHA-256}(x)$$, \operatorname{GF}(2^{128}) gives $$\operatorname{GF}(2^{128})$$. This ensures the spacing is correct for a named function with or without parentheses: $$\operatorname{Log}(x)$$, $$\operatorname{Log} x$$; compare $$\text{Log} x$$ using \text. If you use a well-known named function Foo many times, you can define \Foo with DeclareMathOperator{\Foo}{Foo}.

Big-O notation is alternatively written in italic, roman, or calligraphic face, according to the author's taste, or lack thereof: O(n) gives $$O(n)$$, \operatorname O(n) gives the unnecessarily uptight $$\operatorname O(n)$$, \mathcal O(n) gives the floofy fancy $$\mathcal O(n)$$. The related notation $$\tilde O(f(n))$$ means $$O(f(n) \log^k n)$$ for some $$k$$.

For a function definition like $$F\colon \Bbb R \to \Bbb Z_n, \quad x \mapsto {\lfloor x\rfloor}^2\bmod n$$
one can use F\colon \Bbb R \to \Bbb Z_n, \quad x \mapsto {\lfloor x\rfloor}^2\bmod n.

## Predefined functions

There are some predefined functions that are useful for cryptography.

• \Pr(X) for probability of X happening $$\Pr(X)$$
• \log_2(x) gives you logarithm base two: $$\log_2(x)$$
• others are \gcd, \exp, \ln and so on; for those missing (no \lcm), see above.

## Sub- and superscript

• just A gives you $$A$$
• P_A gives you $$P_A$$
• P^2 gives you $$P^2$$
• combined: P_A^2 gives $$P_A^2$$ while {P_A}^2 gives $${P_A}^2$$ and P_{A^2} gives $$P_{A^2}$$
• P' gives you $$P'$$, and P_A'' gives you $$P_A''$$
• P', \tilde P, \hat P can be used for variations on a variable: $$P'$$, $$\tilde P$$, $$\hat P$$
• \widetilde{P + Q}, \widehat{P + Q} for tildes and hats on entire expressions: $$\widetilde{P + Q}$$, $$\widehat{P + Q}$$
• \overline{S \cup T} for complements or conjugates: $$\overline{S \cup T}$$, $$\overline{z + w}$$

## Bit-ops

Bit operations can be shown like this:

• bitwise XOR: x \oplus y gives you $$x \oplus y$$
• bitwise AND: x \wedge y gives you $$x \wedge y$$
alternatively, x \mathbin\& y gives you $$x \mathbin\& y$$ (note the backslash escape)
• bitwise OR: x \vee y gives $$x \vee y$$
alternatively, x \mathbin| y gives you $$x \mathbin| y$$
• negation: \neg x gives you $$\neg x$$
• left shift: use x \ll n for $$x \ll n$$
• right shift: use x \gg n for $$x \gg n$$
• left rotate: use x \lll n for $$x \lll n$$
• right rotate: use x \ggg n for $$x \ggg n$$

## Simple math

• addition: x + y gives you $$x + y$$
• subtraction: x - y gives you $$x - y$$
• multiplication: x \cdot y gives you $$x \cdot y$$
• division: x / y gives you $$x / y$$
• divisibility: x \mid y gives you $$x \mid y$$, x \nmid y gives you $$x \nmid y$$
• modulo:
• the binary operator on two integer operands:
• x \bmod n gives you $$x \bmod n$$
• the relation that two integers are congruent modulo a third:
• x \equiv y \pmod n gives you $$x \equiv y \pmod n$$
• x \equiv y \mod n gives you $$x \equiv y \mod n$$
• power: x ^ y gives you $$x ^ y$$
• square root: \sqrt{x} gives you $$\sqrt{x}$$
• $$n^{\mathit{th}}$$ root: \sqrt[n]{x} gives you $$\sqrt[n]{x}$$

For an equation holding modulo $$n$$, use x \equiv y \pmod n for $$x \equiv y \pmod n$$.

You may also want to use division with a divider line using \frac, for instance c \cdot \frac {x + y} z: $$c \cdot \frac {x + y} z$$

Floor and ceiling: \lfloor x \rceil gives $$\lfloor x \rceil$$. These can be expanded: \bigg\lceil \frac{x}{y} \bigg\rfloor gives $$\bigg\lceil \frac{x}{y} \bigg\rfloor,$$ and the sizing is automatic with \left\lfloor f(x)^d \right\rceil gives $$\left\lfloor f(x)^d \right\rceil$$.

To multiply vectors or sets it may be wise to chose A \times B over the \cdot notation giving you $$A \times B$$.

## Comparison

• x = y is equality $$x = y$$
• x \approx y is approximately equal $$x \approx y$$
• x \neq y is non-equality $$x \neq y$$
• x < y is less-than $$x < y$$ (no need to escape or use &lt; within dollar signs)
• x \le y is less-than-or-equal $$x \le y$$
• x \ge y is greater-than-or-equal $$x \ge y$$
• x \equiv y is equivalence $$x \equiv y$$
• X \implies Y is used to show implication: $$X \implies Y$$ , sometimes X \to Y for $$X \to Y$$ is used instead
• X \iff Y is used to show equivalence: $$X \iff Y$$

It is possible to prefix anything with \not to negate the next symbol, e.g. \not\le is $$\not\le$$

## Sets, groups

• \{ 0, 1 \} gives the two-elements set $$\{ 0, 1 \}$$
• \emptyset gives the empty set $$\emptyset$$
• A \cup B and A \cap B give $$A \cup B$$ (union) and $$A \cap B$$ (intersection)
• a \in A gives $$a \in A$$, while b \not\in A gives $$b \not\in A$$
• A \subset B or A \subseteq B give $$A \subset B$$ or $$A \subseteq B$$
• Standard sets, usually written in blackboard bold with \mathbb or \Bbb:
• $$\Bbb N$$ \Bbb N is some set of natural numbers with ambiguity over whether zero is included or not, so avoid this notation
• $$\Bbb N^0, \Bbb N_0$$ \Bbb N^0, \Bbb N_0 is the set of natural numbers starting at zero
• $$\Bbb N^1, \Bbb N_1, \Bbb N^+$$ \Bbb N^1, \Bbb N_1, \Bbb N^+ is the set of natural numbers starting at one
• $$\Bbb Z$$ \Bbb Z is the ring of integers (from German Zahl)
• $$\Bbb Z^-, \Bbb Z^+, \Bbb Z \setminus \{0\}$$ \Bbb Z^-, \Bbb Z^+, \Bbb Z \setminus \{0\} are the sets of negative, positive, and nonzero integers
• $$\Bbb Q$$ \Bbb Q is the field of rational numbers; similarly, $$\Bbb Q^-, \Bbb Q^+, \Bbb Q \setminus \{0\}$$ \Bbb Q^-, \Bbb Q^+, \Bbb Q \setminus \{0\} for negative, positive, and nonzero
• $$\Bbb R$$ \Bbb R is the field of real numbers; similarly, $$\Bbb R^-, \Bbb R^+, \Bbb R \setminus \{0\}$$ \Bbb R^-, \Bbb R^+, \Bbb R \setminus \{0\} for negative, positive, and nonzero
• $$\Bbb C$$ \Bbb C is the field of complex numbers
• the ring of integers modplo $$p$$ is alternately written $$\Bbb Z/p\Bbb Z$$\Bbb Z/p\Bbb z or $$\Bbb Z_p$$ \Bbb Z_p, but beware $$\Bbb Z_p$$ also means the $$p$$-adic integers
• the multiplicative group of integers modulo $$n$$ is alternately written $$(\Bbb Z/n\Bbb Z)^\times$$(\Bbb Z/n\Bbb Z)^\times, $$(\Bbb Z/n\Bbb Z)^*$$ (\Bbb Z/n\Bbb Z)^*, $$\Bbb Z_n^*$$ (\Bbb Z/n\Bbb Z)^*, etc.
• $$\operatorname{GF}(p^n), \Bbb F_{p^n}$$ \operatorname{GF}(p^n), \Bbb F_{p^n} is the finite field of characteristic $$p$$ with $$p^n$$ elements

## Others

• M \mathbin \| N is concatenation of strings: $$M\mathbin\|N$$
• To indicate failure the \bot (bottom) sign is often used: $$\bot$$
• x_1, x_2, \ldots or x_1 + x_2 + \cdots gives you something that hasn't been finished: $$x_1, x_2, \ldots$$ or $$x_1 + x_2 + \cdots$$
• \square is used for Q.E.D. $$\square$$

## Displayed formulas

If $$y^2 \equiv x^3 + 486662 x^2 + x \pmod{2^{255} - 19}$$ is too unwieldy inline, use $$y^2 \equiv x^3 + 486662 x^2 + x \pmod{2^{255} - 19}$$ to set it in a display: $$y^2 \equiv x^3 + 486662 x^2 + x \pmod{2^{255} - 19}.$$ (Punctuation at the end goes before the closing  so it stays in the display.)

Use \begin{align} ... \end{align} for multiple equations aligned at relations marked with &, separated by \\. Write comments with \text.

\begin{align}
\Pr[E] &= \Pr[E \mid A] \Pr[A] \\
&\quad + \Pr[E \mid B] \Pr[B] \\
&\quad + \Pr[E \mid C] \Pr[C], \\
1 &= \Pr[A] + \Pr[B] + \Pr[C]. &&\text{($A$, $B$, and $C$ are exhaustive)}
\end{align}


\begin{align} \Pr[E] &= \Pr[E \mid A] \Pr[A] \\ &\quad + \Pr[E \mid B] \Pr[B] \\ &\quad + \Pr[E \mid C] \Pr[C], \\ 1 &= \Pr[A] + \Pr[B] + \Pr[C]. &&\text{(A, B, and C are exhaustive)} \end{align}

Use \begin{gather} ... \end{gather} for multiple equations centered:

\begin{gather}
M\colon y^2 = x^3 + 486662 x^2 + x, \\
E\colon -x^2 + y^2 = 1 - \frac{121665}{121666} x^2 y^2.
\end{gather}


$$\begin{gather} M\colon y^2 = x^3 + 486662 x^2 + x, \\ E\colon -x^2 + y^2 = 1 - \frac{121665}{121666} x^2 y^2. \end{gather}$$

Use \begin{multline} ... \end{multline} for a multi-line equation:

\begin{multline}
\frac{U^{-\xi} - 1}{\xi}
= \frac{1}{\xi} (e^{-\xi \log U} - 1)
= \frac{1}{\xi} \biggl(-\xi \log U + \frac{\xi^2 \log^2 U}{2!} \\
- \frac{\xi^3 \log^3 U}{3!}
+ \frac{\xi^4 \log^4 U}{4!}
- \cdots\biggr)
\end{multline}


$$\begin{multline} \frac{U^{-\xi} - 1}{\xi} = \frac{1}{\xi} (e^{-\xi \log U} - 1) = \frac{1}{\xi} \biggl(-\xi \log U + \frac{\xi^2 \log^2 U}{2!} \\ - \frac{\xi^3 \log^3 U}{3!} + \frac{\xi^4 \log^4 U}{4!} - \cdots\biggr) \end{multline}$$

• OK for a Keep It Simple and Short policy, I tried to apply that in my latest edit. This comment is enough to state that $x=y\bmod n$ implies $0\le x<n$ (because \bmodis the remainder operator), when $x\equiv y\pmod n$ leaves $x$ unbounded (because the pair \equiv \pmod indicates equivalence modulo); and to note the ambiguity on that point of $x=y\pmod n$ and $x\equiv y\mod n$ and $x=y\mod n$, which thus should be avoided at least when the difference matters.
– fgrieu Mod
Jul 13, 2018 at 16:20
• Math.SE is often the starting point for discussions around MathJax within SE sites (and part of experiments such as beta releases). There's also math.meta.stackexchange.com/questions/5020/…. However, TeX.SE considers MathJax questions off topic. Nov 22, 2018 at 17:54