poulpy-hal 0.8.1

A crate providing layouts and a trait-based hardware acceleration layer with open extension points, matching the API and types of spqlios-arithmetic.
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
use std::{
    fmt,
    hash::{DefaultHasher, Hasher},
    marker::PhantomData,
};

use crate::layouts::{
    Backend, Data, DataView, DataViewMut, DftWord, DigestU64, HostDataMut, HostDataRef, VecZnxBig, VecZnxInfos, VecZnxShape,
    ZnxInfos, ZnxView, ZnxViewMut, ZnxZero,
};

/// Polynomial vector in DFT (evaluation) domain.
///
/// `VecZnxDft` has the same structural shape as [`VecZnx`](crate::layouts::VecZnx)
/// but stores coefficients as [`DftWord`] values in the frequency domain
/// rather than [`ZnxWord`](crate::layouts::ZnxWord) values in the coefficient
/// domain.
///
/// The word type `W` names the byte-layout convention of the buffer (see
/// [`DftWord`]); the backend `B` pins which implementation produced it.
/// Containers of different backends are distinct types even when their words
/// match — cross-backend movement is explicit, via the zero-copy re-tag
/// guarded by the layout-compatibility markers or via a transfer/re-prepare.
///
/// Multiplication and scalar-vector/vector-matrix products are performed
/// in this representation to exploit FFT-based convolution. Use
/// [`VecZnxDftApply`](crate::api::VecZnxDftApply) /
/// [`VecZnxIdftApply`](crate::api::VecZnxIdftApply) to convert
/// between coefficient and DFT domains.
#[repr(C)]
pub struct VecZnxDft<D: Data, W: DftWord, B: Backend<DftWord = W>> {
    pub data: D,
    shape: VecZnxShape,
    pub _phantom: PhantomData<(W, B)>,
}

// Equality (and hashing, where provided) is defined directly on the
// representation: same shape, same buffer bytes. No `W`/`B` value is ever
// compared, so no bound on them is needed — in particular `Eq` holds even
// for non-`Eq` words like `f64` (byte equality is a total equivalence).
impl<D: Data, W: DftWord, B: Backend<DftWord = W>> PartialEq for VecZnxDft<D, W, B> {
    fn eq(&self, other: &Self) -> bool {
        self.shape == other.shape && self.data == other.data
    }
}

impl<D: Data, W: DftWord, B: Backend<DftWord = W>> Eq for VecZnxDft<D, W, B> {}

impl<D: HostDataRef, W: DftWord, B: Backend<DftWord = W>> DigestU64 for VecZnxDft<D, W, B> {
    fn digest_u64(&self) -> u64 {
        let mut h: DefaultHasher = DefaultHasher::new();
        h.write(self.data.as_ref());
        h.write_usize(self.n());
        h.write_usize(self.cols());
        h.write_usize(self.size());
        h.finish()
    }
}

impl<D: HostDataRef, W: DftWord, B: Backend<DftWord = W>> ZnxView for VecZnxDft<D, W, B> {
    type Scalar = W;
}

impl<D: Data, W: DftWord, B: Backend<DftWord = W>> VecZnxDft<D, W, B> {
    pub fn n(&self) -> usize {
        self.shape.n()
    }

    pub fn cols(&self) -> usize {
        self.shape.cols()
    }

    pub fn size(&self) -> usize {
        self.shape.size()
    }

    /// Reinterprets this DFT vector as a [`VecZnxBig`], consuming `self`.
    ///
    /// This is a zero-copy conversion that changes only the type tag; the
    /// underlying data buffer is moved as-is. The backend `B` declares both
    /// words, which guarantees the buffer is large enough for the big-domain
    /// interpretation.
    pub fn into_big(self) -> VecZnxBig<D, B::BigWord, B> {
        let shape = self.shape;
        assert!(
            B::bytes_of_vec_znx_big(shape.n(), shape.cols(), shape.size())
                <= B::bytes_of_vec_znx_dft(shape.n(), shape.cols(), shape.size()),
            "into_big: big-domain buffer would exceed the DFT-domain allocation"
        );
        VecZnxBig::<D, B::BigWord, B>::from_data(self.data, shape.n(), shape.cols(), shape.size())
    }
}

