lschoe / mpyc

MPyC: Multiparty Computation in Python
MIT License
367 stars 76 forks source link

Issue with pointwise array division of SecFxp #85

Closed sam-leder closed 6 months ago

sam-leder commented 6 months ago

Hi! I'm trying to perform pointwise multiplications and divisions on arrays of secure fixed points. Multiplication by a vector and division by a scalar work well, but (pointwise) division by a vector gives an error. See the following example:

from mpyc.runtime import mpc
import numpy as np

secfxp = mpc.SecFxp()

async def main():
    async with mpc:

        a = secfxp.array(1 / np.array([[1, 2], [3, 4], [5, 6]]))
        b = secfxp.array(1 / np.array([7, 8]))
        c = secfxp(1 / 9)

        print(f"Scalar multiplication:\n{await mpc.output(a * c)}")
        print(f"Vector multiplication:\n{await mpc.output(a * b)}")
        print(f"Scalar division:\n{await mpc.output(a / c)}")
        print(f"Vector division:\n{await mpc.output(a / b)}")

if __name__ == "__main__":
    mpc.run(main())

The first three calculations work fine, but dividing pointwise by a vector gives the error AttributeError: type object 'ArraySecFxp32:16' has no attribute 'bit_length'. Did you mean: 'frac_length'? Is this easily fixable, or is there a good workaround? Thanks in advance!

lschoe commented 6 months ago

Hi! Yes, that problem has been fixed since version 0.9.5, see #76. So, you can upgrade to the latest version of MPyC from GitHub. (The computation of 1/b for a secfxp array b is partly vectorized currently.)

sam-leder commented 6 months ago

Ah, I was unaware of that, but it works perfectly now! Thank you for the quick reply