use poulpy_hal::layouts::{Backend, DataMut, Module, Scratch}; use crate::{ GGSWExpandRows, ScratchTakeCore, keyswitching::GLWEKeyswitch, layouts::{GGLWEInfos, GGLWEPreparedToRef, GGLWEToGGSWKeyPreparedToRef, GGSW, GGSWInfos, GGSWToMut, GGSWToRef, LWEInfos}, }; impl GGSW> { pub fn keyswitch_tmp_bytes( module: &M, res_infos: &R, a_infos: &A, key_infos: &K, tsk_infos: &T, ) -> usize where R: GGSWInfos, A: GGSWInfos, K: GGLWEInfos, T: GGLWEInfos, M: GGSWKeyswitch, { module.ggsw_keyswitch_tmp_bytes(res_infos, a_infos, key_infos, tsk_infos) } } impl GGSW { pub fn keyswitch(&mut self, module: &M, a: &A, key: &K, tsk: &T, scratch: &mut Scratch) where A: GGSWToRef, K: GGLWEPreparedToRef, T: GGLWEToGGSWKeyPreparedToRef, Scratch: ScratchTakeCore, M: GGSWKeyswitch, { module.ggsw_keyswitch(self, a, key, tsk, scratch); } pub fn keyswitch_inplace(&mut self, module: &M, key: &K, tsk: &T, scratch: &mut Scratch) where K: GGLWEPreparedToRef, T: GGLWEToGGSWKeyPreparedToRef, Scratch: ScratchTakeCore, M: GGSWKeyswitch, { module.ggsw_keyswitch_inplace(self, key, tsk, scratch); } } impl GGSWKeyswitch for Module where Self: GLWEKeyswitch + GGSWExpandRows, { fn ggsw_keyswitch_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K, tsk_infos: &T) -> usize where R: GGSWInfos, A: GGSWInfos, K: GGLWEInfos, T: GGLWEInfos, { assert_eq!(key_infos.rank_in(), key_infos.rank_out()); assert_eq!(tsk_infos.rank_in(), tsk_infos.rank_out()); assert_eq!(key_infos.rank_in(), tsk_infos.rank_in()); self.glwe_keyswitch_tmp_bytes(res_infos, a_infos, key_infos) .max(self.ggsw_expand_rows_tmp_bytes(res_infos, tsk_infos)) } fn ggsw_keyswitch_inplace(&self, res: &mut R, key: &K, tsk: &T, scratch: &mut Scratch) where R: GGSWToMut, K: GGLWEPreparedToRef, T: GGLWEToGGSWKeyPreparedToRef, Scratch: ScratchTakeCore, { let res: &mut GGSW<&mut [u8]> = &mut res.to_mut(); for row in 0..res.dnum().into() { // Key-switch column 0, i.e. // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0, a1, a2) -> (-(a0s0' + a1s1' + a2s2') + M[i], a0, a1, a2) self.glwe_keyswitch_inplace(&mut res.at_mut(row, 0), key, scratch); } self.ggsw_expand_row(res, tsk, scratch); } fn ggsw_keyswitch(&self, res: &mut R, a: &A, key: &K, tsk: &T, scratch: &mut Scratch) where R: GGSWToMut, A: GGSWToRef, K: GGLWEPreparedToRef, T: GGLWEToGGSWKeyPreparedToRef, Scratch: ScratchTakeCore, { 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.base2k(), a.base2k()); for row in 0..a.dnum().into() { // Key-switch column 0, i.e. // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0, a1, a2) -> (-(a0s0' + a1s1' + a2s2') + M[i], a0, a1, a2) self.glwe_keyswitch(&mut res.at_mut(row, 0), &a.at(row, 0), key, scratch); } self.ggsw_expand_row(res, tsk, scratch); } } pub trait GGSWKeyswitch where Self: GLWEKeyswitch + GGSWExpandRows, { fn ggsw_keyswitch_tmp_bytes(&self, res_infos: &R, a_infos: &A, key_infos: &K, tsk_infos: &T) -> usize where R: GGSWInfos, A: GGSWInfos, K: GGLWEInfos, T: GGLWEInfos; fn ggsw_keyswitch(&self, res: &mut R, a: &A, key: &K, tsk: &T, scratch: &mut Scratch) where R: GGSWToMut, A: GGSWToRef, K: GGLWEPreparedToRef, T: GGLWEToGGSWKeyPreparedToRef, Scratch: ScratchTakeCore; fn ggsw_keyswitch_inplace(&self, res: &mut R, key: &K, tsk: &T, scratch: &mut Scratch) where R: GGSWToMut, K: GGLWEPreparedToRef, T: GGLWEToGGSWKeyPreparedToRef, Scratch: ScratchTakeCore; }