Skip to content

Commit

Permalink
Disable pade and Taylor approximations for the time being.
Browse files Browse the repository at this point in the history
  • Loading branch information
tshead2 committed Sep 14, 2023
1 parent b076739 commit bf8f8f4
Show file tree
Hide file tree
Showing 8 changed files with 1,807 additions and 2,393 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/regression-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install behave coveralls coverage ipython nbformat toyplot
pip install behave coveralls coverage ipython nbformat
pip install .
- name: Run tests
run: |
Expand Down
199 changes: 99 additions & 100 deletions cicada/active.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,62 +1035,62 @@ def negative(self, operand):



def _pade_approx(self, func, operand,*, encoding=None, center=0, degree=12, scale=3):
"""Return the pade approximation of `func` sampled with `operand`.
Note
----
This is a collective operation that *must* be called
by all players that are members of :attr:`communicator`.
Parameters
----------
func: callable object, required
The function to be approximated via the pade method.
operand: :class:`ActiveArrayShare`, required
Secret-shared values where `func` should be evaluated.
center: :class:`float`, optional
The value at which the approximation should be centered. Sample
errors will be larger the further they are from this point.
Returns
-------
result: :class:`ActiveArrayShare`
Secret shared result of evaluating the pade approximant of func(operand) with the given parameters.
"""
from scipy.interpolate import approximate_taylor_polynomial, pade
num_deg = degree%2+degree//2
den_deg = degree//2

self._assert_unary_compatible(operand, "operand")
encoding = self._require_encoding(encoding)

func_taylor = approximate_taylor_polynomial(func, center, degree, scale)
func_pade_num, func_pade_den = pade([x for x in func_taylor][::-1], den_deg, n=num_deg)
enc_func_pade_num = encoding.encode(numpy.array([x for x in func_pade_num]), self.field)
enc_func_pade_den = encoding.encode(numpy.array([x for x in func_pade_den]), self.field)
op_pows_num_list = [self.share(src=1, secret=numpy.array(1), shape=())]
for i in range(num_deg):
op_pows_num_list.append(self.multiply(operand, op_pows_num_list[-1]))
if degree%2:
op_pows_den_list=[thing for thing in op_pows_num_list[:-1]]
else:
op_pows_den_list=[thing for thing in op_pows_num_list]

num_add_shares = AdditiveArrayShare(numpy.array([x.additive.storage for x in op_pows_num_list]))
num_sham_shares = ShamirArrayShare(numpy.array([x.shamir.storage for x in op_pows_num_list]))
den_add_shares = AdditiveArrayShare(numpy.array([x.additive.storage for x in op_pows_den_list]))
den_sham_shares = ShamirArrayShare(numpy.array([x.shamir.storage for x in op_pows_den_list]))
op_pows_num = ActiveArrayShare((num_add_shares, num_sham_shares))
op_pows_den = ActiveArrayShare((den_add_shares, den_sham_shares ))

result_num_prod = self.field_multiply(op_pows_num, enc_func_pade_num)
result_num = self.right_shift(self.sum(result_num_prod), bits=encoding.precision)

result_den_prod = self.field_multiply(op_pows_den, enc_func_pade_den)
result_den = self.right_shift(self.sum(result_den_prod), bits=encoding.precision)
result = self.divide(result_num, result_den)
return result
# def _pade_approx(self, func, operand,*, encoding=None, center=0, degree=12, scale=3):
# """Return the pade approximation of `func` sampled with `operand`.
#
# Note
# ----
# This is a collective operation that *must* be called
# by all players that are members of :attr:`communicator`.
#
# Parameters
# ----------
# func: callable object, required
# The function to be approximated via the pade method.
# operand: :class:`ActiveArrayShare`, required
# Secret-shared values where `func` should be evaluated.
# center: :class:`float`, optional
# The value at which the approximation should be centered. Sample
# errors will be larger the further they are from this point.
#
# Returns
# -------
# result: :class:`ActiveArrayShare`
# Secret shared result of evaluating the pade approximant of func(operand) with the given parameters.
# """
# from scipy.interpolate import approximate_taylor_polynomial, pade
# num_deg = degree%2+degree//2
# den_deg = degree//2
#
# self._assert_unary_compatible(operand, "operand")
# encoding = self._require_encoding(encoding)
#
# func_taylor = approximate_taylor_polynomial(func, center, degree, scale)
# func_pade_num, func_pade_den = pade([x for x in func_taylor][::-1], den_deg, n=num_deg)
# enc_func_pade_num = encoding.encode(numpy.array([x for x in func_pade_num]), self.field)
# enc_func_pade_den = encoding.encode(numpy.array([x for x in func_pade_den]), self.field)
# op_pows_num_list = [self.share(src=1, secret=numpy.array(1), shape=())]
# for i in range(num_deg):
# op_pows_num_list.append(self.multiply(operand, op_pows_num_list[-1]))
# if degree%2:
# op_pows_den_list=[thing for thing in op_pows_num_list[:-1]]
# else:
# op_pows_den_list=[thing for thing in op_pows_num_list]
#
# num_add_shares = AdditiveArrayShare(numpy.array([x.additive.storage for x in op_pows_num_list]))
# num_sham_shares = ShamirArrayShare(numpy.array([x.shamir.storage for x in op_pows_num_list]))
# den_add_shares = AdditiveArrayShare(numpy.array([x.additive.storage for x in op_pows_den_list]))
# den_sham_shares = ShamirArrayShare(numpy.array([x.shamir.storage for x in op_pows_den_list]))
# op_pows_num = ActiveArrayShare((num_add_shares, num_sham_shares))
# op_pows_den = ActiveArrayShare((den_add_shares, den_sham_shares ))
#
# result_num_prod = self.field_multiply(op_pows_num, enc_func_pade_num)
# result_num = self.right_shift(self.sum(result_num_prod), bits=encoding.precision)
#
# result_den_prod = self.field_multiply(op_pows_den, enc_func_pade_den)
# result_den = self.right_shift(self.sum(result_den_prod), bits=encoding.precision)
# result = self.divide(result_num, result_den)
# return result



