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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use super::*;

use std::mem::MaybeUninit;

#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub struct GF2P64 {}

impl GF2P64 {
    // x86 / x86_64 AVX2 impl.
    #[target_feature(enable = "avx2")]
    #[cfg(target_feature = "avx2")]
    unsafe fn convert_avx2(dst: &mut [<Self as Domain>::Sharing], src: &[<Self as Domain>::Batch]) {
        // do a single range-check up front
        assert!(dst.len() >= <Self as Domain>::Batch::DIMENSION);

        macro_rules! pack {
            ($x:expr, $y:expr) => {
                src.get_unchecked($x).0[$y] as i8
            };
        }

        macro_rules! pack8x4 {
            ($x:expr, $y:expr) => {
                _mm256_set_epi8(
                    pack!($x, $y),
                    pack!($x + 1, $y),
                    pack!($x + 2, $y),
                    pack!($x + 3, $y),
                    pack!($x + 4, $y),
                    pack!($x + 5, $y),
                    pack!($x + 6, $y),
                    pack!($x + 7, $y),
                    pack!($x, $y + 1),
                    pack!($x + 1, $y + 1),
                    pack!($x + 2, $y + 1),
                    pack!($x + 3, $y + 1),
                    pack!($x + 4, $y + 1),
                    pack!($x + 5, $y + 1),
                    pack!($x + 6, $y + 1),
                    pack!($x + 7, $y + 1),
                    pack!($x, $y + 2),
                    pack!($x + 1, $y + 2),
                    pack!($x + 2, $y + 2),
                    pack!($x + 3, $y + 2),
                    pack!($x + 4, $y + 2),
                    pack!($x + 5, $y + 2),
                    pack!($x + 6, $y + 2),
                    pack!($x + 7, $y + 2),
                    pack!($x, $y + 3),
                    pack!($x + 1, $y + 3),
                    pack!($x + 2, $y + 3),
                    pack!($x + 3, $y + 3),
                    pack!($x + 4, $y + 3),
                    pack!($x + 5, $y + 3),
                    pack!($x + 6, $y + 3),
                    pack!($x + 7, $y + 3),
                )
            };
        }

        // transpose four batches at a time, byte-by-byte
        for i in (0..BATCH_SIZE_BYTES).step_by(4) {
            // pack 4 bytes from 64 different players
            let mut v: [__m256i; 8] = [
                pack8x4!(0x00, i),
                pack8x4!(0x08, i),
                pack8x4!(0x10, i),
                pack8x4!(0x18, i),
                pack8x4!(0x20, i),
                pack8x4!(0x28, i),
                pack8x4!(0x30, i),
                pack8x4!(0x38, i),
            ];

            // calculate the 8 sharings
            let mut idx = i * 8;
            for _ in 0..8 {
                let mut res_0: [u8; 8] = [0u8; 8];
                let mut res_1: [u8; 8] = [0u8; 8];
                let mut res_2: [u8; 8] = [0u8; 8];
                let mut res_3: [u8; 8] = [0u8; 8];

                for i in 0..8 {
                    let mask = _mm256_movemask_epi8(v[i]);
                    res_0[i] = (mask >> 24) as u8;
                    res_1[i] = (mask >> 16) as u8;
                    res_2[i] = (mask >> 8) as u8;
                    res_3[i] = mask as u8;
                    v[i] = _mm256_add_epi8(v[i], v[i]);
                }

                dst[idx] = BitSharing64(u64::from_le_bytes(res_0));
                dst[idx + 8] = BitSharing64(u64::from_le_bytes(res_1));
                dst[idx + 16] = BitSharing64(u64::from_le_bytes(res_2));
                dst[idx + 24] = BitSharing64(u64::from_le_bytes(res_3));
                idx += 1;
            }
        }
    }

