data61 / MP-SPDZ

Versatile framework for multi-party computation
Other
944 stars 280 forks source link

Wrong output #1464

Closed Dar9586 closed 3 months ago

Dar9586 commented 4 months ago

I have the following problem with this circuit

@compiler.register_function('Func')
def func():
    sint = compiler.sint
    size_alice = int(compiler.options.size_alice)
    size_bob = int(compiler.options.size_bob)
    merged = sint.Array(size_alice + size_bob + 2)
    comp = sint.Array((size_alice + size_bob + 2) // 2)
    @for_range_opt(size_alice)
    def _(i):
        merged[i] = sint.get_input_from(0)
    @for_range_opt(size_bob)
    def _(i):
        merged[size_alice + i] = sint.get_input_from(1)
    merged[size_alice + size_bob + 1] = sint(2 ** 120 - 1)
    merged[size_alice + size_bob] = sint(2 ** 120)
    merged.sort()
    @for_range_opt(0, size_alice + size_bob, 2)
    def _(i):
        vvv = ((merged[i] == merged[i + 1]) + (merged[i + 1] == merged[i + 2]))
        comp[i // 2] = vvv * merged[i + 1]
    # comp.secure_shuffle()
    print_ln("%s", comp.reveal())

Compiled with:

python /opt/MP-SPDZ/Programs/Source/Func.mpc -G -l -B 128 --size-alice 10 --size-bob 10

And executing using:

/opt/MP-SPDZ/yao-party.x 0 Func -pn 19330 -h localhost
/opt/MP-SPDZ/yao-party.x 1 Func -pn 19330 -h localhost

Alice input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Bob input: [10, 11, 12, 13, 14, 15, 16, 8, 17, 18] Expected output: [0, 0, 0, 8, 0, 10, 0, 0, 0, 0, 0] Program Output: [0, 0, 0, 0, 0, 147573952589676412928, 8, 0, 0, 184467440737095516160, 10]

Also, I would like to execute a secure_shuffle on the comp array but it doesn't seem possible, is there any way to do it (in the snippet I commented it)?

mkskeller commented 4 months ago

Thank you for raising this. You should find that 5c4382c1397 fixes it.

Dar9586 commented 4 months ago

Thank you for the fix, is there a way to call secure_shuffle on the comp variable?

mkskeller commented 4 months ago

Secure shuffling isn't supported with binary circuits, so only without -G and -B.

Dar9586 commented 4 months ago

Is it possibile to generate random values with binary circuits?

mkskeller commented 4 months ago

Yes, but currently only bit-wise using sbits.get_random_bit().