host.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import numpy as np
  17. from federatedml.secureprotol.spdz.tensor.fixedpoint_numpy import FixedPointTensor
  18. from federatedml.secureprotol.spdz import SPDZ
  19. from fate_arch.session import Session
  20. s = Session()
  21. # on host side
  22. guest_party_id = 10000
  23. host_party_id = 10001
  24. host_proxy_ip = "192.168.0.1" # Generally, it is your current machine IP
  25. federation_id = "spdz_demo" # choose a common federation id (this should be same in both site)
  26. session_id = "_".join([federation_id, "host", str(host_party_id)])
  27. s.init_computing(session_id)
  28. s.init_federation(federation_id,
  29. runtime_conf={
  30. "local": {"role": "host", "party_id": host_party_id},
  31. "role": {"guest": [guest_party_id], "host": [host_party_id]},
  32. },
  33. service_conf={"host": host_proxy_ip, "port": 9370})
  34. s.as_global()
  35. partys = s.parties.all_parties
  36. # [Party(role=guest, party_id=10000), Party(role=host, party_id=10001)]
  37. # on host side(assuming PartyId is partys[1]):
  38. data = np.array([[3, 2, 1], [6, 5, 4]])
  39. with SPDZ() as spdz:
  40. y = FixedPointTensor.from_source("y", data)
  41. x = FixedPointTensor.from_source("x", partys[0])
  42. z = (x + y).get()
  43. t = (x - y).get()
  44. print(z)
  45. print(t)