From f404704d7db219fea93932f6feb1d9f6c461ae31 Mon Sep 17 00:00:00 2001 From: Johannes Date: Sat, 23 May 2020 17:55:29 +0200 Subject: [PATCH] Finding a generator of Z*p --- TTM4135.ipynb | 57 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/TTM4135.ipynb b/TTM4135.ipynb index 84b87ad..20a28e2 100644 --- a/TTM4135.ipynb +++ b/TTM4135.ipynb @@ -130,7 +130,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 16, "metadata": {}, "outputs": [ { @@ -179,15 +179,15 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Ļ†(16) = 8 since the 8 items in Z*16 are each relatively prime to 16.\n", - "Z*16 = [1, 3, 5, 7, 9, 11, 13, 15]\n" + "Ļ†(21) = 12 since the 12 items in Z*21 are each relatively prime to 21.\n", + "Z*21 = [1, 2, 4, 5, 8, 10, 11, 13, 16, 17, 19, 20]\n" ] } ], @@ -206,7 +206,7 @@ " r.append(i)\n", " return r\n", "\n", - "n = 16\n", + "n = 21\n", "ar = z_star(n)\n", "e = len(ar)\n", "print(f\"Ļ†({n}) = {e} since the {e} items in Z*{n} are each relatively prime to {n}.\")\n", @@ -233,7 +233,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -275,7 +275,50 @@ " return (int)(result)\n", "\n", "n = 1125\n", - "print(f\"šœ‘({n}) = {totient(1125)}\")" + "print(f\"šœ‘({n}) = {totient(n)}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Finding a generator of Z*p\n", + "- https://crypto.stanford.edu/pbc/notes/numbertheory/gen.html\n", + "- Lecture 2, page 19\n", + "\n", + "A generator of $Z^āˆ—p$ is an element of order $p āˆ’ 1$\n", + "\n", + "To find a generator of Zāˆ—p we can choose a value g and test it as follows:\n", + "1. compute all the distinct prime factors of p āˆ’ 1 and call them f1,f2,...,fr\n", + "2. then g is a generator as long as $g^{\\frac{pāˆ’1}{fi}} \\neq 1 \\mod(p)$ for $i = 1,2,,...,r$" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Is 3 a generator for Z*4? True\n" + ] + } + ], + "source": [ + "# Finding a generator g of Zāˆ—p\n", + "def is_generator(p, g):\n", + " pf = prime_factors(p-1)\n", + " for f in pf:\n", + " if (g**((p-1)/f))%p == 1:\n", + " return False\n", + " return True\n", + "\n", + "g = 3 # Generator\n", + "p = 4 # Z*p\n", + "\n", + "print(f\"Is {g} a generator for Z*{p}? {is_generator(p, g)}\")" ] }, {