Exemple #1
0
 def from_source(cls, tensor_name, source, **kwargs):
     spdz = cls.get_spdz()
     q_field = kwargs['q_field'] if 'q_field' in kwargs else spdz.q_field
     if 'encoder' in kwargs:
         encoder = kwargs['encoder']
     else:
         base = kwargs['base'] if 'base' in kwargs else 10
         frac = kwargs['frac'] if 'frac' in kwargs else 4
         encoder = FixedPointEndec(n=q_field,
                                   field=q_field,
                                   base=base,
                                   precision_fractional=frac)
     if isinstance(source, np.ndarray):
         source = encoder.encode(source)
         _pre = urand_tensor(q_field, source)
         spdz.communicator.remote_share(share=_pre,
                                        tensor_name=tensor_name,
                                        party=spdz.other_parties[0])
         for _party in spdz.other_parties[1:]:
             r = urand_tensor(q_field, source)
             spdz.communicator.remote_share(share=(r - _pre) % q_field,
                                            tensor_name=tensor_name,
                                            party=_party)
             _pre = r
         share = (source - _pre) % q_field
     elif isinstance(source, Party):
         share = spdz.communicator.get_share(tensor_name=tensor_name,
                                             party=source)[0]
     else:
         raise ValueError(f"type={type(source)}")
     return FixedPointTensor(share, q_field, encoder, tensor_name)
Exemple #2
0
    def from_source(cls, tensor_name, source, **kwargs):
        spdz = cls.get_spdz()
        q_field = kwargs['q_field'] if 'q_field' in kwargs else spdz.q_field

        if 'encoder' in kwargs:
            encoder = kwargs['encoder']
        else:
            base = kwargs['base'] if 'base' in kwargs else 10
            frac = kwargs['frac'] if 'frac' in kwargs else 4
            encoder = FixedPointEndec(n=q_field,
                                      field=q_field,
                                      base=base,
                                      precision_fractional=frac)

        if is_table(source):
            _pre = urand_tensor(q_field, source, use_mix=spdz.use_mix_rand)

            share = _pre

            spdz.communicator.remote_share(share=_table_binary_op(
                source, encoder.decode(_pre), operator.sub),
                                           tensor_name=tensor_name,
                                           party=spdz.other_parties[-1])
            return FixedPointTensor(value=share,
                                    q_field=q_field,
                                    endec=encoder,
                                    tensor_name=tensor_name)

        elif isinstance(source, Party):
            share = spdz.communicator.get_share(tensor_name=tensor_name,
                                                party=source)[0]
            is_cipher_source = kwargs[
                'is_cipher_source'] if 'is_cipher_source' in kwargs else True
            if is_cipher_source:
                cipher = kwargs.get("cipher")
                if cipher is None:
                    raise ValueError("Cipher is not provided")

                share = cipher.distribute_decrypt(share)
                share = encoder.encode(share)
            return FixedPointTensor(value=share,
                                    q_field=q_field,
                                    endec=encoder,
                                    tensor_name=tensor_name)
        else:
            raise ValueError(f"type={type(source)}")
Exemple #3
0
    def from_source(cls, tensor_name, source, **kwargs):
        spdz = cls.get_spdz()
        q_field = kwargs['q_field'] if 'q_field' in kwargs else spdz.q_field
        if 'encoder' in kwargs:
            encoder = kwargs['encoder']
        else:
            base = kwargs['base'] if 'base' in kwargs else 10
            frac = kwargs['frac'] if 'frac' in kwargs else 4
            encoder = FixedPointEndec(n=q_field,
                                      field=q_field,
                                      base=base,
                                      precision_fractional=frac)

        if isinstance(source, np.ndarray):
            _pre = urand_tensor(q_field, source)

            share = _pre

            spdz.communicator.remote_share(share=source - encoder.decode(_pre),
                                           tensor_name=tensor_name,
                                           party=spdz.other_parties[-1])

            return FixedPointTensor(value=share,
                                    q_field=q_field,
                                    endec=encoder,
                                    tensor_name=tensor_name)

        elif isinstance(source, Party):
            share = spdz.communicator.get_share(tensor_name=tensor_name,
                                                party=source)[0]

            is_cipher_source = kwargs[
                'is_cipher_source'] if 'is_cipher_source' in kwargs else True
            if is_cipher_source:
                cipher = kwargs['cipher']
                share = cipher.recursive_decrypt(share)
                share = encoder.encode(share)
            return FixedPointTensor(value=share,
                                    q_field=q_field,
                                    endec=encoder,
                                    tensor_name=tensor_name)
        else:
            raise ValueError(f"type={type(source)}")
Exemple #4
0
    def _cross(self_index, other_index):
        LOGGER.debug(f"_cross: a={a}, b={b}")
        _c = dot(a, b)
        encrypted_a = encrypt_tensor(a, public_key)
        communicator.remote_encrypted_tensor(encrypted=encrypted_a,
                                             tag=f"{name}_a_{self_index}")
        r = urand_tensor(q_field, _c)
        _p, (ea, ) = communicator.get_encrypted_tensors(
            tag=f"{name}_a_{other_index}")
        eab = dot(ea, b)
        eab += r
        _c -= r
        communicator.remote_encrypted_cross_tensor(
            encrypted=eab,
            parties=_p,
            tag=f"{name}_cross_a_{other_index}_b_{self_index}")
        crosses = communicator.get_encrypted_cross_tensors(
            tag=f"{name}_cross_a_{self_index}_b_{other_index}")
        for eab in crosses:
            _c += decrypt_tensor(eab, private_key, [object])

        return _c