-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreaded_db_pir.go
155 lines (111 loc) · 2.99 KB
/
threaded_db_pir.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
package main
import (
"bytes"
"log"
"math/rand"
"checklist/pir"
"fmt"
"time"
"encoding/binary"
"math"
"sync"
)
func rangeIn(low, hi int) uint64 {
return uint64(low + rand.Intn(hi-low))
}
// Generate a database filled with random bytes
func getRows(nRows int, rowLen int) []pir.Row {
rows := make([]pir.Row, nRows)
for i := 0; i < nRows; i++ {
rows[i] = make(pir.Row, rowLen)
phone_num := rangeIn(1000000000, 9999999999)
bs := make([]byte, 8)
binary.LittleEndian.PutUint64(bs, phone_num)
copy(rows[i], bs)
}
return rows
}
func getDBandPartitions(rows []pir.Row, nPartitions int, nPir int) (*pir.StaticDB, []*pir.StaticDB) {
db := pir.StaticDBFromRows(rows)
partitions := make([]*pir.StaticDB, nPartitions)
for i := 0; i < nPartitions; i++ {
partitions[i] = pir.StaticDBFromRows(rows[i*nPir : i*nPir + nPir])
}
return db, partitions
}
func main() {
pirType := pir.Punc
nRows := int(math.Pow(2, 27))
rowLen := 61
rows := getRows(nRows, rowLen)
dum := 84
nPartitions := 64
nPir := nRows/nPartitions
db, partitions := getDBandPartitions(rows, nPartitions, nPir)
offlineReq := pir.NewHintReq(pir.RandSource(), pirType)
all_clients := make([][]pir.Client, 10)
for j := 0; j < 10; j++ {
clients := make([]pir.Client, nPartitions)
for i := 0; i < nPartitions; i++ {
offlineResp, err := offlineReq.Process(*partitions[i])
if err != nil {
log.Fatal("Offline hint generation failed")
}
clients[i] = offlineResp.(pir.HintResp).InitClient(pir.RandSource())
}
all_clients[j] = clients
}
var wg sync.WaitGroup
wg.Add(10)
var lock sync.Mutex
s1 := time.Now()
for cli := 0; cli < len(all_clients); cli++ {
go func (cli int) {
defer wg.Done()
clients := all_clients[cli]
for queryRow := 0; queryRow < 1000; queryRow++ {
partition := int(math.Floor(float64(queryRow / nPir)))
index := queryRow - (nPir*partition)
// Servers answer queries
var err error
lock.Lock()
queries, recon := clients[partition].Query(index)
lock.Unlock()
answers := make([]interface{}, len(queries))
for i := 0; i < len(queries); i++ {
answers[i], err = queries[i].Process(*partitions[partition])
if err != nil {
log.Fatal("Error answering query")
}
}
// Client reconstructs
row, err := recon(answers)
if err != nil {
log.Fatal("Could not reconstruct")
}
if !bytes.Equal(row, db.Row(queryRow)) {
log.Fatal("Incorrect answer returned")
}
}
var err error
for part := 0; part < nPartitions; part++ {
for j := 0; j < dum; j++ {
lock.Lock()
dummy := clients[part].DummyQuery()
lock.Unlock()
dummies := make([]interface{}, len(dummy))
for i := 0; i < len(dummies); i++ {
dummies[i], err = dummy[i].Process(*partitions[part])
if err != nil {
log.Fatal("Error answering query")
}
}
}
}
} (cli)
}
wg.Wait()
t1 := time.Now()
fmt.Print("Time to answer all client queries: ")
fmt.Println(t1.Sub(s1))
}