data61 / MP-SPDZ

Versatile framework for multi-party computation
Other
899 stars 278 forks source link

for_range_opt: incorrect number of loops #1500

Open PixelwarStudio opened 5 days ago

PixelwarStudio commented 5 days ago

Hi,

I tried to go through an array in reversed order using for_range_opt, however that led to an overflow error. The problem seems to be the calculation of n_loops in for_range_opt.

Example that leads to overflow in the second loop:

l = 3 
a = sint.Array(l)
a[0] = 0
a[1] = 1
a[2] = 2

@for_range_opt(0, l, 1)
def _(i):
    print_ln("%s", a[i].reveal())

@for_range_opt(l-1, -1, -1)
def _(i):
    print_ln("%s", a[i].reveal())

In the example, the current calculation leeds to nloops=5 in for_range_opt(3-1, -1, -1) instead of the expected nloops=3. I was able to quickfix by replacing the n_loops calculation with

range_ = stop-start
n_loops = ((range_% step) != 0) + range_ // step

Another issue I noticed in the process is that for e.g for_range_opt(0, 0, -3) the number of loops is 1 even though it should loop 0 times.

mkskeller commented 2 days ago

Thank you for raising this. You should find that de77d89ce36ffa72bed7a0a6f60499800f5550ac adds your fix.