Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array value does not change after update #1539

Open
sebw42 opened this issue Nov 22, 2024 · 3 comments
Open

Array value does not change after update #1539

sebw42 opened this issue Nov 22, 2024 · 3 comments

Comments

@sebw42
Copy link

sebw42 commented Nov 22, 2024

Hello,
I noticed that when using arrays, the value of an element is sometimes not updated right away.
Here is an example:

arr = sint.Array(1)
arr.assign_all(42)

# change arr[0] to 0
new_value = sint(0) * sint(0)
arr.assign(new_value, cint(0))

# use arr[0] in a calculation. Should result in 0
times_one = arr[0] * sint(1)

# prints `0 ; 42`
print_ln('%s ; %s', arr[0].reveal(), times_one.reveal())

Assigning new_value to arr[0] only takes effect after calculating the value of times_one.
During the calculation, arr[0] seems to still be 42.

After a bit of trial and error, I found out that the bug disappears in the following cases:

  • if no multiplication is involved when calculating new_value or times_one
  • if not both operands of the two multiplications are secret
  • if we use a python integer instead of a cint in arr.assign

Thanks for looking into this!

@mkskeller
Copy link
Member

This is because the compiler cannot recognise that the cint(0) is the same as 0. You can either introduce a break_point() after the assign call or surround the commands with memory protection:

program.protect_memory(True)
...
program.protect_memory(False)

@sebw42
Copy link
Author

sebw42 commented Nov 25, 2024

Thank you for the quick response!
The same thing can also happen if the index is a python integer:

arr = sint.Array(1)
arr.assign(sint(1) * sint(1), 0)
arr.assign(sint(0), 0)
_ignored = sint(1) * sint(1)
print_ln('%s', arr[0].reveal()) # prints 1

Again, the wrong output does not occur if there is no secret multiplication before/after the assignment of sint(0) to arr[0].
Introducing a break_point() after one of the assigns also fixes it.
protect_memory or compiling with -M does not solve the issue here however.

Does that mean I have to add a break_point() after every assign to make sure the value is updated in time?

Thanks!

@mkskeller
Copy link
Member

Thank you for raising this. This is indeed a bug, and 8a4b029 should fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants