-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcompiler_test.go
270 lines (248 loc) · 4.33 KB
/
compiler_test.go
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//
// Copyright (c) 2019-2023 Markku Rossi
//
// All rights reserved.
//
package compiler
import (
"math"
"math/big"
"math/rand"
"testing"
"github.com/markkurossi/mpc/compiler/utils"
)
type IteratorTest struct {
Name string
Operand string
Bits int
Eval func(a int64, b int64) int64
Code string
}
var iteratorTests = []IteratorTest{
{
Name: "Add",
Operand: "+",
Bits: 2,
Eval: func(a int64, b int64) int64 {
return a + b
},
Code: `
package main
func main(a, b uint3) uint3 {
return a + b
}
`,
},
{
Name: "Sub",
Operand: "-",
Bits: 2,
Eval: func(a int64, b int64) int64 {
return a - b
},
Code: `
package main
func main(a, b uint64) uint {
return a - b
}
`,
},
// 1-bit, 2-bit, and n-bit multipliers have a bit different wiring.
{
Name: "Multiply 1-bit",
Operand: "*",
Bits: 1,
Eval: func(a int64, b int64) int64 {
return a * b
},
Code: `
package main
func main(a, b uint1) uint1 {
return a * b
}
`,
},
{
Name: "Multiply 2-bits",
Operand: "*",
Bits: 2,
Eval: func(a int64, b int64) int64 {
return a * b
},
Code: `
package main
func main(a, b uint4) uint4 {
return a * b
}
`,
},
{
Name: "Multiply n-bits",
Operand: "*",
Bits: 2,
Eval: func(a int64, b int64) int64 {
return a * b
},
Code: `
package main
func main(a, b uint6) uint6 {
return a * b
}
`,
},
}
func TestIterator(t *testing.T) {
for idx, test := range iteratorTests {
circ, _, err := New(utils.NewParams()).Compile(test.Code, nil)
if err != nil {
t.Fatalf("Failed to compile test %s: %s", test.Name, err)
}
limit := int64(1 << test.Bits)
var g, e int64
for g = 0; g < limit; g++ {
for e = 0; e < limit; e++ {
n1 := big.NewInt(g)
n2 := big.NewInt(e)
results, err := circ.Compute([]*big.Int{n1, n2})
if err != nil {
t.Fatalf("%d: compute failed: %s\n", idx, err)
}
expected := test.Eval(g, e)
if expected != results[0].Int64() {
t.Errorf("%s failed: %d %s %d = %d, expected %d\n",
test.Name, g, test.Operand, e, results[0],
expected)
}
}
}
}
}
type FixedTest struct {
N1 int64
N2 int64
N3 int64
Code string
}
var fixedTests = []FixedTest{
{
N1: 5,
N2: 3,
N3: 5,
Code: `
package main
func main(a, b int4) int4 {
if a > b {
return a
}
return b
}
`,
},
{
N1: 5,
N2: 3,
N3: 6,
Code: `
package main
func main(a, b int4) int4 {
if a > b {
return add1(a)
}
return add1(b)
}
func add1(val int) int {
return val + 1
}
`,
},
{
N1: 5,
N2: 3,
N3: 7,
Code: `
package main
func main(a, b int4) int4 {
if a > b {
return add2(a)
}
return add2(b)
}
func add1(val int) int {
return val + 1
}
func add2(val int) int {
return add1(add1(val))
}
`,
},
{
N1: 5,
N2: 3,
N3: 8,
Code: `
package main
func main(a, b int4) int4 {
return Sum2(MinMax(a, b))
}
func Sum2(a, b int) int {
return a + b
}
func MinMax(a, b int) (int, int) {
if a > b {
return b, a
}
return a, b
}
`,
},
}
func TestFixed(t *testing.T) {
for idx, test := range fixedTests {
circ, _, err := New(utils.NewParams()).Compile(test.Code, nil)
if err != nil {
t.Errorf("failed to compile test %d: %s", idx, err)
continue
}
n1 := big.NewInt(test.N1)
n2 := big.NewInt(test.N2)
results, err := circ.Compute([]*big.Int{n1, n2})
if err != nil {
t.Errorf("compute failed: %s", err)
continue
}
if results[0].Int64() != test.N3 {
t.Errorf("test %d failed: got %d (%x), expected %d (%x)", idx,
results[0].Int64(), results[0].Int64(), test.N3, test.N3)
}
}
}
func TestSubtraction(t *testing.T) {
circ, _, err := New(utils.NewParams()).Compile(`package main
func main(a, b uint64) uint64 {
return a - b
}
`, nil)
if err != nil {
t.Fatalf("Failed to compile test: %s", err)
}
r := rand.New(rand.NewSource(99))
for i := 0; i < 1000; i++ {
g := new(big.Int).Rand(r, big.NewInt(math.MaxInt64))
var e *big.Int
for {
e = new(big.Int).Rand(r, big.NewInt(math.MaxInt64))
if e.Cmp(g) < 0 {
break
}
}
results, err := circ.Compute([]*big.Int{g, e})
if err != nil {
t.Fatalf("compute failed: %s\n", err)
}
expected := new(big.Int).Sub(g, e)
if expected.Cmp(results[0]) != 0 {
t.Errorf("failed: %d - %d = %d, expected %d\n",
g, e, results[0], expected)
}
}
}