forked from henrycg/prio
/
count_min.go
96 lines (79 loc) · 2.04 KB
/
count_min.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
package mpc
import (
"fmt"
"log"
"math/big"
"github.com/KuraTheDog/prio/circuit"
"github.com/KuraTheDog/prio/utils"
)
func bucketToIndex(nBuckets, hash, bucket int) int {
return hash*nBuckets + bucket
}
func rowCircuit(nHashes, nBuckets, row int, bits *circuit.Circuit) *circuit.Circuit {
ckt := circuit.Empty()
var last *circuit.Gate
inps := bits.InputGates()
// Sum up the bits in every row
for i := 0; i < nBuckets; i++ {
g := circuit.NewGate()
g.ParentL = inps[(nBuckets*row)+i]
if i == 0 {
g.GateType = circuit.Gate_AddConst
g.Constant = circuit.NegOne
} else {
g.GateType = circuit.Gate_Add
g.ParentR = last
}
if i == nBuckets-1 {
ckt.AddZeroGate(g)
} else {
ckt.AddGate(g)
}
last = g
}
return ckt
}
func countMin_Circuit(name string, nHashes, nBuckets int) *circuit.Circuit {
total := nHashes * nBuckets
// Ensure that each value in the sketch is a 0/1 value
cktsBits := make([]*circuit.Circuit, total)
for i := 0; i < total; i++ {
cktsBits[i] = circuit.OneBit(fmt.Sprintf("%v[%v]", name, i))
}
ckt := circuit.AndCircuits(cktsBits)
// Ensure that the sum of every row is 1
ckts := make([]*circuit.Circuit, nHashes+1)
ckts[0] = ckt
for h := 0; h < nHashes; h++ {
ckts[h+1] = rowCircuit(nHashes, nBuckets, h, ckt)
}
return circuit.AndCircuits(ckts)
}
func countMin_NewRandom(nHashes, nBuckets int) []*big.Int {
total := nHashes * nBuckets
values := make([]bool, total)
bigHashes := big.NewInt(int64(nBuckets))
for h := 0; h < nHashes; h++ {
idx := int(utils.RandInt(bigHashes).Int64())
values[bucketToIndex(nBuckets, h, idx)] = true
}
return countMin_New(nHashes, nBuckets, values)
}
func countMin_New(nHashes, nBuckets int, values []bool) []*big.Int {
if nBuckets < 1 || nHashes < 1 {
log.Fatal("nBuckets and nHashes must have value >= 1")
}
total := nHashes * nBuckets
if len(values) != total {
log.Fatal("Malformed request")
}
out := make([]*big.Int, total)
for i := 0; i < total; i++ {
if values[i] {
out[i] = utils.One
} else {
out[i] = utils.Zero
}
}
return out
}