tfhe 1.6.0

TFHE-rs is a fully homomorphic encryption (FHE) library that implements Zama's variant of TFHE.
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
use crate::core_crypto::prelude::{CastFrom, Numeric, SignedNumeric};
use std::ops::{AddAssign, ShlAssign};

const fn max_value_for_signed_u64_based_integer<const N: usize>() -> [u64; N] {
    let mut max = [u64::MAX; N];
    max[N - 1] = u64::MAX >> 1;
    max
}

const fn min_value_for_signed_u64_based_integer<const N: usize>() -> [u64; N] {
    let mut max = [0u64; N];
    max[N - 1] = 1u64 << 63;
    max
}

const fn one_for_signed_u64_based_integer<const N: usize>() -> [u64; N] {
    let mut max = [0u64; N];
    max[0] = 1u64;
    max
}

const fn two_for_signed_u64_based_integer<const N: usize>() -> [u64; N] {
    let mut max = [0u64; N];
    max[0] = 2u64;
    max
}

// Little endian order
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(transparent)]
pub struct StaticSignedBigInt<const N: usize>(pub(crate) [u64; N]);

// Cannot derive Default, [u64, N] only impl default for a few sizes
impl<const N: usize> Default for StaticSignedBigInt<N> {
    fn default() -> Self {
        Self([0u64; N])
    }
}

impl<const N: usize> From<[u64; N]> for StaticSignedBigInt<N> {
    fn from(array: [u64; N]) -> Self {
        Self(array)
    }
}

impl<const N: usize> StaticSignedBigInt<N> {
    pub const BITS: u32 = u64::BITS * N as u32;
    pub const MAX: Self = Self(max_value_for_signed_u64_based_integer());
    pub const MIN: Self = Self(min_value_for_signed_u64_based_integer());
    pub const ZERO: Self = Self([0; N]);
    pub const ONE: Self = Self(one_for_signed_u64_based_integer());
    pub const TWO: Self = Self(two_for_signed_u64_based_integer());

    pub fn data(&self) -> &[u64; N] {
        &self.0
    }

    pub fn wrapping_neg(self) -> Self {
        -self
    }

    pub fn wrapping_sub(self, other: Self) -> Self {
        self - other
    }

    /// Replaces the current value by interpreting the bytes in big endian order
    pub fn copy_from_be_byte_slice(&mut self, bytes: &[u8]) {
        super::algorithms::copy_from_be_byte_slice(self.0.as_mut_slice(), bytes);
    }

    /// Replaces the current value by interpreting the bytes in little endian order
    pub fn copy_from_le_byte_slice(&mut self, bytes: &[u8]) {
        super::algorithms::copy_from_le_byte_slice(self.0.as_mut_slice(), bytes);
    }

    pub fn copy_to_le_byte_slice(&self, bytes: &mut [u8]) {
        super::algorithms::copy_to_le_byte_slice(self.0.as_slice(), bytes);
    }

    pub fn copy_to_be_byte_slice(&self, bytes: &mut [u8]) {
        super::algorithms::copy_to_be_byte_slice(self.0.as_slice(), bytes);
    }

    pub fn is_power_of_two(self) -> bool {
        if self <= Self::ZERO {
            return false;
        }
        (self & (self - Self::ONE)) == Self::ZERO
    }

    pub fn leading_zeros(self) -> u32 {
        super::algorithms::leading_zeros(self.0.as_slice())
    }

    pub fn ilog2(self) -> u32 {
        // Rust has the same assert
        assert!(
            self > Self::ZERO,
            "argument of integer logarithm must be positive"
        );
        (self.0.len() as u32 * u64::BITS) - self.leading_zeros() - 1
    }

    pub fn ceil_ilog2(self) -> u32 {
        self.ilog2() + u32::from(!self.is_power_of_two())
    }

    pub fn wrapping_abs(self) -> Self {
        super::algorithms::absolute_value(self)
    }
}

impl<const N: usize> std::cmp::Ord for StaticSignedBigInt<N> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        super::algorithms::compare_signed(&self.0, &other.0)
    }
}

impl<const N: usize> std::cmp::PartialOrd for StaticSignedBigInt<N> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl<const N: usize> std::ops::Add<Self> for StaticSignedBigInt<N> {
    type Output = Self;

    fn add(mut self, rhs: Self) -> Self::Output {
        self += rhs;
        self
    }
}

impl<const N: usize> std::ops::AddAssign<Self> for StaticSignedBigInt<N> {
    fn add_assign(&mut self, rhs: Self) {
        super::algorithms::wrapping_add_assign_words(self.0.as_mut_slice(), rhs.0.as_slice());
    }
}

impl<const N: usize> std::ops::Neg for StaticSignedBigInt<N> {
    type Output = Self;

    fn neg(mut self) -> Self::Output {
        super::algorithms::bitnot_assign(&mut self.0);
        self += Self::ONE;
        self
    }
}

impl<const N: usize> std::ops::Not for StaticSignedBigInt<N> {
    type Output = Self;

    fn not(mut self) -> Self::Output {
        super::algorithms::bitnot_assign(&mut self.0);
        self
    }
}

impl<const N: usize> std::ops::SubAssign<Self> for StaticSignedBigInt<N> {
    fn sub_assign(&mut self, rhs: Self) {
        self.add_assign(-rhs);
    }
}

impl<const N: usize> std::ops::Sub<Self> for StaticSignedBigInt<N> {
    type Output = Self;

    fn sub(mut self, rhs: Self) -> Self::Output {
        self -= rhs;
        self
    }
}

impl<const N: usize> std::ops::MulAssign<Self> for StaticSignedBigInt<N> {
    fn mul_assign(&mut self, rhs: Self) {
        if rhs.is_power_of_two() {
            self.shl_assign(rhs.ilog2());
            return;
        }
        super::algorithms::schoolbook_mul_assign(self.0.as_mut_slice(), rhs.0.as_slice());
    }
}

impl<const N: usize> std::ops::Mul<Self> for StaticSignedBigInt<N> {
    type Output = Self;

    fn mul(mut self, rhs: Self) -> Self::Output {
        self *= rhs;
        self
    }
}

impl<const N: usize> std::ops::DivAssign<Self> for StaticSignedBigInt<N> {
    fn div_assign(&mut self, rhs: Self) {
        *self = *self / rhs;
    }
}

impl<const N: usize> std::ops::Div<Self> for StaticSignedBigInt<N> {
    type Output = Self;

    fn div(self, rhs: Self) -> Self::Output {
        let (q, _) = super::algorithms::slow_div_signed(self, rhs);
        q
    }
}

impl<const N: usize> std::ops::RemAssign<Self> for StaticSignedBigInt<N> {
    fn rem_assign(&mut self, rhs: Self) {
        *self = *self % rhs;
    }
}

impl<const N: usize> std::ops::Rem<Self> for StaticSignedBigInt<N> {
    type Output = Self;

    fn rem(self, rhs: Self) -> Self::Output {
        let (_, r) = super::algorithms::slow_div_signed(self, rhs);
        r
    }
}

impl<const N: usize> std::ops::ShrAssign<u32> for StaticSignedBigInt<N> {
    fn shr_assign(&mut self, shift: u32) {
        super::algorithms::shr_assign(
            self.0.as_mut_slice(),
            shift,
            super::algorithms::ShiftType::Arithmetic,
        );
    }
}

impl<const N: usize> std::ops::Shr<u32> for StaticSignedBigInt<N> {
    type Output = Self;

    fn shr(mut self, rhs: u32) -> Self::Output {
        self >>= rhs;
        self
    }
}

impl<const N: usize> std::ops::ShrAssign<usize> for StaticSignedBigInt<N> {
    fn shr_assign(&mut self, shift: usize) {
        super::algorithms::shr_assign(
            self.0.as_mut_slice(),
            shift as u32,
            super::algorithms::ShiftType::Arithmetic,
        );
    }
}

impl<const N: usize> std::ops::Shr<usize> for StaticSignedBigInt<N> {
    type Output = Self;

    fn shr(mut self, rhs: usize) -> Self::Output {
        self >>= rhs;
        self
    }
}

impl<const N: usize> std::ops::ShlAssign<u32> for StaticSignedBigInt<N> {
    fn shl_assign(&mut self, shift: u32) {
        super::algorithms::shl_assign(self.0.as_mut_slice(), shift);
    }
}

impl<const N: usize> std::ops::ShlAssign<usize> for StaticSignedBigInt<N> {
    fn shl_assign(&mut self, shift: usize) {
        super::algorithms::shl_assign(self.0.as_mut_slice(), shift as u32);
    }
}

impl<const N: usize> std::ops::Shl<u32> for StaticSignedBigInt<N> {
    type Output = Self;

    fn shl(mut self, rhs: u32) -> Self::Output {
        self <<= rhs;
        self
    }
}

impl<const N: usize> std::ops::Shl<usize> for StaticSignedBigInt<N> {
    type Output = Self;

    fn shl(mut self, rhs: usize) -> Self::Output {
        self <<= rhs;
        self
    }
}

impl<const N: usize> std::ops::BitAndAssign<Self> for StaticSignedBigInt<N> {
    fn bitand_assign(&mut self, rhs: Self) {
        super::algorithms::bitand_assign(self.0.as_mut_slice(), rhs.0.as_slice());
    }
}

impl<const N: usize> std::ops::BitAnd<Self> for StaticSignedBigInt<N> {
    type Output = Self;

    fn bitand(mut self, rhs: Self) -> Self::Output {
        self &= rhs;
        self
    }
}

impl<const N: usize> std::ops::BitOrAssign<Self> for StaticSignedBigInt<N> {
    fn bitor_assign(&mut self, rhs: Self) {
        super::algorithms::bitor_assign(self.0.as_mut_slice(), rhs.0.as_slice());
    }
}

impl<const N: usize> std::ops::BitOr<Self> for StaticSignedBigInt<N> {
    type Output = Self;

    fn bitor(mut self, rhs: Self) -> Self::Output {
        self |= rhs;
        self
    }
}

impl<const N: usize> std::ops::BitXorAssign<Self> for StaticSignedBigInt<N> {
    fn bitxor_assign(&mut self, rhs: Self) {
        super::algorithms::bitxor_assign(self.0.as_mut_slice(), rhs.0.as_slice());
    }
}

impl<const N: usize> std::ops::BitXor<Self> for StaticSignedBigInt<N> {
    type Output = Self;

    fn bitxor(mut self, rhs: Self) -> Self::Output {
        self ^= rhs;
        self
    }
}

// SAFETY
//
// StaticBigInt<N> is allowed to be all zeros
unsafe impl<const N: usize> bytemuck::Zeroable for StaticSignedBigInt<N> {}

// SAFETY
//
// u64 impl bytemuck::Pod,
// [T; N] impl bytemuck::Pod if T: bytemuck::Pod
//
// https://docs.rs/bytemuck/latest/bytemuck/trait.Pod.html#foreign-impls
//
// Thus StaticBigInt<N> can safely be considered Pod
unsafe impl<const N: usize> bytemuck::Pod for StaticSignedBigInt<N> {}

impl<const N: usize> Numeric for StaticSignedBigInt<N> {
    const BITS: usize = Self::BITS as usize;

    const ZERO: Self = Self::ZERO;

    const ONE: Self = Self::ONE;

    const TWO: Self = Self::TWO;

    const MAX: Self = Self::MAX;
}

impl<const N: usize> SignedNumeric for StaticSignedBigInt<N> {
    type NumericUnsignedType = super::static_unsigned::StaticUnsignedBigInt<N>;
}

