作业要求
作业要求
Homework 7: Report
Lecturer: Dr. Adi Akavia Student(s): Nir Segal, Hallel Weinberg, Michael Rodel
Abstract
In this homework we implemented Yao’s protocol for the Boolean circuit of a specific func-
tion.
We wrote code in Python that uses this protocol compute the following function:
1 a1 x1 + a2 x2 ≥ 4
f⃗a,4 (x) =
0 otherwise
1 Introduction
Motivation. In recent years, there has been increasing interest in privacy-preserving com-
putation and secure two-party computation [LP04]. These techniques aim to enable parties
to perform computations on their private data without revealing sensitive information to
each other or any potential adversaries [Wik].
When 2 parties want to exchange information, they have to use a secure protocol to en-
sure that the information remains confidential and protected from any malicious actors who
may try to intercept it. Yao’s protocol enables two parties to securely exchange information
using a garbled circuit, ensuring confidentiality and privacy.
1. Input and Circuit Representation: Alice and Bob each hold private inputs, rep-
resented as binary strings. The function they want to compute is expressed as a
Boolean circuit. The circuit computes the desired function based on Alice and Bob’s
inputs.
7-1
labels hide the gate’s functionality and inputs. Alice then sends the garbled circuit
to Bob.
3. Input Oblivious Transfer (OT): Oblivious Transfer allows Bob to securely obtain
labels corresponding to his input wires in the garbled circuit. Bob’s input choices (0
or 1) are sent to Alice via OT, ensuring Alice remains oblivious to Bob’s actual input.
Alice performs the OT protocol and sends labels back to Bob.
4. Garbled Circuit Evaluation: With the garbled circuit and labels corresponding
to his input wires, Bob locally evaluates the garbled circuit without revealing his
inputs. He uses the labels to determine encrypted outputs of each gate and performs
decryption operations.
5. Result Retrieval: Bob obtains the encrypted output of the entire circuit and sends
it back to Alice.
6. Decryption and Output Extraction: Alice, holding the private key, decrypts the
output received from Bob. She obtains the final result of the computation, the output
of the jointly computed function.
Through this process, Alice and Bob maintain privacy and confidentiality.
Application. In our application we defined 2 classes: Alice and Bob. We first randomly
generate ⃗a, ⃗x as inputs to the protocol and then communicate between the classes using a
few lines of code:
2. Alice assigns labels of 128 − bits to each wire and gate in the boolean circuit.
4. Alice sends to Bob the garbled circuit and the labels that fit Alice’s inputs.
5. Alice and Bob compute the labels of the Bob’s input wires using OT protocol.
6. Bob compute the output label using Alice’s labels and sends it to Alice.
7. Finally, Alice converts the output label to the real output z and outputs z. Note that
actually Given ⃗a and ⃗x, z is equal to the result of the function f⃗a,4 (x1 , x2 ) for ⃗a, ⃗x
above.
7-2
2 Preliminaries
In this paper we used the boolean circuit we built in homework 1 (in figures 3, 4, 5 and 6).
7-3
3 Protocols
3.1 Secure Computation Technique: Yao’s Protocol
Parties: Alice A and Bob B.
Functionality: f : {0, 1}n × {0, 1}n → {0, 1} × ⊥, (x, y) → (f (x, y), ⊥).
Circuit: A Boolean circuit C : {0, 1}n × {0, 1}n → {0, 1}.
L wires x1 , . . . , xL : x1 , . . . , xn - Alice’s input. xn+1 , ..., x2n - Bob’s input. xL - output wire.
d layers s.t. inputs to gates at layer i ∈ {1, .., d} are from layers < i.
Gates: XOR with constant or of two wires. AND with constant or of two wires.
7-4
3.1.2 Sub-Protocols
1) Use SHA − 256 to encode the input labels A, B. mark the 256 − bits result as X.
2) Add to the output label C a tail of 128 zeros.
3) Return XOR(X, C).
2: DA,B (C):
1) Use SHA − 256 to encode the input labels A, B. mark the 256 − bits result as X.
2) Compute decrypted label = XOR(X, C).
3) If decrypted label has a tail of 128 zeros: return the first 128−bits of decrypted label.
4) Else: return N one.
7-5
Algorithm 4 Sub-protocol | Passive 1-Out-Of-2 Oblivious Transfer from ElGamal
1: OT2 (p, q, g): ▷ use ElGamal subprotocol found in algorithm 3
Receiver:
Receiver has a choice bit- 0 or 1.
1) pk 0 , sk = Gen().
2) pk 1 = OGen(random()).
3) receiver choice = choice.
Sender:
Sender has 2 messages- m0 and m1 and (pk0, pk1).
1) c0 = Encpk0 (m0 ) and c1 = Encpk1 (m1 ).
2) Send (c0 , c1 ) to receiver.
Receiver:
Receiver has receiver choice, (c0 , c1 ) and sk.
1) If receiver choice == 1 do m′ = c1 , else m′ = c0 .
2) dec′m = Decsk (m′ )
3) Output dec′m .
7-6
4 Implementation
The implementation of the algorithm is done using Python programming language. In order
to run the code, you will need to have Numpy, random, and hashlib libraries installed.
Our code generates 2 random inputs ⃗a = (a1 , a2 ) and ⃗x = (x1 , x2 ) and returns the output
z, which corresponds to the result of f⃗a,4 (x1 , x2 ).
The code is here:
https://colab.research.google.com/drive/14mqxprFbJD92OPiR5jR1sFScD_U66UaA?usp=
sharing.
5 Empirical Evaluation
Each row in the table represents an experiment, and we conducted a total of 256 experiments
by considering all possible inputs of a1 , a2 , and x1 , x2 for which we obtained an output z:
Begin of Table
x1 x2 a1 a2 z
0 0 0 0 0
0 0 0 1 0
0 0 0 2 0
0 0 1 0 0
0 0 2 0 0
0 0 0 3 0
0 0 1 1 0
0 0 2 1 0
0 0 1 2 0
0 0 2 2 0
0 0 3 0 0
0 0 1 3 0
0 0 2 3 0
0 0 3 1 0
0 0 3 2 0
0 0 3 3 0
0 1 0 0 0
0 1 0 1 0
0 1 0 2 0
0 1 1 0 0
0 1 2 0 0
0 1 0 3 0
0 1 1 1 0
0 1 2 1 0
0 1 1 2 0
7-7
Continuation of Table 1
x1 x2 a1 a2 z
0 1 2 2 0
0 1 3 0 0
0 1 1 3 0
0 1 2 3 0
0 1 3 1 0
0 1 3 2 0
0 1 3 3 0
0 2 0 0 0
0 2 0 1 0
0 2 0 2 1
0 2 1 0 0
0 2 2 0 0
0 2 0 3 1
0 2 1 1 0
0 2 2 1 0
0 2 1 2 1
0 2 2 2 1
0 2 3 0 0
0 2 1 3 1
0 2 2 3 1
0 2 3 1 0
0 2 3 2 1
0 2 3 3 1
1 0 0 0 0
1 0 0 1 0
1 0 0 2 0
1 0 1 0 0
1 0 2 0 0
1 0 0 3 0
1 0 1 1 0
1 0 2 1 0
1 0 1 2 0
1 0 2 2 0
1 0 3 0 0
1 0 1 3 0
1 0 2 3 0
1 0 3 1 0
1 0 3 2 0
1 0 3 3 0
2 0 0 0 0
2 0 0 1 0
7-8
Continuation of Table 1
x1 x2 a1 a2 z
2 0 0 2 0
2 0 1 0 0
2 0 2 0 1
2 0 0 3 0
2 0 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0 2 1 1
3 0 1 2 0
3 0 2 2 1
3 0 3 0 1
3 0 1 3 0
3 0 2 3 1
3 0 3 1 1
3 0 3 2 1
3 0 3 3 1
1 3 0 0 0
1 3 0 1 0
1 3 0 2 1
1 3 1 0 0
1 3 2 0 0
1 3 0 3 1
1 3 1 1 1
1 3 2 1 1
1 3 1 2 1
1 3 2 2 1
1 3 3 0 0
1 3 1 3 1
1 3 2 3 1
7-11
Continuation of Table 1
x1 x2 a1 a2 z
1 3 3 1 1
1 3 3 2 1
1 3 3 3 1
2 3 0 0 0
2 3 0 1 0
2 3 0 2 1
2 3 1 0 0
2 3 2 0 1
2 3 0 3 1
2 3 1 1 1
2 3 2 1 1
2 3 1 2 1
2 3 2 2 1
2 3 3 0 1
2 3 1 3 1
2 3 2 3 1
2 3 3 1 1
2 3 3 2 1
2 3 3 3 1
3 1 0 0 0
3 1 0 1 0
3 1 0 2 0
3 1 1 0 0
3 1 2 0 1
3 1 0 3 0
3 1 1 1 1
3 1 2 1 1
3 1 1 2 1
3 1 2 2 1
3 1 3 0 1
3 1 1 3 1
3 1 2 3 1
3 1 3 1 1
3 1 3 2 1
3 1 3 3 1
3 2 0 0 0
3 2 0 1 0
3 2 0 2 1
3 2 1 0 0
3 2 2 0 1
3 2 0 3 1
7-12
Continuation of Table 1
x1 x2 a1 a2 z
3 2 1 1 1
3 2 2 1 1
3 2 1 2 1
3 2 2 2 1
3 2 3 0 1
3 2 1 3 1
3 2 2 3 1
3 2 3 1 1
3 2 3 2 1
3 2 3 3 1
3 3 0 0 0
3 3 0 1 0
3 3 0 2 1
3 3 1 0 0
3 3 2 0 1
3 3 0 3 1
3 3 1 1 1
3 3 2 1 1
3 3 1 2 1
3 3 2 2 1
3 3 3 0 1
3 3 1 3 1
3 3 2 3 1
3 3 3 1 1
3 3 3 2 1
3 3 3 3 1
End of Table
Note that for each a1 , a2 , x1 , x2 we got z which corresponds to the result of the function
f⃗a,4 (x1 , x2 ) for these a1 , a2 , x1 , x2 :
7-13
Figure 1: Comparison between privacy-preserving computation and not privacy-preserving
computation
7-14
6 Conclusions
As shown in the results in the previous seciton, We see that the proposed approach yields
correct results for f⃗a,4 (x1 , x2 ). Therefore, the output correctness of the proposed approach
is not compromised by considering privacy.
By using Yao’s protocol, we were able to maintain participants’ (Alice and Bob) privacy
because the private data didn’t need to be disclosed for computations.
References
[GMW91] Oded Goldreich, Silvio Micali, and Avi Wigderson. On the play-off between
computational and statistical zero-knowledge. Journal of Cryptology, 4(2):101–
139, 1991.
[LP04] Yehuda Lindell and Benny Pinkas. A proof of yao’s protocol for secure two-
party computation. Cryptology ePrint Archive, Paper 2004/175, 2004. https:
//eprint.iacr.org/2004/175.
7-15
Appendices
A Schematic Diagrams of The Boolean Circuit From Home-
work 1
7-16
7-17
Figure 4: A black box of bits addition
Figure 5: A black box of multiplication
7-18
Figure 6: The entire Boolean circuit
7-19