A
Appendix

Math Review

A.1. Strings

Definition A.1.1 (Strings)

{0,1}n\bits^n denotes the set of nn-bit binary strings. X|X| denotes the length of the string XX, usually measured in bits.

When it is clear from context that we are talking about strings, 0n\bit 0^n means “the string of nn 0\bit 0's,” not the integer 0 to the nnth power. Similarly, 1n\bit 1^n refers to the string of nn 1\bit 1's.

Definition A.1.2 (XOR)

When XX and YY are strings of the same length, XYX \oplus Y denotes the bitwise exclusive-or (xor) of the two strings.

For example, 00110101=0110\bit{0011} \oplus \bit{0101} = \bit{0110}. The following algebraic facts about the xor operation are frequently useful:

XX=0n,X0n=X,XY=YX,(XY)Z=X(YZ).\begin{aligned} X \oplus X &= \bit{0}^n, \\ X \oplus \bit{0}^n &= X, \\ X \oplus Y &= Y \oplus X, \\ (X \oplus Y) \oplus Z &= X \oplus (Y \oplus Z). \end{aligned}

There are two ways to think about xor that can be helpful in different situations:

  • As bit-flipping: xor'ing a bit with 0\bit0 has no effect; xor'ing with 1\bit1 flips that bit. You can think of XYX \oplus Y as: “starting with XX, flip the bits at all the positions where YY has a 1\bit1.

  • As addition mod 2: xor is just addition mod 2 in every position. This interpretation of xor helps to justify its algebraic properties, like the fact that it is commutative and associative. Also, 1+11+1 is congruent to 0 mod 2, which explains why XX=0nX \oplus X = \bit 0^n.

Definition A.1.3 (Concatenation)

We write the result of concatenating two strings XX and YY as XY\hl{X \| Y}.

A.2. Probability

Definition A.2.1 (Probability distributions)

A (discrete) probability distribution over a set XX of outcomes is written as a function Pr\Pr that associates each outcome xXx \in X with a probability Pr[x]\PR{x}. We often say that the distribution assigns probability Pr[x]\PR{x} to outcome xx. For each outcome xXx \in X, the probability distribution must satisfy the condition 0Pr[x]10 \le \PR{x} \le 1. The sum of all probabilities xXPr[x]\sum_{x \in X} \PR{x} must also equal 1.

Definition A.2.2 (Uniform distribution)

The uniform distribution over a finite set XX assigns probability Pr[x]=1/X\PR{x} = 1/|X| to every xXx \in X.

The Pr\Pr notation is defined over individual outcomes, but we also extend it to apply to events, which are just collections of many outcomes. For example, when I roll a six-sided die, the event “I rolled an even number” corresponds to the events 2, 4, and 6. Formally, an event AA defines a subset of outcomes, and its probability is defined to be Pr[A]=xAPr[x]\PR{A} = \sum_{x\in A} \PR{x}.

Example A.2.3 (Rolling dice)

A six-sided die has faces numbered {1,2,,6}\{1, 2, \ldots, 6\}. When rolling such a die (at least in a math problem) the outcome is uniformly distributed. Thus,

Pr[3 is rolled]=1/6Pr[an odd number is rolled]=1/2Pr[a prime is rolled]=1/2\begin{aligned} \PR{\text{3 is rolled}} &= 1/6 \\ \PR{\text{an odd number is rolled}} &= 1/2 \\ \PR{\text{a prime is rolled}} &= 1/2\end{aligned}
Claim A.2.4

If AA and BB are events, then we write ABA \land B to denote the event that AA happens and BB happens. If AA and BB are independent events, then Pr[AB]=Pr[A]Pr[B]\PR{A \land B} = \PR{A} \cdot \PR{B}.

Upper Bounds

We often use probabilities to describe a certain “bad event” that violates security. In these cases, it is enough to show just an upper bound on the probability.

Claim A.2.5 (Union bound)

If AA and BB are events, then we write ABA \lor B to denote the event that AA happens or BB happens (or both). The union bound states that:

