poulpy/base2k/src/lib.rs
base2k/src/lib.rsRaw
1pub mod encoding;
2#[allow(non_camel_case_types, non_snake_case, non_upper_case_globals, dead_code, improper_ctypes)]
3// Other modules and exports
4pub mod ffi;
5pub mod infos;
6pub mod module;
7pub mod sampling;
8pub mod stats;
9pub mod svp;
10pub mod vec_znx;
11pub mod vec_znx_big;
12pub mod vec_znx_dft;
13pub mod vmp;
14
15pub use encoding::*;
16pub use infos::*;
17pub use module::*;
18pub use sampling::*;
19#[allow(unused_imports)]
20pub use stats::*;
21pub use svp::*;
22pub use vec_znx::*;
23pub use vec_znx_big::*;
24pub use vec_znx_dft::*;
25pub use vmp::*;
26
27pub const GALOISGENERATOR: u64 = 5;
28pub const DEFAULTALIGN: usize = 64;
29
30fn is_aligned_custom<T>(ptr: *const T, align: usize) -> bool {
31 (ptr as usize) % align == 0
32}
33
34pub fn is_aligned<T>(ptr: *const T) -> bool {
35 is_aligned_custom(ptr, DEFAULTALIGN)
36}
37
38pub fn assert_alignement<T>(ptr: *const T) {
39 assert!(
40 is_aligned(ptr),
41 "invalid alignement: ensure passed bytes have been allocated with [alloc_aligned_u8] or [alloc_aligned]"
42 )
43}
44
45pub fn cast<T, V>(data: &[T]) -> &[V] {
46 let ptr: *const V = data.as_ptr() as *const V;
47 let len: usize = data.len() / std::mem::size_of::<V>();
48 unsafe { std::slice::from_raw_parts(ptr, len) }
49}
50
51pub fn cast_mut<T, V>(data: &[T]) -> &mut [V] {
52 let ptr: *mut V = data.as_ptr() as *mut V;
53 let len: usize = data.len() / std::mem::size_of::<V>();
54 unsafe { std::slice::from_raw_parts_mut(ptr, len) }
55}
56
57/// Allocates a block of bytes with a custom alignement.
58/// Alignement must be a power of two and size a multiple of the alignement.
59/// Allocated memory is initialized to zero.
60fn alloc_aligned_custom_u8(size: usize, align: usize) -> Vec<u8> {
61 assert!(
62 align.is_power_of_two(),
63 "Alignment must be a power of two but is {}",
64 align
65 );
66 assert_eq!(
67 (size * std::mem::size_of::<u8>()) % align,
68 0,
69 "size={} must be a multiple of align={}",
70 size,
71 align
72 );
73 unsafe {
74 let layout: std::alloc::Layout = std::alloc::Layout::from_size_align(size, align).expect("Invalid alignment");
75 let ptr: *mut u8 = std::alloc::alloc(layout);
76 if ptr.is_null() {
77 panic!("Memory allocation failed");
78 }
79 assert!(
80 is_aligned_custom(ptr, align),
81 "Memory allocation at {:p} is not aligned to {} bytes",
82 ptr,
83 align
84 );
85 // Init allocated memory to zero
86 std::ptr::write_bytes(ptr, 0, size);
87 Vec::from_raw_parts(ptr, size, size)
88 }
89}
90
91/// Allocates a block of T aligned with [DEFAULTALIGN].
92/// Size of T * size msut be a multiple of [DEFAULTALIGN].
93pub fn alloc_aligned_custom<T>(size: usize, align: usize) -> Vec<T> {
94 assert_eq!(
95 (size * std::mem::size_of::<T>()) % align,
96 0,
97 "size={} must be a multiple of align={}",
98 size,
99 align
100 );
101 let mut vec_u8: Vec<u8> = alloc_aligned_custom_u8(std::mem::size_of::<T>() * size, align);
102 let ptr: *mut T = vec_u8.as_mut_ptr() as *mut T;
103 let len: usize = vec_u8.len() / std::mem::size_of::<T>();
104 let cap: usize = vec_u8.capacity() / std::mem::size_of::<T>();
105 std::mem::forget(vec_u8);
106 unsafe { Vec::from_raw_parts(ptr, len, cap) }
107}
108
109pub fn alloc_aligned<T>(size: usize) -> Vec<T> {
110 alloc_aligned_custom::<T>(size, DEFAULTALIGN)
111}