diff --git a/.gitmodules b/.gitmodules index 7ba4798..0558478 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "base2k/spqlios-arithmetic"] path = base2k/spqlios-arithmetic - url = https://github.com/tfhe/spqlios-arithmetic \ No newline at end of file + url = https://github.com/Pro7ech/spqlios-arithmetic diff --git a/.vscode/settings.json b/.vscode/settings.json index d04dd22..6abc417 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,61 @@ { - "cmake.sourceDirectory": "C:/Users/boss_/go/src/github.com/gausslabs/fhe/base2k/spqlios-arithmetic" + "cmake.sourceDirectory": "C:/Users/boss_/go/src/github.com/gausslabs/fhe/base2k/spqlios-arithmetic", + "files.associations": { + "vector": "cpp", + "xstring": "cpp", + "xutility": "cpp", + "algorithm": "cpp", + "atomic": "cpp", + "bit": "cpp", + "cctype": "cpp", + "charconv": "cpp", + "cinttypes": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "complex": "cpp", + "concepts": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "exception": "cpp", + "format": "cpp", + "initializer_list": "cpp", + "ios": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "iterator": "cpp", + "limits": "cpp", + "locale": "cpp", + "memory": "cpp", + "new": "cpp", + "optional": "cpp", + "ostream": "cpp", + "random": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "string": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "typeinfo": "cpp", + "utility": "cpp", + "xfacet": "cpp", + "xiosbase": "cpp", + "xlocale": "cpp", + "xlocbuf": "cpp", + "xlocinfo": "cpp", + "xlocmes": "cpp", + "xlocmon": "cpp", + "xlocnum": "cpp", + "xloctime": "cpp", + "xmemory": "cpp", + "xtr1common": "cpp" + } } \ No newline at end of file diff --git a/base2k/examples/vector_matrix_product.rs b/base2k/examples/vector_matrix_product.rs index d1a780b..16568b3 100644 --- a/base2k/examples/vector_matrix_product.rs +++ b/base2k/examples/vector_matrix_product.rs @@ -17,7 +17,7 @@ fn main() { let cols: usize = limbs + 1; // Maximum size of the byte scratch needed - let tmp_bytes: usize = module.vmp_prepare_contiguous_tmp_bytes(rows, cols) + let tmp_bytes: usize = module.vmp_prepare_tmp_bytes(rows, cols) | module.vmp_apply_dft_tmp_bytes(limbs, limbs, rows, cols); let mut buf: Vec = vec![0; tmp_bytes]; @@ -29,16 +29,21 @@ fn main() { a.encode_vec_i64(log_base2k, log_k, &a_values, 32); a.normalize(log_base2k, &mut buf); - (0..a.limbs()).for_each(|i| println!("{}: {:?}", i, a.at(i))); + a.print_limbs(a.limbs(), n); + println!(); - let mut b_mat: Matrix3D = Matrix3D::new(rows, cols, n); - - (0..min(rows, cols)).for_each(|i| { - b_mat.at_mut(i, i)[1] = 1 as i64; + let mut vecznx: Vec= Vec::new(); + (0..rows).for_each(|_|{ + vecznx.push(module.new_vec_znx(cols)); }); + (0..rows).for_each(|i|{ + vecznx[i].data[i*n+1] = 1 as i64; + }); + + let mut vmp_pmat: VmpPMat = module.new_vmp_pmat(rows, cols); - module.vmp_prepare_contiguous(&mut vmp_pmat, &b_mat.data, &mut buf); + module.vmp_prepare_dblptr(&mut vmp_pmat, &vecznx, &mut buf); let mut c_dft: VecZnxDft = module.new_vec_znx_dft(cols); module.vmp_apply_dft(&mut c_dft, &a, &vmp_pmat, &mut buf); @@ -52,7 +57,8 @@ fn main() { let mut values_res: Vec = vec![i64::default(); n]; res.decode_vec_i64(log_base2k, log_k, &mut values_res); - (0..res.limbs()).for_each(|i| println!("{}: {:?}", i, res.at(i))); + + res.print_limbs(res.limbs(), n); module.free(); c_dft.free(); @@ -60,3 +66,48 @@ fn main() { //println!("{:?}", values_res) } + + +/* + +use base2k::{ + Encoding, Free, Infos, Matrix3D, Module, VecZnx, VecZnxBig, VecZnxDft, VecZnxOps, VmpPMat, + VmpPMatOps, FFT64, +}; +use std::cmp::min; + +fn main() { + use base2k::{Module, FFT64, Matrix3D, VmpPMat, VmpPMatOps, VecZnx, VecZnxOps, Free}; + use std::cmp::min; + + let n: usize = 32; + let module: Module = Module::new::(n); + let rows: usize = 5; + let cols: usize = 6; + + let mut vecznx: Vec= Vec::new(); + (0..rows).for_each(|_|{ + vecznx.push(module.new_vec_znx(cols)); + }); + + (0..rows).for_each(|i|{ + vecznx[i].data[i*n] = 1 as i64; + vecznx[i].print_limbs(cols, n); + }); + + let mut buf: Vec = vec![u8::default(); module.vmp_prepare_tmp_bytes(rows, cols)]; + + let mut vmp_pmat: VmpPMat = module.new_vmp_pmat(rows, cols); + + println!("123"); + + module.vmp_prepare_dblptr(&mut vmp_pmat, &vecznx, &mut buf); + + + module.vmp_apply_dft(c, a, b, buf); + + vmp_pmat.free(); + module.free(); +} + +*/ \ No newline at end of file diff --git a/base2k/spqlios-arithmetic b/base2k/spqlios-arithmetic index 7ea875f..83555cc 160000 --- a/base2k/spqlios-arithmetic +++ b/base2k/spqlios-arithmetic @@ -1 +1 @@ -Subproject commit 7ea875f61c51c67687ec9c15ae0eae04cda961f8 +Subproject commit 83555cc664b4ebedd9b82d35120c80605b895b87 diff --git a/base2k/src/ffi/vmp.rs b/base2k/src/ffi/vmp.rs index 44ed6c0..9aeb184 100644 --- a/base2k/src/ffi/vmp.rs +++ b/base2k/src/ffi/vmp.rs @@ -80,8 +80,6 @@ unsafe extern "C" { ); } -/* -NOT IMPLEMENTED IN SPQLIOS unsafe extern "C" { pub unsafe fn vmp_prepare_dblptr( module: *const MODULE, @@ -92,9 +90,9 @@ unsafe extern "C" { tmp_space: *mut u8, ); } -*/ + unsafe extern "C" { - pub unsafe fn vmp_prepare_contiguous_tmp_bytes( + pub unsafe fn vmp_prepare_tmp_bytes( module: *const MODULE, nrows: u64, ncols: u64, diff --git a/base2k/src/vmp.rs b/base2k/src/vmp.rs index a228d54..c850e6a 100644 --- a/base2k/src/vmp.rs +++ b/base2k/src/vmp.rs @@ -96,7 +96,7 @@ pub trait VmpPMatOps { /// /// * `rows`: number of rows of the [VmpPMat] used in [VmpPMatOps::vmp_prepare_contiguous]. /// * `cols`: number of cols of the [VmpPMat] used in [VmpPMatOps::vmp_prepare_contiguous]. - fn vmp_prepare_contiguous_tmp_bytes(&self, rows: usize, cols: usize) -> usize; + fn vmp_prepare_tmp_bytes(&self, rows: usize, cols: usize) -> usize; /// Prepares a [VmpPMat] from a contiguous array of [i64]. /// The helper struct [Matrix3D] can be used to contruct and populate @@ -106,7 +106,7 @@ pub trait VmpPMatOps { /// /// * `b`: [VmpPMat] on which the values are encoded. /// * `a`: the contiguous array of [i64] of the 3D matrix to encode on the [VmpPMat]. - /// * `buf`: scratch space, the size of buf can be obtained with [VmpPMatOps::vmp_prepare_contiguous_tmp_bytes]. + /// * `buf`: scratch space, the size of buf can be obtained with [VmpPMatOps::vmp_prepare_tmp_bytes]. /// /// # Example /// ``` @@ -125,7 +125,7 @@ pub trait VmpPMatOps { /// b_mat.at_mut(i, i)[1] = 1 as i64; /// }); /// - /// let mut buf: Vec = vec![u8::default(); module.vmp_prepare_contiguous_tmp_bytes(rows, cols)]; + /// let mut buf: Vec = vec![u8::default(); module.vmp_prepare_tmp_bytes(rows, cols)]; /// /// let mut vmp_pmat: VmpPMat = module.new_vmp_pmat(rows, cols); /// module.vmp_prepare_contiguous(&mut vmp_pmat, &b_mat.data, &mut buf); @@ -140,9 +140,9 @@ pub trait VmpPMatOps { /// /// * `b`: [VmpPMat] on which the values are encoded. /// * `a`: the vector of [VecZnx] to encode on the [VmpPMat]. - /// * `buf`: scratch space, the size of buf can be obtained with [VmpPMatOps::vmp_prepare_contiguous_tmp_bytes]. + /// * `buf`: scratch space, the size of buf can be obtained with [VmpPMatOps::vmp_prepare_tmp_bytes]. /// - /// The size of buf can be obtained with [VmpPMatOps::vmp_prepare_contiguous_tmp_bytes]. + /// The size of buf can be obtained with [VmpPMatOps::vmp_prepare_tmp_bytes]. /// /// # Example /// ``` @@ -159,7 +159,7 @@ pub trait VmpPMatOps { /// vecznx.push(module.new_vec_znx(cols)); /// }); /// - /// let mut buf: Vec = vec![u8::default(); module.vmp_prepare_contiguous_tmp_bytes(rows, cols)]; + /// let mut buf: Vec = vec![u8::default(); module.vmp_prepare_tmp_bytes(rows, cols)]; /// /// let mut vmp_pmat: VmpPMat = module.new_vmp_pmat(rows, cols); /// module.vmp_prepare_dblptr(&mut vmp_pmat, &vecznx, &mut buf); @@ -373,8 +373,8 @@ impl VmpPMatOps for Module { } } - fn vmp_prepare_contiguous_tmp_bytes(&self, rows: usize, cols: usize) -> usize { - unsafe { vmp::vmp_prepare_contiguous_tmp_bytes(self.0, rows as u64, cols as u64) as usize } + fn vmp_prepare_tmp_bytes(&self, rows: usize, cols: usize) -> usize { + unsafe { vmp::vmp_prepare_tmp_bytes(self.0, rows as u64, cols as u64) as usize } } fn vmp_prepare_contiguous(&self, b: &mut VmpPMat, a: &[i64], buf: &mut [u8]) { @@ -391,31 +391,17 @@ impl VmpPMatOps for Module { } fn vmp_prepare_dblptr(&self, b: &mut VmpPMat, a: &Vec, buf: &mut [u8]) { - let rows: usize = b.rows(); - let cols: usize = b.cols(); - - let mut mat: Matrix3D = Matrix3D::::new(rows, cols, self.n()); - - (0..min(rows, a.len())).for_each(|i| { - mat.set_row(i, &a[i].data); - }); - - self.vmp_prepare_contiguous(b, &mat.data, buf); - - /* - NOT IMPLEMENTED IN SPQLIOS - let mut ptrs: Vec<*const i64> = a.iter().map(|v| v.data.as_ptr()).collect(); + let ptrs: Vec<*const i64> = a.iter().map(|v| v.data.as_ptr()).collect(); unsafe { - vmp_prepare_dblptr( + vmp::vmp_prepare_dblptr( self.0, b.data(), - ptrs.as_mut_ptr(), + ptrs.as_ptr(), b.rows() as u64, b.cols() as u64, buf.as_mut_ptr(), ); } - */ } fn vmp_apply_dft_tmp_bytes(