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(q_field, base, 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, tensor_name=tensor_name, party=_party) _pre = r share = source - _pre 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, spdz.q_field, encoder, tensor_name)
def from_source(cls, tensor_name, source, **kwargs): spdz = cls.get_spdz() 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 q_field = kwargs['q_field'] if 'q_field' in kwargs else spdz.q_field encoder = fixedpoint_numpy.FixedPointEndec(q_field, base, frac) if isinstance(source, Table): source = encoder.encode(source) _pre = urand_tensor(spdz.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(spdz.q_field, source) spdz.communicator.remote_share(share=_table_binary_op( r, _pre, spdz.q_field, operator.sub), tensor_name=tensor_name, party=_party) _pre = r share = _table_binary_op(source, _pre, spdz.q_field, operator.sub) 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, spdz.q_field, encoder, tensor_name)
def _cross(self_index, other_index): _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
def beaver_triplets(a_tensor, b_tensor, dot, q_field, he_key_pair, communicator: Communicator, name): public_key, private_key = he_key_pair a = rand_tensor(q_field, a_tensor) b = rand_tensor(q_field, b_tensor) c = dot(a, b) # broadcast encrypted a and encrypted b if communicator.party_idx == 0: encrypted_a = encrypt_tensor(a, public_key) encrypted_b = encrypt_tensor(b, public_key) communicator.remote_encrypted_tensor(encrypted=encrypted_a, tag=f"{name}_a") communicator.remote_encrypted_tensor(encrypted=encrypted_b, tag=f"{name}_b") # get encrypted a and b if communicator.party_idx == 1: r = urand_tensor(q_field, c) _p, encrypted_a_list = communicator.get_encrypted_tensors( tag=f"{name}_a") _, encrypted_b_list = communicator.get_encrypted_tensors( tag=f"{name}_b") cross = dot(encrypted_a_list[0], b) + dot(a, encrypted_b_list[0]) cross += r c -= r communicator.remote_encrypted_cross_tensor(encrypted=cross, parties=_p, tag=name) if communicator.party_idx == 0: # get cross terms crosses = communicator.get_encrypted_cross_tensors(tag=name) for cross in crosses: c += decrypt_tensor(cross, private_key, [object]) return a, b, c % q_field