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

Add some test, fix some bugs

parent 639191ef
......@@ -206,6 +206,8 @@ func (P *Protocol) XOR2W(xwire, ywire int) int {
return idx
}
//Only 1 share should be flipped
//NOT A == A XOR 1
func (P *Protocol) NOT(xwire int) int {
var idx, _ = P.A.XORC(xwire, true, true), P.B.XORC(xwire, true, false)
return idx
......@@ -226,8 +228,8 @@ func (P *Protocol) Output(wire int, A_Learns, B_Learns bool) []bool {
output = append(output, A.vals[idx_outA])
}
if B_Learns {
var idx_B = A.Receive()
var idx_outB = A.XOR2W(wire, idx_B)
var idx_B = B.Receive()
var idx_outB = B.XOR2W(wire, idx_B)
output = append(output, B.vals[idx_outB])
}
return output
......@@ -252,28 +254,27 @@ func (P *Protocol) RunProtocol(x, y []bool) bool {
return false
}
//Input round
var idx_s, _ = P.Input(x[0], y[0])
var idx_A, _ = P.Input(x[1], y[1])
var idx_B, _ = P.Input(x[2], y[2])
var idx_xS, idx_yS= P.Input(x[0], y[0])
var idx_xA, idx_yA = P.Input(x[1], y[1])
var idx_xB, idx_yB = P.Input(x[2], y[2])
//Compute f(x,y) = (x_sign OR NOT y_sign) AND
// (x_A OR NOT y_A) AND
// (x_B OR NOT y_B)
//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)
......
package main
import (
"fmt"
"testing"
)
var p Protocol
func init() {
p = *InitProtocol(69420)
}
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))
}
}
}
func TestParams(t *testing.T) {
// Check that params are calculated correctly.
p := NewParams(7)
if p.n != 7 {
t.Fatalf("Expected 7, got %d", p.n)
}
if p.nPow != 128 {
t.Fatalf("Expected 128, got %d", p.nPow)
}
}
func TestProtocol(t *testing.T) {
// Runs the protocol for all combinations of recipient and donor blood types.
n := len(BloodTable)
fmt.Println(n)
for x := 0; x < n; x++ {
for y := 0; y < n; y++ {
p = *InitProtocol(69420)
var px, py = Int2Bools(x, 3), Int2Bools(y, 3)
if z := p.RunProtocol(px, py); z != BloodTable[x][y] {
t.Errorf("Failed blood compatibility test for index [%d,%d]. Expected %t, got %t", x, y, !z, z)
}
}
}
}
func TestPartyStack(t *testing.T) {
t.Run("StackSize|Input", TestStackInput)
t.Run("StackSize|Input", TestStackReceive)
}
//Adds 1 value to A and 0 to B
func TestStackInput(t *testing.T) {
var idx = p.A.Input(true)
if len(p.A.vals) != 1 && len(p.B.vals) != 0 {
t.Error()
}
if idx != 1 {
t.Error()
}
}
//Adds 0 value to A and 1 to B
func TestStackReceive(t *testing.T) {
p.B.Input(true)
var idx = p.A.Receive()
if len(p.A.vals) != 1 && len(p.B.vals) != 1 {
t.Error()
}
if idx != 1 {
t.Error()
}
}
//Adds 1 value to A and 0 to B
func TestStackCombine(t *testing.T) {
var a = p.A.Input(true)
p.B.Receive()
var b = p.A.Input(false)
var ASizeBefore, BSizeBefore = len(p.A.vals), len(p.B.vals)
var idx = p.A.Combine(a, b)
if len(p.A.vals) != ASizeBefore+1 && len(p.B.vals) != BSizeBefore {
t.Error()
}
if idx != 2 {
t.Error()
}
}
//Adds 1 value to A and 0 to B
func TestStackXORC(t *testing.T) {
p.A.Input(true)
var ASizeBefore, BSizeBefore = len(p.A.vals), len(p.B.vals)
var idx1 = p.A.XORC(0, true, true)
if len(p.A.vals) != ASizeBefore+1 && len(p.B.vals) != BSizeBefore {
t.Error()
}
ASizeBefore, BSizeBefore = len(p.A.vals), len(p.B.vals)
var idx2 = p.A.XORC(0, true, false)
if len(p.A.vals) != ASizeBefore+1 && len(p.B.vals) != BSizeBefore {
t.Error()
}
if idx1 != 1 || idx2 != 2 {
t.Error()
}
}
//Adds 1 value to A and 0 to B
func TestStackANDC(t *testing.T) {
var a = p.A.Input(true)
var ASizeBefore, BSizeBefore = len(p.A.vals), len(p.B.vals)
var idx = p.A.ANDC(a, true)
if len(p.A.vals) != ASizeBefore+1 && len(p.B.vals) != BSizeBefore {
t.Error()
}
if idx != 1 {
t.Error()
}
}
func TestStackXOR2W(t *testing.T) {
var a = p.A.Input(true)
p.B.Receive()
var b = p.A.Input(false)
var idx = p.A.XOR2W(a, b)
if len(p.A.vals) != 3 && len(p.B.vals) != 1 {
t.Error()
}
if idx != 2 {
t.Error()
}
}
func TestStackAND(t *testing.T) {
var a = p.A.Input(true)
p.B.Receive()
var b = p.A.Input(false)
p.B.Receive()
var idx = p.AND(a, b)
if len(p.A.vals) != 17 || len(p.B.vals) != 17 {
t.Error()
}
if idx != 16 {
t.Error()
}
t.Log("Success")
}
////////////////////////////////////////////////////////////////
//// Protocol Tests ////
////////////////////////////////////////////////////////////////
func TestProtocolFunctions(t *testing.T) {
}
func TestProtocolInput(t *testing.T) {
var idxA, idxB = p.Input(true, false)
var shareA, shareB = p.A.vals[idxA] != p.B.vals[idxA], p.A.vals[idxB] != p.B.vals[idxB]
if shareA != true || shareB != false {
t.Fatal("Shares has wrong values")
}
if len(p.A.vals) != len(p.B.vals) {
t.Fatal("Party stack size is not equal after function call")
}
}
func TestProtocolXORC(t *testing.T) {
var idxTrue, idxFalse = p.Input(true, false)
//True XOR True
var idxTT = p.XORC(idxTrue, true)
var idxTF = p.XORC(idxTrue, false)
var idxFT = p.XORC(idxFalse, true)
var idxFF = p.XORC(idxFalse, false)
var resTT, resTF, resFT, resFF = p.A.vals[idxTT] != p.B.vals[idxTT],
p.A.vals[idxTF] != p.B.vals[idxTF],
p.A.vals[idxFT] != p.B.vals[idxFT],
p.A.vals[idxFF] != p.B.vals[idxFF]
if resTT != false || resTF != true || resFT != true || resFF != false {
t.Fatal("Shares has wrong values")
}
if len(p.A.vals) != len(p.B.vals) {
t.Fatal("Party stack size is not equal after function call")
}
}
func TestProtocolANDC(t *testing.T) {
var idxTrue, idxFalse = p.Input(true, false)
//True XOR True
var idxTT = p.ANDC(idxTrue, true)
var idxTF = p.ANDC(idxTrue, false)
var idxFT = p.ANDC(idxFalse, true)
var idxFF = p.ANDC(idxFalse, false)
var resTT, resTF, resFT, resFF = p.A.vals[idxTT] != p.B.vals[idxTT],
p.A.vals[idxTF] != p.B.vals[idxTF],
p.A.vals[idxFT] != p.B.vals[idxFT],
p.A.vals[idxFF] != p.B.vals[idxFF]
if resTT != true || resTF != false || resFT != false || resFF != false {
t.Fatal("Shares has wrong values")
}
if len(p.A.vals) != len(p.B.vals) {
t.Fatal("Party stack size is not equal after function call")
}
}
func TestProtocolXOR2W(t *testing.T) {
var idxTrue, idxFalse = p.Input(true, false)
//True XOR True
var idxTT = p.XOR2W(idxTrue, idxTrue)
var idxTF = p.XOR2W(idxTrue, idxFalse)
var idxFT = p.XOR2W(idxFalse, idxTrue)
var idxFF = p.XOR2W(idxFalse, idxFalse)
var resTT, resTF, resFT, resFF = p.A.vals[idxTT] != p.B.vals[idxTT],
p.A.vals[idxTF] != p.B.vals[idxTF],
p.A.vals[idxFT] != p.B.vals[idxFT],
p.A.vals[idxFF] != p.B.vals[idxFF]
if resTT != false || resTF != true || resFT != true || resFF != false {
t.Fatal("Shares has wrong values")
}
if len(p.A.vals) != len(p.B.vals) {
t.Fatal("Party stack size is not equal after function call")
}
}
func TestProtocolNOT(t *testing.T) {
var idxTrue, idxFalse = p.Input(true, false)
var idxT = p.NOT(idxTrue)
var idxF = p.NOT(idxFalse)
var resT, resF = p.A.vals[idxT] != p.B.vals[idxF],
p.A.vals[idxF] != p.B.vals[idxF]
if resT != false || resF != true {
t.Fatal("Shares has wrong values")
}
if len(p.A.vals) != len(p.B.vals) {
t.Fatal("Party stack size is not equal after function call")
}
}
func TestProtocolOUTPUT(t *testing.T) {
var idxTrue, idxFalse = p.Input(true, false)
for i := 0; i < 8; i++ {
var params = Int2Bools(i, 3)
var idx = 0
if params[0] {
idx = idxTrue
} else {
idx = idxFalse
}
var 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")
}
}
}
}
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