Expand Down Expand Up @@ -1543,50 +1543,49 @@ def verify(self, operand):



def _taylor_approx(self, func, operand,*, encoding=None, center=0, degree=7, scale=3):
"""Return the taylor approximation of `func` sampled with `operand`.
Note
----
This is a collective operation that *must* be called
by all players that are members of :attr:`communicator`.
Parameters
----------
func: callable object, required
The function to be approximated via the taylor method
operand: :class:`ActiveArrayShare`, required
Secret-shared values where `func` should be evaluated.
center: :class:`float`, optional
The value at which the approximation should be centered. Sample
errors will be larger the further they are from this point.
Returns
-------
result: :class:`ActiveArrayShare`
Secret shared result of evaluating the taylor approximant of func(operand) with the given parameters
"""
from scipy.interpolate import approximate_taylor_polynomial

self._assert_unary_compatible(operand, "operand")
encoding = self._require_encoding(encoding)

taylor_poly = approximate_taylor_polynomial(func, center, degree, scale)

enc_taylor_coef = encoding.encode(numpy.array([x for x in taylor_poly]), self.field)
op_pow_list = [self.share(src=1, secret=numpy.array(1), shape=())]
for i in range(degree):
op_pow_list.append(self.multiply(operand, op_pow_list[-1]))

op_pow_add_shares = AdditiveArrayShare(numpy.array([x.additive.storage for x in op_pow_list]))
op_pow_sham_shares = ShamirArrayShare(numpy.array([x.shamir.storage for x in op_pow_list]))
op_pow_shares = ActiveArrayShare((op_pow_add_shares, op_pow_sham_shares))

result = self.field_multiply(op_pow_shares, enc_taylor_coef)
result = self.sum(result)
result = self.right_shift(result, bits=encoding.precision)
return result

# def _taylor_approx(self, func, operand,*, encoding=None, center=0, degree=7, scale=3):
# """Return the taylor approximation of `func` sampled with `operand`.
#
# Note
# ----
# This is a collective operation that *must* be called
# by all players that are members of :attr:`communicator`.
#
# Parameters
# ----------
# func: callable object, required
# The function to be approximated via the taylor method
# operand: :class:`ActiveArrayShare`, required
# Secret-shared values where `func` should be evaluated.
# center: :class:`float`, optional
# The value at which the approximation should be centered. Sample
# errors will be larger the further they are from this point.
#
# Returns
# -------
# result: :class:`ActiveArrayShare`
# Secret shared result of evaluating the taylor approximant of func(operand) with the given parameters
# """
# from scipy.interpolate import approximate_taylor_polynomial
#
# self._assert_unary_compatible(operand, "operand")
# encoding = self._require_encoding(encoding)
#
# taylor_poly = approximate_taylor_polynomial(func, center, degree, scale)
#
# enc_taylor_coef = encoding.encode(numpy.array([x for x in taylor_poly]), self.field)
# op_pow_list = [self.share(src=1, secret=numpy.array(1), shape=())]
# for i in range(degree):
# op_pow_list.append(self.multiply(operand, op_pow_list[-1]))
#
# op_pow_add_shares = AdditiveArrayShare(numpy.array([x.additive.storage for x in op_pow_list]))
# op_pow_sham_shares = ShamirArrayShare(numpy.array([x.shamir.storage for x in op_pow_list]))
# op_pow_shares = ActiveArrayShare((op_pow_add_shares, op_pow_sham_shares))
#
# result = self.field_multiply(op_pow_shares, enc_taylor_coef)
# result = self.sum(result)
# result = self.right_shift(result, bits=encoding.precision)
# return result


def zigmoid(self, operand, *, encoding=None):
Expand Down

0 comments on commit bf8f8f4

Please sign in to comment.