sunscreen 0.8.1

A Fully Homomorphic Encryption (FHE) compiler supporting the Brakerski/Fan-Vercauteren (BFV) scheme.
Documentation
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
use crate::{
    fhe::{with_fhe_ctx, FheContextOps, Literal},
    types::{
        intern::{Cipher, FheProgramNode},
        ops::*,
        BfvType, FheType, LaneCount, NumCiphertexts, SwapRows, TryFromPlaintext, TryIntoPlaintext,
        Type, TypeName, TypeNameInstance, Version,
    },
    FheProgramInputTrait, InnerPlaintext, Params, Plaintext, WithContext,
};
use seal_fhe::{
    BFVEncoder, BfvEncryptionParametersBuilder, Context as SealContext, Modulus,
    Result as SealResult,
};
use std::ops::*;
use sunscreen_runtime::{Error as RuntimeError, Result as RuntimeResult};

/**
 * A Batched vector of signed integers. The vector has 2 rows of `LANES`
 * columns. The `LANES` value must be a power of 2 up to 16384.
 *
 * # Remarks
 * Plaintexts in the BFV scheme are polynomials. When the plaintext
 * modulus is an appropriate prime number, one can decompose the
 * cyclotomic field into ideals using the Chinese remainder theorem.
 * Each ideal is a value independent of the other and forms a Batched lane.
 *
 * In the BFV scheme using a vector encoding, plaintexts encode as a
 * `2xN/2` matrix, where N is the scheme's polynomial degree.
 * Homomorphic addition, subtraction, and multiplication
 * operate element-wise, thus making the scheme similar to CPU Batched
 * instructions (e.g. Intel AVX or ARM Neon) with the minor distinction
 * that BFV vector types have 2 rows of values.
 *
 * Unlike CPU vector instructions, which typically feature 4-16 lanes,
 * BFV Batched vectors have thousands of lanes. The LANES values
 * effectively demarks a constraint to the compiler that the polynomial
 * degree must be at least 2*LANES. Should the compiler choose a larger
 * degree for unrelated reasons (e.g. noise budget), the Batched type will
 * automatically repeat the lanes so that rotation operations behave
 * as if you only have `LANES` elements. For example, if `LANES` is
 * 4 (not actually a legal value, but illustrative only!)
 *
 * To combine values across multiple lanes, one can use rotation
 * operations. Unlike a shift, rotation operations cause elements to
 * wrap around rather than truncate. The Batched type exposes these as the
 * `<<`, `>>`, and `swap_rows` operators:
 * * `x << n`, where n is a u64 rotates each row n places to the left.
 * For example, `[0, 1, 2, 3; 4, 5, 6, 7] << 3` yields
 * `[3, 0, 1, 2; 7, 4, 5, 6]` (note that real vectors have many more
 * columns).
 * * `x << n`, where n is a u64 rotates each lane n places to the left.
 * For example, `[0, 1, 2, 3; 4, 5, 6, 7] >> 1` yields `[3, 0, 1, 2; 7, 4, 5, 6]`.
 * * `x.swap_rows()` swaps the rows. For example, `[0, 1, 2, 3; 4, 5, 6, 7].swap_rows()` yields `[4, 5, 6, 7; 0, 1, 2, 3]`.
 *
 * # Performance
 * The BFV scheme is parameterized by a number of values. Generally,
 * the polynomial degree has primacy in determining execution time.
 * A smaller polynomial degree results in a smaller noise budget, but
 * each operation is faster. Additionally, a smaller polynomial degree
 * results in fewer Batched lanes in a plaintext.
 *
 * To maximally utilize FHE program throughput, one should choose a `LANES`
 * value equal to half the polynomial degree needed to accomodate the
 * FHE program's noise budget constraint.
 */
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Batched<const LANES: usize> {
    data: [[i64; LANES]; 2],
}

impl<const LANES: usize> NumCiphertexts for Batched<LANES> {
    const NUM_CIPHERTEXTS: usize = 1;
}

impl<const LANES: usize> TypeName for Batched<LANES> {
    fn type_name() -> Type {
        let version = env!("CARGO_PKG_VERSION");

        Type {
            name: format!("sunscreen::types::Batched<{}>", LANES),
            version: Version::parse(version).expect("Crate version is not a valid semver"),
            is_encrypted: false,
        }
    }
}

impl<const LANES: usize> TypeNameInstance for Batched<LANES> {
    fn type_name_instance(&self) -> Type {
        Self::type_name()
    }
}

impl<const LANES: usize> FheProgramInputTrait for Batched<LANES> {}
impl<const LANES: usize> FheType for Batched<LANES> {}
impl<const LANES: usize> BfvType for Batched<LANES> {}

impl<const LANES: usize> std::fmt::Display for Batched<LANES> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let prefix = "[[";
        let middle = "], [";
        let suffix = "]]";

        let chars_remaining = f.width().unwrap_or(usize::MAX);
        let chars_per_row = usize::max(
            usize::saturating_sub(
                chars_remaining / 2,
                prefix.len() + middle.len() + suffix.len(),
            ),
            2,
        );

        if chars_remaining > "[[..], [..]]".len() {
            write!(f, "{}", prefix)?;

            for i in 0..self.data.len() {
                let mut row_chars = chars_per_row;

                for (j, val) in self.data[i].iter().enumerate() {
                    let val = if j < self.data[i].len() {
                        format!("{}, ", val)
                    } else {
                        format!("{}", val)
                    };

                    if val.len() > row_chars + 2 {
                        write!(f, "..",)?;
                        break;
                    } else {
                        write!(f, "{}", val)?;
                    }

                    row_chars -= val.len();
                }

                if i == 0 {
                    write!(f, "{}", middle)?;
                } else {
                    write!(f, "{}", suffix)?;
                }
            }

            Ok(())
        } else if chars_remaining >= "[..]".len() {
            write!(f, "[..]")
        } else {
            write!(f, "")
        }
    }
}

impl<const LANES: usize> TryIntoPlaintext for Batched<LANES> {
    fn try_into_plaintext(
        &self,
        params: &Params,
    ) -> std::result::Result<Plaintext, sunscreen_runtime::Error> {
        if (params.lattice_dimension / 2) as usize % LANES != 0 {
            return Err(RuntimeError::fhe_type_error("LANES must be a power two"));
        }

        if 2 * LANES > params.lattice_dimension as usize {
            return Err(RuntimeError::fhe_type_error(
                "LANES must be <= polynomial degree / 2",
            ));
        }

        let encryption_params = BfvEncryptionParametersBuilder::new()
            .set_poly_modulus_degree(params.lattice_dimension)
            .set_plain_modulus(Modulus::new(params.plain_modulus)?)
            .set_coefficient_modulus(
                params
                    .coeff_modulus
                    .iter()
                    .map(|x| Modulus::new(*x))
                    .collect::<SealResult<Vec<Modulus>>>()?,
            )
            .build()?;

        let context = SealContext::new(&encryption_params, false, params.security_level)?;
        let encoder = BFVEncoder::new(&context)?;

        let reps = params.lattice_dimension as usize / (2 * LANES);

        let data = [self.data[0].repeat(reps), self.data[1].repeat(reps)].concat();

        let plaintext = encoder.encode_signed(&data)?;

        Ok(Plaintext {
            data_type: Self::type_name(),
            inner: InnerPlaintext::Seal(vec![WithContext {
                params: params.clone(),
                data: plaintext,
            }]),
        })
    }
}

