Chapter I · modular arithmetic

Modular arithmetic and finite fields

Objective
compute modulo p
Solana
Ed25519
Foundation
finite fields

Let's start with arithmetic inside a fixed range.

Modular arithmetic wraps values

Imagine that you are standing in front of seven lockers arranged in a circle and labelled 0 through 6. If you move forward from locker 6, you return to locker 0. When a result reaches 7, we subtract 7 until it fits.

5 + 4 = 9 → 2 Start at 5, walk four lockers: 6, 0, 1, 2.
Modulo n arithmetic keeps the remainder after division by n.

We say that 9 and 2 are congruent modulo 7:

9 ≡ 2 (mod 7)The symbol ≡ means both integers represent the same field element.

Calculate with modular arithmetic

Use the controls and watch each answer return to one allowed value. Try a negative number too.

Try 3 × 5 mod 7, then -1 + 0 mod 7. The second example shows how subtraction wraps modulo 7.

Why ECC uses prime fields

We also need multiplication and division. With a prime modulus, every nonzero value has a multiplicative inverse.

3 × 5 ≡ 1 (mod 7)So 5 is the multiplicative inverse of 3 modulo 7. Division by 3 means multiplication by 5.

The values 0 through 6 form the finite field F7. Add, subtract, multiply, or divide by a nonzero value and the result stays in the field.

That promise fails for many composite moduli. Modulo 8, no value multiplied by 2 produces 1. The value 2 has no multiplicative inverse, so division by 2 breaks.

Quick check 1

In modulo 7 arithmetic, what is 5 + 4?

Quick check 2

Which value is congruent to 16 modulo 7?

Check your understanding

Try to answer each question before you open the solutions.

  1. What does modulo 7 do to an integer?
  2. Why is 9 congruent to 2 modulo 7?
  3. What extra capability does a finite field give us beyond clock arithmetic?
  4. Where do finite fields appear in Solana?
Show numbered solutions
  1. Modulo 7 maps any integer to one of 0 through 6 by keeping the remainder. For example, 23 becomes 2 because 23 = 3·7 + 2.

  2. Both leave remainder 2 when divided by 7. Their difference is also a multiple of 7, so 9 ≡ 2 (mod 7).

  3. A finite field supports multiplication and division by every nonzero element. Division uses a modular inverse, so 3 ÷ 2 modulo 7 means 3 · 4 because 2 · 4 ≡ 1.

  4. Ed25519 uses the field with modulus 2255 − 19 for curve coordinates. This modulus is separate from the subgroup order that bounds private scalars.

Primary reading