{
  "aliases": [
    "binary-exponentiation",
    "square-and-multiply"
  ],
  "backlinks": [
    {
      "field": "uses",
      "target": "01TKKESHSB"
    },
    {
      "field": "uses",
      "target": "0HM584C465"
    },
    {
      "field": "uses",
      "target": "0SZCSYHW60"
    }
  ],
  "body": "\n## Summary\n\nModular exponentiation computes $b^e \\bmod m$ for integers $b$, $e \\ge 0$ and $m \\ge 1$.\nThe binary method writes $e$ in base $2$. It squares $b$ again and again modulo $m$, and it multiplies the squares $b^{2^i}$ for the bits $i$ of $e$ that are $1$.\nIt needs about $2 \\log_2 e$ multiplications modulo $m$, and each intermediate number is smaller than $m^2$.\n\n## Remarks\n\nThe method is correct because of [[compatibility with multiplication|072D5G4SK6]]: one can reduce modulo $m$ after each multiplication.\nThe [[Fermat primality test|0N5ZSZMYH6]], the [[RSA cryptosystem|0HM584C465]] and the [[Diffie–Hellman key exchange|01TKKESHSB]] all use modular exponentiation.\nThe inverse problem, to find $e$ from $b$, $m$ and $b^e \\bmod m$, is the [[discrete logarithm|09B4X51J09]] problem. It seems much harder.\n\n## Explanations\n\n### L1 human\n\nTo compute $3^{13} \\bmod 7$, write $13 = 8 + 4 + 1$. The squares are $3^1 \\equiv 3$, $3^2 \\equiv 2$, $3^4 \\equiv 4$ and $3^8 \\equiv 2$.\nThen $3^{13} \\equiv 2 \\cdot 4 \\cdot 3 = 24 \\equiv 3 \\pmod 7$.\nYou never need the large number $3^{13} = 1594323$.\n",
  "commit": "6ac761d40018146541484a9e8b9eafd6ecc303dc",
  "front": {
    "aliases": [
      "binary-exponentiation",
      "square-and-multiply"
    ],
    "kind": "concept",
    "msc": [
      "11Y16"
    ],
    "name": "Modular exponentiation",
    "same_as": {
      "wikidata": "Q1228841",
      "wikipedia": "Modular exponentiation"
    },
    "sources": [
      {
        "locator": "Right-to-left binary method",
        "url": "https://en.wikipedia.org/wiki/Modular_exponentiation"
      }
    ],
    "tag": "04DR5PM25N"
  },
  "kind": "concept",
  "link": "https://math2.tamnd.com/node/04DR5PM25N/",
  "links": null,
  "markdown": "https://math2.tamnd.com/node/04DR5PM25N.md",
  "msc": [
    "11Y16"
  ],
  "name": "Modular exponentiation",
  "prov": null,
  "role": "",
  "source": "https://github.com/tamnd/math2/blob/main/nodes/04D/04DR5PM25N.md",
  "status": null,
  "tag": "04DR5PM25N",
  "uri": "m2://04DR5PM25N",
  "url": "https://math2.tamnd.com/wiki/modular-exponentiation/",
  "writing": {
    "tag": "04DR5PM25N",
    "kind": "concept",
    "level": "W0",
    "next": [
      {
        "what": "an inventory row",
        "note": "the row of the inventory that the node accounts for",
        "have": 0,
        "want": 1
      }
    ]
  }
}