impl<const LANES: usize> TryFromPlaintext for Batched<LANES> {
    fn try_from_plaintext(
        plaintext: &Plaintext,
        params: &Params,
    ) -> std::result::Result<Self, sunscreen_runtime::Error> {
        let plaintext = plaintext.inner_as_seal_plaintext()?;

        if plaintext.len() != 1 {
            return Err(sunscreen_runtime::Error::fhe_type_error(
                "Expected 1 plaintext",
            ));
        }

        if plaintext[0].params != *params {
            return Err(sunscreen_runtime::Error::ParameterMismatch);
        }

        let encryption_params = BfvEncryptionParametersBuilder::new()
            .set_poly_modulus_degree(params.lattice_dimension)
            .set_plain_modulus(Modulus::new(params.plain_modulus)?)
            .set_coefficient_modulus(
                params
                    .coeff_modulus
                    .iter()
                    .map(|x| Modulus::new(*x))
                    .collect::<SealResult<Vec<Modulus>>>()?,
            )
            .build()?;

        let context = SealContext::new(&encryption_params, false, params.security_level)?;
        let encoder = BFVEncoder::new(&context)?;

        let data = encoder.decode_signed(&plaintext[0].data)?;

        let (row_0, row_1) = data.split_at(params.lattice_dimension as usize / 2);

        Ok(Self {
            data: [
                row_0
                    .iter()
                    .take(LANES)
                    .copied()
                    .collect::<Vec<i64>>()
                    .try_into()
                    .map_err(|_| {
                        RuntimeError::fhe_type_error(&format!(
                            "Failed to convert Vec to [i64;{}]",
                            LANES
                        ))
                    })?,
                row_1
                    .iter()
                    .take(LANES)
                    .copied()
                    .collect::<Vec<i64>>()
                    .try_into()
                    .map_err(|_| {
                        RuntimeError::fhe_type_error(&format!(
                            "Failed to convert Vec to [i64;{}]",
                            LANES
                        ))
                    })?,
            ],
        })
    }
}

impl<const LANES: usize> TryFrom<[Vec<i64>; 2]> for Batched<LANES> {
    type Error = RuntimeError;

    fn try_from(data: [Vec<i64>; 2]) -> RuntimeResult<Self> {
        Ok(Self {
            data: [
                data[0].clone().try_into().map_err(|_| {
                    RuntimeError::fhe_type_error(&format!(
                        "Failed to convert Vec to [i64;{}]",
                        LANES
                    ))
                })?,
                data[1].clone().try_into().map_err(|_| {
                    RuntimeError::fhe_type_error(&format!(
                        "Failed to convert Vec to [i64;{}]",
                        LANES
                    ))
                })?,
            ],
        })
    }
}

impl<const LANES: usize> From<Batched<LANES>> for [Vec<i64>; 2] {
    fn from(val: Batched<LANES>) -> Self {
        [val.data[0].into(), val.data[1].into()]
    }
}

impl<const LANES: usize> From<[[i64; LANES]; 2]> for Batched<LANES> {
    fn from(data: [[i64; LANES]; 2]) -> Self {
        Self { data }
    }
}

impl<const LANES: usize> From<Batched<LANES>> for [[i64; LANES]; 2] {
    fn from(val: Batched<LANES>) -> Self {
        [val.data[0], val.data[1]]
    }
}

impl<const LANES: usize> From<i64> for Batched<LANES> {
    fn from(data: i64) -> Self {
        // Splat the input across all the lanes.
        Self {
            data: [[data; LANES], [data; LANES]],
        }
    }
}

impl<const LANES: usize> Add for Batched<LANES> {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        let r_0: [i64; LANES] = self.data[0]
            .iter()
            .zip(rhs.data[0].iter())
            .map(|(x, y)| x + y)
            .collect::<Vec<i64>>()
            .try_into()
            .unwrap();
        let r_1: [i64; LANES] = self.data[1]
            .iter()
            .zip(rhs.data[1].iter())
            .map(|(x, y)| x + y)
            .collect::<Vec<i64>>()
            .try_into()
            .unwrap();

        Self { data: [r_0, r_1] }
    }
}

