{ "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 }