Add BDD Arithmetic (#98)

* Added some circuit, evaluation + some layouts

* Refactor + memory reduction

* Rows -> Dnum, Digits -> Dsize

* fix #96 + glwe_packing (indirectly CBT)

* clippy
This commit is contained in:
Jean-Philippe Bossuat
2025-10-08 17:52:03 +02:00
committed by GitHub
parent 37e13b965c
commit 6357a05509
119 changed files with 15996 additions and 1659 deletions

View File

@@ -6,8 +6,8 @@ use poulpy_hal::{
use crate::{
dist::Distribution,
layouts::{
Degree, GGLWEAutomorphismKey, GGLWECiphertext, GGLWELayoutInfos, GGLWESwitchingKey, GGLWETensorKey, GGSWCiphertext,
GGSWInfos, GLWECiphertext, GLWEInfos, GLWEPlaintext, GLWEPublicKey, GLWESecret, Rank,
Degree, GGLWEAutomorphismKey, GGLWECiphertext, GGLWEInfos, GGLWESwitchingKey, GGLWETensorKey, GGSWCiphertext, GGSWInfos,
GLWECiphertext, GLWEInfos, GLWEPlaintext, GLWEPublicKey, GLWESecret, Rank,
prepared::{
GGLWEAutomorphismKeyPrepared, GGLWECiphertextPrepared, GGLWESwitchingKeyPrepared, GGLWETensorKeyPrepared,
GGSWCiphertextPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared,
@@ -36,13 +36,13 @@ pub trait TakeGLWEPt<B: Backend> {
pub trait TakeGGLWE {
fn take_gglwe<A>(&mut self, infos: &A) -> (GGLWECiphertext<&mut [u8]>, &mut Self)
where
A: GGLWELayoutInfos;
A: GGLWEInfos;
}
pub trait TakeGGLWEPrepared<B: Backend> {
fn take_gglwe_prepared<A>(&mut self, infos: &A) -> (GGLWECiphertextPrepared<&mut [u8], B>, &mut Self)
where
A: GGLWELayoutInfos;
A: GGLWEInfos;
}
pub trait TakeGGSW {
@@ -80,37 +80,37 @@ pub trait TakeGLWEPkPrepared<B: Backend> {
pub trait TakeGLWESwitchingKey {
fn take_glwe_switching_key<A>(&mut self, infos: &A) -> (GGLWESwitchingKey<&mut [u8]>, &mut Self)
where
A: GGLWELayoutInfos;
A: GGLWEInfos;
}
pub trait TakeGGLWESwitchingKeyPrepared<B: Backend> {
fn take_gglwe_switching_key_prepared<A>(&mut self, infos: &A) -> (GGLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self)
where
A: GGLWELayoutInfos;
A: GGLWEInfos;
}
pub trait TakeTensorKey {
fn take_tensor_key<A>(&mut self, infos: &A) -> (GGLWETensorKey<&mut [u8]>, &mut Self)
where
A: GGLWELayoutInfos;
A: GGLWEInfos;
}
pub trait TakeGGLWETensorKeyPrepared<B: Backend> {
fn take_gglwe_tensor_key_prepared<A>(&mut self, infos: &A) -> (GGLWETensorKeyPrepared<&mut [u8], B>, &mut Self)
where
A: GGLWELayoutInfos;
A: GGLWEInfos;
}
pub trait TakeGGLWEAutomorphismKey {
fn take_gglwe_automorphism_key<A>(&mut self, infos: &A) -> (GGLWEAutomorphismKey<&mut [u8]>, &mut Self)
where
A: GGLWELayoutInfos;
A: GGLWEInfos;
}
pub trait TakeGGLWEAutomorphismKeyPrepared<B: Backend> {
fn take_gglwe_automorphism_key_prepared<A>(&mut self, infos: &A) -> (GGLWEAutomorphismKeyPrepared<&mut [u8], B>, &mut Self)
where
A: GGLWELayoutInfos;
A: GGLWEInfos;
}
impl<B: Backend> TakeGLWECt for Scratch<B>
@@ -180,11 +180,11 @@ where
{
fn take_gglwe<A>(&mut self, infos: &A) -> (GGLWECiphertext<&mut [u8]>, &mut Self)
where
A: GGLWELayoutInfos,
A: GGLWEInfos,
{
let (data, scratch) = self.take_mat_znx(
infos.n().into(),
infos.rows().0.div_ceil(infos.digits().0) as usize,
infos.dnum().0.div_ceil(infos.dsize().0) as usize,
infos.rank_in().into(),
(infos.rank_out() + 1).into(),
infos.size(),
@@ -193,7 +193,7 @@ where
GGLWECiphertext::builder()
.base2k(infos.base2k())
.k(infos.k())
.digits(infos.digits())
.dsize(infos.dsize())
.data(data)
.build()
.unwrap(),
@@ -208,11 +208,11 @@ where
{
fn take_gglwe_prepared<A>(&mut self, infos: &A) -> (GGLWECiphertextPrepared<&mut [u8], B>, &mut Self)
where
A: GGLWELayoutInfos,
A: GGLWEInfos,
{
let (data, scratch) = self.take_vmp_pmat(
infos.n().into(),
infos.rows().into(),
infos.dnum().into(),
infos.rank_in().into(),
(infos.rank_out() + 1).into(),
infos.size(),
@@ -220,7 +220,7 @@ where
(
GGLWECiphertextPrepared::builder()
.base2k(infos.base2k())
.digits(infos.digits())
.dsize(infos.dsize())
.k(infos.k())
.data(data)
.build()
@@ -240,7 +240,7 @@ where
{
let (data, scratch) = self.take_mat_znx(
infos.n().into(),
infos.rows().into(),
infos.dnum().into(),
(infos.rank() + 1).into(),
(infos.rank() + 1).into(),
infos.size(),
@@ -248,7 +248,7 @@ where
(
GGSWCiphertext::builder()
.base2k(infos.base2k())
.digits(infos.digits())
.dsize(infos.dsize())
.k(infos.k())
.data(data)
.build()
@@ -268,7 +268,7 @@ where
{
let (data, scratch) = self.take_vmp_pmat(
infos.n().into(),
infos.rows().into(),
infos.dnum().into(),
(infos.rank() + 1).into(),
(infos.rank() + 1).into(),
infos.size(),
@@ -276,7 +276,7 @@ where
(
GGSWCiphertextPrepared::builder()
.base2k(infos.base2k())
.digits(infos.digits())
.dsize(infos.dsize())
.k(infos.k())
.data(data)
.build()
@@ -367,7 +367,7 @@ where
{
fn take_glwe_switching_key<A>(&mut self, infos: &A) -> (GGLWESwitchingKey<&mut [u8]>, &mut Self)
where
A: GGLWELayoutInfos,
A: GGLWEInfos,
{
let (data, scratch) = self.take_gglwe(infos);
(
@@ -387,7 +387,7 @@ where
{
fn take_gglwe_switching_key_prepared<A>(&mut self, infos: &A) -> (GGLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self)
where
A: GGLWELayoutInfos,
A: GGLWEInfos,
{
let (data, scratch) = self.take_gglwe_prepared(infos);
(
@@ -407,7 +407,7 @@ where
{
fn take_gglwe_automorphism_key<A>(&mut self, infos: &A) -> (GGLWEAutomorphismKey<&mut [u8]>, &mut Self)
where
A: GGLWELayoutInfos,
A: GGLWEInfos,
{
let (data, scratch) = self.take_glwe_switching_key(infos);
(GGLWEAutomorphismKey { key: data, p: 0 }, scratch)
@@ -420,7 +420,7 @@ where
{
fn take_gglwe_automorphism_key_prepared<A>(&mut self, infos: &A) -> (GGLWEAutomorphismKeyPrepared<&mut [u8], B>, &mut Self)
where
A: GGLWELayoutInfos,
A: GGLWEInfos,
{
let (data, scratch) = self.take_gglwe_switching_key_prepared(infos);
(GGLWEAutomorphismKeyPrepared { key: data, p: 0 }, scratch)
@@ -433,7 +433,7 @@ where
{
fn take_tensor_key<A>(&mut self, infos: &A) -> (GGLWETensorKey<&mut [u8]>, &mut Self)
where
A: GGLWELayoutInfos,
A: GGLWEInfos,
{
assert_eq!(
infos.rank_in(),
@@ -468,7 +468,7 @@ where
{
fn take_gglwe_tensor_key_prepared<A>(&mut self, infos: &A) -> (GGLWETensorKeyPrepared<&mut [u8], B>, &mut Self)
where
A: GGLWELayoutInfos,
A: GGLWEInfos,
{
assert_eq!(
infos.rank_in(),