Browse Source

More latex please

master
Johannes Røsvik 3 years ago
parent
commit
da33a814e6
No known key found for this signature in database GPG Key ID: 8A47E30339E13FFD
  1. 138
      TTM4135.ipynb

138
TTM4135.ipynb

@ -10,7 +10,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 14,
"metadata": {},
"outputs": [
{
@ -19,7 +19,7 @@
"5"
]
},
"execution_count": 1,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
@ -46,7 +46,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 17,
"metadata": {},
"outputs": [
{
@ -72,7 +72,7 @@
" x = y1 - (b//a) * x1 \n",
" y = x1 \n",
"\n",
" return gcd,x,y \n",
" return gcd, x, y \n",
"\n",
"a, b = 35,10\n",
"g, x, y = gcdExtended(a, b)\n",
@ -92,7 +92,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 16,
"metadata": {},
"outputs": [
{
@ -130,7 +130,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 4,
"metadata": {},
"outputs": [
{
@ -171,6 +171,57 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Integer factorisation\n",
"Given an integer, find its prime factors"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The factors of 641361 are [3, 7, 7, 4363], verified\n",
"Manual test: True\n"
]
}
],
"source": [
"# Brute force solution\n",
"def prime_factors(n):\n",
" i = 2\n",
" factors = []\n",
" while i * i <= n:\n",
" if n % i:\n",
" i += 1\n",
" else:\n",
" n //= i\n",
" factors.append(i)\n",
" if n > 1:\n",
" factors.append(n)\n",
" return factors\n",
"\n",
"# Check if the numbers in array a is the factors of n\n",
"def factor_test(a, n):\n",
" k = 1\n",
" for i in factors:\n",
" k=k*i\n",
" return k==n\n",
"\n",
"n = 641361\n",
"factors = prime_factors(n)\n",
"print(f\"The factors of {n} are {factors}, {'verified' if factor_test(factors, n) else 'ERROR'}\")\n",
"\n",
"print(\"Manual test:\", factor_test([3, 7, 7, 4363], 641361))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Euler function φ and Z*n\n",
"From slides 2020-4135-l07 - Number Theory for Public Key Cryptography\n",
"\n",
@ -179,7 +230,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 6,
"metadata": {},
"outputs": [
{
@ -233,7 +284,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 7,
"metadata": {},
"outputs": [
{
@ -286,16 +337,16 @@
"- 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",
"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$"
"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 $f_1, f_2, ..., f_r$\n",
"2. then $g$ is a generator as long as $g^{\\frac{p−1}{f_i}} \\neq 1 \\mod(p)$ for $i = 1,2,,...,r$"
]
},
{
"cell_type": "code",
"execution_count": 52,
"execution_count": 8,
"metadata": {},
"outputs": [
{
@ -331,7 +382,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 9,
"metadata": {},
"outputs": [
{
@ -355,7 +406,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@ -409,7 +460,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 11,
"metadata": {},
"outputs": [
{
@ -486,57 +537,6 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Integer factorisation\n",
"Given an integer, find its prime factors"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The factors of 641361 are [3, 7, 7, 4363], verified\n",
"Manual test: True\n"
]
}
],
"source": [
"# Brute force solution\n",
"def prime_factors(n):\n",
" i = 2\n",
" factors = []\n",
" while i * i <= n:\n",
" if n % i:\n",
" i += 1\n",
" else:\n",
" n //= i\n",
" factors.append(i)\n",
" if n > 1:\n",
" factors.append(n)\n",
" return factors\n",
"\n",
"# Check if the numbers in array a is the factors of n\n",
"def factor_test(a, n):\n",
" k = 1\n",
" for i in factors:\n",
" k=k*i\n",
" return k==n\n",
"\n",
"n = 641361\n",
"factors = prime_factors(n)\n",
"print(f\"The factors of {n} are {factors}, {'verified' if factor_test(factors, n) else 'ERROR'}\")\n",
"\n",
"print(\"Manual test:\", factor_test([3, 7, 7, 4363], 641361))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Discrete logarithm problem\n",
"Given a prime `p` and an integer `y` with `0 < y < p`, find `x` such that \n",
"\n",
@ -552,7 +552,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 12,
"metadata": {},
"outputs": [
{
@ -629,7 +629,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 13,
"metadata": {},
"outputs": [
{

Loading…
Cancel
Save