5 releases (breaking)

0.5.0 Apr 4, 2024
0.4.0 Apr 1, 2024
0.3.0 Mar 31, 2024
0.2.0 Mar 31, 2024
0.1.0 Mar 31, 2024

#736 in Algorithms

MIT and LGPL-3.0+

24KB
576 lines

bgv-rs

An efficient FHE library

Quickstart

[dependencies]
bgv = "0.5.0"

Example

use rand::Rng;
use bgv::{math, BGV};

const T: u64 = 65537; // plaintext modulus
const N: usize = 1024; // polynomial degree
const L: usize = 15; // levels
const NBITS: u8 = 50; // small prime bit length
const MU: f64 = 0.0; // sampling mean
const SIGMA: f64 = 3.19; // sampling std. dev

let mut rng = rand::thread_rng();
let b = BGV::new(N, L, NBITS, MU, SIGMA, T);
let k = b.key_gen(); // generate keys

let m1: Vec<u64> = (0..N).map(|_| rng.gen_range(0..T) as u64).collect();
let m2: Vec<u64> = (0..N).map(|_| rng.gen_range(0..T) as u64).collect();
let sum: Vec<u64> = (0..N).map(|i| math::modadd(m1[i], m2[i], T)).collect();

let x = b.encrypt(&m1, &k.pk);
let y = b.encrypt(&m2, &k.pk);
let c = b.add(&x, &y); // homomorphic addition

let m3 = b.decrypt(&c, &k.s);
assert_eq!(m3, sum);

lib.rs:

An efficient FHE library

Example

 use rand::Rng;
 use bgv::{math, BGV};

 const T: u64 = 65537;
 const N: usize = 1024;
 const L: usize = 15;
 const NBITS: u8 = 50;
 const MU: f64 = 0.0;
 const SIGMA: f64 = 3.19;

 let mut rng = rand::thread_rng();
 let b = BGV::new(N, L, NBITS, MU, SIGMA, T);
 let k = b.key_gen();

 let m1: Vec<u64> = (0..N).map(|_| rng.gen_range(0..T) as u64).collect();
 let m2: Vec<u64> = (0..N).map(|_| rng.gen_range(0..T) as u64).collect();
 let sum: Vec<u64> = (0..N).map(|i| math::modadd(m1[i], m2[i], T)).collect();

 let x = b.encrypt(&m1, &k.pk);
 let y = b.encrypt(&m2, &k.pk);
 let c = b.add(&x, &y);

 let m3 = b.decrypt(&c, &k.s);
 assert_eq!(m3, sum);

Dependencies

~4.5MB
~81K SLoC