Exemple #1
0
 def test_send(self, runtime):
     msg_send = [100 * p + runtime.id for p in runtime.players]
     msg_receive = [100 * runtime.id + p for p in runtime.players]
     res = _send(runtime, msg_send)
     def verify(result):
         self.assertEquals(msg_receive, result)
     runtime.schedule_callback(res, verify)
     return res
    def test_send(self, runtime):
        msg_send = [100 * p + runtime.id for p in runtime.players]
        msg_receive = [100 * runtime.id + p for p in runtime.players]
        res = _send(runtime, msg_send)

        def verify(result):
            self.assertEquals(msg_receive, result)

        runtime.schedule_callback(res, verify)
        return res
Exemple #3
0
 def test_modified_paillier_can_encrypt_to_other(self, runtime):
     paillier = ModifiedPaillier(runtime, Random(57503))
     msg = []
     for p in runtime.players:
         msg.append(paillier.encrypt(runtime.id, player_id=p))
     received = _send(runtime, msg)
     def verify(enc):
         plain = [paillier.decrypt(e) for e in enc]
         self.assertEquals(range(1, self.num_players + 1), plain)
     runtime.schedule_callback(received, verify)
     return received
    def test_modified_paillier_can_encrypt_to_other(self, runtime):
        paillier = ModifiedPaillier(runtime, Random(57503))
        msg = []
        for p in runtime.players:
            msg.append(paillier.encrypt(runtime.id, player_id=p))
        received = _send(runtime, msg)

        def verify(enc):
            plain = [paillier.decrypt(e) for e in enc]
            self.assertEquals(range(1, self.num_players + 1), plain)

        runtime.schedule_callback(received, verify)
        return received
Exemple #5
0
    def do_add_macs(partial_share_contents, result_shares):
        """The transmission_restraint_constant is the number of
        encrypted shares we can safely transmit in one call to
        sendData. The sendData method can only transmit up to
        65536 bytes.
        The constant has been imperically determined by running
        TripleGenerator.generate_triples.
        TODO: How can we allow a user of the runtime to adjust this
        constraint at a higher level of abstraction?
        """
        transmission_restraint_constant = 50

        num_players = runtime.num_players

        list_of_player_to_enc_shares_lists = []

        player_to_mac_keys = [[] for x in runtime.players]
        player_to_enc_shares = [[] for x in runtime.players]
        for inx, partial_share_content in enumerate(partial_share_contents):
            if inx % transmission_restraint_constant == 0:
                player_to_enc_shares = [[] for x in runtime.players]
                list_of_player_to_enc_shares_lists.append(player_to_enc_shares)
            for j in xrange(num_players):
                # TODO: This is probably not the fastes way to generate
                # the betas.
                beta = random.randint(0, u_bound)
                if random.choice([True, False]):
                    beta = -beta
                enc_beta = paillier.encrypt(beta, player_id=j + 1)
                c_j = partial_share_content.enc_shares[j]
                n2 = paillier.get_modulus_square(j + 1)
                c = (fast_pow(c_j, alpha, n2) * enc_beta) % n2
                player_to_enc_shares[j].append(c)
                player_to_mac_keys[j].append(field(beta))

        received_cs = []
        for ls in list_of_player_to_enc_shares_lists:
            received_cs.append(_send(runtime, ls, deserialize=eval))

        def merge(received_cs):
            r = [[] for x in xrange(len(received_cs[0]))]
            for ls in received_cs:
                for inx, xs in enumerate(ls):
                    r[inx] += xs
            return r

        def finish_sharing(recevied_cs, partial_share_contents,
                           lists_of_mac_keys, result_shares):
            recevied_cs = merge(recevied_cs)
            shares = []
            for inx in xrange(0, len(partial_share_contents)):
                mac_keys = []
                decrypted_cs = []
                for c_list, mkeys in zip(recevied_cs, lists_of_mac_keys):
                    decrypted_cs.append(field(paillier.decrypt(c_list[inx])))
                    mac_keys.append(mkeys[inx])
                partial_share = partial_share_contents[inx]
                mac_key_list = BeDOZaKeyList(alpha, mac_keys)

                mac_msg_list = BeDOZaMACList(decrypted_cs)
                result_shares[inx].callback(
                    BeDOZaShareContents(partial_share.value, mac_key_list,
                                        mac_msg_list))
            return shares

        runtime.schedule_callback(gatherResults(received_cs), finish_sharing,
                                  partial_share_contents, player_to_mac_keys,
                                  result_shares)
        return received_cs
Exemple #6
0
    def do_add_macs(partial_share_contents, result_shares):
        """The transmission_restraint_constant is the number of
        encrypted shares we can safely transmit in one call to
        sendData. The sendData method can only transmit up to
        65536 bytes.
        The constant has been imperically determined by running
        TripleGenerator.generate_triples.
        TODO: How can we allow a user of the runtime to adjust this
        constraint at a higher level of abstraction?
        """
        transmission_restraint_constant = 50

        num_players = runtime.num_players

        list_of_player_to_enc_shares_lists = []

        player_to_mac_keys = [[] for x in runtime.players]
        player_to_enc_shares = [[] for x in runtime.players]
        for inx, partial_share_content in enumerate(partial_share_contents):
            if inx % transmission_restraint_constant == 0:
                player_to_enc_shares = [[] for x in runtime.players]
                list_of_player_to_enc_shares_lists.append(player_to_enc_shares)
            for j in xrange(num_players):
                # TODO: This is probably not the fastes way to generate
                # the betas.
                beta = random.randint(0, u_bound)
                if random.choice([True, False]):
                    beta = -beta
                enc_beta = paillier.encrypt(beta, player_id=j + 1)
                c_j = partial_share_content.enc_shares[j]
                n2 = paillier.get_modulus_square(j + 1)
                c = (fast_pow(c_j, alpha, n2) * enc_beta) % n2
                player_to_enc_shares[j].append(c)
                player_to_mac_keys[j].append(field(beta))

        received_cs = []
        for ls in list_of_player_to_enc_shares_lists:
            received_cs.append(_send(runtime, ls, deserialize=eval))

        def merge(received_cs):
            r = [[] for x in xrange(len(received_cs[0]))]
            for ls in received_cs:
                for inx, xs in enumerate(ls):
                    r[inx] += xs
            return r

        def finish_sharing(recevied_cs, partial_share_contents, lists_of_mac_keys, result_shares):
            recevied_cs = merge(recevied_cs)
            shares = []
            for inx in xrange(0, len(partial_share_contents)):
                mac_keys = []
                decrypted_cs = []
                for c_list, mkeys in zip(recevied_cs, lists_of_mac_keys):
                    decrypted_cs.append(field(paillier.decrypt(c_list[inx])))
                    mac_keys.append(mkeys[inx])
                partial_share = partial_share_contents[inx]
                mac_key_list = BeDOZaKeyList(alpha, mac_keys)

                mac_msg_list = BeDOZaMACList(decrypted_cs)
                result_shares[inx].callback(BeDOZaShareContents(partial_share.value, mac_key_list, mac_msg_list))
            return shares

        runtime.schedule_callback(
            gatherResults(received_cs), finish_sharing, partial_share_contents, player_to_mac_keys, result_shares
        )
        return received_cs