-
Notifications
You must be signed in to change notification settings - Fork 22
/
ot_test.go
215 lines (180 loc) · 3.71 KB
/
ot_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
//
// ot_test.go
//
// Copyright (c) 2023 Markku Rossi
//
// All rights reserved.
//
package ot
import (
"crypto/rand"
"fmt"
"testing"
)
func testOT(sender, receiver OT, t *testing.T) {
const size int = 64
wires := make([]Wire, size)
flags := make([]bool, size)
labels := make([]Label, size)
done := make(chan error)
for i := 0; i < len(wires); i++ {
var data LabelData
_, err := rand.Read(data[:])
if err != nil {
t.Fatal(err)
}
wires[i].L0.SetData(&data)
_, err = rand.Read(data[:])
if err != nil {
t.Fatal(err)
}
wires[i].L1.SetData(&data)
flags[i] = i%2 == 0
}
pipe, rPipe := NewPipe()
go func(pipe *Pipe) {
err := receiver.InitReceiver(pipe)
if err != nil {
pipe.Close()
pipe.Drain()
done <- err
return
}
err = receiver.Receive(flags, labels)
if err != nil {
pipe.Close()
pipe.Drain()
done <- err
return
}
for i := 0; i < len(flags); i++ {
var expected Label
if flags[i] {
expected = wires[i].L1
} else {
expected = wires[i].L0
}
if !labels[i].Equal(expected) {
err := fmt.Errorf("label %d mismatch %v %v,%v", i,
labels[i], wires[i].L0, wires[i].L1)
pipe.Close()
done <- err
return
}
}
done <- nil
}(rPipe)
err := sender.InitSender(pipe)
if err != nil {
t.Fatalf("InitSender: %v", err)
}
err = sender.Send(wires)
if err != nil {
t.Fatalf("Send: %v", err)
}
err = <-done
if err != nil {
t.Errorf("receiver failed: %v", err)
}
}
func TestOTCO(t *testing.T) {
testOT(NewCO(), NewCO(), t)
}
func TestOTRSA(t *testing.T) {
testOT(NewRSA(2048), NewRSA(2048), t)
}
func benchmarkOT(sender, receiver OT, batchSize int, b *testing.B) {
wires := make([]Wire, batchSize)
flags := make([]bool, batchSize)
labels := make([]Label, batchSize)
done := make(chan error)
for i := 0; i < len(wires); i++ {
var data LabelData
_, err := rand.Read(data[:])
if err != nil {
b.Fatal(err)
}
wires[i].L0.SetData(&data)
_, err = rand.Read(data[:])
if err != nil {
b.Fatal(err)
}
wires[i].L1.SetData(&data)
flags[i] = i%2 == 0
}
pipe, rPipe := NewPipe()
go func(pipe *Pipe) {
err := receiver.InitReceiver(pipe)
if err != nil {
done <- err
pipe.Close()
return
}
for i := 0; i < b.N; i++ {
err = receiver.Receive(flags, labels)
if err != nil {
done <- err
pipe.Close()
return
}
for i := 0; i < len(flags); i++ {
var expected Label
if flags[i] {
expected = wires[i].L1
} else {
expected = wires[i].L0
}
if !labels[i].Equal(expected) {
err := fmt.Errorf("label %d mismatch %v %v,%v", i,
labels[i], wires[i].L0, wires[i].L1)
done <- err
pipe.Close()
return
}
}
}
done <- nil
}(rPipe)
err := sender.InitSender(pipe)
if err != nil {
b.Fatalf("InitSender: %v", err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
err = sender.Send(wires)
if err != nil {
b.Fatalf("Send: %v", err)
}
}
err = <-done
if err != nil {
b.Errorf("receiver failed: %v", err)
}
}
func BenchmarkOTCO_1(b *testing.B) {
benchmarkOT(NewCO(), NewCO(), 1, b)
}
func BenchmarkOTCO_8(b *testing.B) {
benchmarkOT(NewCO(), NewCO(), 8, b)
}
func BenchmarkOTCO_16(b *testing.B) {
benchmarkOT(NewCO(), NewCO(), 16, b)
}
func BenchmarkOTCO_32(b *testing.B) {
benchmarkOT(NewCO(), NewCO(), 32, b)
}
func BenchmarkOTCO_64(b *testing.B) {
benchmarkOT(NewCO(), NewCO(), 64, b)
}
func benchmarkOTRSA(keySize, batchSize int, b *testing.B) {
benchmarkOT(NewRSA(keySize), NewRSA(keySize), batchSize, b)
}
func BenchmarkOTRSA_2048_1(b *testing.B) {
benchmarkOTRSA(2048, 1, b)
}
func BenchmarkOTRSA_2048_8(b *testing.B) {
benchmarkOTRSA(2048, 8, b)
}
func BenchmarkOTRSA_2048_64(b *testing.B) {
benchmarkOTRSA(2048, 64, b)
}