p3_mds/karatsuba_convolution.rs
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
//! Calculate the convolution of two vectors using a Karatsuba-style
//! decomposition and the CRT.
//!
//! This is not a new idea, but we did have the pleasure of
//! reinventing it independently. Some references:
//! - https://cr.yp.to/lineartime/multapps-20080515.pdf
//! - https://2π.com/23/convolution/
//!
//! Given a vector v \in F^N, let v(x) \in F[X] denote the polynomial
//! v_0 + v_1 x + ... + v_{N - 1} x^{N - 1}. Then w is equal to the
//! convolution v * u if and only if w(x) = v(x)u(x) mod x^N - 1.
//! Additionally, define the negacyclic convolution by w(x) = v(x)u(x)
//! mod x^N + 1. Using the Chinese remainder theorem we can compute
//! w(x) as
//! w(x) = 1/2 (w_0(x) + w_1(x)) + x^{N/2}/2 (w_0(x) - w_1(x))
//! where
//! w_0 = v(x)u(x) mod x^{N/2} - 1
//! w_1 = v(x)u(x) mod x^{N/2} + 1
//!
//! To compute w_0 and w_1 we first compute
//! v_0(x) = v(x) mod x^{N/2} - 1
//! v_1(x) = v(x) mod x^{N/2} + 1
//! u_0(x) = u(x) mod x^{N/2} - 1
//! u_1(x) = u(x) mod x^{N/2} + 1
//!
//! Now w_0 is the convolution of v_0 and u_0 which we can compute
//! recursively. For w_1 we compute the negacyclic convolution
//! v_1(x)u_1(x) mod x^{N/2} + 1 using Karatsuba.
//!
//! There are 2 possible approaches to applying Karatsuba which mirror
//! the DIT vs DIF approaches to FFT's, the left/right decomposition
//! or the even/odd decomposition. The latter seems to have fewer
//! operations and so it is the one implemented below, though it does
//! require a bit more data manipulation. It works as follows:
//!
//! Define the even v_e and odd v_o parts so that v(x) = (v_e(x^2) + x v_o(x^2)).
//! Then v(x)u(x)
//! = (v_e(x^2)u_e(x^2) + x^2 v_o(x^2)u_o(x^2))
//! + x ((v_e(x^2) + v_o(x^2))(u_e(x^2) + u_o(x^2))
//! - (v_e(x^2)u_e(x^2) + v_o(x^2)u_o(x^2)))
//! This reduces the problem to 3 negacyclic convolutions of size N/2 which
//! can be computed recursively.
//!
//! Of course, for small sizes we just explicitly write out the O(n^2)
//! approach.
use core::ops::{Add, AddAssign, Neg, ShrAssign, Sub, SubAssign};
/// This trait collects the operations needed by `Convolve` below.
///
/// TODO: Think of a better name for this.
pub trait RngElt:
Add<Output = Self>
+ AddAssign
+ Copy
+ Default
+ Neg<Output = Self>
+ ShrAssign<u32>
+ Sub<Output = Self>
+ SubAssign
{
}
impl RngElt for i64 {}
impl RngElt for i128 {}
/// Template function to perform convolution of vectors.
///
/// Roughly speaking, for a convolution of size `N`, it should be
/// possible to add `N` elements of type `T` without overflowing, and
/// similarly for `U`. Then multiplication via `Self::mul` should
/// produce an element of type `V` which will not overflow after about
/// `N` additions (this is an over-estimate).
///
/// For example usage, see `{mersenne-31,baby-bear,goldilocks}/src/mds.rs`.
///
/// NB: In practice, one of the parameters to the convolution will be
/// constant (the MDS matrix). After inspecting Godbolt output, it
/// seems that the compiler does indeed generate single constants as
/// inputs to the multiplication, rather than doing all that
/// arithmetic on the constant values every time. Note however that,
/// for MDS matrices with large entries (N >= 24), these compile-time
/// generated constants will be about N times bigger than they need to
/// be in principle, which could be a potential avenue for some minor
/// improvements.
///
/// NB: If primitive multiplications are still the bottleneck, a
/// further possibility would be to find an MDS matrix some of whose
/// entries are powers of 2. Then the multiplication can be replaced
/// with a shift, which on most architectures has better throughput
/// and latency, and is issued on different ports (1*p06) to
/// multiplication (1*p1).
pub trait Convolve<F, T: RngElt, U: RngElt, V: RngElt> {
/// Given an input element, retrieve the corresponding internal
/// element that will be used in calculations.
fn read(input: F) -> T;
/// Given input vectors `lhs` and `rhs`, calculate their dot
/// product. The result can be reduced with respect to the modulus
/// (of `F`), but it must have the same lower 10 bits as the dot
/// product if all inputs are considered integers. See
/// `monty-31/src/mds.rs::barrett_red_monty31()` for an example
/// of how this can be implemented in practice.
fn parity_dot<const N: usize>(lhs: [T; N], rhs: [U; N]) -> V;
/// Convert an internal element of type `V` back into an external
/// element.
fn reduce(z: V) -> F;
/// Convolve `lhs` and `rhs`.
///
/// The parameter `conv` should be the function in this trait that
/// corresponds to length `N`.
#[inline(always)]
fn apply<const N: usize, C: Fn([T; N], [U; N], &mut [V])>(
lhs: [F; N],
rhs: [U; N],
conv: C,
) -> [F; N] {
let lhs = lhs.map(Self::read);
let mut output = [V::default(); N];
conv(lhs, rhs, &mut output);
output.map(Self::reduce)
}
#[inline(always)]
fn conv3(lhs: [T; 3], rhs: [U; 3], output: &mut [V]) {
output[0] = Self::parity_dot(lhs, [rhs[0], rhs[2], rhs[1]]);
output[1] = Self::parity_dot(lhs, [rhs[1], rhs[0], rhs[2]]);
output[2] = Self::parity_dot(lhs, [rhs[2], rhs[1], rhs[0]]);
}
#[inline(always)]
fn negacyclic_conv3(lhs: [T; 3], rhs: [U; 3], output: &mut [V]) {
output[0] = Self::parity_dot(lhs, [rhs[0], -rhs[2], -rhs[1]]);
output[1] = Self::parity_dot(lhs, [rhs[1], rhs[0], -rhs[2]]);
output[2] = Self::parity_dot(lhs, [rhs[2], rhs[1], rhs[0]]);
}
#[inline(always)]
fn conv4(lhs: [T; 4], rhs: [U; 4], output: &mut [V]) {
// NB: This is just explicitly implementing
// conv_n_recursive::<4, 2, _, _>(lhs, rhs, output, Self::conv2, Self::negacyclic_conv2)
let u_p = [lhs[0] + lhs[2], lhs[1] + lhs[3]];
let u_m = [lhs[0] - lhs[2], lhs[1] - lhs[3]];
let v_p = [rhs[0] + rhs[2], rhs[1] + rhs[3]];
let v_m = [rhs[0] - rhs[2], rhs[1] - rhs[3]];
output[0] = Self::parity_dot(u_m, [v_m[0], -v_m[1]]);
output[1] = Self::parity_dot(u_m, [v_m[1], v_m[0]]);
output[2] = Self::parity_dot(u_p, v_p);
output[3] = Self::parity_dot(u_p, [v_p[1], v_p[0]]);
output[0] += output[2];
output[1] += output[3];
output[0] >>= 1;
output[1] >>= 1;
output[2] -= output[0];
output[3] -= output[1];
}
#[inline(always)]
fn negacyclic_conv4(lhs: [T; 4], rhs: [U; 4], output: &mut [V]) {
output[0] = Self::parity_dot(lhs, [rhs[0], -rhs[3], -rhs[2], -rhs[1]]);
output[1] = Self::parity_dot(lhs, [rhs[1], rhs[0], -rhs[3], -rhs[2]]);
output[2] = Self::parity_dot(lhs, [rhs[2], rhs[1], rhs[0], -rhs[3]]);
output[3] = Self::parity_dot(lhs, [rhs[3], rhs[2], rhs[1], rhs[0]]);
}
#[inline(always)]
fn conv6(lhs: [T; 6], rhs: [U; 6], output: &mut [V]) {
conv_n_recursive::<6, 3, T, U, V, _, _>(
lhs,
rhs,
output,
Self::conv3,
Self::negacyclic_conv3,
)
}
#[inline(always)]
fn negacyclic_conv6(lhs: [T; 6], rhs: [U; 6], output: &mut [V]) {
negacyclic_conv_n_recursive::<6, 3, T, U, V, _>(lhs, rhs, output, Self::negacyclic_conv3)
}
#[inline(always)]
fn conv8(lhs: [T; 8], rhs: [U; 8], output: &mut [V]) {
conv_n_recursive::<8, 4, T, U, V, _, _>(
lhs,
rhs,
output,
Self::conv4,
Self::negacyclic_conv4,
)
}
#[inline(always)]
fn negacyclic_conv8(lhs: [T; 8], rhs: [U; 8], output: &mut [V]) {
negacyclic_conv_n_recursive::<8, 4, T, U, V, _>(lhs, rhs, output, Self::negacyclic_conv4)
}
#[inline(always)]
fn conv12(lhs: [T; 12], rhs: [U; 12], output: &mut [V]) {
conv_n_recursive::<12, 6, T, U, V, _, _>(
lhs,
rhs,
output,
Self::conv6,
Self::negacyclic_conv6,
)
}
#[inline(always)]
fn negacyclic_conv12(lhs: [T; 12], rhs: [U; 12], output: &mut [V]) {
negacyclic_conv_n_recursive::<12, 6, T, U, V, _>(lhs, rhs, output, Self::negacyclic_conv6)
}
#[inline(always)]
fn conv16(lhs: [T; 16], rhs: [U; 16], output: &mut [V]) {
conv_n_recursive::<16, 8, T, U, V, _, _>(
lhs,
rhs,
output,
Self::conv8,
Self::negacyclic_conv8,
)
}
#[inline(always)]
fn negacyclic_conv16(lhs: [T; 16], rhs: [U; 16], output: &mut [V]) {
negacyclic_conv_n_recursive::<16, 8, T, U, V, _>(lhs, rhs, output, Self::negacyclic_conv8)
}
#[inline(always)]
fn conv24(lhs: [T; 24], rhs: [U; 24], output: &mut [V]) {
conv_n_recursive::<24, 12, T, U, V, _, _>(
lhs,
rhs,
output,
Self::conv12,
Self::negacyclic_conv12,
)
}
#[inline(always)]
fn conv32(lhs: [T; 32], rhs: [U; 32], output: &mut [V]) {
conv_n_recursive::<32, 16, T, U, V, _, _>(
lhs,
rhs,
output,
Self::conv16,
Self::negacyclic_conv16,
)
}
#[inline(always)]
fn negacyclic_conv32(lhs: [T; 32], rhs: [U; 32], output: &mut [V]) {
negacyclic_conv_n_recursive::<32, 16, T, U, V, _>(lhs, rhs, output, Self::negacyclic_conv16)
}
#[inline(always)]
fn conv64(lhs: [T; 64], rhs: [U; 64], output: &mut [V]) {
conv_n_recursive::<64, 32, T, U, V, _, _>(
lhs,
rhs,
output,
Self::conv32,
Self::negacyclic_conv32,
)
}
}
/// Compute output(x) = lhs(x)rhs(x) mod x^N - 1.
/// Do this recursively using a convolution and negacyclic convolution of size HALF_N = N/2.
#[inline(always)]
fn conv_n_recursive<const N: usize, const HALF_N: usize, T, U, V, C, NC>(
lhs: [T; N],
rhs: [U; N],
output: &mut [V],
inner_conv: C,
inner_negacyclic_conv: NC,
) where
T: RngElt,
U: RngElt,
V: RngElt,
C: Fn([T; HALF_N], [U; HALF_N], &mut [V]),
NC: Fn([T; HALF_N], [U; HALF_N], &mut [V]),
{
debug_assert_eq!(2 * HALF_N, N);
// NB: The compiler is smart enough not to initialise these arrays.
let mut lhs_pos = [T::default(); HALF_N]; // lhs_pos = lhs(x) mod x^{N/2} - 1
let mut lhs_neg = [T::default(); HALF_N]; // lhs_neg = lhs(x) mod x^{N/2} + 1
let mut rhs_pos = [U::default(); HALF_N]; // rhs_pos = rhs(x) mod x^{N/2} - 1
let mut rhs_neg = [U::default(); HALF_N]; // rhs_neg = rhs(x) mod x^{N/2} + 1
for i in 0..HALF_N {
let s = lhs[i];
let t = lhs[i + HALF_N];
lhs_pos[i] = s + t;
lhs_neg[i] = s - t;
let s = rhs[i];
let t = rhs[i + HALF_N];
rhs_pos[i] = s + t;
rhs_neg[i] = s - t;
}
let (left, right) = output.split_at_mut(HALF_N);
// left = w1 = lhs(x)rhs(x) mod x^{N/2} + 1
inner_negacyclic_conv(lhs_neg, rhs_neg, left);
// right = w0 = lhs(x)rhs(x) mod x^{N/2} - 1
inner_conv(lhs_pos, rhs_pos, right);
for i in 0..HALF_N {
left[i] += right[i]; // w_0 + w_1
left[i] >>= 1; // (w_0 + w_1)/2
right[i] -= left[i]; // (w_0 - w_1)/2
}
}
/// Compute output(x) = lhs(x)rhs(x) mod x^N + 1.
/// Do this recursively using three negacyclic convolutions of size HALF_N = N/2.
#[inline(always)]
fn negacyclic_conv_n_recursive<const N: usize, const HALF_N: usize, T, U, V, NC>(
lhs: [T; N],
rhs: [U; N],
output: &mut [V],
inner_negacyclic_conv: NC,
) where
T: RngElt,
U: RngElt,
V: RngElt,
NC: Fn([T; HALF_N], [U; HALF_N], &mut [V]),
{
debug_assert_eq!(2 * HALF_N, N);
// NB: The compiler is smart enough not to initialise these arrays.
let mut lhs_even = [T::default(); HALF_N];
let mut lhs_odd = [T::default(); HALF_N];
let mut lhs_sum = [T::default(); HALF_N];
let mut rhs_even = [U::default(); HALF_N];
let mut rhs_odd = [U::default(); HALF_N];
let mut rhs_sum = [U::default(); HALF_N];
for i in 0..HALF_N {
let s = lhs[2 * i];
let t = lhs[2 * i + 1];
lhs_even[i] = s;
lhs_odd[i] = t;
lhs_sum[i] = s + t;
let s = rhs[2 * i];
let t = rhs[2 * i + 1];
rhs_even[i] = s;
rhs_odd[i] = t;
rhs_sum[i] = s + t;
}
let mut even_s_conv = [V::default(); HALF_N];
let (left, right) = output.split_at_mut(HALF_N);
// Recursively compute the size N/2 negacyclic convolutions of
// the even parts, odd parts, and sums.
inner_negacyclic_conv(lhs_even, rhs_even, &mut even_s_conv);
inner_negacyclic_conv(lhs_odd, rhs_odd, left);
inner_negacyclic_conv(lhs_sum, rhs_sum, right);
// Adjust so that the correct values are in right and
// even_s_conv respectively:
right[0] -= even_s_conv[0] + left[0];
even_s_conv[0] -= left[HALF_N - 1];
for i in 1..HALF_N {
right[i] -= even_s_conv[i] + left[i];
even_s_conv[i] += left[i - 1];
}
// Interleave even_s_conv and right in the output:
for i in 0..HALF_N {
output[2 * i] = even_s_conv[i];
output[2 * i + 1] = output[i + HALF_N];
}
}