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
use crate::shortint::atomic_pattern::AtomicPattern;
use crate::shortint::ciphertext::Degree;
use crate::shortint::server_key::GenericServerKey;
use crate::shortint::Ciphertext;
impl<AP: AtomicPattern> GenericServerKey<AP> {
/// Alias to [`unchecked_scalar_div`](`Self::unchecked_scalar_div`) provided for convenience
///
/// This function, like all "default" operations (i.e. not smart, checked or unchecked), will
/// check that the input ciphertext carries are empty and clears them if it's not the case and
/// the operation requires it. It outputs a ciphertext whose carry is 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.
///
/// # Panics
///
/// This function will panic if `scalar == 0`.
pub fn scalar_div(&self, ct_left: &Ciphertext, scalar: u8) -> Ciphertext {
self.unchecked_scalar_div(ct_left, scalar)
}
/// Alias to [`unchecked_scalar_div_assign`](`Self::unchecked_scalar_div_assign`) provided for
/// convenience
///
/// This function, like all "default" operations (i.e. not smart, checked or unchecked), will
/// check that the input ciphertext carries are empty and clears them if it's not the case and
/// the operation requires it. It outputs a ciphertext whose carry is 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.
///
/// # Panics
///
/// This function will panic if `scalar == 0`.
pub fn scalar_div_assign(&self, ct_left: &mut Ciphertext, scalar: u8) {
self.unchecked_scalar_div_assign(ct_left, scalar);
}
/// Compute a division of a ciphertext by a scalar without checks.
///
/// # Panics
///
/// This function will panic if `scalar == 0`.
///
/// # Example
///
///```rust
/// use tfhe::shortint::gen_keys;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS;
///
/// // Generate the client key and the server key
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS);
///
/// let clear_1 = 3;
/// let clear_2 = 2;
///
/// // Encrypt one message
/// let ct_1 = cks.encrypt(clear_1);
///
/// // Compute homomorphically a multiplication
/// let ct_res = sks.unchecked_scalar_div(&ct_1, clear_2);
///
/// // Decrypt
/// let res = cks.decrypt(&ct_res);
/// assert_eq!(clear_1 / (clear_2 as u64), res);
/// ```
pub fn unchecked_scalar_div(&self, ct: &Ciphertext, scalar: u8) -> Ciphertext {
let mut result = ct.clone();
self.unchecked_scalar_div_assign(&mut result, scalar);
result
}
pub fn unchecked_scalar_div_assign(&self, ct: &mut Ciphertext, scalar: u8) {
assert_ne!(scalar, 0, "attempt to divide by zero");
let scalar = u64::from(scalar);
let lookup_table = self.generate_msg_lookup_table(|x| x / scalar, ct.message_modulus);
self.apply_lookup_table_assign(ct, &lookup_table);
ct.degree = Degree::new(ct.degree.get() / scalar);
}
/// Alias to [`unchecked_scalar_mod`](`Self::unchecked_scalar_mod`) provided for convenience
///
/// This function, like all "default" operations (i.e. not smart, checked or unchecked), will
/// check that the input ciphertext carries are empty and clears them if it's not the case and
/// the operation requires it. It outputs a ciphertext whose carry is 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.
///
/// # Panics
///
/// This function will panic if `scalar == 0`.
pub fn scalar_mod(&self, ct_left: &Ciphertext, scalar: u8) -> Ciphertext {
self.unchecked_scalar_mod(ct_left, scalar)
}
/// Alias to [`unchecked_scalar_mod_assign`](`Self::unchecked_scalar_mod_assign`) provided for
/// convenience
///
/// This function, like all "default" operations (i.e. not smart, checked or unchecked), will
/// check that the input ciphertext carries are empty and clears them if it's not the case and
/// the operation requires it. It outputs a ciphertext whose carry is 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.
///
/// # Panics
///
/// This function will panic if `scalar == 0`.
pub fn scalar_mod_assign(&self, ct_left: &mut Ciphertext, scalar: u8) {
self.unchecked_scalar_mod_assign(ct_left, scalar);
}
/// Compute homomorphically a modular reduction without checks.
///
/// # Panics
///
/// This function will panic if `modulus == 0`.
///
/// # Example
///
/// ```rust
/// use tfhe::shortint::gen_keys;
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS;
///
/// // Generate the client key and the server key:
/// let (cks, sks) = gen_keys(PARAM_MESSAGE_2_CARRY_2_KS_PBS);
///
/// let msg = 3;
///
/// let ct = cks.encrypt(msg);
///
/// let modulus: u8 = 2;
/// // Compute homomorphically an addition:
/// let ct_res = sks.unchecked_scalar_mod(&ct, modulus);
///
/// // Decrypt:
/// let dec = cks.decrypt(&ct_res);
/// assert_eq!(1, dec);
/// ```
pub fn unchecked_scalar_mod(&self, ct: &Ciphertext, modulus: u8) -> Ciphertext {
let mut result = ct.clone();
self.unchecked_scalar_mod_assign(&mut result, modulus);
result
}
pub fn unchecked_scalar_mod_assign(&self, ct: &mut Ciphertext, modulus: u8) {
assert_ne!(modulus, 0);
let modulus = u64::from(modulus);
let acc = self.generate_msg_lookup_table(|x| x % modulus, ct.message_modulus);
self.apply_lookup_table_assign(ct, &acc);
ct.degree = Degree::new(modulus - 1);
}
}