main.go 1.56 KB
Newer Older
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main

import (
	"crycomp/internal/blood"
	"fmt"
	"math/big"

	"golang.org/x/crypto/openpgp/elgamal"
)

type Encryption struct {
	C1, C2 *big.Int
}

// Party represents one of the parties in  the protocol. They contain a slice
// of all known wires and channels for communication with the other party.
type Party struct {
	keyChan chan []*elgamal.PublicKey
	encChan chan []*Encryption
	x       int
}

// NewParty returns a new party that communicates on the specified channels.
func NewParty(x int, keyChan chan []*elgamal.PublicKey, encChan chan []*Encryption) *Party {
	return &Party{
		x:       x,
		keyChan: keyChan,
		encChan: encChan,
	}
}

func (p *Party) Listen() {
	_, ok := <-p.keyChan
	if !ok {
		panic("key channel closed unexpectedly")
	}

	// TODO enc column y

	var encs []*Encryption
	p.encChan <- encs
}

func (p *Party) ObliviousTransfer() bool {
	return false // TODO
}

type Protocol struct {
	Receiver, Sender *Party
}

func NewProtocol(x, y int) *Protocol {
	return nil // TODO
}

// RunProtocol runs the protocol between receiving blood type x and donor blood
// type y.
func RunProtocol(x, y int) bool {
	p := NewProtocol(x, y)

	go func() {
		p.Sender.Listen()
	}()
	return p.Receiver.ObliviousTransfer()
}

func main() {
	bloodA, bloodB := blood.Type_ABn, blood.Type_ABp
	z := RunProtocol(bloodA, bloodB)

	if z == blood.Table[bloodA][bloodB] {
		fmt.Println("Protocol succeded")
	} else {
		fmt.Printf("Protocol failed, output was %t, but should be %t", z, blood.Table[bloodA][bloodB])
	}
}