question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

mpc.convert() from mpc.SecFld() has issues with negative numbers

See original GitHub issue

It appears that the conversion from (signed) SecFld objects to other objects yields incorrect results when the shared secret has a negative value.

from mpyc.runtime import mpc

async def main():
    await mpc.start()
    secint = mpc.SecInt()
    secfld = mpc.SecFld(min_order=2**10, signed=True)

    a = secint(3)
    a = mpc.input(a, 0)
    print(await mpc.output(a))
    b = mpc.convert(a, secfld)
    print(await mpc.output(b))
    c = mpc.convert(b, secint)
    print(await mpc.output(c))

    await mpc.shutdown()

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

Result:

2020-03-03 16:55:50,690 Install package gmpy2 for better performance.
2020-03-03 16:55:50,691 Start MPyC runtime v0.6
-3
-3
1028
2020-03-03 16:55:50,700 Stop MPyC runtime -- elapsed time: 0:00:00.009001

Of course, we’d expect the result to be -3. Help would be appreciated!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
lschoecommented, Jun 9, 2020

OK, I’ve now merged your fix. Thanks again! Implementation of function mpc.convert() is still quite limited so will need some further improvements later on. I’ll also reconsider the use of slots for the Runtime, probably better to drop this indeed.

1reaction
lschoecommented, Mar 3, 2020

Yeah, also a good point. This is not so easy to fix, but certainly possible.

Near the end of _convert() in runtime.py there is this call “self._mod(ttype(x[i]), stype.field.modulus)” (line 490). This results in an unsigned value between 0 and the modulus.

To fix this issue, function _mod() needs to be extended to output a signed modulo value in the range -modulus/2 to modulus/2. That can be done at the expense of an extra comparison.

As a workaround you can add this comparison with modulus/2 in your application code. Do you want to try that?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Akai MPC Forums - manually entering numbers
is there a way to manually type in the number so it goes negative?
Read more >
Problems with negative numbers - Stack Overflow
I Have this "simple" code. union { unsigned int res; char bytes[2]; } ADC; char ADC_num ...
Read more >
Change the way negative numbers are displayed
You can display negative numbers by using the minus sign, parentheses, or by applying a red color (with or without parentheses).
Read more >
Answer true or false. If false, explain why. If the MPC has a ...
If the MPC has a positive value, then the MPS must have a negative value. MPC: The term MPC can be defined as...
Read more >
How to convert negative numbers into positive numb...
Hi all, Apologies in advance if this has been answered before. As the subject suggests; How can I convert negative numbers into positive...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found