poulpy-ckks 0.6.0

A backend-agnostic crate implementing the CKKS FHE scheme
Documentation
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
use anyhow::Result;
use poulpy_core::layouts::{
    GGLWEInfos, GLWEInfos, GLWETensorKeyPrepared, GLWEToBackendMut, GLWEToBackendRef, LWEInfos,
    prepared::GLWETensorKeyPreparedToBackendRef,
};
use poulpy_hal::layouts::{Backend, Data, ScratchArena};

use crate::{
    CKKSCtBounds, CKKSInfos,
    layouts::{CKKSCiphertext, UnnormalizedCKKSCiphertext},
};

/// Tree-reduction sum over a slice of ciphertexts.
pub trait CKKSAddManyOps<BE: Backend> {
    fn ckks_add_many_tmp_bytes(&self) -> usize;

    /// Computes `dst = inputs[0] + inputs[1] + … + inputs[n-1]` using a
    /// balanced binary tree of pairwise additions.
    ///
    /// # Metadata
    ///
    /// Follows from repeated application of `ckks_add_into`:
    ///
    /// ```text
    /// log_delta_out  = min over all inputs of log_delta
    /// log_budget_out = min over all inputs of log_budget
    /// ```
    ///
    /// No capacity is consumed.  The destination-capacity offset from the
    /// final write applies:
    ///
    /// ```text
    /// offset         = max(0, result_effective_k − dst.max_k())
    /// log_budget_out = (min log_budget) − offset
    /// ```
    ///
    /// Errors if `inputs` is empty.
    fn ckks_add_many<Dst: Data, Src: Data>(
        &self,
        dst: &mut CKKSCiphertext<Dst>,
        inputs: &[&CKKSCiphertext<Src>],
        scratch: &mut ScratchArena<'_, BE>,
    ) -> Result<()>
    where
        CKKSCiphertext<Dst>: GLWEToBackendMut<BE>,
        CKKSCiphertext<Src>: GLWEToBackendRef<BE>;
}

/// Fused multiply-accumulate: `dst += a * b`.
///
/// Each variant computes the product of two operands and adds it to `dst`
/// without a separate allocation for the intermediate product.
pub trait CKKSMulAddOps<BE: Backend> {
    fn ckks_mul_add_ct_tmp_bytes<R, T>(&self, res: &R, tsk: &T) -> usize
    where
        R: CKKSCtBounds,
        T: GGLWEInfos;

    fn ckks_mul_add_pt_vec_tmp_bytes<R, A, P>(&self, res: &R, a: &A, b: &P) -> usize
    where
        R: CKKSCtBounds,
        A: CKKSCtBounds,
        P: CKKSInfos;

    fn ckks_mul_add_pt_const_tmp_bytes<R, A, P>(&self, res: &R, a: &A, b: &P) -> usize
    where
        R: CKKSCtBounds,
        A: CKKSCtBounds,
        P: CKKSInfos;

    /// Computes `dst += a * b` using tensor-product keyswitching via `tsk`.
    ///
    /// # Metadata
    ///
    /// First the product `a * b` is computed (see [`CKKSMulOps`](crate::api::CKKSMulOps)
    /// for its metadata rule), then that product is added into `dst`.
    /// The final metadata is the result of the addition:
    ///
    /// ```text
    /// // product metadata (capacity consumed by the mul):
    /// prod_delta  = min(a.log_delta, b.log_delta)
    /// prod_budget = min(a.log_budget, b.log_budget) − max(a.log_delta, b.log_delta)
    ///
    /// // addition with dst:
    /// log_delta_out  = min(dst.log_delta, prod_delta)
    /// log_budget_out = min(dst.log_budget, prod_budget)
    ///                  − max(0, min(dst.effective_k(), prod_effective_k) − dst.max_k())
    /// ```
    fn ckks_mul_add_ct_into<Dst: Data, A: Data, B: Data, T: Data>(
        &self,
        dst: &mut CKKSCiphertext<Dst>,
        a: &CKKSCiphertext<A>,
        b: &CKKSCiphertext<B>,
        tsk: &GLWETensorKeyPrepared<T, BE>,
        scratch: &mut ScratchArena<'_, BE>,
    ) -> Result<()>
    where
        CKKSCiphertext<Dst>: GLWEToBackendMut<BE>,
        CKKSCiphertext<A>: GLWEToBackendRef<BE> + LWEInfos + GLWEInfos,
        CKKSCiphertext<B>: GLWEToBackendRef<BE> + LWEInfos + GLWEInfos,
        GLWETensorKeyPrepared<T, BE>: GLWETensorKeyPreparedToBackendRef<BE>;

