Exemplo n.º 1
0
 def federated_secure_sum(self, value, bitwidth):
   """Implements `federated_secure_sum` as defined in `api/intrinsics.py`."""
   value = value_impl.to_value(value, None, self._context_stack)
   value = value_utils.ensure_federated_value(value,
                                              placement_literals.CLIENTS,
                                              'value to be summed')
   type_analysis.check_is_structure_of_integers(value.type_signature)
   bitwidth_value = value_impl.to_value(bitwidth, None, self._context_stack)
   value_member_type = value.type_signature.member
   bitwidth_type = bitwidth_value.type_signature
   if not type_analysis.is_valid_bitwidth_type_for_value_type(
       bitwidth_type, value_member_type):
     raise TypeError(
         'Expected `federated_secure_sum` parameter `bitwidth` to match '
         'the structure of `value`, with one integer bitwidth per tensor in '
         '`value`. Found `value` of `{}` and `bitwidth` of `{}`.'.format(
             value_member_type, bitwidth_type))
   if bitwidth_type.is_tensor() and value_member_type.is_struct():
     bitwidth_value = value_impl.to_value(
         structure.map_structure(lambda _: bitwidth, value_member_type), None,
         self._context_stack)
   value = value_impl.ValueImpl.get_comp(value)
   bitwidth_value = value_impl.ValueImpl.get_comp(bitwidth_value)
   comp = building_block_factory.create_federated_secure_sum(
       value, bitwidth_value)
   comp = self._bind_comp_as_reference(comp)
   return value_impl.ValueImpl(comp, self._context_stack)
Exemplo n.º 2
0
 def federated_secure_sum(self, value, bitwidth):
   """Implements `federated_secure_sum` as defined in `api/intrinsics.py`."""
   value = value_impl.to_value(value, None, self._context_stack)
   value = value_utils.ensure_federated_value(value, placements.CLIENTS,
                                              'value to be summed')
   type_utils.check_is_structure_of_integers(value.type_signature)
   bitwidth = value_impl.to_value(bitwidth, None, self._context_stack)
   type_utils.check_equivalent_types(value.type_signature.member,
                                     bitwidth.type_signature)
   value = value_impl.ValueImpl.get_comp(value)
   bitwidth = value_impl.ValueImpl.get_comp(bitwidth)
   comp = building_block_factory.create_federated_secure_sum(value, bitwidth)
   return value_impl.ValueImpl(comp, self._context_stack)
Exemplo n.º 3
0
def create_whimsy_called_federated_secure_sum(value_type=tf.int32):
    r"""Returns a whimsy called secure sum.

                       Call
                      /    \
  federated_secure_sum      [data, data]

  Args:
    value_type: The type of the value.
  """
    federated_type = computation_types.FederatedType(value_type,
                                                     placements.CLIENTS)
    value = building_blocks.Data('data', federated_type)
    bitwidth = building_blocks.Data('data', value_type)
    return building_block_factory.create_federated_secure_sum(value, bitwidth)
Exemplo n.º 4
0
 def federated_secure_sum(self, value, bitwidth):
     """Implements `federated_secure_sum` as defined in `api/intrinsics.py`."""
     value = value_impl.to_value(value, None, self._context_stack)
     value = value_utils.ensure_federated_value(value, placements.CLIENTS,
                                                'value to be summed')
     type_utils.check_is_structure_of_integers(value.type_signature)
     bitwidth = value_impl.to_value(bitwidth, None, self._context_stack)
     value_member_ty = value.type_signature.member
     bitwidth_ty = bitwidth.type_signature
     if not type_utils.are_equivalent_types(value_member_ty, bitwidth_ty):
         raise TypeError(
             'Expected `federated_secure_sum` parameters `value` and `bitwidth` '
             'to have the same structure. Found `value` of `{}` and `bitwidth` of `{}`'
             .format(value_member_ty, bitwidth_ty))
     value = value_impl.ValueImpl.get_comp(value)
     bitwidth = value_impl.ValueImpl.get_comp(bitwidth)
     comp = building_block_factory.create_federated_secure_sum(
         value, bitwidth)
     return value_impl.ValueImpl(comp, self._context_stack)
Exemplo n.º 5
0
def federated_secure_sum(value, max_input):
    """Computes a sum at `tff.SERVER` of a `value` placed on the `tff.CLIENTS`.

  This function computes a sum such that it should not be possible for the
  server to learn any clients individual value. The specific algorithm and
  mechanism used to compute the secure sum may vary depending on the target
  runtime environment the computation is compiled for or executed on. See
  https://research.google/pubs/pub47246/ for more information.

  Not all executors support `tff.federated_secure_sum()`; consult the
  documentation for the specific executor or executor stack you plan on using
  for the specific of how it's handled by that executor.

  The `max_input` argument is the maximum value (inclusive) that may appear in
  `value`. *Lower values may allow for improved communication efficiency.*
  Attempting to return a `value` higher than `max_input` is invalid, and will
  result in a failure at the given client.

  Example:

  ```python
  value = tff.federated_value(1, tff.CLIENTS)
  result = tff.federated_secure_sum(value, 1)

  value = tff.federated_value((1, 2), tff.CLIENTS)
  result = tff.federated_secure_sum(value, (1, 2))
  ```

  Note: To sum non-integer values or to sum integers with fewer constraints and
  weaker privacy properties, consider using `federated_sum`.

  Args:
    value: An integer or nested structure of integers placed at `tff.CLIENTS`,
      in the range `[0, max_input]`.
    max_input: A Python integer or nested structure of integers matching the
      structure of `value`. If integer `max_value` is used with a nested
      `value`, the same integer is used for each tensor in `value`.

  Returns:
    A representation of the sum of the member constituents of `value` placed
    on the `tff.SERVER`.

  Raises:
    TypeError: If the argument is not a federated TFF value placed at
      `tff.CLIENTS`.
  """
    value = value_impl.to_value(value, None)
    value = value_utils.ensure_federated_value(value, placements.CLIENTS,
                                               'value to be summed')
    type_analysis.check_is_structure_of_integers(value.type_signature)
    max_input_value = value_impl.to_value(max_input, None)
    value_member_type = value.type_signature.member
    max_input_type = max_input_value.type_signature
    if not type_analysis.is_single_integer_or_matches_structure(
            max_input_type, value_member_type):
        raise TypeError(
            'Expected `federated_secure_sum` parameter `max_input` to match '
            'the structure of `value`, with one integer max per tensor in '
            '`value`. Found `value` of `{}` and `max_input` of `{}`.'.format(
                value_member_type, max_input_type))
    if max_input_type.is_tensor() and value_member_type.is_struct():
        max_input_value = value_impl.to_value(
            structure.map_structure(lambda _: max_input, value_member_type),
            None)
    comp = building_block_factory.create_federated_secure_sum(
        value.comp, max_input_value.comp)
    comp = _bind_comp_as_reference(comp)
    return value_impl.Value(comp)
Exemplo n.º 6
0
def federated_secure_sum(value, bitwidth):
    """Computes a sum at `tff.SERVER` of a `value` placed on the `tff.CLIENTS`.

  This function computes a sum such that it should not be possible for the
  server to learn any clients individual value. The specific algorithm and
  mechanism used to compute the secure sum may vary depending on the target
  runtime environment the computation is compiled for or executed on. See
  https://research.google/pubs/pub47246/ for more information.

  Not all executors support `tff.federated_secure_sum()`; consult the
  documentation for the specific executor or executor stack you plan on using
  for the specific of how it's handled by that executor.

  The `bitwidth` argument represents the bitwidth of the aggregand, that is the
  bitwidth of the input `value`. The federated secure sum bitwidth (i.e., the
  bitwidth of the *sum* of the input `value`s over all clients) will be a
  function of this bitwidth and the number of participating clients.

  Example:

  ```python
  value = tff.federated_value(1, tff.CLIENTS)
  result = tff.federated_secure_sum(value, 2)

  value = tff.federated_value([1, 1], tff.CLIENTS)
  result = tff.federated_secure_sum(value, [2, 4])

  value = tff.federated_value([1, [1, 1]], tff.CLIENTS)
  result = tff.federated_secure_sum(value, [2, [4, 8]])
  ```

  Note: To sum non-integer values or to sum integers with fewer constraints and
  weaker privacy properties, consider using `federated_sum`.

  Args:
    value: An integer value of a TFF federated type placed at the `tff.CLIENTS`,
      in the range [0, 2^bitwidth - 1].
    bitwidth: An integer or nested structure of integers matching the structure
      of `value`. If integer `bitwidth` is used with a nested `value`, the same
      integer is used for each tensor in `value`.

  Returns:
    A representation of the sum of the member constituents of `value` placed
    on the `tff.SERVER`.

  Raises:
    TypeError: If the argument is not a federated TFF value placed at
      `tff.CLIENTS`.
  """
    value = value_impl.to_value(value, None)
    value = value_utils.ensure_federated_value(value, placements.CLIENTS,
                                               'value to be summed')
    type_analysis.check_is_structure_of_integers(value.type_signature)
    bitwidth_value = value_impl.to_value(bitwidth, None)
    value_member_type = value.type_signature.member
    bitwidth_type = bitwidth_value.type_signature
    if not type_analysis.is_valid_bitwidth_type_for_value_type(
            bitwidth_type, value_member_type):
        raise TypeError(
            'Expected `federated_secure_sum` parameter `bitwidth` to match '
            'the structure of `value`, with one integer bitwidth per tensor in '
            '`value`. Found `value` of `{}` and `bitwidth` of `{}`.'.format(
                value_member_type, bitwidth_type))
    if bitwidth_type.is_tensor() and value_member_type.is_struct():
        bitwidth_value = value_impl.to_value(
            structure.map_structure(lambda _: bitwidth, value_member_type),
            None)
    comp = building_block_factory.create_federated_secure_sum(
        value.comp, bitwidth_value.comp)
    comp = _bind_comp_as_reference(comp)
    return value_impl.Value(comp)