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
use crate::core_crypto::gpu::CudaStreams;
use crate::integer::gpu::ciphertext::boolean_value::CudaBooleanBlock;
use crate::integer::gpu::ciphertext::{
CudaIntegerRadixCiphertext, CudaSignedRadixCiphertext, CudaUnsignedRadixCiphertext,
};
use crate::integer::gpu::cuda_backend_unchecked_negate;
use crate::integer::gpu::server_key::CudaServerKey;
use crate::integer::server_key::radix_parallel::OutputFlag;
impl CudaServerKey {
/// Homomorphically computes the opposite of a ciphertext encrypting an integer message.
///
/// This function computes the opposite of a message without checking if it exceeds the
/// capacity of the ciphertext.
///
/// The result is returned as a new ciphertext.
///
/// # Example
///
/// ```rust
/// // Encrypt two messages:
/// use tfhe::core_crypto::gpu::CudaStreams;
/// use tfhe::core_crypto::gpu::vec::GpuIndex;
/// use tfhe::integer::gpu::ciphertext::CudaUnsignedRadixCiphertext;
/// use tfhe::integer::gpu::gen_keys_radix_gpu;
/// use tfhe::shortint::parameters::PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128;
///
/// let gpu_index = 0;
/// let streams = CudaStreams::new_single_gpu(GpuIndex::new(gpu_index));
///
/// // We have 4 * 2 = 8 bits of message
/// let size = 4;
/// let modulus = 1 << 8;
/// let (cks, sks) = gen_keys_radix_gpu(PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, size, &streams);
///
/// let msg = 159u64;
///
/// // Encrypt a message
/// let ctxt = cks.encrypt(msg);
/// let d_ctxt = CudaUnsignedRadixCiphertext::from_radix_ciphertext(&ctxt, &streams);
///
/// // Compute homomorphically a negation
/// let d_res = sks.unchecked_neg(&d_ctxt, &streams);
/// let res = d_res.to_radix_ciphertext(&streams);
///
/// // Decrypt
/// let dec: u64 = cks.decrypt(&res);
/// assert_eq!(modulus - msg, dec);
/// ```
pub fn unchecked_neg<T: CudaIntegerRadixCiphertext>(
&self,
ctxt: &T,
streams: &CudaStreams,
) -> T {
let mut ciphertext_out = ctxt.duplicate(streams);
let info = ctxt.as_ref().info.blocks.first().unwrap();
unsafe {
cuda_backend_unchecked_negate(
streams,
ciphertext_out.as_mut(),
ctxt.as_ref(),
info.message_modulus.0 as u32,
info.carry_modulus.0 as u32,
);
}
ciphertext_out
}
/// Homomorphically computes the opposite of a ciphertext encrypting an integer message.
///
/// This function computes the opposite of a message without checking if it exceeds the
/// capacity of the ciphertext.
///
/// The result is returned as a new ciphertext.
///
/// # Example
///
/// ```rust
/// // Encrypt two messages:
/// use tfhe::core_crypto::gpu::CudaStreams;
/// use tfhe::core_crypto::gpu::vec::GpuIndex;
/// use tfhe::integer::gpu::ciphertext::CudaUnsignedRadixCiphertext;
/// use tfhe::integer::gpu::gen_keys_radix_gpu;
/// use tfhe::shortint::parameters::PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128;
///
/// let gpu_index = 0;
/// let streams = CudaStreams::new_single_gpu(GpuIndex::new(gpu_index));
///
/// // We have 4 * 2 = 8 bits of message
/// let size = 4;
/// let modulus = 1 << 8;
/// let (cks, sks) = gen_keys_radix_gpu(PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, size, &streams);
///
/// let msg = 159u64;
///
/// // Encrypt a message
/// let ctxt = cks.encrypt(msg);
/// let d_ctxt = CudaUnsignedRadixCiphertext::from_radix_ciphertext(&ctxt, &streams);
///
/// // Compute homomorphically a negation
/// let d_res = sks.neg(&d_ctxt, &streams);
/// let res = d_res.to_radix_ciphertext(&streams);
///
/// // Decrypt
/// let dec: u64 = cks.decrypt(&res);
/// assert_eq!(modulus - msg, dec);
/// ```
pub fn neg<T: CudaIntegerRadixCiphertext>(&self, ctxt: &T, streams: &CudaStreams) -> T {
let mut tmp_ctxt;
let ct = if ctxt.block_carries_are_empty() {
ctxt
} else {
tmp_ctxt = ctxt.duplicate(streams);
self.full_propagate_assign(&mut tmp_ctxt, streams);
&mut tmp_ctxt
};
let mut res = self.unchecked_neg(ct, streams);
let _carry = self.propagate_single_carry_assign(&mut res, streams, None, OutputFlag::None);
res
}
pub fn get_neg_size_on_gpu<T: CudaIntegerRadixCiphertext>(
&self,
ctxt: &T,
streams: &CudaStreams,
) -> u64 {
self.get_scalar_add_size_on_gpu(ctxt, streams)
}
pub fn overflowing_neg<T>(&self, ctxt: &T, streams: &CudaStreams) -> (T, CudaBooleanBlock)
where
T: CudaIntegerRadixCiphertext,
{
let mut ct = if ctxt.block_carries_are_empty() {
ctxt.duplicate(streams)
} else {
let mut ct = ctxt.duplicate(streams);
self.full_propagate_assign(&mut ct, streams);
ct
};
self.bitnot_assign(&mut ct, streams);
if T::IS_SIGNED {
let tmp = CudaSignedRadixCiphertext {
ciphertext: ct.into_inner(),
};
let (result, overflowed) = self.signed_overflowing_scalar_add(&tmp, 1, streams);
let result = T::from(result.into_inner());
(result, overflowed)
} else {
let mut tmp = CudaUnsignedRadixCiphertext {
ciphertext: ct.into_inner(),
};
let mut overflowed = self.unsigned_overflowing_scalar_add_assign(&mut tmp, 1, streams);
self.unchecked_boolean_bitnot_assign(&mut overflowed, streams);
let result = T::from(tmp.into_inner());
(result, overflowed)
}
}
}