facebookresearch / CrypTen

A framework for Privacy Preserving Machine Learning
MIT License
1.51k stars 273 forks source link

Using binary secret sharing for comparison? #494

Open Poppy22 opened 8 months ago

Poppy22 commented 8 months ago

Hello,

From what I understood, binary secret sharing should be used for logical operations, such as comparisons, as this would be faster.

Given the code below, which simply compares two numbers x and y using both binary secret sharing, I receive an error:

@mpc.run_multiprocess(world_size=3)
def compare_two_numbers(x_input, y_input):
    x = crypten.cryptensor(x_input, ptype=crypten.mpc.binary)
    y = crypten.cryptensor(y_input, ptype=crypten.mpc.binary)

    r = (x < y)
    crypten.print(f"Source {0} [binary] result: {r.get_plain_text()}")

The error is: AttributeError: 'BinarySharedTensor' object has no attribute 'sub'

How should binary secret sharing be used for comparisons? It seems that only arithmetic secret sharing is working. What am I missing?

kwmaeng91 commented 8 months ago

There is no way to do comparison using arithmetic secret sharing from my understanding. It must be already being converted to binary, if it works with arithmetic secret sharing.

mialuyao commented 3 months ago

There is no way to do comparison using arithmetic secret sharing from my understanding. It must be already being converted to binary, if it works with arithmetic secret sharing.

Hello

In paper crypten crypten, that say:

we can produce [x < y] by computing their difference [z]= [x]-[y] and comparing result to zero: [z <0]. We compute [z <0] by first converting [z] to a binary secret-share , computing its sign bit, and converting the resulting bit to an arithmetic sharing [b].

I observe functions gt, ge, le in MPC/primitives. So, how should we call functions when we need to compare two numbers.

Thank you!

kwmaeng91 commented 3 months ago

If you use a comparison operator for arithmetic secret sharing (e.g., a < b?), it internally subtracts the value and compare it with zero (a - b < 0?), which involves arithmetic operation (a-b), and then comparison. It first does the arithmetic operation and then convert to binary secret sharing, and perform comparison with zero, and convert the result back to arithmetic secret sharing.

So, to answer your question:

  1. You should compare two numbers in an arithmetic secret shared format, not binary. CrypTen internally converts it to binary when needed.
mialuyao commented 3 months ago

If you use a comparison operator for arithmetic secret sharing (e.g., a < b?), it internally subtracts the value and compare it with zero (a - b < 0?), which involves arithmetic operation (a-b), and then comparison. It first does the arithmetic operation and then convert to binary secret sharing, and perform comparison with zero, and convert the result back to arithmetic secret sharing.

So, to answer your question:

  1. You should compare two numbers in an arithmetic secret shared format, not binary. CrypTen internally converts it to binary when needed.

I got it. Thank you!