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