-
Notifications
You must be signed in to change notification settings - Fork 0
/
precomputebuffer.go
129 lines (115 loc) · 4.21 KB
/
precomputebuffer.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
// 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 (
"errors"
"math/big"
"sync"
)
// PrecomputeBuffer embeds PublicKey and thus implements the Encrypter interface,
type PrecomputeBuffer struct {
PublicKey // public key for which we generate random exponents
rnBuffer chan *big.Int // buffer of precomputed random exponents
closed chan struct{} // signal closure to the precomputers
numProc int // number of processors/goroutines to use for filling the buffer
wg sync.WaitGroup // waitgroup to ensure proper goroutine cleanup
}
// NewPrecomputeBuffer creates a new PrecomputeBuffer for the specified public key and
// immediately starts filling this buffer using numProc processors (goroutines).
func NewPrecomputeBuffer(pk PublicKey, bufferSize int, numProc int, waitForCompletion bool) (*PrecomputeBuffer, error) {
if numProc <= 0 {
return nil, errors.New("Must use at least one precomputation process")
}
if bufferSize < 0 {
return nil, errors.New("Buffer size can not be negative")
}
bufferFilled := make(chan bool)
pcbuf := &PrecomputeBuffer{pk, make(chan *big.Int, bufferSize), make(chan struct{}), numProc, sync.WaitGroup{}}
pcbuf.spawnPrecomputers(numProc, bufferFilled)
if waitForCompletion {
bufferFilled <- true // This blocks until someone reads from it
}
close(bufferFilled) // Release other computers reading this
return pcbuf, nil
}
// Close stops the goroutines associated with the PrecomputeBuffer
func (pcbuf *PrecomputeBuffer) Close() {
close(pcbuf.closed)
pcbuf.wg.Wait()
}
// Get returns a new random exponent
func (pcbuf *PrecomputeBuffer) Get() *big.Int {
select {
case rn := <-pcbuf.rnBuffer:
return rn
case <-pcbuf.closed:
return nil
}
}
// spawnPrecomputers starts goroutines that fill the channel
func (pcbuf *PrecomputeBuffer) spawnPrecomputers(numProc int, bufferFilled chan bool) {
for i := 0; i != numProc; i++ {
pcbuf.wg.Add(1)
go pcbuf.precompute(bufferFilled)
}
}
// precompute tries to fill the pcbuf.rnBuffer channel with fresh random exponents
// the bufferFilled channel is used signal that the buffered channels has been
// filled
func (pcbuf *PrecomputeBuffer) precompute(bufferFilled chan bool) {
defer pcbuf.wg.Done()
fillingBuffer := true
var newRN *big.Int
for fillingBuffer {
newRN = pcbuf.PublicKey.getRN()
select {
case pcbuf.rnBuffer <- newRN:
// Managed to put something in the buffer
case <-pcbuf.closed:
return
default:
// This is reached only when `pcbuf.rnBuffer` is full
// Read something from bufferFilled, this will trigger the wait
// This channel is closed if nobody is waiting anymore
<-bufferFilled
fillingBuffer = false
}
}
for {
// When we exit this loop, we still need to process the last item
// we weren't able to put into the channel, hence we'll start with
// sending something to the channel here.
select {
case <-pcbuf.closed:
return
case pcbuf.rnBuffer <- newRN:
newRN = pcbuf.PublicKey.getRN()
}
}
}
// Randomize randomizes an encrypted value and uses pre-computed random exponents to speed-up the computation.
func (pcbuf *PrecomputeBuffer) Randomize(a *big.Int) *big.Int {
if rn := pcbuf.Get(); rn != nil {
return rn.Mul(rn, a)
}
// PrecomputationBuffer is already closed
// which makes this call erroneous
// Nevertheless, returning nil is not legal, and may crash the program
// so we opt for the slower method of using the PK for randomization
return pcbuf.PublicKey.Randomize(a)
}
// Encrypt encrypts a message (big.Int) and uses pre-computed random exponents to speed-up the computation.
func (pcbuf *PrecomputeBuffer) Encrypt(m *big.Int) *big.Int {
return pcbuf.Randomize(pcbuf.PublicKey.PartiallyEncrypt(m))
}