Division

In this notebook we demonstrate the division functionality available within cicada, by computing \(37 / 7 = 5.2857142857\). As usual, the result will be an approximation limited by the precision of the fixed point representation of our operands.

Note that the following is a true private division where both the dividend and the divisor are secret-shared values.

[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)

    # Player 0 will provide the secret dividend.
    a = numpy.array(37) if communicator.rank == 0 else None
    # Player 1 will provide the secret divisor.
    b = numpy.array(7) if communicator.rank == 1 else None

    log.info(f"Player 0 dividend: {a}", src=0)
    log.info(f"Player 1 divisor: {b}", src=1)

    # Compute the quotient
    a_share = protocol.share(src=0, secret=a, shape=())
    b_share = protocol.share(src=1, secret=b, shape=())
    quotient_share = protocol.divide(a_share, b_share)
    quotient = protocol.reveal(quotient_share)
    log.info(f"Player {communicator.rank} quotient: {quotient}")

SocketCommunicator.run(world_size=2, fn=main);
INFO:root:Player 0 dividend: 37
INFO:root:Player 1 divisor: 7
INFO:root:Player 0 quotient: 5.2854766845703125
INFO:root:Player 1 quotient: 5.2854766845703125

As you can see, the result revealed to both players is a close approximation to the expected value.