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,12 +3,15 @@ use backend::hal::{
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat},
};
use crate::layouts::{Infos, LWESwitchingKey, prepared::GGLWESwitchingKeyExec};
use crate::layouts::{
Infos, LWESwitchingKey,
prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc},
};
#[derive(PartialEq, Eq)]
pub struct LWESwitchingKeyExec<D: Data, B: Backend>(pub(crate) GGLWESwitchingKeyExec<D, B>);
pub struct LWESwitchingKeyPrepared<D: Data, B: Backend>(pub(crate) GGLWESwitchingKeyPrepared<D, B>);
impl<D: Data, B: Backend> Infos for LWESwitchingKeyExec<D, B> {
impl<D: Data, B: Backend> Infos for LWESwitchingKeyPrepared<D, B> {
type Inner = VmpPMat<D, B>;
fn inner(&self) -> &Self::Inner {
@@ -24,7 +27,7 @@ impl<D: Data, B: Backend> Infos for LWESwitchingKeyExec<D, B> {
}
}
impl<D: Data, B: Backend> LWESwitchingKeyExec<D, B> {
impl<D: Data, B: Backend> LWESwitchingKeyPrepared<D, B> {
pub fn digits(&self) -> usize {
self.0.digits()
}
@@ -42,12 +45,12 @@ impl<D: Data, B: Backend> LWESwitchingKeyExec<D, B> {
}
}
impl<B: Backend> LWESwitchingKeyExec<Vec<u8>, B> {
impl<B: Backend> LWESwitchingKeyPrepared<Vec<u8>, B> {
pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize) -> Self
where
Module<B>: VmpPMatAlloc<B>,
{
Self(GGLWESwitchingKeyExec::alloc(
Self(GGLWESwitchingKeyPrepared::alloc(
module, n, basek, k, rows, 1, 1, 1,
))
}
@@ -56,31 +59,32 @@ impl<B: Backend> LWESwitchingKeyExec<Vec<u8>, B> {
where
Module<B>: VmpPMatAllocBytes,
{
GGLWESwitchingKeyExec::<Vec<u8>, B>::bytes_of(module, n, basek, k, rows, digits, 1, 1)
}
pub fn from<DataOther: DataRef>(module: &Module<B>, other: &LWESwitchingKey<DataOther>, scratch: &mut Scratch<B>) -> Self
where
Module<B>: VmpPMatAlloc<B> + VmpPMatPrepare<B>,
{
let mut ksk_exec: LWESwitchingKeyExec<Vec<u8>, B> = Self::alloc(
module,
other.0.n(),
other.0.basek(),
other.0.k(),
other.0.rows(),
);
ksk_exec.prepare(module, other, scratch);
ksk_exec
GGLWESwitchingKeyPrepared::<Vec<u8>, B>::bytes_of(module, n, basek, k, rows, digits, 1, 1)
}
}
impl<D: DataMut, B: Backend> LWESwitchingKeyExec<D, B> {
pub fn prepare<DataOther>(&mut self, module: &Module<B>, other: &LWESwitchingKey<DataOther>, scratch: &mut Scratch<B>)
where
DataOther: DataRef,
Module<B>: VmpPMatPrepare<B>,
{
impl<D: DataRef, B: Backend> PrepareAlloc<B, LWESwitchingKeyPrepared<Vec<u8>, B>> for LWESwitchingKey<D>
where
Module<B>: VmpPMatPrepare<B> + VmpPMatAlloc<B>,
{
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> LWESwitchingKeyPrepared<Vec<u8>, B> {
let mut ksk_prepared: LWESwitchingKeyPrepared<Vec<u8>, B> = LWESwitchingKeyPrepared::alloc(
module,
self.0.n(),
self.0.basek(),
self.0.k(),
self.0.rows(),
);
ksk_prepared.prepare(module, self, scratch);
ksk_prepared
}
}
impl<DM: DataMut, DR: DataRef, B: Backend> Prepare<B, LWESwitchingKey<DR>> for LWESwitchingKeyPrepared<DM, B>
where
Module<B>: VmpPMatPrepare<B>,
{
fn prepare(&mut self, module: &Module<B>, other: &LWESwitchingKey<DR>, scratch: &mut Scratch<B>) {
self.0.prepare(module, &other.0, scratch);
}
}