# Cryptography

### Part 2: Protocols, or the Search for Spot

PGSS Computer Science Core Slides

We look more at cryptography, examining a special-purpose protocol for finding the average without revealing numbers.

### Signatures

To convince Alice he's truly Bob, Bob uses a signature.

Bob has two keys, one published, one kept private.

Messages sent encrypted by the private key make sense only with the published key, but encryption with the private key can't be imitated.

So Eve can't masquerade as Bob!

### Communicating an Average

Alice, Bob, Krunch, and Spot just got their test grades.

```  Alice 95
Bob 88
Spot 50
Krunch 82
```

They want to know their average score, but nobody wants to reveal their grades.

What can they do?

### Modulo Arithmetic

We write x mod N to denote the remainder of x divided by N.

For addition, this gives ``wrap-around'' behavior. For 0 <= x < N, 0 <= y < N,

```                     x + y      if x + y < N
(x + y) mod N = {
x + y - N  otherwise
```
Notice that modulo addition is commutative and associative.

We take N to be 401, because the total of the four scores will be less than 401.

### The Algorithm

All behave the same.

1. Bob chooses r[B,A], r[B,S], r[B,K] randomly between 0 and 400. He calculates r[B,B] so that (r[B,A] + r[B,B] + r[B,S] + r[B,K]) mod 401 is his own score, 88.
2. Bob sends r[B,A] to Alice, receives r[A,B]. He sends r[B,S] to Spot, receives r[S,B]. He sends r[B,K] to Krunch, receives r[K,B].
3. Compute c[B] = (r[A,B] + r[B,B] + r[S,B] + r[K,B]) mod 401. Give c[B] to everybody, get c[A],c[S],c[K].
4. The total is (c[A] + c[B] + c[S] + c[K]) mod 401.

### Why Does It Work?

Let's look at an example.

```            sends to whom
who    Alice  Bob  Spot  Krunch   total
Alice    135   240   301   221   =  95 = Alice's score
Bob      285   363   109   133   =  88 = Alice's score
Spot     135   300   334    83   =  50 = Spot's score
Krunch   132     5   230   116   =  82 = Krunch's score
---   ---   ---   ---     ---
total    286   106   172   152   = 315
```

Because, as we observed, addition is commutative and associative, we can find the sum of all the entries either by summing the rows first (and getting the actual scores) and then totaling these sums, or by summing the columns first and then totaling these sums. The first is the total of the scores; the second is what the algorithm does. But they're both the total of everything in the array, so the answer is correct.