    /// Computes `dst += a * pt` where `pt` is a full plaintext polynomial.
    ///
    /// # Metadata
    ///
    /// ```text
    /// // product metadata:
    /// prod_delta  = a.log_delta
    /// prod_budget = a.log_budget − pt.log_delta
    ///
    /// // addition with dst:
    /// log_delta_out  = min(dst.log_delta, prod_delta)
    /// log_budget_out = min(dst.log_budget, prod_budget)
    ///                  − max(0, min(dst.effective_k(), prod_effective_k) − dst.max_k())
    /// ```
    fn ckks_mul_add_pt_vec_into<Dst: Data, A: Data, P>(
        &self,
        dst: &mut CKKSCiphertext<Dst>,
        a: &CKKSCiphertext<A>,
        pt: &P,
        scratch: &mut ScratchArena<'_, BE>,
    ) -> Result<()>
    where
        CKKSCiphertext<Dst>: GLWEToBackendMut<BE>,
        CKKSCiphertext<A>: GLWEToBackendRef<BE> + LWEInfos + GLWEInfos,
        P: GLWEToBackendRef<BE> + CKKSCtBounds;

    /// Computes `dst += a * pt[pt_coeff]`.
    ///
    /// Metadata follows the same rule as [`Self::ckks_mul_add_pt_vec_into`].
    fn ckks_mul_add_pt_const_into<Dst: Data, A: Data, P>(
        &self,
        dst: &mut CKKSCiphertext<Dst>,
        a: &CKKSCiphertext<A>,
        pt: &P,
        pt_coeff: usize,
        scratch: &mut ScratchArena<'_, BE>,
    ) -> Result<()>
    where
        CKKSCiphertext<Dst>: GLWEToBackendMut<BE>,
        CKKSCiphertext<A>: GLWEToBackendRef<BE> + LWEInfos + GLWEInfos,
        P: GLWEToBackendRef<BE> + CKKSCtBounds;

    /// Computes `dst += a * pt[pt_coeff]` without normalizing `dst`.
    ///
    /// The accumulator `dst` carries un-propagated carries in its limb digits.
    /// Use this to fuse several multiply-add steps before a single
    /// [`UnnormalizedCKKSCiphertext::normalize`] call.  See
    /// [`crate::api::CKKSAddOpsUnnormalized`] for the digit-growth analysis
    /// and safety bound.
    fn ckks_mul_add_pt_const_into_unnormalized<Dst: Data, A, P>(
        &self,
        dst: &mut UnnormalizedCKKSCiphertext<Dst>,
        a: &A,
        pt: &P,
        pt_coeff: usize,
        scratch: &mut ScratchArena<'_, BE>,
    ) -> Result<()>
    where
        UnnormalizedCKKSCiphertext<Dst>: GLWEToBackendMut<BE>,
        A: GLWEToBackendRef<BE> + CKKSCtBounds,
        P: GLWEToBackendRef<BE> + CKKSCtBounds;

    /// Computes `dst += a * pt` without normalizing `dst`.
    ///
    /// Metadata follows the same rule as
    /// [`Self::ckks_mul_add_pt_const_into_unnormalized`].
    fn ckks_mul_add_pt_vec_into_unnormalized<Dst: Data, A, P>(
        &self,
        dst: &mut UnnormalizedCKKSCiphertext<Dst>,
        a: &A,
        pt: &P,
        scratch: &mut ScratchArena<'_, BE>,
    ) -> Result<()>
    where
        UnnormalizedCKKSCiphertext<Dst>: GLWEToBackendMut<BE>,
        A: GLWEToBackendRef<BE> + CKKSCtBounds,
        P: GLWEToBackendRef<BE> + CKKSCtBounds;
}

/// Fused affine evaluation: `dst = a * scale + offset`.
///
/// Combines a plaintext multiplication followed by a plaintext addition.
/// Both `scale` and `offset` must be CKKS plaintexts compatible with the
/// ciphertext's remaining capacity.
pub trait CKKSAffineOps<BE: Backend> {
    fn ckks_affine_pt_const_tmp_bytes<R, A, P>(&self, res: &R, a: &A, affine_const: &P) -> usize
    where
        R: CKKSCtBounds,
        A: CKKSCtBounds,
        P: CKKSInfos;

