def get_model(model_name, db, num_classes=256, batch_size=100, epochs=75, new=False): if new is False: try: check_file_exists(model_name) # print('oke') return load_model(model_name) except: pass (x_profiling, y_profiling), (x_attack, y_attack) = load_ascad(db) y_profiling = to_categorical(y_profiling, num_classes=num_classes, dtype='int32') y_attack = to_categorical(y_attack, num_classes=num_classes, dtype='int32') save_model = ModelCheckpoint(model_name) callbacks = [save_model] # model = spread_model(num_classes) # model = mlp_model(num_classes) model = cnn_model(num_classes) x_profiling = x_profiling[0:5000] y_profiling = y_profiling[0:5000] # num_traces = 500 # x_profiling = x_profiling[:num_traces, :] # y_profiling = y_profiling[:num_traces, :] if len(model.get_layer(index=0).input_shape) == 3: x_profiling = x_profiling.reshape((x_profiling.shape[0], x_profiling.shape[1], 1)) model.fit(x_profiling, y_profiling, epochs=epochs, batch_size=batch_size, callbacks=callbacks) return model
nn.ReLU(), nn.Linear(n_hidden, n_hidden), nn.ReLU(), nn.Linear(n_hidden, n_hidden), nn.ReLU(), nn.Linear(n_hidden, n_hidden), nn.ReLU(), nn.Linear(n_hidden, num_classes), ).to(device) traces_file = '/media/rico/Data/TU/thesis/data/ASCAD.h5' (x_profiling, y_profiling), (x_attack, y_attack), (metadata_profiling, metadata_attack) = load_ascad(traces_file, load_metadata=True) use_hw = True n_classes = 9 if use_hw else 256 if use_hw: y_profiling = np.array([HW[val] for val in y_profiling]) y_attack = np.array([HW[val] for val in y_attack]) # net = seq(700, n_classes) net = Net(out_shape=n_classes) print(net) optimizer = torch.optim.Adam(net.parameters(), lr=0.00001) criterion = nn.CrossEntropyLoss().to(device) epochs = 700
from util import load_ascad, HW, SBOX import numpy as np path = '/media/rico/Data/TU/thesis' desync = 0 sub_key_index = 2 unmask = True use_hw = False s_index = 2 trace_file = '{}/data/ASCAD/ASCAD_{}_desync{}.h5'.format( path, sub_key_index, desync) (_, _), (_, _), (_, metadata_attack) = \ load_ascad(trace_file, load_metadata=True) print("Loaded traces") # Perform some operations that are desired # if unmask: # y_attack = np.array([y_attack[i] ^ metadata_attack[i]['masks'][0] for i in range(len(y_attack))]) # if use_hw: # y_attack = np.array([HW[val] for val in y_attack]) key_guesses = np.zeros((len(metadata_attack), 256), dtype=int) print("shape key guesses: {}".format(np.shape(key_guesses))) print("Len of metadata attack: {}".format(len(metadata_attack))) for i in range(len(metadata_attack)): plaintext = metadata_attack[i]['plaintext'][s_index] for key_guess in range(256): # res = (SBOX[plaintext ^ key_guess] ^ metadata_attack[i]['masks'][0]).astype(np.int)
def get_ranks(use_hw, runs, train_size, epochs, lr, sub_key_index, attack_size, rank_step, unmask, network_name): ranks_x = [] ranks_y = [] epochs_checkpoint = [ '.1.pt', '.20.pt', '.40.pt', '.60.pt', '.80.pt', '.100.pt', '' ] for epoch in epochs_checkpoint: model_path = '/media/rico/Data/TU/thesis/runs/ASCAD/subkey_{}/{}_SF{}_E{}_BZ{}_LR{}/train{}/model_r{}_{}.pt{}'.format( sub_key_index, type_network, spread_factor, epochs, batch_size, '%.2E' % Decimal(lr), train_size, 1, # the run network_name, epoch) print('path={}'.format(model_path)) model = SpreadNetIn.load_model(model_path) print("Using {}".format(model)) model.to(device) (_, _), (x_attack, y_attack), (metadata_profiling, metadata_attack) = load_ascad(trace_file, load_metadata=True) permutation = np.random.permutation(x_attack.shape[0]) x_attack = shuffle_permutation(permutation, np.array(x_attack)) y_attack = shuffle_permutation(permutation, np.array(y_attack)) metadata_attack = shuffle_permutation(permutation, np.array(metadata_attack)) x, y = test(x_attack, y_attack, metadata_attack, network=model, sub_key_index=sub_key_index, use_hw=use_hw, attack_size=attack_size, rank_step=rank_step, unmask=unmask, only_accuracy=only_accuracy) if isinstance(model, SpreadNetIn): # Get the intermediate values right after the first fully connected layer z = np.transpose(model.intermediate_values2[0]) # Calculate the mse for the maximum and minumum from these traces and the learned min and max min_z = np.min(z, axis=1) max_z = np.max(z, axis=1) msq_min = np.mean(np.square(min_z - model.tensor_min), axis=None) msq_max = np.mean(np.square(max_z - model.tensor_max), axis=None) print('msq min: {}'.format(msq_min)) print('msq max: {}'.format(msq_max)) # Plot the distribution of each neuron right after the first fully connected layer for k in [53]: plt.grid(True) plt.axvline(x=model.tensor_min[k], color='green', lw=5) plt.axvline(x=model.tensor_max[k], color='green', lw=5) leg = plt.legend() # get the individual lines inside legend and set line width plt.hist(z[:][k], bins=40) plt.xlim([-50, 400]) plt.xlabel("Value") plt.ylabel("Frequency") fig = plt.gcf() fig.set_size_inches(16, 9) fig.savefig( '/media/rico/Data/TU/thesis/report/img/spread/inter/spread-{}.png' .format(epoch.replace('.pt', '').replace('.', '')), dpi=100) plt.show() # Retrieve the intermediate values right after the spread layer, # and order them such that each 6 values after each other belong to the neuron of the # previous layer v = model.intermediate_values order = [ int((x % spread_factor) * 100 + math.floor(x / spread_factor)) for x in range(spread_factor * 100) ] inter = [] for x in range(len(v[0])): inter.append([v[0][x][j] for j in order]) # Calculate the standard deviation of each neuron in the spread layer std = np.std(inter, axis=0) threshold = 1.0 / attack_size * 10 print("divby: {}".format(threshold)) res = np.where(std < threshold, 1, 0) # Calculate the mean of each neuron in the spread layer mean_res = np.mean(inter, axis=0) # mean_res2 = np.where(mean_res < threshold, 1, 0) mean_res2 = np.where(mean_res == 0.0, 1, 0) print('Sum std results {}'.format(np.sum(res))) print('Sum mean results {}'.format(np.sum(mean_res2))) # Check which neurons have a std and mean where it is smaller than threshold total_same = 0 for j in range(len(mean_res2)): if mean_res2[j] == 1 and res[j] == 1: total_same += 1 print('Total same: {}'.format(total_same)) # Plot the standard deviations plt.title('Comparison of networks') plt.xlabel('#neuron') plt.ylabel('std') xcoords = [j * spread_factor for j in range(100)] for xc in xcoords: plt.axvline(x=xc, color='green') plt.grid(True) plt.plot(std, label='std') plt.figure() # Plot the means plt.title('Performance of networks') plt.xlabel('#neuron') plt.ylabel('mean') for xc in xcoords: plt.axvline(x=xc, color='green') plt.grid(True) plt.plot(mean_res, label='mean') plt.legend() plt.show() return ranks_x, ranks_y
import numpy as np from sklearn.preprocessing import StandardScaler import util path = "/media/rico/Data/TU/thesis/data/" path_ascad = path + "ASCAD/" desync = 100 subkey = 2 traces_file = '{}/ASCAD_{}_desync{}.h5'.format(path_ascad, subkey, desync) print('Loading {}'.format(traces_file)) (x_train_traces, y_train_traces), (x_test_traces, y_test_traces), (metadata_profiling, metadata_attack) = \ util.load_ascad(traces_file, load_metadata=True) train_size = 45000 validation_size = 1000 test_size = 10000 x_train = x_train_traces[0:train_size] x_validation = x_train_traces[train_size:train_size + validation_size] x_test = x_test_traces print("loaded") scale = StandardScaler() x_train = scale.fit_transform(x_train) x_validation = scale.transform(x_validation) x_test = scale.transform(x_test) new_data = x_train
# num_traces = 500 # x_profiling = x_profiling[:num_traces, :] # y_profiling = y_profiling[:num_traces, :] if len(model.get_layer(index=0).input_shape) == 3: x_profiling = x_profiling.reshape((x_profiling.shape[0], x_profiling.shape[1], 1)) model.fit(x_profiling, y_profiling, epochs=epochs, batch_size=batch_size, callbacks=callbacks) return model if __name__ == "__main__": for sub_key_index in range(2, 3): model_n = "_model_subkey_{}".format(sub_key_index) # file = '/Data/TU/thesis/src/data/ASCAD_data/ASCAD_databases/subkeys/ASCAD_subkey_{}'.format(sub_key_index) file = '/media/rico/Data/TU/thesis/data/ASCAD.h5' use_hw = True n_classes = 8 if use_hw else 256 model = get_model(model_n, file, epochs=80, batch_size=100, new=False) (_, _), (x_test, y_test), (metadata_profiling, metadata_attack) = \ load_ascad(file, load_metadata=True) x_test = x_test.reshape((x_test.shape[0], x_test.shape[1], 1)) predi = model.predict(x_test) x, y = test_model(predi, metadata_attack, sub_key_index) plt.plot(x, y) plt.grid(True) plt.show()
def get_ranks(use_hw, runs, train_size, epochs, lr, sub_key_index, attack_size, rank_step, unmask, network_name, kernel_size_string=""): ranks_x = [] ranks_y = [] (_, _), (x_attack, y_attack), (metadata_profiling, metadata_attack) = load_ascad(trace_file, load_metadata=True) key_guesses = util.load_csv( '/media/rico/Data/TU/thesis/data/ASCAD/key_guesses.csv', delimiter=' ', dtype=np.int, start=0, size=attack_size) x_attack = x_attack[:attack_size] y_attack = y_attack[:attack_size] if unmask: if use_hw: y_attack = np.array([ y_attack[i] ^ metadata_attack[i]['masks'][0] for i in range(len(y_attack)) ]) else: y_attack = np.array([ util.HW[y_attack[i] ^ metadata_attack[i]['masks'][0]] for i in range(len(y_attack)) ]) real_key = metadata_attack[0]['key'][sub_key_index] for run in runs: folder = '/media/rico/Data/TU/thesis/runs2/{}/subkey_{}/{}{}{}_SF{}_' \ 'E{}_BZ{}_LR{}/train{}/'.format( str(data_set), sub_key_index, '' if unmask else 'masked/', '' if desync is 0 else 'desync{}/'.format(desync), type_network, spread_factor, epochs, batch_size, '%.2E' % Decimal(lr), train_size) model_path = '{}/model_r{}_{}{}.pt'.format(folder, run, network_name, kernel_size_string) print('path={}'.format(model_path)) model = load_model(network_name, model_path) model.eval() print("Using {}".format(model)) model.to(device) # Load additional plaintexts dk_plain = None if network_name in req_dk: dk_plain = metadata_attack[:]['plaintext'][:, sub_key_index] dk_plain = hot_encode(dk_plain, 9 if use_hw else 256, dtype=np.float) # Calculate predictions predictions = accuracy(model, x_attack, y_attack, dk_plain) predictions = predictions.cpu().numpy() x, y = [], [] for exp_i in range(num_exps): permutation = permutations[exp_i] # Shuffle data predictions_shuffled = shuffle_permutation(permutation, np.array(predictions)) key_guesses_shuffled = shuffle_permutation(permutation, key_guesses) # Test the data x_exp, y_exp = test_with_key_guess_p(key_guesses_shuffled, predictions_shuffled, attack_size=attack_size, real_key=real_key, use_hw=use_hw) x = x_exp y.append(y_exp) # Calculate the mean over the experimentfs y = np.mean(y, axis=0) util.save_np('{}/model_r{}_{}{}.exp'.format(folder, run, network_name, kernel_size_string), y, f="%f") if isinstance(model, SpreadNetIn): # Get the intermediate values right after the first fully connected layer z = np.transpose(model.intermediate_values2[0]) # Calculate the mse for the maximum and minimum from these traces and the learned min and max min_z = np.min(z, axis=1) max_z = np.max(z, axis=1) msq_min = np.mean(np.square(min_z - model.tensor_min), axis=None) msq_max = np.mean(np.square(max_z - model.tensor_max), axis=None) print('msq min: {}'.format(msq_min)) print('msq max: {}'.format(msq_max)) # Plot the distribution of each neuron right after the first fully connected layer for k in [50]: plt.grid(True) plt.axvline(x=model.tensor_min[k], color='green') plt.axvline(x=model.tensor_max[k], color='green') plt.hist(z[:][k], bins=40) plt.show() exit() # Retrieve the intermediate values right after the spread layer, # and order them such that each 6 values after each other belong to the neuron of the # previous layer v = model.intermediate_values order = [ int((x % spread_factor) * 100 + math.floor(x / spread_factor)) for x in range(spread_factor * 100) ] inter = [] for x in range(len(v[0])): inter.append([v[0][x][j] for j in order]) # Calculate the standard deviation of each neuron in the spread layer std = np.std(inter, axis=0) threshold = 1.0 / attack_size * 10 print("divby: {}".format(threshold)) res = np.where(std < threshold, 1, 0) # Calculate the mean of each neuron in the spread layer mean_res = np.mean(inter, axis=0) # mean_res2 = np.where(mean_res < threshold, 1, 0) mean_res2 = np.where(mean_res == 0.0, 1, 0) print('Sum std results {}'.format(np.sum(res))) print('Sum mean results {}'.format(np.sum(mean_res2))) # Check which neurons have a std and mean where it is smaller than threshold total_same = 0 for j in range(len(mean_res2)): if mean_res2[j] == 1 and res[j] == 1: total_same += 1 print('Total same: {}'.format(total_same)) # Plot the standard deviations plt.title('Comparison of networks') plt.xlabel('#neuron') plt.ylabel('std') xcoords = [j * spread_factor for j in range(100)] for xc in xcoords: plt.axvline(x=xc, color='green') plt.grid(True) plt.plot(std, label='std') plt.figure() # Plot the means plt.title('Performance of networks') plt.xlabel('#neuron') plt.ylabel('mean') for xc in xcoords: plt.axvline(x=xc, color='green') plt.grid(True) plt.plot(mean_res, label='mean') plt.legend() plt.show() ranks_x.append(x) ranks_y.append(y) return ranks_x, ranks_y
def get_ranks(use_hw, runs, train_size, epochs, lr, sub_key_index, attack_size, rank_step, unmask, network_name, kernel_size_string=""): ranks_x = [] ranks_y = [] (_, _), (x_attack, y_attack), (metadata_profiling, metadata_attack) = load_ascad(trace_file, load_metadata=True) key_guesses = util.load_csv('{}/ASCAD/key_guesses.csv'.format(traces_path), delimiter=' ', dtype=np.int, start=0, size=attack_size) x_attack = x_attack[:attack_size] y_attack = y_attack[:attack_size] if unmask: if use_hw: y_attack = np.array([ y_attack[i] ^ metadata_attack[i]['masks'][0] for i in range(len(y_attack)) ]) else: y_attack = np.array([ util.HW[y_attack[i] ^ metadata_attack[i]['masks'][0]] for i in range(len(y_attack)) ]) real_key = metadata_attack[0]['key'][sub_key_index] for run in runs: folder = '{}/{}/subkey_{}/{}{}{}_SF{}_' \ 'E{}_BZ{}_LR{}/train{}/'.format( models_path, str(data_set), sub_key_index, '' if unmask else 'masked/', '' if desync is 0 else 'desync{}/'.format(desync), type_network, spread_factor, epochs, batch_size, '%.2E' % Decimal(lr), train_size) model_path = '{}/model_r{}_{}{}.pt'.format(folder, run, network_name, kernel_size_string) print('path={}'.format(model_path)) model = load_model(network_name, model_path) model.eval() print("Using {}".format(model)) model.to(device) # Load additional plaintexts dk_plain = None if network_name in req_dk: dk_plain = metadata_attack[:]['plaintext'][:, sub_key_index] dk_plain = hot_encode(dk_plain, 9 if use_hw else 256, dtype=np.float) # Calculate predictions predictions = accuracy(model, x_attack, y_attack, dk_plain) predictions = predictions.cpu().numpy() # Shuffle the data using same permutation for n_exp and calculate mean for GE of the model x, y = [], [] for exp_i in range(num_exps): permutation = permutations[exp_i] # Shuffle data predictions_shuffled = shuffle_permutation(permutation, np.array(predictions)) key_guesses_shuffled = shuffle_permutation(permutation, key_guesses) # Test the data x_exp, y_exp = test_with_key_guess_p(key_guesses_shuffled, predictions_shuffled, attack_size=attack_size, real_key=real_key, use_hw=use_hw) x = x_exp y.append(y_exp) # Calculate the mean over the experiments y = np.mean(y, axis=0) util.save_np('{}/model_r{}_{}{}.exp'.format(folder, run, network_name, kernel_size_string), y, f="%f") ranks_x.append(x) ranks_y.append(y) return ranks_x, ranks_y