Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add serde support #278

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 38 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ prost = "^0.12.4"
prost-build = "^0.12.3"
rand = "^0.8.5"
rand_chacha = "^0.3.1"
serde = "1.0.217"
sha2 = "^0.10.8"
thiserror = "^1.0.58"
zeroize = "^1.8.0"
Expand Down
4 changes: 3 additions & 1 deletion crates/fhe-math/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ rust-version.workspace = true
bench = false # Disable default bench (we use criterion)

[features]
serde = ["dep:serde", "ndarray/serde", "num-bigint/serde", "rand/serde1", "rand_chacha/serde"]
concrete-ntt = []
concrete-ntt-nightly = ["concrete-ntt/nightly"]

Expand All @@ -30,12 +31,13 @@ num-bigint.workspace = true
num-bigint-dig.workspace = true
num-traits.workspace = true
prost.workspace = true
pulp = "^0.18.9"
pulp = { git = "https://github.com/zefr0x/pulp.git", branch = "implserde", features = ["serde"] }
Comment on lines -33 to +34
Copy link
Author

@zefr0x zefr0x Jan 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before merging this, pulp should support serde first.

Temporary I have a fork that does it partially to make things work, but it should be upstreamed for new versions.

Also, I don't really know if pulp::Arch is important to be serialized or if it can be reconstructed from other values in the struct, is it possible to drop it?

rand.workspace = true
rand_chacha.workspace = true
thiserror.workspace = true
zeroize.workspace = true
zeroize_derive.workspace = true
serde = { workspace = true, optional = true }
sha2.workspace = true

[build-dependencies]
Expand Down
1 change: 1 addition & 0 deletions crates/fhe-math/src/ntt/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use rand_chacha::ChaCha8Rng;
use std::iter::successors;

/// Number-Theoretic Transform operator.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NttOperator {
p: Modulus,
Expand Down
1 change: 1 addition & 0 deletions crates/fhe-math/src/rns/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod scaler;
pub use scaler::{RnsScaler, ScalingFactor};

/// Context for a Residue Number System.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Default, Clone, PartialEq, Eq)]
pub struct RnsContext {
moduli_u64: Vec<u64>,
Expand Down
2 changes: 2 additions & 0 deletions crates/fhe-math/src/rns/scaler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use num_traits::{One, ToPrimitive, Zero};
use std::{cmp::min, sync::Arc};

/// Scaling factor when performing a RNS scaling.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct ScalingFactor {
numerator: BigUint,
Expand Down Expand Up @@ -41,6 +42,7 @@ impl ScalingFactor {

/// Scaler for a RNS context.
/// This is a helper struct to perform RNS scaling.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct RnsScaler {
from: Arc<RnsContext>,
Expand Down
1 change: 1 addition & 0 deletions crates/fhe-math/src/rq/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{fmt::Debug, sync::Arc};
use crate::{ntt::NttOperator, rns::RnsContext, zq::Modulus, Error, Result};

/// Struct that holds the context associated with elements in rq.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Default, Clone, PartialEq, Eq)]
pub struct Context {
pub(crate) moduli: Box<[u64]>,
Expand Down
3 changes: 3 additions & 0 deletions crates/fhe-math/src/rq/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use std::sync::Arc;
use zeroize::{Zeroize, Zeroizing};

/// Possible representations of the underlying polynomial.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub enum Representation {
/// This is the list of coefficients ci, such that the polynomial is c0 + c1
Expand All @@ -39,6 +40,7 @@ pub enum Representation {
}

/// An exponent for a substitution.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, PartialEq, Eq)]
pub struct SubstitutionExponent {
/// The value of the exponent.
Expand Down Expand Up @@ -76,6 +78,7 @@ impl SubstitutionExponent {
}

/// Struct that holds a polynomial for a specific context.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct Poly {
ctx: Arc<Context>,
Expand Down
1 change: 1 addition & 0 deletions crates/fhe-math/src/rq/scaler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use ndarray::{s, Array2, Axis};
use std::sync::Arc;

/// Context extender.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct Scaler {
from: Arc<Context>,
Expand Down
1 change: 1 addition & 0 deletions crates/fhe-math/src/zq/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const fn const_time_cond_select(on_true: u64, on_false: u64, cond: bool) -> u64
/// Structure encapsulating an integer modulus up to 62 bits.
#[derive(Derivative)]
#[derivative(PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone)]
pub struct Modulus {
pub(crate) p: u64,
Expand Down
2 changes: 2 additions & 0 deletions crates/fhe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ rust-version.workspace = true
bench = false # Disable default bench (we use criterion)

[features]
serde = ["dep:serde", "ndarray/serde", "num-bigint/serde", "rand/serde1", "rand_chacha/serde", "fhe-math/serde"]
concrete-ntt = ["fhe-math/concrete-ntt"]
concrete-ntt-nightly = ["fhe-math/concrete-ntt-nightly"]

Expand All @@ -28,6 +29,7 @@ num-traits.workspace = true
prost.workspace = true
rand.workspace = true
rand_chacha.workspace = true
serde = { workspace = true, optional = true }
zeroize.workspace = true
zeroize_derive.workspace = true
ndarray.workspace = true
Expand Down
1 change: 1 addition & 0 deletions crates/fhe/src/bfv/ciphertext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::ops::{Deref, DerefMut};
use std::sync::Arc;

/// A ciphertext encrypting a plaintext.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Ciphertext {
/// The parameters of the underlying BFV encryption scheme.
Expand Down
2 changes: 2 additions & 0 deletions crates/fhe/src/bfv/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::fmt::Display;

use fhe_traits::FhePlaintextEncoding;

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Eq, PartialEq)]
pub(crate) enum EncodingEnum {
Poly,
Expand All @@ -17,6 +18,7 @@ impl Display for EncodingEnum {
}

/// An encoding for the plaintext.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Encoding {
pub(crate) encoding: EncodingEnum,
Expand Down
1 change: 1 addition & 0 deletions crates/fhe/src/bfv/keys/evaluation_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use zeroize::{Zeroize, ZeroizeOnDrop};
/// - row rotation
/// - oblivious expansion
/// - inner sum
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, PartialEq, Eq)]
pub struct EvaluationKey {
par: Arc<BfvParameters>,
Expand Down
1 change: 1 addition & 0 deletions crates/fhe/src/bfv/keys/galois_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use zeroize::Zeroizing;
/// Galois key for the BFV encryption scheme.
/// A Galois key is a special type of key switching key,
/// which switch from `s(x^i)` to `s(x)` where `s(x)` is the secret key.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, PartialEq, Eq)]
pub struct GaloisKey {
pub(crate) element: SubstitutionExponent,
Expand Down
1 change: 1 addition & 0 deletions crates/fhe/src/bfv/keys/key_switching_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::sync::Arc;
use zeroize::Zeroizing;

/// Key switching key for the BFV encryption scheme.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct KeySwitchingKey {
/// The parameters of the underlying BFV encryption scheme.
Expand Down
1 change: 1 addition & 0 deletions crates/fhe/src/bfv/keys/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use zeroize::Zeroizing;
use super::SecretKey;

/// Public key for the BFV encryption scheme.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct PublicKey {
pub(crate) par: Arc<BfvParameters>,
Expand Down
1 change: 1 addition & 0 deletions crates/fhe/src/bfv/keys/relinearization_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use zeroize::Zeroizing;
/// Relinearization key for the BFV encryption scheme.
/// A relinearization key is a special type of key switching key,
/// which switch from `s^2` to `s` where `s` is the secret key.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct RelinearizationKey {
pub(crate) ksk: KeySwitchingKey,
Expand Down
Loading
Loading