[viff-devel] Error when coding with callbacks and reveals
Martin Geisler
mg at daimi.au.dk
Mon Mar 9 09:43:37 PDT 2009
Tord Ingolf Reistad <tordr at stud.ntnu.no> writes:
I just took a closer look at your code -- some comments below :-)
> #!/usr/bin/python
>
> # Copyright 2007, 2008 VIFF Development Team.
The year should be 2009 since this is the first year in which this code
is released.
> class Protocol:
>
> def __init__(self, runtime):
> # Save the Runtime for later use
> self.runtime = runtime
> own_id = self.runtime.id
>
> Zp = GF(43)
> test_vector = [0, 1, 6, 0]
> self.value = test_vector[own_id - 1]
> m1, m2, m3 = runtime.shamir_share([1, 2, 3], Zp, self.value)
> shares = [m1, m2, m3]
> r_as_bits = [m1, m2, m1]
> r = m3
> mask = m3
> x = m3
> result = self.protocol_part_1(r_as_bits, r, mask, x)
> callback_result = result.addCallback(self.print_answer)
The addCallback method returns self -- so after that assignment we have
callback_result == result
So normally you wont save the result of addCallback.
> #results.addCallback(lambda _: runtime.synchronize())
> m1.addCallback(lambda _: runtime.shutdown())
>
> #Create_random_bits
> #Convert_to_test_values
> #Test_values if useable use random bits otherwise find new bits
> #c = r + x
> #Compare (c,r)
>
> def protocol_part_1(self, r_as_bits, r, mask, x):
> test = self.test_not_all_zero(r_as_bits, mask)
> self.r = r
> self.x = x
> return test.addCallback(self.protocol_part_2)
>
> def protocol_part_2(self, test):
> if test:
> c = self.r + self.x
> c_open = self.runtime.open(c)
> result = gather_shares([c_open])
The gather_shares function may have an unfortunate name... it is not
used to collect the shares from a single number. Instead it is used to
collect a list of several shares.
So if you need to wait on a and b you do:
x = gather_shares([a, b])
Now x is a Share which will call its callbacks with a list of *two*
field elements. This means that we can do this:
x.addCallback(lambda results: result[0] + result[1])
You have now made a Share which represents the future sum of a and b!
So in your case I would do
c_open = self.runtime.open(c)
c_open.addCallback(self.print_answer)
return c_open
> def test_not_all_zero(self, r_as_bits, mask):
> #reveal (sum(bits))*mask
> temp = sum(r_as_bits[:]) * mask
> temp_open = self.runtime.open(temp)
>
> def check_result(check):
> print "Comparing ", check, " to 0"
> if check[0].value == 0:
Here you see how check_result is called with a 1-item list because you
use gather_shares below. If you simply add check_result as a callback to
temp_open, then things become simpler.
And you can just compare a field element with an integer, so
if check == 0
should work just fine.
> return False
> return True
>
> results = gather_shares([temp_open])
> test_results = results.addCallback(check_result)
> return test_results
Like above you can just add the check_result callback to temp_open and
return temp_open.
--
Martin Geisler
VIFF (Virtual Ideal Functionality Framework) brings easy and efficient
SMPC (Secure Multiparty Computation) to Python. See: http://viff.dk/.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://lists.viff.dk/pipermail/viff-devel-viff.dk/attachments/20090309/3180e68a/attachment.pgp>
More information about the viff-devel
mailing list