# HG changeset patch # User Janus Dam Nielsen <janus.niel...@alexandra.dk> # Date 1245394940 -7200 # Node ID 07a8329e75322d482dae15186422dd75e9ddb653 # Parent 4c4228af583fc965fb0722c5b051ffa213152f62 Implementation of input and shift commands.
diff --git a/viff/orlandi.py b/viff/orlandi.py --- a/viff/orlandi.py +++ b/viff/orlandi.py @@ -43,6 +43,12 @@ def __init__(self, runtime, field, value=None, rho=None, commitment=None): Share.__init__(self, runtime, field, (value, rho, commitment)) + def input(self, inputters, field, number=None, threshold=None): + """Input *number* to the computation. + + The input is shared using the :meth:`shift` method. + """ + return self.shift(inputters, field, number) class OrlandiRuntime(Runtime): """The Orlandi runtime. @@ -378,6 +384,64 @@ result.addCallbacks(compute_subs, self.error_handler) return result + @increment_pc + def shift(self, inputters, field, number): + """Shift of a share. + + Useful for input. + + Communication cost: ???. + + Assume the parties are given a random share [r] by a trusted dealer. + Then we denote the following protocol but [x] = Shift(P_{i}, x, [r]). + + 1) r = OpenTo(P_{i}, [r] + + 2) P_{i} broadcasts Delta = r - x + + 3) [x] = [r] - Delta + + """ + # TODO: Communitcation costs? + results = [] + for peer_id in inputters: + # Assume the parties are given a random share [r] by a trusted dealer. + share_r = self.random_share(field) + # 1) r = OpenTo(P_{i}, [r] + open_r = self.open(share_r, [peer_id]) + if peer_id == self.id: + # I am an inputter, send delta + pc = tuple(self.program_counter) + def f(r, share_r, pc): + my_delta = None + # 2) P_{i} broadcasts Delta = r - x + for other_id in self.players: + delta = r - number + if not (other_id == self.id): + self.protocols[other_id].sendShare(pc, delta) + else: + # 3) [x] = [r] - Delta + my_delta = self.sub(share_r, delta) + return my_delta + open_r.addCallback(f, share_r, pc) + open_r.addErrback(self.error_handler) + results.append(open_r) + else: + # I am not an inputter, wait for a delta + d = self._expect_share(peer_id, field) + def f(delta, share_r): + # 3) [x] = [r] - Delta + return self.sub(share_r, delta) + d.addCallback(f, share_r) + results.append(d) + + # do actual communication + self.activate_reactor() + + if len(results) == 1: + return results[0] + return results + def _additive_constant(self, zero, field_element): """Greate an additive constant. diff --git a/viff/test/test_orlandi_runtime.py b/viff/test/test_orlandi_runtime.py --- a/viff/test/test_orlandi_runtime.py +++ b/viff/test/test_orlandi_runtime.py @@ -15,7 +15,7 @@ # You should have received a copy of the GNU Lesser General Public # License along with VIFF. If not, see <http://www.gnu.org/licenses/>. -from twisted.internet.defer import gatherResults +from twisted.internet.defer import gatherResults, DeferredList from viff.test.util import RuntimeTestCase, protocol, BinaryOperatorTestCase from viff.runtime import Share @@ -218,3 +218,37 @@ return d +class OrlandiAdvancedCommandsTest(RuntimeTestCase): + """Test for advanced commands.""" + + # Number of players. + num_players = 3 + + runtime_class = OrlandiRuntime + + @protocol + def test_shift(self, runtime): + """Test addition of the shift command.""" + + def check(v): + self.assertEquals(v, 42) + + x = runtime.shift([2], self.Zp, 42) + d = runtime.open(x) + d.addCallback(check) + return d + + @protocol + def test_shift_two_inputters(self, runtime): + """Test addition of the shift command.""" + + def check(v): + self.assertEquals(v, 42) + + x, y = runtime.shift([1,3], self.Zp, 42) + d1 = runtime.open(x) + d1.addCallback(check) + d2 = runtime.open(y) + d2.addCallback(check) + return DeferredList([d1, d2]) + _______________________________________________ viff-devel mailing list (http://viff.dk/) viff-devel@viff.dk http://lists.viff.dk/listinfo.cgi/viff-devel-viff.dk