impl<D: Data, W: DftWord, B: Backend<DftWord = W>> ZnxInfos for VecZnxDft<D, W, B> {
    fn n(&self) -> usize {
        self.shape.n()
    }

    fn size(&self) -> usize {
        self.shape.size()
    }

    fn poly_count(&self) -> usize {
        crate::layouts::checked_product(&[self.cols(), self.size()], "polynomial count")
    }
}

impl<D: Data, W: DftWord, B: Backend<DftWord = W>> VecZnxInfos for VecZnxDft<D, W, B> {
    fn cols(&self) -> usize {
        self.shape.cols()
    }
}

impl<D: Data, W: DftWord, B: Backend<DftWord = W>> DataView for VecZnxDft<D, W, B> {
    type D = D;
    fn data(&self) -> &Self::D {
        &self.data
    }
}

impl<D: Data, W: DftWord, B: Backend<DftWord = W>> DataViewMut for VecZnxDft<D, W, B> {
    fn data_mut(&mut self) -> &mut Self::D {
        &mut self.data
    }
}

impl<D: Data, W: DftWord, B: Backend<DftWord = W>> VecZnxDft<D, W, B> {
    pub fn shape(&self) -> VecZnxShape {
        self.shape
    }

    /// Whether the backend's physical representation can be viewed as `W`
    /// elements. Packed backends may use `W` only as an identity marker.
    fn has_element_view(&self) -> bool {
        let element_bytes = crate::layouts::element_view_span(self)
            .checked_mul(size_of::<W>())
            .expect("VecZnxDft element-view byte size overflows usize");
        element_bytes == B::bytes_of_vec_znx_dft(self.n(), self.cols(), self.size())
    }
}

impl<'b, B: Backend + 'b> VecZnxDftBackendMut<'b, B> {
    /// Reborrows this buffer as a mutable view with a temporary compute size.
    ///
    /// The returned view addresses the same allocation, but HAL
    /// kernels see `size` as the active limb count. Dropping the view leaves
    /// `self`'s metadata unchanged.
    ///
    /// # Panics
    ///
    /// Panics if `size > self.size()`.
    pub fn with_size_mut(&mut self, size: usize) -> VecZnxDftBackendMut<'_, B> {
        self.with_limb_range_mut(0, size)
    }

    /// Reborrows a contiguous range of DFT limbs as a mutable vector.
    ///
    /// The returned view contains limbs `start..end` for every column and
    /// renumbers `start` as limb zero. Dropping it leaves `self` unchanged.
    /// This is the typed alternative to manually slicing the backend buffer.
    ///
    /// # Panics
    ///
    /// Panics unless `start <= end <= self.size()`.
    pub fn with_limb_range_mut(&mut self, start: usize, end: usize) -> VecZnxDftBackendMut<'_, B> {
        assert!(start <= end, "DFT limb range start ({start}) exceeds end ({end})");
        assert!(
            end <= self.size(),
            "DFT limb range end ({end}) exceeds size ({})",
            self.size()
        );

        let n = self.n();
        let cols = self.cols();
        let offset = B::bytes_of_vec_znx_dft(n, cols, start);
        let len = B::bytes_of_vec_znx_dft(n, cols, end - start);
        VecZnxDft {
            data: B::region_mut_ref(&mut self.data, offset, len),
            shape: VecZnxShape::new(n, cols, end - start),
            _phantom: PhantomData,
        }
    }
}

