secure_add_guest.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright 2019 The FATE Authors. All Rights Reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. import numpy as np
  19. from fate_arch.session import computing_session as session
  20. from federatedml.model_base import ModelBase, ComponentOutput
  21. from federatedml.param.secure_add_example_param import SecureAddExampleParam
  22. from federatedml.transfer_variable.transfer_class.secure_add_example_transfer_variable import \
  23. SecureAddExampleTransferVariable
  24. from federatedml.util import LOGGER
  25. class SecureAddGuest(ModelBase):
  26. def __init__(self):
  27. super(SecureAddGuest, self).__init__()
  28. self.x = None
  29. self.x1 = None
  30. self.x2 = None
  31. self.y1 = None
  32. self.x1_plus_y1 = None
  33. self.data_num = None
  34. self.partition = None
  35. self.seed = None
  36. self.transfer_inst = SecureAddExampleTransferVariable()
  37. self.model_param = SecureAddExampleParam()
  38. self.data_output = None
  39. self.model_output = None
  40. def _init_runtime_parameters(self, cpn_input):
  41. self.model_param.update(cpn_input.parameters)
  42. self._init_model()
  43. def _init_model(self):
  44. self.data_num = self.model_param.data_num
  45. self.partition = self.model_param.partition
  46. self.seed = self.model_param.seed
  47. def _init_data(self):
  48. kvs = [(i, 1) for i in range(self.data_num)]
  49. self.x = session.parallelize(kvs, include_key=True, partition=self.partition)
  50. def share(self, x):
  51. first = np.random.uniform(x, -x)
  52. return first, x - first
  53. def secure(self):
  54. x_shares = self.x.mapValues(self.share)
  55. self.x1 = x_shares.mapValues(lambda shares: shares[0])
  56. self.x2 = x_shares.mapValues(lambda shares: shares[1])
  57. def add(self):
  58. self.x1_plus_y1 = self.x1.join(self.y1, lambda x, y: x + y)
  59. guest_sum = self.x1_plus_y1.reduce(lambda x, y: x + y)
  60. return guest_sum
  61. def reconstruct(self, guest_sum, host_sum):
  62. print("host sum is %.4f" % host_sum)
  63. print("guest sum is %.4f" % guest_sum)
  64. secure_sum = host_sum + guest_sum
  65. print("Secure Add Result is %.4f" % secure_sum)
  66. return secure_sum
  67. def sync_share_to_host(self):
  68. self.transfer_inst.guest_share.remote(self.x2,
  69. role="host",
  70. idx=0)
  71. def recv_share_from_host(self):
  72. self.y1 = self.transfer_inst.host_share.get(idx=0)
  73. def recv_host_sum_from_host(self):
  74. host_sum = self.transfer_inst.host_sum.get(idx=0)
  75. return host_sum
  76. def run(self, cpn_input):
  77. LOGGER.info("begin to init parameters of secure add example guest")
  78. self._init_runtime_parameters(cpn_input)
  79. LOGGER.info("begin to make guest data")
  80. self._init_data()
  81. LOGGER.info("split data into two random parts")
  82. self.secure()
  83. LOGGER.info("share one random part data to host")
  84. self.sync_share_to_host()
  85. LOGGER.info("get share of one random part data from host")
  86. self.recv_share_from_host()
  87. LOGGER.info("begin to get sum of guest and host")
  88. guest_sum = self.add()
  89. LOGGER.info("receive host sum from guest")
  90. host_sum = self.recv_host_sum_from_host()
  91. secure_sum = self.reconstruct(guest_sum, host_sum)
  92. assert (np.abs(secure_sum - self.data_num * 2) < 1e-6)
  93. LOGGER.info("success to calculate secure_sum, it is {}".format(secure_sum))
  94. return ComponentOutput(self.save_data(), self.export_model(), self.save_cache())