impl<const LANES: usize> Sub for Batched<LANES> {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        let r_0: [i64; LANES] = self.data[0]
            .iter()
            .zip(rhs.data[0].iter())
            .map(|(x, y)| x - y)
            .collect::<Vec<i64>>()
            .try_into()
            .unwrap();
        let r_1: [i64; LANES] = self.data[1]
            .iter()
            .zip(rhs.data[1].iter())
            .map(|(x, y)| x - y)
            .collect::<Vec<i64>>()
            .try_into()
            .unwrap();

        Self { data: [r_0, r_1] }
    }
}

impl<const LANES: usize> Mul for Batched<LANES> {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self::Output {
        let r_0: [i64; LANES] = self.data[0]
            .iter()
            .zip(rhs.data[0].iter())
            .map(|(x, y)| x * y)
            .collect::<Vec<i64>>()
            .try_into()
            .unwrap();
        let r_1: [i64; LANES] = self.data[1]
            .iter()
            .zip(rhs.data[1].iter())
            .map(|(x, y)| x * y)
            .collect::<Vec<i64>>()
            .try_into()
            .unwrap();

        Self { data: [r_0, r_1] }
    }
}

impl<const LANES: usize> Neg for Batched<LANES> {
    type Output = Self;

    fn neg(self) -> Self::Output {
        let r_0: [i64; LANES] = self.data[0]
            .iter()
            .map(|x| -x)
            .collect::<Vec<i64>>()
            .try_into()
            .unwrap();
        let r_1: [i64; LANES] = self.data[1]
            .iter()
            .map(|x| -x)
            .collect::<Vec<i64>>()
            .try_into()
            .unwrap();

        Self { data: [r_0, r_1] }
    }
}

impl<const LANES: usize> Shl<u64> for Batched<LANES> {
    type Output = Self;

    fn shl(self, x: u64) -> Self::Output {
        let r_0: [i64; LANES] = [
            self.data[0]
                .iter()
                .skip(x as usize)
                .copied()
                .collect::<Vec<i64>>(),
            self.data[0]
                .iter()
                .take(x as usize)
                .copied()
                .collect::<Vec<i64>>(),
        ]
        .concat()
        .try_into()
        .unwrap();

        let r_1: [i64; LANES] = [
            self.data[1]
                .iter()
                .skip(x as usize)
                .copied()
                .collect::<Vec<i64>>(),
            self.data[1]
                .iter()
                .take(x as usize)
                .copied()
                .collect::<Vec<i64>>(),
        ]
        .concat()
        .try_into()
        .unwrap();

        Self { data: [r_0, r_1] }
    }
}

impl<const LANES: usize> Shr<u64> for Batched<LANES> {
    type Output = Self;

    fn shr(self, x: u64) -> Self::Output {
        let r_0: [i64; LANES] = [
            self.data[0]
                .iter()
                .skip(LANES - x as usize)
                .copied()
                .collect::<Vec<i64>>(),
            self.data[0]
                .iter()
                .take(LANES - x as usize)
                .copied()
                .collect::<Vec<i64>>(),
        ]
        .concat()
        .try_into()
        .unwrap();

        let r_1: [i64; LANES] = [
            self.data[1]
                .iter()
                .skip(LANES - x as usize)
                .copied()
                .collect::<Vec<i64>>(),
            self.data[1]
                .iter()
                .take(LANES - x as usize)
                .copied()
                .collect::<Vec<i64>>(),
        ]
        .concat()
        .try_into()
        .unwrap();

        Self { data: [r_0, r_1] }
    }
}

impl<const LANES: usize> SwapRows for Batched<LANES> {
    type Output = Self;

    fn swap_rows(self) -> Self::Output {
        Self {
            data: [self.data[1], self.data[0]],
        }
    }
}

impl<const LANES: usize> Index<(usize, usize)> for Batched<LANES> {
    type Output = i64;