impl<D: HostDataMut, W: DftWord, B: Backend<DftWord = W>> ZnxZero for VecZnxDft<D, W, B> {
    fn zero(&mut self) {
        if self.has_element_view() {
            self.raw_mut().fill(W::zero());
            return;
        }

        let byte_len = B::bytes_of_vec_znx_dft(self.n(), self.cols(), self.size());
        let data = self.data.as_mut();
        assert!(
            byte_len <= data.len(),
            "VecZnxDft backend representation ({byte_len} bytes) exceeds the {}-byte buffer",
            data.len()
        );
        data[..byte_len].fill(0);
    }

    fn zero_at(&mut self, i: usize, j: usize) {
        if self.has_element_view() {
            self.at_mut(i, j).fill(W::zero());
            return;
        }

        assert!(i < self.cols(), "cols: {} >= self.cols(): {}", i, self.cols());
        assert!(j < self.size(), "size: {} >= self.size(): {}", j, self.size());

        let block_bytes = B::bytes_of_vec_znx_dft(self.n(), 1, 1);
        let byte_len = crate::layouts::checked_product(&[block_bytes, self.cols(), self.size()], "VecZnxDft packed byte size");
        assert_eq!(
            byte_len,
            B::bytes_of_vec_znx_dft(self.n(), self.cols(), self.size()),
            "VecZnxDft backend representation is not block-linear"
        );

        let block = j
            .checked_mul(self.cols())
            .and_then(|x| x.checked_add(i))
            .expect("VecZnxDft packed block index overflows usize");
        let offset = block
            .checked_mul(block_bytes)
            .expect("VecZnxDft packed block offset overflows usize");
        let end = offset
            .checked_add(block_bytes)
            .expect("VecZnxDft packed block end overflows usize");
        let data = self.data.as_mut();
        assert!(
            end <= data.len(),
            "VecZnxDft packed block ({i}, {j}) exceeds the {}-byte buffer",
            data.len()
        );
        data[offset..end].fill(0);
    }
}

impl<B: Backend> VecZnxDft<B::OwnedBuf, B::DftWord, B> {
    /// Allocates a zero-initialized backend-owned `VecZnxDft`.
    pub fn alloc(n: usize, cols: usize, size: usize) -> VecZnxDftOwned<B> {
        let data: <B as Backend>::OwnedBuf = B::alloc_zeroed_bytes(B::bytes_of_vec_znx_dft(n, cols, size));
        VecZnxDft {
            data,
            shape: VecZnxShape::new(n, cols, size),
            _phantom: PhantomData,
        }
    }

    /// Uploads a host byte buffer into a backend-owned `VecZnxDft`.
    ///
    /// # Panics
    ///
    /// Panics if the buffer length does not equal `B::bytes_of_vec_znx_dft(n, cols, size)`.
    pub fn from_bytes(n: usize, cols: usize, size: usize, bytes: impl Into<Vec<u8>>) -> VecZnxDftOwned<B> {
        let data: Vec<u8> = bytes.into();
        assert!(data.len() == B::bytes_of_vec_znx_dft(n, cols, size));
        let data: <B as Backend>::OwnedBuf = B::from_host_bytes(&data);
        VecZnxDft {
            data,
            shape: VecZnxShape::new(n, cols, size),
            _phantom: PhantomData,
        }
    }
}

/// Owned `VecZnxDft` backed by a backend-owned buffer.
pub type VecZnxDftOwned<B> = VecZnxDft<<B as Backend>::OwnedBuf, <B as Backend>::DftWord, B>;
/// Shared backend-native borrow of a `VecZnxDft`.
pub type VecZnxDftBackendRef<'a, B> = VecZnxDft<<B as Backend>::BufRef<'a>, <B as Backend>::DftWord, B>;
/// Mutable backend-native borrow of a `VecZnxDft`.
pub type VecZnxDftBackendMut<'a, B> = VecZnxDft<<B as Backend>::BufMut<'a>, <B as Backend>::DftWord, B>;

