Less Than Comparison
Here we demonstrate comparing two secret-shared arrays elementwise to see which values from the first are less than those from the second. The resulting secret shared array will be the same shape as the input, containing \(1\) where the first input is less than the second, and \(0\) where it is not. Note that we use the Boolean
encoding when we reveal the output because the default FixedPoint
encoding would produce unexpected results.
In this example, we compare \(2 < 3.5\), with the revealed result indicating that the comparison is, indeed true:
[1]:
import logging
import numpy
from cicada.additive import AdditiveProtocolSuite
from cicada.communicator import SocketCommunicator
from cicada.encoding import Boolean
from cicada.logging import Logger
logging.basicConfig(level=logging.INFO)
def main(communicator):
log = Logger(logging.getLogger(), communicator)
protocol = AdditiveProtocolSuite(communicator)
a = numpy.array(2) if communicator.rank == 1 else None
b = numpy.array(3.5) if communicator.rank == 2 else None
log.info(f"Player {communicator.rank} secret: {a}", src=1)
log.info(f"Player {communicator.rank} secret: {b}", src=2)
a_share = protocol.share(src=1, secret=a, shape=())
b_share = protocol.share(src=2, secret=b, shape=())
less_than_share = protocol.less(a_share, b_share)
less_than = protocol.reveal(less_than_share, encoding=Boolean())
log.info(f"Player {communicator.rank} result: {less_than}")
SocketCommunicator.run(world_size=3, fn=main);
INFO:root:Player 1 secret: 2
INFO:root:Player 2 secret: 3.5
INFO:root:Player 0 result: True
INFO:root:Player 1 result: True
INFO:root:Player 2 result: True
See also