def test_secure_lightweight(self): test_array = array([ [1, -1, -1, 1, -1, 1], [-1, 1, 1, -1, -1, 1], ]) assert_array_equal( LTFArray.transform_lightweight_secure(test_array, k=3), [ [ [-1, -1, -1, 1, 1, -1], [-1, -1, 1, 1, -1, -1], [-1, 1, 1, -1, -1, -1], ], [ [-1, -1, -1, -1, 1, 1], [-1, -1, -1, 1, 1, -1], [-1, -1, 1, 1, -1, -1], ], ])
def test_secure_lightweight(self): """This method tests the secure lightweight transformation with predefined input and output.""" test_array = array([ [1, -1, -1, 1, -1, 1], [-1, 1, 1, -1, -1, 1], ], dtype=tools.BIT_TYPE) assert_array_equal( LTFArray.transform_lightweight_secure(test_array, k=3), [ [ [1, -1, 1, -1, -1, -1], [1, 1, -1, -1, 1, -1], [-1, 1, -1, 1, -1, 1], ], [ [1, -1, 1, -1, 1, 1], [-1, -1, -1, 1, 1, -1], [1, -1, 1, -1, -1, -1], ], ])
def __init__(self, n, k, training_set, validation_set, weights_mu=0, weights_sigma=1, weights_prng=RandomState(), lr_iteration_limit=1000, mini_batch_size=0, convergence_decimals=2, shuffle=False, logger=None): """ Initialize a Correlation Attack Learner for the specified LTF Array which uses transform_lightweight_secure. :param n: Input length :param k: Number of parallel LTFs in the LTF Array :param training_set: The training set, i.e. a data structure containing challenge response pairs :param validation_set: The validation set, i.e. a data structure containing challenge response pairs. Used for approximating accuracies of permuted models (can be smaller e.g. 0.1*training_set_size) :param weights_mu: mean of the Gaussian that is used to choose the initial model :param weights_sigma: standard deviation of the Gaussian that is used to choose the initial model :param weights_prng: PRNG to draw the initial model from. Defaults to fresh `numpy.random.RandomState` instance. :param lr_iteration_limit: Iteration limit for a single LR learner run :param logger: logging.Logger Logger which is used to log detailed information of learn iterations. """ self.n = n self.k = k self.validation_set_efba = ChallengeResponseSet( challenges=LTFArray.efba_bit( LTFArray.transform_lightweight_secure( validation_set.challenges, k)), responses=validation_set.responses) self.logger = logger self.lr_learner = LogisticRegression( t_set=training_set, n=n, k=k, transformation=LTFArray.transform_lightweight_secure, combiner=LTFArray.combiner_xor, weights_mu=weights_mu, weights_sigma=weights_sigma, weights_prng=weights_prng, logger=logger, iteration_limit=lr_iteration_limit, minibatch_size=mini_batch_size, convergence_decimals=convergence_decimals, shuffle=shuffle) self.initial_accuracy = .5 self.initial_lr_iterations = 0 self.initial_model = None self.total_lr_iterations = 0 self.best_permutation_iteration = 0 self.total_permutation_iterations = 0 self.best_permutation = None self.best_accuracy = None assert n in ( 64, 128 ), 'Correlation attack for %i bit is currently not supported.' % n assert validation_set.N >= 1000, 'Validation set should contain at least 1000 challenges.' self.correlation_permutations = loadmat( 'data/correlation_permutations_lightweight_secure_%i_10.mat' % n)['shiftOverviewData'][:, :, 0].astype('int64')