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 usage of `TLWE` by `CKKS` or `BFV`.
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::rng();
30let msg_dist = Uniform::new(0_u64, param.t);
31
32let (sk, pk) = TLWE::new_key(&mut rng, ¶m)?;
33
34// get three random msgs in Rt
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 = TLWE::new_const(¶m, &m3); // as constant/public value
43
44// encrypt p1 and m2
45let c1 = TLWE::encrypt(&mut rng, ¶m, &pk, &p1)?;
46let c2 = TLWE::encrypt(&mut rng, ¶m, &pk, &p2)?;
47
48// now we can do encrypted operations (notice that we do them using simple
49// operation notation by rust's operator overloading):
50let c_12 = c1 + c2;
51let c4 = c_12 * c3_const;
52
53// decrypt & decode
54let p4_recovered = c4.decrypt(&sk);
55let m4 = TLWE::decode(¶m, &p4_recovered);
56
57// m4 is equal to (m1+m2)*m3
58assert_eq!(((m1 + m2).to_r() * m3.to_r()).to_rq(param.t), m4);
59```
60
61
62## Status of the implementation
63
64- TFHE
65 - {TLWE, TGLWE, TLev, TGLev, TGSW, TGGSW} encryption & decryption
66 - addition of ciphertexts, addition & multiplication of ciphertext by a plaintext
67 - external products of ciphertexts
68 - TGSW x TLWE
69 - TGGSW x TGLWE
70 - {TGSW, TGGSW} CMux gate
71 - blind rotation, key switching, mod switching
72 - bootstrapping
73- CKKS
74 - encoding & decoding
75 - encryption & decryption
76 - addition & substraction of ciphertexts
77- BFV
78 - encryption & decryption
79 - addition & substraction of ciphertexts
80 - addition & multiplication of ciphertext by a plaintext
81 - multiplication of ciphertexts with relinearization
82- GFHE (generalized FHE)
83 - {GLWE & GLev} encryption & decryption
84 - key switching, mod switching
85 - addition & substraction of ciphertexts
86 - addition & multiplication of ciphertext by a plaintext
87- arith
88 - 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)$
89 - NTT implementation (negacyclic convolution)