-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
71 lines (57 loc) · 1.47 KB
/
main.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
package main
import (
"bytes"
"log"
"math/rand"
"checklist/pir"
)
// Generate a database filled with random bytes
func randomDatabase(nRows int, rowLen int) *pir.StaticDB {
rows := make([]pir.Row, nRows)
for i := 0; i < nRows; i++ {
rows[i] = make(pir.Row, rowLen)
_, err := rand.Read(rows[i][:])
if err != nil {
log.Fatal("rand.Read failed")
}
}
return pir.StaticDBFromRows(rows)
}
func main() {
// Use the puncturable-set-based PIR scheme
pirType := pir.Punc
nRows := 1024
rowLen := 256
queryRow := 79
// Generate a database
db := randomDatabase(nRows, rowLen)
// ===== OFFLINE PHASE =====
// Client asks for offline hint
offlineReq := pir.NewHintReq(pir.RandSource(), pirType)
// Server responds with hint
offlineResp, err := offlineReq.Process(*db)
if err != nil {
log.Fatal("Offline hint generation failed")
}
// Initialize the client state
client := offlineResp.(pir.HintResp).InitClient(pir.RandSource())
// ===== ONLINE PHASE =====
// Client generates queries for servers
queries, recon := client.Query(queryRow)
// Servers answer queries
answers := make([]interface{}, len(queries))
for i := 0; i < len(queries); i++ {
answers[i], err = queries[i].Process(*db)
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")
}
}