[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2024-11-20 17:47:23 +00:00
parent 730ba18e89
commit ffbe3bbd86
5 changed files with 391 additions and 344 deletions

View File

@ -165,7 +165,6 @@
"source": [
"class Graph:\n",
" def __init__(self, adj_list: dict = {}, name=\"Graph\", directed=False):\n",
" \n",
" self.plt = plt\n",
"\n",
" self.nx = nx\n",
@ -175,8 +174,7 @@
" for i in adj_list:\n",
" tmp.extend(adj_list[i])\n",
"\n",
" \n",
" if(not directed and set(tmp)==set(adj_list.keys())):\n",
" if not directed and set(tmp) == set(adj_list.keys()):\n",
" self.adj_list = adj_list\n",
" self.directed = directed\n",
" self.name = name\n",
@ -186,23 +184,27 @@
" def __str__(self):\n",
" string = \" \" + self.name + \"\\n\"\n",
" for i in self.adj_list.keys():\n",
" string +=str(i)+\" : [\"+\",\".join([str(j) for j in self.adj_list[i]])+\"]\\n\"\n",
" string += (\n",
" str(i) + \" : [\" + \",\".join([str(j) for j in self.adj_list[i]]) + \"]\\n\"\n",
" )\n",
" return string\n",
"\n",
" \n",
" def add_node(self, node):\n",
" if node not in self.adj_list.keys():\n",
" self.adj_list[node] = []\n",
" else:\n",
" print(f\"Warning : the node'{node}' already exists\")\n",
"\n",
" def add_edge(self, node1, node2):\n",
" graph = self.adj_list\n",
" if(node1 in graph and node2 in graph):\n",
" if node1 in graph and node2 in graph:\n",
" graph[node1].append(node2)\n",
" if(not self.directed):\n",
" if not self.directed:\n",
" graph[node2].append(node1)\n",
"\n",
" def get_nodeSet(self):\n",
" return list(self.adj_list.keys())\n",
"\n",
" def get_edgSet(self):\n",
" edgSet = []\n",
" for i in self.adj_list.keys():\n",
@ -210,15 +212,14 @@
" edgSet.append(tuple([i, j]))\n",
" return edgSet\n",
"\n",
" \n",
" def dfs(self, node):\n",
" if(node in self.adj_list.keys()):\n",
" if node in self.adj_list.keys():\n",
" traverse = []\n",
" visited = set()\n",
" stack = [node]\n",
" while(stack):\n",
" while stack:\n",
" current = stack.pop()\n",
" if(current not in visited):\n",
" if current not in visited:\n",
" visited.add(current)\n",
" traverse.append(current)\n",
" stack.extend(self.adj_list[current])\n",
@ -228,13 +229,13 @@
" print(f\"Error : node'{node}' does not exist in graph\")\n",
"\n",
" def bfs(self, node):\n",
" if(node in self.adj_list.keys()):\n",
" if node in self.adj_list.keys():\n",
" traverse = []\n",
" visited = set()\n",
" queue = [node]\n",
" while(queue):\n",
" while queue:\n",
" current = queue.pop(0)\n",
" if(current not in visited):\n",
" if current not in visited:\n",
" visited.add(current)\n",
" traverse.append(current)\n",
" queue.extend(self.adj_list[current])\n",
@ -248,9 +249,9 @@
" path += [n1]\n",
" if n2 not in g[n1]:\n",
" for i in g[n1]:\n",
" if(i not in path):\n",
" if i not in path:\n",
" path1 = self.getPath(i, n2, path.copy())\n",
" if(path1):\n",
" if path1:\n",
" return path1\n",
" else:\n",
" return path + [n2]\n",
@ -262,13 +263,14 @@
" t = 2\n",
" for i in range(l):\n",
" for j in range(i + 2, l):\n",
" if(abs(j-i)>t):\n",
" if abs(j - i) > t:\n",
" n1 = path[i]\n",
" n2 = path[j]\n",
" if(n2 in g[n1]):\n",
" if n2 in g[n1]:\n",
" path1 = path[: i + 1] + path[j:]\n",
" t = abs(j - i)\n",
" return path1\n",
"\n",
" def sortestPath(self, n1, n2):\n",
" path = self.getPath(n1, n2)\n",
" spath = self.opt_path(path)\n",
@ -281,9 +283,10 @@
" self.nx.draw_networkx(G)\n",
" self.plt.title(self.name)\n",
" self.plt.show()\n",
"\n",
" def makeTree(self, Root=None):\n",
" temp_ls = dict()\n",
" if((Root == None) or (Root not in self.adj_list.keys())):\n",
" if (Root == None) or (Root not in self.adj_list.keys()):\n",
" root = list(self.adj_list.keys())[0]\n",
" else:\n",
" root = Root\n",
@ -303,6 +306,7 @@
"\n",
" return temp_ls\n",
"\n",
"\n",
"adj_list = {\n",
" 0: [3, 5, 9, 10, 1],\n",
" 1: [6, 7, 4, 0, 10],\n",
@ -314,14 +318,13 @@
" 7: [1],\n",
" 8: [4],\n",
" 9: [0],\n",
" 10:[2,0]\n",
" 10: [2, 0],\n",
"}\n",
"g = Graph(adj_list, name=\"Demo\")\n",
"g.display()\n",
"for i in adj_list:\n",
" \n",
" g1 = Graph(g.makeTree(Root=i), name=\"Demo tree_\" + str(i))\n",
" g1.display()\n"
" g1.display()"
]
},
{
@ -392,33 +395,37 @@
" def __init__(self, data, parent=None):\n",
" self.data = data\n",
" self.childs = []\n",
"\n",
" def addChild(self, *nodes):\n",
" childs = [Tree(i, parent=self) for i in nodes]\n",
" self.childs.extend(childs)\n",
" if (len(childs)==1):\n",
" if len(childs) == 1:\n",
" return childs[0]\n",
" return childs\n",
"\n",
" def getChilds(self):\n",
" return [i.data for i in self.childs]\n",
"\n",
" def getElements(self):\n",
" elements = [self.data]\n",
" for i in self.childs:\n",
" elements.extend(i.getElements())\n",
" return elements\n",
"\n",
" def traverse(self, order=\"pre\"):\n",
" ls = []\n",
" if(order==\"pre\"):\n",
" if order == \"pre\":\n",
" ls.append(self.data)\n",
" for i in self.childs:\n",
" ls.extend(i.traverse(order=order))\n",
" return ls\n",
" elif(order==\"post\"):\n",
" elif order == \"post\":\n",
" for i in self.childs:\n",
" ls.extend(i.traverse(order=order))\n",
" ls.append(self.data)\n",
" return ls\n",
"\n",
"\n",
"tree = Tree(1)\n",
"t2 = tree.addChild(2)\n",
"t2t3 = t2.addChild(2, 3, 4, 5, 1, 7, 9)\n",
@ -472,8 +479,8 @@
"\n",
" def getElement(self, idx):\n",
" path = []\n",
" while(idx>0):\n",
" if(idx%2==1):\n",
" while idx > 0:\n",
" if idx % 2 == 1:\n",
" path.append((idx % 2))\n",
" idx //= 2\n",
" else:\n",
@ -482,11 +489,11 @@
" temp = self\n",
" while path and temp:\n",
" dr = path.pop()\n",
" if(dr==1):\n",
" if dr == 1:\n",
" temp = temp.left\n",
" else:\n",
" temp = temp.right\n",
" if(temp):\n",
" if temp:\n",
" return temp\n",
" else:\n",
" print(\"id does not exists\")\n",
@ -495,10 +502,11 @@
" ld = self.left.getDepth() if self.left else -1\n",
" rd = self.right.getDepth() if self.right else -1\n",
" return max(ld, rd) + 1\n",
"\n",
" def isBalanced(self):\n",
" ld = self.left.getDepth() if self.left else -1\n",
" rd = self.right.getDepth() if self.right else -1\n",
" if(abs(ld-rd) in [0,1]):\n",
" if abs(ld - rd) in [0, 1]:\n",
" lb = self.left.isBalanced() if self.left else True\n",
" rb = self.right.isBalanced() if self.right else True\n",
" return bool(lb * rb)\n",
@ -510,8 +518,8 @@
" return [self.left, self.right]\n",
"\n",
" def getId(self):\n",
" if(self.parent != None):\n",
" if(self == self.parent.left):\n",
" if self.parent != None:\n",
" if self == self.parent.left:\n",
" return self.parent.getId() * 2 + 1\n",
" else:\n",
" return self.parent.getId() * 2 + 2\n",
@ -520,82 +528,80 @@
"\n",
" def __setattr__(self, key, value):\n",
" self.__dict__[key] = value\n",
" if(value):\n",
" if(key in [\"left\", \"right\"]):\n",
" if value:\n",
" if key in [\"left\", \"right\"]:\n",
" value.parent = self\n",
"\n",
" \n",
" def getSize(self):\n",
" lsize = self.left.getSize() if self.left else 0\n",
" rsize = self.right.getSize() if self.right else 0\n",
" return lsize + 1 + rsize\n",
"\n",
" def addData(self, data, idx=-1):\n",
" if(idx==-1):\n",
" if idx == -1:\n",
" q = []\n",
" q.append(self)\n",
" while (len(q)): \n",
" while len(q):\n",
" temp = q[0]\n",
" q.pop(0)\n",
" if (not temp.left):\n",
" if not temp.left:\n",
" temp.left = BTree(data, parent=temp)\n",
" break\n",
" else:\n",
" q.append(temp.left)\n",
" if (not temp.right):\n",
" if not temp.right:\n",
" temp.right = BTree(data, parent=temp)\n",
" break\n",
" else:\n",
" q.append(temp.right)\n",
" else:\n",
" dr = idx % 2\n",
" if(dr==1):\n",
" if dr == 1:\n",
" ele = self.getElement(idx // 2)\n",
" ele.left = BTree(data, parent=ele)\n",
" else:\n",
" ele = self.getElement(idx // 2 - 1)\n",
" ele.right = BTree(data, parent=ele)\n",
"\n",
" def addNode(self, node, idx=-1):\n",
" if(idx==-1):\n",
" if idx == -1:\n",
" q = []\n",
" q.append(self)\n",
" while (len(q)): \n",
" while len(q):\n",
" temp = q[0]\n",
" q.pop(0)\n",
" if (not temp.left):\n",
" if not temp.left:\n",
" temp.left = node\n",
" break\n",
" else:\n",
" q.append(temp.left)\n",
" if (not temp.right):\n",
" if not temp.right:\n",
" temp.right = node\n",
" break\n",
" else:\n",
" q.append(temp.right)\n",
"\n",
" \n",
" \n",
" def traverse(self, order=\"pre\"):\n",
" ls = []\n",
" if(order==\"pre\"):\n",
" if order == \"pre\":\n",
" ls.append(self.data)\n",
" if(self.left):\n",
" if self.left:\n",
" ls.extend(self.left.traverse(order))\n",
" if(self.right):\n",
" if self.right:\n",
" ls.extend(self.right.traverse(order))\n",
" return ls\n",
" elif(order==\"post\"):\n",
" if(self.left):\n",
" elif order == \"post\":\n",
" if self.left:\n",
" ls.extend(self.left.traverse(order))\n",
" if(self.right):\n",
" if self.right:\n",
" ls.extend(self.right.traverse(order))\n",
" ls.append(self.data)\n",
" return ls\n",
" elif(order==\"in\"):\n",
" if(self.left):\n",
" elif order == \"in\":\n",
" if self.left:\n",
" ls.extend(self.left.traverse(order))\n",
" ls.append(self.data)\n",
" if(self.right):\n",
" if self.right:\n",
" ls.extend(self.right.traverse(order))\n",
" return ls"
]
@ -626,7 +632,6 @@
"bt.right.left = BTree(5)\n",
"\n",
"\n",
"\n",
"# bt.getElement(5)\n",
"bt.traverse(\"post\")"
]

