-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_matching.py
144 lines (108 loc) · 4.64 KB
/
template_matching.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 10 11:56:15 2020
@author: anirban727
"""
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
from operator import itemgetter
import re
import csv
def preprocess(file):
file_out = []
file = list(map(lambda each:each.strip("\n"), file))
for item in file:
row = list(map(int, re.split(r'\t+', item)))
file_out.append(row)
return file_out
def parse_filecount(file):
file_out = list(map(int, list(map(lambda each:each.strip("\n"), file))))
return file_out
def openfile(filename, flag):
with open(filename, 'r') as f:
file = f.readlines()
if (flag == 'timing'):
return preprocess(file)
else:
return parse_filecount(file)
numberOfFiles = 400; keys = 32;
template = np.load('rassle_timing_dataset.npy')
no_of_values = template.shape[1]
mean_temp = np.mean(template, axis=1)
median_temp = np.median(template, axis=1)
key_bin_pair = ['100000', '100001', '100010', '100011', '100100', '100101', '100110', '100111',
'101000', '101001', '101010', '101011', '101100', '101101', '101110', '101111',
'110000', '110001', '110010', '110011', '110100', '110101', '110110', '110111',
'111000', '111001', '111010', '111011', '111100', '111101', '111110', '111111']
first_temp = []; second_temp = []; third_temp = []
for j in range(keys):
median_first = []
median_second = []
median_third = []
one_bit_template = template[j]
sm = np.argsort(one_bit_template, axis=0)
new_temp = np.take_along_axis(one_bit_template, sm, axis=0)
for i in range(6):
median_first.append(np.median(new_temp[0:(int(no_of_values/3)),i]))
median_second.append(np.median(new_temp[(int(no_of_values/3)):(int(2*no_of_values/3)),i]))
median_third.append(np.median(new_temp[(int(2*no_of_values/3)):,i]))
first_temp.append(median_first)
second_temp.append(median_second)
third_temp.append(median_third)
mont_start_time = openfile('file_mont_ladder.txt', 'count')
with open('sample_nonces.txt', 'r') as f:
file = f.readlines()
nonces = list(map(lambda each:each.strip("\n"), file))
sample_timing = []
for i in range(500):
filename = 'filetiming_tst_'+str(i+100)+'.txt' # leaving the first 100 nonces
raw_timing = openfile(filename, 'timing')
for index, item in enumerate(raw_timing):
if (item[0] > mont_start_time[i+100]):
if ((index + 5) < len(raw_timing)):
sample_timing.append([raw_timing[index - 1][2], raw_timing[index][2], raw_timing[index + 1][2], raw_timing[index + 2][2], raw_timing[index + 3][2], raw_timing[index + 4][2], raw_timing[index + 5][2]])
break
rank_holder = []; key_holder = []
for i in range(numberOfFiles):
key = nonces[i+100] # leaving the first 100 nonces
keybyte = key[0:2]
keybyte_in_bin = str("{0:08b}".format(int(keybyte, 16)) )
key_six_msb = keybyte_in_bin[0:6]
for index, item in enumerate(key_bin_pair):
if (item == key_six_msb):
correct_index = index
outlist = []
for j in range(keys):
fileX = []
for k in range(6):
dist_first = abs(sample_timing[i][k] - first_temp[j][k])
dist_second = abs(sample_timing[i][k] - second_temp[j][k])
dist_third = abs(sample_timing[i][k] - third_temp[j][k])
if (dist_first <= dist_second and dist_first <= dist_third):
fileX.append(first_temp[j][k])
elif (dist_second <= dist_first and dist_second <= dist_third):
fileX.append(second_temp[j][k])
elif (dist_third <= dist_second and dist_third <= dist_first):
fileX.append(third_temp[j][k])
fileY = sample_timing[i][0:6]
# p,t = stats.pearsonr(fileX, fileY)
p = np.sum((np.array(fileX)-np.array(fileY))**2)
outlist.append((j, p))
outlist = sorted(outlist,key=itemgetter(1))
key_holder.append(outlist[0:5])
for index, item in enumerate(outlist):
if (item[0] == correct_index):
print(index, item)
rank_holder.append(index)
print(' ')
count = len([i for i in rank_holder if i < 5])
print('No. of nonces correctly predicted = '+str(count))
nonce_candidates = [[0 for n in range(5)] for m in range(numberOfFiles)]
for i, keys in enumerate(key_holder):
for j, item in enumerate(keys):
nonce_candidates[i][j] = key_bin_pair[item[0]]
with open("nonce_bits.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(nonce_candidates)