Skip to content

cryptography #12391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

## Algorithms
* [Graph](algorithms/Graph.ipynb)

## Audio Filters
* [Butterworth Filter](audio_filters/butterworth_filter.py)
* [Iir Filter](audio_filters/iir_filter.py)
Expand Down Expand Up @@ -178,6 +181,12 @@
* [Volume Conversions](conversions/volume_conversions.py)
* [Weight Conversion](conversions/weight_conversion.py)

## Cryptography
* [Extended Eucledian](cryptography/extended_eucledian.ipynb)
* [Galois Field](cryptography/galois_field.ipynb)
* [Lfsr Bit Stream](cryptography/lfsr_bit_stream.ipynb)
* [Playfire](cryptography/playfire.ipynb)

## Data Structures
* Arrays
* [Equilibrium Index In Array](data_structures/arrays/equilibrium_index_in_array.py)
Expand Down
823 changes: 823 additions & 0 deletions algorithms/Graph.ipynb

Large diffs are not rendered by default.

207 changes: 207 additions & 0 deletions cryptography/extended_eucledian.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"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",
"execution_count": 7,
"id": "12f36b2f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"21"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"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",
" return a\n",
" r = a % b\n",
" if r == 0:\n",
" return b\n",
" return eucld_gcd(b, r)\n",
"\n",
"\n",
"eucld_gcd(252, 105)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "c060ee17",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, -48]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"\n",
"\n",
"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",
" swap = False\n",
" 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",
" return []\n",
" ls = []\n",
" while b != 1:\n",
" r = a % b\n",
" if r == 0:\n",
" return ls\n",
" idx = (a - r) // b\n",
" ls.append(idx)\n",
" a = b\n",
" b = r\n",
" return ls\n",
"\n",
" row = np.array([[1, 0], [0, 1]])\n",
" ls = eucld(a, b)\n",
" for i in ls:\n",
" row = np.append(row, [row[-2] - i * row[-1]], axis=0)\n",
"\n",
" if swap:\n",
" return list(row[-1])[::-1]\n",
"\n",
" return list(row[-1])\n",
"\n",
"\n",
"ext_eucld(97, 2)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"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",
"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
}
Loading
Loading