main.go 2.33 KB
Newer Older
Thomas Hoffmann's avatar
Thomas Hoffmann committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package main

import (
	"crycomp/internal/blood"
	"crycomp/internal/crypto/util"
	"crycomp/internal/crypto/homomorphic"
	"fmt"
	"math/big"
)

func main() {
	bloodA, bloodB := blood.Type_ABn, blood.Type_ABn
	z, err := RunProtocol(bloodA, bloodB)
	if err != nil {
		fmt.Println("Protocol failed with error:", err)
	} else 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])
	}
}

type Party struct {
Thomas Hoffmann's avatar
Thomas Hoffmann committed
24
	conn chan big.Int
Thomas Hoffmann's avatar
Thomas Hoffmann committed
25
26
27
28
29
	x int
	sKey *big.Int
	HEProt homomorphic.Protocol
}

Thomas Hoffmann's avatar
Thomas Hoffmann committed
30
func NewParty(conn chan big.Int, p homomorphic.Protocol) (*Party, error) {
Thomas Hoffmann's avatar
Thomas Hoffmann committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
	return &Party{
		conn: conn,
		HEProt : p,
		}, nil
}

func (alice *Party) RunAlice(x int) (result bool, err error) {
	xval := make([]int, 3)
	//Negate values
	for i, val := range util.Int2Bools(x,3) {
		if !val {xval[i] = 1}
	}

	// Send encoded input ints to bob
	for i := 0 ; i < 3 ; i++ {
Thomas Hoffmann's avatar
Thomas Hoffmann committed
46
		alice.conn <- *alice.HEProt.Encode(xval[i])
Thomas Hoffmann's avatar
Thomas Hoffmann committed
47
48
49
	}

	//Receive and decode result from bob
Thomas Hoffmann's avatar
Thomas Hoffmann committed
50
	result = alice.HEProt.Decode(<-alice.conn, *alice.sKey) == 1 //Convert 1 -> true
Thomas Hoffmann's avatar
Thomas Hoffmann committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
	return
}

func (bob *Party) RunBob(x int) (err error) {
	c_y := make([]*big.Int,3)
	for i, val := range util.Int2Bools(x,3) {
		bit := 0
		if val {bit = 1}
		c_y[i] = bob.HEProt.Encode(bit)
	}

	//Receive ciphertexts from alice
	c_x := make([]*big.Int, 3)
	for i := range c_x {
Thomas Hoffmann's avatar
Thomas Hoffmann committed
65
66
		val := <-bob.conn
		c_x[i] = &val
Thomas Hoffmann's avatar
Thomas Hoffmann committed
67
68
	}
	
Thomas Hoffmann's avatar
Thomas Hoffmann committed
69
	bob.conn <- *bob.HEProt.Eval(c_x,c_y)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
70
71
72
73
74
75
76
77
78
79
	return nil
}



type Protocol struct {
	A, B *Party
}

func NewProtocol() (p *Protocol, err error) {
Thomas Hoffmann's avatar
Thomas Hoffmann committed
80
	conn := make(chan big.Int)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
	sKey, HEp, err := homomorphic.NewProtocol(3,27)
	if err != nil {
		return
	}
	A, err := NewParty(conn, HEp)
	if err != nil {
		return nil, err
	}
	A.sKey = sKey

	B, err := NewParty(conn, HEp)
	if err != nil {
		return nil, err
	}
	p = &Protocol{
		A: A,
		B: B,
	}
	return p, nil
}

// RunProtocol runs the protocol between receiving blood type x and donor blood
// type y.
func RunProtocol(x, y int) (z bool, err error) {
	p, err := NewProtocol()
	if err != nil {
		return
	}

	// Concurrently run Bob
	go func() {
		err := p.B.RunBob(y)
		if err != nil {
			panic(err) // TODO do not panic
		}
	}()

	z, err = p.A.RunAlice(x)
	return
}