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