forked from kste/cryptosmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
executable file
·64 lines (49 loc) · 1.53 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import numpy
import numpy as np
def makedirs(dirs: list):
for dir in dirs:
if not os.path.exists(dir):
os.makedirs(dir)
def sand_t(n, rotation=0) -> list:
if n % 4 != 0:
return []
res = [[], [], [], []]
for i in range(n):
remainder = i % 4
res[remainder].append(i)
res = np.reshape(res, (len(res), len(res[1])))
if rotation > 0:
res = np.roll(res, rotation, 1)
return np.reshape(res, (1, len(res) * len(res[1]))).tolist()[0]
def sand_rot(n, rotation=0) -> list:
if n % 4 != 0:
return []
res = [i for i in range(n)]
res = np.reshape(res, (4, n // 4))
if rotation > 0:
res = np.roll(res, rotation, 1)
return np.reshape(res, (1, len(res) * len(res[1]))).tolist()[0]
def sand_rot_nibble(n, rotation=0) -> list:
if n % 4 != 0:
return []
res = [i for i in range(n)]
res = np.reshape(res, (4, n // 4))
if rotation > 0:
res = np.roll(res, rotation, 1)
return res
def reverse_p_box():
h = 0x84000000
bins = bin(h)[2:].zfill(32)
bins = [int(bins[i]) for i in range(32)]
bins = numpy.reshape(bins, (4, 8))
res = numpy.zeros((4, 8), dtype=numpy.int32)
# perm = [7, 4, 1, 6, 3, 0, 5, 2]
perm = [0, 7, 6, 5, 4, 3, 2, 1]
for i in range(len(bins)):
for j in range(len(perm)):
res[i][perm[j]] = bins[i][j]
r = list(res[0]) + list(res[1]) + list(res[2]) + list(res[3])
r = [str(i) for i in r]
print(hex(int("".join(r), 2)))
# reverse_p_box()