arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/base2k/src/lib.rs
base2k/src/lib.rsRaw
  1pub mod encoding;
  2#[allow(
  3    non_camel_case_types,
  4    non_snake_case,
  5    non_upper_case_globals,
  6    dead_code,
  7    improper_ctypes
  8)]
  9// Other modules and exports
 10pub mod ffi;
 11pub mod free;
 12pub mod infos;
 13pub mod module;
 14pub mod sampling;
 15pub mod stats;
 16pub mod svp;
 17pub mod vec_znx;
 18pub mod vec_znx_big;
 19pub mod vec_znx_dft;
 20pub mod vmp;
 21
 22pub use encoding::*;
 23pub use free::*;
 24pub use infos::*;
 25pub use module::*;
 26pub use sampling::*;
 27#[allow(unused_imports)]
 28pub use stats::*;
 29pub use svp::*;
 30pub use vec_znx::*;
 31pub use vec_znx_big::*;
 32pub use vec_znx_dft::*;
 33pub use vmp::*;
 34
 35pub const GALOISGENERATOR: u64 = 5;
 36
 37#[allow(dead_code)]
 38fn is_aligned<T>(ptr: *const T, align: usize) -> bool {
 39    (ptr as usize) % align == 0
 40}
 41
 42pub fn cast<T, V>(data: &[T]) -> &[V] {
 43    let ptr: *const V = data.as_ptr() as *const V;
 44    let len: usize = data.len() / std::mem::size_of::<V>();
 45    unsafe { std::slice::from_raw_parts(ptr, len) }
 46}
 47
 48pub fn cast_mut<T, V>(data: &[T]) -> &mut [V] {
 49    let ptr: *mut V = data.as_ptr() as *mut V;
 50    let len: usize = data.len() / std::mem::size_of::<V>();
 51    unsafe { std::slice::from_raw_parts_mut(ptr, len) }
 52}
 53
 54use std::alloc::{alloc, Layout};
 55
 56pub fn alloc_aligned_u8(size: usize, align: usize) -> Vec<u8> {
 57    assert_eq!(
 58        align & (align - 1),
 59        0,
 60        "align={} must be a power of two",
 61        align
 62    );
 63    assert_eq!(
 64        (size * std::mem::size_of::<u8>()) % align,
 65        0,
 66        "size={} must be a multiple of align={}",
 67        size,
 68        align
 69    );
 70    unsafe {
 71        let layout: Layout = Layout::from_size_align(size, align).expect("Invalid alignment");
 72        let ptr: *mut u8 = alloc(layout);
 73        if ptr.is_null() {
 74            panic!("Memory allocation failed");
 75        }
 76        Vec::from_raw_parts(ptr, size, size)
 77    }
 78}
 79
 80pub fn alloc_aligned<T>(size: usize, align: usize) -> Vec<T> {
 81    assert_eq!(
 82        (size * std::mem::size_of::<T>()) % align,
 83        0,
 84        "size={} must be a multiple of align={}",
 85        size,
 86        align
 87    );
 88    let mut vec_u8: Vec<u8> = alloc_aligned_u8(std::mem::size_of::<T>() * size, align);
 89    let ptr: *mut T = vec_u8.as_mut_ptr() as *mut T;
 90    let len: usize = vec_u8.len() / std::mem::size_of::<T>();
 91    let cap: usize = vec_u8.capacity() / std::mem::size_of::<T>();
 92    std::mem::forget(vec_u8);
 93    unsafe { Vec::from_raw_parts(ptr, len, cap) }
 94}
 95
 96fn alias_mut_slice_to_vec<T>(slice: &[T]) -> Vec<T> {
 97    unsafe {
 98        let ptr: *mut T = slice.as_ptr() as *mut T;
 99        let len: usize = slice.len();
100        Vec::from_raw_parts(ptr, len, len)
101    }
102}