    // x86 / x86_64 SSE2 impl.
    #[target_feature(enable = "sse2")]
    #[cfg(all(target_feature = "sse2", not(target_feature = "avx2")))]
    unsafe fn convert_sse2(dst: &mut [<Self as Domain>::Sharing], src: &[<Self as Domain>::Batch]) {
        // do a single range-check up front
        assert!(dst.len() >= <Self as Domain>::Batch::DIMENSION);

        macro_rules! pack {
            ($x:expr, $y:expr) => {
                src.get_unchecked($x).0[$y] as i8
            };
        }

        macro_rules! pack8x2 {
            ($x:expr, $y:expr) => {
                _mm_set_epi8(
                    pack!($x, $y),
                    pack!($x + 1, $y),
                    pack!($x + 2, $y),
                    pack!($x + 3, $y),
                    pack!($x + 4, $y),
                    pack!($x + 5, $y),
                    pack!($x + 6, $y),
                    pack!($x + 7, $y),
                    pack!($x, $y + 1),
                    pack!($x + 1, $y + 1),
                    pack!($x + 2, $y + 1),
                    pack!($x + 3, $y + 1),
                    pack!($x + 4, $y + 1),
                    pack!($x + 5, $y + 1),
                    pack!($x + 6, $y + 1),
                    pack!($x + 7, $y + 1),
                )
            };
        }

        // transpose two batches at a time, byte-by-byte
        for i in (0..BATCH_SIZE_BYTES).step_by(2) {
            // pack 2 bytes from 64 different players
            let mut v: [__m128i; 8] = [
                pack8x2!(0x00, i),
                pack8x2!(0x08, i),
                pack8x2!(0x10, i),
                pack8x2!(0x18, i),
                pack8x2!(0x20, i),
                pack8x2!(0x28, i),
                pack8x2!(0x30, i),
                pack8x2!(0x38, i),
            ];

            // calculate the 8 sharings
            let mut idx = i * 8;
            for _ in 0..8 {
                let mut res_lo: [u8; 8] = [0u8; 8];
                let mut res_hi: [u8; 8] = [0u8; 8];

                for i in 0..8 {
                    let mask = _mm_movemask_epi8(v[i]);
                    res_lo[i] = (mask >> 8) as u8;
                    res_hi[i] = mask as u8;
                    v[i] = _mm_add_epi8(v[i], v[i]);
                }

                dst[idx] = BitSharing64(u64::from_le_bytes(res_lo));
                dst[idx + 8] = BitSharing64(u64::from_le_bytes(res_hi));
                idx += 1;
            }
        }
    }

