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
22const T: u64 = 128; // msg space (msg modulus)
23type M = Rq<T, 1>; // msg space
24type S = TLWE<256>;
25
26let mut rng = rand::thread_rng();
27let msg_dist = Uniform::new(0_u64, T);
28
29let (sk, pk) = S::new_key(&mut rng)?;
30
31// get two random msgs in Z_t
32let m1 = M::rand_u64(&mut rng, msg_dist)?;
33let m2 = M::rand_u64(&mut rng, msg_dist)?;
34let m3 = M::rand_u64(&mut rng, msg_dist)?;
35
36// encode the msgs into the plaintext space
37let p1 = S::encode::<T>(&m1); // plaintext
38let p2 = S::encode::<T>(&m2); // plaintext
39let c3_const: Tn<1> = Tn(array::from_fn(|i| T64(m3.coeffs()[i].0))); // encode it as constant value
40
41let c1 = S::encrypt(&mut rng, &pk, &p1)?;
42let c2 = S::encrypt(&mut rng, &pk, &p2)?;
43
44// now we can do encrypted operations (notice that we do them using simple
45// operations by operator overloading):
46let c_12 = c1 + c2;
47let c4 = c_12 * c3_const;
48
49// decrypt & decode
50let p4_recovered = c4.decrypt(&sk);
51let m4 = S::decode::<T>(&p4_recovered);
52
53// m4 is equal to (m1+m2)*m3
54```