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
use crate::{CrtCiphertext, ServerKey};
impl ServerKey {
/// Computes homomorphically a subtraction between two ciphertexts encrypting integer values.
///
/// This function computes the subtraction without checking if it exceeds the capacity of the
/// ciphertext.
///
/// The result is returned as a new ciphertext.
/// # Example
///
///```rust
/// use concrete_integer::gen_keys;
/// use concrete_shortint::parameters::PARAM_MESSAGE_2_CARRY_2;
///
/// // Generate the client key and the server key:
/// let (cks, sks) = gen_keys(&PARAM_MESSAGE_2_CARRY_2);
///
/// let clear_1 = 14;
/// let clear_2 = 5;
/// let basis = vec![2, 3, 5];
/// // Encrypt two messages
/// let mut ctxt_1 = cks.encrypt_crt(clear_1, basis.clone());
/// let mut ctxt_2 = cks.encrypt_crt(clear_2, basis.clone());
///
/// let ctxt = sks.unchecked_crt_sub(&mut ctxt_1, &mut ctxt_2);
///
/// // Decrypt
/// let res = cks.decrypt_crt(&ctxt);
/// assert_eq!((clear_1 - clear_2) % 30, res);
/// ```
pub fn unchecked_crt_sub(
&self,
ctxt_left: &CrtCiphertext,
ctxt_right: &CrtCiphertext,
) -> CrtCiphertext {
let mut result = ctxt_left.clone();
self.unchecked_crt_sub_assign(&mut result, ctxt_right);
result
}
/// Computes homomorphically a subtraction between two ciphertexts encrypting integer values.
///
/// This function computes the subtraction without checking if it exceeds the capacity of the
/// ciphertext.
///
/// The result is assigned to the `ct_left` ciphertext.
/// # Example
///
///```rust
/// use concrete_integer::gen_keys;
/// use concrete_shortint::parameters::PARAM_MESSAGE_2_CARRY_2;
///
/// // Generate the client key and the server key:
/// let (cks, sks) = gen_keys(&PARAM_MESSAGE_2_CARRY_2);
///
/// let clear_1 = 14;
/// let clear_2 = 5;
/// let basis = vec![2, 3, 5];
/// // Encrypt two messages
/// let mut ctxt_1 = cks.encrypt_crt(clear_1, basis.clone());
/// let mut ctxt_2 = cks.encrypt_crt(clear_2, basis.clone());
///
/// let ctxt = sks.unchecked_crt_sub(&mut ctxt_1, &mut ctxt_2);
///
/// // Decrypt
/// let res = cks.decrypt_crt(&ctxt);
/// assert_eq!((clear_1 - clear_2) % 30, res);
/// ```
pub fn unchecked_crt_sub_assign(
&self,
ctxt_left: &mut CrtCiphertext,
ctxt_right: &CrtCiphertext,
) {
let neg = self.unchecked_crt_neg(ctxt_right);
self.unchecked_crt_add_assign(ctxt_left, &neg);
}
/// Computes homomorphically the subtraction between ct_left and ct_right.
///
/// # Example
///
///```rust
/// use concrete_integer::gen_keys;
/// use concrete_shortint::parameters::PARAM_MESSAGE_2_CARRY_2;
///
/// // Generate the client key and the server key:
/// let (cks, sks) = gen_keys(&PARAM_MESSAGE_2_CARRY_2);
///
/// let clear_1 = 14;
/// let clear_2 = 5;
/// let basis = vec![2, 3, 5];
/// // Encrypt two messages
/// let mut ctxt_1 = cks.encrypt_crt(clear_1, basis.clone());
/// let mut ctxt_2 = cks.encrypt_crt(clear_2, basis.clone());
///
/// let ctxt = sks.smart_crt_sub(&mut ctxt_1, &mut ctxt_2);
///
/// // Decrypt
/// let res = cks.decrypt_crt(&ctxt);
/// assert_eq!((clear_1 - clear_2) % 30, res);
/// ```
pub fn smart_crt_sub(
&self,
ctxt_left: &mut CrtCiphertext,
ctxt_right: &mut CrtCiphertext,
) -> CrtCiphertext {
// If the ciphertext cannot be added together without exceeding the capacity of a ciphertext
if !self.is_crt_sub_possible(ctxt_left, ctxt_right) {
self.full_extract(ctxt_left);
self.full_extract(ctxt_right);
}
let mut result = ctxt_left.clone();
self.unchecked_crt_sub_assign(&mut result, ctxt_right);
result
}
/// Computes homomorphically the subtraction between ct_left and ct_right.
///
/// # Example
///
///```rust
/// use concrete_integer::gen_keys;
/// use concrete_shortint::parameters::PARAM_MESSAGE_2_CARRY_2;
///
/// // Generate the client key and the server key:
/// let (cks, sks) = gen_keys(&PARAM_MESSAGE_2_CARRY_2);
///
/// let clear_1 = 14;
/// let clear_2 = 5;
/// let basis = vec![2, 3, 5];
/// // Encrypt two messages
/// let mut ctxt_1 = cks.encrypt_crt(clear_1, basis.clone());
/// let mut ctxt_2 = cks.encrypt_crt(clear_2, basis.clone());
///
/// sks.smart_crt_sub_assign(&mut ctxt_1, &mut ctxt_2);
///
/// // Decrypt
/// let res = cks.decrypt_crt(&ctxt_1);
/// assert_eq!((clear_1 - clear_2) % 30, res);
/// ```
pub fn smart_crt_sub_assign(
&self,
ctxt_left: &mut CrtCiphertext,
ctxt_right: &mut CrtCiphertext,
) {
// If the ciphertext cannot be added together without exceeding the capacity of a ciphertext
if !self.is_crt_sub_possible(ctxt_left, ctxt_right) {
self.full_extract(ctxt_left);
self.full_extract(ctxt_right);
}
self.unchecked_crt_sub_assign(ctxt_left, ctxt_right);
}
pub fn is_crt_sub_possible(
&self,
ctxt_left: &CrtCiphertext,
ctxt_right: &CrtCiphertext,
) -> bool {
for (ct_left_i, ct_right_i) in ctxt_left.blocks.iter().zip(ctxt_right.blocks.iter()) {
if !self.key.is_sub_possible(ct_left_i, ct_right_i) {
return false;
}
}
true
}
}