Pr[AB]Pr[A]+Pr[B]. \PR{A \lor B} \le \PR{A} + \PR{B}.

The sum Pr[A]+Pr[B]\PR{A}+\PR{B} is an upper bound because it might double-count the probability that both AA and BB happen. Importantly, events AA and BB don't have to be independent for the union bound to apply. We most commonly see the union bound show up in the following way:

Example A.2.6 (Union bound)

Suppose I toss a 100-sided die, ten times, and consider the probability that I observed a 1\bit1 in any of those rolls. We can use the union bound to derive a simple upper bound on the probability:

Pr[some roll resulted in 1]Pr[first roll was 1]+Pr[second roll was 1]+=10(1100)=110.\begin{aligned} \PR{ \text{some roll resulted in $\bit1$} } &\le \PR{ \text{first roll was $\bit1$} } + \PR{ \text{second roll was $\bit1$} } + \cdots \\ & = 10 \cdot \left( \frac{1}{100} \right) \\ & = \frac{1}{10}.\end{aligned}

More generally:

Union Bound

If some event has probability pp, then after nn attempts, the probability of observing the event in any of the trials is at most npnp.

Strategies For Probability

If AA is an event, then we write Pr[¬A]\PR{\lnot A} to refer to the event that AA doesn't happen. The two probabilities Pr[A]\PR{A} and Pr[¬A]\PR{\lnot A} satisfy the following relationship:

Pr[A]=1Pr[¬A]. \PR{A} = 1 - \PR{\lnot A}.

Often one of Pr[A]\PR{A} and Pr[¬A]\PR{\lnot A} is much easier to reason about.

Example A.2.7 (Analyze the complementary probability)

I roll a six-sided die, six times. What is the probability that I see some outcome more than once? Let's think about all the ways this could happen. Well, two of the rolls could be 1, or three of rolls could be 1, or all of them could be 1, two of them could be 1 and the rest could be 2, and so on. Wait, am I double-counting repeated 2s and repeated 1s? \ldots

An easier way to attack the problem is to focus on the complementary event, that all 6 rolls are distinct. The probability we care about is 1Pr[all six rolls are distinct]1 - \PR{\text{all six rolls are distinct}}. All rolls are distinct if the sequence of dice rolls spell out a permutation of {1,,6}\{1, \ldots, 6\}. There are 6!=7206! = 720 such permutations, out of 66=466566^6 = 46656 total possible outcomes. Hence, the answer to the question is

16!66=172046656=45936466560.9846. \displaystyle 1 - \frac{6!}{6^6} = 1 - \frac{720}{46656} = \frac{45936}{46656} \approx 0.9846.

Another trick is to change the relative order of independent sampling events. If two values are sampled independently, then it makes no difference whether they are sampled simultaneously or sequentially, in either order. It can often be helpful to imagine that they are sampled sequentially, and to reason about probabilities from the perspective of an intermediate moment in time, after one value has been chosen/fixed but the other has not.

Example A.2.8 (Order independent events)

When rolling two six-sided dice, what is the probability that the dice match? Here is a standard (and totally correct way) to answer the question:

When rolling two six-sided dice, there are 62=366^2 = 36 total outcomes (a pair of numbers), and each one has probability 1/361/36 under a uniform distribution. There are six outcomes in which the dice match: both dice 1, both dice 2, both dice 3, and so on. Therefore, the probability of rolling matching dice is 6/36=1/66/36 = 1/6.

A different way to arrive at the answer goes like this:

Imagine I roll the dice sequentially, and I pause after rolling the first die but before rolling the second. The result of the first die has been fixed; let's call that value dd. The two dice will match only if another dd is rolled on the second die. Rolling this specific value of dd on the second die (indeed, rolling any particular value) happens with probability 1/61/6.

Linguistic Precision

Being “random” is not a property of a specific outcome, it is a property of the process that generates the outcome. It therefore doesn't make much sense to refer to a “random string.” Is 42 a random number? Is “heads” a random coin?

