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