Skip to content

Commit 2e2b585

Browse files
committed
added a few files
1 parent 5651ce1 commit 2e2b585

8 files changed

+145
-34
lines changed

AVX2_and_FMA_test.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
import ctypes
2-
from time import sleep
32

43

54
def avx2_and_fma():
65
for j in range(3):
76
i = j * 64
87
dest = i + 63 * i + 63 * i + 63 - i + 63 + 2
98
src = dest * 2
10-
119
print(dest)
12-
sleep(2)
1310
buf = ctypes.memset(src, dest, 0)
1411
print(buf)
15-
sleep(2)
12+
1613
break
1714

1815
for j in range(15):
1916
i = j * 16
2017
dst = i + 15 * i + 15 + i + 1
2118
print(dst)
22-
sleep(2)
19+
2320
break
2421

2522

DMA_cell.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
from typing import Union, Tuple, List
2-
from timeit import default_timer as timer
1+
from typing import List, Tuple, Union
32

43

54
def dma_list(
65
dst: int, ea_low: int, nbytes: int
76
) -> Union[None, Tuple[int, List[Tuple[int, int, int, int, int, int]]]]:
8-
t1 = timer()
7+
98
result: List[Tuple[int, int, int, int, int, int]] = []
109
tag_id: int = 0
1110
dma_list_elem: List[int] = [0, 31, 1, ea_low]
@@ -31,8 +30,7 @@ def dma_list(
3130

3231
result.append((dma_list_elem[0], tag_id, dst, bits, all_32, stall))
3332
i += 1
34-
t2 = timer()
35-
print(t2 - t1)
33+
3634
return list_size, result
3735

3836

cell_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ def vmult(
1111
varr = arr[:7] * array_size_by_four
1212
varr2 = arr2[:8] * array_size_by_four
1313
vout = tuple(map(mul, varr, varr2))
14-
print(vout)
1514
return vout
1615

1716

checksum.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import time
2+
3+
4+
def checksum_hacker(arr: bytearray) -> int:
5+
length: int = len(arr)
6+
if length == 0:
7+
return 0
8+
9+
sum_values: bytearray = bytearray([0] * 4)
10+
11+
sum_values = bytearray(
12+
[
13+
sum_values[j] + arr[z + i + j]
14+
for z in range(0, length - 256 + 1, 256)
15+
for i in range(0, min(256, length - z), 4)
16+
for j in range(4)
17+
]
18+
)
19+
20+
sum_values += bytearray(
21+
[
22+
arr[i]
23+
for z in range(0, length - 256 + 1, 256)
24+
for i in range(z + 256, min(length, z + 256))
25+
]
26+
)
27+
28+
return sum(sum_values[:4]) ^ sum(sum_values[4:])
29+
30+
31+
def main():
32+
# Benchmarking parameters
33+
minSize = 1000 # Minimum size of data
34+
maxSize = 10000 # Maximum size of data
35+
step = 1000 # Step size for increasing data size
36+
37+
print("Data Size\tTime (ns)")
38+
39+
for dataSize in range(minSize, maxSize + 1, step):
40+
# Generate random data of given size
41+
data = bytearray(dataSize - 1000)
42+
43+
# Start the timer
44+
start = time.perf_counter()
45+
46+
# Call the function to benchmark
47+
checksum = checksum_hacker(data)
48+
print(checksum)
49+
# End the timer
50+
end = time.perf_counter()
51+
52+
# Calculate elapsed time in nanoseconds
53+
elapsedTime = end - start
54+
55+
print(f"{dataSize}\t\t{elapsedTime}")
56+
57+
58+
if __name__ == "__main__":
59+
main()

fib.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
from math import sqrt
22

3-
# O(1)
4-
53

6-
def fib_const_time(n):
4+
# O(1)
5+
def fib_const_time(n: int):
76
print("Fibonacci series")
8-
phi = (1 + sqrt(5)) / 2
9-
Fn = round((phi**n) / sqrt(5))
7+
phi: float = (1 + sqrt(5)) / 2
8+
Fn: int = round((phi**n) / sqrt(5))
109
print(Fn)
1110

1211

1312
fib_const_time(10)
1413

1514

1615
# O(n)
17-
def fib(n):
16+
def fib(n: int):
1817
print("Fibonacci series")
19-
phi = (1 + sqrt(5)) / 2
18+
phi: float = (1 + sqrt(5)) / 2
2019
for i in range(n):
21-
Fn = round((phi**i) / sqrt(5))
20+
Fn: int = round((phi**i) / sqrt(5))
2221
print(Fn, end=" ")
2322
print()
2423

help_queue.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from typing import Optional
2+
import threading
3+
4+
5+
# Node class
6+
class Node:
7+
def __init__(self, value: int) -> None:
8+
self.value: int = value
9+
self.next: Optional[Node] = None
10+
self.mutex: threading.Lock = threading.Lock()
11+
12+
13+
# Queue class
14+
class Queue:
15+
def __init__(self) -> None:
16+
dummy_value: int = 0
17+
self.dummy_node: Node = Node(dummy_value)
18+
self.head: Node = self.dummy_node
19+
self.tail: Node = self.dummy_node
20+
21+
22+
# Function to try removing the front node
23+
def try_remove_front(queue: Queue, front: int) -> bool:
24+
with queue.head.mutex:
25+
head: Node = queue.head
26+
if head.next is not None:
27+
with head.next.mutex:
28+
next_node: Node = head.next
29+
if next_node.value == front:
30+
head.next = next_node.next
31+
del next_node
32+
return True
33+
return False
34+
35+
36+
# Function to help finish enqueue
37+
def help_finish_enq(queue: Queue) -> None:
38+
next_node: Optional[Node] = queue.tail.next
39+
if next_node is not None and next_node == queue.tail:
40+
with queue.tail.mutex:
41+
with queue.head.mutex:
42+
queue.tail.next = next_node
43+
queue.tail = next_node
44+
45+
46+
# Function to enqueue a value
47+
def enqueue(queue: Queue, value: int) -> None:
48+
node: Node = Node(value)
49+
with queue.tail.mutex:
50+
queue.tail.next = node
51+
help_finish_enq(queue)
52+
53+
54+
# Main function
55+
if __name__ == "__main__":
56+
q: Queue = Queue()
57+
front_value: int = 10
58+
enqueue(q, front_value)
59+
result: bool = try_remove_front(q, front_value)
60+
if result:
61+
print(f"Successfully removed front node: {front_value}")
62+
else:
63+
print("Failed to remove front node")

ohm_enc.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from hmac import compare_digest
33
from math import ceil, sqrt
44
from secrets import SystemRandom, choice
5-
from typing import Tuple, Union, Optional
5+
from typing import Optional, Tuple, Union
66

77

88
def ohm_enc(message: str) -> Tuple[float, int, bytes, int]:
@@ -60,6 +60,7 @@ def ohm_enc(message: str) -> Tuple[float, int, bytes, int]:
6060
def ohm_dec(
6161
key: float, random_key: int, iv: bytes, encrypted_message: int, message: str
6262
) -> Optional[str]:
63+
6364
# Calculate the number of bytes needed to represent the integer
6465
num_bytes = (encrypted_message.bit_length() + 7) // 8
6566

@@ -78,19 +79,14 @@ def ohm_dec(
7879
message_hash = decrypted_bytes[:original_hash_length]
7980

8081
# Compute the SHAKE256 hash of the original message
81-
original_hash = shake_256(message.encode("utf-8")).digest(
82-
original_hash_length
83-
)
82+
original_hash = shake_256(message.encode("utf-8")).digest(original_hash_length)
8483

85-
# Compare the message hash with the original hash
86-
if compare_digest(message_hash, original_hash):
87-
return message
88-
else:
89-
return None
84+
return message if compare_digest(message_hash, original_hash) else None
9085

9186

9287
def main() -> None:
93-
# Encrypt a message using ohm_enc
88+
89+
# Encrypt the message using ohm_enc
9490
message = "This is a secret message."
9591
key, random_key, iv, res = ohm_enc(message)
9692

tic_tac_toe.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
from typing import TypeVar
1+
from typing import Tuple, TypeVar
22

33
T = TypeVar("T", int, int)
44

55

6-
def get_cell_position(cell_str: str) -> tuple[T, T]:
6+
def get_cell_position(cell_str: str) -> Tuple[T, T]:
77
# Convert the letter part of the cell string to a column index
88
column_index = ord(cell_str[0]) - ord("A")
99

1010
# Convert the digit part of the cell string to a row index
1111
row_index = int(cell_str[1]) - 1
1212

1313
# Return the row and column indices as a tuple
14-
return (row_index, column_index)
14+
return row_index, column_index
1515

1616

17-
def tic_tac_toe() -> tuple[T, T]:
17+
def tic_tac_toe() -> Tuple[T, T]:
1818
board = [
1919
(" ", " ", "O"),
2020
("X", " ", " "),
@@ -29,7 +29,7 @@ def tic_tac_toe() -> tuple[T, T]:
2929
print("There is an O in cell", cell_str)
3030
else:
3131
print("There is no X or O in cell", cell_str)
32-
return (row, col)
32+
return row, col
3333

3434

3535
tic_tac_toe()

0 commit comments

Comments
 (0)