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