View File

@ -62,6 +62,7 @@
" return b\n",
" return eucld_gcd(b, r)\n",
"\n",
"\n",
"eucld_gcd(252, 105)"
]
},
@ -85,6 +86,7 @@
"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",
@ -130,7 +132,8 @@
"\n",
" return list(row[-1])\n",
"\n",
"ext_eucld(97, 2)\n"
"\n",
"ext_eucld(97, 2)"
]
},
{

View File

@ -60,13 +60,14 @@
" Returns:\n",
" list: A list of bits representing the binary values.\n",
" \"\"\"\n",
" if(len(ls) == 0):\n",
" if len(ls) == 0:\n",
" return [0, 0, 0, 0, 0, 0, 0, 0] # Return a default 8-bit array\n",
" aa = [0 for _ in range(max(ls) + 1)]\n",
" for i in ls:\n",
" aa[i] = 1\n",
" return aa[::-1]\n",
"\n",
"\n",
"def bit2id(ls: list, log=False):\n",
" \"\"\"\n",
" Converts a binary list (bit vector) back to a list of indices.\n",
@ -85,10 +86,11 @@
" aa = []\n",
"\n",
" for i in range(len(ls)):\n",
" if(ls[i] == 1):\n",
" if ls[i] == 1:\n",
" aa.append(i)\n",
" return aa[::-1]\n",
"\n",
"\n",
"def bit2mul(a, b, log=False):\n",
" \"\"\"\n",
" Multiplies two binary numbers represented as lists of bits.\n",
@ -108,9 +110,9 @@
" bi = bit2id(b)\n",
" a, b = a[::-1], b[::-1]\n",
"\n",
" if(ai == []):\n",
" if ai == []:\n",
" return a\n",
" elif(bi == []):\n",
" elif bi == []:\n",
" return b\n",
"\n",
" addn = [[ai[i] + bi[j] for j in range(len(bi))][::-1] for i in range(len(ai))][::-1]\n",
@ -118,7 +120,7 @@
"\n",
" maxsiz = max([len(i) for i in addn])\n",
" for i in range(len(addn)):\n",
" if(len(addn[i]) < maxsiz):\n",
" if len(addn[i]) < maxsiz:\n",
" addn[i] = [0 for _ in range(maxsiz - len(addn[i]))] + addn[i]\n",
"\n",
" smm = []\n",
@ -130,6 +132,7 @@
"\n",
" return smm\n",
"\n",
"\n",
"def bit2add(a, b):\n",
" \"\"\"\n",
" Adds two binary numbers represented as lists of bits (bitwise addition).\n",
@ -147,9 +150,9 @@
" a, b = a[::-1], b[::-1]\n",
" maxsiz = max(len(a), len(b))\n",
"\n",
" if(len(a) < maxsiz):\n",
" if len(a) < maxsiz:\n",
" a = a + [0 for _ in range(maxsiz - len(a))]\n",
" if(len(b) < maxsiz):\n",
" if len(b) < maxsiz:\n",
" b = b + [0 for _ in range(maxsiz - len(b))]\n",
"\n",
" smm = []\n",
@ -158,6 +161,7 @@
"\n",
" return smm[::-1]\n",
"\n",
"\n",
"def bit2str(bit: list):\n",
" \"\"\"\n",
" Converts a list of bits into a string.\n",
@ -175,6 +179,7 @@
" s += str(i)\n",
" return s\n",
"\n",
"\n",
"def str2bit(s: str):\n",
" \"\"\"\n",
" Converts a string of '0's and '1's into a list of bits.\n",
@ -190,12 +195,13 @@
" Raises:\n",
" ValueError: If the string contains characters other than '0' and '1'.\n",
" \"\"\"\n",
" if(set(s).issubset(set('01'))):\n",
" if set(s).issubset(set(\"01\")):\n",
" bit = [int(i) for i in s]\n",
" return bit\n",
" else:\n",
" print(\"bit string should contain 1s and 0s\")\n",
"\n",
"\n",
"def modgf(dsr: list, dnt=[1, 0, 0, 0, 1, 1, 0, 1, 1]):\n",
" \"\"\"\n",
" Performs polynomial division over Galois Field (GF(2)).\n",
@ -214,7 +220,7 @@
" dnt = bit2id(dnt)\n",
" qtnt = []\n",
"\n",
" while (len(dnt) != 0 and len(dsr) != 0 and (max(dnt) - max(dsr) >= 0)):\n",
" while len(dnt) != 0 and len(dsr) != 0 and (max(dnt) - max(dsr) >= 0):\n",
" ml = max(dnt) - max(dsr)\n",
" qtnt.append(ml)\n",
" plus = id2bit(dnt)\n",
@ -224,6 +230,7 @@
"\n",
" return id2bit(dnt), id2bit(qtnt)\n",
"\n",
"\n",
"def ext_eucld(a, b, log=False):\n",
" \"\"\"\n",
" Extended Euclidean algorithm for binary polynomials.\n",
@ -240,16 +247,16 @@
" list: The coefficients of the linear combination of `a` and `b` (as lists of bits).\n",
" \"\"\"\n",
" ai, bi = bit2id(a), bit2id(b)\n",
" if((len(ai) != 0 and len(bi) != 0)):\n",
" if(max(max(ai), max(bi)) == max(bi)):\n",
" if len(ai) != 0 and len(bi) != 0:\n",
" if max(max(ai), max(bi)) == max(bi):\n",
" a, b = b, a\n",
" elif(len(ai) == 0 and len(bi) != 0):\n",
" elif len(ai) == 0 and len(bi) != 0:\n",
" a, b = b, a\n",
"\n",
" def eucld(a, b, log=False):\n",
" a, b = a[::-1], b[::-1]\n",
"\n",
" if(set(b) == set([0]) or (b[0] == 1 and (set(b[1:]) == set([0])))):\n",
" if set(b) == set([0]) or (b[0] == 1 and (set(b[1:]) == set([0]))):\n",
" return []\n",
"\n",
" ls = []\n",
@ -258,7 +265,7 @@
" r, idx = modgf(b[::-1], dnt=a[::-1])\n",
" r, idx = r[::-1], idx[::-1]\n",
"\n",
" if(set(r) == set([0])):\n",
" if set(r) == set([0]):\n",
" return ls\n",
"\n",
" ls.append(idx[::-1])\n",
@ -266,8 +273,10 @@
" b = r\n",
" return ls\n",
"\n",
" row = [[[0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0]],\n",
" [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1]]]\n",
" row = [\n",
" [[0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0]],\n",
" [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1]],\n",
" ]\n",
"\n",
" ls = eucld(a, b)\n",
" for i in ls:\n",
@ -280,6 +289,7 @@
"\n",
" return row[-1]\n",
"\n",
"\n",
"def Gfinv(bit, irrpoly=[1, 0, 0, 0, 1, 1, 0, 1, 1]):\n",
" \"\"\"\n",
" Computes the multiplicative inverse of a binary polynomial over GF(2).\n",
@ -294,15 +304,16 @@
" Returns:\n",
" list: The multiplicative inverse of the polynomial `bit` (list of bits).\n",
" \"\"\"\n",
" if(set(bit) == set('0')):\n",
" return '--'\n",
" if set(bit) == set(\"0\"):\n",
" return \"--\"\n",
"\n",
" ans = ext_eucld(irrpoly, bit)\n",
" ans = ans[-1][-len(bit) :]\n",
" return ans\n",
"\n",
"\n",
"# Example call\n",
"Gfinv([0, 0, 0, 0, 0, 1, 0, 0], irrpoly=[0, 0, 0, 1, 0, 0, 1, 1])\n"
"Gfinv([0, 0, 0, 0, 0, 1, 0, 0], irrpoly=[0, 0, 0, 1, 0, 0, 1, 1])"
]
},
{
@ -349,7 +360,7 @@
" # Ensure each element has length n (pad with leading zeros if necessary)\n",
" for i in range(len(gf)):\n",
" if len(gf[i]) < n:\n",
" gf[i] = '0' * (n - len(gf[i])) + gf[i]\n",
" gf[i] = \"0\" * (n - len(gf[i])) + gf[i]\n",
"\n",
" # Create mappings: index -> element (key2ele) and element -> index (ele2key)\n",
" key2ele = dict(enumerate(gf))\n",
@ -358,11 +369,13 @@
" # Generate the inverse map for all non-zero elements\n",
" invmap = dict()\n",
" for i in gf:\n",
" if set(i) != set('0'): # Skip zero element\n",
" if set(i) != set(\"0\"): # Skip zero element\n",
" inv = bit2str(Gfinv(str2bit(i), irrpoly=irrpoly)) # Find the inverse of i\n",
" invmap[ele2key[i]] = ele2key[inv] # Map the inverse using element-to-key mapping\n",
" invmap[ele2key[i]] = ele2key[\n",
" inv\n",
" ] # Map the inverse using element-to-key mapping\n",
"\n",
" return gf, invmap\n"
" return gf, invmap"
]
},
{
@ -464,15 +477,14 @@
"source": [
"gf28 = [str(bin(i))[2:] for i in range(256)]\n",
"for i in range(len(gf28)):\n",
" if(len(gf28[i])<8):\n",
" gf28[i] = '0'*(8-len(gf28[i])) + gf28[i]\n",
" if len(gf28[i]) < 8:\n",
" gf28[i] = \"0\" * (8 - len(gf28[i])) + gf28[i]\n",
"\n",
"key2ele = dict(enumerate(gf28))\n",
"ele2key = dict([i[::-1] for i in list(enumerate(gf28))])\n",
"invmap = dict()\n",
"for i in gf28:\n",
"\n",
" if(set(i)!=set('0')):\n",
" if set(i) != set(\"0\"):\n",
" inv = bit2str(Gfinv(str2bit(i)))\n",
" invmap[ele2key[i]] = ele2key[inv]"
]

View File

@ -208,7 +208,9 @@
" Returns:\n",
" int: The next bit in the keystream generated by the A51 cipher.\n",
" \"\"\"\n",
" majority = self.majority(*[self.lfsrs[i].seq[self.clock_bits[i]] for i in range(self.lfsr_count)])\n",
" majority = self.majority(\n",
" *[self.lfsrs[i].seq[self.clock_bits[i]] for i in range(self.lfsr_count)]\n",
" )\n",
" for i in range(self.lfsr_count):\n",
" if self.lfsrs[i].seq[self.clock_bits[i]] == majority:\n",
" self.lfsrs[i].clock()\n",
@ -224,7 +226,7 @@
"\n",
"# Generate a keystream of 10 bits\n",
"stream = [a51.clock() for i in range(10)]\n",
"stream\n"
"stream"
]
},
{
@ -236,6 +238,7 @@
"source": [
"import os\n",
"\n",
"\n",
"def write2txt_file(bitstream, filename):\n",
" \"\"\"\n",
" Writes a bitstream (string of '0's and '1's) to a text file.\n",
@ -246,7 +249,7 @@
" bitstream (str): A string of '0's and '1's representing the bitstream to be written.\n",
" filename (str): The path to the text file where the bitstream will be written.\n",
" \"\"\"\n",
" with open(filename, 'a') as f: # Open in append mode to continue writing\n",
" with open(filename, \"a\") as f: # Open in append mode to continue writing\n",
" f.write(bitstream)\n",
"\n",
"\n",
@ -265,14 +268,14 @@
"\n",
" # Pad the bitstream if it's not a multiple of 8\n",
" padding = (8 - (len(bitstream) % 8)) % 8\n",
" bitstream += '0' * padding # Add extra '0's to make the length a multiple of 8\n",
" bitstream += \"0\" * padding # Add extra '0's to make the length a multiple of 8\n",
"\n",
" for i in range(0, len(bitstream), 8):\n",
" byte = bitstream[i : i + 8]\n",
" byte_list.append(int(byte, 2)) # Convert 8 bits to an integer (byte)\n",
"\n",
" # Append the bytes to the binary file\n",
" with open(filename, 'ab') as f: # 'ab' mode to append to the binary file\n",
" with open(filename, \"ab\") as f: # 'ab' mode to append to the binary file\n",
" f.write(bytearray(byte_list))\n",
"\n",
"\n",
@ -298,7 +301,9 @@
"\n",
" current_size = 0\n",
" bitstream_chunk = \"\" # Chunk of bits to write periodically\n",
" chunk_size = 10000 # Number of bits to generate at a time (can adjust for performance)\n",
" chunk_size = (\n",
" 10000 # Number of bits to generate at a time (can adjust for performance)\n",
" )\n",
" progress_interval = target_size // 10 # 1/10th of the target size (100 MB)\n",
" next_progress_checkpoint = progress_interval\n",
"\n",
@ -317,10 +322,14 @@
"\n",
" # Check if the file size has crossed the 1/10th checkpoint\n",
" if current_size >= next_progress_checkpoint:\n",
" print(f\"File size crossed {round(next_progress_checkpoint / (1024 * 1024), 2)} MB\")\n",
" next_progress_checkpoint += progress_interval # Update to next 10% checkpoint\n",
" print(\n",
" f\"File size crossed {round(next_progress_checkpoint / (1024 * 1024), 2)} MB\"\n",
" )\n",
" next_progress_checkpoint += (\n",
" progress_interval # Update to next 10% checkpoint\n",
" )\n",
"\n",
" print(f\"File generation complete: {file_path} (target)\")\n"
" print(f\"File generation complete: {file_path} (target)\")"
]
},
{
@ -342,7 +351,7 @@
"data = [\n",
" {\"start\": [0, 1, 0, 1, 1], \"poly\": id2bit([5, 2, 0]), \"clock\": 2},\n",
" {\"start\": [1, 0, 0, 1, 0], \"poly\": id2bit([5, 4, 3, 1, 0]), \"clock\": 3},\n",
" {\"start\": [0, 1, 1, 0, 0], \"poly\": id2bit([5, 4, 2, 1, 0]), \"clock\": 2}\n",
" {\"start\": [0, 1, 1, 0, 0], \"poly\": id2bit([5, 4, 2, 1, 0]), \"clock\": 2},\n",
"]\n",
"gen_bit_stream(data, target_size=1 * 1024**2, file_path=\"mine_gen_100MB.txt\")"
]