    #[target_feature(enable = "avx2")]
    #[cfg(target_feature = "avx2")]
    unsafe fn convert_inv_avx2(
        dst: &mut [<Self as Domain>::Batch],
        src: &[<Self as Domain>::Sharing],
    ) {
        // NOTE(ww): This is safe, since we fully initialize sharings immediately below.
        // We could probably avoid this with an impl `From<Domain::Sharing> for ...`
        #[allow(clippy::uninit_assumed_init)]
        let mut sharings: [[u8; 8]; <Self as Domain>::Batch::DIMENSION] =
            MaybeUninit::uninit().assume_init();

        for i in 0..<Self as Domain>::Batch::DIMENSION {
            sharings[i] = src[i].0.to_le_bytes();
        }

        macro_rules! pack {
            ( $x:expr, $y:expr ) => {
                *sharings.get_unchecked($x).get_unchecked($y) as i8
            };
        }

        macro_rules! pack8x4 {
            ( $x:expr, $y:expr ) => {
                _mm256_set_epi8(
                    pack!($x, $y),
                    pack!($x + 1, $y),
                    pack!($x + 2, $y),
                    pack!($x + 3, $y),
                    pack!($x + 4, $y),
                    pack!($x + 5, $y),
                    pack!($x + 6, $y),
                    pack!($x + 7, $y),
                    pack!($x, $y + 1),
                    pack!($x + 1, $y + 1),
                    pack!($x + 2, $y + 1),
                    pack!($x + 3, $y + 1),
                    pack!($x + 4, $y + 1),
                    pack!($x + 5, $y + 1),
                    pack!($x + 6, $y + 1),
                    pack!($x + 7, $y + 1),
                    pack!($x, $y + 2),
                    pack!($x + 1, $y + 2),
                    pack!($x + 2, $y + 2),
                    pack!($x + 3, $y + 2),
                    pack!($x + 4, $y + 2),
                    pack!($x + 5, $y + 2),
                    pack!($x + 6, $y + 2),
                    pack!($x + 7, $y + 2),
                    pack!($x, $y + 3),
                    pack!($x + 1, $y + 3),
                    pack!($x + 2, $y + 3),
                    pack!($x + 3, $y + 3),
                    pack!($x + 4, $y + 3),
                    pack!($x + 5, $y + 3),
                    pack!($x + 6, $y + 3),
                    pack!($x + 7, $y + 3),
                )
            };
        }

        // transpose 4 batches at a time, byte-by-byte
        for i in (0..(<Self as Domain>::Sharing::DIMENSION / 8)).step_by(4) {
            // pack 4 bytes from 64 different players
            let mut v: [__m256i; 8] = [
                pack8x4!(0x00, i),
                pack8x4!(0x08, i),
                pack8x4!(0x10, i),
                pack8x4!(0x18, i),
                pack8x4!(0x20, i),
                pack8x4!(0x28, i),
                pack8x4!(0x30, i),
                pack8x4!(0x38, i),
            ];

            // calculate the 8 sharings
            let mut idx = i * 8;

            for _ in 0..8 {
                let masks: [_; 8] = [
                    _mm256_movemask_epi8(v[0]),
                    _mm256_movemask_epi8(v[1]),
                    _mm256_movemask_epi8(v[2]),
                    _mm256_movemask_epi8(v[3]),
                    //
                    _mm256_movemask_epi8(v[4]),
                    _mm256_movemask_epi8(v[5]),
                    _mm256_movemask_epi8(v[6]),
                    _mm256_movemask_epi8(v[7]),
                ];

                dst[idx] = BitBatch([
                    (masks[0] >> 24) as u8,
                    (masks[1] >> 24) as u8,
                    (masks[2] >> 24) as u8,
                    (masks[3] >> 24) as u8,
                    (masks[4] >> 24) as u8,
                    (masks[5] >> 24) as u8,
                    (masks[6] >> 24) as u8,
                    (masks[7] >> 24) as u8,
                ]);

                dst[idx + 8] = BitBatch([
                    (masks[0] >> 16) as u8,
                    (masks[1] >> 16) as u8,
                    (masks[2] >> 16) as u8,
                    (masks[3] >> 16) as u8,
                    (masks[4] >> 16) as u8,
                    (masks[5] >> 16) as u8,
                    (masks[6] >> 16) as u8,
                    (masks[7] >> 16) as u8,
                ]);

                dst[idx + 16] = BitBatch([
                    (masks[0] >> 8) as u8,
                    (masks[1] >> 8) as u8,
                    (masks[2] >> 8) as u8,
                    (masks[3] >> 8) as u8,
                    (masks[4] >> 8) as u8,
                    (masks[5] >> 8) as u8,
                    (masks[6] >> 8) as u8,
                    (masks[7] >> 8) as u8,
                ]);

                dst[idx + 24] = BitBatch([
                    masks[0] as u8,
                    masks[1] as u8,
                    masks[2] as u8,
                    masks[3] as u8,
                    masks[4] as u8,
                    masks[5] as u8,
                    masks[6] as u8,
                    masks[7] as u8,
                ]);

                for i in 0..8 {
                    v[i] = _mm256_add_epi8(v[i], v[i]);
                }

                idx += 1;
            }
        }
    }

