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