View File

@ -51,7 +51,7 @@
" decrypt(msg): Decrypts the given encrypted message using the Playfair cipher.\n",
" \"\"\"\n",
"\n",
" def __init__(self, key, extra='x'):\n",
" def __init__(self, key, extra=\"x\"):\n",
" \"\"\"\n",
" Initializes the PlayFire cipher with a key and an optional extra character for padding.\n",
"\n",
@ -75,9 +75,9 @@
" \"\"\"\n",
" keyy = []\n",
" for i in key:\n",
" if(i not in keyy):\n",
" if i not in keyy:\n",
" keyy.append(i)\n",
" if(len(set(key)) == len(key)):\n",
" if len(set(key)) == len(key):\n",
" return key\n",
" else:\n",
" print(\"key Error\")\n",
@ -113,13 +113,13 @@
" idxs = [6, 6]\n",
" for i in range(6):\n",
" for j in range(6):\n",
" if(i == 5):\n",
" if i == 5:\n",
" i = -1\n",
" if(j == 5):\n",
" if j == 5:\n",
" j = -1\n",
" if(pair[0] == self.key_matrix[i][j]):\n",
" if pair[0] == self.key_matrix[i][j]:\n",
" idxs[0] = [i, j]\n",
" if(pair[1] == self.key_matrix[i][j]):\n",
" if pair[1] == self.key_matrix[i][j]:\n",
" idxs[1] = [i, j]\n",
" return idxs\n",
"\n",
@ -134,7 +134,7 @@
" str: The encrypted message.\n",
" \"\"\"\n",
" msg = list(msg.lower())\n",
" if(len(msg) % 2 == 1):\n",
" if len(msg) % 2 == 1:\n",
" msg.append(self.extra)\n",
" pairs = []\n",
" for i in range(0, len(msg), 2):\n",
@ -142,12 +142,21 @@
" en_msg = \"\"\n",
" for i in pairs:\n",
" idxs = self.find_idx(i)\n",
" if(idxs[0][0] == idxs[1][0]):\n",
" en_m = self.key_matrix[idxs[0][0]][idxs[0][1] + 1] + self.key_matrix[idxs[0][0]][idxs[1][1] + 1]\n",
" elif(idxs[0][1] == idxs[1][1]):\n",
" en_m = self.key_matrix[idxs[0][0] + 1][idxs[0][1]] + self.key_matrix[idxs[1][0] + 1][idxs[1][1]]\n",
" if idxs[0][0] == idxs[1][0]:\n",
" en_m = (\n",
" self.key_matrix[idxs[0][0]][idxs[0][1] + 1]\n",
" + self.key_matrix[idxs[0][0]][idxs[1][1] + 1]\n",
" )\n",
" elif idxs[0][1] == idxs[1][1]:\n",
" en_m = (\n",
" self.key_matrix[idxs[0][0] + 1][idxs[0][1]]\n",
" + self.key_matrix[idxs[1][0] + 1][idxs[1][1]]\n",
" )\n",
" else:\n",
" en_m = self.key_matrix[idxs[0][0]][idxs[1][1]] + self.key_matrix[idxs[1][0]][idxs[0][1]]\n",
" en_m = (\n",
" self.key_matrix[idxs[0][0]][idxs[1][1]]\n",
" + self.key_matrix[idxs[1][0]][idxs[0][1]]\n",
" )\n",
" en_msg += en_m\n",
" return en_msg\n",
"\n",
@ -162,7 +171,7 @@
" str: The decrypted plaintext message.\n",
" \"\"\"\n",
" msg = list(msg.lower())\n",
" if(len(msg) % 2 == 1):\n",
" if len(msg) % 2 == 1:\n",
" msg.append(self.extra)\n",
" pairs = []\n",
" for i in range(0, len(msg), 2):\n",
@ -170,14 +179,23 @@
" en_msg = \"\"\n",
" for i in pairs:\n",
" idxs = self.find_idx(i)\n",
" if(idxs[0][0] == idxs[1][0]):\n",
" en_m = self.key_matrix[idxs[0][0]][idxs[0][1] - 1] + self.key_matrix[idxs[0][0]][idxs[1][1] - 1]\n",
" elif(idxs[0][1] == idxs[1][1]):\n",
" en_m = self.key_matrix[idxs[0][0] - 1][idxs[0][1]] + self.key_matrix[idxs[1][0] - 1][idxs[1][1]]\n",
" if idxs[0][0] == idxs[1][0]:\n",
" en_m = (\n",
" self.key_matrix[idxs[0][0]][idxs[0][1] - 1]\n",
" + self.key_matrix[idxs[0][0]][idxs[1][1] - 1]\n",
" )\n",
" elif idxs[0][1] == idxs[1][1]:\n",
" en_m = (\n",
" self.key_matrix[idxs[0][0] - 1][idxs[0][1]]\n",
" + self.key_matrix[idxs[1][0] - 1][idxs[1][1]]\n",
" )\n",
" else:\n",
" en_m = self.key_matrix[idxs[0][0]][idxs[1][1]] + self.key_matrix[idxs[1][0]][idxs[0][1]]\n",
" en_m = (\n",
" self.key_matrix[idxs[0][0]][idxs[1][1]]\n",
" + self.key_matrix[idxs[1][0]][idxs[0][1]]\n",
" )\n",
" en_msg += en_m\n",
" return en_msg\n"
" return en_msg"
]
},
{
@ -225,7 +243,7 @@
}
],
"source": [
"msg = 'hello1234'\n",
"msg = \"hello1234\"\n",
"enc = pf.encrypt(msg)\n",
"enc"
]
@ -290,7 +308,7 @@
}
],
"source": [
"pf.decrypt(pf.encrypt('r'))"
"pf.decrypt(pf.encrypt(\"r\"))"
]
},
{