I try to use precise language in this book and avoid describing values as “random.” However, every cryptographer I know (yes, even your dear author) says things like “XX is a random string” all the time to mean “XX was chosen uniformly.” It's not a terrible sin to speak in this way, just keep in mind that this is merely a (lazy?) shortcut for a more precise meaning.

A.3. Modular arithmetic

Definition A.3.1 (Divisors)

When xx and n\nmod are integers, we say that xx divides n\nmod (equivalently, xx is a factor of n\nmod, and n\nmod is a multiple of xx), and write xnx \mid \nmod, if there exists an integer kk such that n=kx\nmod = kx . When xx does not divide n\nmod, we write xnx \nmid \nmod.

This definition applies to both positive and negative numbers (and to zero).

Example A.3.2 (Divisors)
784because84=127;70because0=07;777because77=(11)7;742because42=(6)(7).\begin{aligned} 7 & \mid 84 & \text{because} && 84 & = 12 \cdot 7; \\ 7 & \mid 0 & \text{because} && 0 & = 0 \cdot 7; \\ 7 & \mid -77 & \text{because} && -77 & = (-11) \cdot 7; \\ -7 & \mid 42 & \text{because} && 42 & = (-6)\cdot (-7).\end{aligned}
Definition A.3.3 (Modular reduction)

Let aa and n\nmod be integers, with n\nmod positive. Then a%na \pct \nmod (pronounced “aa mod n\nmod”) denotes the remainder after dividing aa by n\nmod. More formally, a%na\pct \nmod is the unique value r{0,,n1}r \in \{0, \ldots, \nmod-1\} such that n(ar)n \mid (a-r).

Pay special attention to the fact that a%na \pct \nmod is always a nonnegative number, even if aa is negative. A good way to remember how this works is:

Mnemonic For The Mod Operation

aa is (a%n)(a \pct \nmod) more than a multiple of n\nmod.

Not “closer to zero than” but “more than” a multiple of n\nmod.

Example A.3.4 (Modular reduction)
21%7=0because21=37+0;20%7=6because20=27+6;20%7=1because20=(3)7+1;1%7=6because1=(1)7+6.\begin{aligned} 21 \pct 7 & = 0 & \text{because} && 21 & = 3 \cdot 7 + \hl{0}; \\ 20 \pct 7 & = 6 & \text{because} && 20 & = 2 \cdot 7 + \hl{6}; \\ -20 \pct 7 & = 1 & \text{because} && -20 & = (-3) \cdot 7 + \hl{1}; \\ -1 \pct 7 & = 6 & \text{because} && -1 & = (-1) \cdot 7 + \hl{6}.\end{aligned}

In the third example, 20-20 is 1 more than 21-21, which is a multiple of 7. In the fourth example, 1-1 is 6 more than 7-7, which is a multiple of 7.

Unfortunately, some programming languages define %\pct for negative numbers as (a)%n=(a%n)(-a)\pct \nmod = -(a\pct \nmod), so they would define 20%7-20 \pct 7 to be (20%7)=6-(20 \pct 7) = -6. This is clearly absurd, and it's about time we stood up to these programming language designers and smashed them over the head with some mathematical truth! For now, if you are experimenting using a programming language, be sure to check whether it defines %\pct in the correct way.

Definition A.3.5 (Integers mod n\nmod)

For positive n\nmod, we write Zn={0,,n1}\hl{\Z_\nmod} = \{0,\ldots, \nmod-1\} to denote the set of integers modulo n\nmod. These are the possible remainders that result when you divide by n.\nmod.

Definition A.3.6 (Modular congruence)

For positive n\nmod, we say that integers aa and bb are congruent modulo n\nmod, and write anba \equiv_\nmod b, if n(ab)\nmod \mid (a-b). An alternative definition is that anba \equiv_\nmod b if and only if a%n=b%n.a \pct \nmod = b \pct \nmod.

