Skip to main content

poulpy_cpu_ref/
lib.rs

1#![allow(clippy::too_many_arguments)]
2
3//! Reference (portable) CPU backend for the Poulpy lattice cryptography library.
4//!
5//! This crate provides two reference implementations for [`poulpy_hal`]:
6//!
7//! - [`FFT64Ref`]: scalar `f64` FFT arithmetic — see the [`fft64`] module.
8//! - [`NTT120Ref`]: scalar Q120 NTT arithmetic (CRT over four ~30-bit primes) — see the [`ntt120`] module.
9//!
10//! Both are canonical reference implementations: portable across all CPU architectures,
11//! prioritising correctness and debuggability over throughput.
12//!
13//! # Platform support
14//!
15//! Compiles and runs on any target supported by the Rust standard library.
16//! No platform-specific intrinsics or assembly are used.
17
18#[cfg(feature = "enable-ckks")]
19mod ckks_impl;
20#[cfg(feature = "enable-core")]
21#[doc(hidden)]
22pub mod core_impl;
23pub mod fft64;
24pub mod hal_defaults;
25mod hal_impl;
26pub mod ntt120;
27pub mod reference;
28
29#[cfg(test)]
30mod tests;
31
32pub use poulpy_hal::cast_mut;
33
34pub mod api {
35    pub use poulpy_hal::api::*;
36}
37
38pub mod layouts {
39    pub use poulpy_hal::layouts::*;
40}
41
42pub mod source {
43    pub use poulpy_hal::source::*;
44}
45
46pub use fft64::{FFT64Ref, FFT64ReimTable};
47pub use ntt120::{NTT120Ref, NTT120RefHandle};
48
49// --- TransferFrom impls ---
50mod transfer_impls {
51    use poulpy_hal::layouts::{Backend, TransferFrom};
52
53    use crate::{FFT64Ref, NTT120Ref};
54
55    impl TransferFrom<FFT64Ref> for FFT64Ref {
56        fn transfer_buf(src: &Vec<u8>) -> Vec<u8> {
57            FFT64Ref::from_host_bytes(&FFT64Ref::to_host_bytes(src))
58        }
59    }
60
61    impl TransferFrom<NTT120Ref> for NTT120Ref {
62        fn transfer_buf(src: &Vec<u8>) -> Vec<u8> {
63            NTT120Ref::from_host_bytes(&NTT120Ref::to_host_bytes(src))
64        }
65    }
66
67    // Cross-family: coefficient-domain buffers are compatible (plain i64 data).
68    // Prepared layouts are backend-specific and must not be transferred directly;
69    // transfer the non-prepared form and re-prepare on the destination backend.
70    impl TransferFrom<NTT120Ref> for FFT64Ref {
71        fn transfer_buf(src: &Vec<u8>) -> Vec<u8> {
72            FFT64Ref::from_host_bytes(&NTT120Ref::to_host_bytes(src))
73        }
74    }
75    impl TransferFrom<FFT64Ref> for NTT120Ref {
76        fn transfer_buf(src: &Vec<u8>) -> Vec<u8> {
77            NTT120Ref::from_host_bytes(&FFT64Ref::to_host_bytes(src))
78        }
79    }
80}