{
  "schema_version": 1,
  "title": "Modular arithmetic and finite fields",
  "url": "/writing/understanding-elliptic-curves/lessons/0001-modular-arithmetic-and-finite-fields.html",
  "author": "Abhineet Biju",
  "text": "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. First number Operationaddmultiply Second number Modulus, from 2 to 16 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 F_(7). 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 = 2^(255) − 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. A finite field is the number system in which elliptic-curve operations run. 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 2^(255) − 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.",
  "headings": [
    {
      "level": 1,
      "id": null,
      "text": "Modular arithmetic and finite fields"
    },
    {
      "level": 2,
      "id": null,
      "text": "Modular arithmetic wraps values"
    },
    {
      "level": 2,
      "id": null,
      "text": "Calculate with modular arithmetic"
    },
    {
      "level": 2,
      "id": null,
      "text": "Why ECC uses prime fields"
    },
    {
      "level": 3,
      "id": null,
      "text": "Quick check 1"
    },
    {
      "level": 3,
      "id": null,
      "text": "Quick check 2"
    },
    {
      "level": 2,
      "id": null,
      "text": "How finite fields apply to Solana"
    },
    {
      "level": 2,
      "id": null,
      "text": "Check your understanding"
    },
    {
      "level": 3,
      "id": null,
      "text": "Primary reading"
    }
  ],
  "sections": [
    {
      "id": "one-rule",
      "heading": "Modular arithmetic wraps values",
      "text": "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."
    },
    {
      "id": "experiment",
      "heading": "Calculate with modular arithmetic",
      "text": "Calculate with modular arithmetic Use the controls and watch each answer return to one allowed value. Try a negative number too. First number Operationaddmultiply Second number Modulus, from 2 to 16 Try 3 × 5 mod 7, then -1 + 0 mod 7. The second example shows how subtraction wraps modulo 7."
    },
    {
      "id": "field",
      "heading": "Why ECC uses prime fields",
      "text": "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 F_(7). 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?"
    },
    {
      "id": "builder-link",
      "heading": "How finite fields apply to Solana",
      "text": "How finite fields apply to Solana When you use Ed25519, its curve operates over the field defined by p = 2^(255) − 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. A finite field is the number system in which elliptic-curve operations run."
    },
    {
      "id": "close",
      "heading": "Check your understanding",
      "text": "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 2^(255) − 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."
    }
  ],
  "links": [
    {
      "label": "writing",
      "url": "/#writing"
    },
    {
      "label": "Guide home",
      "url": "/writing/understanding-elliptic-curves/"
    },
    {
      "label": "Glossary",
      "url": "/writing/understanding-elliptic-curves/reference/0001-cryptography-glossary.html"
    },
    {
      "label": "congruent",
      "url": "/writing/understanding-elliptic-curves/reference/0001-cryptography-glossary.html"
    },
    {
      "label": "RFC 8032, section 5.1.1",
      "url": "https://www.rfc-editor.org/info/rfc8032/"
    },
    {
      "label": "Solana's PDA documentation",
      "url": "https://solana.com/docs/core/pda"
    },
    {
      "label": "Previous: guide home",
      "url": "/writing/understanding-elliptic-curves/"
    },
    {
      "label": "Next: Elliptic-curve points and addition",
      "url": "/writing/understanding-elliptic-curves/lessons/0002-elliptic-curve-point-addition.html"
    }
  ]
}
