-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTARtest.py
More file actions
411 lines (347 loc) · 15.7 KB
/
TARtest.py
File metadata and controls
411 lines (347 loc) · 15.7 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import math
import time
from multiprocessing.dummy import freeze_support
from joblib import Parallel, delayed
import os
import sys
import glob
import re
import random
from matplotlib import pyplot as plt
import numpy as np
import multiprocessing as mp
import pickle
from hashlib import sha512
from statistics import variance
from PythonImpl.FuzzyExtractor import FuzzyExtractor
#np.random.seed(1337) # for reproducibility`
################################################################################
# FUNCTION DEFINITIONS #
################################################################################
def numericalSort(value):
parts = numbers.split(value)
parts[1::2] = map(int, parts[1::2])
return parts
def read_fvector(filePath):
with open(filePath) as f:
for line in f.readlines():
temp_str = np.fromstring(line, sep=",")
return [int(x) for x in temp_str]
# Returns a numpy array of python arrays each chosen randomly with size number_samples
def sample_uniform(size, biometric_len, number_samples=1, confidence=None):
pick_range = range(0, biometric_len - 1)
randGen = random.SystemRandom()
return np.array([randGen.sample(pick_range, size) for x in range(number_samples)])
def min_entropy(val):
return min(- math.log2(val), - math.log2(1-val))
def binary_entropy(val):
return -(val) * math.log2(val) - (1 - val) * (math.log2(1-val))
def read_complex_conf(filepath):
bad_list = [28, 200, 503, 754]
with open(filepath, 'r') as f:
confidence = []
lines = f.readlines()
for line in lines:
numbers = line[42:].strip()
numbers_list = numbers.split(' ')
predictability = (1 - float(numbers_list[2]))
entropy = float(numbers_list[3])
pair = [predictability, entropy]
# print(numbers_list, "numbers list")
if int(numbers_list[0]) in bad_list:
confidence.append([0,0.000000000000001])
else:
confidence.append(pair)
return confidence, bad_list
def gen(template,positions):
ret_value = []
for x in range(positions.shape[0]):
v_i = template[positions[x]]
ret_value.append(v_i)
return ret_value
def sample_alpha(size, biometric_len, number_samples, confidence, alpha_param):
bad_list = [28, 200, 503, 754]
if confidence is None:
print("Can't run Smart sampling without confidence, calling uniform")
return sample_uniform(size, biometric_len, number_samples, confidence)
sample_array = []
new_confidence = [pair[0] ** alpha_param for pair in confidence]
for set_selection_iter in range(number_samples):
sample_indices = random.choices(range(len(new_confidence)), weights=new_confidence, k=size)
dedup_indices = list(set(sample_indices))
loop_count = 1
while len(dedup_indices) < size:
new_index = random.choices(range(len(new_confidence)), weights=new_confidence, k=1)
sample_indices = dedup_indices
sample_indices.extend(new_index)
dedup_indices = []
[dedup_indices.append(n) for n in sample_indices if n not in dedup_indices and n not in bad_list]
loop_count = loop_count +1
if loop_count == 1000000:
print("Smart sampling failed to find a non-duplicating subset")
exit(1)
sample_array.append(dedup_indices)
return np.array(sample_array)
# Current Working Project
def sample_alpha_with_entropy(size, biometric_len, number_samples, confidence, alpha_param):
bad_list = [28, 200, 503, 754]
if confidence is None:
print("Can't run Smart sampling without confidence, calling uniform")
return sample_uniform(size, biometric_len, number_samples, confidence)
sample_array = []
new_confidence = [pair[0] ** (alpha_param / min_entropy(pair[1])) for pair in confidence]
for set_selection_iter in range(number_samples):
sample_indices = random.choices(range(len(new_confidence)), weights=new_confidence, k=size)
sample_indices = [index for index in sample_indices if index not in bad_list]
dedup_indices = list(set(sample_indices))
loop_count = 1
while len(dedup_indices) < size:
new_index = random.choices(range(len(new_confidence)), weights=new_confidence, k=max(1,size - len(dedup_indices)))
[dedup_indices.append(n) for n in new_index if n not in dedup_indices and n not in bad_list]
loop_count = loop_count +1
if loop_count == 1000000:
print("Smart sampling failed to find a non-duplicating subset")
exit(1)
sample_array.append(dedup_indices)
return np.array(sample_array)
def rep_helperr2(template, positions, gen_template,index,num_jobs, sum):
i = 0
match_list = []
for x in range(positions.shape[0]):
v_i = template[positions[x]]
if np.array_equal(v_i ,gen_template[i]):
match_list.append(i + sum)
i = i + 1
return match_list
def rep(template, positions, gen_template, num_jobs=32):
number_jobs = num_jobs
# print("positions shape" ,positions.shape)
positions_split = np.array_split (positions, number_jobs)
# print("",len(positions_split))
sums = [0]
sum = 0
for sublist in positions_split:
# print(" ",len(sublist))
sum = sum + len(sublist)
sums.append(sum)
gen_template_split = np.array_split(gen_template, number_jobs)
found_match = Parallel(n_jobs=number_jobs)(delayed(rep_helperr2)
(template, positions_split[i], gen_template_split[i],i,num_jobs,sums[i])
for i in range(number_jobs))
matches = []
for match_list in found_match:
if match_list != []:
matches.extend(match_list)
return matches
def subsample(templates,positions):
print("In subsampling")
print(templates.shape)
print(len(positions),len(positions[0]))
subsampled_array = []
for x in range(templates.shape[0]):
# print("Template:", x)
new_subsample = []
for list in positions:
new_subsample = [templates[x][index] for index in list]
# print("Subsample:",new_subsample)
subsampled_array.append(new_subsample)
print("Returning from subsampling")
return np.array(subsampled_array)
def entropy_helper(template,template_split,gt, gt_split):
i = 0
blue_list = []
red_list = []
for x in range(template.shape[0]):
for y in range(template_split.shape[0]):
if(gt[x][0] < gt_split[y][0]):
continue
dis = np.count_nonzero(template[x]!=template_split[y])
# dis = dis/template[x].shape[1]
dis = dis / template.shape[1]
#Just a stupid hack
if (dis == 0):
continue
if(gt[x][0] == gt_split[y][0]):
blue_list.append(dis)
else:
red_list.append(dis)
return blue_list,red_list
def entropy(templates, ground_truth, selection_method,size_or_threshold,num_jobs=4,positions=[],start=0):
if len(positions) != 0:
runs = len(positions)
else:
runs = 10
entropy_list = []
for r in range(start,runs):
if len(positions) == 0:
if selection_method == 'complex':
print("Using Complex Alpha Sampling")
positions = sample_alpha_with_entropy(size_or_threshold,1024,1,confidence,alpha_param)
else:
print("Using Simple Alpha Sampling")
positions = sample_alpha(size_or_threshold,1024,1,confidence,alpha_param)
print("Subsampling Templates")
subsampled_templates = subsample(templates,positions[r:r+1])
print(len(subsampled_templates), len(subsampled_templates[0]))
print("Finished Subsampling")
blue = []
red = []
i = 0
print("Using",num_jobs,"cores for Entropy")
number_jobs = num_jobs
print("Splitting templates and Ground Truths")
templates_split = np.array(np.array_split(subsampled_templates, number_jobs),dtype=object)
ground_truth_split = np.array(np.array_split(ground_truth, number_jobs),dtype=object)
print("Finished Split")
print("Searching for Matches")
found_match = Parallel(n_jobs=number_jobs)(delayed(entropy_helper)
(subsampled_templates,templates_split[i],ground_truth,ground_truth_split[i])
for i in range(number_jobs))
for x in range(len(found_match)):
blue.extend(found_match[x][0])
red.extend(found_match[x][1])
print("Calculating Statistics")
u = np.mean(red)
degrees_freedom = (u*(1-u))/np.var(red)
print("Adding to list")
entropy = degrees_freedom * min_entropy(u)
entropy_list.append(2**(-1 * entropy))
print(u,np.var(red))
print ("Entropy Run #",r," Entropy:",entropy,"Mean of unlike dist:",u, "Mean of like:", np.mean(blue))
# plt.hist(red, bins=20)
# plt.show()
# plt.hist(blue, bins=20)
# plt.show()
exp_ent = np.mean(entropy_list)
avg_ent = -1 * math.log(exp_ent, 2)
print ("Average Entropy", avg_ent)
return avg_ent,entropy_list
################################################################################
# EXECUTION SCRIPT #
################################################################################
# Command Line Usage:
# python3 TarTest.py [subset size or entropy threshold] [number of subsets] [filename for positions]
if __name__ == '__main__':
freeze_support()
do_cryptography=0
size_or_threshold = int(sys.argv[1]) # Subset size
num_lockers = int(sys.argv[2]) # number of subsets sampled
filename = sys.argv[3]
feature_vector_folder = sys.argv[4]
number_to_group = int(sys.argv[5])
numbers = re.compile(r'(\d+)')
cwd = os.getcwd()
num_cpus = 2*mp.cpu_count()
folder_list = sorted(glob.glob(cwd + "/"+feature_vector_folder+"/*"),key=numericalSort)
CLASSES = len(folder_list)
print ("Folders: ",len(folder_list))
num_classes = range(len(folder_list))
print ("Reading templates")
templates = []
ground_truth = []
dimension=1024
for x in range(len(num_classes)):
template_temp = []
ground_truth_temp = []
template_list = glob.glob(folder_list[num_classes[x]] + "/*")
if len(template_list)<=number_to_group+1:
continue
ret_template = np.array(read_fvector(template_list[0]))
if ret_template.size !=dimension:
continue
template_temp.append(ret_template)
ground_truth_temp.append([x, 0])
for y in range(math.floor((len(template_list)-1)/number_to_group)):
exclude_iteration = 0
ret_template_list = [0 for i in range(number_to_group)]
for i in range(number_to_group):
ret_template_list[i] = np.array(read_fvector(template_list[y*number_to_group+i+1]))
if ret_template_list[i].ndim == 0:
exclude_iteration = 1
ret_template = np.array([0 for i in range(dimension)])
# print(ret_template_list)
if exclude_iteration == 0:
for i in range(dimension):
count = 0
for j in range(number_to_group):
count = count+ret_template_list[j][i]
if count > number_to_group/2:
ret_template[i]=1
# print(ret_template)
# exit(1)
# ret_template = np.array(read_fvector(template_list[y+i])) for i in [number_to_group]
# print(ret_template)
template_temp.append(ret_template)
ground_truth_temp.append([x, y])
templates.append(template_temp)
ground_truth.extend(ground_truth_temp)
print("Finished reading Templates")
print("Length of templates "+str(len(templates)))
all_tpr = []
all_matches = []
reps_done = 0
with open("subsets/" + filename + ".pkl", 'rb') as f:
positions = pickle.load(f)
f.close()
print("Finished reading positions")
print ("Starting gen and rep for Subset size",str(size_or_threshold),"and", str(num_lockers),"subsets")
gen_time=[]
rep_time=[]
num_attempts = 0
num_successes = 0
for x in range(min(len(templates),200)):
templateNum = x
print(len(templates[templateNum]))
if templates[templateNum] is None or len(templates[templateNum]) < 2:
continue
matches = []
hamming_distance = []
if do_cryptography == 0:
gen_start=time.time()
gen_template = np.array(gen( np.array(templates[templateNum][0]),np.array(positions)))
gen_end=time.time()
# print("Finished Gen")
gen_time.append(gen_end-gen_start)
person_tpr = []
# print("Starting Rep")
rep_start = time.time()
for y in range(1,min(len(templates[templateNum]),11)):
num_attempts= num_attempts+1
hdist = np.count_nonzero([templates[templateNum][y][i]!=templates[templateNum][0][i] for i in range(len(templates[templateNum][0]))])
hamming_distance.append(hdist)
temp_matches = rep(templates[templateNum][y], positions, gen_template,2*num_cpus)
matches.extend(temp_matches)
person_tpr.append(temp_matches != [])
if(temp_matches!=[]):
num_successes= num_successes+1
rep_end = time.time()
rep_time.append(rep_end-rep_start)
else:
gen_start = time.time()
fuzzext = FuzzyExtractor(positions=np.array(positions))
(r, encrypted_lockers, seeds) = fuzzext.gen(templates[templateNum][0], locker_size=size_or_threshold, lockers=num_lockers)
gen_end = time.time()
gen_time.append(gen_end-gen_start)
person_tpr = []
for y in range(1,min(len(templates[templateNum]), 11)):
hdist = np.count_nonzero([templates[templateNum][y][i]!=templates[templateNum][0][i] for i in range(len(templates[templateNum][0]))])
hamming_distance.append(hdist)
rep_start = time.time()
temp_matches = fuzzext.rep(templates[templateNum][y], encrypted_lockers, seeds, num_processes=1)
rep_end = time.time()
rep_time.append(rep_end-rep_start)
person_tpr.append(temp_matches!=-1)
matches.append(temp_matches)
reps_done += len(person_tpr)
print("Number of reps done "+str(reps_done))
all_tpr.extend(person_tpr)
all_matches.extend(matches)
if (x % 10) == 0 and num_attempts > 0:
print("Number averaging: "+str(number_to_group)+" TPR :", str(num_successes / num_attempts))
# print ("Subsample size:", str(size_or_threshold), "| TPR :", str(sum(all_tpr)/len(all_tpr)) ,"| Reps done:",reps_done)
# print("Generate time:",str(sum(gen_time)/len(gen_time)), " Gen variance:",str(variance(gen_time)))
# print("Rep time:", str(sum(rep_time)/len(rep_time)), "Rep variance:", str(variance(rep_time)))
if num_attempts > 0:
print(" Complete TPR :", str(num_successes / num_attempts))
# print ("Matched Indicies over TPR:", set(all_matches), "With lockers: ", num_lockers)
print("Finished TAR test")