def get_ascad_trace_set(name, data, meta, limit=None): """ Convert ASCAD data to a TraceSet object. """ data_x, data_y = data traces = [] plaintexts = [] keys = [] masks = [] limit = len(data_x) if limit is None else min(len(data_x), limit) for i in range(0, limit): traces.append(data_x[i]) plaintexts.append(meta[i]['plaintext']) keys.append(meta[i]['key']) masks.append(meta[i]['masks']) traces = np.array(traces) plaintexts = np.array(plaintexts) keys = np.array(keys) masks = np.array(masks) trace_set = TraceSet(name='ascad-%s' % name, traces=traces, plaintexts=plaintexts, ciphertexts=None, keys=keys, masks=masks) trace_set.window = Window(begin=0, end=len(trace_set.traces[0].signal)) trace_set.windowed = True return trace_set
def on_epoch_end(self, epoch, logs=None): logs = logs or {} if epoch % self.metric_freq != 0 or epoch == 0: return if self.trace_set is not None: # Fetch inputs from trace_set x = AIInput(self.conf).get_trace_set_inputs(self.trace_set) if self.cnn: x = np.expand_dims(x, axis=-1) encodings = self.model.predict(x) # Output: [?, 16] # Store encodings as fake traceset keys = np.array([trace.key for trace in self.trace_set.traces]) plaintexts = np.array( [trace.plaintext for trace in self.trace_set.traces]) fake_ts = TraceSet(traces=encodings, plaintexts=plaintexts, keys=keys, name="fake_ts") fake_ts.window = Window(begin=0, end=encodings.shape[1]) fake_ts.windowed = True for i in range(self.key_low, self.key_high): if len(set(keys[:, i])) > 1: print( "Warning: nonidentical key bytes detected. Skipping rank calculation" ) print("Subkey %d:" % i) print(keys[:, i]) break rank, confidence = calculate_traceset_rank( fake_ts, i, keys[0][i], self.conf ) # TODO: It is assumed here that all true keys of the test set are the same self._save_best_rank_model(rank, confidence) logs['rank %d' % i] = rank logs['confidence %d' % i] = confidence #self._save_best_rank_model(np.mean(ranks)) else: print("Warning: no trace_set supplied to RankCallback")