Finding a generator of Z*p

This commit is contained in:
Johannes Røsvik 2020-05-23 17:55:29 +02:00
parent f0279f0a5d
commit f404704d7d
No known key found for this signature in database
GPG Key ID: 8A47E30339E13FFD
1 changed files with 50 additions and 7 deletions

View File

@ -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 Zp 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{p1}{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 Zp\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)}\")"
]
},
{