arnaucube
arnaucube/poulpyPublic Self-hosted repository
Add bdd rotation
Jean-Philippe Bossuat jeanphilippe.bossuat@gmail.com
Fri, 24 Oct 2025 18:13:43 +0200
commit

d989867c914314f91f4b5a5009a850ca656246ff

parent

96d8f4cfc4cff989324bb8b6663cfe3c51e80ea7

M Cargo.lock → Cargo.lock

@@ -353,7 +353,7 @@ ]

[[package]] name = "poulpy-backend" -version = "0.2.0" +version = "0.3.1" dependencies = [ "byteorder", "cmake",

@@ -370,7 +370,7 @@ ]

[[package]] name = "poulpy-core" -version = "0.2.0" +version = "0.3.1" dependencies = [ "byteorder", "criterion",

@@ -383,7 +383,7 @@ ]

[[package]] name = "poulpy-hal" -version = "0.2.0" +version = "0.3.1" dependencies = [ "bytemuck", "byteorder",

@@ -400,7 +400,7 @@ ]

[[package]] name = "poulpy-schemes" -version = "0.2.0" +version = "0.3.0" dependencies = [ "byteorder", "criterion",
M poulpy-backend/Cargo.toml → poulpy-backend/Cargo.toml

@@ -1,6 +1,6 @@

[package] name = "poulpy-backend" -version = "0.2.0" +version = "0.3.1" edition = "2024" license = "Apache-2.0" readme = "README.md"
M poulpy-core/Cargo.toml → poulpy-core/Cargo.toml

@@ -1,6 +1,6 @@

[package] name = "poulpy-core" -version = "0.2.0" +version = "0.3.1" edition = "2024" license = "Apache-2.0" description = "A backend agnostic crate implementing RLWE-based encryption & arithmetic."
A poulpy-core/src/operations/ggsw.rs

@@ -0,0 +1,55 @@

+use poulpy_hal::layouts::{Backend, Module, Scratch}; + +use crate::{ + GLWERotate, ScratchTakeCore, + layouts::{GGSW, GGSWInfos, GGSWToMut, GGSWToRef, GLWEInfos}, +}; + +impl<BE: Backend> GGSWRotate<BE> for Module<BE> where Module<BE>: GLWERotate<BE> {} + +pub trait GGSWRotate<BE: Backend> +where + Self: GLWERotate<BE>, +{ + fn ggsw_rotate_tmp_bytes(&self) -> usize { + self.glwe_rotate_tmp_bytes() + } + + fn ggsw_rotate<R, A>(&self, k: i64, res: &mut R, a: &A) + where + R: GGSWToMut, + A: GGSWToRef, + { + let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); + let a: &GGSW<&[u8]> = &a.to_ref(); + + assert!(res.dnum() <= a.dnum()); + assert_eq!(res.dsize(), a.dsize()); + assert_eq!(res.rank(), a.rank()); + let rows: usize = res.dnum().into(); + let cols: usize = (res.rank() + 1).into(); + + for row in 0..rows { + for col in 0..cols { + self.glwe_rotate(k, &mut res.at_mut(row, col), &a.at(row, col)); + } + } + } + + fn ggsw_rotate_inplace<R>(&self, k: i64, res: &mut R, scratch: &mut Scratch<BE>) + where + R: GGSWToMut, + Scratch<BE>: ScratchTakeCore<BE>, + { + let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); + + let rows: usize = res.dnum().into(); + let cols: usize = (res.rank() + 1).into(); + + for row in 0..rows { + for col in 0..cols { + self.glwe_rotate_inplace(k, &mut res.at_mut(row, col), scratch); + } + } + } +}
M poulpy-core/src/operations/glwe.rs → poulpy-core/src/operations/glwe.rs

@@ -5,6 +5,7 @@ VecZnxNormalize, VecZnxNormalizeInplace, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub,

VecZnxSubInplace, VecZnxSubNegateInplace, }, layouts::{Backend, Module, Scratch, VecZnx, ZnxZero}, + reference::vec_znx::vec_znx_rotate_inplace_tmp_bytes, }; use crate::{

@@ -185,6 +186,10 @@ pub trait GLWERotate<BE: Backend>

where Self: ModuleN + VecZnxRotate + VecZnxRotateInplace<BE>, { + fn glwe_rotate_tmp_bytes(&self) -> usize { + vec_znx_rotate_inplace_tmp_bytes(self.n()) + } + fn glwe_rotate<R, A>(&self, k: i64, res: &mut R, a: &A) where R: GLWEToMut,
M poulpy-core/src/operations/mod.rs → poulpy-core/src/operations/mod.rs

@@ -1,3 +1,5 @@

+mod ggsw; mod glwe; +pub use ggsw::*; pub use glwe::*;
M poulpy-hal/Cargo.toml → poulpy-hal/Cargo.toml

@@ -1,6 +1,6 @@

[package] name = "poulpy-hal" -version = "0.2.0" +version = "0.3.1" edition = "2024" license = "Apache-2.0" readme = "README.md"
M poulpy-schemes/Cargo.toml → poulpy-schemes/Cargo.toml

@@ -1,6 +1,6 @@

[package] name = "poulpy-schemes" -version = "0.2.0" +version = "0.3.0" edition = "2024" license = "Apache-2.0" readme = "README.md"
A poulpy-schemes/src/tfhe/bdd_arithmetic/bdd_rotation.rs

@@ -0,0 +1,42 @@

+use poulpy_core::{ + GLWECopy, GLWERotate, ScratchTakeCore, + layouts::{GLWE, GLWEToMut}, +}; +use poulpy_hal::layouts::{Backend, Scratch}; + +use crate::tfhe::bdd_arithmetic::{Cmux, GetGGSWBit, UnsignedInteger}; + +pub trait BDDRotation<T: UnsignedInteger, BE: Backend> +where + Self: GLWECopy + GLWERotate<BE> + Cmux<BE>, + Scratch<BE>: ScratchTakeCore<BE>, +{ + /// Homomorphic multiplication of res by X^{k[bit_start..bit_start + bit_size] * bit_step}. + fn bdd_rotate<R, K, D>( + &self, + res: &mut R, + k: K, + bit_start: usize, + bit_size: usize, + bit_step: usize, + scratch: &mut Scratch<BE>, + ) where + R: GLWEToMut, + K: GetGGSWBit<T, BE>, + Scratch<BE>: ScratchTakeCore<BE>, + { + let res: &mut GLWE<&mut [u8]> = &mut res.to_mut(); + + let (mut tmp_res, scratch_1) = scratch.take_glwe(res); + + self.glwe_copy(&mut tmp_res, res); + + for i in 1..bit_size { + // res' = res * X^2^(i * bit_step) + self.glwe_rotate(1 << (i + bit_step), &mut tmp_res, res); + + // res = (res - res') * GGSW(b[i]) + res' + self.cmux_inplace(res, &tmp_res, &k.get_bit(i + bit_start), scratch_1); + } + } +}
M poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs → poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs

@@ -39,6 +39,17 @@ self.blocks[0].rank()

} } +impl<D: Data, T: UnsignedInteger> FheUintBlocks<D, T> { + pub fn new(blocks: Vec<GLWE<D>>) -> Self { + assert_eq!(blocks.len(), T::WORD_SIZE); + Self { + blocks, + _base: 1, + _phantom: PhantomData, + } + } +} + impl<T: UnsignedInteger> FheUintBlocks<Vec<u8>, T> { pub fn alloc_from_infos<A, BE: Backend>(module: &Module<BE>, infos: &A) -> Self where
M poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs → poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block_prepared.rs

@@ -3,6 +3,7 @@

use poulpy_core::layouts::{ Base2K, Dnum, Dsize, GGSWInfos, GGSWPreparedFactory, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::GGSWPrepared, }; +use poulpy_core::layouts::{GGSWPreparedToMut, GGSWPreparedToRef}; use poulpy_core::{GGSWEncryptSk, ScratchTakeCore, layouts::GLWESecretPreparedToRef}; use poulpy_hal::layouts::{Backend, Data, DataRef, Module};

@@ -26,6 +27,28 @@

impl<T: UnsignedInteger, BE: Backend> FheUintBlocksPreparedFactory<T, BE> for Module<BE> where Self: Sized + GGSWPreparedFactory<BE> { +} + +pub trait GetGGSWBit<T: UnsignedInteger, BE: Backend> { + fn get_bit(&self, bit: usize) -> GGSWPrepared<&[u8], BE>; +} + +impl<D: DataRef, T: UnsignedInteger, BE: Backend> GetGGSWBit<T, BE> for FheUintBlocksPrepared<D, T, BE> { + fn get_bit(&self, bit: usize) -> GGSWPrepared<&[u8], BE> { + assert!(bit <= self.blocks.len()); + self.blocks[bit].to_ref() + } +} + +pub trait GetGGSWBitMut<T: UnsignedInteger, BE: Backend> { + fn get_bit(&mut self, bit: usize) -> GGSWPrepared<&mut [u8], BE>; +} + +impl<D: DataMut, T: UnsignedInteger, BE: Backend> GetGGSWBitMut<T, BE> for FheUintBlocksPrepared<D, T, BE> { + fn get_bit(&mut self, bit: usize) -> GGSWPrepared<&mut [u8], BE> { + assert!(bit <= self.blocks.len()); + self.blocks[bit].to_mut() + } } pub trait FheUintBlocksPreparedFactory<T: UnsignedInteger, BE: Backend>
M poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs → poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs

@@ -3,12 +3,9 @@

use itertools::Itertools; use poulpy_core::{ GLWEAdd, GLWECopy, GLWEExternalProduct, GLWESub, ScratchTakeCore, - layouts::{ - GLWE, LWEInfos, - prepared::{GGSWPrepared, GGSWPreparedToRef}, - }, + layouts::{GLWE, GLWEToMut, GLWEToRef, LWEInfos, prepared::GGSWPreparedToRef}, }; -use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero}; +use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch, ZnxZero}; use crate::tfhe::bdd_arithmetic::UnsignedInteger;

@@ -146,30 +143,38 @@ Copy,

None, } -pub trait Cmux<BE: Backend> { - fn cmux<O, T, F, S>(&self, out: &mut GLWE<O>, t: &GLWE<T>, f: &GLWE<F>, s: &GGSWPrepared<S, BE>, scratch: &mut Scratch<BE>) +pub trait Cmux<BE: Backend> +where + Self: GLWEExternalProduct<BE> + GLWESub + GLWEAdd, + Scratch<BE>: ScratchTakeCore<BE>, +{ + fn cmux<R, T, F, S>(&self, res: &mut R, t: &T, f: &F, s: &S, scratch: &mut Scratch<BE>) where - O: DataMut, - T: DataRef, - F: DataRef, - S: DataRef; + R: GLWEToMut, + T: GLWEToRef, + F: GLWEToRef, + S: GGSWPreparedToRef<BE>, + { + self.glwe_sub(res, t, f); + self.glwe_external_product_inplace(res, s, scratch); + self.glwe_add_inplace(res, f); + } + + fn cmux_inplace<R, A, S>(&self, res: &mut R, a: &A, s: &S, scratch: &mut Scratch<BE>) + where + R: GLWEToMut, + A: GLWEToRef, + S: GGSWPreparedToRef<BE>, + { + self.glwe_sub_inplace(res, a); + self.glwe_external_product_inplace(res, s, scratch); + self.glwe_add_inplace(res, a); + } } impl<BE: Backend> Cmux<BE> for Module<BE> where - Module<BE>: GLWEExternalProduct<BE> + GLWESub + GLWEAdd, + Self: GLWEExternalProduct<BE> + GLWESub + GLWEAdd, Scratch<BE>: ScratchTakeCore<BE>, { - fn cmux<O, T, F, S>(&self, out: &mut GLWE<O>, t: &GLWE<T>, f: &GLWE<F>, s: &GGSWPrepared<S, BE>, scratch: &mut Scratch<BE>) - where - O: DataMut, - T: DataRef, - F: DataRef, - S: DataRef, - { - // let mut out: GLWECiphertext<&mut [u8]> = out.to_mut(); - self.glwe_sub(out, t, f); - self.glwe_external_product_inplace(out, s, scratch); - self.glwe_add_inplace(out, f); - } }
M poulpy-schemes/src/tfhe/bdd_arithmetic/mod.rs → poulpy-schemes/src/tfhe/bdd_arithmetic/mod.rs

@@ -1,10 +1,12 @@

mod bdd_2w_to_1w; +mod bdd_rotation; mod ciphertexts; mod circuits; mod eval; mod key; pub use bdd_2w_to_1w::*; +pub use bdd_rotation::*; pub use ciphertexts::*; pub(crate) use circuits::*; pub(crate) use eval::*;