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

Generated with cgit - Back to sebastiano.tronto.net