prolly a mistake

This commit is contained in:
Janmajaya Mall
2024-06-11 11:24:03 +05:30
parent a6ced5c036
commit 80ae5d7c8f
6 changed files with 133 additions and 104 deletions

View File

@@ -15,7 +15,9 @@ use num_traits::{FromPrimitive, Num, One, Pow, PrimInt, ToPrimitive, WrappingSub
use rand_distr::uniform::SampleUniform;
use crate::{
backend::{ArithmeticOps, GetModulus, ModInit, ModularOpsU64, Modulus, VectorOps},
backend::{
ArithmeticOps, GetModulus, ModInit, ModularOpsU64, Modulus, ShoupMatrixFMA, VectorOps,
},
decomposer::{Decomposer, DefaultDecomposer, NumInfo, RlweDecomposer},
lwe::{decrypt_lwe, encrypt_lwe, lwe_key_switch, lwe_ksk_keygen, measure_noise_lwe, LweSecret},
multi_party::public_key_share,
@@ -43,6 +45,7 @@ use super::{
parameters::{BoolParameters, CiphertextModulus},
CommonReferenceSeededCollectivePublicKeyShare, CommonReferenceSeededMultiPartyServerKeyShare,
SeededMultiPartyServerKey, SeededServerKey, ServerKeyEvaluationDomain,
ShoupServerKeyEvaluationDomain,
};
pub struct MultiPartyCrs<S> {
@@ -78,7 +81,7 @@ impl<S: Default + Copy> MultiPartyCrs<S> {
pub(crate) trait BooleanGates {
type Ciphertext: RowEntity;
type Key;
type Key: Global;
fn and_inplace(&mut self, c0: &mut Self::Ciphertext, c1: &Self::Ciphertext, key: &Self::Key);
fn nand_inplace(&mut self, c0: &mut Self::Ciphertext, c1: &Self::Ciphertext, key: &Self::Key);
@@ -231,12 +234,12 @@ pub(super) struct BoolPbsInfo<M: Matrix, Ntt, RlweModOp, LweModOp> {
impl<M: Matrix, NttOp, RlweModOp, LweModOp> PbsInfo for BoolPbsInfo<M, NttOp, RlweModOp, LweModOp>
where
M::MatElement: PrimInt + WrappingSub + NumInfo + FromPrimitive + From<bool> + Display,
RlweModOp: ArithmeticOps<Element = M::MatElement> + VectorOps<Element = M::MatElement>,
RlweModOp: ArithmeticOps<Element = M::MatElement> + ShoupMatrixFMA<M::R>,
LweModOp: ArithmeticOps<Element = M::MatElement> + VectorOps<Element = M::MatElement>,
NttOp: Ntt<Element = M::MatElement>,
{
type M = M;
type Modulus = CiphertextModulus<M::MatElement>;
type Element = M::MatElement;
type D = DefaultDecomposer<M::MatElement>;
type RlweModOp = RlweModOp;
type LweModOp = LweModOp;
@@ -291,7 +294,7 @@ where
}
}
pub(crate) struct BoolEvaluator<M, Ntt, RlweModOp, LweModOp>
pub(crate) struct BoolEvaluator<Info, SKey>
where
M: Matrix,
{
@@ -316,7 +319,7 @@ impl<M: Matrix, NttOp, RlweModOp, LweModOp> BoolEvaluator<M, NttOp, RlweModOp, L
}
}
impl<M: Matrix, NttOp, RlweModOp, LweModOp> BoolEvaluator<M, NttOp, RlweModOp, LweModOp>
impl<M: Matrix, NttOp, RlweModOp, LweModOp, SKey> BoolEvaluator<M, NttOp, RlweModOp, LweModOp, SKey>
where
M: MatrixEntity + MatrixMut,
M::MatElement: PrimInt
@@ -330,7 +333,8 @@ where
NttOp: Ntt<Element = M::MatElement>,
RlweModOp: ArithmeticOps<Element = M::MatElement>
+ VectorOps<Element = M::MatElement>
+ GetModulus<Element = M::MatElement, M = CiphertextModulus<M::MatElement>>,
+ GetModulus<Element = M::MatElement, M = CiphertextModulus<M::MatElement>>
+ ShoupMatrixFMA<M::R>,
LweModOp: ArithmeticOps<Element = M::MatElement>
+ VectorOps<Element = M::MatElement>
+ GetModulus<Element = M::MatElement, M = CiphertextModulus<M::MatElement>>,
@@ -1083,12 +1087,8 @@ where
M: MatrixMut + MatrixEntity,
M::R: RowMut + RowEntity,
M::MatElement: PrimInt + FromPrimitive + One + Copy + Zero + Display + WrappingSub + NumInfo,
RlweModOp: VectorOps<Element = M::MatElement>
+ ArithmeticOps<Element = M::MatElement>
+ GetModulus<Element = M::MatElement, M = CiphertextModulus<M::MatElement>>,
LweModOp: VectorOps<Element = M::MatElement>
+ ArithmeticOps<Element = M::MatElement>
+ GetModulus<Element = M::MatElement, M = CiphertextModulus<M::MatElement>>,
RlweModOp: VectorOps<Element = M::MatElement> + ArithmeticOps<Element = M::MatElement>,
LweModOp: VectorOps<Element = M::MatElement> + ArithmeticOps<Element = M::MatElement>,
NttOp: Ntt<Element = M::MatElement>,
{
/// Returns c0 + c1 + Q/4
@@ -1118,14 +1118,12 @@ where
PrimInt + FromPrimitive + One + Copy + Zero + Display + WrappingSub + NumInfo + From<bool>,
RlweModOp: VectorOps<Element = M::MatElement>
+ ArithmeticOps<Element = M::MatElement>
+ GetModulus<Element = M::MatElement, M = CiphertextModulus<M::MatElement>>,
LweModOp: VectorOps<Element = M::MatElement>
+ ArithmeticOps<Element = M::MatElement>
+ GetModulus<Element = M::MatElement, M = CiphertextModulus<M::MatElement>>,
+ ShoupMatrixFMA<M::R>,
LweModOp: VectorOps<Element = M::MatElement> + ArithmeticOps<Element = M::MatElement>,
NttOp: Ntt<Element = M::MatElement>,
{
type Ciphertext = M::R;
type Key = ServerKeyEvaluationDomain<M, BoolParameters<u64>, DefaultSecureRng, NttOp>;
type Key = Key;
fn nand_inplace(&mut self, c0: &mut M::R, c1: &M::R, server_key: &Self::Key) {
self._add_and_shift_lwe_cts(c0, c1);
@@ -2053,14 +2051,8 @@ where
// rlwe_ct[0].copy_from_slice(&auto_key_i[i]);
// rlwe_ct[1].copy_from_slice(
// &auto_key_i[auto_decomposer.decomposition_count()
// + i], );
// decrypt_rlwe(
// &rlwe_ct,
// ideal_client_key.sk_rlwe().values(),
// &mut m_out,
// rlwe_nttop,
// rlwe_modop,
// );
// + i], ); decrypt_rlwe( &rlwe_ct,
// ideal_client_key.sk_rlwe().values(), &mut m_out, rlwe_nttop, rlwe_modop, );
// // diff
// rlwe_modop.elwise_sub_mut(m_out.as_mut_slice(),

View File

@@ -3,6 +3,7 @@ use std::{collections::HashMap, hash::Hash, marker::PhantomData};
use crate::{
backend::{ModInit, VectorOps},
lwe::LweSecret,
pbs::WithShoupRepr,
random::{NewWithSeed, RandomFillUniformInModulus},
rgsw::RlweSecret,
utils::WithLocal,
@@ -682,6 +683,19 @@ pub(crate) struct ShoupServerKeyEvaluationDomain<M, P, R, N> {
pub(crate) struct NormalAndShoup<M>(M, M);
impl<M> AsRef<M> for NormalAndShoup<M> {
fn as_ref(&self) -> &M {
&self.0
}
}
impl<M> WithShoupRepr for NormalAndShoup<M> {
type M = M;
fn shoup_repr(&self) -> &Self::M {
&self.1
}
}
mod shoup_server_key_eval_domain {
use crate::pbs::PbsKey;

View File

@@ -23,7 +23,12 @@ thread_local! {
}
static BOOL_SERVER_KEY: OnceLock<
ServerKeyEvaluationDomain<Vec<Vec<u64>>, BoolParameters<u64>, DefaultSecureRng, NttBackendU64>,
ShoupServerKeyEvaluationDomain<
Vec<Vec<u64>>,
BoolParameters<u64>,
DefaultSecureRng,
NttBackendU64,
>,
> = OnceLock::new();
static MULTI_PARTY_CRS: OnceLock<MultiPartyCrs<[u8; 32]>> = OnceLock::new();
@@ -40,7 +45,7 @@ pub fn set_mp_seed(seed: [u8; 32]) {
}
fn set_server_key(
key: ServerKeyEvaluationDomain<
key: ShoupServerKeyEvaluationDomain<
Vec<Vec<u64>>,
BoolParameters<u64>,
DefaultSecureRng,
@@ -140,7 +145,7 @@ impl
}
impl Global
for ServerKeyEvaluationDomain<
for ShoupServerKeyEvaluationDomain<
Vec<Vec<u64>>,
BoolParameters<u64>,
DefaultSecureRng,

View File

@@ -3,12 +3,14 @@ use std::{fmt::Display, marker::PhantomData};
use num_traits::{FromPrimitive, One, PrimInt, ToPrimitive, Zero};
use crate::{
backend::{ArithmeticOps, Modulus, VectorOps},
backend::{ArithmeticOps, Modulus, ShoupMatrixFMA, VectorOps},
decomposer::Decomposer,
lwe::lwe_key_switch,
ntt::Ntt,
random::DefaultSecureRng,
rgsw::{galois_auto, rlwe_by_rgsw, rlwe_by_rgsw_shoup, IsTrivial, RlweCiphertext},
rgsw::{
galois_auto, galois_auto_shoup, rlwe_by_rgsw, rlwe_by_rgsw_shoup, IsTrivial, RlweCiphertext,
},
Matrix, MatrixEntity, MatrixMut, RowMut,
};
pub(crate) trait PbsKey {
@@ -24,22 +26,24 @@ pub(crate) trait PbsKey {
fn lwe_ksk(&self) -> &Self::LweKskKey;
}
trait WithShoupRepr: AsRef<Self::M> {
pub(crate) trait WithShoupRepr: AsRef<Self::M> {
type M;
fn shoup_repr(&self) -> Self::M;
fn shoup_repr(&self) -> &Self::M;
}
pub(crate) trait PbsInfo {
type Element;
type Modulus: Modulus<Element = Self::Element>;
type NttOp: Ntt<Element = Self::Element>;
type D: Decomposer<Element = Self::Element>;
type M: Matrix;
type Modulus: Modulus<Element = <Self::M as Matrix>::MatElement>;
type NttOp: Ntt<Element = <Self::M as Matrix>::MatElement>;
type D: Decomposer<Element = <Self::M as Matrix>::MatElement>;
// Although both types have same bounds, they can be different types. For ex,
// type RlweModOp may only support native modulus, where LweModOp may only
// support prime modulus, etc.
type RlweModOp: VectorOps<Element = Self::Element> + ArithmeticOps<Element = Self::Element>;
type LweModOp: VectorOps<Element = Self::Element> + ArithmeticOps<Element = Self::Element>;
type RlweModOp: ArithmeticOps<Element = <Self::M as Matrix>::MatElement>
+ ShoupMatrixFMA<<Self::M as Matrix>::R>;
type LweModOp: VectorOps<Element = <Self::M as Matrix>::MatElement>
+ ArithmeticOps<Element = <Self::M as Matrix>::MatElement>;
fn rlwe_q(&self) -> &Self::Modulus;
fn lwe_q(&self) -> &Self::Modulus;
@@ -79,8 +83,9 @@ pub(crate) trait PbsInfo {
/// - blind rotate
pub(crate) fn pbs<
M: MatrixMut + MatrixEntity,
P: PbsInfo<Element = M::MatElement>,
K: PbsKey<RgswCt = M, AutoKey = M, LweKskKey = M>,
MShoup: WithShoupRepr<M = M>,
P: PbsInfo<M = M>,
K: PbsKey<RgswCt = MShoup, AutoKey = MShoup, LweKskKey = M>,
>(
pbs_info: &P,
test_vec: &M::R,
@@ -217,10 +222,10 @@ fn blind_rotation<
Mmut: MatrixMut<MatElement = MT::MatElement>,
D: Decomposer<Element = MT::MatElement>,
NttOp: Ntt<Element = MT::MatElement>,
ModOp: ArithmeticOps<Element = MT::MatElement> + VectorOps<Element = MT::MatElement>,
ModOp: ArithmeticOps<Element = MT::MatElement> + ShoupMatrixFMA<Mmut::R>,
MShoup: WithShoupRepr<M = Mmut>,
K: PbsKey<RgswCt = MShoup, AutoKey = MShoup>,
P: PbsInfo<Element = MT::MatElement>,
P: PbsInfo<M = Mmut>,
>(
trivial_rlwe_test_poly: &mut MT,
scratch_matrix: &mut Mmut,
@@ -249,19 +254,11 @@ fn blind_rotation<
s_indices.iter().for_each(|s_index| {
// let new = std::time::Instant::now();
// rlwe_by_rgsw(
// trivial_rlwe_test_poly,
// pbs_key.rgsw_ct_lwe_si(*s_index),
// scratch_matrix,
// rlwe_rgsw_decomposer,
// ntt_op,
// mod_op,
// );
let ct = pbs_key.rgsw_ct_lwe_si(*s_index);
rlwe_by_rgsw_shoup(
trivial_rlwe_test_poly,
ct.as_ref(),
&ct.shoup_repr(),
ct.shoup_repr(),
scratch_matrix,
rlwe_rgsw_decomposer,
ntt_op,
@@ -275,9 +272,11 @@ fn blind_rotation<
let (auto_map_index, auto_map_sign) = parameters.rlwe_auto_map(v);
// let now = std::time::Instant::now();
galois_auto(
let auto_key = pbs_key.galois_key_for_auto(v);
galois_auto_shoup(
trivial_rlwe_test_poly,
pbs_key.galois_key_for_auto(v),
auto_key.as_ref(),
auto_key.shoup_repr(),
scratch_matrix,
&auto_map_index,
&auto_map_sign,
@@ -293,37 +292,46 @@ fn blind_rotation<
}
// -(g^0)
gk_to_si[q_by_4].iter().for_each(|s_index| {
rlwe_by_rgsw(
{
gk_to_si[q_by_4].iter().for_each(|s_index| {
let ct = pbs_key.rgsw_ct_lwe_si(*s_index);
rlwe_by_rgsw_shoup(
trivial_rlwe_test_poly,
ct.as_ref(),
ct.shoup_repr(),
scratch_matrix,
rlwe_rgsw_decomposer,
ntt_op,
mod_op,
);
});
let (auto_map_index, auto_map_sign) = parameters.rlwe_auto_map(0);
let auto_key = pbs_key.galois_key_for_auto(0);
galois_auto_shoup(
trivial_rlwe_test_poly,
pbs_key.rgsw_ct_lwe_si(*s_index),
auto_key.as_ref(),
auto_key.shoup_repr(),
scratch_matrix,
rlwe_rgsw_decomposer,
ntt_op,
&auto_map_index,
&auto_map_sign,
mod_op,
ntt_op,
auto_decomposer,
);
});
let (auto_map_index, auto_map_sign) = parameters.rlwe_auto_map(0);
galois_auto(
trivial_rlwe_test_poly,
pbs_key.galois_key_for_auto(0),
scratch_matrix,
&auto_map_index,
&auto_map_sign,
mod_op,
ntt_op,
auto_decomposer,
);
count += 1;
count += 1;
}
// +(g^k)
let mut v = 0;
for i in (1..q_by_4).rev() {
let s_indices = &gk_to_si[i];
s_indices.iter().for_each(|s_index| {
rlwe_by_rgsw(
let ct = pbs_key.rgsw_ct_lwe_si(*s_index);
rlwe_by_rgsw_shoup(
trivial_rlwe_test_poly,
pbs_key.rgsw_ct_lwe_si(*s_index),
ct.as_ref(),
ct.shoup_repr(),
scratch_matrix,
rlwe_rgsw_decomposer,
ntt_op,
@@ -334,9 +342,11 @@ fn blind_rotation<
if gk_to_si[i - 1].len() != 0 || v == w || i == 1 {
let (auto_map_index, auto_map_sign) = parameters.rlwe_auto_map(v);
galois_auto(
let auto_key = pbs_key.galois_key_for_auto(v);
galois_auto_shoup(
trivial_rlwe_test_poly,
pbs_key.galois_key_for_auto(v),
auto_key.as_ref(),
auto_key.shoup_repr(),
scratch_matrix,
&auto_map_index,
&auto_map_sign,
@@ -351,9 +361,11 @@ fn blind_rotation<
// +(g^0)
gk_to_si[0].iter().for_each(|s_index| {
rlwe_by_rgsw(
let ct = pbs_key.rgsw_ct_lwe_si(*s_index);
rlwe_by_rgsw_shoup(
trivial_rlwe_test_poly,
pbs_key.rgsw_ct_lwe_si(*s_index),
ct.as_ref(),
ct.shoup_repr(),
scratch_matrix,
rlwe_rgsw_decomposer,
ntt_op,

View File

@@ -185,7 +185,7 @@ pub(crate) fn galois_auto_shoup<
MT: Matrix + IsTrivial + MatrixMut,
Mmut: MatrixMut<MatElement = MT::MatElement>,
ModOp: ArithmeticOps<Element = MT::MatElement>
+ VectorOps<Element = MT::MatElement>
// + VectorOps<Element = MT::MatElement>
+ ShoupMatrixFMA<Mmut::R>,
NttOp: Ntt<Element = MT::MatElement>,
D: Decomposer<Element = MT::MatElement>,
@@ -422,7 +422,7 @@ pub(crate) fn rlwe_by_rgsw_shoup<
Mmut: MatrixMut,
MT: Matrix<MatElement = Mmut::MatElement> + MatrixMut<MatElement = Mmut::MatElement> + IsTrivial,
D: RlweDecomposer<Element = Mmut::MatElement>,
ModOp: VectorOps<Element = Mmut::MatElement> + ShoupMatrixFMA<Mmut::R>,
ModOp: ShoupMatrixFMA<Mmut::R>,
NttOp: Ntt<Element = Mmut::MatElement>,
>(
rlwe_in: &mut MT,

View File

@@ -97,12 +97,18 @@ mod frontend {
eight_bit_mul,
};
use crate::{
bool::{evaluator::BoolEvaluator, keys::ServerKeyEvaluationDomain},
bool::{
evaluator::{self, BoolEvaluator, BooleanGates},
keys::{ServerKeyEvaluationDomain, ShoupServerKeyEvaluationDomain},
},
utils::{Global, WithLocal},
};
use super::FheUint8;
type ShortIntBoolEvaluator<M, Ntt, RlweModOp, LweModOp> =
BoolEvaluator<M, Ntt, RlweModOp, LweModOp>;
mod arithetic {
use crate::bool::{evaluator::BooleanGates, FheBool};
@@ -111,8 +117,8 @@ mod frontend {
impl AddAssign<&FheUint8> for FheUint8 {
fn add_assign(&mut self, rhs: &FheUint8) {
BoolEvaluator::with_local_mut_mut(&mut |e| {
let key = ServerKeyEvaluationDomain::global();
ShortIntBoolEvaluator::with_local_mut_mut(&mut |e| {
let key = <ShortIntBoolEvaluator<_, _, _, _> as BooleanGates>::Key::global();
arbitrary_bit_adder(e, self.data_mut(), rhs.data(), false, key);
});
}