Commit a4db733f authored by Thomas Hoffmann's avatar Thomas Hoffmann
Browse files

1st attempt on 3rd assignment

parent dbe3ad49
package main
import (
"math"
"math/rand"
)
// Enumeration of blood types. The 'n'-suffix means negative and 'p'-suffix means positive.
const (
BloodType_On = 0
BloodType_Op = 1
BloodType_An = 2
BloodType_Ap = 3
BloodType_Bn = 4
BloodType_Bp = 5
BloodType_ABn = 6
BloodType_ABp = 7
)
// BloodTable contains the blood type compatibility truth table. Rows are the
// recipient's blood type and the columns are the donor's blood type.
//
// The table contains bools, since we compute a boolean function. Duh!
var BloodTable = [][]bool{
{true, false, false, false, false, false, false, false},
{true, true, false, false, false, false, false, false},
{true, false, true, false, false, false, false, false},
{true, true, true, true, false, false, false, false},
{true, false, false, false, true, false, false, false},
{true, true, false, false, true, true, false, false},
{true, false, true, false, true, false, true, false},
{true, true, true, true, true, true, true, true},
}
// Params contains the global protocol paramteres that are shared between Dealer, Alice and Bob.
type Params struct {
n int
nPow int // pow(2, n)
}
// NewParams returns a new Params struct with n and precomputed 2^n (pow(2, n)).
func NewParams(n int) *Params {
return &Params{
n: n,
nPow: int(math.Pow(2, float64(n))),
}
}
// mod computes the modulo a%n but handles negative arguments correctly.
func mod(a, n int) int {
return (a%n + n) % n
}
type Party struct {
rand *rand.Rand
inLayer []bool
outLayer []bool
in chan bool
out chan bool
}
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,
}
}
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.out <- xb //Send to other party
return len(a.inLayer) - 1
}
func (a *Party) Receive() int {
a.inLayer = append(a.inLayer, <-a.in)
return len(a.inLayer) - 1
}
func (a *Party) Send(wire int) {
a.out <- a.inLayer[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) XORC(xwire int, c bool, mode bool) int {
var xa = a.inLayer[xwire]
if mode { //Only one party is supposed to do the XOR
a.outLayer = append(a.inLayer, xa != c)
} else {
a.outLayer = append(a.inLayer, xa)
}
return len(a.outLayer) - 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
}
func (a *Party) XOR2W(xwire, ywire int) int {
var xa = a.inLayer[xwire]
var xb = a.outLayer[ywire]
a.outLayer = append(a.outLayer, xa != xb)
return len(a.outLayer) - 1
}
func AND2W(rand *rand.Rand, A, B Party, xwire, ywire int) int {
//1a. Generate [u], [v] and [w]
u := rand.Intn(2) == 1
ua := rand.Intn(2) == 1
ub := u != ua // x XOR xb
v := rand.Intn(2) == 1
va := rand.Intn(2) == 1
vb := v != va // x XOR xb
w := u && v
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]
}
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)
//Init parties
alice := InitParty(rand, b2a, a2b)
bob := InitParty(rand, a2b, b2a)
//Input round
alice.Input(x)
bob.Input(y)
//
return true
}
func main() {
}
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