Exemple #1
0
    def _mul(self, inx, jnx, n, ais=None, cjs=None):
        """Multiply each of the field elements in *ais* with the
        corresponding encrypted elements in *cjs*.
        
        Returns a deferred which will yield a list of field elements.
        """
        CKIND = 1
        """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 = 425

        number_of_packets = n / transmission_restraint_constant
        if n % transmission_restraint_constant != 0:
            number_of_packets += 1

        self.runtime.increment_pc()

        pc = tuple(self.runtime.program_counter)

        deferred = []
        zis = []
        if self.runtime.id == inx:
            Nj_square = self.paillier.get_modulus_square(jnx)
            all_cs = []
            for iny, (ai, cj) in enumerate(zip(ais, cjs)):
                if iny % transmission_restraint_constant == 0:
                    cs = []
                    all_cs.append(cs)
                u = rand.randint(0, self.u_bound)
                Ej_u = self.paillier.encrypt(u, jnx)
                cs.append(
                    (fast_pow(cj, ai.value, Nj_square) * Ej_u) % Nj_square)
                zi = self.Zp(-u)
                zis.append(zi)

            for cs in all_cs:
                self.runtime.protocols[jnx].sendData(pc, CKIND, str(cs))

        if self.runtime.id == jnx:
            all_cs = []
            for _ in xrange(number_of_packets):
                cs = Deferred()
                self.runtime._expect_data(inx, CKIND, cs)
                all_cs.append(cs)

            def decrypt(all_cs, pc, zis):
                zjs = []
                cs = reduce(lambda x, y: x + eval(y), all_cs, [])
                for iny, c in enumerate(cs):
                    t = self.paillier.decrypt(c)
                    zj = self.Zp(t)
                    zjs.append(zj)
                if not zis == []:
                    return [x + y for x, y in zip(zis, zjs)]
                else:
                    return zjs

            all_cs_d = gatherResults(all_cs)
            all_cs_d.addCallback(decrypt, pc, zis)
            deferred = all_cs_d
        else:
            zis_deferred = Deferred()
            zis_deferred.callback(zis)
            deferred = zis_deferred

        return deferred
Exemple #2
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 #3
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 #4
0
    def _mul(self, inx, jnx, n, ais=None, cjs=None):
        """Multiply each of the field elements in *ais* with the
        corresponding encrypted elements in *cjs*.
        
        Returns a deferred which will yield a list of field elements.
        """
        CKIND = 1
 
        """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 = 425

        number_of_packets = n / transmission_restraint_constant
        if n % transmission_restraint_constant != 0:
            number_of_packets += 1
        
        self.runtime.increment_pc()

        pc = tuple(self.runtime.program_counter)

        deferred = []
        zis = []
        if self.runtime.id == inx:
            Nj_square = self.paillier.get_modulus_square(jnx)
            all_cs = []
            for iny, (ai, cj) in enumerate(zip(ais, cjs)):
                if iny % transmission_restraint_constant == 0:
                    cs = []
                    all_cs.append(cs)
                u = rand.randint(0, self.u_bound)
                Ej_u = self.paillier.encrypt(u, jnx)
                cs.append( (fast_pow(cj, ai.value, Nj_square) * Ej_u) % Nj_square )
                zi = self.Zp(-u)
                zis.append(zi)
                
            for cs in all_cs:
                self.runtime.protocols[jnx].sendData(pc, CKIND, str(cs))

        if self.runtime.id == jnx:
            all_cs = []
            for _ in xrange(number_of_packets):
                cs = Deferred()
                self.runtime._expect_data(inx, CKIND, cs)
                all_cs.append(cs)
                
            def decrypt(all_cs, pc, zis):
                zjs = []
                cs = reduce(lambda x, y: x + eval(y), all_cs, [])
                for iny, c in enumerate(cs):
                    t = self.paillier.decrypt(c)
                    zj = self.Zp(t)
                    zjs.append(zj)
                if not zis == []:
                    return [x + y for x, y in zip(zis, zjs)]
                else:
                    return zjs 
            all_cs_d = gatherResults(all_cs)
            all_cs_d.addCallback(decrypt, pc, zis)
            deferred = all_cs_d
        else:
            zis_deferred = Deferred()
            zis_deferred.callback(zis)
            deferred = zis_deferred

        return deferred