Commit 2413f8f2 authored by Thomas Hoffmann's avatar Thomas Hoffmann
Browse files

Kinda works, but not really

parent a4db733f
package main
import (
"fmt"
"math"
"math/rand"
)
......@@ -52,9 +53,8 @@ func mod(a, n int) int {
}
type Party struct {
rand *rand.Rand
inLayer []bool
outLayer []bool
rand *rand.Rand
vals []bool
in chan bool
out chan bool
......@@ -62,65 +62,63 @@ type Party struct {
func InitParty(rand *rand.Rand, in, out chan bool) *Party {
return &Party{
rand: rand,
inLayer: make([]bool, 0),
outLayer: make([]bool, 0),
in: in,
out: out,
rand: rand,
vals: make([]bool, 0),
in: in,
out: out,
}
}
func (a *Party) Input(x bool) int {
xb := a.rand.Intn(2) == 1
xa := x != xb // x XOR xb
a.inLayer = append(a.inLayer, xa)
a.vals = append(a.vals, xa)
a.out <- xb //Send to other party
return len(a.inLayer) - 1
return len(a.vals) - 1
}
func (a *Party) Receive() int {
a.inLayer = append(a.inLayer, <-a.in)
return len(a.inLayer) - 1
a.vals = append(a.vals, <-a.in)
return len(a.vals) - 1
}
func (a *Party) Send(wire int) {
a.out <- a.inLayer[wire]
a.out <- a.vals[wire]
}
func (a *Party) Output() bool {
//TODO
return true
}
func (a *Party) NextLayer() {
a.inLayer = a.outLayer
a.outLayer = make([]bool, 0)
func (a *Party) Combine(xwire, ywire int) int {
a.vals = append(a.vals, a.vals[xwire] && a.vals[ywire])
return len(a.vals) - 1
}
func (a *Party) XORC(xwire int, c bool, mode bool) int {
var xa = a.inLayer[xwire]
var xa = a.vals[xwire]
if mode { //Only one party is supposed to do the XOR
a.outLayer = append(a.inLayer, xa != c)
a.vals = append(a.vals, xa != c)
} else {
a.outLayer = append(a.inLayer, xa)
a.vals = append(a.vals, xa)
}
return len(a.outLayer) - 1
return len(a.vals) - 1
}
func (a *Party) ANDC(xwire int, c bool) int {
var xa = a.inLayer[xwire]
a.outLayer = append(a.inLayer, xa && c)
return len(a.outLayer) - 1
var xa = a.vals[xwire]
a.vals = append(a.vals, xa && c)
return len(a.vals) - 1
}
func (a *Party) XOR2W(xwire, ywire int) int {
var xa = a.inLayer[xwire]
var xb = a.outLayer[ywire]
var xa, xb = a.vals[xwire], a.vals[ywire]
a.vals = append(a.vals, xa != xb)
return len(a.vals) - 1
}
a.outLayer = append(a.outLayer, xa != xb)
return len(a.outLayer) - 1
type Protocol struct {
rand *rand.Rand
A, B *Party
}
func AND2W(rand *rand.Rand, A, B Party, xwire, ywire int) int {
func (P *Protocol) AND(xwire, ywire int) int {
var rand, A, B = P.rand, P.A, P.B
//1a. Generate [u], [v] and [w]
u := rand.Intn(2) == 1
ua := rand.Intn(2) == 1
......@@ -132,35 +130,173 @@ func AND2W(rand *rand.Rand, A, B Party, xwire, ywire int) int {
wa := rand.Intn(2) == 1
wb := w != wa
//1b send shares to parties
var index = A.Input(ua)
A.Input(va)
A.Input(wa)
B.Input(ub)
B.Input(vb)
B.Input(wb)
return index
//2 compute [d] = [X]
//1b. send shares to parties
A.in <- ua
B.in <- ub
var idx_u, _ = A.Receive(), B.Receive()
A.in <- va
B.in <- vb
var idx_v, _ = A.Receive(), B.Receive()
A.in <- wa
B.in <- wb
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)
//3. compute [e] = [y] XOR [v]
var idx_e1, _ = 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()
//5. Open e
A.Send(idx_e1)
B.Send(idx_e1)
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)
//6b. Compute [z]-parts
var idx_z1, _ = A.ANDC(xwire, A.vals[idx_e]), B.ANDC(xwire, B.vals[idx_e])
var idx_z2, _ = A.ANDC(ywire, A.vals[idx_d]), B.ANDC(ywire, B.vals[idx_d])
var 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.XOR2W(idx_z5, idx_z3), B.XOR2W(idx_z5, idx_z3)
return idx_z
}
func (P *Protocol) Input(x, y bool) (int, int) {
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.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)
return idx
}
func (P *Protocol) XOR2W(xwire, ywire int) int {
var idx, _ = P.A.XOR2W(xwire, ywire), P.B.XOR2W(xwire, ywire)
return idx
}
func (P *Protocol) NOT(xwire int) int {
var idx, _ = P.A.XORC(xwire, true, true), P.B.XORC(xwire, true, false)
return idx
}
func (P *Protocol) Output(wire int, A_Learns, B_Learns bool) []bool {
var A, B = P.A, P.B
var output = make([]bool, 0)
if A_Learns {
B.Send(wire)
}
if B_Learns {
A.Send(wire)
}
if A_Learns {
var idx_A = A.Receive()
var idx_outA = A.XOR2W(wire, idx_A)
output = append(output, A.vals[idx_outA])
}
if B_Learns {
var idx_B = A.Receive()
var idx_outB = A.XOR2W(wire, idx_B)
output = append(output, B.vals[idx_outB])
}
return output
}
func RunProtocol(seed int64, x, y bool) bool {
rand := rand.New(rand.NewSource(seed))
a2b := make(chan bool, 1) //Directional non-blocking channels
b2a := make(chan bool, 1)
func InitProtocol(seed int64) *Protocol {
var rand = rand.New(rand.NewSource(seed))
var a2b = make(chan bool, 1) //Directional non-blocking channels
var b2a = make(chan bool, 1)
return &Protocol{
rand: rand,
A: InitParty(rand, b2a, a2b),
B: InitParty(rand, a2b, b2a),
}
}
//Init parties
alice := InitParty(rand, b2a, a2b)
bob := InitParty(rand, a2b, b2a)
func (P *Protocol) RunProtocol(x, y []bool) bool {
//alice := P.A
//bob := P.B
if len(x) != len(y) {
return false
}
//Input round
alice.Input(x)
bob.Input(y)
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])
//
//Compute f(x,y) = (x_sign OR NOT y_sign) AND
// (x_A OR NOT y_A) AND
// (x_B OR NOT y_B)
return true
//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
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
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
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)
return output[0]
}
func main() {
var p = InitProtocol(1)
x, y := []bool{false, false, false}, []bool{true, true, true}
z := p.RunProtocol(x, y)
if z == BloodTable[BloodType_ABp][BloodType_Bp] {
fmt.Println("Protocol succeded")
fmt.Println(z)
} else {
fmt.Println("Protocol failed")
}
}
// 0 - 0 - 1
// 0 - 1 - 0
// 1 - 0 - 1
// 1 - 1 - 1
// 0 - 0 - 1
// 0 - 1 - 0
// 1 - 0 - 1
// 1 - 1 - 1
// NOT (NOT x AND Y)
// 0 - 0 - 0
// 0 - 1 - 1
// 1 - 0 - 0
// 1 - 1 - 0
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