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

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

parents 68974499 4d19e278
......@@ -102,7 +102,7 @@ func (p *Party) XORC(xwire int, c bool, mode bool) int {
if mode { //Only one party is supposed to do the XOR
return p.Push(p.wires[xwire] != c)
} else {
return xwire
return p.Push(p.wires[xwire])
}
}
func (p *Party) ANDC(xwire int, c bool) int {
......@@ -119,8 +119,8 @@ type Protocol struct {
}
func NewProtocol() *Protocol {
var a2b = make(chan bool, 1) //Directional non-blocking channels
var b2a = make(chan bool, 1)
a2b := make(chan bool, 1) //Directional non-blocking channels
b2a := make(chan bool, 1)
return &Protocol{
A: NewParty(b2a, a2b),
B: NewParty(a2b, b2a),
......@@ -143,67 +143,67 @@ func (P *Protocol) AND(xwire, ywire int) int {
//1b. send shares to parties
A.in <- ua
B.in <- ub
var idx_u, _ = A.Receive(), B.Receive()
idx_u, _ := A.Receive(), B.Receive()
A.in <- va
B.in <- vb
var idx_v, _ = A.Receive(), B.Receive()
idx_v, _ := A.Receive(), B.Receive()
A.in <- wa
B.in <- wb
var idx_w, _ = A.Receive(), B.Receive()
idx_w, _ := A.Receive(), B.Receive()
//2 compute [d] = [x] XOR [u]
var idx_d1 = P.XOR2W(xwire, idx_u) //A.XOR2W(xwire, idx_u), B.XOR2W(xwire, idx_u)
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 = P.XOR2W(ywire, idx_v) //A.XOR2W(ywire, idx_v), B.XOR2W(ywire, idx_v)
idx_e1 := P.XOR2W(ywire, idx_v) //A.XOR2W(ywire, idx_v), B.XOR2W(ywire, idx_v)
//4. Open d
A.Send(idx_d1)
B.Send(idx_d1)
var idx_d2, _ = A.Receive(), B.Receive()
idx_d2, _ := A.Receive(), B.Receive()
//5. Open e
A.Send(idx_e1)
B.Send(idx_e1)
var idx_e2, _ = A.Receive(), B.Receive()
idx_e2, _ := A.Receive(), B.Receive()
//6a. Compute d and e
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)
idx_d := P.XOR2W(idx_d1, idx_d2) //A.XOR2W(idx_d1, idx_d2), B.XOR2W(idx_d1, idx_d2)
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])
var idx_z2, _ = A.ANDC(ywire, A.wires[idx_d]), B.ANDC(ywire, B.wires[idx_d])
var idx_z3, _ = A.Combine(idx_e, idx_d), B.Combine(idx_e, idx_d)
idx_z1, _ := A.ANDC(xwire, A.wires[idx_e]), B.ANDC(xwire, B.wires[idx_e])
idx_z2, _ := A.ANDC(ywire, A.wires[idx_d]), B.ANDC(ywire, B.wires[idx_d])
idx_z3, _ := A.Combine(idx_e, idx_d), B.Combine(idx_e, idx_d)
//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.XORC(idx_z5, A.wires[idx_z3], true), B.XORC(idx_z5, B.wires[idx_z3], false)
idx_z4, _ := A.XOR2W(idx_w, idx_z1), B.XOR2W(idx_w, idx_z1)
idx_z5, _ := A.XOR2W(idx_z4, idx_z2), B.XOR2W(idx_z4, idx_z2)
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.A.Input(x)
idx1 := P.A.Input(x)
P.B.Receive()
P.B.Input(y)
var idx2 = P.A.Receive()
idx2 := P.A.Receive()
return idx1, idx2
}
func (P *Protocol) XORC(wire int, c bool) int {
var idx, _ = P.A.XORC(wire, c, true), P.B.XORC(wire, c, false)
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.A.ANDC(wire, c), P.B.ANDC(wire, c)
idx, _ := P.A.ANDC(wire, c), P.B.ANDC(wire, c)
return idx
}
func (P *Protocol) XOR2W(xwire, ywire int) int {
var idx, _ = P.A.XOR2W(xwire, ywire), P.B.XOR2W(xwire, ywire)
idx, _ := P.A.XOR2W(xwire, ywire), P.B.XOR2W(xwire, ywire)
return idx
}
......@@ -216,8 +216,8 @@ func (P *Protocol) NOT(xwire int) int {
}
func (P *Protocol) Output(wire int, A_Learns, B_Learns bool) []bool {
var A, B = P.A, P.B
var output = make([]bool, 0)
A, B := P.A, P.B
output := make([]bool, 0)
if A_Learns {
B.Send(wire)
}
......@@ -225,13 +225,13 @@ func (P *Protocol) Output(wire int, A_Learns, B_Learns bool) []bool {
A.Send(wire)
}
if A_Learns {
var idx_A = A.Receive()
var idx_outA = A.XOR2W(wire, idx_A)
idx_A := A.Receive()
idx_outA := A.XOR2W(wire, idx_A)
output = append(output, A.wires[idx_outA])
}
if B_Learns {
var idx_B = A.Receive()
var idx_outB = A.XOR2W(wire, idx_B)
idx_B := B.Receive()
idx_outB := B.XOR2W(wire, idx_B)
output = append(output, B.wires[idx_outB])
}
return output
......@@ -259,30 +259,37 @@ 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
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 = 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_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_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)
var idx_f = P.AND(idx_f1, idx_f2)
idx_f = P.AND(idx_f, idx_f3)
var output = P.Output(idx_f, true, false)
// Input Round
idx_xS, idx_yS := P.Input(x[2], y[2])
idx_xA, idx_yA := P.Input(x[1], y[1])
idx_xB, idx_yB := P.Input(x[0], y[0])
// 3 Rounds to compute (x OR not y)
// Round 1: (OR Gate) Compute first NOT
idx_1a := P.NOT(idx_xS)
idx_1b := P.NOT(idx_xA)
idx_1c := P.NOT(idx_xB)
// Round 2: (OR Gate) Compute AND
idx_2a := P.AND(idx_1a, idx_yS)
idx_2b := P.AND(idx_1b, idx_yA)
idx_2c := P.AND(idx_1c, idx_yB)
// Round 3: (OR Gate) Compute last NOT
idx_3a := P.NOT(idx_2a)
idx_3b := P.NOT(idx_2b)
idx_3c := P.NOT(idx_2c)
// 2 Rounds to compute ANDs (depend on eachother)
// Round 4: Compute first AND
idx_4 := P.AND(idx_3a, idx_3b)
// Round 5: Compute second AND
idx_f := P.AND(idx_4, idx_3c)
// Output Round
output := P.Output(idx_f, true, false)
return output[0], nil
}
......
......@@ -29,7 +29,7 @@ func TestPartyWires(t *testing.T) {
}
func TestPartyInput(t *testing.T) {
A := NewProtocol().A
A := NewParty(nil, make(chan bool, 10))
idx := A.Input(true)
if len(A.wires) != 1 {
t.Errorf("Expected len(A.wires) == %d, was %d", 1, idx)
......@@ -40,10 +40,12 @@ func TestPartyInput(t *testing.T) {
}
func TestPartyReceive(t *testing.T) {
p := NewProtocol()
A, B := p.A, p.B
A := NewParty(nil, make(chan bool, 1))
B := NewParty(A.out, nil)
A.Input(true)
idx := B.Receive()
if len(B.wires) != 1 {
t.Errorf("Expected len(A.wires) == %d, was %d", 1, idx)
}
......@@ -72,7 +74,7 @@ func TestPartyCombine(t *testing.T) {
}
func TestPartyXORC(t *testing.T) {
A := NewProtocol().A
A := NewParty(nil, make(chan bool, 10))
A.Input(true)
idx1 := A.XORC(0, true, true)
......@@ -84,16 +86,16 @@ func TestPartyXORC(t *testing.T) {
}
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 len(A.wires) != 3 {
t.Errorf("Expected len(A.wires) == %d, was %d", 3, len(A.wires))
}
if idx2 != 0 {
t.Errorf("Expected idx2 == %d, was %d", 0, idx2)
if idx2 != 2 {
t.Errorf("Expected idx2 == %d, was %d", 2, idx2)
}
}
func TestPartyANDC(t *testing.T) {
A := NewProtocol().A
A := NewParty(nil, make(chan bool, 10))
A.Input(true)
idx := A.ANDC(0, true)
......@@ -106,11 +108,9 @@ func TestPartyANDC(t *testing.T) {
}
func TestPartyXOR(t *testing.T) {
p := NewProtocol()
A, B := p.A, p.B
A := NewParty(nil, make(chan bool, 10))
A.Input(true)
B.Receive()
A.Input(false)
idx := A.XOR2W(0, 1)
......@@ -143,10 +143,6 @@ func TestStackAND(t *testing.T) {
//// Protocol Tests ////
////////////////////////////////////////////////////////////////
func TestProtocolFunctions(t *testing.T) {
}
func TestProtocolInput(t *testing.T) {
p := NewProtocol()
idxA, idxB := p.Input(true, false)
......
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