-
Notifications
You must be signed in to change notification settings - Fork 2
/
bootstrap_internals.rs
218 lines (192 loc) · 7.05 KB
/
bootstrap_internals.rs
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//! Module containing the bootstrapping procedure.
//!
//! The bootstrapping function [`tfhe_bootstrap`](fn.tfhe_bootstrap.html) can
//! be used externally, but it should not be required unless you're implementing specific
//! functionality such as custom gates and want to reduce the number of bootstrapping
//! operations you perform.
use crate::{
lwe::{lwe_key_switch, LweBootstrappingKey, LweSample},
numerics::{Modulo, Torus32},
polynomial::{Polynomial, TorusPolynomial},
tgsw::{tgsw_extern_mul_to_tlwe, TGswParams, TGswSample},
tlwe::TLweSample,
};
/// Performs the bootstrapping operation to the given ciphertext.
/// This is an expensive operation and should only be performed if the
/// ciphertext requires further computation.
///
/// # Arguments
/// - `bk` - The bootstrapping + keyswitch key
/// - `mu` - The output message (if phase(x)>0)
/// - `x` - The input sample
/// Returns LWE(mu) iff phase(x) > 0, LWE(-mu) iff phase(x) < 0
pub fn tfhe_bootstrap(bk: &LweBootstrappingKey, mu: Torus32, x: LweSample) -> LweSample {
let res = tfhe_bootstrap_without_key_switching(bk, mu, x);
// Key Switching
lwe_key_switch(&bk.ks, res)
}
/**
* result = LWE(mu) iff phase(x)>0, LWE(-mu) iff phase(x)<0
* @param result The resulting LweSample
* @param bk The bootstrapping + keyswitch key
* @param mu The output message (if phase(x)>0)
* @param x The input sample
*/
pub(crate) fn tfhe_bootstrap_without_key_switching(
bk: &LweBootstrappingKey,
mu: Torus32,
x: LweSample,
) -> LweSample {
let bk_params = &bk.bk_params;
let accum_params = &bk.accum_params;
let in_params = &bk.in_out_params;
let big_n = accum_params.n;
let n_x_2 = 2 * big_n;
let n = in_params.n;
let barb = crate::numerics::decode_message(x.b, n_x_2);
let bara: Vec<i32> = x
.coefficients
.iter()
.map(|c| crate::numerics::decode_message(*c, n_x_2))
.collect();
// The initial testvec = [mu,mu,mu,...,mu]
let test_vec = TorusPolynomial::from(
std::iter::repeat(mu)
.take(big_n as usize)
.collect::<Vec<Torus32>>(),
);
tfhe_blind_rotate_and_extract(test_vec, &bk.bk, barb, bara, n, bk_params)
}
/**
* result = LWE(v_p) where p=barb-sum(bara_i.s_i) mod 2N
* @param result the output LWE sample
* @param v a 2N-elt anticyclic function (represented by a TorusPolynomial)
* @param bk An array of n TGSW samples where bk_i encodes s_i
* @param barb A coefficients between 0 and 2N-1
* @param bara An array of n coefficients between 0 and 2N-1
* @param bk_params The parameters of bk
*/
pub(crate) fn tfhe_blind_rotate_and_extract(
v: TorusPolynomial,
bk: &[TGswSample],
barb: i32,
bara: Vec<i32>,
n: i32,
bk_params: &TGswParams,
) -> LweSample {
let accum_params = &bk_params.tlwe_params;
let extract_params = &accum_params.extracted_lweparams;
let _2n = 2 * v.len() as i32;
let test_vec_bis = if barb != 0 {
// `_2n - barb` should be equivalent to `-barb mod _2n`
crate::polynomial::mul_by_monomial(v, (-barb).modulo(_2n))
} else {
v
};
let acc = TLweSample::trivial(test_vec_bis, accum_params);
let acc = tfhe_blind_rotate(acc, bk, bara, n, bk_params);
acc.extract_lwe(extract_params, accum_params)
}
/// The BlindRotate algorithm multiplies the polynomial encrypted in the input
/// TRLWE ciphertext by an encrypted power of X. The effect produced is a rotation of the coefficients.
/// Multiply the accumulator by X^sum(bara_i.s_i)
/// # Arguments
/// * `accum` - The TLWE sample to multiply
/// * `bk` - An array of n TGSW samples where bk_i encodes s_i
/// * `bara` - An array of n coefficients between 0 and 2N-1
/// * `bk_params` - The parameters of bk
pub(crate) fn tfhe_blind_rotate(
accum: TLweSample,
bk: &[TGswSample],
bara: Vec<i32>,
n: i32,
bk_params: &TGswParams,
) -> TLweSample {
let mut temp = accum.clone(); //TLweSample::new(&bk_params.tlwe_params);
temp.clear();
let mut temp2 = accum;
for i in 0..n as usize {
let barai = bara[i];
if barai == 0 {
// Indeed, this is an easy case!
// TODO: Figure out why this is an easy case. I'm guessing no work needs to be done for some reason
continue;
}
temp = tfhe_mux_rotate(&temp.clone(), &temp2, &bk[i], barai, bk_params);
std::mem::swap(&mut temp, &mut temp2);
}
// TODO: Figure out if this was a correct translation
temp2
}
fn tfhe_mux_rotate(
result: &TLweSample,
accum: &TLweSample,
bki: &TGswSample,
barai: i32,
bk_params: &TGswParams,
) -> TLweSample {
// accum + BK_i * [(X^bar{a}_i-1) * accum]
let temp = crate::tlwe::mul_by_monomial(result.clone() + accum.clone(), barai) - accum.clone();
accum.clone() + tgsw_extern_mul_to_tlwe(&temp, bki, bk_params)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{polynomial::Polynomial, tlwe::TLweParameters};
use rand::Rng;
use rand_distr::Distribution;
const BIG_N: u32 = 1024;
const SMOL_N: u32 = 500;
const ALPHA_IN: f64 = 5e-4;
const ALPHA_BK: f64 = 9e-9;
const BG_BIT_BK: i32 = 10;
const K: i32 = 1;
const L_BK: i32 = 3;
fn generate_random_key(n: u32) -> Vec<bool> {
let mut rng = rand::thread_rng();
(0..n).map(|_| rng.gen()).collect()
}
#[test]
#[ignore]
fn test_blind_rotate() {
let d = rand_distr::Uniform::new(0, i32::max_value());
let mut rng = rand::thread_rng();
let key = generate_random_key(SMOL_N);
let bara: Vec<i32> = (0..SMOL_N)
/* NOTE: This actually said BIG_N,
but that doesn't make sense as it's greater than SMOL_N
and will cause a panic in crate::numerics::torus_polynomial_mul_by_xai_minus_one;*/
.map(|_| (d.sample(&mut rng) % (2 * SMOL_N as i32)))
.collect();
let accum_params = TLweParameters::new(SMOL_N as i32, 1, ALPHA_BK, 1f64 / 16f64);
let bk_params = TGswParams::new(L_BK, BG_BIT_BK, accum_params.clone());
let bk: Vec<TGswSample> = (0..SMOL_N).map(|_| TGswSample::new(&bk_params)).collect();
let mut expected_accum_message = TorusPolynomial::uniform(BIG_N as usize);
let init_accum_message = expected_accum_message.clone();
let init_alpha_accum = 0.2;
let mut accum = TLweSample::new(&accum_params);
accum.current_variance = init_alpha_accum * init_alpha_accum;
let mut expected_offset = 0;
// for i in 0..SMOL_N as usize {
// accum = tfhe_blind_rotate(accum, &bk[i..], &bara[i..], 1, &bk_params);
// if key[i] == true && bara[i] != 0 {
// expected_offset = (expected_offset + bara[i]) % ((2 * BIG_N) as i32);
// expected_accum_message = torus_polynomial_mul_by_xai(expected_offset, &init_accum_message);
// }
// expected_accum_message
// .coefs
// .iter()
// .zip(accum.b.coefs.iter())
// .for_each(|(a, b)| assert_eq!(a, b))
// }
// Now, bootstraprotate: all iterations at once (same offset)
// torusPolynomialCopy(faccum->message, initAccumMessage);
// faccum->current_variance = initAlphaAccum * initAlphaAccum;
// accum = tfhe_blind_rotate(accum, &bk, &bara, SMOL_N as i32, &bk_params);
// expected_accum_message
// .coefs
// .iter()
// .zip(accum.b.coefs.iter())
// .for_each(|(a, b)| assert_eq!(a, b));
}
}