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

Fix some tests

parents a5f8aaf0 e229081f
...@@ -35,8 +35,8 @@ var BloodTable = [][]bool{ ...@@ -35,8 +35,8 @@ var BloodTable = [][]bool{
{true, true, true, true, true, true, true, true}, {true, true, true, true, true, true, true, true},
} }
// EncodeBloodType return x as a slice containing the binary representation. // Int2Bools return x as a slice containing the binary representation.
func EncodeBloodType(x, n int) []bool { func Int2Bools(x, n int) []bool {
output := make([]bool, 0) output := make([]bool, 0)
tmp := 0 tmp := 0
for i := n - 1; i >= 0; i-- { for i := n - 1; i >= 0; i-- {
...@@ -54,21 +54,6 @@ func EncodeBloodType(x, n int) []bool { ...@@ -54,21 +54,6 @@ func EncodeBloodType(x, n int) []bool {
return output return output
} }
type Dealer struct {
}
func NewDealer() *Dealer {
return &Dealer{}
}
func (d *Dealer) DealA() []bool {
return []bool{}
}
func (d *Dealer) DealB() []bool {
return []bool{}
}
type Party struct { type Party struct {
wires []bool wires []bool
...@@ -76,10 +61,13 @@ type Party struct { ...@@ -76,10 +61,13 @@ type Party struct {
out chan bool out chan bool
} }
func NewParty(bloodType int, secrets []bool) *Party { func NewParty(in, out chan bool) *Party {
return &Party{ return &Party{
wires: make([]bool, 0), wires: make([]bool, 0),
in: in,
out: out,
} }
} }
// Push v to the wires and return the index of the pushed value. // Push v to the wires and return the index of the pushed value.
...@@ -88,9 +76,9 @@ func (p *Party) Push(v bool) int { ...@@ -88,9 +76,9 @@ func (p *Party) Push(v bool) int {
return len(p.wires) - 1 return len(p.wires) - 1
} }
// Share samples a random value xb, computes xa = x XOR xb and sends xb to the // Input samples a random value xb, computes xa = x XOR xb and sends xb to the
// other party. // other party.
func (p *Party) Share(x bool) int { func (p *Party) Input(x bool) int {
xb := rand.Intn(2) == 1 xb := rand.Intn(2) == 1
xa := x != xb // x XOR xb xa := x != xb // x XOR xb
...@@ -99,47 +87,48 @@ func (p *Party) Share(x bool) int { ...@@ -99,47 +87,48 @@ func (p *Party) Share(x bool) int {
} }
func (p *Party) Receive() int { func (p *Party) Receive() int {
p.wires = append(p.wires, <-p.in) return p.Push(<-p.in)
return len(p.wires) - 1
} }
func (a *Party) Send(wire int) { func (p *Party) Send(wire int) {
a.out <- a.wires[wire] p.out <- p.wires[wire]
} }
func (a *Party) Combine(xwire, ywire int) int { func (p *Party) Combine(xwire, ywire int) int {
a.wires = append(a.wires, a.wires[xwire] && a.wires[ywire]) return p.Push(p.wires[xwire] && p.wires[ywire])
return len(a.wires) - 1
} }
func (a *Party) XORC(xwire int, c bool, mode bool) int { func (p *Party) XORC(xwire int, c bool, mode bool) int {
var xa = a.wires[xwire]
if mode { //Only one party is supposed to do the XOR if mode { //Only one party is supposed to do the XOR
a.wires = append(a.wires, xa != c) return p.Push(p.wires[xwire] != c)
} else { } else {
a.wires = append(a.wires, xa) return xwire
} }
return len(a.wires) - 1
} }
func (a *Party) ANDC(xwire int, c bool) int { func (p *Party) ANDC(xwire int, c bool) int {
var xa = a.wires[xwire] return p.Push(p.wires[xwire] && c)
a.wires = append(a.wires, xa && c)
return len(a.wires) - 1
} }
func (a *Party) XOR2W(xwire, ywire int) int { func (p *Party) XOR2W(xwire, ywire int) int {
var xa, xb = a.wires[xwire], a.wires[ywire] return p.Push(p.wires[xwire] != p.wires[ywire])
a.wires = append(a.wires, xa != xb)
return len(a.wires) - 1
} }
type Protocol struct { type Protocol struct {
Alice *Party A *Party
Bob *Party B *Party
}
func NewProtocol() *Protocol {
var a2b = make(chan bool, 1) //Directional non-blocking channels
var b2a = make(chan bool, 1)
return &Protocol{
A: NewParty(b2a, a2b),
B: NewParty(a2b, b2a),
}
} }
func (P *Protocol) AND(xwire, ywire int) int { func (P *Protocol) AND(xwire, ywire int) int {
A, B := P.Alice, P.Bob A, B := P.A, P.B
//1a. Generate [u], [v] and [w] //1a. Generate [u], [v] and [w]
u := rand.Intn(2) == 1 u := rand.Intn(2) == 1
ua := rand.Intn(2) == 1 ua := rand.Intn(2) == 1
...@@ -163,10 +152,10 @@ func (P *Protocol) AND(xwire, ywire int) int { ...@@ -163,10 +152,10 @@ func (P *Protocol) AND(xwire, ywire int) int {
var idx_w, _ = A.Receive(), B.Receive() var idx_w, _ = A.Receive(), B.Receive()
//2 compute [d] = [x] XOR [u] //2 compute [d] = [x] XOR [u]
var idx_d1, _ = A.XOR2W(xwire, idx_u), B.XOR2W(xwire, idx_u) var idx_d1 = P.XOR2W(xwire, idx_u) //A.XOR2W(xwire, idx_u), B.XOR2W(xwire, idx_u)
//3. compute [e] = [y] XOR [v] //3. compute [e] = [y] XOR [v]
var idx_e1, _ = A.XOR2W(ywire, idx_v), B.XOR2W(ywire, idx_v) var idx_e1 = P.XOR2W(ywire, idx_v) //A.XOR2W(ywire, idx_v), B.XOR2W(ywire, idx_v)
//4. Open d //4. Open d
A.Send(idx_d1) A.Send(idx_d1)
...@@ -179,8 +168,8 @@ func (P *Protocol) AND(xwire, ywire int) int { ...@@ -179,8 +168,8 @@ func (P *Protocol) AND(xwire, ywire int) int {
var idx_e2, _ = A.Receive(), B.Receive() var idx_e2, _ = A.Receive(), B.Receive()
//6a. Compute d and e //6a. Compute d and e
var idx_d, _ = A.XOR2W(idx_d1, idx_d2), B.XOR2W(idx_d1, idx_d2) var idx_d = P.XOR2W(idx_d1, idx_d2) //A.XOR2W(idx_d1, idx_d2), B.XOR2W(idx_d1, idx_d2)
var idx_e, _ = A.XOR2W(idx_e1, idx_e2), B.XOR2W(idx_e1, idx_e2) var idx_e = P.XOR2W(idx_e1, idx_e2) //A.XOR2W(idx_e1, idx_e2), B.XOR2W(idx_e1, idx_e2)
//6b. Compute [z]-parts //6b. Compute [z]-parts
var idx_z1, _ = A.ANDC(xwire, A.wires[idx_e]), B.ANDC(xwire, B.wires[idx_e]) var idx_z1, _ = A.ANDC(xwire, A.wires[idx_e]), B.ANDC(xwire, B.wires[idx_e])
...@@ -190,42 +179,44 @@ func (P *Protocol) AND(xwire, ywire int) int { ...@@ -190,42 +179,44 @@ func (P *Protocol) AND(xwire, ywire int) int {
//6c. Compute [z] //6c. Compute [z]
var idx_z4, _ = A.XOR2W(idx_w, idx_z1), B.XOR2W(idx_w, idx_z1) var idx_z4, _ = A.XOR2W(idx_w, idx_z1), B.XOR2W(idx_w, idx_z1)
var idx_z5, _ = A.XOR2W(idx_z4, idx_z2), B.XOR2W(idx_z4, idx_z2) var idx_z5, _ = A.XOR2W(idx_z4, idx_z2), B.XOR2W(idx_z4, idx_z2)
var idx_z, _ = A.XOR2W(idx_z5, idx_z3), B.XOR2W(idx_z5, idx_z3) var idx_z, _ = A.XORC(idx_z5, A.wires[idx_z3], true), B.XORC(idx_z5, B.wires[idx_z3], false)
return idx_z return idx_z
} }
// Input the x bit to // Input the x bit to
func (P *Protocol) Input(x, y bool) (int, int) { func (P *Protocol) Input(x, y bool) (int, int) {
var idx1 = P.Alice.Share(x) var idx1 = P.A.Input(x)
P.Bob.Receive() P.B.Receive()
P.Bob.Share(y) P.B.Input(y)
var idx2 = P.Alice.Receive() var idx2 = P.A.Receive()
return idx1, idx2 return idx1, idx2
} }
func (P *Protocol) XORC(wire int, c bool) int { func (P *Protocol) XORC(wire int, c bool) int {
var idx, _ = P.Alice.XORC(wire, c, true), P.Bob.XORC(wire, c, false) var idx, _ = P.A.XORC(wire, c, true), P.B.XORC(wire, c, false)
return idx return idx
} }
func (P *Protocol) ANDC(wire int, c bool) int { func (P *Protocol) ANDC(wire int, c bool) int {
var idx, _ = P.Alice.ANDC(wire, c), P.Bob.ANDC(wire, c) var idx, _ = P.A.ANDC(wire, c), P.B.ANDC(wire, c)
return idx return idx
} }
func (P *Protocol) XOR2W(xwire, ywire int) int { func (P *Protocol) XOR2W(xwire, ywire int) int {
var idx, _ = P.Alice.XOR2W(xwire, ywire), P.Bob.XOR2W(xwire, ywire) var idx, _ = P.A.XOR2W(xwire, ywire), P.B.XOR2W(xwire, ywire)
return idx return idx
} }
//Only 1 share should be flipped
//NOT A == A XOR 1
func (P *Protocol) NOT(xwire int) int { func (P *Protocol) NOT(xwire int) int {
i := P.Alice.XORC(xwire, true, true) i := P.A.XORC(xwire, true, true)
_ = P.Bob.XORC(xwire, true, false) _ = P.B.XORC(xwire, true, false)
return i return i
} }
func (P *Protocol) Output(wire int, A_Learns, B_Learns bool) []bool { func (P *Protocol) Output(wire int, A_Learns, B_Learns bool) []bool {
var A, B = P.Alice, P.Bob var A, B = P.A, P.B
var output = make([]bool, 0) var output = make([]bool, 0)
if A_Learns { if A_Learns {
B.Send(wire) B.Send(wire)
...@@ -246,15 +237,6 @@ func (P *Protocol) Output(wire int, A_Learns, B_Learns bool) []bool { ...@@ -246,15 +237,6 @@ func (P *Protocol) Output(wire int, A_Learns, B_Learns bool) []bool {
return output return output
} }
func NewProtocol() *Protocol {
// var a2b = make(chan bool, 1) //Directional non-blocking channels
// var b2a = make(chan bool, 1)
return &Protocol{
// Alice: NewParty(b2a, a2b),
// Bob: NewParty(a2b, b2a),
}
}
// Run the protocol for blood type compatibility using a passive secure BeDOZa // Run the protocol for blood type compatibility using a passive secure BeDOZa
// protocol. // protocol.
// //
...@@ -277,29 +259,25 @@ func (P *Protocol) Run(x, y []bool) (bool, error) { ...@@ -277,29 +259,25 @@ func (P *Protocol) Run(x, y []bool) (bool, error) {
if lenY := len(y); lenY != 3 { if lenY := len(y); lenY != 3 {
return false, fmt.Errorf("length of y-encoding must be 3, was %d", lenY) return false, fmt.Errorf("length of y-encoding must be 3, was %d", lenY)
} }
//Input round
// Input round var idx_xS, idx_yS = P.Input(x[2], y[2])
var idx_xA, idx_yA = P.Input(x[1], y[1])
// Input round var idx_xB, idx_yB = P.Input(x[0], y[0])
idx_s, _ := P.Input(x[0], y[0])
idx_A, _ := P.Input(x[1], y[1])
idx_B, _ := P.Input(x[2], y[2])
//a. Compute (x_sign OR NOT y_sign) = NOT (NOT x_sign AND y_sign) //a. Compute (x_sign OR NOT y_sign) = NOT (NOT x_sign AND y_sign)
//var idx_f1 = alice.XORC(idx_s, true , true) //compute NOT x_sign var idx_f1 = P.NOT(idx_xS)
var idx_f1 = P.NOT(idx_s) idx_f1 = P.AND(idx_f1, idx_yS) // NOT x_sign AND y_sign
idx_f1 = P.AND(idx_f1, idx_s) // NOT x_sign AND y_sign idx_f1 = P.NOT(idx_f1) //NOT (NOT x_sign AND y_sign)
idx_f1 = P.NOT(idx_f1) //NOT (NOT x_sign AND y_sign)
//b. Compute (x_A OR NOT y_A) = NOT (NOT x_A AND y_A) //b. Compute (x_A OR NOT y_A) = NOT (NOT x_A AND y_A)
var idx_f2 = P.NOT(idx_A) //compute NOT x_A var idx_f2 = P.NOT(idx_xA) //compute NOT x_A
idx_f2 = P.AND(idx_f2, idx_A) // NOT x_A AND y_A idx_f2 = P.AND(idx_f2, idx_yA) // NOT x_A AND y_A
idx_f2 = P.NOT(idx_f2) //NOT (NOT x_A AND y_A) idx_f2 = P.NOT(idx_f2) //NOT (NOT x_A AND y_A)
//c. Compute (x_B OR NOT y_B) = NOT (NOT x_B AND y_B) //c. Compute (x_B OR NOT y_B) = NOT (NOT x_B AND y_B)
var idx_f3 = P.NOT(idx_B) //compute NOT x_B var idx_f3 = P.NOT(idx_xB) //compute NOT x_B
idx_f3 = P.AND(idx_f3, idx_B) // NOT x_B AND y_B idx_f3 = P.AND(idx_f3, idx_yB) // NOT x_B AND y_B
idx_f3 = P.NOT(idx_f3) //NOT (NOT x_B AND y_B) idx_f3 = P.NOT(idx_f3) //NOT (NOT x_B AND y_B)
//d. Compute f(x,y) //d. Compute f(x,y)
var idx_f = P.AND(idx_f1, idx_f2) var idx_f = P.AND(idx_f1, idx_f2)
...@@ -311,13 +289,8 @@ func (P *Protocol) Run(x, y []bool) (bool, error) { ...@@ -311,13 +289,8 @@ func (P *Protocol) Run(x, y []bool) (bool, error) {
func RunProtocol(x, y int) (bool, error) { func RunProtocol(x, y int) (bool, error) {
protocol := NewProtocol() protocol := NewProtocol()
encodingX := EncodeBloodType(x, 3) encodingX := Int2Bools(x, 3)
encodingY := EncodeBloodType(y, 3) encodingY := Int2Bools(y, 3)
dealer := NewDealer()
A := NewParty(x, dealer.DealA())
B := NewParty(y, dealer.DealB())
return protocol.Run(encodingX, encodingY) return protocol.Run(encodingX, encodingY)
} }
......
...@@ -5,11 +5,281 @@ import ( ...@@ -5,11 +5,281 @@ import (
"testing" "testing"
) )
func CheckBloodType(t *testing.T, bloodType int, expected []bool) { func TestBloodTable(t *testing.T) {
actual := EncodeBloodType(bloodType, 3) // Check the dimensions of BloodTable.
if len(BloodTable) != 8 {
t.Fatalf("Expected 8 rows, got %d", len(BloodTable))
}
for i := range BloodTable {
if len(BloodTable[i]) != 8 {
t.Fatalf("Expected columns in row %d, got %d", i, len(BloodTable))
}
}
}
////////////////////////////////////////////////////////////////
//// Party Tests ////
////////////////////////////////////////////////////////////////
func TestPartyWires(t *testing.T) {
t.Run("WireSize|Input", TestPartyInput)
t.Run("WireSize|Receive", TestPartyReceive)
}
func TestPartyInput(t *testing.T) {
A := NewProtocol().A
idx := A.Input(true)
if len(A.wires) != 1 {
t.Errorf("Expected len(A.wires) == %d, was %d", 1, idx)
}
if idx != 0 {
t.Errorf("Expected idx == %d, was %d", 0, idx)
}
}
func TestPartyReceive(t *testing.T) {
p := NewProtocol()
A, B := p.A, p.B
A.Input(true)
idx := B.Receive()
if len(B.wires) != 1 {
t.Errorf("Expected len(A.wires) == %d, was %d", 1, idx)
}
if idx != 0 {
t.Errorf("Expected idx == %d, was %d", 0, idx)
}
}
func TestPartyCombine(t *testing.T) {
// TODO
p := NewProtocol()
A, B := p.A, p.B
a := A.Input(true)
B.Receive()
b := A.Input(false)
ASizeBefore, BSizeBefore := len(p.A.wires), len(p.B.wires)
idx := p.A.Combine(a, b)
if len(p.A.wires) != ASizeBefore+1 && len(p.B.wires) != BSizeBefore {
t.Error()
}
if idx != 2 {
t.Errorf("Expected idx == %d, was %d", 2, idx)
}
}
func TestPartyXORC(t *testing.T) {
A := NewProtocol().A
A.Input(true)
idx1 := A.XORC(0, true, true)
if len(A.wires) != 2 {
t.Errorf("Expected len(A.wires) == %d, was %d", 2, len(A.wires))
}
if idx1 != 1 {
t.Errorf("Expected idx1 == %d, was %d", 1, idx1)
}
idx2 := A.XORC(0, true, false)
if len(A.wires) != 2 {
t.Errorf("Expected len(A.wires) == %d, was %d", 2, len(A.wires))
}
if idx2 != 0 {
t.Errorf("Expected idx2 == %d, was %d", 0, idx2)
}
}
func TestPartyANDC(t *testing.T) {
A := NewProtocol().A
A.Input(true)
idx := A.ANDC(0, true)
if len(A.wires) != 2 {
t.Errorf("Expected len(A.wires) == %d, was %d", 2, len(A.wires))
}
if idx != 1 {
t.Errorf("Expected idx == %d, was %d", 1, idx)
}
}
func TestPartyXOR(t *testing.T) {
p := NewProtocol()
A, B := p.A, p.B
A.Input(true)
B.Receive()
A.Input(false)
idx := A.XOR2W(0, 1)
if len(A.wires) != 3 {
t.Errorf("Expected len(A.wires) == %d, was %d", 3, len(A.wires))
}
if idx != 2 {
t.Errorf("Expected idx == %d, was %d", 2, idx)
}
}
func TestStackAND(t *testing.T) {
p := NewProtocol()
a := p.A.Input(true)
p.B.Receive()
b := p.A.Input(false)
p.B.Receive()
idx := p.AND(a, b)
if len(p.A.wires) != 17 || len(p.B.wires) != 17 {
t.Error()
}
if idx != 16 {
t.Error()
}
t.Log("Success")
}
////////////////////////////////////////////////////////////////
//// Protocol Tests ////
////////////////////////////////////////////////////////////////
func TestProtocolFunctions(t *testing.T) {
}
func TestProtocolInput(t *testing.T) {
p := NewProtocol()
idxA, idxB := p.Input(true, false)
shareA, shareB := p.A.wires[idxA] != p.B.wires[idxA], p.A.wires[idxB] != p.B.wires[idxB]
if shareA != true || shareB != false {
t.Fatal("Shares has wrong values")
}
if len(p.A.wires) != len(p.B.wires) {
t.Fatal("Party stack size is not equal after function call")
}
}
func TestProtocolXORC(t *testing.T) {
p := NewProtocol()
idxTrue, idxFalse := p.Input(true, false)
//True XOR True
idxTT := p.XORC(idxTrue, true)
idxTF := p.XORC(idxTrue, false)
idxFT := p.XORC(idxFalse, true)
idxFF := p.XORC(idxFalse, false)
resTT, resTF, resFT, resFF := p.A.wires[idxTT] != p.B.wires[idxTT],
p.A.wires[idxTF] != p.B.wires[idxTF],
p.A.wires[idxFT] != p.B.wires[idxFT],
p.A.wires[idxFF] != p.B.wires[idxFF]
if resTT != false || resTF != true || resFT != true || resFF != false {
t.Fatal("Shares has wrong values")
}
if len(p.A.wires) != len(p.B.wires) {
t.Fatal("Party stack size is not equal after function call")
}
}
func TestProtocolANDC(t *testing.T) {
p := NewProtocol()
idxTrue, idxFalse := p.Input(true, false)
//True XOR True
idxTT := p.ANDC(idxTrue, true)
idxTF := p.ANDC(idxTrue, false)
idxFT := p.ANDC(idxFalse, true)
idxFF := p.ANDC(idxFalse, false)
resTT, resTF, resFT, resFF := p.A.wires[idxTT] != p.B.wires[idxTT],
p.A.wires[idxTF] != p.B.wires[idxTF],
p.A.wires[idxFT] != p.B.wires[idxFT],
p.A.wires[idxFF] != p.B.wires[idxFF]
if resTT != true || resTF != false || resFT != false || resFF != false {
t.Fatal("Shares has wrong values")
}
if len(p.A.wires) != len(p.B.wires) {
t.Fatal("Party stack size is not equal after function call")
}
}
func TestProtocolXOR2W(t *testing.T) {
p := NewProtocol()
idxTrue, idxFalse := p.Input(true, false)
//True XOR True
idxTT := p.XOR2W(idxTrue, idxTrue)
idxTF := p.XOR2W(idxTrue, idxFalse)
idxFT := p.XOR2W(idxFalse, idxTrue)
idxFF := p.XOR2W(idxFalse, idxFalse)
resTT, resTF, resFT, resFF := p.A.wires[idxTT] != p.B.wires[idxTT],
p.A.wires[idxTF] != p.B.wires[idxTF],
p.A.wires[idxFT] != p.B.wires[idxFT],
p.A.wires[idxFF] != p.B.wires[idxFF]
if resTT != false || resTF != true || resFT != true || resFF != false {
t.Fatal("Shares has wrong values")
}
if len(p.A.wires) != len(p.B.wires) {
t.Fatal("Party stack size is not equal after function call")
}
}
func TestProtocolNOT(t *testing.T) {
p := NewProtocol()
idxTrue, idxFalse := p.Input(true, false)
idxT := p.NOT(idxTrue)
idxF := p.NOT(idxFalse)
resT, resF := p.A.wires[idxT] != p.B.wires[idxF],
p.A.wires[idxF] != p.B.wires[idxF]