/
simple_pir.go
220 lines (177 loc) · 5.81 KB
/
simple_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
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
package pir
// #cgo CFLAGS: -O3 -march=native
// #include "pir.h"
import "C"
import "fmt"
type SimplePIR struct{}
func (pi *SimplePIR) Name() string {
return "SimplePIR"
}
func (pi *SimplePIR) PickParams(N, d, n, logq uint64) Params {
good_p := Params{}
found := false
// Iteratively refine p and DB dims, until find tight values
for mod_p := uint64(2); ; mod_p += 1 {
l, m := ApproxSquareDatabaseDims(N, d, mod_p)
p := Params{
N: n,
Logq: logq,
L: l,
M: m,
}
p.PickParams(false, m)
if p.P < mod_p {
if !found {
panic("Error; should not happen")
}
good_p.PrintParams()
return good_p
}
good_p = p
found = true
}
panic("Cannot be reached")
return Params{}
}
func (pi *SimplePIR) PickParamsGivenDimensions(l, m, n, logq uint64) Params {
p := Params{
N: n,
Logq: logq,
L: l,
M: m,
}
p.PickParams(false, m)
return p
}
// Works for SimplePIR because vertical concatenation doesn't increase
// the number of LWE samples (so don't need to change LWE params)
func (pi *SimplePIR) ConcatDBs(DBs []*Database, p *Params) *Database {
if len(DBs) == 0 {
panic("Should not happen")
}
if DBs[0].Info.Num != p.L * p.M {
panic("Not yet implemented")
}
rows := DBs[0].Data.Rows
for j:=1; j<len(DBs); j++ {
if DBs[j].Data.Rows != rows {
panic("Bad input")
}
}
D := new(Database)
D.Data = MatrixZeros(0, 0)
D.Info = DBs[0].Info
D.Info.Num *= uint64(len(DBs))
p.L *= uint64(len(DBs))
for j:=0; j<len(DBs); j++ {
D.Data.Concat(DBs[j].Data.SelectRows(0, rows))
}
return D
}
func (pi *SimplePIR) GetBW(info DBinfo, p Params) {
offline_download := float64(p.L*p.N*p.Logq) / (8.0 * 1024.0)
fmt.Printf("\t\tOffline download: %d KB\n", uint64(offline_download))
online_upload := float64(p.M*p.Logq) / (8.0 * 1024.0)
fmt.Printf("\t\tOnline upload: %d KB\n", uint64(online_upload))
online_download := float64(p.L*p.Logq) / (8.0 * 1024.0)
fmt.Printf("\t\tOnline download: %d KB\n", uint64(online_download))
}
func (pi *SimplePIR) Init(info DBinfo, p Params) State {
A := MatrixRand(p.M, p.N, p.Logq, 0)
return MakeState(A)
}
func (pi *SimplePIR) InitCompressed(info DBinfo, p Params) (State, CompressedState) {
seed := RandomPRGKey()
return pi.InitCompressedSeeded(info, p, seed)
}
func (pi *SimplePIR) InitCompressedSeeded(info DBinfo, p Params, seed *PRGKey) (State, CompressedState) {
bufPrgReader = NewBufPRG(NewPRG(seed))
return pi.Init(info, p), MakeCompressedState(seed)
}
func (pi *SimplePIR) DecompressState(info DBinfo, p Params, comp CompressedState) State {
bufPrgReader = NewBufPRG(NewPRG(comp.Seed))
return pi.Init(info, p)
}
func (pi *SimplePIR) Setup(DB *Database, shared State, p Params) (State, Msg) {
A := shared.Data[0]
H := MatrixMul(DB.Data, A)
// map the database entries to [0, p] (rather than [-p/1, p/2]) and then
// pack the database more tightly in memory, because the online computation
// is memory-bandwidth-bound
DB.Data.Add(p.P / 2)
DB.Squish()
return MakeState(), MakeMsg(H)
}
func (pi *SimplePIR) FakeSetup(DB *Database, p Params) (State, float64) {
offline_download := float64(p.L*p.N*uint64(p.Logq)) / (8.0 * 1024.0)
fmt.Printf("\t\tOffline download: %d KB\n", uint64(offline_download))
// map the database entries to [0, p] (rather than [-p/1, p/2]) and then
// pack the database more tightly in memory, because the online computation
// is memory-bandwidth-bound
DB.Data.Add(p.P / 2)
DB.Squish()
return MakeState(), offline_download
}
func (pi *SimplePIR) Query(i uint64, shared State, p Params, info DBinfo) (State, Msg) {
A := shared.Data[0]
secret := MatrixRand(p.N, 1, p.Logq, 0)
err := MatrixGaussian(p.M, 1)
query := MatrixMul(A, secret)
query.MatrixAdd(err)
query.Data[i%p.M] += C.Elem(p.Delta())
// Pad the query to match the dimensions of the compressed DB
if p.M%info.Squishing != 0 {
query.AppendZeros(info.Squishing - (p.M % info.Squishing))
}
return MakeState(secret), MakeMsg(query)
}
func (pi *SimplePIR) Answer(DB *Database, query MsgSlice, server State, shared State, p Params) Msg {
ans := new(Matrix)
num_queries := uint64(len(query.Data)) // number of queries in the batch of queries
batch_sz := DB.Data.Rows / num_queries // how many rows of the database each query in the batch maps to
last := uint64(0)
// Run SimplePIR's answer routine for each query in the batch
for batch, q := range query.Data {
if batch == int(num_queries-1) {
batch_sz = DB.Data.Rows - last
}
a := MatrixMulVecPacked(DB.Data.SelectRows(last, batch_sz),
q.Data[0],
DB.Info.Basis,
DB.Info.Squishing)
ans.Concat(a)
last += batch_sz
}
return MakeMsg(ans)
}
func (pi *SimplePIR) Recover(i uint64, batch_index uint64, offline Msg, query Msg, answer Msg,
shared State, client State, p Params, info DBinfo) uint64 {
secret := client.Data[0]
H := offline.Data[0]
ans := answer.Data[0]
ratio := p.P/2
offset := uint64(0);
for j := uint64(0); j<p.M; j++ {
offset += ratio*query.Data[0].Get(j,0)
}
offset %= (1 << p.Logq)
offset = (1 << p.Logq)-offset
row := i / p.M
interm := MatrixMul(H, secret)
ans.MatrixSub(interm)
var vals []uint64
// Recover each Z_p element that makes up the desired database entry
for j := row * info.Ne; j < (row+1)*info.Ne; j++ {
noised := uint64(ans.Data[j]) + offset
denoised := p.Round(noised)
vals = append(vals, denoised)
//fmt.Printf("Reconstructing row %d: %d\n", j, denoised)
}
ans.MatrixAdd(interm)
return ReconstructElem(vals, i, info)
}
func (pi *SimplePIR) Reset(DB *Database, p Params) {
// Uncompress the database, and map its entries to the range [-p/2, p/2].
DB.Unsquish()
DB.Data.Sub(p.P / 2)
}