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
use super::ServerKey;
use crate::shortint::ciphertext::Degree;
use crate::shortint::Ciphertext;
impl ServerKey {
/// Compute homomorphically a bitwise AND between a ciphertext and a clear value
///
///
/// # 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 msg1 = 3u64;
/// let msg2 = 2u64;
///
/// // Encrypt two messages:
/// let ct1 = cks.encrypt(msg1);
///
/// // Compute homomorphically an AND:
/// let ct_res = sks.scalar_bitand(&ct1, msg2 as u8);
///
/// // Decrypt:
/// let res = cks.decrypt(&ct_res);
/// assert_eq!(msg1 & msg2, res);
/// ```
pub fn scalar_bitand(&self, lhs: &Ciphertext, rhs: u8) -> Ciphertext {
let mut ct_res = lhs.clone();
self.scalar_bitand_assign(&mut ct_res, rhs);
ct_res
}
pub fn scalar_bitand_assign(&self, lhs: &mut Ciphertext, rhs: u8) {
if !lhs.carry_is_empty() {
self.message_extract_assign(lhs);
}
self.unchecked_scalar_bitand_assign(lhs, rhs);
}
pub fn unchecked_scalar_bitand(&self, lhs: &Ciphertext, rhs: u8) -> Ciphertext {
let mut result = lhs.clone();
self.unchecked_scalar_bitand_assign(&mut result, rhs);
result
}
pub fn unchecked_scalar_bitand_assign(&self, lhs: &mut Ciphertext, rhs: u8) {
let new_degree = lhs.degree.after_bitand(Degree::new(rhs as usize));
self.evaluate_msg_univariate_function_assign(lhs, |x| x & rhs as u64);
lhs.degree = new_degree;
}
#[allow(clippy::needless_pass_by_ref_mut)]
pub fn smart_scalar_bitand(&self, lhs: &mut Ciphertext, rhs: u8) -> Ciphertext {
let mut result = lhs.clone();
self.smart_scalar_bitand_assign(&mut result, rhs);
result
}
pub fn smart_scalar_bitand_assign(&self, lhs: &mut Ciphertext, rhs: u8) {
self.unchecked_scalar_bitand_assign(lhs, rhs);
}
/// Compute homomorphically a bitwise XOR between a ciphertext and a clear value
///
///
/// # 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 msg1 = 3u64;
/// let msg2 = 2u64;
///
/// // Encrypt two messages:
/// let ct1 = cks.encrypt(msg1);
///
/// // Compute homomorphically a XOR:
/// let ct_res = sks.scalar_bitxor(&ct1, msg2 as u8);
///
/// // Decrypt:
/// let res = cks.decrypt(&ct_res);
/// assert_eq!(msg1 ^ msg2, res);
/// ```
pub fn scalar_bitxor(&self, lhs: &Ciphertext, rhs: u8) -> Ciphertext {
let mut ct_res = lhs.clone();
self.scalar_bitxor_assign(&mut ct_res, rhs);
ct_res
}
pub fn scalar_bitxor_assign(&self, lhs: &mut Ciphertext, rhs: u8) {
if !lhs.carry_is_empty() {
self.message_extract_assign(lhs);
}
self.unchecked_scalar_bitxor_assign(lhs, rhs);
}
pub fn unchecked_scalar_bitxor(&self, lhs: &Ciphertext, rhs: u8) -> Ciphertext {
let mut result = lhs.clone();
self.unchecked_scalar_bitxor_assign(&mut result, rhs);
result
}
pub fn unchecked_scalar_bitxor_assign(&self, lhs: &mut Ciphertext, rhs: u8) {
let new_degree = lhs.degree.after_bitxor(Degree::new(rhs as usize));
self.evaluate_msg_univariate_function_assign(lhs, |x| x ^ rhs as u64);
lhs.degree = new_degree;
}
#[allow(clippy::needless_pass_by_ref_mut)]
pub fn smart_scalar_bitxor(&self, lhs: &mut Ciphertext, rhs: u8) -> Ciphertext {
let mut result = lhs.clone();
self.smart_scalar_bitxor_assign(&mut result, rhs);
result
}
pub fn smart_scalar_bitxor_assign(&self, lhs: &mut Ciphertext, rhs: u8) {
self.unchecked_scalar_bitxor_assign(lhs, rhs);
}
/// Compute homomorphically a bitwise OR between a ciphertext and a clear value
///
///
/// # 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 msg1 = 3u64;
/// let msg2 = 2u64;
///
/// // Encrypt two messages:
/// let ct1 = cks.encrypt(msg1);
///
/// // Compute homomorphically an OR:
/// let ct_res = sks.scalar_bitor(&ct1, msg2 as u8);
///
/// // Decrypt:
/// let res: u64 = cks.decrypt(&ct_res);
/// assert_eq!(msg1 | msg2, res);
/// ```
pub fn scalar_bitor(&self, lhs: &Ciphertext, rhs: u8) -> Ciphertext {
let mut ct_res = lhs.clone();
self.scalar_bitor_assign(&mut ct_res, rhs);
ct_res
}
pub fn scalar_bitor_assign(&self, lhs: &mut Ciphertext, rhs: u8) {
if !lhs.carry_is_empty() {
self.message_extract_assign(lhs);
}
self.unchecked_scalar_bitor_assign(lhs, rhs);
}
pub fn unchecked_scalar_bitor(&self, lhs: &Ciphertext, rhs: u8) -> Ciphertext {
let mut result = lhs.clone();
self.unchecked_scalar_bitor_assign(&mut result, rhs);
result
}
pub fn unchecked_scalar_bitor_assign(&self, lhs: &mut Ciphertext, rhs: u8) {
let new_degree = lhs.degree.after_bitor(Degree::new(rhs as usize));
self.evaluate_msg_univariate_function_assign(lhs, |x| x | rhs as u64);
lhs.degree = new_degree;
}
#[allow(clippy::needless_pass_by_ref_mut)]
pub fn smart_scalar_bitor(&self, lhs: &mut Ciphertext, rhs: u8) -> Ciphertext {
let mut result = lhs.clone();
self.smart_scalar_bitor_assign(&mut result, rhs);
result
}
pub fn smart_scalar_bitor_assign(&self, lhs: &mut Ciphertext, rhs: u8) {
self.unchecked_scalar_bitor_assign(lhs, rhs);
}
}