It's important to appreciate the (subtle) distinction between anba \equiv_\nmod b and a=b%na = b \pct \nmod:

Example A.3.7 (Important distinction)

99101999 \equiv_{10} 19 is true, because 1010 divides 991999-19. Neither side of the equation (congruence) are from the set Z10\Z_{10}, and that's fine.

99=19%1099 = 19 \pct 10 is false. The right-hand side evaluates to the integer 99. The equals-sign compares equality as integers; 9999 and 99 are different integers.

Expressions like anba \equiv_\nmod b make sense for any a,ba, b (including negative!), but expressions like a=b%na = b \pct \nmod make sense only if aZn.a \in \Z_\nmod.

In other resources you might see the notation “ab (mod n)a \equiv b ~(\text{mod}~\nmod) instead of “anba \equiv_\nmod b.” I dislike this notation because “(mod n)(\text{mod}~\nmod) is easily mistaken for an operation or action that affects only the right-hand side of the equation. In reality, it modifies the entire expression aba \equiv b. Even though subscripts on the \equiv symbol look weird at first, I think it's worth it.

Definition A.3.8 (Common divisors)

If dxd \mid x and dyd \mid y, then dd is a common divisor of xx and yy. The largest possible such dd is called the greatest common divisor (GCD), denoted gcd(x,y)\gcd(x,y). If gcd(x,y)=1\gcd(x,y)=1, then we say that xx and yy are relatively prime.

The recursive algorithm that we still use today to compute GCDs was first described by Euclid around 300bce. It is widely regarded as the first numerical algorithm:

// Euclid's algorithm:
gcd(x,y)\subname{gcd}(x,y):
if y=0y=0 then return xx
else return gcd(y,x%y)\subname{gcd}(y, x \pct y)

Strategies For Modular Arithmetic:

You may often need to compute a value like:

(some complicated arithmetic expression)%n. (\text{some complicated arithmetic expression}) \pct \nmod.

The straightforward way to evaluate this expression is to first compute the value of the complicated expression over the integers, then reduce the answer mod n\nmod. You'll get the correct answer this way, but there's often a better choice.

The Best Trick For Modular Arithmetic

If you need to know the final result mod n\nmod, then you can/should simplify intermediate values mod n\nmod as you go. You'll get the same answer, but usually with less effort.

Example A.3.9 (Simplify intermediate values)

Consider the expression:

(678910)%11. (6 \cdot 7 \cdot 8 \cdot 9 \cdot 10) \pct 11.

The unsophisticated way of computing this value is:

(678910)%11=30240%11=1. (6 \cdot 7 \cdot 8 \cdot 9 \cdot 10) \pct 11 = 30240 \pct 11 = 1.

Those of us who don't want to compute 6789106\cdot7\cdot8\cdot9\cdot10 in our heads can reduce the intermediate results mod 11 as we go:

678910=11(42)89101198910=11(72)910116910=11(54)10111010=11100111.\begin{aligned} 6 \cdot 7 \cdot 8 \cdot 9 \cdot 10 &=\phantom{{}_{11}} (42) \cdot 8 \cdot 9 \cdot 10 \\ &\equiv_{11} {}\hl{9} \cdot 8 \cdot 9 \cdot 10 \\ &=\phantom{{}_{11}} (72) \cdot 9 \cdot 10 \\ &\equiv_{11} {}\hl{6} \cdot 9 \cdot 10 \\ &=\phantom{{}_{11}} (54) \cdot 10 \\ &\equiv_{11} {}\hl{10} \cdot 10 \\ &=\phantom{{}_{11}} 100 \\ &\equiv_{11} 1.\end{aligned}

“Simplify” doesn't always mean “reduce mod n\nmod,” resulting in a number from Zn\Z_\nmod. It means to replace one value with a congruent value that is easier to work with. Often a negative number is more favorable than a positive one:

Example A.3.10 (Simplify intermediate values)

Here's another way to evaluate the expression from the previous example:

67891011(5)(4)(3)(2)(1)=115432=11120=11121+1110+1=1.\begin{aligned} 6 \cdot 7 \cdot 8 \cdot 9 \cdot 10 &\equiv_{11} (-5) \cdot (-4) \cdot (-3) \cdot (-2) \cdot (-1) \\ &=\phantom{{}_{11}} {- 5} \cdot 4 \cdot 3 \cdot 2 \\ &=\phantom{{}_{11}} {-120} \\ &=\phantom{{}_{11}} {-121} + 1 \\ &\equiv_{11} 0 + 1 = 1 .\end{aligned}
Example A.3.11 (Simplify mod n\nmod)

I can compute 7500%87^{500} \pct 8 in my head, by noticing that 7817 \equiv_{8} -1. Therefore:

75008(1)500=1. 7^{500} \equiv_8 (-1)^{500} = 1.

I can quickly compute 892%9989^2 \pct 99 in my head, although I have not memorized the integer 89289^2. I simply notice that 89991089 \equiv_{99} -10 and compute this way:

89299(10)2=100991. 89^2 \equiv_{99} (-10)^2 = 100 \equiv_{99} 1 .

A.4. Exponents and logarithms

You probably learned (and then forgot) these identities in middle school or high school:

xaxb=xa+b,(xa)b=xab,logx(ab)=logxa+logxb,alogxb=logx(ba).\begin{aligned} x^a \cdot x^b &= x^{a+b}, \\ (x^a)^b &= x^{ab}, \\ \log_x (ab) &= \log_x a + \log_x b, \\ a \log_x b &= \log_x (b^a).\end{aligned}

Never ever ever write xaxb=xabx^a \cdot x^b = x^{ab}. If you do, your cryptography instructor will realize that life is too short, immediately resign from teaching, and join a traveling circus. But not before changing your grade in the course to a zero. Harsh, maybe, but fair.

A.5. Big-OO

Big-OO notation describes the “shape” of a function as it approaches infinity.

Definition A.5.1 (Big-OO)

A function f(n)f(n) is in the set O(g(n))O(g(n)) if there exists cc and n0n_0 such that for all n>n0n>n_0 we have f(n)cg(n)f(n) \le c \cdot g(n). In other words, cg(n)c \cdot g(n) eventually (after n0n_0) overtakes f(n)f(n).

Example A.5.2

f(n)=3n2+4n10f(n) = 3n^2 + 4 n - 10 is O(n3)O(n^3) because:

f(n)=3n2+4n103n2+4n3n2+n2(for all n4)4n2,\begin{aligned} f(n) = 3n^2 + 4n - 10 &\le 3n^2 + 4n \\ &\le 3n^2 + n^2 & \text{(for all $n \ge 4$)} \\ &\le 4n^2,\end{aligned}

so the definition of O(n3)O(n^3) is satisfied with n0=4n_0=4 and c=4c=4.

We could have also written:

f(n)=3n2+4n103n2+4n3n2+4n2(for all n1)7n2,\begin{aligned} f(n) = 3n^2 + 4n - 10 &\le 3n^2 + 4n \\ &\le 3n^2 + 4n^2 & \text{(for all $n \ge 1$)} \\ &\le 7n^2,\end{aligned}

so the definition of O(n3)O(n^3) is satisfied with n0=1n_0=1 and c=7c=7.

A.6. Linear algebra

A matrix is a 2-dimensional grid of scalar values. An n×mn \times m matrix has nn rows and mm columns. The example below is a 2×32 \times 3 matrix:

[123456] \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix}

A vector is, according to our conventions, a matrix with one column. If x=(x1,,xn)\vec x = (x_1, \ldots, x_n)^\top and y=(y1,,yn)\vec y = (y_1, \ldots, y_n)^\top, then their inner product, written xy\vec x^\top \vec y, is defined to be:

xy=i=1nxiyi. \vec x^\top \vec y = \sum_{i=1}^n x_i y_i.

