forked from sachaservan/private-ann
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.go
72 lines (56 loc) · 1.83 KB
/
query.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
package pir
import (
"github.com/sachaservan/private-ann/pir/dpfc"
"github.com/sachaservan/private-ann/pir/field"
)
// QueryShare is a secret share of a query over the database
// to retrieve a row
type QueryShare struct {
DPFKey *dpfc.DPFKey
PrfKey dpfc.PrfKey
ShareNumber uint
IsKeywordBased bool
}
// BatchQueryShare is a secret share of a batch query over the database
// to retrieve a row
type BatchQueryShare struct {
Queries []*QueryShare
}
// NewIndexQueryShares generates PIR query shares for the index
func (dbmd *DBMetadata) NewIndexQueryShares(index uint64, numShares uint, rangeBits uint) []*QueryShare {
return dbmd.newQueryShares(index, numShares, true, rangeBits)
}
// NewKeywordQueryShares generates keyword-based PIR query shares for keyword
func (dbmd *DBMetadata) NewKeywordQueryShares(keyword uint64, numShares uint, rangeBits uint) []*QueryShare {
return dbmd.newQueryShares(keyword, numShares, false, rangeBits)
}
// NewQueryShares generates random PIR query shares for the index
func (dbmd *DBMetadata) newQueryShares(key uint64, numShares uint, isIndexQuery bool, rangeBits uint) []*QueryShare {
if numShares != 2 {
panic("only two-server DPF supported")
}
client := dpfc.ClientDPFInitialize()
keyA, keyB := client.GenDPFKeys(key, rangeBits)
shares := make([]*QueryShare, numShares)
for i := 0; i < int(numShares); i++ {
shares[i] = &QueryShare{}
shares[i].ShareNumber = uint(i)
shares[i].PrfKey = client.PrfKey
shares[i].IsKeywordBased = !isIndexQuery
if i == 0 {
shares[i].DPFKey = keyA
} else {
shares[i].DPFKey = keyB
}
}
client.Free()
return shares
}
// Recover combines shares of slots to recover the data
func Recover(resShares []*SecretSharedQueryResult) field.FP {
res := field.FP(0)
for _, s := range resShares {
res = field.Add(res, s.Share)
}
return res
}