/// Reborrow a mutable backend-native `VecZnxDft` view as a shared backend-native view.
pub fn vec_znx_dft_backend_ref_from_mut<'a, 'b, B: Backend + 'b>(
    vec: &'a VecZnxDftBackendMut<'b, B>,
) -> VecZnxDftBackendRef<'a, B> {
    VecZnxDft {
        data: B::view_ref_mut(&vec.data),
        shape: vec.shape,
        _phantom: PhantomData,
    }
}

pub fn vec_znx_dft_backend_mut_from_mut<'a, 'b, B: Backend + 'b>(
    vec: &'a mut VecZnxDftBackendMut<'b, B>,
) -> VecZnxDftBackendMut<'a, B> {
    VecZnxDft {
        data: B::view_mut_ref(&mut vec.data),
        shape: vec.shape,
        _phantom: PhantomData,
    }
}

impl<D: Data, W: DftWord, B: Backend<DftWord = W>> VecZnxDft<D, W, B> {
    /// Constructs a `VecZnxDft` from raw parts without validation.
    pub fn from_data(data: D, n: usize, cols: usize, size: usize) -> Self {
        Self {
            data,
            shape: VecZnxShape::new(n, cols, size),
            _phantom: PhantomData,
        }
    }
}

/// Borrow a backend-owned `VecZnxDft` using the backend's native view type.
pub trait VecZnxDftToBackendRef<B: Backend> {
    fn to_backend_ref(&self) -> VecZnxDftBackendRef<'_, B>;
}

impl<B: Backend> VecZnxDftToBackendRef<B> for VecZnxDft<B::OwnedBuf, B::DftWord, B> {
    fn to_backend_ref(&self) -> VecZnxDftBackendRef<'_, B> {
        VecZnxDft {
            data: B::view(&self.data),
            shape: self.shape,
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<'b, B: Backend + 'b> VecZnxDftToBackendRef<B> for &VecZnxDft<B::BufRef<'b>, B::DftWord, B> {
    fn to_backend_ref(&self) -> VecZnxDftBackendRef<'_, B> {
        VecZnxDft {
            data: B::view_ref(&self.data),
            shape: self.shape,
            _phantom: std::marker::PhantomData,
        }
    }
}

/// Reborrow an already backend-borrowed `VecZnxDft` as a shared backend-native view.
pub trait VecZnxDftReborrowBackendRef<B: Backend> {
    fn reborrow_backend_ref(&self) -> VecZnxDftBackendRef<'_, B>;
}

impl<'b, B: Backend + 'b> VecZnxDftReborrowBackendRef<B> for VecZnxDft<B::BufMut<'b>, B::DftWord, B> {
    fn reborrow_backend_ref(&self) -> VecZnxDftBackendRef<'_, B> {
        vec_znx_dft_backend_ref_from_mut::<B>(self)
    }
}

/// Mutably borrow a backend-owned `VecZnxDft` using the backend's native view type.
pub trait VecZnxDftToBackendMut<B: Backend> {
    fn to_backend_mut(&mut self) -> VecZnxDftBackendMut<'_, B>;
}

impl<B: Backend> VecZnxDftToBackendMut<B> for VecZnxDft<B::OwnedBuf, B::DftWord, B> {
    fn to_backend_mut(&mut self) -> VecZnxDftBackendMut<'_, B> {
        VecZnxDft {
            data: B::view_mut(&mut self.data),
            shape: self.shape,
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<'b, B: Backend + 'b> VecZnxDftToBackendMut<B> for &mut VecZnxDft<B::BufMut<'b>, B::DftWord, B> {
    fn to_backend_mut(&mut self) -> VecZnxDftBackendMut<'_, B> {
        vec_znx_dft_backend_mut_from_mut::<B>(self)
    }
}

/// Reborrow an already backend-borrowed `VecZnxDft` as a mutable backend-native view.
pub trait VecZnxDftReborrowBackendMut<B: Backend> {
    fn reborrow_backend_mut(&mut self) -> VecZnxDftBackendMut<'_, B>;
}

