Ref. + AVX code & generic tests + benches (#85)

This commit is contained in:
Jean-Philippe Bossuat
2025-09-15 16:16:11 +02:00
committed by GitHub
parent 99b9e3e10e
commit 56dbd29c59
286 changed files with 27797 additions and 7270 deletions

View File

@@ -1,19 +1,24 @@
use crate::{
api::{
VecZnxAdd, VecZnxAddDistF64, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism,
VecZnxAutomorphismInplace, VecZnxCopy, VecZnxFillDistF64, VecZnxFillNormal, VecZnxFillUniform, VecZnxLshInplace,
VecZnxMerge, VecZnxMulXpMinusOne, VecZnxMulXpMinusOneInplace, VecZnxNegate, VecZnxNegateInplace, VecZnxNormalize,
VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSplit,
VecZnxSub, VecZnxSubABInplace, VecZnxSubBAInplace, VecZnxSubScalarInplace, VecZnxSwithcDegree,
VecZnxAdd, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalar, VecZnxAddScalarInplace, VecZnxAutomorphism,
VecZnxAutomorphismInplace, VecZnxAutomorphismInplaceTmpBytes, VecZnxCopy, VecZnxFillNormal, VecZnxFillUniform, VecZnxLsh,
VecZnxLshInplace, VecZnxLshTmpBytes, VecZnxMergeRings, VecZnxMergeRingsTmpBytes, VecZnxMulXpMinusOne,
VecZnxMulXpMinusOneInplace, VecZnxMulXpMinusOneInplaceTmpBytes, VecZnxNegate, VecZnxNegateInplace, VecZnxNormalize,
VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRotateInplaceTmpBytes,
VecZnxRsh, VecZnxRshInplace, VecZnxRshTmpBytes, VecZnxSplitRing, VecZnxSplitRingTmpBytes, VecZnxSub, VecZnxSubABInplace,
VecZnxSubBAInplace, VecZnxSubScalar, VecZnxSubScalarInplace, VecZnxSwitchRing,
},
layouts::{Backend, Module, ScalarZnxToRef, Scratch, VecZnxToMut, VecZnxToRef},
oep::{
VecZnxAddDistF64Impl, VecZnxAddImpl, VecZnxAddInplaceImpl, VecZnxAddNormalImpl, VecZnxAddScalarInplaceImpl,
VecZnxAutomorphismImpl, VecZnxAutomorphismInplaceImpl, VecZnxCopyImpl, VecZnxFillDistF64Impl, VecZnxFillNormalImpl,
VecZnxFillUniformImpl, VecZnxLshInplaceImpl, VecZnxMergeImpl, VecZnxMulXpMinusOneImpl, VecZnxMulXpMinusOneInplaceImpl,
VecZnxNegateImpl, VecZnxNegateInplaceImpl, VecZnxNormalizeImpl, VecZnxNormalizeInplaceImpl, VecZnxNormalizeTmpBytesImpl,
VecZnxRotateImpl, VecZnxRotateInplaceImpl, VecZnxRshInplaceImpl, VecZnxSplitImpl, VecZnxSubABInplaceImpl,
VecZnxSubBAInplaceImpl, VecZnxSubImpl, VecZnxSubScalarInplaceImpl, VecZnxSwithcDegreeImpl,
VecZnxAddImpl, VecZnxAddInplaceImpl, VecZnxAddNormalImpl, VecZnxAddScalarImpl, VecZnxAddScalarInplaceImpl,
VecZnxAutomorphismImpl, VecZnxAutomorphismInplaceImpl, VecZnxAutomorphismInplaceTmpBytesImpl, VecZnxCopyImpl,
VecZnxFillNormalImpl, VecZnxFillUniformImpl, VecZnxLshImpl, VecZnxLshInplaceImpl, VecZnxLshTmpBytesImpl,
VecZnxMergeRingsImpl, VecZnxMergeRingsTmpBytesImpl, VecZnxMulXpMinusOneImpl, VecZnxMulXpMinusOneInplaceImpl,
VecZnxMulXpMinusOneInplaceTmpBytesImpl, VecZnxNegateImpl, VecZnxNegateInplaceImpl, VecZnxNormalizeImpl,
VecZnxNormalizeInplaceImpl, VecZnxNormalizeTmpBytesImpl, VecZnxRotateImpl, VecZnxRotateInplaceImpl,
VecZnxRotateInplaceTmpBytesImpl, VecZnxRshImpl, VecZnxRshInplaceImpl, VecZnxRshTmpBytesImpl, VecZnxSplitRingImpl,
VecZnxSplitRingTmpBytesImpl, VecZnxSubABInplaceImpl, VecZnxSubBAInplaceImpl, VecZnxSubImpl, VecZnxSubScalarImpl,
VecZnxSubScalarInplaceImpl, VecZnxSwitchRingImpl,
},
source::Source,
};
@@ -79,6 +84,20 @@ where
}
}
impl<B> VecZnxAddScalar for Module<B>
where
B: Backend + VecZnxAddScalarImpl<B>,
{
fn vec_znx_add_scalar<R, A, D>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &D, b_col: usize, b_limb: usize)
where
R: VecZnxToMut,
A: ScalarZnxToRef,
D: VecZnxToRef,
{
B::vec_znx_add_scalar_impl(self, res, res_col, a, a_col, b, b_col, b_limb)
}
}
impl<B> VecZnxAddScalarInplace for Module<B>
where
B: Backend + VecZnxAddScalarInplaceImpl<B>,
@@ -132,6 +151,20 @@ where
}
}
impl<B> VecZnxSubScalar for Module<B>
where
B: Backend + VecZnxSubScalarImpl<B>,
{
fn vec_znx_sub_scalar<R, A, D>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize, b: &D, b_col: usize, b_limb: usize)
where
R: VecZnxToMut,
A: ScalarZnxToRef,
D: VecZnxToRef,
{
B::vec_znx_sub_scalar_impl(self, res, res_col, a, a_col, b, b_col, b_limb)
}
}
impl<B> VecZnxSubScalarInplace for Module<B>
where
B: Backend + VecZnxSubScalarInplaceImpl<B>,
@@ -170,27 +203,87 @@ where
}
}
impl<B> VecZnxLshInplace for Module<B>
impl<B> VecZnxRshTmpBytes for Module<B>
where
B: Backend + VecZnxLshInplaceImpl<B>,
B: Backend + VecZnxRshTmpBytesImpl<B>,
{
fn vec_znx_lsh_inplace<A>(&self, basek: usize, k: usize, a: &mut A)
where
A: VecZnxToMut,
{
B::vec_znx_lsh_inplace_impl(self, basek, k, a)
fn vec_znx_rsh_tmp_bytes(&self) -> usize {
B::vec_znx_rsh_tmp_bytes_impl(self)
}
}
impl<B> VecZnxRshInplace for Module<B>
impl<B> VecZnxLshTmpBytes for Module<B>
where
B: Backend + VecZnxRshInplaceImpl<B>,
B: Backend + VecZnxLshTmpBytesImpl<B>,
{
fn vec_znx_rsh_inplace<A>(&self, basek: usize, k: usize, a: &mut A)
fn vec_znx_lsh_tmp_bytes(&self) -> usize {
B::vec_znx_lsh_tmp_bytes_impl(self)
}
}
impl<B> VecZnxLsh<B> for Module<B>
where
B: Backend + VecZnxLshImpl<B>,
{
fn vec_znx_lsh<R, A>(
&self,
basek: usize,
k: usize,
res: &mut R,
res_col: usize,
a: &A,
a_col: usize,
scratch: &mut Scratch<B>,
) where
R: VecZnxToMut,
A: VecZnxToRef,
{
B::vec_znx_lsh_inplace_impl(self, basek, k, res, res_col, a, a_col, scratch);
}
}
impl<B> VecZnxRsh<B> for Module<B>
where
B: Backend + VecZnxRshImpl<B>,
{
fn vec_znx_rsh<R, A>(
&self,
basek: usize,
k: usize,
res: &mut R,
res_col: usize,
a: &A,
a_col: usize,
scratch: &mut Scratch<B>,
) where
R: VecZnxToMut,
A: VecZnxToRef,
{
B::vec_znx_rsh_inplace_impl(self, basek, k, res, res_col, a, a_col, scratch);
}
}
impl<B> VecZnxLshInplace<B> for Module<B>
where
B: Backend + VecZnxLshInplaceImpl<B>,
{
fn vec_znx_lsh_inplace<A>(&self, basek: usize, k: usize, a: &mut A, a_col: usize, scratch: &mut Scratch<B>)
where
A: VecZnxToMut,
{
B::vec_znx_rsh_inplace_impl(self, basek, k, a)
B::vec_znx_lsh_inplace_impl(self, basek, k, a, a_col, scratch)
}
}
impl<B> VecZnxRshInplace<B> for Module<B>
where
B: Backend + VecZnxRshInplaceImpl<B>,
{
fn vec_znx_rsh_inplace<A>(&self, basek: usize, k: usize, a: &mut A, a_col: usize, scratch: &mut Scratch<B>)
where
A: VecZnxToMut,
{
B::vec_znx_rsh_inplace_impl(self, basek, k, a, a_col, scratch)
}
}
@@ -207,15 +300,24 @@ where
}
}
impl<B> VecZnxRotateInplace for Module<B>
impl<B> VecZnxRotateInplaceTmpBytes for Module<B>
where
B: Backend + VecZnxRotateInplaceTmpBytesImpl<B>,
{
fn vec_znx_rotate_inplace_tmp_bytes(&self) -> usize {
B::vec_znx_rotate_inplace_tmp_bytes_impl(self)
}
}
impl<B> VecZnxRotateInplace<B> for Module<B>
where
B: Backend + VecZnxRotateInplaceImpl<B>,
{
fn vec_znx_rotate_inplace<A>(&self, k: i64, a: &mut A, a_col: usize)
fn vec_znx_rotate_inplace<A>(&self, k: i64, a: &mut A, a_col: usize, scratch: &mut Scratch<B>)
where
A: VecZnxToMut,
{
B::vec_znx_rotate_inplace_impl(self, k, a, a_col)
B::vec_znx_rotate_inplace_impl(self, k, a, a_col, scratch)
}
}
@@ -232,15 +334,24 @@ where
}
}
impl<B> VecZnxAutomorphismInplace for Module<B>
impl<B> VecZnxAutomorphismInplaceTmpBytes for Module<B>
where
B: Backend + VecZnxAutomorphismInplaceTmpBytesImpl<B>,
{
fn vec_znx_automorphism_inplace_tmp_bytes(&self) -> usize {
B::vec_znx_automorphism_inplace_tmp_bytes_impl(self)
}
}
impl<B> VecZnxAutomorphismInplace<B> for Module<B>
where
B: Backend + VecZnxAutomorphismInplaceImpl<B>,
{
fn vec_znx_automorphism_inplace<A>(&self, k: i64, a: &mut A, a_col: usize)
fn vec_znx_automorphism_inplace<R>(&self, k: i64, res: &mut R, res_col: usize, scratch: &mut Scratch<B>)
where
A: VecZnxToMut,
R: VecZnxToMut,
{
B::vec_znx_automorphism_inplace_impl(self, k, a, a_col)
B::vec_znx_automorphism_inplace_impl(self, k, res, res_col, scratch)
}
}
@@ -257,54 +368,81 @@ where
}
}
impl<B> VecZnxMulXpMinusOneInplace for Module<B>
impl<B> VecZnxMulXpMinusOneInplaceTmpBytes for Module<B>
where
B: Backend + VecZnxMulXpMinusOneInplaceTmpBytesImpl<B>,
{
fn vec_znx_mul_xp_minus_one_inplace_tmp_bytes(&self) -> usize {
B::vec_znx_mul_xp_minus_one_inplace_tmp_bytes_impl(self)
}
}
impl<B> VecZnxMulXpMinusOneInplace<B> for Module<B>
where
B: Backend + VecZnxMulXpMinusOneInplaceImpl<B>,
{
fn vec_znx_mul_xp_minus_one_inplace<R>(&self, p: i64, res: &mut R, res_col: usize)
fn vec_znx_mul_xp_minus_one_inplace<R>(&self, p: i64, res: &mut R, res_col: usize, scratch: &mut Scratch<B>)
where
R: VecZnxToMut,
{
B::vec_znx_mul_xp_minus_one_inplace_impl(self, p, res, res_col);
B::vec_znx_mul_xp_minus_one_inplace_impl(self, p, res, res_col, scratch);
}
}
impl<B> VecZnxSplit<B> for Module<B>
impl<B> VecZnxSplitRingTmpBytes for Module<B>
where
B: Backend + VecZnxSplitImpl<B>,
B: Backend + VecZnxSplitRingTmpBytesImpl<B>,
{
fn vec_znx_split<R, A>(&self, res: &mut [R], res_col: usize, a: &A, a_col: usize, scratch: &mut Scratch<B>)
fn vec_znx_split_ring_tmp_bytes(&self) -> usize {
B::vec_znx_split_ring_tmp_bytes_impl(self)
}
}
impl<B> VecZnxSplitRing<B> for Module<B>
where
B: Backend + VecZnxSplitRingImpl<B>,
{
fn vec_znx_split_ring<R, A>(&self, res: &mut [R], res_col: usize, a: &A, a_col: usize, scratch: &mut Scratch<B>)
where
R: VecZnxToMut,
A: VecZnxToRef,
{
B::vec_znx_split_impl(self, res, res_col, a, a_col, scratch)
B::vec_znx_split_ring_impl(self, res, res_col, a, a_col, scratch)
}
}
impl<B> VecZnxMerge for Module<B>
impl<B> VecZnxMergeRingsTmpBytes for Module<B>
where
B: Backend + VecZnxMergeImpl<B>,
B: Backend + VecZnxMergeRingsTmpBytesImpl<B>,
{
fn vec_znx_merge<R, A>(&self, res: &mut R, res_col: usize, a: &[A], a_col: usize)
fn vec_znx_merge_rings_tmp_bytes(&self) -> usize {
B::vec_znx_merge_rings_tmp_bytes_impl(self)
}
}
impl<B> VecZnxMergeRings<B> for Module<B>
where
B: Backend + VecZnxMergeRingsImpl<B>,
{
fn vec_znx_merge_rings<R, A>(&self, res: &mut R, res_col: usize, a: &[A], a_col: usize, scratch: &mut Scratch<B>)
where
R: VecZnxToMut,
A: VecZnxToRef,
{
B::vec_znx_merge_impl(self, res, res_col, a, a_col)
B::vec_znx_merge_rings_impl(self, res, res_col, a, a_col, scratch)
}
}
impl<B> VecZnxSwithcDegree for Module<B>
impl<B> VecZnxSwitchRing for Module<B>
where
B: Backend + VecZnxSwithcDegreeImpl<B>,
B: Backend + VecZnxSwitchRingImpl<B>,
{
fn vec_znx_switch_degree<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
fn vec_znx_switch_ring<R, A>(&self, res: &mut R, res_col: usize, a: &A, a_col: usize)
where
R: VecZnxToMut,
A: VecZnxToRef,
{
B::vec_znx_switch_degree_impl(self, res, res_col, a, a_col)
B::vec_znx_switch_ring_impl(self, res, res_col, a, a_col)
}
}
@@ -325,51 +463,11 @@ impl<B> VecZnxFillUniform for Module<B>
where
B: Backend + VecZnxFillUniformImpl<B>,
{
fn vec_znx_fill_uniform<R>(&self, basek: usize, res: &mut R, res_col: usize, k: usize, source: &mut Source)
fn vec_znx_fill_uniform<R>(&self, basek: usize, res: &mut R, res_col: usize, source: &mut Source)
where
R: VecZnxToMut,
{
B::vec_znx_fill_uniform_impl(self, basek, res, res_col, k, source);
}
}
impl<B> VecZnxFillDistF64 for Module<B>
where
B: Backend + VecZnxFillDistF64Impl<B>,
{
fn vec_znx_fill_dist_f64<R, D: rand::prelude::Distribution<f64>>(
&self,
basek: usize,
res: &mut R,
res_col: usize,
k: usize,
source: &mut Source,
dist: D,
bound: f64,
) where
R: VecZnxToMut,
{
B::vec_znx_fill_dist_f64_impl(self, basek, res, res_col, k, source, dist, bound);
}
}
impl<B> VecZnxAddDistF64 for Module<B>
where
B: Backend + VecZnxAddDistF64Impl<B>,
{
fn vec_znx_add_dist_f64<R, D: rand::prelude::Distribution<f64>>(
&self,
basek: usize,
res: &mut R,
res_col: usize,
k: usize,
source: &mut Source,
dist: D,
bound: f64,
) where
R: VecZnxToMut,
{
B::vec_znx_add_dist_f64_impl(self, basek, res, res_col, k, source, dist, bound);
B::vec_znx_fill_uniform_impl(self, basek, res, res_col, source);
}
}