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{
{true, true, true, true, true, true, true, true},
}
// EncodeBloodType return x as a slice containing the binary representation.
func EncodeBloodType(x, n int) []bool {
// Int2Bools return x as a slice containing the binary representation.
func Int2Bools(x, n int) []bool {
output := make([]bool, 0)
tmp := 0
for i := n - 1; i >= 0; i-- {
......@@ -54,21 +54,6 @@ func EncodeBloodType(x, n int) []bool {
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 {
wires []bool
......@@ -76,10 +61,13 @@ type Party struct {
out chan bool
}
func NewParty(bloodType int, secrets []bool) *Party {
func NewParty(in, out chan bool) *Party {
return &Party{
wires: make([]bool, 0),
in: in,
out: out,
}
}
// Push v to the wires and return the index of the pushed value.
......@@ -88,9 +76,9 @@ func (p *Party) Push(v bool) int {
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.
func (p *Party) Share(x bool) int {
func (p *Party) Input(x bool) int {
xb := rand.Intn(2) == 1
xa := x != xb // x XOR xb
......@@ -99,47 +87,48 @@ func (p *Party) Share(x bool) int {
}
func (p *Party) Receive() int {
p.wires = append(p.wires, <-p.in)
return len(p.wires) - 1
return p.Push(<-p.in)
}
func (a *Party) Send(wire int) {
a.out <- a.wires[wire]
func (p *Party) Send(wire int) {
p.out <- p.wires[wire]
}
func (a *Party) Combine(xwire, ywire int) int {
a.wires = append(a.wires, a.wires[xwire] && a.wires[ywire])
return len(a.wires) - 1
func (p *Party) Combine(xwire, ywire int) int {
return p.Push(p.wires[xwire] && p.wires[ywire])
}
func (a *Party) XORC(xwire int, c bool, mode bool) int {
var xa = a.wires[xwire]
func (p *Party) XORC(xwire int, c bool, mode bool) int {
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 {
a.wires = append(a.wires, xa)
return xwire
}
return len(a.wires) - 1
}
func (a *Party) ANDC(xwire int, c bool) int {
var xa = a.wires[xwire]
a.wires = append(a.wires, xa && c)
return len(a.wires) - 1
func (p *Party) ANDC(xwire int, c bool) int {
return p.Push(p.wires[xwire] && c)
}
func (a *Party) XOR2W(xwire, ywire int) int {
var xa, xb = a.wires[xwire], a.wires[ywire]
a.wires = append(a.wires, xa != xb)
return len(a.wires) - 1
func (p *Party) XOR2W(xwire, ywire int) int {
return p.Push(p.wires[xwire] != p.wires[ywire])
}
type Protocol struct {
Alice *Party
Bob *Party
A *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 {
A, B := P.Alice, P.Bob
A, B := P.A, P.B
//1a. Generate [u], [v] and [w]
u := rand.Intn(2) == 1
ua := rand.Intn(2) == 1
......@@ -163,10 +152,10 @@ func (P *Protocol) AND(xwire, ywire int) int {
var idx_w, _ = A.Receive(), B.Receive()
//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]
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
A.Send(idx_d1)
......@@ -179,8 +168,8 @@ func (P *Protocol) AND(xwire, ywire int) int {
var idx_e2, _ = A.Receive(), B.Receive()
//6a. Compute d and e
var idx_d, _ = 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_d = P.XOR2W(idx_d1, idx_d2) //A.XOR2W(idx_d1, idx_d2), B.XOR2W(idx_d1, idx_d2)
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
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 {
//6c. Compute [z]
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_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
}
// Input the x bit to
func (P *Protocol) Input(x, y bool) (int, int) {
var idx1 = P.Alice.Share(x)
P.Bob.Receive()
P.Bob.Share(y)
var idx2 = P.Alice.Receive()
var idx1 = P.A.Input(x)
P.B.Receive()
P.B.Input(y)
var idx2 = P.A.Receive()
return idx1, idx2
}
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
}
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
}
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
}
//Only 1 share should be flipped
//NOT A == A XOR 1
func (P *Protocol) NOT(xwire int) int {
i := P.Alice.XORC(xwire, true, true)
_ = P.Bob.XORC(xwire, true, false)
i := P.A.XORC(xwire, true, true)
_ = P.B.XORC(xwire, true, false)
return i
}
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)
if A_Learns {
B.Send(wire)
......@@ -246,15 +237,6 @@ func (P *Protocol) Output(wire int, A_Learns, B_Learns bool) []bool {
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
// protocol.
//
......@@ -277,28 +259,24 @@ func (P *Protocol) Run(x, y []bool) (bool, error) {
if lenY := len(y); lenY != 3 {
return false, fmt.Errorf("length of y-encoding must be 3, was %d", lenY)
}
// Input round
// Input round
idx_s, _ := P.Input(x[0], y[0])
idx_A, _ := P.Input(x[1], y[1])
idx_B, _ := P.Input(x[2], y[2])
//Input round
var idx_xS, idx_yS = P.Input(x[2], y[2])
var idx_xA, idx_yA = P.Input(x[1], y[1])
var idx_xB, idx_yB = P.Input(x[0], y[0])
//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_s)
idx_f1 = P.AND(idx_f1, idx_s) // NOT x_sign AND y_sign
var idx_f1 = P.NOT(idx_xS)
idx_f1 = P.AND(idx_f1, idx_yS) // 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)
var idx_f2 = P.NOT(idx_A) //compute NOT x_A
idx_f2 = P.AND(idx_f2, idx_A) // NOT x_A AND y_A
var idx_f2 = P.NOT(idx_xA) //compute NOT x_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)
//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
idx_f3 = P.AND(idx_f3, idx_B) // NOT x_B AND y_B
var idx_f3 = P.NOT(idx_xB) //compute NOT x_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)
//d. Compute f(x,y)
......@@ -311,13 +289,8 @@ func (P *Protocol) Run(x, y []bool) (bool, error) {
func RunProtocol(x, y int) (bool, error) {
protocol := NewProtocol()
encodingX := EncodeBloodType(x, 3)
encodingY := EncodeBloodType(y, 3)
dealer := NewDealer()
A := NewParty(x, dealer.DealA())
B := NewParty(y, dealer.DealB())
encodingX := Int2Bools(x, 3)
encodingY := Int2Bools(y, 3)
return protocol.Run(encodingX, encodingY)
}
......
......@@ -5,11 +5,281 @@ import (
"testing"
)
func CheckBloodType(t *testing.T, bloodType int, expected []bool) {
actual := EncodeBloodType(bloodType, 3)
func TestBloodTable(t *testing.T) {
// 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]
if resT != false || resF != true {
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 TestProtocolOUTPUT(t *testing.T) {
p := NewProtocol()
idxTrue, idxFalse := p.Input(true, false)
for i := 0; i < 8; i++ {
params := Int2Bools(i, 3)
var idx int
if params[0] {
idx = idxTrue
} else {
idx = idxFalse
}
res := p.Output(idx, params[1], params[2])
if params[1] == false && params[2] == false {
if len(res) != 0 {
t.Fatal("output should be empty")
}
} else if params[1] == true && params[2] == true {
if len(res) != 2 {
t.Fatal("output should have 2 values")
}
if res[0] != params[0] || res[1] != params[0] {
t.Error("Output does not match input")
}
} else {
if len(res) != 1 {
t.Fatal("output should have 1 value")
}
if res[0] != params[0] {
t.Error("Output does not match input")
}
}
}
}
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))
t.Fatalf("Expected length to be %d, was %d", len(expected), len(actual))
}
for i := 0; i < 3; i++ {
......@@ -20,14 +290,14 @@ func CheckBloodType(t *testing.T, bloodType int, expected []bool) {
}
func TestInt2Bools(t *testing.T) {
t.Run("O-", func(t *testing.T) { CheckBloodType(t, BloodType_On, []bool{false, false, false}) })
t.Run("O+", func(t *testing.T) { CheckBloodType(t, BloodType_Op, []bool{true, false, false}) })
t.Run("A-", func(t *testing.T) { CheckBloodType(t, BloodType_An, []bool{false, true, false}) })
t.Run("A+", func(t *testing.T) { CheckBloodType(t, BloodType_Ap, []bool{true, true, false}) })
t.Run("B-", func(t *testing.T) { CheckBloodType(t, BloodType_Bn, []bool{false, false, true}) })
t.Run("B+", func(t *testing.T) { CheckBloodType(t, BloodType_Bp, []bool{true, false, true}) })
t.Run("AB-", func(t *testing.T) { CheckBloodType(t, BloodType_ABn, []bool{false, true, true}) })
t.Run("AB+", func(t *testing.T) { CheckBloodType(t, BloodType_ABp, []bool{true, true, true}) })
t.Run("O-", func(t *testing.T) { AssertBloodType(t, BloodType_On, []bool{false, false, false}) })
t.Run("O+", func(t *testing.T) { AssertBloodType(t, BloodType_Op, []bool{true, false, false}) })
t.Run("A-", func(t *testing.T) { AssertBloodType(t, BloodType_An, []bool{false, true, false}) })
t.Run("A+", func(t *testing.T) { AssertBloodType(t, BloodType_Ap, []bool{true, true, false}) })
t.Run("B-", func(t *testing.T) { AssertBloodType(t, BloodType_Bn, []bool{false, false, true}) })
t.Run("B+", func(t *testing.T) { AssertBloodType(t, BloodType_Bp, []bool{true, false, true}) })
t.Run("AB-", func(t *testing.T) { AssertBloodType(t, BloodType_ABn, []bool{false, true, true}) })
t.Run("AB+", func(t *testing.T) { AssertBloodType(t, BloodType_ABp, []bool{true, true, true}) })
}
func TestProtocol(t *testing.T) {
......
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