mathsoftware

A course about LaTeX and SageMath
git clone https://git.tronto.net/mathsoftware
Download | Log | Files | Refs | README | LICENSE

X2-StudentsRequests-checkpoint.ipynb (5285B)


      1 {
      2  "cells": [
      3   {
      4    "cell_type": "markdown",
      5    "metadata": {},
      6    "source": [
      7     "# Diffie-Hellman key exchange\n",
      8     "\n",
      9     "The following is a simple implementation of the classic [Diffie-Hellman key exchange](https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange) cryptographic protocol."
     10    ]
     11   },
     12   {
     13    "cell_type": "code",
     14    "execution_count": 9,
     15    "metadata": {},
     16    "outputs": [
     17     {
     18      "name": "stdout",
     19      "output_type": "stream",
     20      "text": [
     21       "Public key: p = 20747 and g = 13428 \n",
     22       "\n",
     23       "[[ Alice's secret key: a = 12403 ]]\n",
     24       "[[ Bob's secret key: b = 17642 ]] \n",
     25       "\n",
     26       "Alice sends h1 = 14710 to Bob\n",
     27       "Bob sends h2 = 10680 to Alice \n",
     28       "\n",
     29       "Alice computed 10455 using h2 and her secret a\n",
     30       "Bob computed 10455 using h1 and his secret b\n"
     31      ]
     32     }
     33    ],
     34    "source": [
     35     "# Public information:\n",
     36     "p = Primes()[10^3 + randint(1,10000)] # random prime\n",
     37     "g = randint(2, p-1) # random integer\n",
     38     "\n",
     39     "print(\"Public key: p =\", p, \"and g =\", g, \"\\n\")\n",
     40     "\n",
     41     "a = randint(2, p-1) # Only Alice knows this\n",
     42     "b = randint(2, p-1) # Only Bob knows this\n",
     43     "\n",
     44     "print(\"[[ Alice's secret key: a =\", a, \"]]\")\n",
     45     "print(\"[[ Bob's secret key: b =\", b, \"]]\", \"\\n\")\n",
     46     "\n",
     47     "h1 = (g^a) % p # Alice sends this to Bob\n",
     48     "h2 = (g^b) % p # Bob sends this to Alice\n",
     49     "\n",
     50     "print(\"Alice sends h1 =\", h1, \"to Bob\")\n",
     51     "print(\"Bob sends h2 =\", h2, \"to Alice\", \"\\n\")\n",
     52     "\n",
     53     "secret_a = (h2^a) % p # Alice can compute this because she knows a\n",
     54     "secret_b = (h1^b) % p # Bob can compute this because he knows b\n",
     55     "\n",
     56     "print(\"Alice computed\", secret_a, \"using h2 and her secret a\")\n",
     57     "print(\"Bob computed\", secret_b, \"using h1 and his secret b\")"
     58    ]
     59   },
     60   {
     61    "cell_type": "markdown",
     62    "metadata": {},
     63    "source": [
     64     "## General Diffie-Hellman\n",
     65     "\n",
     66     "The following code is an implementation of a generic Diffie-Hellman key exchange protocol that uses a group $G$ instead of $(\\mathbb Z/p \\mathbb Z)^\\times$."
     67    ]
     68   },
     69   {
     70    "cell_type": "code",
     71    "execution_count": 24,
     72    "metadata": {},
     73    "outputs": [
     74     {
     75      "name": "stdout",
     76      "output_type": "stream",
     77      "text": [
     78       "Public key:\n",
     79       "G = Additive abelian group isomorphic to Z/171 embedded in Abelian group of points on Elliptic Curve defined by y^2 = x^3 + x + 156 over Finite Field of size 157 \n",
     80       "g = (155 : 60 : 1) \n",
     81       "\n",
     82       "[[ Alice's secret key: a = 141 ]]\n",
     83       "[[ Bob's secret key: b = 158 ]] \n",
     84       "\n",
     85       "Alice sends h1 = (29 : 125 : 1) to Bob\n",
     86       "Bob sends h2 = (60 : 59 : 1) to Alice \n",
     87       "\n",
     88       "Alice computed (109 : 94 : 1) using h2 and her secret a\n",
     89       "Bob computed (109 : 94 : 1) using h1 and his secret b\n"
     90      ]
     91     }
     92    ],
     93    "source": [
     94     "def genericDH(G):\n",
     95     "    if G.cardinality() == 1:\n",
     96     "        print(\"Group is trivial, can't do anything\")\n",
     97     "        return\n",
     98     "    g = G.random_element()\n",
     99     "    while g == G.identity(): # Make sure g is not trivial\n",
    100     "        g = G.random_element()\n",
    101     "        \n",
    102     "    print(\"Public key:\\nG =\", G, \"\\ng =\", g, \"\\n\")\n",
    103     "    \n",
    104     "    a = randint(2, G.exponent()-1) # Only Alice knows this\n",
    105     "    b = randint(2, G.exponent()-1) # Only Bob knows this\n",
    106     "\n",
    107     "    print(\"[[ Alice's secret key: a =\", a, \"]]\")\n",
    108     "    print(\"[[ Bob's secret key: b =\", b, \"]]\", \"\\n\")\n",
    109     "    \n",
    110     "    # \"Ternary operator\", I did not explain this\n",
    111     "    # https://docs.python.org/3/reference/expressions.html#conditional-expressions\n",
    112     "    h1 = g^a if G.is_multiplicative() else a*g # Alice sends this to Bob\n",
    113     "    h2 = g^b if G.is_multiplicative() else b*g # Bob sends this to Alice\n",
    114     "\n",
    115     "    print(\"Alice sends h1 =\", h1, \"to Bob\")\n",
    116     "    print(\"Bob sends h2 =\", h2, \"to Alice\", \"\\n\")\n",
    117     "    \n",
    118     "    secret_a = h2^a if G.is_multiplicative() else a*h2 # Alice can compute this because she knows a\n",
    119     "    secret_b = h1^b if G.is_multiplicative() else b*h1 # Bob can compute this because he knows b\n",
    120     "\n",
    121     "    print(\"Alice computed\", secret_a, \"using h2 and her secret a\")\n",
    122     "    print(\"Bob computed\", secret_b, \"using h1 and his secret b\")\n",
    123     "    \n",
    124     "E = EllipticCurve(GF(157), [1,-1])\n",
    125     "G = E.abelian_group()\n",
    126     "genericDH(G)"
    127    ]
    128   },
    129   {
    130    "cell_type": "markdown",
    131    "metadata": {},
    132    "source": [
    133     "# Numerical methods for PDEs"
    134    ]
    135   },
    136   {
    137    "cell_type": "code",
    138    "execution_count": null,
    139    "metadata": {},
    140    "outputs": [],
    141    "source": []
    142   }
    143  ],
    144  "metadata": {
    145   "kernelspec": {
    146    "display_name": "SageMath 9.0",
    147    "language": "sage",
    148    "name": "sagemath"
    149   },
    150   "language_info": {
    151    "codemirror_mode": {
    152     "name": "ipython",
    153     "version": 3
    154    },
    155    "file_extension": ".py",
    156    "mimetype": "text/x-python",
    157    "name": "python",
    158    "nbconvert_exporter": "python",
    159    "pygments_lexer": "ipython3",
    160    "version": "3.8.5"
    161   }
    162  },
    163  "nbformat": 4,
    164  "nbformat_minor": 4
    165 }