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