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`cargo test --release`
11
12
13## Example of usage
14> the repo is a work in progress, interfaces will change.
15
16This example shows usage of TFHE, but the idea is that the same interface would
17work for using CKKS & BFV, the only thing to be changed would be the parameters
18and the line `type S = TWLE<K>` to use `CKKS<Q, N>` or `BFV<Q, N, T>`.
19
20```rust
21const T: u64 = 128; // msg space (msg modulus)
22type M = Rq<T, 1>; // msg space
23type S = TLWE<256>;
24
25let mut rng = rand::thread_rng();
26let msg_dist = Uniform::new(0_u64, T);
27
28let (sk, pk) = S::new_key(&mut rng)?;
29
30// get two random msgs in Z_t
31let m1 = M::rand_u64(&mut rng, msg_dist)?;
32let m2 = M::rand_u64(&mut rng, msg_dist)?;
33let m3 = M::rand_u64(&mut rng, msg_dist)?;
34
35// encode the msgs into the plaintext space
36let p1 = S::encode::<T>(&m1); // plaintext
37let p2 = S::encode::<T>(&m2); // plaintext
38let c3_const: Tn<1> = Tn(array::from_fn(|i| T64(m3.coeffs()[i].0))); // encode it as constant value
39
40let c1 = S::encrypt(&mut rng, &pk, &p1)?;
41let c2 = S::encrypt(&mut rng, &pk, &p2)?;
42
43// now we can do encrypted operations (notice that we do them using simple
44// operations by operator overloading):
45let c_12 = c1 + c2;
46let c4 = c_12 * c3_const;
47
48// decrypt & decode
49let p4_recovered = c4.decrypt(&sk);
50let m4 = S::decode::<T>(&p4_recovered);
51
52// m4 is equal to (m1+m2)*m3
53```