If A\mat A is an n×mn \times m matrix and B\mat B is a m×km \times k matrix, then their product AB\mat A \mat B is an n×kn \times k matrix, and its value at row ii and column jj is the inner product of the ii-th row of A\mat A with the jj-th column of B\mat B.

The transpose of a matrix A\mat A, written as A\mat A^\top, is what results by reflecting it across its main diagonal. For example:

[123456]=[142536] \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix}^\top = \begin{bmatrix} 1 & 4 \\ 2& 5 \\ 3 & 6 \end{bmatrix}

The transpose of an n×mn \times m matrix is an m×nm\times n matrix. The transpose operation satisfies the following important properties:

(AB)=BA,(A)=A.\begin{aligned} (\mat A\mat B)^\top &= \mat B^\top \mat A^\top , \\ (\mat A^\top)^\top &= \mat A.\end{aligned}

Exercises

  1. Carefully use the algebraic properties of xor to derive the following useful fact, for strings A,B,C{0,1}nA,B,C \in \bits^n:

    A=BC    B=AC    C=AB. A = B \oplus C \iff B = A \oplus C \iff C = A \oplus B.

    In other words, if you know any two of the values in the equation “A=BCA = B \oplus C, then you can solve for the third.

  2. What is the result of xor'ing every nn-bit string? For example, the expression below is the xor of every 5-bit string:

    000000000100010000111111011111. \bit{00000} \oplus \bit{00001} \oplus \bit{00010} \oplus \bit{00011} \oplus \cdots \oplus \bit{11110} \oplus \bit{11111}.

    Give a convincing justification for your answer.

  3. Compute the exact probability described in example A.2.6.

  4. Consider rolling several dd-sided dice, where the sides are labeled {1,,d}\{1, \ldots, d\}.

    1. When rolling two of these dice, what is the probability of rolling a pair of 1s?

    2. When rolling two of these dice, what is the probability that they don't match?

    3. When rolling three of these dice, what is the probability that they all match?

    4. When rolling three of these dice, what is the probability that they don't all match (including the case where two match)?

    5. When rolling three of these dice, what is the probability that at least two of them match (including the case where all three match)?

    6. When rolling three of these dice, what is the probability of seeing at least one 1?

  5. h What is [0+1+2++(n2)+(n1)]%n[0 + 1 + 2 + \cdots + (\nmod-2) + (\nmod-1)] \pct \nmod? The answer might depend on whether n\nmod is even or odd.

    Rearrange the terms of the sum so that you get many values that are “favorable” mod n\nmod.

  6. What is (99)%10(-99) \pct 10?

  7. Without using a calculator, what are the last two digits of 42798642798^6? Start by translating “what are the last two digits” into a question about modular arithmetic.

  8. Without using a calculator, what is 1000!%4271000! \pct 427? That's not me being excited about the number one thousand, it's one thousand factorial!

  9. Which values xZ11x \in \Z_{11} satisfy x2115x^2 \equiv_{11} 5? Which satisfy x2116x^2 \equiv_{11} 6?

    Over the integers, x2=(x)2x^2 = (-x)^2. How is this fact reflected when working mod 11?

  10. Rewrite each of these expressions as something of the form 2x2^x.

    1. (2n)n=??\left(2^n \right)^n = {\text{??}}

    2. 2n+2n=??2^n + 2^n = {\text{??}}

    3. (2n)(2n)=??(2^n)(2^n) = {\text{??}}

    4. (2n)/2=??(2^n)/2 = {\text{??}}

    5. 2n=??\sqrt{2^n} = {\text{??}}

    6. (2n)2=??\left(2^n\right)^2 = {\text{??}}

Chapter Notes

It is possible to give sense to statements like “XX is a random string” using the methodology of Kolmogorov complexity. See [146] for a comprehensive reference.

Euclid described the GCD algorithm in Book 7 of his famous Elements. He was likely not the first to discover the algorithm.

  1. Next Chapter1. One-Time Pad and the Provable Security Mindset