    /// Computes `dst = a * affine_const[scale_coeff] + affine_const[offset_coeff]`.
    ///
    /// Reads two quantized scalar constants from `affine_const`: one for the
    /// multiplicative scale (at `scale_coeff`) and one for the additive offset
    /// (at `offset_coeff`).  The offset is always applied to ZNX coefficient 0
    /// of `dst` (the real-slot constant term).  Use
    /// [`CKKSAddOps::ckks_add_pt_const_into`](crate::api::CKKSAddOps::ckks_add_pt_const_into)
    /// directly if you need to target a different destination coefficient.
    ///
    /// # Metadata
    ///
    /// The operation is `mul` then `add`.  The `add` step does not consume
    /// capacity, so the net cost is from the `mul` step only:
    ///
    /// ```text
    /// natural_eff_k  = a.effective_k() − affine_const.log_delta
    /// offset         = max(0, natural_eff_k − dst.max_k())
    ///
    /// log_delta_out  = a.log_delta
    /// log_budget_out = a.log_budget − affine_const.log_delta − offset
    /// ```
    ///
    /// **Net capacity consumed**: `affine_const.log_delta + offset` bits.
    fn ckks_affine_pt_const_into<Dst, A, P>(
        &self,
        dst: &mut Dst,
        a: &A,
        affine_const: &P,
        offset_coeff: usize,
        scale_coeff: usize,
        scratch: &mut ScratchArena<'_, BE>,
    ) -> Result<()>
    where
        Dst: GLWEToBackendMut<BE> + CKKSCtBounds + crate::SetCKKSInfos,
        A: GLWEToBackendRef<BE> + CKKSCtBounds,
        P: GLWEToBackendRef<BE> + CKKSCtBounds;

    /// Computes `dst = dst * affine_const[scale_coeff] + affine_const[offset_coeff]` in-place.
    ///
    /// For an in-place operation `dst` acts as both source and destination.
    /// No destination-capacity offset applies:
    ///
    /// ```text
    /// log_delta_out  = dst.log_delta
    /// log_budget_out = dst.log_budget − affine_const.log_delta
    /// ```
    fn ckks_affine_pt_const_assign<Dst, P>(
        &self,
        dst: &mut Dst,
        affine_const: &P,
        offset_coeff: usize,
        scale_coeff: usize,
        scratch: &mut ScratchArena<'_, BE>,
    ) -> Result<()>
    where
        Dst: GLWEToBackendMut<BE> + GLWEToBackendRef<BE> + CKKSCtBounds + crate::SetCKKSInfos,
        P: GLWEToBackendRef<BE> + CKKSCtBounds;

    fn ckks_affine_pt_vec_tmp_bytes<R, A, S>(&self, res: &R, a: &A, scale: &S) -> usize
    where
        R: CKKSCtBounds,
        A: CKKSCtBounds,
        S: CKKSInfos;

    /// Computes `dst = a * scale + offset` where `scale` and `offset` are full
    /// plaintext polynomials in the ZNX domain.
    ///
    /// # Metadata
    ///
    /// The `add offset` step does not consume capacity; the cost comes from the
    /// `mul scale` step:
    ///
    /// ```text
    /// natural_eff_k  = a.effective_k() − scale.log_delta
    /// offset_bits    = max(0, natural_eff_k − dst.max_k())
    ///
    /// log_delta_out  = a.log_delta
    /// log_budget_out = a.log_budget − scale.log_delta − offset_bits
    /// ```
    ///
    /// **Net capacity consumed**: `scale.log_delta + offset_bits` bits.
    ///
    /// **Precondition**: `(a.log_budget − scale.log_delta) + offset.log_delta >= offset.effective_k()`
    /// (the offset plaintext must fit in the ciphertext headroom after the multiply).
    fn ckks_affine_pt_vec_into<Dst, A, S, P>(
        &self,
        dst: &mut Dst,
        a: &A,
        scale: &S,
        offset: &P,
        scratch: &mut ScratchArena<'_, BE>,
    ) -> Result<()>
    where
        Dst: GLWEToBackendMut<BE> + CKKSCtBounds + crate::SetCKKSInfos,
        A: GLWEToBackendRef<BE> + CKKSCtBounds,
        S: GLWEToBackendRef<BE> + CKKSCtBounds,
        P: GLWEToBackendRef<BE> + CKKSCtBounds;

