circuit.go 3.69 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
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
77
78
79
80
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package garbled

import (
	"crycomp/internal/util"
	"crypto/rand"
	"crypto"
	"bytes"
	m "math/rand"
	_ "crypto/sha256"
)

type Table struct {
	data []byte
	wires, numValues, numBytes int
}

func NewTable(wires, numValues, k int) *Table {
	t := Table{
		data : make([]byte, wires*numValues*k/8),
		wires : wires,
		numValues : numValues,
		numBytes : k/8,
	}
	return &t
}

func (t *Table) SetValue(data []byte, i, s int) {
	valStart := i*t.numBytes+s*t.numBytes
	copy(t.data[valStart : valStart+t.numBytes], data)
}

func (t *Table) GetValue(i,s int) []byte {
	valStart := i*t.numBytes+s*t.numBytes
	return t.data[valStart : valStart+t.numBytes]
}

func (t *Table) SetRow(data [][]byte, i int) {
	for j, val := range data {
		t.SetValue(val,i,j)
	}
}

func (t *Table) GetRow(i int) [][]byte {
	values := make([][]byte, 0)
	for j := 0; j < t.numValues; j++ {
		values = append(values,t.GetValue(i,j))
	}
	return values
}

func (t *Table) GetKFirstValues(numInputWire int) [][][]byte {
	values := make([][][]byte, 0)
	for i := 0; i < numInputWire; i++ {
		values = append(values,t.GetRow(i))
	}
	return values
}

func (t *Table) RandomizeTable() {
	rand.Read(t.data)
}

type GCircuit struct {
	F *Table
	E [][][]byte
	D [][]byte
}

type Gate struct {
	Li, Ri int
	i int
	truthfunc func(a,b int) int
}

func newGate(Li, Ri, i int, f func(a,b int) int) (g *Gate) {
	g.Li = Li
	g.Ri = Ri
	g.i = i
	g.truthfunc = f
	return
}

func NewGarbBloodCircuit() (circuit *GCircuit) {
	numInput := 6
	numWires := 11
	// (1)
	K := NewTable(numWires,2,128)
	K.RandomizeTable()

	// (2)
	C := NewTable(numWires-numInput,4,256)
	for i := 0; i < 3; i++ { //1st layer (consisting of not's and or's )
		//INput wires order: xs, xa, xb, ys, ya, yb (hopefully)
		C.SetRow(GBC(K, i,i+3, i+numInput, ORGateWithNot, G),i)
	}
	C.SetRow(GBC(K,numInput,numInput+1,numInput+3, ANDGate,G),4)
	C.SetRow(GBC(K,numInput+3,numInput+2,numInput+4, ANDGate, G),5)

	return &GCircuit{
		F : C,
		D : K.GetRow(numWires),
		E : K.GetKFirstValues(numInput),
	}
}
//Creates a row (C_0^i, C_1^i, C_2^i ,C_3^i) for the garbled table, where 
func GBC (K *Table, Li,Ri,i int, c func(a,b int) int, g func(A,B []byte, i int) []byte) (CRow [][]byte) {
	CRow = make([][]byte, 0)
	CVals := make([][]byte, 4) // 4 x 256 bits

	// (a)
	for a := range []int{0,1} {
		for b := range []int{0,1} {
			left := g(K.GetValue(Li,a),K.GetValue(Ri,b),i)
			right := make([]byte,16)
			copy(right, K.GetValue(i,c(a,b)))
			right = append(right, make([]byte,16)...)
			CVals[a*2+b] = util.XOR(left, right)
		}
	}
	// (b)
	//TODO: Fix permutation
	for i:= range m.Perm(4) { //Generate random order
		CRow = append(CRow, CVals[i])
	}
	return
}

func G (A,B []byte, i int) []byte {
	hash := crypto.SHA256.New()
	hash.Write(A)
	hash.Write(append(B,byte(i)))
	return hash.Sum(nil)
}

func ORGateWithNot(a,b int) int {
	if ((1-a)+b) == 0 {
		return 0
	} else {
		return 1
	}
}

func ANDGate(a,b int) int {
	return a*b
}

func Enc(e [][][]byte, x []bool) (X [][]byte){
	X = make([][]byte, 0)
	for i, val := range x {
		if val {
			X = append(X, e[i][1])
		} else {
			X = append(X, e[i][0])
		}
	}
	return
}

func Eval(C *Table, X [][]byte) []byte{
	var K = X
	var res []byte
	for i := 0; i < 3; i++ {
		for j:= 0; j < 4; j++ {
			res = util.XOR(G(K[i],K[i+3],i), C.GetValue(i,j))
			if !bytes.Equal(res[16:], make([]byte,16)) {
				K = append(K, res[:16])
				break
			}
		}
	}
	return K[len(K)-1]
}

func Decode(d [][]byte, Z []byte) bool {
	if bytes.Equal(d[0],Z) {
		return false
	} else if bytes.Equal(d[1],Z) {
		return true
	}
	//TODO: Error handling
	return false
}