Chapter I · modular arithmetic
Modular arithmetic and 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.
We say that 9 and 2 are congruent modulo 7:
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.
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?
How finite fields apply to Solana
When you use Ed25519, its curve operates over the field defined by p = 2255 − 19. Curve points, keys, and signatures all use modular arithmetic, although the SDK hides it.
Solana uses the same curve test for PDAs. A PDA must be off-curve, so it has no Ed25519 private key. Programs authorize PDAs with invoke_signed.
Check your understanding
Try to answer each question before you open the solutions.
- What does modulo 7 do to an integer?
- Why is 9 congruent to 2 modulo 7?
- What extra capability does a finite field give us beyond clock arithmetic?
- Where do finite fields appear in Solana?
Show numbered solutions
-
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.
-
Both leave remainder 2 when divided by 7. Their difference is also a multiple of 7, so 9 ≡ 2 (mod 7).
-
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.
-
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
- RFC 8032, section 5.1.1 defines the modular arithmetic used by Ed25519.
- Solana's PDA documentation explains why PDAs must be off-curve.