Added trace operation + test and renamed base2k to backend

This commit is contained in:
Jean-Philippe Bossuat
2025-05-21 16:54:29 +02:00
parent acd81c40c2
commit 27a5395ce2
62 changed files with 1926 additions and 1620 deletions

56
backend/examples/fft.rs Normal file
View File

@@ -0,0 +1,56 @@
use backend::ffi::reim::*;
use std::ffi::c_void;
use std::time::Instant;
fn main() {
let log_bound: usize = 19;
let n: usize = 2048;
let m: usize = n >> 1;
let mut a: Vec<i64> = vec![i64::default(); n];
let mut b: Vec<i64> = vec![i64::default(); n];
let mut c: Vec<i64> = vec![i64::default(); n];
a.iter_mut().enumerate().for_each(|(i, x)| *x = i as i64);
b[1] = 1;
println!("{:?}", b);
unsafe {
let reim_fft_precomp = new_reim_fft_precomp(m as u32, 2);
let reim_ifft_precomp = new_reim_ifft_precomp(m as u32, 1);
let buf_a = reim_fft_precomp_get_buffer(reim_fft_precomp, 0);
let buf_b = reim_fft_precomp_get_buffer(reim_fft_precomp, 1);
let buf_c = reim_ifft_precomp_get_buffer(reim_ifft_precomp, 0);
let now = Instant::now();
(0..1024).for_each(|_| {
reim_from_znx64_simple(m as u32, log_bound as u32, buf_a as *mut c_void, a.as_ptr());
reim_fft(reim_fft_precomp, buf_a);
reim_from_znx64_simple(m as u32, log_bound as u32, buf_b as *mut c_void, b.as_ptr());
reim_fft(reim_fft_precomp, buf_b);
reim_fftvec_mul_simple(
m as u32,
buf_c as *mut c_void,
buf_a as *mut c_void,
buf_b as *mut c_void,
);
reim_ifft(reim_ifft_precomp, buf_c);
reim_to_znx64_simple(
m as u32,
m as f64,
log_bound as u32,
c.as_mut_ptr(),
buf_c as *mut c_void,
)
});
println!("time: {}us", now.elapsed().as_micros());
println!("{:?}", &c[..16]);
}
}

View File

@@ -0,0 +1,133 @@
use backend::{
AddNormal, Decoding, Encoding, FFT64, FillUniform, Module, ScalarZnx, ScalarZnxAlloc, ScalarZnxDft, ScalarZnxDftAlloc,
ScalarZnxDftOps, ScratchOwned, VecZnx, VecZnxAlloc, VecZnxBig, VecZnxBigAlloc, VecZnxBigOps, VecZnxBigScratch, VecZnxDft,
VecZnxDftAlloc, VecZnxDftOps, VecZnxOps, ZnxInfos,
};
use itertools::izip;
use sampling::source::Source;
fn main() {
let n: usize = 16;
let basek: usize = 18;
let ct_size: usize = 3;
let msg_size: usize = 2;
let log_scale: usize = msg_size * basek - 5;
let module: Module<FFT64> = Module::<FFT64>::new(n);
let mut scratch: ScratchOwned = ScratchOwned::new(module.vec_znx_big_normalize_tmp_bytes());
let seed: [u8; 32] = [0; 32];
let mut source: Source = Source::new(seed);
// s <- Z_{-1, 0, 1}[X]/(X^{N}+1)
let mut s: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
s.fill_ternary_prob(0, 0.5, &mut source);
// Buffer to store s in the DFT domain
let mut s_dft: ScalarZnxDft<Vec<u8>, FFT64> = module.new_scalar_znx_dft(s.cols());
// s_dft <- DFT(s)
module.svp_prepare(&mut s_dft, 0, &s, 0);
// Allocates a VecZnx with two columns: ct=(0, 0)
let mut ct: VecZnx<Vec<u8>> = module.new_vec_znx(
2, // Number of columns
ct_size, // Number of small poly per column
);
// Fill the second column with random values: ct = (0, a)
ct.fill_uniform(basek, 1, ct_size, &mut source);
let mut buf_dft: VecZnxDft<Vec<u8>, FFT64> = module.new_vec_znx_dft(1, ct_size);
module.vec_znx_dft(&mut buf_dft, 0, &ct, 1);
// Applies DFT(ct[1]) * DFT(s)
module.svp_apply_inplace(
&mut buf_dft, // DFT(ct[1] * s)
0, // Selects the first column of res
&s_dft, // DFT(s)
0, // Selects the first column of s_dft
);
// Alias scratch space (VecZnxDft<B> is always at least as big as VecZnxBig<B>)
// BIG(ct[1] * s) <- IDFT(DFT(ct[1] * s)) (not normalized)
let mut buf_big: VecZnxBig<Vec<u8>, FFT64> = module.new_vec_znx_big(1, ct_size);
module.vec_znx_idft_tmp_a(&mut buf_big, 0, &mut buf_dft, 0);
// Creates a plaintext: VecZnx with 1 column
let mut m = module.new_vec_znx(
1, // Number of columns
msg_size, // Number of small polynomials
);
let mut want: Vec<i64> = vec![0; n];
want.iter_mut()
.for_each(|x| *x = source.next_u64n(16, 15) as i64);
m.encode_vec_i64(0, basek, log_scale, &want, 4);
module.vec_znx_normalize_inplace(basek, &mut m, 0, scratch.borrow());
// m - BIG(ct[1] * s)
module.vec_znx_big_sub_small_b_inplace(
&mut buf_big,
0, // Selects the first column of the receiver
&m,
0, // Selects the first column of the message
);
// Normalizes back to VecZnx
// ct[0] <- m - BIG(c1 * s)
module.vec_znx_big_normalize(
basek,
&mut ct,
0, // Selects the first column of ct (ct[0])
&buf_big,
0, // Selects the first column of buf_big
scratch.borrow(),
);
// Add noise to ct[0]
// ct[0] <- ct[0] + e
ct.add_normal(
basek,
0, // Selects the first column of ct (ct[0])
basek * ct_size, // Scaling of the noise: 2^{-basek * limbs}
&mut source,
3.2, // Standard deviation
19.0, // Truncatation bound
);
// Final ciphertext: ct = (-a * s + m + e, a)
// Decryption
// DFT(ct[1] * s)
module.vec_znx_dft(&mut buf_dft, 0, &ct, 1);
module.svp_apply_inplace(
&mut buf_dft,
0, // Selects the first column of res.
&s_dft,
0,
);
// BIG(c1 * s) = IDFT(DFT(c1 * s))
module.vec_znx_idft_tmp_a(&mut buf_big, 0, &mut buf_dft, 0);
// BIG(c1 * s) + ct[0]
module.vec_znx_big_add_small_inplace(&mut buf_big, 0, &ct, 0);
// m + e <- BIG(ct[1] * s + ct[0])
let mut res = module.new_vec_znx(1, ct_size);
module.vec_znx_big_normalize(basek, &mut res, 0, &buf_big, 0, scratch.borrow());
// have = m * 2^{log_scale} + e
let mut have: Vec<i64> = vec![i64::default(); n];
res.decode_vec_i64(0, basek, res.size() * basek, &mut have);
let scale: f64 = (1 << (res.size() * basek - log_scale)) as f64;
izip!(want.iter(), have.iter())
.enumerate()
.for_each(|(i, (a, b))| {
println!("{}: {} {}", i, a, (*b as f64) / scale);
});
}