-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfss_test.go
131 lines (106 loc) · 2.86 KB
/
fss_test.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
package fss
// Source: https://github.com/frankw2/libfss/blob/master/go/test_fss/test_fss.go
import (
"math/rand"
"testing"
"github.com/si-co/vpir-code/lib/field"
"github.com/si-co/vpir-code/lib/utils"
"github.com/stretchr/testify/require"
)
const (
testBlockLength = 17
numBits = 64
)
func TestPoint(t *testing.T) {
// Generate fss Keys on client
fClient := ClientInitialize(testBlockLength)
// random index,
index := randomIndex(numBits)
// Test with if x = index, evaluate to vector b
bLen := testBlockLength
b := make([]uint32, bLen)
for i := range b {
b[i] = field.RandElement()
}
fssKeys := fClient.GenerateTreePF(index, b)
// Simulate server
fServer := ServerInitialize(testBlockLength)
zeros := make([]uint32, bLen)
// test only part of the input space, impossible to do a complete test
// over 64 bits
for j := 0; j <= 10000; j++ {
indexToTest := randomIndex(numBits)
if j == 0 {
indexToTest = index
}
out0 := make([]uint32, bLen)
out1 := make([]uint32, bLen)
sum := make([]uint32, bLen)
fServer.EvaluatePF(0, fssKeys[0], indexToTest, out0)
fServer.EvaluatePF(1, fssKeys[1], indexToTest, out1)
for i := range sum {
sum[i] = (out0[i] + out1[i]) % field.ModP
}
if equalIndices(index, indexToTest) {
require.Equal(t, b, sum)
} else {
require.Equal(t, zeros, sum)
}
}
}
func TestPointWithAlphaVector(t *testing.T) {
// Generate fss Keys on client
fClient := ClientInitialize(testBlockLength)
// random index, biased but fine for this test
index := randomIndex(numBits)
// Test with if x = index, evaluate to vector b
alpha := field.RandElementWithPRG(utils.RandomPRG())
bLen := testBlockLength
b := make([]uint32, testBlockLength)
b[0] = 1
for i := 1; i < len(b); i++ {
a := (uint64(b[i-1]) * uint64(alpha)) % uint64(field.ModP)
b[i] = uint32(a)
}
fssKeys := fClient.GenerateTreePF(index, b)
// Simulate server
fServer := ServerInitialize(testBlockLength)
zeros := make([]uint32, bLen)
// test only random samples of the input space, impossible to do a complete test
// over 64 bits
for j := 0; j <= 10000; j++ {
indexToTest := randomIndex(numBits)
if j == 0 {
indexToTest = index
}
out0 := make([]uint32, bLen)
out1 := make([]uint32, bLen)
sum := make([]uint32, bLen)
fServer.EvaluatePF(0, fssKeys[0], indexToTest, out0)
fServer.EvaluatePF(1, fssKeys[1], indexToTest, out1)
for i := range sum {
sum[i] = (out0[i] + out1[i]) % field.ModP
}
if equalIndices(index, indexToTest) {
require.Equal(t, b, sum)
} else {
require.Equal(t, zeros, sum)
}
}
}
// return random index, biased but fine for this test
func randomIndex(bits int) []bool {
index := make([]bool, bits)
for i := range index {
index[i] = (rand.Intn(2) % 2) != 0
}
return index
}
func equalIndices(a, b []bool) bool {
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}