poulpy/base2k/src/vec_znx_big.rs
base2k/src/vec_znx_big.rsRaw
1use crate::ffi::vec_znx_big;
2use crate::znx_base::{ZnxInfos, ZnxView};
3use crate::{Backend, DataView, DataViewMut, FFT64, Module, ZnxSliceSize, alloc_aligned};
4use std::fmt;
5use std::marker::PhantomData;
6
7pub struct VecZnxBig<D, B: Backend> {
8 data: D,
9 n: usize,
10 cols: usize,
11 size: usize,
12 _phantom: PhantomData<B>,
13}
14
15impl<D, B: Backend> ZnxInfos for VecZnxBig<D, B> {
16 fn cols(&self) -> usize {
17 self.cols
18 }
19
20 fn rows(&self) -> usize {
21 1
22 }
23
24 fn n(&self) -> usize {
25 self.n
26 }
27
28 fn size(&self) -> usize {
29 self.size
30 }
31}
32
33impl<D> ZnxSliceSize for VecZnxBig<D, FFT64> {
34 fn sl(&self) -> usize {
35 self.n() * self.cols()
36 }
37}
38
39impl<D, B: Backend> DataView for VecZnxBig<D, B> {
40 type D = D;
41 fn data(&self) -> &Self::D {
42 &self.data
43 }
44}
45
46impl<D, B: Backend> DataViewMut for VecZnxBig<D, B> {
47 fn data_mut(&mut self) -> &mut Self::D {
48 &mut self.data
49 }
50}
51
52impl<D: AsRef<[u8]>> ZnxView for VecZnxBig<D, FFT64> {
53 type Scalar = i64;
54}
55
56pub(crate) fn bytes_of_vec_znx_big<B: Backend>(module: &Module<B>, cols: usize, size: usize) -> usize {
57 unsafe { vec_znx_big::bytes_of_vec_znx_big(module.ptr, size as u64) as usize * cols }
58}
59
60impl<D: From<Vec<u8>>, B: Backend> VecZnxBig<D, B> {
61 pub(crate) fn new(module: &Module<B>, cols: usize, size: usize) -> Self {
62 let data = alloc_aligned::<u8>(bytes_of_vec_znx_big(module, cols, size));
63 Self {
64 data: data.into(),
65 n: module.n(),
66 cols,
67 size,
68 _phantom: PhantomData,
69 }
70 }
71
72 pub(crate) fn new_from_bytes(module: &Module<B>, cols: usize, size: usize, bytes: impl Into<Vec<u8>>) -> Self {
73 let data: Vec<u8> = bytes.into();
74 assert!(data.len() == bytes_of_vec_znx_big(module, cols, size));
75 Self {
76 data: data.into(),
77 n: module.n(),
78 cols,
79 size,
80 _phantom: PhantomData,
81 }
82 }
83}
84
85impl<D, B: Backend> VecZnxBig<D, B> {
86 pub(crate) fn from_data(data: D, n: usize, cols: usize, size: usize) -> Self {
87 Self {
88 data,
89 n,
90 cols,
91 size,
92 _phantom: PhantomData,
93 }
94 }
95}
96
97pub type VecZnxBigOwned<B> = VecZnxBig<Vec<u8>, B>;
98
99pub trait VecZnxBigToRef<B: Backend> {
100 fn to_ref(&self) -> VecZnxBig<&[u8], B>;
101}
102
103pub trait VecZnxBigToMut<B: Backend> {
104 fn to_mut(&mut self) -> VecZnxBig<&mut [u8], B>;
105}
106
107impl<B: Backend> VecZnxBigToMut<B> for VecZnxBig<Vec<u8>, B> {
108 fn to_mut(&mut self) -> VecZnxBig<&mut [u8], B> {
109 VecZnxBig {
110 data: self.data.as_mut_slice(),
111 n: self.n,
112 cols: self.cols,
113 size: self.size,
114 _phantom: PhantomData,
115 }
116 }
117}
118
119impl<B: Backend> VecZnxBigToRef<B> for VecZnxBig<Vec<u8>, B> {
120 fn to_ref(&self) -> VecZnxBig<&[u8], B> {
121 VecZnxBig {
122 data: self.data.as_slice(),
123 n: self.n,
124 cols: self.cols,
125 size: self.size,
126 _phantom: PhantomData,
127 }
128 }
129}
130
131impl<B: Backend> VecZnxBigToMut<B> for VecZnxBig<&mut [u8], B> {
132 fn to_mut(&mut self) -> VecZnxBig<&mut [u8], B> {
133 VecZnxBig {
134 data: self.data,
135 n: self.n,
136 cols: self.cols,
137 size: self.size,
138 _phantom: PhantomData,
139 }
140 }
141}
142
143impl<B: Backend> VecZnxBigToRef<B> for VecZnxBig<&mut [u8], B> {
144 fn to_ref(&self) -> VecZnxBig<&[u8], B> {
145 VecZnxBig {
146 data: self.data,
147 n: self.n,
148 cols: self.cols,
149 size: self.size,
150 _phantom: PhantomData,
151 }
152 }
153}
154
155impl<B: Backend> VecZnxBigToRef<B> for VecZnxBig<&[u8], B> {
156 fn to_ref(&self) -> VecZnxBig<&[u8], B> {
157 VecZnxBig {
158 data: self.data,
159 n: self.n,
160 cols: self.cols,
161 size: self.size,
162 _phantom: PhantomData,
163 }
164 }
165}
166
167impl<D: AsRef<[u8]>> fmt::Display for VecZnxBig<D, FFT64> {
168 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
169 writeln!(
170 f,
171 "VecZnxBig(n={}, cols={}, size={})",
172 self.n, self.cols, self.size
173 )?;
174
175 for col in 0..self.cols {
176 writeln!(f, "Column {}:", col)?;
177 for size in 0..self.size {
178 let coeffs = self.at(col, size);
179 write!(f, " Size {}: [", size)?;
180
181 let max_show = 100;
182 let show_count = coeffs.len().min(max_show);
183
184 for (i, &coeff) in coeffs.iter().take(show_count).enumerate() {
185 if i > 0 {
186 write!(f, ", ")?;
187 }
188 write!(f, "{}", coeff)?;
189 }
190
191 if coeffs.len() > max_show {
192 write!(f, ", ... ({} more)", coeffs.len() - max_show)?;
193 }
194
195 writeln!(f, "]")?;
196 }
197 }
198 Ok(())
199 }
200}