emp-sh2pc icon indicating copy to clipboard operation
emp-sh2pc copied to clipboard

wrong array value after assignment in loop

Open sebw42 opened this issue 10 months ago • 1 comments

Hello, I've noticed weird behaviour when using loops in combination with array accesses. Here is an example program:

void fun(){
    int a[] = {0};
    int b[] = {0, 6};

    // runtime guard
    if ((Integer(32, 0) == Integer(32, 0)).reveal<bool>()) {
        a[0] = 6;
    }
    
    // useless secret computation. Error vanishes without this
    Integer _ignored = Integer(32, 0) * Integer(32, 0);

    for (int i = 0; i < a[0]; i++) {
        // should always be 0 because a[0] is 6
        int zero = 6 - a[0];
        int n = (i - b[zero]);
        b[zero] = b[1] + n;
    }

    cout << "b[0]: expected 3, got: " << b[0] << endl; // got: 9
}
int main(int argc, char** argv) {
    int port, party;
    parse_party_and_port(argv, &party, &port);
    NetIO * io = new NetIO(party==ALICE ? nullptr : "127.0.0.1", port);
    setup_semi_honest(io, party);
    fun();
    delete io;
}

After the loop, the value of b[0] should be 3, but it is actually 9. This error seems to require specific circumstances to appear. It vanishes if:

  • a[0] = 6 is outside the if, or the if has a compile-time guard
  • there is no secret computation between the if and the loop
  • zero is a compile-time value
  • we use a[0] in place of b[1] or vice versa (which should make no difference since they are both 6)
  • if we print anything (even just a newline) in the loop
  • if we do a secret computation in the loop

Thank you for looking into this!

sebw42 avatar Feb 18 '25 10:02 sebw42

Thanks! It's kind of interesting because here there is technically no MPC. The integers are public so comparison is also public.

wangxiao1254 avatar Feb 24 '25 16:02 wangxiao1254