impl<'b, B: Backend + 'b> VecZnxDftReborrowBackendMut<B> for VecZnxDft<B::BufMut<'b>, B::DftWord, B> {
    fn reborrow_backend_mut(&mut self) -> VecZnxDftBackendMut<'_, B> {
        vec_znx_dft_backend_mut_from_mut::<B>(self)
    }
}

impl<D: HostDataRef, W: DftWord, B: Backend<DftWord = W>> fmt::Display for VecZnxDft<D, W, B> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "VecZnxDft(n={}, cols={}, size={})", self.n(), self.cols(), self.size())?;

        if !self.has_element_view() {
            return writeln!(
                f,
                "  <backend-packed representation: {} bytes>",
                B::bytes_of_vec_znx_dft(self.n(), self.cols(), self.size())
            );
        }

        for col in 0..self.cols() {
            writeln!(f, "Column {col}:")?;
            for size in 0..self.size() {
                let coeffs = self.at(col, size);
                write!(f, "  Size {size}: [")?;

                let max_show = 100;
                let show_count = coeffs.len().min(max_show);

                for (i, &coeff) in coeffs.iter().take(show_count).enumerate() {
                    if i > 0 {
                        write!(f, ", ")?;
                    }
                    write!(f, "{coeff}")?;
                }

                if coeffs.len() > max_show {
                    write!(f, ", ... ({} more)", coeffs.len() - max_show)?;
                }

                writeln!(f, "]")?;
            }
        }
        Ok(())
    }
}

impl<D: Data, W: DftWord, B: Backend<DftWord = W>> VecZnxDft<D, W, B> {
    /// Zero-copy re-tag of this container to a layout-compatible backend `B2`.
    ///
    /// The buffer moves as-is; only the type tag changes. Requires the
    /// [`VecZnxDftLayoutCompatible`](crate::layouts::VecZnxDftLayoutCompatible) marker declared by the backend
    /// pair. `D` is kept, so for further backend-native use `B2`'s buffer
    /// types must match `D` (true for all current CPU backends).
    pub fn into_backend<B2>(self) -> VecZnxDft<D, W, B2>
    where
        B2: Backend<DftWord = W>,
        B: crate::layouts::VecZnxDftLayoutCompatible<B2>,
    {
        let shape = self.shape;
        assert_eq!(
            B::bytes_of_vec_znx_dft(shape.n(), shape.cols(), shape.size()),
            B2::bytes_of_vec_znx_dft(shape.n(), shape.cols(), shape.size()),
            "into_backend: byte sizes diverge despite declared layout compatibility"
        );
        VecZnxDft {
            data: self.data,
            shape,
            _phantom: PhantomData,
        }
    }
}

#[cfg(test)]
mod limb_range_tests {
    use super::*;
    use crate::layouts::{HostBytesBackend, VecZnxDftToBackendMut};

    #[test]
    fn mutable_limb_range_rebases_a_nonzero_start() {
        let (n, cols, size) = (4, 2, 4);
        let mut dft = VecZnxDft::<Vec<u8>, i64, HostBytesBackend>::alloc(n, cols, size);
        dft.data.fill(0xA5);

        {
            let mut backend = dft.to_backend_mut();
            let middle = backend.with_limb_range_mut(1, 3);
            assert_eq!((middle.n(), middle.cols(), middle.size()), (n, cols, 2));
            middle.data.fill(0);
        }

        let limb_bytes = HostBytesBackend::bytes_of_vec_znx_dft(n, cols, 1);
        assert!(dft.data[..limb_bytes].iter().all(|byte| *byte == 0xA5));
        assert!(dft.data[limb_bytes..3 * limb_bytes].iter().all(|byte| *byte == 0));
        assert!(dft.data[3 * limb_bytes..].iter().all(|byte| *byte == 0xA5));
    }
}