    fn index(&self, index: (usize, usize)) -> &Self::Output {
        let (row, col) = index;

        if row != 0 && row != 1 {
            panic!("Out of range [0, 1]");
        }

        &self.data[row][col]
    }
}

impl<const LANES: usize> GraphCipherAdd for Batched<LANES> {
    type Left = Self;
    type Right = Self;

    fn graph_cipher_add(
        a: FheProgramNode<Cipher<Self::Left>>,
        b: FheProgramNode<Cipher<Self::Right>>,
    ) -> FheProgramNode<Cipher<Self::Left>> {
        with_fhe_ctx(|ctx| {
            let n = ctx.add_addition(a.ids[0], b.ids[0]);

            FheProgramNode::new(&[n])
        })
    }
}

impl<const LANES: usize> GraphCipherSub for Batched<LANES> {
    type Left = Self;
    type Right = Self;

    fn graph_cipher_sub(
        a: FheProgramNode<Cipher<Self::Left>>,
        b: FheProgramNode<Cipher<Self::Right>>,
    ) -> FheProgramNode<Cipher<Self::Left>> {
        with_fhe_ctx(|ctx| {
            let n = ctx.add_subtraction(a.ids[0], b.ids[0]);

            FheProgramNode::new(&[n])
        })
    }
}

impl<const LANES: usize> GraphCipherMul for Batched<LANES> {
    type Left = Self;
    type Right = Self;

    fn graph_cipher_mul(
        a: FheProgramNode<Cipher<Self::Left>>,
        b: FheProgramNode<Cipher<Self::Right>>,
    ) -> FheProgramNode<Cipher<Self::Left>> {
        with_fhe_ctx(|ctx| {
            let n = ctx.add_multiplication(a.ids[0], b.ids[0]);

            FheProgramNode::new(&[n])
        })
    }
}

impl<const LANES: usize> GraphCipherInsert for Batched<LANES> {
    type Lit = i64;
    type Val = Self;

    fn graph_cipher_insert(lit: Self::Lit) -> FheProgramNode<Self::Val> {
        with_fhe_ctx(|ctx| {
            let lit = Self::from(lit).try_into_plaintext(&ctx.data).unwrap();
            let l = ctx.add_plaintext_literal(lit.inner);

            FheProgramNode::new(&[l])
        })
    }
}

impl<const LANES: usize> GraphCipherConstMul for Batched<LANES> {
    type Left = Self;
    type Right = i64;

    fn graph_cipher_const_mul(
        a: FheProgramNode<Cipher<Self::Left>>,
        b: Self::Right,
    ) -> FheProgramNode<Cipher<Self::Left>> {
        let l = Self::graph_cipher_insert(b);
        with_fhe_ctx(|ctx| {
            let n = ctx.add_multiplication_plaintext(a.ids[0], l.ids[0]);

            FheProgramNode::new(&[n])
        })
    }
}

impl<const LANES: usize> GraphCipherSwapRows for Batched<LANES> {
    fn graph_cipher_swap_rows(x: FheProgramNode<Cipher<Self>>) -> FheProgramNode<Cipher<Self>> {
        with_fhe_ctx(|ctx| {
            let n = ctx.add_swap_rows(x.ids[0]);

            FheProgramNode::new(&[n])
        })
    }
}

impl<const LANES: usize> GraphCipherRotateLeft for Batched<LANES> {
    fn graph_cipher_rotate_left(
        x: FheProgramNode<Cipher<Self>>,
        y: u64,
    ) -> FheProgramNode<Cipher<Self>> {
        with_fhe_ctx(|ctx| {
            let y = ctx.add_literal(Literal::U64(y));
            let n = ctx.add_rotate_left(x.ids[0], y);

            FheProgramNode::new(&[n])
        })
    }
}