    /// Computes `dst = dst * scale + offset` in-place.
    ///
    /// For an in-place operation no destination-capacity offset applies:
    ///
    /// ```text
    /// log_delta_out  = dst.log_delta
    /// log_budget_out = dst.log_budget − scale.log_delta
    /// ```
    fn ckks_affine_pt_vec_assign<Dst, S, P>(
        &self,
        dst: &mut Dst,
        scale: &S,
        offset: &P,
        scratch: &mut ScratchArena<'_, BE>,
    ) -> Result<()>
    where
        Dst: GLWEToBackendMut<BE> + GLWEToBackendRef<BE> + CKKSCtBounds + crate::SetCKKSInfos,
        S: GLWEToBackendRef<BE> + CKKSCtBounds,
        P: GLWEToBackendRef<BE> + CKKSCtBounds;
}

/// Fused multiply-subtract: `dst -= a * b`.
pub trait CKKSMulSubOps<BE: Backend> {
    fn ckks_mul_sub_ct_tmp_bytes<R, T>(&self, res: &R, tsk: &T) -> usize
    where
        R: CKKSCtBounds,
        T: GGLWEInfos;

    fn ckks_mul_sub_pt_vec_tmp_bytes<R, A, P>(&self, res: &R, a: &A, b: &P) -> usize
    where
        R: CKKSCtBounds,
        A: CKKSCtBounds,
        P: CKKSInfos;

    fn ckks_mul_sub_pt_const_tmp_bytes<R, A, P>(&self, res: &R, a: &A, b: &P) -> usize
    where
        R: CKKSCtBounds,
        A: CKKSCtBounds,
        P: CKKSInfos;

    /// Computes `dst -= a * b` using tensor-product keyswitching via `tsk`.
    ///
    /// Metadata follows the same rule as
    /// [`CKKSMulAddOps::ckks_mul_add_ct_into`] with `+=` replaced by `-=`.
    fn ckks_mul_sub_ct_into<Dst: Data, A: Data, B: Data, T: Data>(
        &self,
        dst: &mut CKKSCiphertext<Dst>,
        a: &CKKSCiphertext<A>,
        b: &CKKSCiphertext<B>,
        tsk: &GLWETensorKeyPrepared<T, BE>,
        scratch: &mut ScratchArena<'_, BE>,
    ) -> Result<()>
    where
        CKKSCiphertext<Dst>: GLWEToBackendMut<BE>,
        CKKSCiphertext<A>: GLWEToBackendRef<BE> + LWEInfos + GLWEInfos,
        CKKSCiphertext<B>: GLWEToBackendRef<BE> + LWEInfos + GLWEInfos,
        GLWETensorKeyPrepared<T, BE>: GLWETensorKeyPreparedToBackendRef<BE>;

    /// Computes `dst -= a * pt` where `pt` is a full plaintext polynomial.
    ///
    /// Metadata follows the same rule as
    /// [`CKKSMulAddOps::ckks_mul_add_pt_vec_into`].
    fn ckks_mul_sub_pt_vec_into<Dst: Data, A: Data, P>(
        &self,
        dst: &mut CKKSCiphertext<Dst>,
        a: &CKKSCiphertext<A>,
        pt: &P,
        scratch: &mut ScratchArena<'_, BE>,
    ) -> Result<()>
    where
        CKKSCiphertext<Dst>: GLWEToBackendMut<BE>,
        CKKSCiphertext<A>: GLWEToBackendRef<BE> + LWEInfos + GLWEInfos,
        P: GLWEToBackendRef<BE> + CKKSCtBounds;

    /// Computes `dst -= a * pt[pt_coeff]`.
    ///
    /// Metadata follows the same rule as
    /// [`CKKSMulAddOps::ckks_mul_add_pt_const_into`].
    fn ckks_mul_sub_pt_const_into<Dst: Data, A: Data, P>(
        &self,
        dst: &mut CKKSCiphertext<Dst>,
        a: &CKKSCiphertext<A>,
        pt: &P,
        pt_coeff: usize,
        scratch: &mut ScratchArena<'_, BE>,
    ) -> Result<()>
    where
        CKKSCiphertext<Dst>: GLWEToBackendMut<BE>,
        CKKSCiphertext<A>: GLWEToBackendRef<BE> + LWEInfos + GLWEInfos,
        P: GLWEToBackendRef<BE> + CKKSCtBounds;
}

