-
Notifications
You must be signed in to change notification settings - Fork 0
/
precomputebuffer_test.go
171 lines (147 loc) · 4.52 KB
/
precomputebuffer_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
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
// Copyright 2021 TNO
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package paillier
import (
"math/big"
"runtime"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestPrecomputeBuffer1(t *testing.T) {
assert := assert.New(t)
sk, err := GenerateKey(2048)
if err != nil {
t.Fatalf("Error generating key: %v", err)
}
pk := sk.PublicKey
pcbuf, err := NewPrecomputeBuffer(pk, 4, 1, true)
if err != nil {
t.Fatalf("Error making buffer: %v", err)
}
defer pcbuf.Close()
m := big.NewInt(42)
ct := pk.PartiallyEncrypt(m)
randomized1 := pcbuf.Randomize(ct)
randomized2 := pcbuf.Randomize(ct)
// Check whether we don't accidentally make a silly mistake
assert.NotEqual(0, randomized1.Cmp(randomized2), "randomized1 == randomized2")
// Just in case, also check whether decryption works in both cases
assert.Equal(sk.Decrypt(randomized1).Uint64(), uint64(42), "Decryption failed after randomize (1)")
assert.Equal(sk.Decrypt(randomized2).Uint64(), uint64(42), "Decryption failed after randomize (2)")
// Just in case, also check that the buffer keeps computing new values
for i := 0; i != 100; i++ {
assert.NotNil(pcbuf.Get())
}
}
func TestPrecomputeBufferClose(t *testing.T) {
sk, err := GenerateKey(2048)
if err != nil {
t.Fatalf("Error generating key: %v", err)
}
pk := sk.PublicKey
pcbuf, err := NewPrecomputeBuffer(pk, 4, 1, true)
if err != nil {
t.Fatalf("Error making buffer: %v", err)
}
time.Sleep(1 * time.Second)
// Testing waitgroup
// If test timeout occurs, then goroutines are dangling.
pcbuf.Close()
}
func TestPrecomputeBufferPrematureClose(t *testing.T) {
sk, err := GenerateKey(2048)
if err != nil {
t.Fatalf("Error generating key: %v", err)
}
pk := sk.PublicKey
pcbuf, err := NewPrecomputeBuffer(pk, 10000, 4, false)
if err != nil {
t.Fatalf("Error making buffer: %v", err)
}
time.Sleep(100 * time.Millisecond)
// Should close quickly, ending all precomputers
t1 := time.Now()
pcbuf.Close()
t2 := time.Now()
assert.Greater(t, int64(500), int64(t2.Sub(t1).Milliseconds()))
// Drain the buffer
for len(pcbuf.rnBuffer) != 0 {
pcbuf.Get()
}
// Should not crash or hang on Get, but return quickly
assert.Nil(t, pcbuf.Get())
// Should still randomize numbers
assert.NotNil(t, pcbuf.Randomize(big.NewInt(1)))
}
func TestEncryptDecryptWithPrecomputation(t *testing.T) {
assert := assert.New(t)
sk, err := GenerateKey(2048)
if err != nil {
t.Fatalf("Error generating key: %v", err)
}
pk := sk.PublicKey
pcbuf, err := NewPrecomputeBuffer(pk, 4, 1, true)
if err != nil {
t.Fatalf("Error making buffer: %v", err)
}
defer pcbuf.Close()
m := big.NewInt(42)
ct := pcbuf.Encrypt(m)
assert.Equal(sk.Decrypt(ct).Uint64(), uint64(42), "Dec(Enc(m)) != m (precompute)")
}
func TestPrecomputationWithWait1(t *testing.T) {
testPrecomputation(t, true, 1)
}
func TestPrecomputationWithoutWait1(t *testing.T) {
testPrecomputation(t, false, 1)
}
func TestPrecomputationWithWaitNumCPU(t *testing.T) {
testPrecomputation(t, true, runtime.NumCPU())
}
func TestPrecomputationWithoutWaitNumCPU(t *testing.T) {
testPrecomputation(t, false, runtime.NumCPU())
}
func testPrecomputation(t *testing.T, waitForPrecomputation bool, nProc int) {
// Yes, this is a benchmark disguised as a test ;)
// TODO: make proper benchmark of this
sk, err := GenerateKey(2048)
if err != nil {
t.Fatalf("Error generating key: %v", err)
}
t.Logf("Done generating key-pair")
pk := sk.PublicKey
n := 500
if testing.Short() {
n = 10
}
t.Logf("Creating precompute buffer of length %d, using %d goroutines", n, nProc)
pcbuf, err := NewPrecomputeBuffer(pk, n, nProc, waitForPrecomputation)
if err != nil {
t.Fatalf("Error making buffer: %v", err)
}
defer pcbuf.Close()
t.Logf("Done creating precompute buffer.")
// here, the buffer should be full if waitForPrecomputation is true
if waitForPrecomputation {
assert.Equal(t, n, len(pcbuf.rnBuffer))
}
m := big.NewInt(42)
t.Logf("Encrypting %d values", n)
cts := make([]*big.Int, n)
for i := 0; i < n; i++ {
cts[i] = pcbuf.Encrypt((m))
}
t.Logf("Done.")
}