Python/cryptography/extended_eucledian.ipynb

208 lines
4.5 KiB
Plaintext
Raw Normal View History

2024-11-20 17:22:43 +00:00
{
"cells": [
{
"cell_type": "code",
2024-11-20 17:43:11 +00:00
"execution_count": 8,
2024-11-20 17:22:43 +00:00
"id": "8561e0a7",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style>.container{width:100%}</style>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%HTML\n",
"<style>.container{width:100%}</style>"
]
},
{
"cell_type": "code",
2024-11-20 17:43:11 +00:00
"execution_count": 7,
2024-11-20 17:22:43 +00:00
"id": "12f36b2f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"21"
]
},
2024-11-20 17:43:11 +00:00
"execution_count": 7,
2024-11-20 17:22:43 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2024-11-20 17:43:11 +00:00
"def eucld_gcd(a, b):\n",
" \"\"\"\n",
" Computes the greatest common divisor (GCD) of two integers using the Euclidean algorithm.\n",
"\n",
" Parameters:\n",
" a (int): The first integer.\n",
" b (int): The second integer.\n",
"\n",
" Returns:\n",
" int: The greatest common divisor of a and b.\n",
" \"\"\"\n",
" if a < b:\n",
" a, b = b, a\n",
" if b == 0:\n",
2024-11-20 17:22:43 +00:00
" return a\n",
2024-11-20 17:43:11 +00:00
" r = a % b\n",
" if r == 0:\n",
2024-11-20 17:22:43 +00:00
" return b\n",
2024-11-20 17:43:11 +00:00
" return eucld_gcd(b, r)\n",
"\n",
"\n",
"eucld_gcd(252, 105)"
2024-11-20 17:22:43 +00:00
]
},
{
"cell_type": "code",
2024-11-20 17:43:11 +00:00
"execution_count": 9,
2024-11-20 17:22:43 +00:00
"id": "c060ee17",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, -48]"
]
},
2024-11-20 17:43:11 +00:00
"execution_count": 9,
2024-11-20 17:22:43 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
2024-11-20 17:43:11 +00:00
"\n",
"\n",
2024-11-20 17:43:11 +00:00
"def ext_eucld(a, b):\n",
" \"\"\"\n",
" Computes the extended Euclidean algorithm to find the greatest common divisor (GCD)\n",
" of two integers, and also the coefficients (x, y) of the equation:\n",
" a*x + b*y = GCD(a, b)\n",
"\n",
" This method returns the coefficients (x, y) such that a*x + b*y = GCD(a, b).\n",
"\n",
" Parameters:\n",
" a (int): The first integer.\n",
" b (int): The second integer.\n",
"\n",
" Returns:\n",
" list: A list of two integers [x, y] where x and y are the coefficients for the linear\n",
" combination of a and b that equals their GCD.\n",
" \"\"\"\n",
2024-11-20 17:22:43 +00:00
" swap = False\n",
2024-11-20 17:43:11 +00:00
" if a < b:\n",
" a, b = b, a\n",
" swap = True\n",
"\n",
" def eucld(a, b):\n",
" if b == 0 or b == 1:\n",
2024-11-20 17:22:43 +00:00
" return []\n",
" ls = []\n",
2024-11-20 17:43:11 +00:00
" while b != 1:\n",
" r = a % b\n",
" if r == 0:\n",
2024-11-20 17:22:43 +00:00
" return ls\n",
2024-11-20 17:43:11 +00:00
" idx = (a - r) // b\n",
2024-11-20 17:22:43 +00:00
" ls.append(idx)\n",
" a = b\n",
" b = r\n",
" return ls\n",
2024-11-20 17:43:11 +00:00
"\n",
" row = np.array([[1, 0], [0, 1]])\n",
" ls = eucld(a, b)\n",
2024-11-20 17:22:43 +00:00
" for i in ls:\n",
2024-11-20 17:43:11 +00:00
" row = np.append(row, [row[-2] - i * row[-1]], axis=0)\n",
"\n",
2024-11-20 17:43:11 +00:00
" if swap:\n",
2024-11-20 17:22:43 +00:00
" return list(row[-1])[::-1]\n",
"\n",
2024-11-20 17:22:43 +00:00
" return list(row[-1])\n",
2024-11-20 17:43:11 +00:00
"\n",
"\n",
"ext_eucld(97, 2)"
2024-11-20 17:22:43 +00:00
]
},
{
"cell_type": "code",
2024-11-20 17:43:11 +00:00
"execution_count": 10,
2024-11-20 17:22:43 +00:00
"id": "d3edd1f6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a=5, b=7, gcd=1, m=3, n=-2, ma+nb=1\n"
]
}
],
"source": [
"a = 5\n",
"b = 7\n",
"gcd = eucld_gcd(a, b)\n",
"m, n = ext_eucld(a, b)\n",
2024-11-20 17:22:43 +00:00
"print(f\"a={a}, b={b}, gcd={gcd}, m={m}, n={n}, ma+nb={m*a+n*b}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "782bb8d3",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "e224063e",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "63f1d5a4",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}