Add schemes (#71)

* Move br + cbt to schemes/tfhe

* refactor blind rotation

* refactor circuit bootstrapping

* renamed exec -> prepared
This commit is contained in:
Jean-Philippe Bossuat
2025-08-15 15:06:26 +02:00
committed by GitHub
parent 8d9897b88b
commit c7219c35e9
130 changed files with 2631 additions and 3270 deletions

View File

@@ -3,22 +3,25 @@ use backend::hal::{
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat},
};
use crate::layouts::{GGLWETensorKey, Infos, prepared::GGLWESwitchingKeyExec};
use crate::layouts::{
GGLWETensorKey, Infos,
prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc},
};
#[derive(PartialEq, Eq)]
pub struct GGLWETensorKeyExec<D: Data, B: Backend> {
pub(crate) keys: Vec<GGLWESwitchingKeyExec<D, B>>,
pub struct GGLWETensorKeyPrepared<D: Data, B: Backend> {
pub(crate) keys: Vec<GGLWESwitchingKeyPrepared<D, B>>,
}
impl<B: Backend> GGLWETensorKeyExec<Vec<u8>, B> {
impl<B: Backend> GGLWETensorKeyPrepared<Vec<u8>, B> {
pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self
where
Module<B>: VmpPMatAlloc<B>,
{
let mut keys: Vec<GGLWESwitchingKeyExec<Vec<u8>, B>> = Vec::new();
let mut keys: Vec<GGLWESwitchingKeyPrepared<Vec<u8>, B>> = Vec::new();
let pairs: usize = (((rank + 1) * rank) >> 1).max(1);
(0..pairs).for_each(|_| {
keys.push(GGLWESwitchingKeyExec::alloc(
keys.push(GGLWESwitchingKeyPrepared::alloc(
module, n, basek, k, rows, digits, 1, rank,
));
});
@@ -30,32 +33,11 @@ impl<B: Backend> GGLWETensorKeyExec<Vec<u8>, B> {
Module<B>: VmpPMatAllocBytes,
{
let pairs: usize = (((rank + 1) * rank) >> 1).max(1);
pairs * GGLWESwitchingKeyExec::bytes_of(module, n, basek, k, rows, digits, 1, rank)
}
pub fn from<D: DataRef>(
module: &Module<B>,
other: &GGLWETensorKey<D>,
scratch: &mut Scratch<B>,
) -> GGLWETensorKeyExec<Vec<u8>, B>
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
let mut tsk_exec: GGLWETensorKeyExec<Vec<u8>, B> = Self::alloc(
module,
other.n(),
other.basek(),
other.k(),
other.rows(),
other.digits(),
other.rank(),
);
tsk_exec.prepare(module, other, scratch);
tsk_exec
pairs * GGLWESwitchingKeyPrepared::bytes_of(module, n, basek, k, rows, digits, 1, rank)
}
}
impl<D: Data, B: Backend> Infos for GGLWETensorKeyExec<D, B> {
impl<D: Data, B: Backend> Infos for GGLWETensorKeyPrepared<D, B> {
type Inner = VmpPMat<D, B>;
fn inner(&self) -> &Self::Inner {
@@ -71,7 +53,7 @@ impl<D: Data, B: Backend> Infos for GGLWETensorKeyExec<D, B> {
}
}
impl<D: Data, B: Backend> GGLWETensorKeyExec<D, B> {
impl<D: Data, B: Backend> GGLWETensorKeyPrepared<D, B> {
pub fn rank(&self) -> usize {
self.keys[0].rank()
}
@@ -89,9 +71,9 @@ impl<D: Data, B: Backend> GGLWETensorKeyExec<D, B> {
}
}
impl<D: DataMut, B: Backend> GGLWETensorKeyExec<D, B> {
impl<D: DataMut, B: Backend> GGLWETensorKeyPrepared<D, B> {
// Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j])
pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWESwitchingKeyExec<D, B> {
pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWESwitchingKeyPrepared<D, B> {
if i > j {
std::mem::swap(&mut i, &mut j);
};
@@ -100,9 +82,9 @@ impl<D: DataMut, B: Backend> GGLWETensorKeyExec<D, B> {
}
}
impl<D: DataRef, B: Backend> GGLWETensorKeyExec<D, B> {
impl<D: DataRef, B: Backend> GGLWETensorKeyPrepared<D, B> {
// Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j])
pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWESwitchingKeyExec<D, B> {
pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWESwitchingKeyPrepared<D, B> {
if i > j {
std::mem::swap(&mut i, &mut j);
};
@@ -111,12 +93,11 @@ impl<D: DataRef, B: Backend> GGLWETensorKeyExec<D, B> {
}
}
impl<D: DataMut, B: Backend> GGLWETensorKeyExec<D, B> {
pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &GGLWETensorKey<DataOther>, scratch: &mut Scratch<B>)
where
DataOther: DataRef,
Module<B>: VmpPMatPrepare<B>,
{
impl<D: DataMut, DR: DataRef, B: Backend> Prepare<B, GGLWETensorKey<DR>> for GGLWETensorKeyPrepared<D, B>
where
Module<B>: VmpPMatPrepare<B>,
{
fn prepare(&mut self, module: &Module<B>, other: &GGLWETensorKey<DR>, scratch: &mut Scratch<B>) {
#[cfg(debug_assertions)]
{
assert_eq!(self.keys.len(), other.keys.len());
@@ -129,3 +110,22 @@ impl<D: DataMut, B: Backend> GGLWETensorKeyExec<D, B> {
});
}
}
impl<D: DataRef, B: Backend> PrepareAlloc<B, GGLWETensorKeyPrepared<Vec<u8>, B>> for GGLWETensorKey<D>
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGLWETensorKeyPrepared<Vec<u8>, B> {
let mut tsk_prepared: GGLWETensorKeyPrepared<Vec<u8>, B> = GGLWETensorKeyPrepared::alloc(
module,
self.n(),
self.basek(),
self.k(),
self.rows(),
self.digits(),
self.rank(),
);
tsk_prepared.prepare(module, self, scratch);
tsk_prepared
}
}