Commit a989090f authored by Thomas Hoffmann's avatar Thomas Hoffmann
Browse files

Small changes

parent 552f26be
...@@ -70,49 +70,65 @@ func RunProtocol(x, y int, outputMode int) (bool, error) { ...@@ -70,49 +70,65 @@ func RunProtocol(x, y int, outputMode int) (bool, error) {
inCh, circCh, outCh := make(chan [][]byte), make(chan garbled.Table), make(chan []byte) inCh, circCh, outCh := make(chan [][]byte), make(chan garbled.Table), make(chan []byte)
p1, p2 := oblivious.NewPartyPair() p1, p2 := oblivious.NewPartyPair()
Alice, Bob := NewParty(x, inCh, circCh, outCh, p1), NewParty(y, inCh, circCh, outCh, p2) Alice, Bob := NewParty(x, inCh, circCh, outCh, p1), NewParty(y, inCh, circCh, outCh, p2)
fmt.Printf("Running protocol with [%d,%d]\n", x,y)
go func(Bob *Party){ go func(Bob *Party){
// 1. Garble // 1. Garble
Bob.GenerateCircuit() Bob.GenerateCircuit()
Bob.CircuitChan <- *Bob.Circuit.F Bob.CircuitChan <- *Bob.Circuit.F
fmt.Printf("Bob has generated the circuit and sent F to alice\n")
// 2. Encode Bobs input // 2. Encode Bobs input
Bob.EncInput = garbled.Enc(Bob.Circuit.E[:3], Bob.x) Bob.EncInput = garbled.Enc(Bob.Circuit.E[:3], Bob.x)
Bob.InChan <- Bob.EncInput Bob.InChan <- Bob.EncInput
fmt.Printf("Bob has garbled his input and sent it to alice\n")
// 3. Encode Alices input // 3. Encode Alices input
Bob.OTSend() Bob.OTSend()
fmt.Printf("Bob has finished garbling alices input\n")
// 5. Output // 5. Output
if outputMode != 1 { // Alice learns if outputMode != 1 { // Alice learns
// Bob sends d // Bob sends d
Bob.InChan <- Bob.Circuit.D Bob.InChan <- Bob.Circuit.D
fmt.Printf("Bob sends D so Alice can learn\n")
} }
if outputMode != 0 { // Bob learns if outputMode != 0 { // Bob learns
// Alice sends Z // Alice sends Z
Bob.result = garbled.Decode(Bob.Circuit.D, <- Bob.outChan) Bob.result = garbled.Decode(Bob.Circuit.D, <- Bob.outChan)
fmt.Printf("Bob has received Z so he can learn\n")
} }
}(Bob) }(Bob)
go func (Alice *Party) { //go func (Alice *Party) {
// 1. Garble // 1. Garble
F := <-Alice.CircuitChan F := <-Alice.CircuitChan
Alice.Circuit = &garbled.GCircuit{F: &F} Alice.Circuit = &garbled.GCircuit{F: &F}
fmt.Printf("Alice has received F\n")
// 2. Encode Bobs input // 2. Encode Bobs input
Alice.EncInput = <- Alice.InChan Alice.EncInput = <- Alice.InChan
fmt.Printf("Alice has received Bobs input\n")
// 3. Encode Alices input // 3. Encode Alices input
X, _ := Alice.OTReceive() X, _ := Alice.OTReceive()
Alice.EncInput = append(Alice.EncInput, X...) Alice.EncInput = append(Alice.EncInput, X...)
fmt.Printf("Alice is finished garbling her input\n")
// 4. Evaluation // 4. Evaluation
Z := garbled.Eval(Alice.Circuit.F,Alice.EncInput) Z, eval_err := garbled.Eval(Alice.Circuit.F,Alice.EncInput)
if eval_err != nil {
return false, eval_err
}
fmt.Printf("Alice has evaluated the circuit\n")
// 5. Output // 5. Output
if outputMode != 1 { // Alice learns if outputMode != 1 { // Alice learns
// Bob sends d // Bob sends d
Alice.result = garbled.Decode(<- Alice.InChan, Z) Alice.result = garbled.Decode(<- Alice.InChan, Z)
fmt.Printf("Alice has learned the output\n")
} }
if outputMode != 0 { // Bob learns if outputMode != 0 { // Bob learns
// Alice sends Z // Alice sends Z
Alice.outChan <- Z Alice.outChan <- Z
fmt.Printf("Alice has sent Z to bob\n")
} }
}(Alice) //}(Alice)
if outputMode != 1 { if outputMode != 1 {
return Alice.result, nil return Alice.result, nil
......
...@@ -38,3 +38,28 @@ func TestProtocol(t *testing.T) { ...@@ -38,3 +38,28 @@ func TestProtocol(t *testing.T) {
} }
} }
} }
func TestProtocolInSeries(t *testing.T) {
// Runs the protocol for all combinations of recipient and donor blood types.
n := len(blood.Table)
for x := 0; x < n; x++ {
for y := 0; y < n; y++ {
z, err := RunProtocol(x, y, 0)
if err != nil {
t.Errorf("Protocol error: %s", err)
} else if z != blood.Table[x][y] {
t.Fatalf("Failed blood compatibility test for index [%d,%d]. Expected %t, got %t", x, y, !z, z)
}
}
}
}
func TestProtocolFirst(t *testing.T) {
z, err := RunProtocol(0, 0, 0)
if err != nil {
t.Errorf("Protocol error: %s", err)
} else if z != blood.Table[0][0] {
t.Fatalf("Failed blood compatibility test for index [0,0]. Expected %t, got %t", !z, z)
}
}
package garbled package garbled
import ( import (
"bytes"
"crycomp/internal/util" "crycomp/internal/util"
"crypto/rand"
"crypto" "crypto"
"bytes" "crypto/rand"
m "math/rand"
_ "crypto/sha256" _ "crypto/sha256"
"fmt"
m "math/rand"
) )
type Table struct { type Table struct {
...@@ -48,7 +49,7 @@ func (t *Table) GetRow(i int) [][]byte { ...@@ -48,7 +49,7 @@ func (t *Table) GetRow(i int) [][]byte {
return values return values
} }
func (t *Table) GetKFirstValues(numInputWire int) [][][]byte { func (t *Table) getKFirstValues(numInputWire int) [][][]byte {
values := make([][][]byte, 0) values := make([][][]byte, 0)
for i := 0; i < numInputWire; i++ { for i := 0; i < numInputWire; i++ {
values = append(values,t.GetRow(i)) values = append(values,t.GetRow(i))
...@@ -56,7 +57,7 @@ func (t *Table) GetKFirstValues(numInputWire int) [][][]byte { ...@@ -56,7 +57,7 @@ func (t *Table) GetKFirstValues(numInputWire int) [][][]byte {
return values return values
} }
func (t *Table) RandomizeTable() { func (t *Table) randomizeTable() {
rand.Read(t.data) rand.Read(t.data)
} }
...@@ -66,29 +67,17 @@ type GCircuit struct { ...@@ -66,29 +67,17 @@ type GCircuit struct {
D [][]byte D [][]byte
} }
type Gate struct {
Li, Ri int
i int
truthfunc func(a,b int) int
}
func newGate(Li, Ri, i int, f func(a,b int) int) (g *Gate) {
g.Li = Li
g.Ri = Ri
g.i = i
g.truthfunc = f
return
}
func NewGarbBloodCircuit() (circuit *GCircuit) { func NewGarbBloodCircuit() (circuit *GCircuit) {
numInput := 6 numInput := 6
numWires := 11 numWires := 11
k_bits := 128
// (1) // (1)
K := NewTable(numWires,2,128) K := NewTable(numWires,2,k_bits)
K.RandomizeTable() K.randomizeTable()
// (2) // (2)
C := NewTable(numWires-numInput,4,256) C := NewTable(numWires-numInput,4,k_bits*2)
for i := 0; i < 3; i++ { //1st layer (consisting of not's and or's ) for i := 0; i < 3; i++ { //1st layer (consisting of not's and or's )
//INput wires order: xs, xa, xb, ys, ya, yb (hopefully) //INput wires order: xs, xa, xb, ys, ya, yb (hopefully)
C.SetRow(GBC(K, i,i+3, i+numInput, ORGateWithNot, G),i) C.SetRow(GBC(K, i,i+3, i+numInput, ORGateWithNot, G),i)
...@@ -99,7 +88,7 @@ func NewGarbBloodCircuit() (circuit *GCircuit) { ...@@ -99,7 +88,7 @@ func NewGarbBloodCircuit() (circuit *GCircuit) {
return &GCircuit{ return &GCircuit{
F : C, F : C,
D : K.GetRow(numWires), D : K.GetRow(numWires),
E : K.GetKFirstValues(numInput), E : K.getKFirstValues(numInput),
} }
} }
//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, where
...@@ -132,18 +121,6 @@ func G (A,B []byte, i int) []byte { ...@@ -132,18 +121,6 @@ func G (A,B []byte, i int) []byte {
return hash.Sum(nil) return hash.Sum(nil)
} }
func ORGateWithNot(a,b int) int {
if ((1-a)+b) == 0 {
return 0
} else {
return 1
}
}
func ANDGate(a,b int) int {
return a*b
}
func Enc(e [][][]byte, x []bool) (X [][]byte){ func Enc(e [][][]byte, x []bool) (X [][]byte){
X = make([][]byte, 0) X = make([][]byte, 0)
for i, val := range x { for i, val := range x {
...@@ -156,19 +133,20 @@ func Enc(e [][][]byte, x []bool) (X [][]byte){ ...@@ -156,19 +133,20 @@ func Enc(e [][][]byte, x []bool) (X [][]byte){
return return
} }
func Eval(C *Table, X [][]byte) []byte{ func Eval(C *Table, X [][]byte) ([]byte, error){
var K = X var K = X
var res []byte var res []byte
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
for j:= 0; j < 4; j++ { for j:= 0; j < 4; j++ {
res = util.XOR(G(K[i],K[i+3],i), C.GetValue(i,j)) res = util.XOR(G(K[i],K[i+3],i), C.GetValue(i,j))
if !bytes.Equal(res[16:], make([]byte,16)) { if bytes.Equal(res[16:], make([]byte,16)) {
K = append(K, res[:16]) K = append(K, res[:16])
break break
} }
} }
return nil, fmt.Errorf("Aborted while evaluating the garbled circuit, no match was found")
} }
return K[len(K)-1] return K[len(K)-1], nil
} }
func Decode(d [][]byte, Z []byte) bool { func Decode(d [][]byte, Z []byte) bool {
...@@ -179,4 +157,71 @@ func Decode(d [][]byte, Z []byte) bool { ...@@ -179,4 +157,71 @@ func Decode(d [][]byte, Z []byte) bool {
} }
//TODO: Error handling //TODO: Error handling
return false return false
} }
\ No newline at end of file
////////////////////////////////////////////////////////////////
// Circuit
////////////////////////////////////////////////////////////////
type Circuit struct {
wires []int
gates []Gate
outputWire int
numInput int
}
func NewCircuit(numInput int) *Circuit {
return &Circuit{
wires: make([]int, numInput),
gates: make([]Gate, 0),
numInput : numInput,
}
}
func (c *Circuit) getInputWires(i int) (Li, Ri int) {
return i, c.numInput+i
}
func (c *Circuit) AddGate(Li, Ri int, f func(a,b int) int) *Gate {
c.wires = append(c.wires, 0)
g := newGate(Li,Ri,len(c.wires)-1,f)
c.gates = append(c.gates, *g)
return g
}
type Gate struct {
Li, Ri int
i int
truthfunc func(a,b int) int
}
func newGate(Li, Ri, i int, f func(a,b int) int) (g *Gate) {
g.Li = Li
g.Ri = Ri
g.i = i
g.truthfunc = f
return
}
////////////////////////////////////////////////////////////////
// GATES
////////////////////////////////////////////////////////////////
func ORGate(a, b int) int {
if a+b != 0 {
return 1
} else {
return 0
}
}
func ORGateWithNot(a,b int) int {
if ((1-a)+b) == 0 {
return 0
} else {
return 1
}
}
func ANDGate(a,b int) int {
return a*b
}
...@@ -8,7 +8,7 @@ import ( ...@@ -8,7 +8,7 @@ import (
func TestGBC(t *testing.T) { func TestGBC(t *testing.T) {
K := NewTable(3, 2, 128) K := NewTable(3, 2, 128)
K.RandomizeTable() K.randomizeTable()
res := GBC(K, 0, 1, 2, ORGate, g) res := GBC(K, 0, 1, 2, ORGate, g)
print(res) print(res)
...@@ -38,10 +38,6 @@ func g(A, B []byte, i int) []byte { ...@@ -38,10 +38,6 @@ func g(A, B []byte, i int) []byte {
return make([]byte, len(A)*2) return make([]byte, len(A)*2)
} }
func ORGate(a, b int) int { func TestDecode(t *testing.T) {
if a+b != 0 {
return 1
} else {
return 0
}
} }
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