Skip to main content

poulpy_hal/api/
module.rs

1use crate::layouts::{Backend, MatZnx, ScalarZnx, VecZnx};
2
3/// Instantiate a new [crate::layouts::Module].
4pub trait ModuleNew<B: Backend> {
5    /// Backend-specific construction parameters; see
6    /// [`HalModuleImpl::Config`](crate::oep::HalModuleImpl::Config).
7    type Config: Default = ();
8
9    fn new(n: u64) -> Self;
10
11    /// Instantiates under an explicit configuration (device selection, ...).
12    fn new_with(n: u64, config: Self::Config) -> Self;
13}
14
15/// Query the maximum ring degree `N` of a [`Module`](crate::layouts::Module).
16pub trait ModuleN {
17    fn n(&self) -> usize;
18}
19
20/// Query `log2(N)` with a default implementation derived from [`ModuleN::n`].
21pub trait ModuleLogN
22where
23    Self: ModuleN,
24{
25    fn log_n(&self) -> usize {
26        (u64::BITS - (self.n() as u64 - 1).leading_zeros()) as usize
27    }
28}
29
30/// Allocates backend-owned [`ScalarZnx`](crate::layouts::ScalarZnx) layouts.
31pub trait ScalarZnxAlloc<B: Backend>: ModuleN {
32    fn scalar_znx_alloc(&self, cols: usize) -> ScalarZnx<B::OwnedBuf, B::ZnxWord>;
33}
34
35/// Allocates backend-owned [`VecZnx`](crate::layouts::VecZnx) layouts.
36pub trait VecZnxAlloc<B: Backend>: ModuleN {
37    fn vec_znx_alloc(&self, cols: usize, size: usize) -> VecZnx<B::OwnedBuf, B::ZnxWord>;
38}
39
40/// Allocates backend-owned [`MatZnx`](crate::layouts::MatZnx) layouts.
41pub trait MatZnxAlloc<B: Backend>: ModuleN {
42    fn mat_znx_alloc(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> MatZnx<B::OwnedBuf, B::ZnxWord>;
43}