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
use crate::integer::block_decomposition::{BlockDecomposer, DecomposableInto};
use crate::integer::ciphertext::IntegerRadixCiphertext;
use crate::integer::server_key::radix::scalar_mul::ScalarMultiplier;
use crate::integer::ServerKey;
use rayon::prelude::*;
impl ServerKey {
pub fn unchecked_scalar_mul_parallelized<T, Scalar>(&self, ct: &T, scalar: Scalar) -> T
where
T: IntegerRadixCiphertext,
Scalar: ScalarMultiplier + DecomposableInto<u8>,
{
let mut ct_res = ct.clone();
self.unchecked_scalar_mul_assign_parallelized(&mut ct_res, scalar);
ct_res
}
pub fn unchecked_scalar_mul_assign_parallelized<T, Scalar>(&self, lhs: &mut T, scalar: Scalar)
where
T: IntegerRadixCiphertext,
Scalar: ScalarMultiplier + DecomposableInto<u8>,
{
if scalar == Scalar::ZERO || lhs.blocks().is_empty() {
for block in lhs.blocks_mut() {
self.key.create_trivial_assign(block, 0);
}
return;
}
if scalar == Scalar::ONE {
return;
}
if scalar.is_power_of_two() {
// Shifting cost one bivariate PBS so its always faster
// than multiplying
self.unchecked_scalar_left_shift_assign_parallelized(lhs, scalar.ilog2() as u64);
return;
}
let num_blocks = lhs.blocks().len();
let msg_bits = self.key.message_modulus.0.ilog2() as usize;
let scalar_bits = BlockDecomposer::with_early_stop_at_zero(scalar, 1)
.iter_as::<u8>()
.collect::<Vec<_>>();
// We don't want to compute shifts if we are not going to use the
// resulting value
let mut has_at_least_one_set = vec![false; msg_bits];
for (i, bit) in scalar_bits.iter().copied().enumerate() {
if bit == 1 {
has_at_least_one_set[i % msg_bits] = true;
}
}
// Contains all shifted values of lhs for shift in range (0..msg_bits)
// The idea is that with these we can create all other shift that are in
// range (0..total_bits) for free (block rotation)
let preshifted_lhs = (0..msg_bits)
.into_par_iter()
.map(|shift_amount| {
if has_at_least_one_set[shift_amount] {
self.unchecked_scalar_left_shift_parallelized(lhs, shift_amount)
} else {
self.create_trivial_zero_radix(num_blocks)
}
})
.collect::<Vec<_>>();
let num_ciphertext_bits = msg_bits * num_blocks;
let all_shifted_lhs = scalar_bits
.iter()
.enumerate()
.take(num_ciphertext_bits) // shift beyond that are technically resulting in 0s
.filter(|(_, &rhs_bit)| rhs_bit == 1)
.map(|(i, _)| self.blockshift(&preshifted_lhs[i % msg_bits], i / msg_bits))
.collect::<Vec<_>>();
if let Some(result) = self.unchecked_sum_ciphertexts_vec_parallelized(all_shifted_lhs) {
*lhs = result;
} else {
self.create_trivial_zero_assign_radix(lhs);
}
}
/// Computes homomorphically a multiplication between a scalar and a ciphertext.
///
///
/// # Example
///
/// ```rust
/// use tfhe::integer::gen_keys_radix;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128;
///
/// // We have 4 * 2 = 8 bits of message
/// let modulus = 1 << 8;
/// let size = 4;
/// let (cks, sks) = gen_keys_radix(PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, size);
///
/// let msg = 230;
/// let scalar = 376;
///
/// let mut ct = cks.encrypt(msg);
///
/// // Compute homomorphically a scalar multiplication:
/// let ct_res = sks.smart_scalar_mul_parallelized(&mut ct, scalar);
///
/// // Decrypt:
/// let clear: u64 = cks.decrypt(&ct_res);
/// assert_eq!(msg * scalar % modulus, clear);
/// ```
pub fn smart_scalar_mul_parallelized<T, Scalar>(&self, lhs: &mut T, scalar: Scalar) -> T
where
T: IntegerRadixCiphertext,
Scalar: ScalarMultiplier + DecomposableInto<u8>,
{
if !lhs.block_carries_are_empty() {
self.full_propagate_parallelized(lhs);
}
self.unchecked_scalar_mul_parallelized(lhs, scalar)
}
pub fn smart_scalar_mul_assign_parallelized<T, Scalar>(&self, lhs: &mut T, scalar: Scalar)
where
T: IntegerRadixCiphertext,
Scalar: ScalarMultiplier + DecomposableInto<u8>,
{
if !lhs.block_carries_are_empty() {
self.full_propagate_parallelized(lhs);
}
self.unchecked_scalar_mul_assign_parallelized(lhs, scalar);
}
/// Computes homomorphically a multiplication between a scalar and a ciphertext.
///
/// This function, like all "default" operations (i.e. not smart, checked or unchecked), will
/// check that the input ciphertexts block carries are empty and clears them if it's not the
/// case and the operation requires it. It outputs a ciphertext whose block carries are always
/// empty.
///
/// This means that when using only "default" operations, a given operation (like add for
/// example) has always the same performance characteristics from one call to another and
/// guarantees correctness by pre-emptively clearing carries of output ciphertexts.
///
/// # Example
///
/// ```rust
/// use tfhe::integer::gen_keys_radix;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128;
///
/// // We have 4 * 2 = 8 bits of message
/// let modulus = 1 << 8;
/// let size = 4;
/// let (cks, sks) = gen_keys_radix(PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, size);
///
/// let msg = 230;
/// let scalar = 376;
///
/// let ct = cks.encrypt(msg);
///
/// // Compute homomorphically a scalar multiplication:
/// let ct_res = sks.scalar_mul_parallelized(&ct, scalar);
///
/// // Decrypt:
/// let clear: u64 = cks.decrypt(&ct_res);
/// assert_eq!(msg * scalar % modulus, clear);
/// ```
pub fn scalar_mul_parallelized<T, Scalar>(&self, ct: &T, scalar: Scalar) -> T
where
T: IntegerRadixCiphertext,
Scalar: ScalarMultiplier + DecomposableInto<u8>,
{
let mut ct_res = ct.clone();
self.scalar_mul_assign_parallelized(&mut ct_res, scalar);
ct_res
}
pub fn scalar_mul_assign_parallelized<T, Scalar>(&self, lhs: &mut T, scalar: Scalar)
where
T: IntegerRadixCiphertext,
Scalar: ScalarMultiplier + DecomposableInto<u8>,
{
if !lhs.block_carries_are_empty() {
self.full_propagate_parallelized(lhs);
}
self.unchecked_scalar_mul_assign_parallelized(lhs, scalar);
}
}