← All projects

Cryptography explainer · 2023

How to safely ask someone out

What if you could find out whether someone likes you back, without ever revealing that you like them?

A red heart painted on a yellow wall

If you’re reading this, you’re probably planning on asking that special someone out on a date, or want to figure out if a good friend might be more than a good friend. Also, you’re most likely afraid of asking them out, or you would have done so already.

If you could just know whether the other person liked you as well, it would be so much easier! If they like you, well, great 🙂. If they don’t like you, then you don’t need to ask them out in the first place, saving yourself from potential gossip and embarrassment.

But don’t worry: mathematicians, maybe due to their lack of social skills and confidence, have invented a way to ask people out without embarrassing themselves!

There is a method for both of you to determine whether you like each other without needing to reveal your individual preferences. That is, the other person only learns that you like them if they also like you; if they don’t like you, they never find out whether you like them or not. Sorry, that’s a bit of a mouthful.

How can we translate this mathematically? Let pap^a and pbp^b be the intentions of you (Alice) and the other person (Bob), respectively. If a person ii is interested in moving towards a romantic (casual, …) relationship, they set pi=1p^i = 1, or pi=0p^i=0 otherwise. You end up going out together if pa=pb=1p^a=p^b=1, and you don’t otherwise. This function can be expressed either as pa∧pbp^a\land p^b (logical AND) or as pa⋅pbp^a\cdot p^b.

Obviously, this can be easily computed if you both reveal your preferences, but then you might as well just ask the person out normally. Alternatively, you could ask a friend to help you: both of you tell your friend whether you are interested in the other person, and your friend then just tells you the result. But then both of you would need to trust this friend not to tell anyone about your individual preferences or use that information for personal gain. Note that if you write a piece of software to return the answer, and you both just enter your value into the software, it’s the same problem: both of you need to trust the software with your personal preferences.

There are two ways this can be solved, but we’ll only cover the simple method here. The other is more secure, but it is more advanced and requires far too many computations: you’ll lose the person 20% of the way through because they’ll get bored, unless they really love math. If you’re interested, check out multi-party computation for Boolean circuits. You’ll also need to read up on oblivious transfer.

The simpler method can be done with just a few slips of paper, and it is easy enough to understand for someone who didn’t study mathematics. Unfortunately, it still requires your friend, but unlike before, they’ll never learn either of your individual preferences in the process: the friend just gives you some numbers, and you don’t reveal anything to your friend.

The protocol

Step 1 (friend): prepare a Beaver triple. Your friend prepares six numbers a1,a2,b1,b2,c1,c2a_1, a_2, b_1, b_2, c_1, c_2 such that the following holds:

c=a⋅bc1+c2=(a1+a2)⋅(b1+b2)\begin{aligned} c &= a\cdot b \\ c_1 + c_2 &= (a_1+a_2)\cdot (b_1+b_2) \end{aligned}

Suggestion: randomly choose some a,ba, b, and pick random numbers a1,b1,c1a_1, b_1, c_1 such that a2=a−a1a_2=a - a_1, b2=b−b1b_2=b-b_1 and c2=ab−c1c_2=ab-c_1.

Give a1,b1,c1a_1, b_1, c_1 to Alice, and a2,b2,c2a_2, b_2, c_2 to Bob. For future notation: variables subscripted with 1 are for Alice, those with 2 are for Bob.

Step 1 (Alice): split your preference. Your preference pap^a is itself sensitive and cannot be shared with Bob. We therefore split it randomly, in a way that the pieces individually reveal no information about your preference. Simply pick some random number rr, and set p1a=rp^a_1 = r and p2a=pa−rp^a_2 = p^a - r. Note how pa=p1a+p2ap^a = p^a_1 + p^a_2. Keep p1ap^a_1 and share p2ap^a_2 with Bob.

Step 1 (Bob): split your preference. You do the exact same step as Alice, but with different numbers of course. Keep p2bp^b_2 and share p1bp^b_1 with Alice.

Step 2 (Alice). Alice just received p1bp^b_1 from Bob, and a1,b1a_1, b_1 from the friend. She computes

ϵ1=p1a−a1,δ1=p1b−b1\epsilon_1 = p^a_1 - a_1, \quad \delta_1 = p^b_1 - b_1

Step 2 (Bob). Bob just received p2ap^a_2 from Alice, and a2,b2a_2, b_2 from the friend. He computes

ϵ2=p2a−a2,δ2=p2b−b2\epsilon_2 = p^a_2 - a_2, \quad \delta_2 = p^b_2 - b_2

Step 3: reveal ϵ\epsilon and δ\delta. Alice reveals ϵ1,δ1\epsilon_1, \delta_1, and Bob reveals ϵ2,δ2\epsilon_2, \delta_2. These values reveal nothing about the preferences, aa or bb. Both of you compute

ϵ=ϵ1+ϵ2,δ=δ1+δ2\epsilon = \epsilon_1 + \epsilon_2, \quad \delta=\delta_1 + \delta_2

Step 4 (Alice). Compute z1=c1+ϵ⋅b1+δ⋅a1+ϵ⋅δz_1 = c_1 + \epsilon \cdot b_1 + \delta \cdot a_1 + \epsilon \cdot \delta.

Step 4 (Bob). Compute z2=c2+ϵ⋅b2+δ⋅a2z_2 = c_2 + \epsilon \cdot b_2 + \delta \cdot a_2.

Step 5: get the result. Both of you reveal your share z1z_1 or z2z_2; the result of the function is z=z1+z2z = z_1 + z_2. If you did everything correctly, you should have z=1z=1 if and only if you both set your preferences to 1, and z=0z = 0 otherwise.

You can verify that these steps make sense by replacing the variables with their definitions; after simplifying, you should find z=pa⋅pbz=p^a\cdot p^b.

Warning: Please do not use these steps in a production setting, or in any other scenario where confidentiality is business-critical.

A variation for polygamous relationships

Assume you wanted to know how many people in a given group were interested in starting a polygamous relationship. (An alternative would be to check whether everyone was interested, revealing no information if a single person is not, but we skip that scenario here.) This boils down to computing the sum of preferences, assuming the same preference values as before: 1 if interested and 0 if not.

This is, in fact, easier than the previous setting. Say we have four people a,b,c,da, b, c, d with preferences pip^i, i∈{a,b,c,d}i\in\{a,b,c,d\}. Everyone creates random numbers p1i,p2i,p3i,p4ip^i_1, p^i_2, p^i_3, p^i_4 such that pi=p1i+p2i+p3i+p4ip^i=p^i_1+p^i_2+p^i_3+p^i_4, and shares the 1s with aa, the 2s with bb, …, and the 4s with dd. In the end, the jj-th person holds the values pja,pjb,pjc,pjdp^a_j, p^b_j, p^c_j, p^d_j, from which they compute the temporary value zj=pja+pjb+pjc+pjdz_j = p^a_j + p^b_j + p^c_j + p^d_j. Everyone reveals their zjz_j, which lets them compute the result z=z1+z2+z3+z4z = z_1 + z_2 + z_3 + z_4. Substituting the variables shows that this is indeed the sum of all preferences.

Warning: It is very easy for a person to choose a preference that is not 0 or 1, allowing them to skew the result (by choosing negative values, or values greater than 1).

You might ask why we didn’t use this method for the monogamous setting. Whatever the final result, both people would instantly know the other person’s preference (the other person’s preference is the result minus your own). With more people there are more unknowns, so these deductions can no longer be made deterministically.