tfhe: ciphertext-plaintext multiplication

This commit is contained in:
2025-07-22 15:42:24 +00:00
parent f0745da902
commit 4790fdbb3b
5 changed files with 88 additions and 15 deletions

View File

@@ -167,15 +167,6 @@ fn naive_poly_mul<const N: usize>(poly1: &Tn<N>, poly2: &Tn<N>) -> Tn<N> {
// apply mod (X^N + 1))
modulus_u128::<N>(&mut result);
// sanity check: check that there are no coeffs > i64_max
assert_eq!(
result,
Tn::<N>(array::from_fn(|i| T64(result[i] as u64)))
.coeffs()
.iter()
.map(|c| c.0 as u128)
.collect::<Vec<_>>()
);
Tn(array::from_fn(|i| T64(result[i] as u64)))
}
fn modulus_u128<const N: usize>(p: &mut Vec<u128>) {

View File

@@ -44,7 +44,7 @@ impl Add<T64> for T64 {
}
impl AddAssign for T64 {
fn add_assign(&mut self, rhs: Self) {
self.0 += rhs.0;
self.0 = self.0.wrapping_add(rhs.0)
}
}
@@ -57,7 +57,7 @@ impl Sub<T64> for T64 {
}
impl SubAssign for T64 {
fn sub_assign(&mut self, rhs: Self) {
self.0 -= rhs.0;
self.0 = self.0.wrapping_sub(rhs.0)
}
}
@@ -87,7 +87,7 @@ impl Mul<T64> for T64 {
}
impl MulAssign for T64 {
fn mul_assign(&mut self, rhs: Self) {
self.0 *= rhs.0;
self.0 = self.0.wrapping_mul(rhs.0)
}
}
@@ -96,14 +96,14 @@ impl Mul<u64> for T64 {
type Output = Self;
fn mul(self, s: u64) -> Self {
Self(self.0 * s)
Self(self.0.wrapping_mul(s))
}
}
impl Mul<&u64> for &T64 {
type Output = T64;
fn mul(self, s: &u64) -> Self::Output {
T64(self.0 * s)
T64(self.0.wrapping_mul(*s))
}
}

View File

@@ -55,7 +55,7 @@ impl<R: Ring, const K: usize> Sub<TR<R, K>> for TR<R, K> {
}
}
/// for (TR,TR), the Mul operation is defined as:
/// for (TR,TR), the Mul operation is defined as the dot product:
/// for A, B \in R^k, result = Σ A_i * B_i \in R
impl<R: Ring, const K: usize> Mul<TR<R, K>> for TR<R, K> {
type Output = R;