forked from tuneinsight/lattigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ring_sampler_uniform.go
169 lines (127 loc) · 4.6 KB
/
ring_sampler_uniform.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
package ring
import (
"encoding/binary"
"github.com/jzhchu/lattigo/utils"
)
// UniformSampler wraps a util.PRNG and represents the state of a sampler of uniform polynomials.
type UniformSampler struct {
baseSampler
randomBufferN []byte
}
// NewUniformSampler creates a new instance of UniformSampler from a PRNG and ring definition.
func NewUniformSampler(prng utils.PRNG, baseRing *Ring) *UniformSampler {
uniformSampler := new(UniformSampler)
uniformSampler.baseRing = baseRing
uniformSampler.prng = prng
uniformSampler.randomBufferN = make([]byte, baseRing.N)
return uniformSampler
}
// Read generates a new polynomial with coefficients following a uniform distribution over [0, Qi-1].
func (uniformSampler *UniformSampler) Read(Pol *Poly) {
var randomUint, mask, qi uint64
var ptr int
uniformSampler.prng.Read(uniformSampler.randomBufferN)
for j := range uniformSampler.baseRing.Modulus[:len(Pol.Coeffs)] {
qi = uniformSampler.baseRing.Modulus[j]
// Starts by computing the mask
mask = uniformSampler.baseRing.Mask[j]
ptmp := Pol.Coeffs[j]
// Iterates for each modulus over each coefficient
for i := 0; i < uniformSampler.baseRing.N; i++ {
// Samples an integer between [0, qi-1]
for {
// Refills the buff if it runs empty
if ptr == uniformSampler.baseRing.N {
uniformSampler.prng.Read(uniformSampler.randomBufferN)
ptr = 0
}
// Reads bytes from the buff
randomUint = binary.BigEndian.Uint64(uniformSampler.randomBufferN[ptr:ptr+8]) & mask
ptr += 8
// If the integer is between [0, qi-1], breaks the loop
if randomUint < qi {
break
}
}
ptmp[i] = randomUint
}
}
}
// ReadLvl generates a new polynomial with coefficients following a uniform distribution over [0, Qi-1].
func (uniformSampler *UniformSampler) ReadLvl(level int, Pol *Poly) {
var randomUint, mask, qi uint64
var ptr int
uniformSampler.prng.Read(uniformSampler.randomBufferN)
for j := 0; j < level+1; j++ {
qi = uniformSampler.baseRing.Modulus[j]
// Starts by computing the mask
mask = uniformSampler.baseRing.Mask[j]
ptmp := Pol.Coeffs[j]
// Iterates for each modulus over each coefficient
for i := 0; i < uniformSampler.baseRing.N; i++ {
// Samples an integer between [0, qi-1]
for {
// Refills the buff if it runs empty
if ptr == uniformSampler.baseRing.N {
uniformSampler.prng.Read(uniformSampler.randomBufferN)
ptr = 0
}
// Reads bytes from the buff
randomUint = binary.BigEndian.Uint64(uniformSampler.randomBufferN[ptr:ptr+8]) & mask
ptr += 8
// If the integer is between [0, qi-1], breaks the loop
if randomUint < qi {
break
}
}
ptmp[i] = randomUint
}
}
}
// ReadNew generates a new polynomial with coefficients following a uniform distribution over [0, Qi-1].
// Polynomial is created at the max level.
func (uniformSampler *UniformSampler) ReadNew() (Pol *Poly) {
Pol = uniformSampler.baseRing.NewPoly()
uniformSampler.Read(Pol)
return
}
// ReadLvlNew generates a new polynomial with coefficients following a uniform distribution over [0, Qi-1].
// Polynomial is created at the specified level.
func (uniformSampler *UniformSampler) ReadLvlNew(level int) (Pol *Poly) {
Pol = uniformSampler.baseRing.NewPolyLvl(level)
uniformSampler.ReadLvl(level, Pol)
return
}
func (uniformSampler *UniformSampler) WithPRNG(prng utils.PRNG) *UniformSampler {
return &UniformSampler{baseSampler: baseSampler{prng: prng, baseRing: uniformSampler.baseRing}, randomBufferN: uniformSampler.randomBufferN}
}
// RandUniform samples a uniform randomInt variable in the range [0, mask] until randomInt is in the range [0, v-1].
// mask needs to be of the form 2^n -1.
func RandUniform(prng utils.PRNG, v uint64, mask uint64) (randomInt uint64) {
for {
randomInt = randInt64(prng, mask)
if randomInt < v {
return randomInt
}
}
}
// randInt32 samples a uniform variable in the range [0, mask], where mask is of the form 2^n-1, with n in [0, 32].
func randInt32(prng utils.PRNG, mask uint64) uint64 {
// generate random 4 bytes
randomBytes := make([]byte, 4)
prng.Read(randomBytes)
// convert 4 bytes to a uint32
randomUint32 := uint64(binary.BigEndian.Uint32(randomBytes))
// return required bits
return mask & randomUint32
}
// randInt64 samples a uniform variable in the range [0, mask], where mask is of the form 2^n-1, with n in [0, 64].
func randInt64(prng utils.PRNG, mask uint64) uint64 {
// generate random 8 bytes
randomBytes := make([]byte, 8)
prng.Read(randomBytes)
// convert 8 bytes to a uint64
randomUint64 := binary.BigEndian.Uint64(randomBytes)
// return required bits
return mask & randomUint64
}