impl<const LANES: usize> GraphCipherRotateRight for Batched<LANES> {
    fn graph_cipher_rotate_right(
        x: FheProgramNode<Cipher<Self>>,
        y: u64,
    ) -> FheProgramNode<Cipher<Self>> {
        with_fhe_ctx(|ctx| {
            let y = ctx.add_literal(Literal::U64(y));
            let n = ctx.add_rotate_right(x.ids[0], y);

            FheProgramNode::new(&[n])
        })
    }
}

impl<const LANES: usize> GraphCipherNeg for Batched<LANES> {
    type Val = Self;

    fn graph_cipher_neg(x: FheProgramNode<Cipher<Self>>) -> FheProgramNode<Cipher<Self::Val>> {
        with_fhe_ctx(|ctx| {
            let n = ctx.add_negate(x.ids[0]);

            FheProgramNode::new(&[n])
        })
    }
}

impl<const LANES: usize> LaneCount for Batched<LANES> {
    fn lane_count() -> usize {
        LANES
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::SchemeType;
    use seal_fhe::{CoefficientModulus, PlainModulus, SecurityLevel};

    #[test]
    fn can_roundtrip_encode_batched() {
        let data = [vec![0, 1, 2, 3], vec![4, 5, 6, 7]];

        let params = Params {
            lattice_dimension: 4096,
            plain_modulus: PlainModulus::batching(4096, 16).unwrap().value(),
            coeff_modulus: CoefficientModulus::bfv_default(4096, SecurityLevel::TC128)
                .unwrap()
                .iter()
                .map(|x| x.value())
                .collect::<Vec<u64>>(),
            scheme_type: SchemeType::Bfv,
            security_level: SecurityLevel::TC128,
        };

        let x = Batched::<4>::try_from(data).unwrap();

        let plaintext = x.try_into_plaintext(&params).unwrap();
        let y = Batched::<4>::try_from_plaintext(&plaintext, &params).unwrap();

        assert_eq!(x, y);
    }

    const A_VEC: [[i64; 4]; 2] = [[1, 2, 3, 4], [5, 6, 7, 8]];
    const B_VEC: [[i64; 4]; 2] = [[5, 6, 7, 8], [1, 2, 3, 4]];

    #[test]
    fn can_add_non_fhe() {
        let a = Batched::<4>::try_from(A_VEC).unwrap();
        let b = Batched::<4>::try_from(B_VEC).unwrap();

        assert_eq!(a + b, [[6, 8, 10, 12], [6, 8, 10, 12]].into());
    }

    #[test]
    fn can_mul_non_fhe() {
        let a = Batched::<4>::try_from(A_VEC).unwrap();
        let b = Batched::<4>::try_from(B_VEC).unwrap();

        assert_eq!(a * b, [[5, 12, 21, 32], [5, 12, 21, 32]].into());
    }

    #[test]
    fn can_sub_non_fhe() {
        let a = Batched::<4>::try_from(A_VEC).unwrap();
        let b = Batched::<4>::try_from(B_VEC).unwrap();

        assert_eq!(a - b, [[-4, -4, -4, -4], [4, 4, 4, 4]].into());
    }

    #[test]
    fn can_neg_non_fhe() {
        let a = Batched::<4>::try_from(A_VEC).unwrap();

        assert_eq!(-a, [[-1, -2, -3, -4], [-5, -6, -7, -8]].into());
    }

    #[test]
    fn can_shl_non_fhe() {
        let a = Batched::<4>::try_from(A_VEC).unwrap();

        assert_eq!(a << 3, [[4, 1, 2, 3], [8, 5, 6, 7]].into());
    }

    #[test]
    fn can_shr_non_fhe() {
        let a = Batched::<4>::try_from(A_VEC).unwrap();

        assert_eq!(a >> 3, [[2, 3, 4, 1], [6, 7, 8, 5]].into());
    }

    #[test]
    fn can_swap_rows_non_fhe() {
        let a = Batched::<4>::try_from(A_VEC).unwrap();

        assert_eq!(a.swap_rows(), [[5, 6, 7, 8], [1, 2, 3, 4]].into());
    }
}