impl<const N: usize> From<i32> for StaticSignedBigInt<N> {
    fn from(value: i32) -> Self {
        Self::from(value as i64)
    }
}

impl<const N: usize> From<i64> for StaticSignedBigInt<N> {
    fn from(value: i64) -> Self {
        // Casting from signed to unsigned is a no op
        if value < 0 {
            let mut converted = [u64::MAX; N];
            converted[0] = value as u64;
            Self(converted)
        } else {
            let mut converted = [u64::ZERO; N];
            converted[0] = value as u64;
            Self(converted)
        }
    }
}

impl<const N: usize> From<i128> for StaticSignedBigInt<N> {
    fn from(value: i128) -> Self {
        // Casting from signed to unsigned is a no op
        let mut converted = if value < 0 {
            [u64::MAX; N]
        } else {
            [u64::ZERO; N]
        };

        converted[0] = value as u64;
        if let Some(v) = converted.get_mut(1) {
            *v = ((value as u128) >> i64::BITS) as u64;
        }
        Self(converted)
    }
}

impl<const N: usize> From<(u64, u64, u64, u64)> for StaticSignedBigInt<N> {
    fn from(value: (u64, u64, u64, u64)) -> Self {
        let mut converted = [u64::ZERO; N];
        if let Some(e) = converted.get_mut(0) {
            *e = value.0;
        }
        if let Some(e) = converted.get_mut(1) {
            *e = value.1;
        }
        if let Some(e) = converted.get_mut(2) {
            *e = value.2;
        }
        if let Some(e) = converted.get_mut(3) {
            *e = value.3;
        }
        Self(converted)
    }
}

impl<const N_IN: usize, const N_OUT: usize> CastFrom<StaticSignedBigInt<N_IN>>
    for StaticSignedBigInt<N_OUT>
{
    fn cast_from(input: StaticSignedBigInt<N_IN>) -> Self {
        // Get rid of special case, will be removed at compile time, no runtime impact
        if N_IN == 0 {
            return Self::ZERO;
        }

        // Safe to unwrap, N_IN > 0
        let sign = input.0.last().unwrap() >> 63;
        // If sign == 1, i.e. top bit was set, then the BigInt was negative, taking the wrapping_neg
        // yields u64::MAX == -1 == 0xffffffffffffffff, if sign == 0, wrapping_neg does nothing and
        // we get 0, this is a branchless way of getting the fill u64 for sign extension
        let fill = sign.wrapping_neg();

        let smallest = N_IN.min(N_OUT);
        // init data with fill will naturally sign extend as required, since the low bits will be
        // copied and the high bits will be 1s or 0s depending on sign
        let mut data = [fill; N_OUT];
        data[..smallest].copy_from_slice(&input.0[..smallest]);
        Self(data)
    }
}

impl<const N: usize> CastFrom<u8> for StaticSignedBigInt<N> {
    fn cast_from(input: u8) -> Self {
        let mut converted = [u64::ZERO; N];
        converted[0] = input as u64;
        Self(converted)
    }
}

impl<const N: usize> CastFrom<u16> for StaticSignedBigInt<N> {
    fn cast_from(input: u16) -> Self {
        let mut converted = [u64::ZERO; N];
        converted[0] = input as u64;
        Self(converted)
    }
}

impl<const N: usize> CastFrom<u32> for StaticSignedBigInt<N> {
    fn cast_from(input: u32) -> Self {
        let mut converted = [u64::ZERO; N];
        converted[0] = input as u64;
        Self(converted)
    }
}

impl<const N: usize> CastFrom<u64> for StaticSignedBigInt<N> {
    fn cast_from(input: u64) -> Self {
        let mut converted = [u64::ZERO; N];
        converted[0] = input;
        Self(converted)
    }
}

impl<const N: usize> CastFrom<u128> for StaticSignedBigInt<N> {
    fn cast_from(input: u128) -> Self {
        let mut converted = [u64::ZERO; N];
        if N == 0 {
            return Self(converted);
        }

        converted[0] = input as u64;
        if N == 1 {
            return Self(converted);
        }

        converted[1] = (input >> 64) as u64;
        Self(converted)
    }
}

