fhe-study/README.md
README.mdRaw
1# fhe-study
2Implementations from scratch done while studying some FHE papers; do not use in production.
3
4- `arith`: contains $\mathbb{Z}_q$, $R_q=\mathbb{Z}_q[X]/(X^N+1)$, $R=\mathbb{Z}[X]/(X^N+1)$, $\mathbb{T}_q[X]/(X^N +1)$ arithmetic implementations, together with the NTT implementation.
5- `gfhe`: (gfhe=generalized-fhe) contains the structs and logic for RLWE, GLWE, GLev, GGSW, RGSW cryptosystems, and modulus switching and key switching methods, which can be used by concrete FHE schemes.
6- `bfv`: https://eprint.iacr.org/2012/144.pdf scheme implementation
7- `ckks`: https://eprint.iacr.org/2016/421.pdf scheme implementation
8- `tfhe`: https://eprint.iacr.org/2018/421.pdf scheme implementation
9
10
11## Run tests
12`cargo test --release`
13
14## Example of usage
15> the repo is a work in progress, interfaces will change.
16
17This example shows usage of TFHE, but the idea is that the same interface would
18work for using CKKS & BFV, the only thing to be changed would be the parameters
19and the line `type S = TWLE<K>` to use `CKKS<Q, N>` or `BFV<Q, N, T>`.
20
21```rust
22let param = Param {
23 err_sigma: crate::ERR_SIGMA,
24 ring: RingParam { q: u64::MAX, n: 1 },
25 k: 256,
26 t: 128, // plaintext modulus
27};
28
29let mut rng = rand::thread_rng();
30let msg_dist = Uniform::new(0_u64, param.t);
31
32let (sk, pk) = TLWE::new_key(&mut rng, ¶m)?;
33
34// get two random msgs in Z_t
35let m1 = Rq::rand_u64(&mut rng, msg_dist, ¶m.pt())?;
36let m2 = Rq::rand_u64(&mut rng, msg_dist, ¶m.pt())?;
37let m3 = Rq::rand_u64(&mut rng, msg_dist, ¶m.pt())?;
38
39// encode the msgs into the plaintext space
40let p1 = TLWE::encode(¶m, &m1); // plaintext
41let p2 = TLWE::encode(¶m, &m2); // plaintext
42let c3_const: T64 = T64(m3.coeffs()[0].v); // encode it as constant
43
44let c1 = TLWE::encrypt(&mut rng, ¶m, &pk, &p1)?;
45let c2 = TLWE::encrypt(&mut rng, ¶m, &pk, &p2)?;
46
47// now we can do encrypted operations (notice that we do them using simple
48// operation notation by rust's operator overloading):
49let c_12 = c1 + c2;
50let c4 = c_12 * c3_const;
51
52// decrypt & decode
53let p4_recovered = c4.decrypt(&sk);
54let m4 = TLWE::decode(¶m, &p4_recovered);
55
56// m4 is equal to (m1+m2)*m3
57assert_eq!(((m1 + m2).to_r() * m3.to_r()).to_rq(param.t), m4);
58```
59
60
61## Status of the implementation
62
63- TFHE
64 - {TLWE, TGLWE, TLev, TGLev, TGSW, TGGSW} encryption & decryption
65 - addition of ciphertexts, addition & multiplication of ciphertext by a plaintext
66 - external products of ciphertexts
67 - TGSW x TLWE
68 - TGGSW x TGLWE
69 - {TGSW, TGGSW} CMux gate
70 - blind rotation, key switching, mod switching
71 - bootstrapping
72- CKKS
73 - encoding & decoding
74 - encryption & decryption
75 - addition & substraction of ciphertexts
76- BFV
77 - encryption & decryption
78 - addition & substraction of ciphertexts
79 - addition & multiplication of ciphertext by a plaintext
80 - multiplication of ciphertexts with relinearization
81- GFHE (generalized FHE)
82 - {GLWE & GLev} encryption & decryption
83 - key switching, mod switching
84 - addition & substraction of ciphertexts
85 - addition & multiplication of ciphertext by a plaintext
86- arith
87 - base arithmetic for $\mathbb{Z}_q,~~ R_q=\mathbb{Z}_q[X]/(X^N+1),~~ R=\mathbb{Z}[X]/(X^N+1),~~ \mathbb{T}_q[X]/(X^N +1)$
88 - NTT implementation (negacyclic convolution)