https://github.com/zhuzilin/garbled_circuit
A python implementation of Yao's GC.
https://github.com/zhuzilin/garbled_circuit
Last synced: 3 months ago
JSON representation
A python implementation of Yao's GC.
- Host: GitHub
- URL: https://github.com/zhuzilin/garbled_circuit
- Owner: zhuzilin
- License: mit
- Created: 2021-10-04T13:13:54.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-10-04T13:35:30.000Z (over 3 years ago)
- Last Synced: 2025-01-20T11:24:04.306Z (5 months ago)
- Language: Python
- Size: 99.6 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## garbled_circuit
This is a python implementation of Yao's GC.
### Usage
I choose the [Bristol Format](https://homes.esat.kuleuven.be/~nsmart/MPC/) to represent the circuit, which is a standard format adopted by many tools and libraries. Some examples of the format can be found in the `circuit/basic` folder. All the circuit files are downloaded from [here](https://homes.esat.kuleuven.be/~nsmart/MPC/).
You can use the `parse` function in `garbled_circuit.parser` to get the `Circuit` structure:
```python
from garbled_circuit.parser import parsedef read_circuit_from_file(filename):
with open(filename) as f:
s = f.read()
circuit = parse(s)
return circuitfilename = "circuit/basic/mult64.txt"
circuit = read_circuit_from_file(filename)
```And for the 2 players in the MPC, I call the one who creates and sends the garbled_table and decoding table `SENDER` and the other one who will receive them `RECEIVER`. You could use the following way to do a integer multiplication with Yao's GC (which used the `mult64.txt` file above).
- **Sender**
```python
from garbled_circuit.basic_types import Int, PlaceHolder
from garbled_circuit.gc import GarbledCircuit, Roleinputs = [Int(15), PlaceHolder()]
p1 = GarbledCircuit(
circuit, role=Role.SENDER, addr="tcp://127.0.0.1:5004", ot_addr="tcp://*:5005"
)outputs = p1(inputs)
```- **Receiver**
```python
from garbled_circuit.basic_types import Int, PlaceHolder
from garbled_circuit.gc import GarbledCircuit, Roleinputs = [PlaceHolder(), Int(20)]
p2 = GarbledCircuit(
circuit, role=Role.RECEIVER, addr="tcp://*:5004", ot_addr="tcp://127.0.0.1:5005"
)outputs = p2(inputs)
```And both player will get output `300`.
### Acknowlegement
The implementation mainly follows the protocol in the great book [A Pragmatic Introduction to Secure Multi-Party Computation](https://securecomputation.org/).