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
use crate::ciphertext::RadixCiphertext;
use crate::ServerKey;
impl ServerKey {
/// Computes homomorphically a right shift.
///
/// The result is returned as a new ciphertext.
///
/// # Example
///
/// ```rust
/// use concrete_integer::gen_keys_radix;
/// use concrete_shortint::parameters::PARAM_MESSAGE_2_CARRY_2;
///
/// // We have 4 * 2 = 8 bits of message
/// let size = 4;
/// let (cks, sks) = gen_keys_radix(&PARAM_MESSAGE_2_CARRY_2, size);
///
/// let msg = 128;
/// let shift = 2;
///
/// let ct = cks.encrypt(msg);
///
/// // Compute homomorphically a right shift:
/// let ct_res = sks.unchecked_scalar_right_shift_parallelized(&ct, shift);
///
/// // Decrypt:
/// let dec = cks.decrypt(&ct_res);
/// assert_eq!(msg >> shift, dec);
/// ```
pub fn unchecked_scalar_right_shift_parallelized(
&self,
ct: &RadixCiphertext,
shift: usize,
) -> RadixCiphertext {
let mut result = ct.clone();
self.unchecked_scalar_right_shift_assign_parallelized(&mut result, shift);
result
}
/// Computes homomorphically a right shift.
///
/// The result is returned as a new ciphertext.
///
/// # Example
///
/// ```rust
/// use concrete_integer::gen_keys_radix;
/// use concrete_shortint::parameters::PARAM_MESSAGE_2_CARRY_2;
///
/// // We have 4 * 2 = 8 bits of message
/// let size = 4;
/// let (cks, sks) = gen_keys_radix(&PARAM_MESSAGE_2_CARRY_2, size);
///
/// let msg = 18;
/// let shift = 4;
///
/// let mut ct = cks.encrypt(msg);
///
/// // Compute homomorphically a right shift:
/// sks.unchecked_scalar_right_shift_assign_parallelized(&mut ct, shift);
///
/// // Decrypt:
/// let dec = cks.decrypt(&ct);
/// assert_eq!(msg >> shift, dec);
/// ```
pub fn unchecked_scalar_right_shift_assign_parallelized(
&self,
ct: &mut RadixCiphertext,
shift: usize,
) {
let tmp = self.key.message_modulus.0 as f64;
//number of bits of message
let nb_bits = tmp.log2() as usize;
// 2^u = 2^{p*q+r} = 2^{p*(q+1)}*2^{r-p}
let quotient = shift / nb_bits;
//p-r
let modified_remainder = nb_bits - (shift % nb_bits);
//if r == 0
if modified_remainder == nb_bits {
self.full_propagate_parallelized(ct);
self.blockshift_right_assign(ct, quotient as usize);
} else {
// B/2^u = (B*2^{p-r}) / (2^{p*(q+1)})
self.unchecked_scalar_left_shift_assign_parallelized(ct, modified_remainder);
// We partially propagate in order to not lose information
self.partial_propagate_parallelized(ct);
self.blockshift_right_assign(ct, 1_usize);
// We propagate the last block in order to not lose information
self.propagate_parallelized(ct, ct.blocks.len() - 2);
self.blockshift_right_assign(ct, quotient as usize);
}
}
/// Propagates all carries except the last one.
/// For development purpose only.
fn partial_propagate_parallelized(&self, ctxt: &mut RadixCiphertext) {
let len = ctxt.blocks.len() - 1;
for i in 0..len {
self.propagate_parallelized(ctxt, i);
}
}
/// Computes homomorphically a left shift by a scalar.
///
/// The result is returned as a new ciphertext.
///
/// # Example
///
/// ```rust
/// use concrete_integer::gen_keys_radix;
/// use concrete_shortint::parameters::PARAM_MESSAGE_2_CARRY_2;
///
/// // We have 4 * 2 = 8 bits of message
/// let size = 4;
/// let (cks, sks) = gen_keys_radix(&PARAM_MESSAGE_2_CARRY_2, size);
///
/// let msg = 21;
/// let shift = 2;
///
/// let ct1 = cks.encrypt(msg);
///
/// // Compute homomorphically a right shift:
/// let ct_res = sks.unchecked_scalar_left_shift_parallelized(&ct1, shift);
///
/// // Decrypt:
/// let dec = cks.decrypt(&ct_res);
/// assert_eq!(msg << shift, dec);
/// ```
pub fn unchecked_scalar_left_shift_parallelized(
&self,
ct_left: &RadixCiphertext,
shift: usize,
) -> RadixCiphertext {
let mut result = ct_left.clone();
self.unchecked_scalar_left_shift_assign_parallelized(&mut result, shift);
result
}
/// Computes homomorphically a left shift by a scalar.
///
/// The result is assigned in the input ciphertext
///
/// # Example
///
/// ```rust
/// use concrete_integer::gen_keys_radix;
/// use concrete_shortint::parameters::PARAM_MESSAGE_2_CARRY_2;
///
/// // We have 4 * 2 = 8 bits of message
/// let size = 4;
/// let (cks, sks) = gen_keys_radix(&PARAM_MESSAGE_2_CARRY_2, size);
///
/// let msg = 13;
/// let shift = 2;
///
/// let mut ct = cks.encrypt(msg);
///
/// // Compute homomorphically a right shift:
/// sks.unchecked_scalar_left_shift_assign_parallelized(&mut ct, shift);
///
/// // Decrypt:
/// let dec = cks.decrypt(&ct);
/// assert_eq!(msg << shift, dec);
/// ```
pub fn unchecked_scalar_left_shift_assign_parallelized(
&self,
ct: &mut RadixCiphertext,
shift: usize,
) {
let tmp = 1_u64 << shift;
self.smart_scalar_mul_assign_parallelized(ct, tmp);
}
}