arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-schemes/src/tfhe/bdd_arithmetic/mod.rs
poulpy-schemes/src/tfhe/bdd_arithmetic/mod.rsRaw
  1mod bdd_2w_to_1w;
  2mod blind_retrieval;
  3mod blind_rotation;
  4mod blind_selection;
  5mod ciphertexts;
  6mod circuits;
  7mod eval;
  8mod key;
  9
 10pub use bdd_2w_to_1w::*;
 11pub use blind_retrieval::*;
 12pub use blind_rotation::*;
 13pub use blind_selection::*;
 14pub use ciphertexts::*;
 15pub(crate) use circuits::*;
 16pub use eval::*;
 17pub use key::*;
 18
 19pub mod tests;
 20
 21pub trait UnsignedInteger: Copy + Sync + Send + 'static {
 22    const BITS: u32;
 23    const LOG_BITS: u32;
 24    const LOG_BYTES: u32;
 25    const LOG_BYTES_MASK: usize;
 26
 27    #[inline(always)]
 28    fn bit_index(i: usize) -> usize {
 29        ((i & 7) << Self::LOG_BYTES) | (i >> 3)
 30    }
 31}
 32
 33impl UnsignedInteger for u8 {
 34    const BITS: u32 = u8::BITS;
 35    const LOG_BITS: u32 = (u32::BITS - (Self::BITS - 1).leading_zeros());
 36    const LOG_BYTES: u32 = Self::LOG_BITS - 3;
 37    const LOG_BYTES_MASK: usize = (1 << Self::LOG_BYTES) - 1;
 38}
 39impl UnsignedInteger for u16 {
 40    const BITS: u32 = u16::BITS;
 41    const LOG_BITS: u32 = (u32::BITS - (Self::BITS - 1).leading_zeros());
 42    const LOG_BYTES: u32 = Self::LOG_BITS - 3;
 43    const LOG_BYTES_MASK: usize = (1 << Self::LOG_BYTES) - 1;
 44}
 45impl UnsignedInteger for u32 {
 46    const BITS: u32 = u32::BITS;
 47    const LOG_BITS: u32 = (u32::BITS - (Self::BITS - 1).leading_zeros());
 48    const LOG_BYTES: u32 = Self::LOG_BITS - 3;
 49    const LOG_BYTES_MASK: usize = (1 << Self::LOG_BYTES) - 1;
 50}
 51impl UnsignedInteger for u64 {
 52    const BITS: u32 = u64::BITS;
 53    const LOG_BITS: u32 = (u32::BITS - (Self::BITS - 1).leading_zeros());
 54    const LOG_BYTES: u32 = Self::LOG_BITS >> 3;
 55    const LOG_BYTES_MASK: usize = (1 << Self::LOG_BYTES) - 1;
 56}
 57impl UnsignedInteger for u128 {
 58    const BITS: u32 = u128::BITS;
 59    const LOG_BITS: u32 = (u32::BITS - (Self::BITS - 1).leading_zeros());
 60    const LOG_BYTES: u32 = Self::LOG_BITS >> 3;
 61    const LOG_BYTES_MASK: usize = (1 << Self::LOG_BYTES) - 1;
 62}
 63
 64pub trait ToBits {
 65    fn bit(&self, i: usize) -> u8;
 66}
 67
 68macro_rules! impl_tobits {
 69    ($($t:ty),*) => {
 70        $(
 71            impl ToBits for $t {
 72                fn bit(&self, i: usize) -> u8 {
 73                    if i >= (std::mem::size_of::<$t>() * 8) {
 74                        panic!("bit index {} out of range for {}", i, stringify!($t));
 75                    }
 76                    ((self >> i) & 1) as u8
 77                }
 78            }
 79        )*
 80    };
 81}
 82
 83impl_tobits!(u8, u16, u32, u64, u128);
 84
 85pub trait FromBits: Sized {
 86    fn from_bits(bits: &[u8]) -> Self;
 87}
 88
 89macro_rules! impl_from_bits {
 90    ($($t:ty),*) => {
 91        $(
 92            impl FromBits for $t {
 93                fn from_bits(bits: &[u8]) -> Self {
 94                    let mut value: $t = 0;
 95                    let max_bits = std::mem::size_of::<$t>() * 8;
 96                    let n = bits.len().min(max_bits);
 97
 98                    for (i, &bit) in bits.iter().take(n).enumerate() {
 99                        if bit != 0 {
100                            value |= 1 << i;
101                        }
102                    }
103                    value
104                }
105            }
106        )*
107    };
108}
109
110impl_from_bits!(u8, u16, u32, u64, u128);