def test_read(self): trace_count = 100 sample_count = 1000 original_traces = [ Trace( SampleCoding.FLOAT, [get_sample(i) for i in range(0, sample_count)], TraceParameterMap() ) for i in range(0, trace_count) ] # Create a trace with trsfile.open(self.tmp_path, 'w', headers = { Header.LABEL_X: 'Testing X', Header.LABEL_Y: 'Testing Y', Header.DESCRIPTION: 'Testing trace creation', }, padding_mode = TracePadding.AUTO ) as trs_traces: trs_traces.extend(original_traces) # Make sure length is equal self.assertEqual(len(original_traces), len(trs_traces)) # Read the trace and check if everything is good with trsfile.open(self.tmp_path, 'r') as trs_traces: # Check if lengths are still good :) self.assertEqual(len(original_traces), len(trs_traces)) # Check if every trace is saved correctly for original_trace, trs_trace in zip(trs_traces, original_traces): self.assertEqual(original_trace, trs_trace)
def test_padding(self): trace_count = 100 sample_count = 1000 fmt = SampleCoding.FLOAT with trsfile.open(self.tmp_path, 'w', padding_mode=TracePadding.AUTO) as trs_traces: # This is the length everything should be padded/clipped to trs_traces.extend( Trace( fmt, b'\xDE' * (sample_count * fmt.size), TraceParameterMap({'LEGACY_DATA': ByteArrayParameter(bytes(8))}) ) ) # Padding mode trs_traces.extend([ Trace( fmt, b'\xDE' * (sample_count + i) * fmt.size, TraceParameterMap({'LEGACY_DATA': ByteArrayParameter(abs(i).to_bytes(8, byteorder='big'))}) ) for i in range(0, -trace_count, -1)] ) # Clipping mode trs_traces.extend([ Trace( fmt, b'\xDE' * (sample_count + i) * fmt.size, TraceParameterMap({'LEGACY_DATA': ByteArrayParameter(i.to_bytes(8, byteorder='big'))}) ) for i in range(0, trace_count)] ) with trsfile.open(self.tmp_path, 'r') as trs_traces: self.assertEqual(len(trs_traces), trace_count * 2 + 1) # Check that all traces are of the same size for trs_trace in trs_traces: self.assertEqual(len(trs_trace), sample_count) # Check that all padding is zero for i, trs_trace in enumerate(trs_traces[1:101]): # Difficult case :) if i == 0: continue for si, sample in enumerate(trs_trace[-i:]): self.assertEqual(sample, 0.0 if fmt == SampleCoding.FLOAT else 0, str(i)) # Test that this is indeed not zero self.assertNotEqual(trs_trace[-i - 1], 0)
def __init__(self, filename, metadatas_parsers, custom_headers={}, dtype=None): self._filename = filename self._sub_traceset_indices = None self._headers = {k: v for k, v in custom_headers.items()} self._custom_headers = self._headers try: with _trsfile.open(filename, 'r') as trs_file: self._internal_headers = trs_file.get_headers() self._headers.update({ f'{str(k).replace("Header.", "").lower()}': v.value if isinstance(v, _trsfile.SampleCoding) else v for k, v in self._internal_headers.items() }) self._size = self._internal_headers[ _trsfile.Header.NUMBER_TRACES] except (FileNotFoundError, ValueError, NotImplementedError) as e: raise AttributeError( f"{filename} is not a valid TRS file or filename. Original exception was:{e}." ) self._set_metadatas_parsers(metadatas_parsers) self.dtype = _np.dtype(dtype) if dtype else self._internal_headers[ _trsfile.Header.SAMPLE_CODING].format
def test_extend(self): trace_count = 100 sample_count = 1000 with trsfile.open(self.tmp_path, 'w', padding_mode=TracePadding.AUTO) as trs_traces: # Extend empty list trs_traces.extend([]) self.assertEqual(len(trs_traces), 0) # Extend non empty list trs_traces.extend([ Trace( SampleCoding.FLOAT, [0] * sample_count, data = b'\x00' * 8 ) ] ) self.assertEqual(len(trs_traces), 1) # Extend non empty list trs_traces.extend([ Trace( SampleCoding.FLOAT, [0] * sample_count, data = i.to_bytes(8, byteorder='big') ) for i in range(0, trace_count)] ) self.assertEqual(len(trs_traces), trace_count + 1)
def test_extend(self): trace_count = 100 sample_count = 1000 with trsfile.open(self.tmp_path, 'w', padding_mode=TracePadding.AUTO) as trs_traces: # Extend empty list trs_traces.extend([]) self.assertEqual(len(trs_traces), 0) # Extend non empty list trs_traces.extend([ Trace( SampleCoding.FLOAT, [0] * sample_count, TraceParameterMap({'LEGACY_DATA': ByteArrayParameter(bytes(8))}) ) ] ) self.assertEqual(len(trs_traces), 1) # Extend non empty list trs_traces.extend([ Trace( SampleCoding.FLOAT, [0] * sample_count, TraceParameterMap({'LEGACY_DATA': ByteArrayParameter(i.to_bytes(8, byteorder='big'))}) ) for i in range(0, trace_count)] ) self.assertEqual(len(trs_traces), trace_count + 1)
def test_open(self): with trsfile.open(dirname(abspath(__file__)) + '/data/90x500xfloat.trs', engine='TrsEngine') as trs_file: self.assertIsInstance(trs_file, trsfile.TraceSet) self.assertIsInstance(trs_file.engine, trsfile.Engine) self.assertIsInstance(trs_file.engine, trsfile.TrsEngine) # Assume both handles to be not closed self.assertFalse(trs_file.is_closed())
def fetch_metadatas(self, key, trace_id): if trace_id is not None: trace_index = self._convert_traces_indices_to_file_indices_array( trace_id) with _trsfile.open(self._filename, 'r') as trs_file: raw_trace = trs_file[trace_index] res = self._metadatas_parsers[key](raw_trace.data) else: if self._sub_traceset_indices is not None: indices = [i for i in self._sub_traceset_indices] else: indices = [i for i in range(len(self))] with _trsfile.open(self._filename, 'r') as trs_file: res = _np.array([ self._metadatas_parsers[key](trs_file[i].data) for i in indices ]) return res
def fetch_samples(self, traces, frame=None): traces = self._convert_traces_indices_to_file_indices_array(traces) if isinstance(frame, int): frame = [frame] with _trsfile.open(self._filename, 'r') as trs_file: raw_traces = [trs_file[trace] for trace in traces] samples = _np.array([trace[frame] for trace in raw_traces], dtype=self.dtype) return samples
def test_close(self): trs_file = trsfile.open(dirname(abspath(__file__)) + '/data/90x500xfloat.trs', engine='TrsEngine') self.assertIsInstance(trs_file, trsfile.TraceSet) self.assertIsInstance(trs_file.engine, trsfile.Engine) self.assertIsInstance(trs_file.engine, trsfile.TrsEngine) trs_file.close() # Assume both handles to be closed self.assertTrue(trs_file.is_closed())
def get_trs_info(traces_path): ''' 获取.trs文件基本信息 :return 波形条数,波形点数 ''' with trsfile.open(traces_path, 'r') as traces: values = list(traces.get_headers().values()) number_traces = values[0] # traces.get_headers()['<Header.NUMBER_TRACES: 65>'] number_samples = values[1] # traces.get_headers()['<Header.NUMBER_SAMPLES: 66>'] info = "traces number is {}, samples number is {}.".format(number_traces, number_samples) return info
def test_append(self): trace_count = 100 sample_count = 1000 # Append to a non-existing file, behaves same as normal "write" with trsfile.open(self.tmp_path, 'a', padding_mode=TracePadding.AUTO) as trs_traces: self.assertEqual(len(trs_traces), 0) # Extend the trace file with 100 traces with each 1000 samples trs_traces.extend([ Trace( SampleCoding.FLOAT, [0] * sample_count, TraceParameterMap({'LEGACY_DATA': ByteArrayParameter(i.to_bytes(8, byteorder='big'))}) ) for i in range(0, trace_count)] ) self.assertEqual(len(trs_traces), trace_count) # Now open and close for a few times while adding some number of traces expected_length = trace_count for t in range(0, 10): trace_count = (t + 1) * 10 with trsfile.open(self.tmp_path, 'a', padding_mode=TracePadding.AUTO) as trs_traces: self.assertEqual(len(trs_traces), expected_length) # Extend the trace file with 100 traces with each 1000 samples trs_traces.extend([ Trace( SampleCoding.FLOAT, [0] * sample_count, TraceParameterMap({'LEGACY_DATA': ByteArrayParameter(i.to_bytes(8, byteorder='big'))}) ) for i in range(0, trace_count)] ) expected_length += trace_count self.assertEqual(len(trs_traces), expected_length)
def test_padding_none(self): sample_count = 1000 with trsfile.open( self.tmp_path, 'w', padding_mode = TracePadding.NONE, headers = { Header.NUMBER_SAMPLES: sample_count, Header.LENGTH_DATA: 8, Header.SAMPLE_CODING: SampleCoding.FLOAT } ) as trs_traces: # This is the length of the trace trs_traces.extend( Trace( SampleCoding.FLOAT, [0] * sample_count, TraceParameterMap({'LEGACY_DATA': ByteArrayParameter(bytes(8))}) ) ) # Length is smaller with self.assertRaises(ValueError): trs_traces.extend( Trace( SampleCoding.FLOAT, [0] * (sample_count - 1), TraceParameterMap({'LEGACY_DATA': ByteArrayParameter(b'\x10' * 8)}) ) ) self.assertEqual(len(trs_traces), 1) # Length is bigger with self.assertRaises(ValueError): trs_traces.extend( Trace( SampleCoding.FLOAT, [0] * (sample_count + 1), TraceParameterMap({'LEGACY_DATA': ByteArrayParameter(b'\x01' * 8)}) ) ) self.assertEqual(len(trs_traces), 1) # Length is equal trs_traces.extend( Trace( SampleCoding.FLOAT, [0] * sample_count, TraceParameterMap({'LEGACY_DATA': ByteArrayParameter(bytes(8))}) ) ) self.assertEqual(len(trs_traces), 2)
def write_trs_file(): with trsfile.open(TRS_FILENAME, 'w', live_update=1, padding_mode=trsfile.TracePadding.AUTO) as trs: trs.extend([ trsfile.Trace( trsfile.SampleCoding.SHORT, DATAS[i], data=bytes(CIPHERS[i].tolist() + PLAINS[i].tolist()) + INDICES[i].encode('utf8'), title=f'The {i}th trace') for i in range(len(DATAS)) ])
def test_exclusive(self): trace_count = 100 sample_count = 1000 # Write to file exclusively with trsfile.open(self.tmp_path, 'x', padding_mode=TracePadding.AUTO) as trs_traces: self.assertEqual(len(trs_traces), 0) # Extend the trace file with 100 traces with each 1000 samples trs_traces.extend([ Trace( SampleCoding.FLOAT, [0] * sample_count, TraceParameterMap({'LEGACY_DATA': ByteArrayParameter(i.to_bytes(8, byteorder='big'))}) ) for i in range(0, trace_count)] ) self.assertEqual(len(trs_traces), trace_count) # Now try again (this should throw an exception) with self.assertRaises(FileExistsError): with trsfile.open(self.tmp_path, 'x') as trs_traces: self.assertEqual(len(trs_traces), trace_count)
def test_write_closed(self): trace_count = 100 sample_count = 1000 with trsfile.open(self.tmp_path, 'w', padding_mode=TracePadding.AUTO) as trs_traces: trs_traces.extend([ Trace( SampleCoding.FLOAT, [0] * sample_count, TraceParameterMap({'LEGACY_DATA': ByteArrayParameter(i.to_bytes(8, byteorder='big'))}) ) for i in range(0, trace_count)] ) # Should raise an "ValueError: I/O operation on closed trace set" with self.assertRaises(ValueError): print(trs_traces)
def read_ave_trs(traces_path, ave_nums): ''' 获取.trs文件的平均波形数据,每ave_nums条平均成一条,以矩阵的形式返回 波形条数为N,波形点数为L @:return N*L的矩阵,即矩阵的每一行代表一条波形 ''' try: with trsfile.open(traces_path, 'r') as traces: values = list(traces.get_headers().values()) N = values[0] L = values[1] matrix = np.zeros((N//ave_nums, L)) for i in range(N//ave_nums): means=[] for j in range(ave_nums): means.append(traces[i*ave_nums+j].samples) matrix[i, :] = np.mean(means, axis=0) return matrix except: print(traceback.format_exc())
def read_trs(traces_path, nums=0): ''' 获取.trs文件的波形数据,以矩阵的形式返回 波形条数为N,波形点数为L @:return N*L的矩阵,即矩阵的每一行代表一条波形 ''' try: with trsfile.open(traces_path, 'r') as traces: values = list(traces.get_headers().values()) if nums == 0: N = values[0] else: N = nums L = values[1] matrix = np.zeros((N, L)) for i in range(N): matrix[i, :] = traces[i].samples return matrix except: print(traceback.format_exc())
def test_write(self): trace_count = 100 sample_count = 1000 try: with trsfile.open(self.tmp_path, 'w', headers = { Header.LABEL_X: 'Testing X', Header.LABEL_Y: 'Testing Y', Header.DESCRIPTION: 'Testing trace creation', }, padding_mode = TracePadding.AUTO) as trs_traces: trs_traces.extend([ Trace( SampleCoding.FLOAT, [0] * sample_count, data = i.to_bytes(8, byteorder='big') ) for i in range(0, trace_count)] ) except Exception: self.assertTrue(False)
def test_write(self): trace_count = 100 sample_count = 1000 try: with trsfile.open(self.tmp_path, 'w', headers = { Header.LABEL_X: 'Testing X', Header.LABEL_Y: 'Testing Y', Header.DESCRIPTION: 'Testing trace creation', }, padding_mode = TracePadding.AUTO) as trs_traces: trs_traces.extend([ Trace( SampleCoding.FLOAT, [0] * sample_count, TraceParameterMap({'LEGACY_DATA': ByteArrayParameter(i.to_bytes(8, byteorder='big'))}) ) for i in range(0, trace_count)] ) except Exception as e: self.fail('Exception occurred: ' + str(e))
def get_trs_plaintext_ciphertext(traces_path, plaintext_pos, plaintext_len, ciphertext_pos, ciphertext_len): ''' 获取波形的明文密文 :param traces_path: 波形的路径 :param plaintext_pos: 明文的初始位置 :param plaintext_len: 明文长度 :param ciphertext_pos: 密文的初始位置 :param ciphertext_len: 密文长度 :return: 明文,密文 ''' try: with trsfile.open(traces_path, 'r') as traces: values = list(traces.get_headers().values()) N = values[0] plaintext = [] ciphertext = [] for i in range(N): infos = list(traces[i].data) plaintext.append(infos[plaintext_pos:plaintext_pos + plaintext_len]) ciphertext.append(infos[ciphertext_pos:ciphertext_pos + ciphertext_len]) return plaintext, ciphertext except: print(traceback.format_exc())
#!/usr/bin/python3 # sample code to read a TRS file and show its contents. # The trsfile package can be either found at its github page: # https://github.com/Riscure/python-trsfile # and in the pip3 installer: # pip3 install trsfile import trsfile with trsfile.open('trace-set.trs', 'r') as traces: # Show all headers for header, value in traces.get_headers().items(): print(header, '=', value) print() # Iterate over the first 25 traces for i, trace in enumerate(traces[0:25]): print('Trace {0:d} contains {1:d} samples'.format(i, len(trace))) print(' - minimum value in trace: {0:f}'.format(min(trace))) print(' - maximum value in trace: {0:f}'.format(max(trace)))
labels.append(0) n_digits = 2 sample_size = 256 def bench_k_means(estimator, name, data): t0 = time() estimator.fit(data) print('%-9s\t%.2fs\t%i\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f' % (name, (time() - t0), estimator.inertia_, metrics.homogeneity_score(labels, estimator.labels_), metrics.completeness_score(labels, estimator.labels_), metrics.v_measure_score(labels, estimator.labels_), metrics.adjusted_rand_score(labels, estimator.labels_), metrics.adjusted_mutual_info_score(labels, estimator.labels_), metrics.silhouette_score(data, estimator.labels_, metric='euclidean', sample_size=sample_size))) return estimator with trsfile.open('./0_255_align.trs', 'r') as traces: data = numpy.array(traces) pca = PCA(n_components=n_digits).fit(data) a = bench_k_means(KMeans(init=pca.components_, n_clusters=n_digits, n_init=1), name="PCA-based", data=data) # for i in range(0,256): # print(i,estimator.predict(data[i]))
def test_read_non_existing(self): with self.assertRaises(FileNotFoundError): with trsfile.open(self.tmp_path, 'r') as trs_traces: pass
def setUp(self): self.trs_file = trsfile.open(dirname(abspath(__file__)) + '/data/90x500xfloat.trs', engine='TrsEngine')
# folder and file definitions ######################################### # The file which contains the TRS files OUTCOMING_FOLDER = 'DEMA_GALS2_noRC_noPipe_100000PTI_1av' #OUTCOMING_FOLDER='.' #TRS_FILENAME='trace-set.trs' TRS_FILENAME = 'DEMA_GALS2_noRC_noPipe_100000PTI.trs' input_filename = OUTCOMING_FOLDER + '/' + TRS_FILENAME ######################################### # main code ######################################### with trsfile.open(input_filename, 'r') as traces: # Show all headers for header, value in traces.get_headers().items(): print(header, '=', value) print() # Iterate over the first 25 traces for i, trace in enumerate(traces[0:25]): # for debug - stops at each trace processed # pdb.set_trace() # for debug. continue with 'c' print( '################################################################################' ) print('Trace {0:d}: {1:d} samples, name {2}'.format( i, len(trace), trace.title)) print(' initial 10 samples: {0}'.format(trace[0:10]))
#trs_filename=sys.argv[1] trs_filename = OUTCOMING_FOLDER + '/' + 'DEMA_GALS2_noRC_noPipe_100000PTI.trs' traces_filename = trs_filename + '.traces' input_filename = trs_filename + '.input' config_filename = trs_filename + '.config' traces_loop = 0 ######################################### # main code ######################################### traces_file = open(traces_filename, 'wb') tracename_file = open(input_filename, 'wb') with trsfile.open(trs_filename, 'r') as traces: # Show all headers for header, value in traces.get_headers().items(): print(header, '=', value) print() ntraces = traces.get_header(Header.NUMBER_TRACES) nsamples = traces.get_header(Header.NUMBER_SAMPLES) #isfloat = Header.SAMPLE_CODING.value == 63 isfloat = traces[0].sample_coding.is_float #samplesize #datasize #pdb.set_trace() # Iterate over all the traces for i, trace in enumerate(traces[:]): pdb.set_trace() # for debug. continue with 'c'