impl<const N: usize> CastFrom<i8> for StaticSignedBigInt<N> {
    fn cast_from(input: i8) -> Self {
        Self::from(input as i128)
    }
}

impl<const N: usize> CastFrom<i16> for StaticSignedBigInt<N> {
    fn cast_from(input: i16) -> Self {
        Self::from(input as i128)
    }
}

impl<const N: usize> CastFrom<i32> for StaticSignedBigInt<N> {
    fn cast_from(input: i32) -> Self {
        Self::from(input as i128)
    }
}

impl<const N: usize> CastFrom<i64> for StaticSignedBigInt<N> {
    fn cast_from(input: i64) -> Self {
        Self::from(input as i128)
    }
}

impl<const N: usize> CastFrom<i128> for StaticSignedBigInt<N> {
    fn cast_from(input: i128) -> Self {
        Self::from(input)
    }
}

impl<const N: usize> CastFrom<StaticSignedBigInt<N>> for u8 {
    fn cast_from(input: StaticSignedBigInt<N>) -> Self {
        input.0[0] as Self
    }
}

impl<const N: usize> CastFrom<StaticSignedBigInt<N>> for u16 {
    fn cast_from(input: StaticSignedBigInt<N>) -> Self {
        input.0[0] as Self
    }
}

impl<const N: usize> CastFrom<StaticSignedBigInt<N>> for u32 {
    fn cast_from(input: StaticSignedBigInt<N>) -> Self {
        input.0[0] as Self
    }
}

impl<const N: usize> CastFrom<StaticSignedBigInt<N>> for u64 {
    fn cast_from(input: StaticSignedBigInt<N>) -> Self {
        input.0[0]
    }
}

impl<const N: usize> CastFrom<StaticSignedBigInt<N>> for u128 {
    fn cast_from(input: StaticSignedBigInt<N>) -> Self {
        let inner = &input.0;
        inner[0] as Self | ((inner.get(1).copied().unwrap_or(0) as Self) << 64)
    }
}

impl<const N: usize> CastFrom<super::static_unsigned::StaticUnsignedBigInt<N>>
    for StaticSignedBigInt<N>
{
    fn cast_from(input: super::static_unsigned::StaticUnsignedBigInt<N>) -> Self {
        Self(input.0)
    }
}
impl<const N: usize> CastFrom<StaticSignedBigInt<N>> for i128 {
    fn cast_from(input: StaticSignedBigInt<N>) -> Self {
        let inner = &input.0;
        inner[0] as Self | ((inner.get(1).copied().unwrap_or(0) as Self) << 64)
    }
}

#[cfg(test)]
mod test {
    use crate::core_crypto::commons::numeric::CastFrom;
    use crate::integer::bigint::StaticSignedBigInt;

    #[test]
    fn test_u128_cast() {
        let a = 1_u128 << 64;

        let b = StaticSignedBigInt::<4>::cast_from(a);

        let c = u128::cast_from(b);

        assert_eq!(a, c);
    }

    #[test]
    fn test_i128_cast() {
        {
            let a = 1_i128 << 64;

            let b: StaticSignedBigInt<4> = a.into();

            let c = i128::cast_from(b);

            assert_eq!(a, c);
        }

        {
            let a = -1_i128 << 64;

            let b: StaticSignedBigInt<4> = a.into();

            let c = i128::cast_from(b);

            assert_eq!(a, c);
            assert_eq!(b, StaticSignedBigInt::<4>::ONE.wrapping_neg() << 64u32);

            let d = StaticSignedBigInt::<5>::cast_from(b);
            let e = i128::cast_from(d);

            assert_eq!(a, e);
            assert_eq!(d, StaticSignedBigInt::<5>::ONE.wrapping_neg() << 64u32);
        }
    }
}