1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// SPDX-License-Identifier: AGPL-3.0-or-later
use crate::device::compute_pipeline::ComputeDispatch;
use crate::error::{BarracudaError, Result};
use crate::tensor::Tensor;
use bytemuck::{Pod, Zeroable};
/// Point-wise multiplication of two polynomials in NTT domain
///
/// Given two polynomials A and B in NTT domain (already transformed),
/// computes C = A ⊙ B (element-wise product).
///
/// This is the core operation in fast polynomial multiplication:
/// `poly_mul(a`, b) = `INTT(pointwise_mul(NTT(a)`, NTT(b)))
///
/// Complexity: O(N) - much faster than O(N²) convolution!
///
/// # Example
/// ```ignore
/// use barracuda::ops::{FheNtt, FhePointwiseMul, FheIntt};
///
/// // Fast polynomial multiplication
/// let ntt_a = FheNtt::new(poly_a, degree, modulus, root)?;
/// let ntt_b = FheNtt::new(poly_b, degree, modulus, root)?;
///
/// let a_ntt = ntt_a.execute().await?;
/// let b_ntt = ntt_b.execute().await?;
///
/// let pointwise_mul = FhePointwiseMul::new(a_ntt, b_ntt, degree, modulus)?;
/// let c_ntt = pointwise_mul.execute().await?;
///
/// let intt = FheIntt::new(c_ntt, degree, modulus, inv_root)?;
/// let c = intt.execute().await?; // c = a * b (polynomial multiplication!)
/// ```
pub struct FhePointwiseMul {
input_a: Tensor,
input_b: Tensor,
degree: u32,
modulus: u64,
barrett_mu: u64,
}
/// Parameters passed to GPU shader
#[repr(C)]
#[derive(Copy, Clone, Pod, Zeroable)]
struct PointwiseMulParams {
degree: u32,
modulus_low: u32,
modulus_high: u32,
barrett_mu_low: u32,
barrett_mu_high: u32,
_padding: [u32; 3], // Align to 16 bytes
}
impl FhePointwiseMul {
/// Create a new point-wise multiplication operation
/// # Arguments
/// * `input_a` - First polynomial in NTT domain (N × 2 u32 values)
/// * `input_b` - Second polynomial in NTT domain (N × 2 u32 values)
/// * `degree` - Polynomial degree (N), must be power of 2
/// * `modulus` - FHE modulus q (64-bit prime)
/// # Errors
/// Returns [`Err`] if polynomial lengths do not match degree, tensors are on
/// different devices, or modulus is zero.
pub fn new(input_a: Tensor, input_b: Tensor, degree: u32, modulus: u64) -> Result<Self> {
// Validate inputs
if !degree.is_power_of_two() {
return Err(BarracudaError::Device(format!(
"Degree must be power of 2, got {degree}"
)));
}
if !(4..=65_536).contains(°ree) {
return Err(BarracudaError::Device(format!(
"Degree must be in range [4, 65_536], got {degree}"
)));
}
let expected_len = (degree * 2) as usize; // 2 u32 per coefficient
if input_a.len() != expected_len {
return Err(BarracudaError::Device(format!(
"Input A has wrong length: expected {}, got {}",
expected_len,
input_a.len()
)));
}
if input_b.len() != expected_len {
return Err(BarracudaError::Device(format!(
"Input B has wrong length: expected {}, got {}",
expected_len,
input_b.len()
)));
}
// Ensure both tensors are on same device
if !std::ptr::eq(input_a.device().as_ref(), input_b.device().as_ref()) {
return Err(BarracudaError::Device(
"input_a and input_b must be on the same device".to_string(),
));
}
// Compute Barrett reduction constant: μ = ⌊2^128 / q⌋
// For 64-bit approximation: μ ≈ u64::MAX / q
let barrett_mu = u64::MAX.checked_div(modulus).unwrap_or(0);
Ok(Self {
input_a,
input_b,
degree,
modulus,
barrett_mu,
})
}
/// Execute point-wise multiplication on GPU
/// Returns: C = A ⊙ B (element-wise product in NTT domain)
/// # Errors
/// Returns [`Err`] if buffer allocation, GPU dispatch, or buffer
/// readback fails (e.g. device lost or out of memory).
pub fn execute(&self) -> Result<Tensor> {
let device = self.input_a.device();
// Create output buffer
let result_buffer = device.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Point-wise Multiply Output"),
size: (self.degree as u64 * 2 * std::mem::size_of::<u32>() as u64),
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
mapped_at_creation: false,
});
let params = PointwiseMulParams {
degree: self.degree,
modulus_low: (self.modulus & 0xFFFF_FFFF) as u32,
modulus_high: (self.modulus >> 32) as u32,
barrett_mu_low: (self.barrett_mu & 0xFFFF_FFFF) as u32,
barrett_mu_high: (self.barrett_mu >> 32) as u32,
_padding: [0; 3],
};
let params_buffer = device.create_uniform_buffer("FHE Pointwise Mul Params", ¶ms);
ComputeDispatch::new(device.as_ref(), "FHE Pointwise Mul")
.shader(include_str!("fhe_pointwise_mul.wgsl"), "main")
.storage_read(0, self.input_a.buffer())
.storage_read(1, self.input_b.buffer())
.storage_rw(2, &result_buffer)
.uniform(3, ¶ms_buffer)
.dispatch_1d(self.degree)
.submit()?;
// Return tensor (data stays on GPU)
Ok(Tensor::from_buffer(
result_buffer,
vec![self.degree as usize * 2],
device.clone(),
))
}
}
// Tests disabled - requires integration testing framework
// Will be tested via fhe_fast_poly_mul integration tests