forked from WhiteboxFiltering/WhiteboxFiltering
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RNR.py
341 lines (275 loc) · 11.8 KB
/
RNR.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
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
import os
import argparse
import pathlib
import pickle
import fnmatch
from time import time
from sage.all import Matrix, GF, floor, vector
from transposeTraces import getHeader, getNodeVectors
#@TODO
def reduceVectors(vectors, with_kernel=True):
if not with_kernel:
return Matrix(GF(2), vectors).pivot_rows(), None
mat = Matrix(GF(2), vectors)
return mat.pivot_rows(), mat.left_kernel_matrix()
'''
sage: version()
'SageMath version 10.0, Release Date: 2023-05-20'
sage: %timeit matrix(GF(2), vecs).left_kernel().matrix()
1.03 s ± 5.17 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
sage: %timeit matrix(GF(2), vecs).left_kernel_matrix()
99.3 ms ± 957 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
sage: %timeit matrix(GF(2), vecs).pivot_rows()
99.8 ms ± 414 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
But when cached...
sage: %timeit mat.left_kernel().matrix()
215 ns ± 0.222 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
sage: %timeit mat.left_kernel_matrix()
1.27 ms ± 1.75 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
'''
# failed attempt to optimize by hand:
ker = Matrix(GF(2), vectors).left_kernel().matrix()
pivots = []
x = 0
for y in range(ker.nrows()):
while not ker[y,x]:
x += 1
pivots.append(x)
return pivots, ker
#bestNRNfile allows to find the best pickle file containing the indexes of the
#non-redundant nodes, using their names. It returns the window size that has
#been used to create this file with RNR.
def bestNRNfile(pathToTraces):
correspondingFiles = []
#Getting all the files called NRN_W
for root, dirs, files in os.walk(pathToTraces):
for name in files:
if fnmatch.fnmatch(name, 'NRN_W*.pkl'):
correspondingFiles.append(os.path.join(root, name))
maxValue=0
if len(correspondingFiles)>1:
#Getting the biggest value for W
for file in correspondingFiles:
value=int(file[-8:-4])
if value>maxValue:
maxValue=value
elif len(correspondingFiles)==1:
maxValue=int(correspondingFiles[0][-8:-4])
return maxValue
#Given the path of a folder containing the NRN file, getNRN will load into a
#list all the indexes of the non redundant nodes.
def getNRN(pathToTraces):
(numOfNodes, T)=getHeader(pathToTraces)
maxValue=bestNRNfile(pathToTraces)
if maxValue==0:
#If no NRN file exists, returns all the list of all the node indexes
return list(range(numOfNodes))
else:
#Else, we open the pickle file and load it in the list
fNRN = pathToTraces / ("NRN_W%04d.pkl" % maxValue)
with open(fNRN, "rb") as file:
return pickle.load(file)
#@TODO
def bestRNrelFile(pathToTraces):
correspondingFiles = []
for root, dirs, files in os.walk(pathToTraces):
for name in files:
if fnmatch.fnmatch(name, 'RNrel_W*.pkl'):
correspondingFiles.append(os.path.join(root, name))
if not correspondingFiles:
raise IOError("RNrel file not found")
maxValue=0
if len(correspondingFiles)>1:
for file in correspondingFiles:
value=int(file[-8:-4])
if value>maxValue:
maxValue=value
elif len(correspondingFiles)==1:
maxValue=int(correspondingFiles[0][-8:-4])
return maxValue
#@TODO
def getNRrel(pathToTraces):
maxValue=bestNRNfile(pathToTraces)
fNRN = pathToTraces / ("RNrel_W%04d.pkl" % maxValue)
with open(fNRN, "rb") as file:
return pickle.load(file)
#Main implementation of RNR, see the ReadMe file for more details about the inputs
def RNR(pathToTraces, W=-1, S=-1, t=30, save_relations=False, ignoreExisting=False, extraNRN=None, affine=False):
#RECOVERING THE HEADER OF THE NODE VECTORS FILE: number of traces and nodes
(numOfNodes, T) = getHeader(pathToTraces)
totalNumOfNodes = numOfNodes
#VERIFYING INPUT PARAMETER
W=min(W, numOfNodes)
maxWValue=floor(T-t)
if W>maxWValue:
print("/!\\ Not enough traces to perform RNR with window size W=%d /!\\" % W)
print("With the available T=%d traces, you can choose W%d at maximum." % (T,maxWValue))
print("Performing RNR with W=%d would require T=%d traces." % (W, W+t))
return
if S>W:
S=W
if W==0:
W=maxWValue
if W==-1:
W=min(maxWValue,500)
if S==-1:
S=max(1,W//6)
if ignoreExisting:
maxValue = 0
else:
maxValue = bestNRNfile(pathToTraces)
if maxValue>=W:
print("A better file containing indices for non redundant nodes (NRN) already exists for W=%d" % maxValue)
return
#OPENNING AN EXISTING NRN FILE IF IT EXISTS TO RUN RNR ONTO IT
if maxValue==0:
NonRedundantNodes=list(range(numOfNodes))
else:
fNRN = pathToTraces / ("NRN_W%04d.pkl" % maxValue)
with open(fNRN, "rb") as file:
NonRedundantNodes=pickle.load(file)
numOfNodes=len(NonRedundantNodes)
#@TODO
if extraNRN:
with open(extraNRN, "rb") as file:
extraNRN=pickle.load(file)
orig = len(NonRedundantNodes)
NonRedundantNodes=sorted(set(NonRedundantNodes) & set(extraNRN))
print("reduced NRN using extra file:", orig, "->", len(NonRedundantNodes))
reverseQueue = list(reversed(NonRedundantNodes))
NonRedundantNodes = []
T=W+t
SUMtime=0
numberOfTimes=0
#GETTING THE NODE VECTORS
NODEVECTORS = getNodeVectors(pathToTraces, T)[0]
if save_relations:
redundant_relations = []
slidingWindowPosition=0
removedNodes=0
ONES = vector(GF(2), [1]*len(NODEVECTORS[0]))
#PREPARING FIRST WINDOW
XORnodesRemoved=True
while XORnodesRemoved:
XORnodesRemoved=False
indexes=revQueuePopN(reverseQueue, W-S)
windowMatrix=NODEVECTORS[indexes]
if affine:
# reduce node vectors (rows) by the all-constant vector
windowMatrix -= windowMatrix.column(0).outer_product(ONES)
linIndRows, ker = reduceVectors(windowMatrix, with_kernel=save_relations)
if len(linIndRows)<len(indexes):
XORnodesRemoved=True
# return back independent indexes
for index in reversed(linIndRows):
globalIndex = indexes[index]
reverseQueue.append(globalIndex)
for index in linIndRows:
globalIndex = indexes[index]
if (not NonRedundantNodes) or globalIndex > NonRedundantNodes[-1]:
NonRedundantNodes.append(globalIndex)
removedNodes += len(indexes) - len(linIndRows)
if save_relations:
for row in ker:
relation = [index for index, take in zip(indexes, row) if take]
redundant_relations.append(relation)
#ACTUAL REDUNDANT NODES REMOVAL
print("Removing redundant nodes for window size W=%d t=%d on T=%d traces " % (W, t, T))
t0 = time()
SUMtime=0
numberOfTimes=0
while reverseQueue:
t1=time()
indexes=revQueuePopN(reverseQueue, W)
slidingWindowPosition = indexes[0]
windowMatrix=NODEVECTORS[indexes]
if affine:
# reduce node vectors (rows) by the all-constant vector
windowMatrix -= windowMatrix.column(0).outer_product(ONES)
linIndRows, ker = reduceVectors(windowMatrix, with_kernel=save_relations)
wasEmptied = not reverseQueue
# return back independent indexes
for index in reversed(linIndRows):
globalIndex = indexes[index]
reverseQueue.append(globalIndex)
for index in linIndRows:
globalIndex = indexes[index]
if (not NonRedundantNodes) or globalIndex > NonRedundantNodes[-1]:
NonRedundantNodes.append(globalIndex)
removedNodes += len(indexes) - len(linIndRows)
if save_relations:
for row in ker:
relation = [index for index, take in zip(indexes, row) if take]
redundant_relations.append(relation)
# slide W//6 vectors
revQueuePopN(reverseQueue, S)
if wasEmptied:
reverseQueue.clear()
t2=time()
SUMtime+=t2-t1
numberOfTimes+=1
percentDone=max(slidingWindowPosition,1)/numOfNodes
remainingTime=((1-percentDone)/percentDone)*SUMtime
if numberOfTimes%32==0:
print("[.] node %d/%d (%.2f%%), number of removed XOR nodes so far: %d (%.2f%%), estimated remaining time: %dh%02dm%fs" % (slidingWindowPosition, numOfNodes, percentDone*100, removedNodes, 100*removedNodes/(slidingWindowPosition), remainingTime//3600, (remainingTime//60)%60, remainingTime%60), end="\r")
TOTALtime = time() - t0
print(" ")
print('\033[1A', end='\x1b[2K')
print("[✓] removed XOR nodes: %d (%.2f%%), remaining: %d, time elapsed: %dh%02dm%fs " % (numOfNodes-len(NonRedundantNodes), 100*(numOfNodes-len(NonRedundantNodes))/numOfNodes, len(NonRedundantNodes), TOTALtime//3600, (TOTALtime//60)%60, TOTALtime%60))
print(" "*101)
print(" "*101)
print('\033[1A', end='\x1b[2K')
print('\033[1A', end='\x1b[2K')
ftrace = pathToTraces / ("NRN_W%04d.pkl" % W)
with open(ftrace, "wb") as file:
pickle.dump(NonRedundantNodes, file)
if save_relations:
frel = pathToTraces / ("RNrel_W%04d.pkl" % W)
with open(frel, "wb") as file:
pickle.dump(redundant_relations, file)
if maxValue>0:
#os.remove(args.trace_dir / ("NRN_W%04d.pkl" % maxValue))
print("The pickle file \"NRN_W%04d.pkl\" containig the list of all non redudant nodes has been created" % W)
print("Total number of removed node from the original file: %d (%.2f%%)" % (totalNumOfNodes-len(NonRedundantNodes), 100*(totalNumOfNodes-len(NonRedundantNodes))/totalNumOfNodes))
def revQueuePopN(d, N):
assert N >= 0
ret = list(reversed(d[-N:]))
del d[-N:]
return ret
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='description to do later',
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
'trace_dirs', type=pathlib.Path, nargs='+',
help="1 or more paths to directories with trace/plaintext/ciphertext files"
)
parser.add_argument(
'-NRN', type=pathlib.Path, default=None,
help="Extra list for non-redundant nodes",
)
parser.add_argument(
'-W', '--Window', type=int, default=-1,
help="Window Size for Redundant Node Removal"
)
parser.add_argument(
'-S', '--Sliding', type=int, default=-1,
help="Number of nodes to slide after resolving a window for Redundant Node Removal"
)
parser.add_argument(
'-t', '--falsePos', type=int, default=30,
help="Exceding number of traces to avoid false-positives"
)
parser.add_argument(
'--save-relations', action='store_true',
help="Save redundant relations in a pickle file",
)
parser.add_argument(
'--no-affine', action='store_true',
help="Do not remove affine redundancies"
)
args = parser.parse_args()
for trace_dir in args.trace_dirs:
print("Processing trace folder", trace_dir)
RNR(trace_dir, extraNRN=args.NRN, W=args.Window, S=args.Sliding, t=args.falsePos, save_relations=args.save_relations, affine=not args.no_affine, ignoreExisting=True)