mirror of
https://github.com/arnaucube/phantom-zone.git
synced 2026-01-08 23:21:29 +01:00
remove is_trivial from shoup ops
This commit is contained in:
31
src/pbs.rs
31
src/pbs.rs
@@ -155,11 +155,7 @@ pub(crate) fn pbs<
|
||||
gb_monomial_sign = false
|
||||
}
|
||||
// monomial mul
|
||||
let mut trivial_rlwe_test_poly = RlweCiphertext::<_, DefaultSecureRng> {
|
||||
data: M::zeros(2, rlwe_n),
|
||||
is_trivial: true,
|
||||
_phatom: PhantomData,
|
||||
};
|
||||
let mut trivial_rlwe_test_poly = M::zeros(2, rlwe_n);
|
||||
if pbs_info.embedding_factor() == 1 {
|
||||
monomial_mul(
|
||||
test_vec.as_ref(),
|
||||
@@ -218,16 +214,15 @@ pub(crate) fn pbs<
|
||||
///
|
||||
/// gk_to_si: [g^0, ..., g^{q/2-1}, -g^0, -g^1, .., -g^{q/2-1}]
|
||||
fn blind_rotation<
|
||||
MT: IsTrivial + MatrixMut,
|
||||
Mmut: MatrixMut<MatElement = MT::MatElement>,
|
||||
D: Decomposer<Element = MT::MatElement>,
|
||||
NttOp: Ntt<Element = MT::MatElement>,
|
||||
ModOp: ArithmeticOps<Element = MT::MatElement> + ShoupMatrixFMA<Mmut::R>,
|
||||
Mmut: MatrixMut,
|
||||
D: Decomposer<Element = Mmut::MatElement>,
|
||||
NttOp: Ntt<Element = Mmut::MatElement>,
|
||||
ModOp: ArithmeticOps<Element = Mmut::MatElement> + ShoupMatrixFMA<Mmut::R>,
|
||||
MShoup: WithShoupRepr<M = Mmut>,
|
||||
K: PbsKey<RgswCt = MShoup, AutoKey = MShoup>,
|
||||
P: PbsInfo<M = Mmut>,
|
||||
>(
|
||||
trivial_rlwe_test_poly: &mut MT,
|
||||
trivial_rlwe_test_poly: &mut Mmut,
|
||||
scratch_matrix: &mut Mmut,
|
||||
_g: isize,
|
||||
w: usize,
|
||||
@@ -242,8 +237,9 @@ fn blind_rotation<
|
||||
) where
|
||||
<Mmut as Matrix>::R: RowMut,
|
||||
Mmut::MatElement: Copy + Zero,
|
||||
<MT as Matrix>::R: RowMut,
|
||||
{
|
||||
let mut is_trivial = true;
|
||||
|
||||
let q_by_4 = q >> 2;
|
||||
let mut count = 0;
|
||||
// -(g^k)
|
||||
@@ -263,7 +259,9 @@ fn blind_rotation<
|
||||
rlwe_rgsw_decomposer,
|
||||
ntt_op,
|
||||
mod_op,
|
||||
is_trivial,
|
||||
);
|
||||
is_trivial = false;
|
||||
// println!("Rlwe x Rgsw time: {:?}", new.elapsed());
|
||||
});
|
||||
v += 1;
|
||||
@@ -283,6 +281,7 @@ fn blind_rotation<
|
||||
mod_op,
|
||||
ntt_op,
|
||||
auto_decomposer,
|
||||
is_trivial,
|
||||
);
|
||||
// println!("Auto time: {:?}", now.elapsed());
|
||||
|
||||
@@ -303,7 +302,9 @@ fn blind_rotation<
|
||||
rlwe_rgsw_decomposer,
|
||||
ntt_op,
|
||||
mod_op,
|
||||
is_trivial,
|
||||
);
|
||||
is_trivial = false;
|
||||
});
|
||||
|
||||
let (auto_map_index, auto_map_sign) = parameters.rlwe_auto_map(0);
|
||||
@@ -318,6 +319,7 @@ fn blind_rotation<
|
||||
mod_op,
|
||||
ntt_op,
|
||||
auto_decomposer,
|
||||
is_trivial,
|
||||
);
|
||||
count += 1;
|
||||
}
|
||||
@@ -336,7 +338,9 @@ fn blind_rotation<
|
||||
rlwe_rgsw_decomposer,
|
||||
ntt_op,
|
||||
mod_op,
|
||||
is_trivial,
|
||||
);
|
||||
is_trivial = false;
|
||||
});
|
||||
v += 1;
|
||||
|
||||
@@ -353,6 +357,7 @@ fn blind_rotation<
|
||||
mod_op,
|
||||
ntt_op,
|
||||
auto_decomposer,
|
||||
is_trivial,
|
||||
);
|
||||
v = 0;
|
||||
count += 1;
|
||||
@@ -370,7 +375,9 @@ fn blind_rotation<
|
||||
rlwe_rgsw_decomposer,
|
||||
ntt_op,
|
||||
mod_op,
|
||||
is_trivial,
|
||||
);
|
||||
is_trivial = false;
|
||||
});
|
||||
// println!("Auto count: {count}");
|
||||
}
|
||||
|
||||
@@ -762,11 +762,7 @@ pub(crate) mod tests {
|
||||
|
||||
// rlwe x rgsw with additional RGSW ciphertexts in shoup repr
|
||||
let rlwe_in_ct_shoup = {
|
||||
let mut rlwe_in_ct_shoup = RlweCiphertext::<_, DefaultSecureRng> {
|
||||
data: rlwe_in_ct.data.clone(),
|
||||
is_trivial: rlwe_in_ct.is_trivial,
|
||||
_phatom: PhantomData::default(),
|
||||
};
|
||||
let mut rlwe_in_ct_shoup = rlwe_in_ct.data.clone();
|
||||
|
||||
let rgsw_ct_shoup = ShoupRgswCiphertextEvaluationDomain::from(&rgsw_ct);
|
||||
|
||||
@@ -778,6 +774,7 @@ pub(crate) mod tests {
|
||||
&decomposer,
|
||||
&ntt_op,
|
||||
&mod_op,
|
||||
false,
|
||||
);
|
||||
|
||||
rlwe_in_ct_shoup
|
||||
@@ -797,7 +794,7 @@ pub(crate) mod tests {
|
||||
|
||||
// output from both functions must be equal
|
||||
{
|
||||
assert_eq!(rlwe_in_ct.data, rlwe_in_ct_shoup.data);
|
||||
assert_eq!(rlwe_in_ct.data, rlwe_in_ct_shoup);
|
||||
}
|
||||
|
||||
// Decrypt RLWE(m0m1)
|
||||
@@ -907,11 +904,7 @@ pub(crate) mod tests {
|
||||
// galois auto with additional auto key in shoup repr
|
||||
let rlwe_m_shoup = {
|
||||
let auto_key_shoup = ShoupAutoKeyEvaluationDomain::from(&auto_key);
|
||||
let mut rlwe_m_shoup = RlweCiphertext::<_, DefaultSecureRng> {
|
||||
data: rlwe_m.data.clone(),
|
||||
is_trivial: rlwe_m.is_trivial,
|
||||
_phatom: PhantomData::default(),
|
||||
};
|
||||
let mut rlwe_m_shoup = rlwe_m.data.clone();
|
||||
galois_auto_shoup(
|
||||
&mut rlwe_m_shoup,
|
||||
&auto_key.data,
|
||||
@@ -922,6 +915,7 @@ pub(crate) mod tests {
|
||||
&mod_op,
|
||||
&ntt_op,
|
||||
&decomposer,
|
||||
false,
|
||||
);
|
||||
rlwe_m_shoup
|
||||
};
|
||||
@@ -941,7 +935,7 @@ pub(crate) mod tests {
|
||||
}
|
||||
|
||||
// rlwe out from both functions must be same
|
||||
assert_eq!(rlwe_m.data, rlwe_m_shoup.data);
|
||||
assert_eq!(rlwe_m.data, rlwe_m_shoup);
|
||||
|
||||
let rlwe_m_k = rlwe_m;
|
||||
|
||||
|
||||
@@ -195,15 +195,14 @@ pub(crate) fn rlwe_auto<
|
||||
/// key switching polynomials in evaluation domain, shoup representation,
|
||||
/// `ksk_shoup`, of the polynomials in evaluation domain is also supplied.
|
||||
pub(crate) fn galois_auto_shoup<
|
||||
MT: Matrix + IsTrivial + MatrixMut,
|
||||
Mmut: MatrixMut<MatElement = MT::MatElement>,
|
||||
ModOp: ArithmeticOps<Element = MT::MatElement>
|
||||
Mmut: MatrixMut,
|
||||
ModOp: ArithmeticOps<Element = Mmut::MatElement>
|
||||
// + VectorOps<Element = MT::MatElement>
|
||||
+ ShoupMatrixFMA<Mmut::R>,
|
||||
NttOp: Ntt<Element = MT::MatElement>,
|
||||
D: Decomposer<Element = MT::MatElement>,
|
||||
NttOp: Ntt<Element = Mmut::MatElement>,
|
||||
D: Decomposer<Element = Mmut::MatElement>,
|
||||
>(
|
||||
rlwe_in: &mut MT,
|
||||
rlwe_in: &mut Mmut,
|
||||
ksk: &Mmut,
|
||||
ksk_shoup: &Mmut,
|
||||
scratch_matrix: &mut Mmut,
|
||||
@@ -212,10 +211,10 @@ pub(crate) fn galois_auto_shoup<
|
||||
mod_op: &ModOp,
|
||||
ntt_op: &NttOp,
|
||||
decomposer: &D,
|
||||
is_trivial: bool,
|
||||
) where
|
||||
<Mmut as Matrix>::R: RowMut,
|
||||
<MT as Matrix>::R: RowMut,
|
||||
MT::MatElement: Copy + Zero,
|
||||
Mmut::MatElement: Copy + Zero,
|
||||
{
|
||||
let d = decomposer.decomposition_count();
|
||||
let ring_size = rlwe_in.dimension().1;
|
||||
@@ -228,7 +227,7 @@ pub(crate) fn galois_auto_shoup<
|
||||
debug_assert!(tmp_rlwe_out.len() == 2);
|
||||
debug_assert!(scratch_matrix_d_ring.len() == d);
|
||||
|
||||
if !rlwe_in.is_trivial() {
|
||||
if !is_trivial {
|
||||
tmp_rlwe_out.iter_mut().for_each(|r| {
|
||||
r.as_mut().fill(Mmut::MatElement::zero());
|
||||
});
|
||||
@@ -436,22 +435,21 @@ pub(crate) fn rlwe_by_rgsw<
|
||||
/// evaluation domain, `rgsw_in_shoup`, is also supplied.
|
||||
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: ShoupMatrixFMA<Mmut::R>,
|
||||
NttOp: Ntt<Element = Mmut::MatElement>,
|
||||
>(
|
||||
rlwe_in: &mut MT,
|
||||
rlwe_in: &mut Mmut,
|
||||
rgsw_in: &Mmut,
|
||||
rgsw_in_shoup: &Mmut,
|
||||
scratch_matrix: &mut Mmut,
|
||||
decomposer: &D,
|
||||
ntt_op: &NttOp,
|
||||
mod_op: &ModOp,
|
||||
is_trivial: bool,
|
||||
) where
|
||||
Mmut::MatElement: Copy + Zero,
|
||||
<Mmut as Matrix>::R: RowMut,
|
||||
<MT as Matrix>::R: RowMut,
|
||||
{
|
||||
let decomposer_a = decomposer.a();
|
||||
let decomposer_b = decomposer.b();
|
||||
@@ -472,7 +470,7 @@ pub(crate) fn rlwe_by_rgsw_shoup<
|
||||
scratch_rlwe_out[0].as_mut().fill(Mmut::MatElement::zero());
|
||||
|
||||
// RLWE_in = a_in, b_in; RLWE_out = a_out, b_out
|
||||
if !rlwe_in.is_trivial() {
|
||||
if !is_trivial {
|
||||
// a_in = 0 when RLWE_in is trivial RLWE ciphertext
|
||||
// decomp<a_in>
|
||||
decompose_r(
|
||||
@@ -541,7 +539,6 @@ pub(crate) fn rlwe_by_rgsw_shoup<
|
||||
rlwe_in
|
||||
.get_row_mut(1)
|
||||
.copy_from_slice(scratch_rlwe_out[1].as_mut());
|
||||
rlwe_in.set_not_trivial();
|
||||
}
|
||||
|
||||
/// Inplace mutates RGSW(m0) to equal RGSW(m0m1) = RGSW(m0)xRGSW(m1)
|
||||
|
||||
Reference in New Issue
Block a user