def shuffle(x, config=None, value_type=sgf2n, reverse=False): """ Simulate secure shuffling with Waksman network for 2 players. Returns the network switching config so it may be re-used later. """ n = len(x) if n & (n - 1) != 0: raise CompilerError('shuffle requires n a power of 2') if config is None: config = permutation.configure_waksman(permutation.random_perm(n)) for i, c in enumerate(config): config[i] = [value_type(b) for b in c] permutation.waksman(x, config, reverse=reverse) permutation.waksman(x, config, reverse=reverse) return config
def shuffle(x, config=None, value_type=sgf2n, reverse=False): """ Simulate secure shuffling with Waksman network for 2 players. Returns the network switching config so it may be re-used later. """ n = len(x) if n & (n-1) != 0: raise CompilerError('shuffle requires n a power of 2') if config is None: config = permutation.configure_waksman(permutation.random_perm(n)) for i,c in enumerate(config): config[i] = [value_type(b) for b in c] permutation.waksman(x, config, reverse=reverse) permutation.waksman(x, config, reverse=reverse) return config
def config_shuffle_given_perm(perm, value_type=sint): """ Compute config for oblivious shuffling. Take mod 2 for active sec. """ n = len(perm) if n & (n - 1) != 0: # pad permutation to power of 2 m = 2 ** int(math.ceil(math.log(n, 2))) perm += list(range(n, m)) config_bits = configure_waksman(perm) # 2-D array config = Array(len(config_bits) * len(perm), value_type.reg_type) for i, c in enumerate(config_bits): for j, b in enumerate(c): config[i * len(perm) + j] = b return config
def default_config_shuffle(values, use_iter=True): """Configures waksman network for default shuffle algorithm.""" if use_iter: return config_shuffle_for_length(len(values)) else: return configure_waksman(random_perm(len(values)))