def column(self, index): if isinstance(index, Share): is_selected = mpc.unit_vector(index, self.number_of_attributes) return self.map(lambda sample: ObliviousArray.create(sample.inputs) .select(is_selected).sum()) else: return self.map(lambda sample: sample.inputs[index])
def create(cls, *samples, continuous=None, labels=None): samples = ObliviousArray.create(samples) number_of_attributes = len(samples[0]) if len(samples) > 0 else 0 if not continuous: continuous = [False for i in range(number_of_attributes)] if not labels: labels = [secint(i) for i in range(number_of_attributes)] return ObliviousDataset(samples, number_of_attributes, continuous, labels)
def test_getitem_with_secret_index(self): array = ObliviousArray.create(s(10), s(20), s(30)) item = array.getitem(s(1)) self.assertEqual(reveal(item), 20)
def test_initialize_with_list(self): self.assertEqual(reveal(ObliviousArray.create(s(1), s(2))), reveal(ObliviousArray([s(1), s(2)]))) self.assertEqual(reveal(ObliviousArray.create(s(1))), reveal(ObliviousArray([s(1)])))
def test_sum_on_selected_elements(self): array = ObliviousArray.create(s(10), s(20), s(30)) array = array.select(s(1), s(0), s(1)) sum = array.sum() self.assertEqual(reveal(sum), 40)
def test_len_of_subset(self): dataset = ObliviousArray.create(s(0), s(1), s(2)) dataset = dataset.select(s(1), s(0), s(1)) self.assertEqual(reveal(dataset.len()), 2)
def is_continuous(self, attribute_index): if isinstance(attribute_index, Share): continuous = ObliviousArray(list(map(secint, self.continuous))) return continuous.getitem(attribute_index) else: return self.continuous[attribute_index]
def sort_array(column, outcomes): values, outcome_values = sort_lists(column.values, outcomes.values) return (ObliviousArray(values), ObliviousArray(outcome_values))
def test_map_on_selected_elements(self): array = ObliviousArray.create(s(10), s(20), s(30)) array = array.select(s(1), s(0), s(1)) array = array.map(lambda x: 2 * x) self.assertEqual(reveal(array), [20, 60])
def test_map(self): array = ObliviousArray.create(s(10), s(20), s(30)) array = array.map(lambda x: 2 * x) self.assertEqual(reveal(array), [20, 40, 60])
def test_select_with_selection_as_argument(self): array = ObliviousArray.create(s(10), s(20), s(30)) included = ObliviousArray.create(s(1), s(0), s(1)) included = included.select(s(0), s(0), s(1)) array = array.select(included) self.assertEqual(reveal(array), [30])
def test_select_and_select_again(self): array = ObliviousArray.create(s(10), s(20), s(30)) array = array.select(s(1), s(0), s(1)) array = array.select(s(0), s(1), s(1)) self.assertEqual(reveal(array), [30])
def test_reveal_selected_elements(self): array = ObliviousArray.create(s(10), s(20), s(30)) array = array.select(s(0), s(1), s(0)) self.assertEqual(reveal(array), [20])
def test_reveal_all_elements(self): array = ObliviousArray.create(s(10), s(20), s(30)) self.assertEqual(reveal(array), [10, 20, 30])
def test_getitem_on_selected_elements(self): array = ObliviousArray.create(s(10), s(20), s(30)) selection = array.select(s(1), s(0), s(1)) self.assertEqual(reveal(selection.getitem(s(0))), 10) self.assertEqual(reveal(selection.getitem(s(1))), 0) self.assertEqual(reveal(selection.getitem(s(2))), 30)
def arr(*values): return ObliviousArray([*values])
def test_reduce(self): array = ObliviousArray.create(s(10), s(20), s(30)) product = array.reduce(s(1), operator.mul) self.assertEqual(reveal(product), 6000)
def test_reduce_on_selected_elements(self): array = ObliviousArray.create(s(10), s(20), s(30)) array = array.select(s(1), s(0), s(1)) product = array.reduce(s(1), operator.mul) self.assertEqual(reveal(product), 300)
def test_reduce_with_initial_value(self): array = ObliviousArray.create() product = array.reduce(s(1), operator.mul, initial=s(42)) self.assertEqual(reveal(product), 42)
def label(self, attribute_index): labels = ObliviousArray(self.labels) return labels.getitem(attribute_index)
def test_sum(self): array = ObliviousArray.create(s(10), s(20), s(30)) sum = array.sum() self.assertEqual(reveal(sum), 60)
def select_best_attribute(samples): (gains, thresholds) = calculate_gains(samples) (_, index) = maximum(ObliviousArray.create(gains)) threshold = ObliviousArray(thresholds).getitem(index) return (index, threshold)
def test_len(self): self.assertEqual(reveal(ObliviousArray.create().len()), 0) self.assertEqual(reveal(ObliviousArray([s(0), s(1), s(2)]).len()), 3)