<a id="ch-01"></a>

## Chapter 1 — Mathematical reasoning

### Learning outcomes

You will (1) reason precisely about definitions and claims in cryptography; (2) use the XOR operation and its algebraic properties; (3) prove why reusing a one-time pad key fails.

### 1.1 The case and precise claims

Priya Raman is a first-year bachelor's student at the fictional Ashford University. Over four years she will work with the fictional Tidewell Public Library Consortium, twelve branch libraries sharing a patron portal, an e-book service and staff systems, guided by lecturer Dr. Tomás Echeverría and the consortium's IT lead, Grace Okafor. Every patron, key and figure here is synthetic. Her first lesson is that cryptography is a mathematical discipline: a claim such as "this cipher is secure" means nothing until security is defined, the attacker is described and the claim is proved or reduced to a well-studied assumption, as Katz and Lindell insist.

### 1.2 XOR and its algebra

Exclusive or, written XOR, compares two bits and outputs 1 when they differ. It has properties Priya proves from its truth table: x XOR x = 0, x XOR 0 = x, and XOR is associative and commutative. These make XOR reversible: if c = p XOR k then c XOR k = p. The one-time pad encrypts by XORing a message with a truly random key of the same length used once, and Shannon proved it perfectly secret under those conditions. Proving small algebraic facts carefully is practice for reading the proofs in later courses.

### Lab — One-time pad reuse

```python
"""Why a one-time pad must never be reused: XOR of two ciphertexts cancels the key (toy data)."""
import secrets
def xor(a, b): return bytes(x ^ y for x, y in zip(a, b))

def main():
    p1, p2 = b"RENEW BOOK 0417", b"FINE PAID  0938"
    key = bytes.fromhex("8f1e2a77c03b5d9e4412ab6f0e7c31")  # fixed demo pad, used twice on purpose
    c1, c2 = xor(p1, key), xor(p2, key)
    print("c1 xor c2 == p1 xor p2:", xor(c1, c2) == xor(p1, p2))
    guess = xor(xor(c1, c2), b"RENEW BOOK 0417")
    print("knowing p1 reveals p2:", guess.decode())
    fresh = secrets.token_bytes(len(p1))
    print("fresh pad length matches message:", len(fresh) == len(p1))

if __name__ == "__main__":
    main()
```

Run with `python3 lab_xor.py`. Verified output:

```text
c1 xor c2 == p1 xor p2: True
knowing p1 reveals p2: FINE PAID  0938
fresh pad length matches message: True
```

### 1.3 Why reuse fails

The lab encrypts two patron messages with the same pad on purpose. XORing the two ciphertexts cancels the key, since (p1 XOR k) XOR (p2 XOR k) = p1 XOR p2, which the lab confirms as True. If an attacker knows or guesses one message, such as a standard "RENEW BOOK" notice, the other appears in clear: FINE PAID 0938. The proof takes three lines, and the lesson lasts a career: a scheme that is perfect under its conditions can fail completely when a condition, here "use once", is violated. The fresh pad drawn from the secrets module shows how keys should be generated, though real systems use standard ciphers instead of pads.

### Worked example

Priya proves x XOR x = 0 from the truth table, derives c1 XOR c2 = p1 XOR p2, and uses the lab to recover a second message from a known first message when a pad is reused.

> **Synthetic case.** Tidewell, its patrons and messages are invented.

### Errors and limits

Perfect secrecy holds only for truly random, never-reused keys as long as the message; practical systems use AES-GCM or similar from vetted libraries, not pads. Toy demonstrations are not designs.

### Exercises

- **EX001 — Defining secure.** Why is "this cipher is secure" meaningless on its own? *Rubric:* 3 = security must be defined with an attacker model and proved or reduced to an assumption;; 2 = needs definition;; 1 = it is meaningful;; 0 = none.
- **EX002 — XOR identities.** State two XOR identities that make decryption work. *Rubric:* 3 = x XOR x = 0 and x XOR 0 = x (with associativity);; 2 = one;; 1 = wrong;; 0 = none.
- **EX003 — Pad reuse result.** What did the lab recover, and why? *Rubric:* 3 = FINE PAID 0938, because c1 XOR c2 = p1 XOR p2 and p1 was known;; 2 = message;; 1 = vague;; 0 = none.

### Synthesis

Cryptography begins with precise definitions and proofs; the one-time pad shows how a perfect scheme collapses when one condition is broken.

### Continue studying — chapter 01 of 40

[← Course home](../../README.md) · [Course chapter index](../SUBJECTS.md) · [Chapter 02 (draft) →](#ch-02)

## Worked answers

<a id="answer-ex001"></a>
**EX001.** Without a precise definition of security and of the attacker, the claim cannot be proved or tested.

<a id="answer-ex002"></a>
**EX002.** x XOR x equals 0 and x XOR 0 equals x, so XORing the key again restores the message.

<a id="answer-ex003"></a>
**EX003.** It recovered "FINE PAID 0938", since XORing the ciphertexts cancels the key and the first message was known.
