freecoding.school100% FREE · NO SIGNUP
Proof PeaksISSUE #1 of 38

finite fields · modular arithmetic for crypto

ZeekaVSThe Collision
Zeeka saysAll blockchain cryptography lives in a finite field — integers mod a prime, where add, multiply, and invert always stay in range and wrap around.

Strip a blockchain down far enough and you reach a finite field: the integers 0…p−1 for some prime p, with arithmetic done mod p. Addition and multiplication wrap around, so results never grow out of a fixed size — which is exactly why keys, hashes, and signatures are fixed-length. The magic property is that every nonzero element has a multiplicative inverse, so you can "divide" in a field even though there are no fractions.

You rarely divide directly; instead you multiply by the inverse, found with Fermat's little theorem: a⁻¹ ≡ a^(p−2) mod p. Elliptic curves, hash functions, and zero-knowledge circuits all compute inside a field, so getting comfortable with modular add / multiply / invert is the prerequisite for everything that follows in this track.

Power-ups you unlock

The Collision attacks — common mistakes

Boss battleCompute the modular inverse of 42 in GF(97) and verify 42 × inverse ≡ 1 (mod 97).

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre id="o"></pre>
<script>
const p = 97;                              // a small prime field GF(97)
const add = (a,b)=> (a+b) % p;
const mul = (a,b)=> (a*b) % p;
const pow = (a,e)=>{ let r=1; a%=p; while(e>0){ if(e&1) r=mul(r,a); a=mul(a,a); e>>=1; } return r; };
const inv = (a)=> pow(a, p-2);             // Fermat: a^(p-2) ≡ a⁻¹ (mod p)
const a = 42, ai = inv(a);
document.getElementById('o').textContent = [
  'field GF(' + p + ')',
  '42 + 70 = ' + add(42,70) + '   (wraps mod ' + p + ')',
  '42 × 70 = ' + mul(42,70),
  'inverse of 42 = ' + ai + '   (= 42^(p-2) mod p)',
  'check 42 × ' + ai + ' mod ' + p + ' = ' + mul(a,ai) + '   ← must be 1'
].join('\n');
</script></body></html>
▶ Open the interactive comic issue
Elliptic Curves · Group Law · Secp256k1 Vs Bn254 ›