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