Commit 9dd8f7ad authored by Thomas Hoffmann's avatar Thomas Hoffmann
Browse files

Merge branch 'main' of gitlab.au.dk:au583826/crycomp into main

parents a989090f 186f27d8
...@@ -3,8 +3,8 @@ package main ...@@ -3,8 +3,8 @@ package main
import ( import (
"crycomp/internal/blood" "crycomp/internal/blood"
"crycomp/internal/crypto/oblivious"
"crycomp/internal/crypto/garbled" "crycomp/internal/crypto/garbled"
"crycomp/internal/crypto/oblivious"
"crycomp/internal/util" "crycomp/internal/util"
"fmt" "fmt"
) )
...@@ -22,14 +22,14 @@ type Party struct { ...@@ -22,14 +22,14 @@ type Party struct {
outChan chan []byte outChan chan []byte
} }
func NewParty(x int, inChan chan [][]byte, circuitChan chan garbled.Table, outChan chan []byte, party *oblivious.Party) (p *Party){ func NewParty(x int, inChan chan [][]byte, circuitChan chan garbled.Table, outChan chan []byte, party *oblivious.Party) (p *Party) {
return &Party{ return &Party{
x : util.Int2Bools(x,3), x: util.Int2Bools(x, 3),
EncInput : make([][]byte,6), EncInput: make([][]byte, 6),
InChan : inChan, InChan: inChan,
outChan : outChan, outChan: outChan,
CircuitChan : circuitChan, CircuitChan: circuitChan,
Party : party, Party: party,
} }
} }
...@@ -43,12 +43,16 @@ func (p *Party) OTSend() { ...@@ -43,12 +43,16 @@ func (p *Party) OTSend() {
} }
} }
func (p *Party) OTReceive() (res [][]byte, err error){ func (p *Party) OTReceive() (res [][]byte, err error) {
res = make([][]byte, 3) res = make([][]byte, 3)
var val int var val int
for i := range p.x { for i := range p.x {
if p.x[i] {val = 1} else {val = 0} if p.x[i] {
var result, err = p.ObliviousTransfer(2,val) val = 1
} else {
val = 0
}
var result, err = p.ObliviousTransfer(2, val)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -57,8 +61,7 @@ func (p *Party) OTReceive() (res [][]byte, err error){ ...@@ -57,8 +61,7 @@ func (p *Party) OTReceive() (res [][]byte, err error){
return res, nil return res, nil
} }
func OTransfer(sender, receiver *Party) {
func OTransfer(sender,receiver *Party) {
sender.Party, receiver.Party = oblivious.NewPartyPair() sender.Party, receiver.Party = oblivious.NewPartyPair()
//sender.Listen(sender.Circuit.e) //sender.Listen(sender.Circuit.e)
} }
...@@ -105,8 +108,7 @@ func RunProtocol(x, y int, outputMode int) (bool, error) { ...@@ -105,8 +108,7 @@ func RunProtocol(x, y int, outputMode int) (bool, error) {
Alice.Circuit = &garbled.GCircuit{F: &F} Alice.Circuit = &garbled.GCircuit{F: &F}
fmt.Printf("Alice has received F\n") 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...)
...@@ -120,7 +122,7 @@ func RunProtocol(x, y int, outputMode int) (bool, error) { ...@@ -120,7 +122,7 @@ func RunProtocol(x, y int, outputMode int) (bool, error) {
// 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") fmt.Printf("Alice has learned the output\n")
} }
if outputMode != 0 { // Bob learns if outputMode != 0 { // Bob learns
......
...@@ -6,20 +6,6 @@ import ( ...@@ -6,20 +6,6 @@ import (
"testing" "testing"
) )
func TestBloodTable(t *testing.T) {
// Check the dimensions of BloodTable.
if len(blood.Table) != 8 {
t.Fatalf("Expected 8 rows, got %d", len(blood.Table))
}
for i := range blood.Table {
if len(blood.Table[i]) != 8 {
t.Fatalf("Expected columns in row %d, got %d", i, len(blood.Table))
}
}
}
func TestProtocol(t *testing.T) { func TestProtocol(t *testing.T) {
// Runs the protocol for all combinations of recipient and donor blood types. // Runs the protocol for all combinations of recipient and donor blood types.
n := len(blood.Table) n := len(blood.Table)
......
package blood
import "testing"
func TestBloodTable(t *testing.T) {
// Check the dimensions of BloodTable.
if len(Table) != 8 {
t.Fatalf("Expected 8 rows, got %d", len(Table))
}
for i := range Table {
if len(Table[i]) != 8 {
t.Fatalf("Expected columns in row %d, got %d", i, len(Table))
}
}
}
...@@ -23,9 +23,10 @@ func Int2Bools(x, n int) []bool { ...@@ -23,9 +23,10 @@ func Int2Bools(x, n int) []bool {
return output return output
} }
func XOR (a,b []byte) (c []byte) { // XOR computes the byte-wise XOR of two byte slices. Slices must hve equal length.
func XOR(a, b []byte) (c []byte) {
if len(a) != len(b) { if len(a) != len(b) {
panic("Parameters length must be equal") panic("length must be equal")
} }
c = make([]byte, len(a)) c = make([]byte, len(a))
for i := range a { for i := range a {
......
package util
import (
"crycomp/internal/blood"
"testing"
)
func AssertBloodType(t *testing.T, bloodType int, expected []bool) {
actual := Int2Bools(bloodType, 3)
if len(actual) != len(expected) {
t.Fatalf("Expected length to be %d, was %d", len(expected), len(actual))
}
for i := 0; i < 3; i++ {
if actual[i] != expected[i] {
t.Errorf("Expected %t at index %d, got %t", expected[i], i, actual[i])
}
}
}
func TestInt2Bools(t *testing.T) {
t.Run("O-", func(t *testing.T) { AssertBloodType(t, blood.Type_On, []bool{false, false, false}) })
t.Run("O+", func(t *testing.T) { AssertBloodType(t, blood.Type_Op, []bool{true, false, false}) })
t.Run("A-", func(t *testing.T) { AssertBloodType(t, blood.Type_An, []bool{false, true, false}) })
t.Run("A+", func(t *testing.T) { AssertBloodType(t, blood.Type_Ap, []bool{true, true, false}) })
t.Run("B-", func(t *testing.T) { AssertBloodType(t, blood.Type_Bn, []bool{false, false, true}) })
t.Run("B+", func(t *testing.T) { AssertBloodType(t, blood.Type_Bp, []bool{true, false, true}) })
t.Run("AB-", func(t *testing.T) { AssertBloodType(t, blood.Type_ABn, []bool{false, true, true}) })
t.Run("AB+", func(t *testing.T) { AssertBloodType(t, blood.Type_ABp, []bool{true, true, true}) })
}
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