Absolute Value

In this example, player 1 secret-shares a vector, and all players compute the absolute values element-wise while preserving privacy:

[1]:
import logging

import numpy

from cicada.additive import AdditiveProtocolSuite
from cicada.communicator import SocketCommunicator
from cicada.logging import Logger

logging.basicConfig(level=logging.INFO)

def main(communicator):
    log = Logger(logging.getLogger(), communicator)
    protocol = AdditiveProtocolSuite(communicator)

    secret = numpy.array([1, -2, -3.5, 6.7]) if communicator.rank == 1 else None
    log.info(f"Player {communicator.rank} secret: {secret}", src=1)

    secret_share = protocol.share(src=1, secret=secret, shape=(4,))
    absolute_share = protocol.absolute(secret_share)
    absolute = protocol.reveal(absolute_share)

    log.info(f"Player {communicator.rank} abs(secret): {absolute}")

SocketCommunicator.run(world_size=3, fn=main);
INFO:root:Player 1 secret: [ 1.  -2.  -3.5  6.7]
INFO:root:Player 0 abs(secret): [1.         2.         3.5        6.69999695]
INFO:root:Player 1 abs(secret): [1.         2.         3.5        6.69999695]
INFO:root:Player 2 abs(secret): [1.         2.         3.5        6.69999695]

Note that \(6.69999695\) is an approximation to the original value, due to the limited precision of the fixed point encoding.

See also

Floor