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

something

parent 3efc310f
......@@ -36,81 +36,110 @@ var BloodTable = [][]bool{
}
// EncodeBloodType return x as a slice containing the binary representation.
func EncodeBloodType(x, bits int) []bool {
func EncodeBloodType(x, n int) []bool {
output := make([]bool, 0)
tmp := 0
for i := bits - 1; i >= 0; i-- {
for i := n - 1; i >= 0; i-- {
tmp = int(math.Exp2(float64(i)))
output = append(output, (x&tmp)/tmp == 1)
}
// Reverse: Needed in protocol
for i := 0; i < n/2; i++ {
temp := output[i]
output[i] = output[n-i-1]
output[n-i-1] = temp
}
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 {
stack []bool
wires []bool
in chan bool
out chan bool
}
func InitParty(in, out chan bool) *Party {
func NewParty(bloodType int, secrets []bool) *Party {
return &Party{
stack: make([]bool, 0),
in: in,
out: out,
wires: make([]bool, 0),
}
}
func (a *Party) Input(x bool) int {
// Push v to the wires and return the index of the pushed value.
func (p *Party) Push(v bool) int {
p.wires = append(p.wires, v)
return len(p.wires) - 1
}
// Share samples a random value xb, computes xa = x XOR xb and sends xb to the
// other party.
func (p *Party) Share(x bool) int {
xb := rand.Intn(2) == 1
xa := x != xb // x XOR xb
a.stack = append(a.stack, xa)
a.out <- xb //Send to other party
return len(a.stack) - 1
p.out <- xb //Send to other party
return p.Push(xa)
}
func (a *Party) Receive() int {
a.stack = append(a.stack, <-a.in)
return len(a.stack) - 1
func (p *Party) Receive() int {
p.wires = append(p.wires, <-p.in)
return len(p.wires) - 1
}
func (a *Party) Send(wire int) {
a.out <- a.stack[wire]
a.out <- a.wires[wire]
}
func (a *Party) Combine(xwire, ywire int) int {
a.stack = append(a.stack, a.stack[xwire] && a.stack[ywire])
return len(a.stack) - 1
a.wires = append(a.wires, a.wires[xwire] && a.wires[ywire])
return len(a.wires) - 1
}
func (a *Party) XORC(xwire int, c bool, mode bool) int {
var xa = a.stack[xwire]
var xa = a.wires[xwire]
if mode { //Only one party is supposed to do the XOR
a.stack = append(a.stack, xa != c)
a.wires = append(a.wires, xa != c)
} else {
a.stack = append(a.stack, xa)
a.wires = append(a.wires, xa)
}
return len(a.stack) - 1
return len(a.wires) - 1
}
func (a *Party) ANDC(xwire int, c bool) int {
var xa = a.stack[xwire]
a.stack = append(a.stack, xa && c)
return len(a.stack) - 1
var xa = a.wires[xwire]
a.wires = append(a.wires, xa && c)
return len(a.wires) - 1
}
func (a *Party) XOR2W(xwire, ywire int) int {
var xa, xb = a.stack[xwire], a.stack[ywire]
a.stack = append(a.stack, xa != xb)
return len(a.stack) - 1
var xa, xb = a.wires[xwire], a.wires[ywire]
a.wires = append(a.wires, xa != xb)
return len(a.wires) - 1
}
type Protocol struct {
A *Party
B *Party
Alice *Party
Bob *Party
}
func (P *Protocol) AND(xwire, ywire int) int {
A, B := P.A, P.B
A, B := P.Alice, P.Bob
//1a. Generate [u], [v] and [w]
u := rand.Intn(2) == 1
ua := rand.Intn(2) == 1
......@@ -154,8 +183,8 @@ func (P *Protocol) AND(xwire, ywire int) int {
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.stack[idx_e]), B.ANDC(xwire, B.stack[idx_e])
var idx_z2, _ = A.ANDC(ywire, A.stack[idx_d]), B.ANDC(ywire, B.stack[idx_d])
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)
//6c. Compute [z]
......@@ -165,37 +194,38 @@ func (P *Protocol) AND(xwire, ywire int) int {
return idx_z
}
// Input the x bit to
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()
var idx1 = P.Alice.Share(x)
P.Bob.Receive()
P.Bob.Share(y)
var idx2 = P.Alice.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)
var idx, _ = P.Alice.XORC(wire, c, true), P.Bob.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)
var idx, _ = P.Alice.ANDC(wire, c), P.Bob.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)
var idx, _ = P.Alice.XOR2W(xwire, ywire), P.Bob.XOR2W(xwire, ywire)
return idx
}
func (P *Protocol) NOT(xwire int) int {
i := P.A.XORC(xwire, true, true)
_ = P.B.XORC(xwire, true, false)
i := P.Alice.XORC(xwire, true, true)
_ = P.Bob.XORC(xwire, true, false)
return i
}
func (P *Protocol) Output(wire int, A_Learns, B_Learns bool) []bool {
var A, B = P.A, P.B
var A, B = P.Alice, P.Bob
var output = make([]bool, 0)
if A_Learns {
B.Send(wire)
......@@ -206,22 +236,22 @@ func (P *Protocol) Output(wire int, A_Learns, B_Learns bool) []bool {
if A_Learns {
var idx_A = A.Receive()
var idx_outA = A.XOR2W(wire, idx_A)
output = append(output, A.stack[idx_outA])
output = append(output, A.wires[idx_outA])
}
if B_Learns {
var idx_B = A.Receive()
var idx_outB = A.XOR2W(wire, idx_B)
output = append(output, B.stack[idx_outB])
output = append(output, B.wires[idx_outB])
}
return output
}
func NewProtocol() *Protocol {
var a2b = make(chan bool, 1) //Directional non-blocking channels
var b2a = make(chan bool, 1)
// var a2b = make(chan bool, 1) //Directional non-blocking channels
// var b2a = make(chan bool, 1)
return &Protocol{
A: InitParty(b2a, a2b),
B: InitParty(a2b, b2a),
// Alice: NewParty(b2a, a2b),
// Bob: NewParty(a2b, b2a),
}
}
......@@ -234,9 +264,11 @@ func NewProtocol() *Protocol {
// (x_B OR NOT y_B)
//
// The inputs are the binary encoding of the blood types, with x being Alice's
// blood type and y being Bob's blood type. The encodings must have length 3,
// where index 2 indiciates blood sign, index 1 indicates if bloods has A and
// index 0 indicates if blood have B.
// blood type and y being Bob's blood type. The encodings must have the form
//
// index 0: sign of blood type
// 1: blood as A
// 2: blood as B
//
func (P *Protocol) Run(x, y []bool) (bool, error) {
if lenX := len(x); lenX != 3 {
......@@ -246,7 +278,9 @@ func (P *Protocol) Run(x, y []bool) (bool, error) {
return false, fmt.Errorf("length of y-encoding must be 3, was %d", lenY)
}
//Input round
// 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])
......@@ -280,6 +314,11 @@ func RunProtocol(x, y int) (bool, error) {
encodingX := EncodeBloodType(x, 3)
encodingY := EncodeBloodType(y, 3)
dealer := NewDealer()
A := NewParty(x, dealer.DealA())
B := NewParty(y, dealer.DealB())
return protocol.Run(encodingX, encodingY)
}
......
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