mpc-msri / EzPC

MIT License
401 stars 122 forks source link

[EzPC] Strange behavior with shifting operation on secret integer data type #192

Open winnnnnny opened 1 year ago

winnnnnny commented 1 year ago

Hi, when I use a shifting operation on the secret integer data type, it turns out to be an unexpected behavior. Here is an example.

def void main(){
    uint32_bl var0001 = 98u;
    output(CLIENT, var0001);
    var0001 = ~var0001;
    output(CLIENT, var0001);
    var0001 = var0001 >> 27u;
    output(CLIENT, var0001);

    uint32_pl var0002 = 98u;
    output(CLIENT, var0002);
    var0002 = ~var0002;
    output(CLIENT, var0002);
    var0002 = var0002 >> 27u;
    output(CLIENT, var0002);
}

The output is:

Value of var0002:
98
Value of var0002:
4294967197
Value of var0002:
31
Value of var0001:
98
Value of var0001:
4294967197
Value of var0001:
0

For the public integer variable var0002, it works as expected. In the binary form, it can represented as: 00000000 00000000 00000000 01100010 -> 11111111 11111111 11111111 10011101 -> 00000000 00000000 00000000 00011111, which is finally 31. However, for the secret integer variable var0001, it goes wrong at the step of shifting. I guess there is some problem with shifting operations on secret integers. I also tried this example on signed integers and arithmetic sharing, which showed the same result. Otherwise, did I use the shifting operation in the wrong way?