-
Notifications
You must be signed in to change notification settings - Fork 22
/
arithmetic_test.go
217 lines (188 loc) · 3.84 KB
/
arithmetic_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
//
// Copyright (c) 2019-2023 Markku Rossi
//
// All rights reserved.
//
package compiler
import (
"fmt"
"io"
"math/big"
"testing"
"github.com/markkurossi/mpc/circuit"
"github.com/markkurossi/mpc/compiler/utils"
"github.com/markkurossi/mpc/ot"
"github.com/markkurossi/mpc/p2p"
)
type Test struct {
Name string
Heavy bool
Operand string
Bits int
Eval func(a *big.Int, b *big.Int) *big.Int
Code string
}
var tests = []Test{
{
Name: "Add",
Heavy: true,
Operand: "+",
Bits: 2,
Eval: func(a *big.Int, b *big.Int) *big.Int {
result := big.NewInt(0)
result.Add(a, b)
return result
},
Code: `
package main
func main(a, b int3) int3 {
return a + b
}
`,
},
// 1-bit, 2-bit, and n-bit multipliers have a bit different wiring.
{
Name: "Multiply 1-bit",
Heavy: false,
Operand: "*",
Bits: 1,
Eval: func(a *big.Int, b *big.Int) *big.Int {
result := big.NewInt(0)
result.Mul(a, b)
return result
},
Code: `
package main
func main(a, b int1) int1 {
return a * b
}
`,
},
{
Name: "Multiply 2-bits",
Heavy: true,
Operand: "*",
Bits: 2,
Eval: func(a *big.Int, b *big.Int) *big.Int {
result := big.NewInt(0)
result.Mul(a, b)
return result
},
Code: `
package main
func main(a, b int4) int4 {
return a * b
}
`,
},
{
Name: "Multiply n-bits",
Heavy: true,
Operand: "*",
Bits: 2,
Eval: func(a *big.Int, b *big.Int) *big.Int {
result := big.NewInt(0)
result.Mul(a, b)
return result
},
Code: `
package main
func main(a, b int6) int6 {
return a * b
}
`,
},
}
func TestArithmetics(t *testing.T) {
for _, test := range tests {
if testing.Short() && test.Heavy {
fmt.Printf("Skipping %s\n", test.Name)
continue
}
circ, _, err := New(utils.NewParams()).Compile(test.Code, nil)
if err != nil {
t.Fatalf("Failed to compile test %s: %s", test.Name, err)
}
limit := 1 << test.Bits
for g := 0; g < limit; g++ {
for e := 0; e < limit; e++ {
gr, ew := io.Pipe()
er, gw := io.Pipe()
gio := newReadWriter(gr, gw)
eio := newReadWriter(er, ew)
gInput := big.NewInt(int64(g))
eInput := big.NewInt(int64(e))
gerr := make(chan error)
go func() {
_, err := circuit.Garbler(p2p.NewConn(gio), ot.NewCO(),
circ, gInput, false)
gerr <- err
}()
result, err := circuit.Evaluator(p2p.NewConn(eio),
ot.NewCO(), circ, eInput, false)
if err != nil {
t.Fatalf("Evaluator failed: %s\n", err)
}
err = <-gerr
if err != nil {
t.Fatalf("Garbler failed: %s\n", err)
}
expected := test.Eval(gInput, eInput)
if expected.Cmp(result[0]) != 0 {
t.Errorf("%s failed: %s %s %s = %s, expected %s\n",
test.Name, gInput, test.Operand, eInput, result,
expected)
}
}
}
}
}
var mult512 = `
package main
func main(a, b int512) int512 {
return a * b
}
`
func BenchmarkMult(b *testing.B) {
circ, _, err := New(utils.NewParams()).Compile(mult512, nil)
if err != nil {
b.Fatalf("failed to compile test: %s", err)
}
gr, ew := io.Pipe()
er, gw := io.Pipe()
gio := newReadWriter(gr, gw)
eio := newReadWriter(er, ew)
gInput := big.NewInt(int64(11))
eInput := big.NewInt(int64(13))
gerr := make(chan error)
go func() {
_, err := circuit.Garbler(p2p.NewConn(gio), ot.NewCO(), circ, gInput,
false)
gerr <- err
}()
_, err = circuit.Evaluator(p2p.NewConn(eio), ot.NewCO(), circ, eInput,
false)
if err != nil {
b.Fatalf("Evaluator failed: %s\n", err)
}
err = <-gerr
if err != nil {
b.Fatalf("Garbler failed: %s\n", err)
}
}
func newReadWriter(in io.Reader, out io.Writer) io.ReadWriter {
return &wrap{
in: in,
out: out,
}
}
type wrap struct {
in io.Reader
out io.Writer
}
func (w *wrap) Read(p []byte) (n int, err error) {
return w.in.Read(p)
}
func (w *wrap) Write(p []byte) (n int, err error) {
return w.out.Write(p)
}