solution-checkpoint.ipynb (6547B)
1 { 2 "cells": [ 3 { 4 "cell_type": "markdown", 5 "metadata": {}, 6 "source": [ 7 "**Exercise 1**\n", 8 "\n", 9 "Use SageMath to solve the following problems:\n", 10 "\n", 11 "(a) Find the roots of the following polynomial over $\\mathbb Q$:\n", 12 "\\begin{align*}\n", 13 " 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", 14 "\\end{align*}\n", 15 "\n", 16 "(b) Find the roots of the same polynomial $p$ over $\\mathbb R$ and over $\\mathbb C$.\n", 17 "\n", 18 "(c) Find the determinant, the trace and the characteristic polynomial of the following matrix:\n", 19 "\\begin{align*}\n", 20 "A=\\left(\\begin{array}{rrrr}\n", 21 "-1 & 1 & -1 & 0 \\\\\n", 22 "1 & \\frac{1}{2} & 1 & 0 \\\\\n", 23 "\\frac{1}{2} & -\\frac{1}{2} & -2 & 1 \\\\\n", 24 "0 & 0 & 1 & 1\n", 25 "\\end{array}\\right)\n", 26 "\\end{align*}\n", 27 "\n", 28 "(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", 29 "\n", 30 "Write your code in the cell below." 31 ] 32 }, 33 { 34 "cell_type": "code", 35 "execution_count": null, 36 "metadata": {}, 37 "outputs": [], 38 "source": [] 39 }, 40 { 41 "cell_type": "markdown", 42 "metadata": {}, 43 "source": [ 44 "**Exercise 2**\n", 45 "\n", 46 "*(Yes, I know that the exercise is long to read, but it will not be so long to solve. It is a cool application of cryptography which, similarly to RSA, is based on the fact that factoring a number is hard.)*\n", 47 "\n", 48 "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 much time to set it up and they have exactly the same chances of winning.\n", 49 "\n", 50 "Unfortunately, due to the COVID-19 pandemic they cannot meet in person, and they despite being good friends they don't trust each other enough to play this game via videocall. Luckily, Alice is an expert in cryptography and she knows how to play this game using the Chinese remainder theorem.\n", 51 "\n", 52 "The game plays as follows:\n", 53 "\n", 54 "(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", 55 "\n", 56 "(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", 57 "\n", 58 "(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$. Then she sends $z$ to Bob.\n", 59 "\n", 60 "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", 61 "\\begin{align*}\\begin{cases}\n", 62 "z\\equiv \\pm x\\pmod p\\\\\n", 63 "z \\equiv \\pm y\\pmod q\n", 64 "\\end{cases}\\end{align*}\n", 65 "\n", 66 "One of those solutions is $a$ and another is $-a$, and Bob knows them. Since 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. She will win if she picks $\\pm a$:\n", 67 "\n", 68 "(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", 69 "\n", 70 "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", 71 "\n" 72 ] 73 }, 74 { 75 "cell_type": "code", 76 "execution_count": 12, 77 "metadata": {}, 78 "outputs": [ 79 { 80 "name": "stdout", 81 "output_type": "stream", 82 "text": [ 83 "Alice picked n = 239880868500983\n", 84 "(Alice's secret: 15489269 15486907 )\n", 85 "Bob picked b = 19945704365802\n", 86 "(Bob's secret: 3040890259756 )\n", 87 "Alice picked z = 3040890259756\n", 88 "Bob has lost\n" 89 ] 90 } 91 ], 92 "source": [ 93 "# Alice needs this to compute the square roots\n", 94 "from sage.rings.finite_rings.integer_mod import square_root_mod_prime\n", 95 "\n", 96 "def A1():\n", 97 " # This function must return two distinct primes and their product\n", 98 " p, q = 0, 0\n", 99 " while p == q:\n", 100 " p = Primes()[10^6+randint(1,1000)]\n", 101 " q = Primes()[10^6+randint(1,1000)]\n", 102 " return p, q, p*q\n", 103 " \n", 104 "def B1(n):\n", 105 " a = 0\n", 106 " while gcd(a,n) != 1:\n", 107 " a = randint(2,n-1)\n", 108 " return a\n", 109 "\n", 110 "def A2(b, p, q):\n", 111 " x = ZZ(square_root_mod_prime(Integers(p)(b), p))\n", 112 " y = ZZ(square_root_mod_prime(Integers(q)(b), q))\n", 113 " return crt(x, y, p, q)\n", 114 "\n", 115 "def B2(a, z, n):\n", 116 " # This function must print out one of two messages:\n", 117 " # \"Bob has lost\" if z is congruent to a or -a modulo n\n", 118 " # \"Bob has won, proof: \" followed by a prime factor of n otherwise\n", 119 " if a%n == z%n or a%n == (-z)%n:\n", 120 " print(\"Bob has lost\")\n", 121 " else:\n", 122 " print(\"Bob has won, proof:\", gcd(n, a+z))\n", 123 "\n", 124 "# This is how the game plays:\n", 125 "p, q, n = A1() # p and q are secret to Alice\n", 126 "print(\"Alice picked n =\", n)\n", 127 "print(\"(Alice's secret:\", p, q, \")\")\n", 128 "a = B1(n) # a is secret to Bob\n", 129 "b = a^2 % n\n", 130 "print(\"Bob picked b =\", b)\n", 131 "print(\"(Bob's secret:\", a, \")\")\n", 132 "z = A2(b, p, q)\n", 133 "print(\"Alice picked z =\", z)\n", 134 "B2(a, z, n)" 135 ] 136 }, 137 { 138 "cell_type": "code", 139 "execution_count": null, 140 "metadata": {}, 141 "outputs": [], 142 "source": [] 143 } 144 ], 145 "metadata": { 146 "kernelspec": { 147 "display_name": "SageMath 9.2", 148 "language": "sage", 149 "name": "sagemath" 150 }, 151 "language_info": { 152 "codemirror_mode": { 153 "name": "ipython", 154 "version": 3 155 }, 156 "file_extension": ".py", 157 "mimetype": "text/x-python", 158 "name": "python", 159 "nbconvert_exporter": "python", 160 "pygments_lexer": "ipython3", 161 "version": "3.8.5" 162 } 163 }, 164 "nbformat": 4, 165 "nbformat_minor": 4 166 }