mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 05:06:44 +01:00
various API uniformization
This commit is contained in:
10
rlwe/Cargo.toml
Normal file
10
rlwe/Cargo.toml
Normal file
@@ -0,0 +1,10 @@
|
||||
cargo-features = ["edition2024"]
|
||||
|
||||
[package]
|
||||
name = "rlwe"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
base2k = {path="../base2k"}
|
||||
rand_distr = {workspace = true}
|
||||
17
rlwe/src/ciphertext.rs
Normal file
17
rlwe/src/ciphertext.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use crate::elem::Elem;
|
||||
use crate::parameters::Parameters;
|
||||
use crate::plaintext::Plaintext;
|
||||
|
||||
pub struct Ciphertext(pub Elem);
|
||||
|
||||
impl Parameters {
|
||||
pub fn new_ciphertext(&self, degree: usize, log_q: usize) -> Ciphertext {
|
||||
Ciphertext(self.new_elem(degree, log_q))
|
||||
}
|
||||
}
|
||||
|
||||
impl Ciphertext {
|
||||
pub fn as_plaintext(&self) -> Plaintext {
|
||||
unsafe { Plaintext(std::ptr::read(&self.0)) }
|
||||
}
|
||||
}
|
||||
18
rlwe/src/elem.rs
Normal file
18
rlwe/src/elem.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
use crate::parameters::Parameters;
|
||||
use base2k::VecZnx;
|
||||
|
||||
pub struct Elem {
|
||||
pub value: Vec<VecZnx>,
|
||||
pub log_scale: usize,
|
||||
}
|
||||
|
||||
impl Parameters {
|
||||
pub fn new_elem(&self, degree: usize, log_q: usize) -> Elem {
|
||||
let mut value: Vec<VecZnx> = Vec::new();
|
||||
(0..degree + 1).for_each(|_| value.push(VecZnx::new(self.n(), self.log_base2k(), log_q)));
|
||||
Elem {
|
||||
value: value,
|
||||
log_scale: self.log_scale(),
|
||||
}
|
||||
}
|
||||
}
|
||||
1
rlwe/src/encryptor.rs
Normal file
1
rlwe/src/encryptor.rs
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
5
rlwe/src/lib.rs
Normal file
5
rlwe/src/lib.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
pub mod ciphertext;
|
||||
pub mod elem;
|
||||
pub mod encryptor;
|
||||
pub mod parameters;
|
||||
pub mod plaintext;
|
||||
61
rlwe/src/parameters.rs
Normal file
61
rlwe/src/parameters.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
use base2k::module::{MODULETYPE, Module};
|
||||
|
||||
pub struct ParametersLiteral {
|
||||
pub log_n: usize,
|
||||
pub log_q: usize,
|
||||
pub log_p: usize,
|
||||
pub log_base2k: usize,
|
||||
pub log_scale: usize,
|
||||
pub xe: f64,
|
||||
pub xs: usize,
|
||||
}
|
||||
|
||||
pub struct Parameters {
|
||||
log_n: usize,
|
||||
log_q: usize,
|
||||
log_p: usize,
|
||||
log_scale: usize,
|
||||
log_base2k: usize,
|
||||
xe: f64,
|
||||
xs: usize,
|
||||
module: Module,
|
||||
}
|
||||
|
||||
impl Parameters {
|
||||
pub fn new<const MTYPE: MODULETYPE>(p: &ParametersLiteral) -> Self {
|
||||
assert!(
|
||||
p.log_n + 2 * p.log_base2k <= 53,
|
||||
"invalid parameters: p.log_n + 2*p.log_base2k > 53"
|
||||
);
|
||||
Self {
|
||||
log_n: p.log_n,
|
||||
log_q: p.log_q,
|
||||
log_p: p.log_p,
|
||||
log_scale: p.log_scale,
|
||||
log_base2k: p.log_base2k,
|
||||
xe: p.xe,
|
||||
xs: p.xs,
|
||||
module: Module::new::<MTYPE>(1 << p.log_n),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn n(&self) -> usize {
|
||||
1 << self.log_n
|
||||
}
|
||||
|
||||
pub fn log_scale(&self) -> usize {
|
||||
self.log_scale
|
||||
}
|
||||
|
||||
pub fn log_q(&self) -> usize {
|
||||
self.log_q
|
||||
}
|
||||
|
||||
pub fn log_p(&self) -> usize {
|
||||
self.log_p
|
||||
}
|
||||
|
||||
pub fn log_base2k(&self) -> usize {
|
||||
self.log_base2k
|
||||
}
|
||||
}
|
||||
17
rlwe/src/plaintext.rs
Normal file
17
rlwe/src/plaintext.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use crate::ciphertext::Ciphertext;
|
||||
use crate::elem::Elem;
|
||||
use crate::parameters::Parameters;
|
||||
|
||||
pub struct Plaintext(pub Elem);
|
||||
|
||||
impl Parameters {
|
||||
pub fn new_plaintext(&self, log_q: usize) -> Plaintext {
|
||||
Plaintext(self.new_elem(0, log_q))
|
||||
}
|
||||
}
|
||||
|
||||
impl Plaintext {
|
||||
pub fn as_ciphertext(&self) -> Ciphertext {
|
||||
unsafe { Ciphertext(std::ptr::read(&self.0)) }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user