/// Inner product (dot product) of ciphertext and plaintext slices.
///
/// Computes the weighted sum `dst = Σ a[i] * b[i]` over all pairs.
pub trait CKKSDotProductOps<BE: Backend> {
    fn ckks_dot_product_ct_tmp_bytes<R, T>(&self, n: usize, res: &R, tsk: &T) -> usize
    where
        R: CKKSCtBounds,
        T: GGLWEInfos;

    fn ckks_dot_product_pt_vec_tmp_bytes<R, A, P>(&self, res: &R, a: &A, b: &P) -> usize
    where
        R: CKKSCtBounds,
        A: CKKSCtBounds,
        P: CKKSInfos;

    fn ckks_dot_product_pt_const_tmp_bytes<R, A, P>(&self, res: &R, a: &A, b: &P) -> usize
    where
        R: CKKSCtBounds,
        A: CKKSCtBounds,
        P: CKKSInfos;

    /// Computes `dst = Σ a[i] * b[i]` over ciphertext–ciphertext pairs.
    ///
    /// # Metadata
    ///
    /// Equivalent to a sequence of `ckks_mul_add_ct_into` calls.  With
    /// uniform-precision inputs:
    ///
    /// ```text
    /// natural_eff_k  = a[i].log_budget   (= a[i].effective_k() − a[i].log_delta)
    /// offset         = max(0, natural_eff_k − dst.max_k())
    ///
    /// log_delta_out  = a[i].log_delta
    /// log_budget_out = a[i].log_budget − a[i].log_delta − offset
    /// ```
    fn ckks_dot_product_ct<Dst: Data, D: Data, E: Data, T: Data>(
        &self,
        dst: &mut CKKSCiphertext<Dst>,
        a: &[&CKKSCiphertext<D>],
        b: &[&CKKSCiphertext<E>],
        tsk: &GLWETensorKeyPrepared<T, BE>,
        scratch: &mut ScratchArena<'_, BE>,
    ) -> Result<()>
    where
        CKKSCiphertext<Dst>: GLWEToBackendMut<BE>,
        CKKSCiphertext<D>: GLWEToBackendRef<BE> + LWEInfos + GLWEInfos,
        CKKSCiphertext<E>: GLWEToBackendRef<BE> + LWEInfos + GLWEInfos,
        GLWETensorKeyPrepared<T, BE>: GLWETensorKeyPreparedToBackendRef<BE>;

    /// Computes `dst = Σ a[i] * b[i]` over ciphertext–plaintext-polynomial pairs.
    ///
    /// # Metadata
    ///
    /// ```text
    /// natural_eff_k  = a[i].effective_k() − b[i].log_delta
    /// offset         = max(0, natural_eff_k − dst.max_k())
    ///
    /// log_delta_out  = a[i].log_delta
    /// log_budget_out = a[i].log_budget − b[i].log_delta − offset
    /// ```
    fn ckks_dot_product_pt_vec<Dst: Data, D: Data, E>(
        &self,
        dst: &mut CKKSCiphertext<Dst>,
        a: &[&CKKSCiphertext<D>],
        b: &[&E],
        scratch: &mut ScratchArena<'_, BE>,
    ) -> Result<()>
    where
        CKKSCiphertext<Dst>: GLWEToBackendMut<BE>,
        CKKSCiphertext<D>: GLWEToBackendRef<BE> + LWEInfos + GLWEInfos,
        E: GLWEToBackendRef<BE> + CKKSCtBounds;

    /// Computes `dst = Σ a[i] * b[i][pt_coeffs[i]]` over ciphertext–scalar-constant pairs.
    ///
    /// Each `b[i]` contributes a single ZNX coefficient `pt_coeffs[i]`.
    /// Metadata follows the same rule as [`Self::ckks_dot_product_pt_vec`].
    fn ckks_dot_product_pt_const<Dst: Data, D: Data, E>(
        &self,
        dst: &mut CKKSCiphertext<Dst>,
        a: &[&CKKSCiphertext<D>],
        b: &[&E],
        pt_coeffs: &[usize],
        scratch: &mut ScratchArena<'_, BE>,
    ) -> Result<()>
    where
        CKKSCiphertext<Dst>: GLWEToBackendMut<BE>,
        CKKSCiphertext<D>: GLWEToBackendRef<BE> + LWEInfos + GLWEInfos,
        E: GLWEToBackendRef<BE> + CKKSCtBounds;
}