    #[target_feature(enable = "sse2")]
    #[cfg(all(target_feature = "sse2", not(target_feature = "avx2")))]
    unsafe fn convert_inv_sse2(
        dst: &mut [<Self as Domain>::Batch],
        src: &[<Self as Domain>::Sharing],
    ) {
        // NOTE(ww): This is safe, since we fully initialize sharings immediately below.
        // We could probably avoid this with an impl `From<Domain::Sharing> for ...`
        #[allow(clippy::uninit_assumed_init)]
        let mut sharings: [[u8; 8]; <Self as Domain>::Batch::DIMENSION] =
            MaybeUninit::uninit().assume_init();

        for i in 0..<Self as Domain>::Batch::DIMENSION {
            sharings[i] = src[i].0.to_le_bytes();
        }

        macro_rules! pack {
            ( $x:expr, $y:expr ) => {
                *sharings.get_unchecked($x).get_unchecked($y) as i8
            };
        }

        macro_rules! pack8x2 {
            ( $x:expr, $y:expr ) => {
                _mm_set_epi8(
                    pack!($x, $y),
                    pack!($x + 1, $y),
                    pack!($x + 2, $y),
                    pack!($x + 3, $y),
                    pack!($x + 4, $y),
                    pack!($x + 5, $y),
                    pack!($x + 6, $y),
                    pack!($x + 7, $y),
                    pack!($x, $y + 1),
                    pack!($x + 1, $y + 1),
                    pack!($x + 2, $y + 1),
                    pack!($x + 3, $y + 1),
                    pack!($x + 4, $y + 1),
                    pack!($x + 5, $y + 1),
                    pack!($x + 6, $y + 1),
                    pack!($x + 7, $y + 1),
                )
            };
        }

        // transpose two batches at a time, byte-by-byte
        for i in (0..(<Self as Domain>::Sharing::DIMENSION / 8)).step_by(2) {
            // pack 2 bytes from 64 different players
            let mut vecs: [__m128i; 8] = [
                pack8x2!(0x00, i),
                pack8x2!(0x08, i),
                pack8x2!(0x10, i),
                pack8x2!(0x18, i),
                pack8x2!(0x20, i),
                pack8x2!(0x28, i),
                pack8x2!(0x30, i),
                pack8x2!(0x38, i),
            ];

            // calculate the 8 sharings
            let mut idx = i * 8;

            for _ in 0..8 {
                let masks: [_; 8] = [
                    _mm_movemask_epi8(vecs[0]),
                    _mm_movemask_epi8(vecs[1]),
                    _mm_movemask_epi8(vecs[2]),
                    _mm_movemask_epi8(vecs[3]),
                    //
                    _mm_movemask_epi8(vecs[4]),
                    _mm_movemask_epi8(vecs[5]),
                    _mm_movemask_epi8(vecs[6]),
                    _mm_movemask_epi8(vecs[7]),
                ];

                dst[idx] = BitBatch([
                    (masks[0] >> 8) as u8,
                    (masks[1] >> 8) as u8,
                    (masks[2] >> 8) as u8,
                    (masks[3] >> 8) as u8,
                    (masks[4] >> 8) as u8,
                    (masks[5] >> 8) as u8,
                    (masks[6] >> 8) as u8,
                    (masks[7] >> 8) as u8,
                ]);

                dst[idx + 8] = BitBatch([
                    masks[0] as u8,
                    masks[1] as u8,
                    masks[2] as u8,
                    masks[3] as u8,
                    masks[4] as u8,
                    masks[5] as u8,
                    masks[6] as u8,
                    masks[7] as u8,
                ]);

                for vec in &mut vecs {
                    *vec = _mm_add_epi8(*vec, *vec);
                }

                idx += 1;
            }
        }
    }
}

impl Domain for GF2P64 {
    type Scalar = BitScalar;
    type Batch = BitBatch;
    type Sharing = BitSharing64;

    const PLAYERS: usize = 64;
    const PREPROCESSING_REPETITIONS: usize = 631;
    const ONLINE_REPETITIONS: usize = 23;

    fn convert(dst: &mut [Self::Sharing], src: &[Self::Batch]) {
        // do a single bounds check up front
        assert_eq!(src.len(), Self::PLAYERS);

        // If we have AVX2, prefer it.
        #[cfg(target_feature = "avx2")]
        return unsafe { Self::convert_avx2(dst, src) };

        // Otherwise, fall back on SSE2.
        #[cfg(all(target_feature = "sse2", not(target_feature = "avx2")))]
        return unsafe { Self::convert_sse2(dst, src) };

        // Without either, fail.
        #[cfg(not(any(target_feature = "avx2", target_feature = "sse2")))]
        compile_error!("unsupported platform: requires x86{-64} with SSE2 or AVX2");
    }

    fn convert_inv(dst: &mut [Self::Batch], src: &[Self::Sharing]) {
        assert_eq!(src.len(), Self::Batch::DIMENSION);
        assert_eq!(dst.len(), Self::Sharing::DIMENSION);

        // If we have AVX2, prefer it.
        #[cfg(target_feature = "avx2")]
        return unsafe { Self::convert_inv_avx2(dst, src) };

        // Otherwise, fall back on SSE2.
        #[cfg(all(target_feature = "sse2", not(target_feature = "avx2")))]
        return unsafe { Self::convert_inv_sse2(dst, src) };

        // Without either, fail.
        #[cfg(not(any(target_feature = "avx2", target_feature = "sse2")))]
        compile_error!("unsupported platform: requires x86{-64} with SSE2 or AVX2");
    }
}