{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fcb452c2",
   "metadata": {},
   "source": [
    "# CR36 · Prime fields and composite rings\n",
    "\n",
    "A field has additive and multiplicative identities and every nonzero element has a multiplicative inverse. Modulo a composite integer, zero divisors can exist; GF(pⁿ) is not ordinary integer arithmetic modulo pⁿ.\n",
    "\n",
    "Foundational concepts; prerequisite: CR35.\n",
    "\n",
    "**Scope:** Small synthetic parameters; educational mathematics, not production cryptography."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "id": "concept036",
   "source": [
    "## Fields versus rings\n",
    "\n",
    "A group has closure, associativity, an identity and inverses for one operation. A field supports addition, multiplication and division by nonzero elements with distributivity. Integers modulo a prime form GF(p).\n",
    "\n",
    "**Mathematical / protocol foundations:** For nonzero a modulo p, gcd(a,p)=1 and Bézout guarantees an inverse. Composite moduli can contain nonzero zero divisors, so division is not generally defined.\n",
    "\n",
    "**Worked teaching example and interpretation:** Modulo 17, 2·9=18≡1, so 9 is inverse of 2. Modulo 8, 2·4≡0 although neither operand is zero. That ring is not a field.\n",
    "\n",
    "Compare corresponding states; for randomized or timed runs compare invariants rather than literal random bytes or timings.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "id": "m291-theory-36",
   "source": [
    "## Mathematical foundations\n",
    "\n",
    "A field has addition and multiplication, identities 0 and 1, additive inverses and a multiplicative inverse for every nonzero element. Integers modulo a prime form a field. Composite moduli form rings and can contain nonzero zero divisors.\n",
    "\n",
    "**Defining equation:** a·b ≡ 1 (mod n) iff b is an inverse of a; gcd(a,n)=1\n",
    "\n",
    "**Worked example:** Modulo 17, 7·5≡1. Modulo 15, 3·5≡0 although neither factor is zero.\n",
    "\n",
    "**Common misconception:** A table containing a zero product of nonzero elements is not a field multiplication table.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e687a89a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ===== CR36 | Algorithm and verification =====\n",
    "# ===== Algorithm and verification =====\n",
    "# The function returns the exact intermediate states and verifies the invariant.\n",
    "import math\n",
    "import json\n",
    "\n",
    "def prime(n):\n",
    "    return n>=2 and all(n%i for i in range(2,math.isqrt(n)+1))\n",
    "\n",
    "DEFAULT={'n': 17}\n",
    "ALTERNATE={'n': 15}\n",
    "\n",
    "def run(overrides=None):\n",
    "    # Merge only explicitly supplied inputs with the reference baseline.\n",
    "    params={**DEFAULT, **(overrides or {})}\n",
    "    n=params['n']\n",
    "    steps=[{'row':i,'addition':[(i+j)%n for j in range(n)],'multiplication':[i*j%n for j in range(n)],'inverse':next((j for j in range(1,n) if i*j%n==1),None)} for i in range(n)]\n",
    "    result={'prime':prime(n),'nonzero_without_inverse':[s['row'] for s in steps[1:] if s['inverse'] is None]}\n",
    "    # Independently check the stated mathematical or protocol invariant.\n",
    "    assert result[\"prime\"] == (not result[\"nonzero_without_inverse\"]), \"verification failed\"\n",
    "    return {\"steps\":steps,\"result\":result}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aeabbb1e",
   "metadata": {},
   "source": [
    "## Baseline calculation\n",
    "Inspect each trace row. The final result is separate from intermediate states."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a0539369",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ===== CR36 | Execute the baseline =====\n",
    "# ===== Execute the baseline =====\n",
    "# Run the default worked example; inspect the intermediate states in order.\n",
    "baseline=run()\n",
    "for step in baseline[\"steps\"]: print(step)\n",
    "print(\"Result:\",baseline[\"result\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd331e13",
   "metadata": {},
   "source": [
    "## Controlled comparison\n",
    "Change the indicated input; leave other inputs unchanged. A baseline is the reference run. A mathematical verification substitutes the result back into its defining equation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da66514a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ===== CR36 | Compare the specified alternative =====\n",
    "# ===== Compare the specified alternative =====\n",
    "# Override only the chosen input(s), leaving remaining parameters at baseline.\n",
    "comparison=run(ALTERNATE)\n",
    "for step in comparison[\"steps\"]: print(step)\n",
    "print(\"Result:\",comparison[\"result\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24e5e76f",
   "metadata": {},
   "source": [
    "## Student activity\n",
    "Compare n=17 and 15. Count nonzero rows with no 1 in the multiplication table and identify a zero divisor.\n",
    "\n",
    "Submit a baseline/alternate table, trace evidence and a justification. Identify which checks passed and what they do not prove."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "id": "m291-evidence-36",
   "source": [
    "## Reproduce the browser experiment\n",
    "\n",
    "Run n=17. Inspect multiplication row 7 and locate the value 1. Record its column. Change only n to 15; inspect rows 3 and 5 and record nonzero rows missing 1. Compare counts and the zero-divisor example.\n",
    "\n",
    "Use the same input values in the browser and Python. Keep a table of input, intermediate operation, result, verification and interpretation.\n",
    "\n",
    "**Independent task:** Compare modulo 11 and modulo 12. Give a nonzero zero divisor in the composite ring and show why it has no inverse.\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}