{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "102a68c7",
   "metadata": {},
   "source": [
    "# CR35 · Modular clock and residue classes\n",
    "\n",
    "For positive modulus n, a=qn+r with 0≤r<n. Congruence means equal residues, not equal integers. On the GF(17) clock, 35→1, −7→10 and −11→6. Addition and multiplication reduce after the operation.\n",
    "\n",
    "Foundational concepts; prerequisite: none.\n",
    "\n",
    "**Scope:** Small synthetic parameters; educational mathematics, not production cryptography."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "id": "concept035",
   "source": [
    "## Congruence and residues\n",
    "\n",
    "Euclidean division defines a canonical remainder for any integer, including negatives. Congruence means two integers differ by a multiple of the modulus, not that their numerical values are identical.\n",
    "\n",
    "**Mathematical / protocol foundations:** a=qn+r with n>0 and 0≤r<n; q=floor(a/n). a≡b (mod n) iff n divides a−b. Reduce addition, subtraction and multiplication results into the same interval.\n",
    "\n",
    "**Worked teaching example and interpretation:** 35=2·17+1; −7=−1·17+10; −11=−1·17+6. 13+9=22≡5 and 8·15=120≡1. The clock moves backward for negative inputs.\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-35",
   "source": [
    "## Mathematical foundations\n",
    "\n",
    "For a positive integer n, Euclidean division uniquely expresses an integer a as qn+r, 0≤r<n. Congruence means two numbers differ by a multiple of n. On the clock, reducing a negative number wraps backward. Modular addition, subtraction and multiplication reduce the ordinary operation result.\n",
    "\n",
    "**Defining equation:** a = qn+r; a ≡ r (mod n); q=floor(a/n)\n",
    "\n",
    "**Worked example:** 35=2·17+1; −7=−1·17+10; −11=−1·17+6. (13+9) mod 17=5; (8·15) mod 17=1.\n",
    "\n",
    "**Common misconception:** Congruence is not ordinary equality. A negative remainder from a language-specific operator may need normalization.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1dbd4321",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ===== CR35 | Algorithm and verification =====\n",
    "# ===== Algorithm and verification =====\n",
    "# The function returns the exact intermediate states and verifies the invariant.\n",
    "import json\n",
    "\n",
    "\n",
    "\n",
    "DEFAULT={'a': 35, 'b': 9, 'n': 17}\n",
    "ALTERNATE={'a': -7}\n",
    "\n",
    "def run(overrides=None):\n",
    "    # Merge only explicitly supplied inputs with the reference baseline.\n",
    "    params={**DEFAULT, **(overrides or {})}\n",
    "    a=params['a']\n",
    "    b=params['b']\n",
    "    n=params['n']\n",
    "    q,r=divmod(a,n)\n",
    "    steps=[{'a':a,'q':q,'n':n,'r':r},{'operation':'addition','raw':a+b,'residue':(a+b)%n},{'operation':'subtraction','raw':a-b,'residue':(a-b)%n},{'operation':'multiplication','raw':a*b,'residue':a*b%n}]\n",
    "    result={'residue':r,'identity':q*n+r==a,'sum':(a+b)%n,'product':a*b%n}\n",
    "    # Independently check the stated mathematical or protocol invariant.\n",
    "    assert result[\"identity\"] and result[\"residue\"]==a%n, \"verification failed\"\n",
    "    return {\"steps\":steps,\"result\":result}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0fdd91f",
   "metadata": {},
   "source": [
    "## Baseline calculation\n",
    "Inspect each trace row. The final result is separate from intermediate states."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f90b5247",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ===== CR35 | 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": "0ef1ea53",
   "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": "4c1f3e20",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ===== CR35 | 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": "0335e709",
   "metadata": {},
   "source": [
    "## Student activity\n",
    "Change a from 35 to −7, then −11 while holding n=17. Record q and r and verify a=qn+r. Explain why negative inputs still have nonnegative residues.\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-35",
   "source": [
    "## Reproduce the browser experiment\n",
    "\n",
    "Run a=35,b=9,n=17. Inspect the quotient and remainder. Hold b and n fixed; set a=−7 and −11. Write a=qn+r for each. Next use a=13,b=9, then a=8,b=15 to verify the addition and multiplication examples separately.\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:** For n=19, reduce −42 and verify the quotient identity. Give two distinct integers congruent to its residue.\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}