Commit fbc0ec4b authored by Anders Jensen Løvig's avatar Anders Jensen Løvig
Browse files

Update README.md

parent c7af0db5
# Cryptographic Computing - Handin 4
# Cryptographic Computing - Handin 5
A Go implementation of a passive secure two-party protocol for blood type compatibility based on Yao's garbled circuit protocol.
## Implementation
The protocol is implemented in `cmd/handin4/main.go` with tests in `cmd/handin4/main_test.go`.
In addition we have created som supporting packages which provides an Elgamal implementation
and OGen for Elgamal. These can be found in `internal/crypto`.
The protocol is implemented in `cmd/handin5/main.go` with tests in `cmd/handin5/main_test.go`.
For the implementation we have implemented support packages which can be reused:
- `internal/crypto/elgamal` implements textbook ElGamal encryption.
- `internal/crypto/oblivious` implements a 1 out of n oblivious transfer protocol.
- `internal/crypto/garbled` implements Yao's garbled circuits.
## Requirements
......@@ -17,7 +19,11 @@ The protocol is tested using [Go 1.17](https://golang.org/dl/), but will likely
You can run the tests with go:
```
go test ./cmd/handin4
go test ./cmd/handin5
```
This executes the protocol tests. This includes a test that tries the protocol on all combinations of recipient and donor blood types.
This executes the protocol tests. This includes a test that tries the protocol on all combinations of recipient and donor blood types.
\ No newline at end of file
To run all tests in the repository (could take some time):
```
go test ./...
```
\ No newline at end of file
......@@ -10,8 +10,6 @@ import (
)
////////////////////////////////////////////////////////////////
const NumInputs = 6 // How many inputs each player provide
const NumWires = 11
const K = 128
////////////////////////////////////////////////////////////////
......@@ -130,7 +128,6 @@ func (bob *Party) RunBob(y int) (err error) {
// 5b. Alice output
bob.send <- bob.circuit.D.GetData()
return
}
......@@ -148,12 +145,12 @@ func AndGate(a, b bool) bool {
// type y.
// outputMode: 0: Alice learns, 1: Bob learns else both learns
func RunProtocol(x, y int, outputMode int) (z bool, err error) {
builder := garbled.NewCircuitBuilder(6, 128)
or1 := builder.AddGate(0, 3, OrGateWithNot)
or2 := builder.AddGate(1, 4, OrGateWithNot)
or3 := builder.AddGate(2, 5, OrGateWithNot)
and1 := builder.AddGate(or1, or2, AndGate)
_ = builder.AddGate(or3, and1, AndGate)
builder := garbled.NewCircuitBuilder(6, K)
or1 := builder.AddGate(OrGateWithNot, 0, 3)
or2 := builder.AddGate(OrGateWithNot, 1, 4)
or3 := builder.AddGate(OrGateWithNot, 2, 5)
and1 := builder.AddGate(AndGate, or1, or2)
_ = builder.AddGate(AndGate, or3, and1)
p, err := NewProtocol(builder)
if err != nil {
......
package garbled
type gateFun func(a, b bool) bool
type gate struct {
// left is the left input wire
left int
......@@ -8,7 +10,7 @@ type gate struct {
// out is the output wire
out int
// fun is the gate evaluation function
fun func(a, b bool) bool
fun gateFun
}
type CircuitBuilder struct {
......@@ -31,13 +33,13 @@ func NewCircuitBuilder(numInputs, k int) *CircuitBuilder {
// Adds a gate with left and right input wire and evaluted by fun.
// Returns the output wire.
func (b *CircuitBuilder) AddGate(left, right int, fun func(a, b bool) bool) (out int) {
func (b *CircuitBuilder) AddGate(fun gateFun, left, right int) (out int) {
out = b.numInputs + len(b.gates)
b.gates = append(b.gates, &gate{
fun: fun,
left: left,
right: right,
out: out,
fun: fun,
})
return
}
......
......@@ -43,6 +43,7 @@ func NewCircuit(numInputs, numWires, k int, gates []*gate) (c *Circuit, err erro
return
}
// Garble initializes (F, e, d)
func (c *Circuit) Garble() (err error) {
// 1. Create two random strings for each wire.
kTable, err := NewTable(c.numWires, 2, c.k)
......@@ -59,13 +60,6 @@ func (c *Circuit) Garble() (err error) {
c.F.setRow(i, c.garbleGate(kTable, gate))
}
// c.F.setRow(0, c.garbleGate(kTable, 0, 3, 6, ORGateWithNot))
// c.F.setRow(1, c.garbleGate(kTable, 1, 4, 7, ORGateWithNot))
// c.F.setRow(2, c.garbleGate(kTable, 2, 5, 8, ORGateWithNot))
// c.F.setRow(3, c.garbleGate(kTable, 6, 7, 9, ANDGate))
// c.F.setRow(4, c.garbleGate(kTable, 8, 9, 10, ANDGate))
// Create e
c.E = make([]*Table, 2)
c.E[0], err = NewTable(c.numInputs/2, 2, c.k)
......@@ -89,8 +83,7 @@ func (c *Circuit) Garble() (err error) {
return
}
// Encode x using the e table. If ey == true, we use the second
// half of e, otherwise first half
// Encode x using the e table.
func Encode(e *Table, x []bool) (X []byte) {
X = make([]byte, 0)
for i, b := range x {
......@@ -106,6 +99,7 @@ func Encode(e *Table, x []bool) (X []byte) {
return
}
// Decode Z using the d table.
func Decode(d *Table, Z []byte) (bool, error) {
if bytes.Equal(d.getValue(0, 0), Z) {
return false, nil
......@@ -116,7 +110,7 @@ func Decode(d *Table, Z []byte) (bool, error) {
}
}
//Creates a row (C_0^i, C_1^i, C_2^i ,C_3^i) for the garbled table, where
//Creates a row (C_0^i, C_1^i, C_2^i ,C_3^i) for the garbled table
func (c *Circuit) garbleGate(K *Table, gate *gate) []byte {
cRow := make([]byte, 4*2*K.valueLen) // because 4 values of double length
perm := util.Perm(4) // Random permutation
......@@ -181,28 +175,5 @@ func (c *Circuit) Evaluate(x []byte) ([]byte, error) {
}
}
// for i := 0; i < c.F.rows; i++ {
// // For each C
// success := false
// for j := 0; j < 4; j++ {
// left := c.G(K.getValue(0, Li[i]), K.getValue(0, Ri[i]), Oi[i])
// right := c.F.getValue(i, j)
// dst := util.XOR(left, right)
// if bytes.Equal(zeroes, dst[K.valueLen:]) {
// K.setValue(0, i+c.numInputs, dst[:K.valueLen])
// success = true
// break
// }
// }
// if !success {
// return nil, fmt.Errorf("failed to evaluate circuit layer %d", i)
// }
// }
return K.getValue(0, c.numWires-1), nil
}
////////////////////////////////////////////////////////////////
// GATES
////////////////////////////////////////////////////////////////
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment