Skip to content

Commit

Permalink
add exception throwing for zero division and mask non-zero confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
kgoss1729 committed Jan 25, 2024
1 parent f15e819 commit 9f54f80
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
17 changes: 14 additions & 3 deletions cicada/additive.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from cicada.arithmetic import Field
from cicada.communicator.interface import Communicator
from cicada.encoding import FixedPoint, Identity
from cicada.encoding import FixedPoint, Identity, Boolean
from cicada.przs import PRZSProtocol


Expand Down Expand Up @@ -331,8 +331,17 @@ def divide(self, lhs, rhs, *, encoding=None, rmask=None, mask1=None, rem1=None,

# Private-private division.
if isinstance(lhs, AdditiveArrayShare) and isinstance(rhs, AdditiveArrayShare):
if rmask is None:
_, rmask = self.random_bitwise_secret(bits=encoding.precision, shape=rhs.storage.shape)
zshare = self.share(src=0, secret=numpy.zeros_like(rhs.storage), shape=rhs.storage.shape)
if self.reveal(self.equal(rhs, zshare), encoding=Boolean()):
raise ZeroDivisionError()
oops = True
while oops:
if rmask is None:
_, rmask = self.random_bitwise_secret(bits=encoding.precision, shape=rhs.storage.shape)
if self.reveal(self.equal(rmask, zshare), encoding=Boolean()):
rmask = None
else:
oops = False
rhsmasked = self.field_multiply(rmask, rhs)
if mask1 != None and rem1 != None:
rhsmasked = self.right_shift(rhsmasked, bits=encoding.precision, trunc_mask=mask1, rem_mask=rem1)
Expand All @@ -352,6 +361,8 @@ def divide(self, lhs, rhs, *, encoding=None, rmask=None, mask1=None, rem1=None,

# Private-public division.
if isinstance(lhs, AdditiveArrayShare) and isinstance(rhs, numpy.ndarray):
if rhs == numpy.zeros_like(rhs):
raise ZeroDivisionError()
divisor = encoding.encode(numpy.array(1 / rhs), self.field)
result = self.field_multiply(lhs, divisor)
result = self.right_shift(result, bits=encoding.precision)
Expand Down
17 changes: 14 additions & 3 deletions cicada/shamir.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from cicada.arithmetic import Field
from cicada.communicator.interface import Communicator
from cicada.encoding import FixedPoint, Identity
from cicada.encoding import FixedPoint, Identity, Boolean

class ShamirArrayShare(object):
"""Stores the local share of a secret shared array for :class:`ShamirProtocolSuite`.
Expand Down Expand Up @@ -797,8 +797,17 @@ def divide(self, lhs, rhs, *, encoding=None, rmask=None, mask1=None, rem1=None,

# Private-private division.
if isinstance(lhs, ShamirArrayShare) and isinstance(rhs, ShamirArrayShare):
if rmask is None:
_, rmask = self.random_bitwise_secret(bits=encoding.precision, shape=rhs.storage.shape)
zshare = self.share(src=0, secret=numpy.zeros_like(rhs.storage), shape=rhs.storage.shape)
if self.reveal(self.equal(rhs, zshare), encoding=Boolean()):
raise ZeroDivisionError()
oops = True
while oops:
if rmask is None:
_, rmask = self.random_bitwise_secret(bits=encoding.precision, shape=rhs.storage.shape)
if self.reveal(self.equal(rmask, zshare), encoding=Boolean()):
rmask = None
else:
oops = False
rhsmasked = self.field_multiply(rmask, rhs)
if mask1 != None and rem1 != None:
rhsmasked = self.right_shift(rhsmasked, bits=encoding.precision, trunc_mask=mask1, rem_mask=rem1)
Expand All @@ -818,6 +827,8 @@ def divide(self, lhs, rhs, *, encoding=None, rmask=None, mask1=None, rem1=None,

# Private-public division.
if isinstance(lhs, ShamirArrayShare) and isinstance(rhs, numpy.ndarray):
if rhs == numpy.zeros_like(rhs):
raise ZeroDivisionError()
divisor = encoding.encode(numpy.array(1 / rhs), self.field)
result = self.field_multiply(lhs, divisor)
result = self.right_shift(result, bits=encoding.precision)
Expand Down

0 comments on commit 9f54f80

Please sign in to comment.