Homework3-checkpoint.ipynb (7576B)
1 { 2 "cells": [ 3 { 4 "cell_type": "markdown", 5 "metadata": {}, 6 "source": [ 7 "*For this exercise you should have received this text in .ipynb format. Complete the exercises by modifying this file, and submit the modified version*" 8 ] 9 }, 10 { 11 "cell_type": "markdown", 12 "metadata": {}, 13 "source": [ 14 "**Exercise 1**\n", 15 "\n", 16 "Use SageMath to solve the following problems:\n", 17 "\n", 18 "(a) Find the roots of the following polynomial over $\\mathbb Q$:\n", 19 "\\begin{align*}\n", 20 " p = 4 x^{7} + 4 x^{6} + 3 x^{5} - 13 x^{4} - 13 x^{3} - 9 x^{2} + 3 x + 3 \\in \\mathbb Q[x]\n", 21 "\\end{align*}\n", 22 "\n", 23 "(b) Find the roots of the same polynomial $p$ over $\\mathbb R$ and over $\\mathbb C$.\n", 24 "\n", 25 "(c) Find the determinant, the trace and the characteristic polynomial of the following matrix:\n", 26 "\\begin{align*}\n", 27 "A=\\left(\\begin{array}{rrrr}\n", 28 "-1 & 1 & -1 & 0 \\\\\n", 29 "1 & \\frac{1}{2} & 1 & 0 \\\\\n", 30 "\\frac{1}{2} & -\\frac{1}{2} & -2 & 1 \\\\\n", 31 "0 & 0 & 1 & 1\n", 32 "\\end{array}\\right)\n", 33 "\\end{align*}\n", 34 "\n", 35 "(d) Find a solution to the linear system $A\\mathbf x =\\mathbf v$, where $A$ is the matrix above and $\\mathbf v=(1, 2, 3, 4)$.\n", 36 "\n", 37 "Write your code in the cell below." 38 ] 39 }, 40 { 41 "cell_type": "code", 42 "execution_count": null, 43 "metadata": {}, 44 "outputs": [], 45 "source": [] 46 }, 47 { 48 "cell_type": "markdown", 49 "metadata": {}, 50 "source": [ 51 "**Exercise 2**\n", 52 "\n", 53 "After exchanging messages with the RSA protocol seen in class, Alice and Bob decide to meet and play their favorite game: flip a coin. They like this game very much because it does not take long to set it up and they have exactly the same chances of winning.\n", 54 "\n", 55 "Unfortunately, due to the COVID-19 pandemic they cannot meet in person, and despite being good friends they don't trust each other enough to play this game via Webex call. Luckily, Alice is an expert in cryptography and she knows how to play this game using the Chinese remainder theorem.\n", 56 "\n", 57 "The game plays out as follows:\n", 58 "\n", 59 "(A1) Alice picks two large prime numbers $p$ and $q$, she computes $n=pq$ and sends $n$ to Bob, keeping $p$ and $q$ secret.\n", 60 "\n", 61 "(B1) Bob picks a random number $a$ with $1<a<n$ and $\\gcd(a,n)=1$, computes $b=a^2\\mod n$ and sends $b$ to Alice, keeping $a$ secret.\n", 62 "\n", 63 "(A2) Alice computes two numbers $x$ and $y$ such that $x^2\\equiv b\\pmod p$ and $y^2\\equiv b\\pmod q$ and she uses the Chinese remainder theorem to compute a number $z$ such that $z\\equiv x\\pmod p$ and $z\\equiv y\\pmod q$, so that $z^2\\equiv b\\pmod n$. Then she sends $z$ to Bob.\n", 64 "\n", 65 "Since $n$ is the product of two primes, there are $4$ possible square roots of $b$ modulo $n$, corresponding to the solutions of the four systems of congruences (one for each possible combination of $\\pm$)\n", 66 "\\begin{align*}\\begin{cases}\n", 67 "z\\equiv \\pm x\\pmod p\\\\\n", 68 "z \\equiv \\pm y\\pmod q\n", 69 "\\end{cases}\\end{align*}\n", 70 "\n", 71 "One of those solutions is $a$ and another is $-a$, and Bob knows them. Alice is picking one of the $4$ possible roots at random (she chooses between $x$ and $-x$ and between $y$ and $-y$), so she has $50\\%$ chance of picking one that Bob already knows. This corresponds to Alice flipping a coin, and she wins if she picks $\\pm a$:\n", 72 "\n", 73 "(B2) If $z\\equiv\\pm a\\pmod n$, Bob declares to have lost. Otherwise, Bob claims to have won, and as proof he produces one prime factor of $n$ by computing $g=\\gcd(n,a+z)$. *(One can prove that in this situation $g$ is always one of the two prime factors of $n$.)*\n", 74 "\n", 75 "Since factoring a number without extra information is very hard, Alice will be convinced that she must have given Bob one of the square roots that he did not know, so she admits the loss.\n", 76 "\n", 77 "Now to the actual exercise:\n", 78 "\n", 79 "(a) Write the code for the functions A1, B1 and B2 as indicated in the cell below. The function A2 is already written.\n", 80 "\n", 81 "(b) Modify the functions B1, A2 and B2 to check that the opponent is not cheating. More precisely:\n", 82 "* In B1, Bob should check that $n$ is not a prime power. *(This is the only way Alice can try to cheat: if she sends Bob a number $n$ that is the product of more than two primes, than she has less than $50\\%$ chance of winning!)*\n", 83 "* In A2, Alice should check that $b$ is a square modulo $n$.\n", 84 "* In B2, Bob should check that $z^2\\equiv a^2\\pmod n$.\n", 85 "\n", 86 "In case cheating is detected, a message should be printed saying that the person is cheating." 87 ] 88 }, 89 { 90 "cell_type": "code", 91 "execution_count": 1, 92 "metadata": { 93 "collapsed": true 94 }, 95 "outputs": [ 96 { 97 "ename": "IndentationError", 98 "evalue": "expected an indented block (<ipython-input-1-60f510bf175f>, line 7)", 99 "output_type": "error", 100 "traceback": [ 101 "\u001b[1;36m File \u001b[1;32m\"<ipython-input-1-60f510bf175f>\"\u001b[1;36m, line \u001b[1;32m7\u001b[0m\n\u001b[1;33m def B1(n):\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m expected an indented block\n" 102 ] 103 } 104 ], 105 "source": [ 106 "# Alice needs this to compute the square roots\n", 107 "from sage.rings.finite_rings.integer_mod import square_root_mod_prime\n", 108 "\n", 109 "def A1():\n", 110 " # This function must return two distinct primes and their product.\n", 111 " \n", 112 "def B1(n):\n", 113 " # This function must return a random integer a\n", 114 " # with 1<a<n and gcd(a,n)=1.\n", 115 "\n", 116 "def A2(b, p, q):\n", 117 " x = ZZ(square_root_mod_prime(Integers(p)(b), p))\n", 118 " y = ZZ(square_root_mod_prime(Integers(q)(b), q))\n", 119 " return crt(x, y, p, q)\n", 120 "\n", 121 "def B2(a, z, n):\n", 122 " # This function must print out one of two messages:\n", 123 " # \"Bob has lost\" if z is congruent to a or -a modulo n.\n", 124 " # \"Bob has won, proof: \" followed by a prime factor of n otherwise.\n", 125 " # In this case the prime must be calculated as explained above.\n", 126 "\n", 127 "\n", 128 "# This is how the game plays out:\n", 129 "p, q, n = A1()\n", 130 "print(\"Alice picked n =\", n)\n", 131 "print(\"[[ Alice's secret:\", p, q, \"]]\")\n", 132 "a = B1(n)\n", 133 "b = a^2 % n\n", 134 "print(\"Bob picked b =\", b)\n", 135 "print(\"[[ Bob's secret:\", a, \"]]\")\n", 136 "z = A2(b, p, q)\n", 137 "print(\"Alice picked z =\", z)\n", 138 "B2(a, z, n)" 139 ] 140 }, 141 { 142 "cell_type": "markdown", 143 "metadata": {}, 144 "source": [ 145 "**Grading**\n", 146 "\n", 147 "This homework assignment is worth $20\\%$ of your final grade. Exercise 1 is worth 4 points (one for each part) and Exercise 2 is worth 12 points (8 points for part (a) and 4 points for part (b)), for a total of **16 points**." 148 ] 149 }, 150 { 151 "cell_type": "code", 152 "execution_count": null, 153 "metadata": {}, 154 "outputs": [], 155 "source": [] 156 } 157 ], 158 "metadata": { 159 "kernelspec": { 160 "display_name": "SageMath 9.2", 161 "language": "sage", 162 "name": "sagemath" 163 }, 164 "language_info": { 165 "codemirror_mode": { 166 "name": "ipython", 167 "version": 3 168 }, 169 "file_extension": ".py", 170 "mimetype": "text/x-python", 171 "name": "python", 172 "nbconvert_exporter": "python", 173 "pygments_lexer": "ipython3", 174 "version": "3.8.5" 175 } 176 }, 177 "nbformat": 4, 178 "nbformat_minor": 4 179 }