viff

view viff/bedoza/add_macs.py @ 1535:b4451e4ac76d

BeDOZa: Use gmpy for modular exponentiation.
author Thomas P Jakobsen <tpj@cs.au.dk>
date Tue Aug 10 16:03:54 2010 +0200 (21 months ago)
parents 4e8c0035e894
children
line source
1 # Copyright 2010 VIFF Development Team.
2 #
3 # This file is part of VIFF, the Virtual Ideal Functionality Framework.
4 #
5 # VIFF is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License (LGPL) as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 #
10 # VIFF is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
13 # Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with VIFF. If not, see <http://www.gnu.org/licenses/>.
18 import struct
20 from twisted.internet.defer import gatherResults
21 from viff.runtime import Share
23 from viff.bedoza.util import _send, fast_pow
24 from viff.bedoza.keylist import BeDOZaKeyList
25 from viff.bedoza.maclist import BeDOZaMACList
27 from viff.bedoza.shares import BeDOZaShare, BeDOZaShareContents
30 def add_macs(runtime, field, u_bound, alpha, random, paillier, partial_shares):
31 """Adds macs to the set of PartialBeDOZaShares.
33 Returns a deferred which yields a list of full shares, e.g.
34 including macs. (the full shares are deferreds of type
35 BeDOZaShare.)
36 """
37 # TODO: Would be nice with a class ShareContents like the class
38 # PartialShareContents used here.
40 runtime.increment_pc() # Huh!?
42 def do_add_macs(partial_share_contents, result_shares):
43 """The transmission_restraint_constant is the number of
44 encrypted shares we can safely transmit in one call to
45 sendData. The sendData method can only transmit up to
46 65536 bytes.
47 The constant has been imperically determined by running
48 TripleGenerator.generate_triples.
49 TODO: How can we allow a user of the runtime to adjust this
50 constraint at a higher level of abstraction?
51 """
52 transmission_restraint_constant = 50
54 num_players = runtime.num_players
56 list_of_player_to_enc_shares_lists = []
58 player_to_mac_keys = [ [] for x in runtime.players]
59 player_to_enc_shares = [ [] for x in runtime.players]
60 for inx, partial_share_content in enumerate(partial_share_contents):
61 if inx % transmission_restraint_constant == 0:
62 player_to_enc_shares = [ [] for x in runtime.players]
63 list_of_player_to_enc_shares_lists.append(player_to_enc_shares)
64 for j in xrange(num_players):
65 # TODO: This is probably not the fastes way to generate
66 # the betas.
67 beta = random.randint(0, u_bound)
68 if random.choice([True, False]):
69 beta = -beta
70 enc_beta = paillier.encrypt(beta, player_id = j + 1)
71 c_j = partial_share_content.enc_shares[ j ]
72 n2 = paillier.get_modulus_square( j + 1 )
73 c = (fast_pow(c_j, alpha, n2) * enc_beta) % n2
74 player_to_enc_shares[j].append(c)
75 player_to_mac_keys[j].append(field(beta))
77 received_cs = []
78 for ls in list_of_player_to_enc_shares_lists:
79 received_cs.append(_send(runtime, ls, deserialize=eval))
81 def merge(received_cs):
82 r = [ [] for x in xrange(len(received_cs[0]))]
83 for ls in received_cs:
84 for inx, xs in enumerate(ls):
85 r[inx] += xs
86 return r
88 def finish_sharing(recevied_cs, partial_share_contents,
89 lists_of_mac_keys, result_shares):
90 recevied_cs = merge(recevied_cs)
91 shares = []
92 for inx in xrange(0, len(partial_share_contents)):
93 mac_keys = []
94 decrypted_cs = []
95 for c_list, mkeys in zip(recevied_cs,
96 lists_of_mac_keys):
97 decrypted_cs.append(field(paillier.decrypt(c_list[inx])))
98 mac_keys.append(mkeys[inx])
99 partial_share = partial_share_contents[inx]
100 mac_key_list = BeDOZaKeyList(alpha, mac_keys)
102 mac_msg_list = BeDOZaMACList(decrypted_cs)
103 result_shares[inx].callback(
104 BeDOZaShareContents(partial_share.value, mac_key_list,
105 mac_msg_list))
106 return shares
108 runtime.schedule_callback(gatherResults(received_cs),
109 finish_sharing,
110 partial_share_contents,
111 player_to_mac_keys,
112 result_shares)
113 return received_cs
115 result_shares = [Share(runtime, field)
116 for x in xrange(len(partial_shares))]
117 runtime.schedule_callback(gatherResults(partial_shares),
118 do_add_macs,
119 result_shares)
120 return result_shares