Skip to content

Commit

Permalink
Serde is now a non-optional dependency (#996)
Browse files Browse the repository at this point in the history
* Serde is now a non-optional dependency

Removed the "enable-serde" flag and made Serde a non-optional dependency.
Removed the cfg attributes that depended on the feature since now the
library is included by default.

* Fixed formatting

* Fixed build issues

* Removed unused structures to help with coverage

* fixing format
  • Loading branch information
cberkhoff authored Apr 4, 2024
1 parent fc791b2 commit b0b700a
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 231 deletions.
10 changes: 4 additions & 6 deletions ipa-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ default = [
"ipa-prf",
]
cli = ["comfy-table", "clap"]
enable-serde = ["serde", "serde_json"]
disable-metrics = []
# TODO move web-app to a separate crate. It adds a lot of build time to people who mostly write protocols
# TODO Consider moving out benches as well
Expand All @@ -28,7 +27,6 @@ web-app = [
"base64",
"clap",
"comfy-table",
"enable-serde",
"hyper",
"hyper-rustls",
"rcgen",
Expand All @@ -40,7 +38,7 @@ web-app = [
"tower",
"tower-http",
]
test-fixture = ["enable-serde", "weak-field"]
test-fixture = ["weak-field"]
# Include observability instruments that detect lack of progress inside MPC. If there is a bug that leads to helper
# miscommunication, this feature helps to detect it. Turning it on has some cost.
# If "shuttle" feature is enabled, turning this on has no effect.
Expand Down Expand Up @@ -134,8 +132,8 @@ rustls-pemfile = { version = "1", optional = true }
# we can remove pinning
rustls-webpki = "^0.101.4"
# TODO consider using zerocopy or serde_bytes or in-house serialization
serde = { version = "1.0", optional = true, features = ["derive"] }
serde_json = { version = "1.0", optional = true }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0" }
sha2 = "0.10"
shuttle-crate = { package = "shuttle", version = "0.6.1", optional = true }
thiserror = "1.0"
Expand Down Expand Up @@ -179,7 +177,7 @@ bench = false
[[bin]]
name = "ipa_bench"
path = "src/bin/ipa_bench/ipa_bench.rs"
required-features = ["cli", "enable-serde"]
required-features = ["cli"]
bench = false

[[bin]]
Expand Down
2 changes: 0 additions & 2 deletions ipa-core/src/bin/ipa_bench/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ use std::ops::Range;

use serde::{Deserialize, Serialize};

#[cfg(feature = "enable-serde")]
#[derive(Serialize, Deserialize, Debug)]
pub struct WeightedIndex<T> {
pub index: T,
pub weight: f64,
}

#[cfg(feature = "enable-serde")]
#[derive(Serialize, Deserialize, Debug)]
pub struct Config {
pub devices_per_user: Vec<WeightedIndex<u8>>,
Expand Down
126 changes: 9 additions & 117 deletions ipa-core/src/bin/ipa_bench/models.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use std::{
fmt::{Debug, Formatter},
io::{Error as IoError, ErrorKind as IoErrorKind},
ops::Range,
};

use rand::{CryptoRng, Rng, RngCore};
use serde::{Deserialize, Serialize};

// Type aliases to indicate whether the parameter should be encrypted, secret shared, etc.
// Underlying types are temporalily assigned for PoC.
pub type CipherText = Vec<u8>;
type PlainText = String;
pub type MatchKey = u64;
pub type Number = u32;
Expand All @@ -20,107 +17,7 @@ pub type Epoch = u8;
/// An offset in seconds into a given epoch. Using an 32-bit value > 20-bit > 604,800 seconds.
pub type Offset = u32;

#[derive(Debug, Clone)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub struct SecretShare {
ss: [CipherText; 3],
}

impl SecretShare {
fn combine(&self) -> Vec<u8> {
let mut result = Vec::new();

assert!(self.ss[0].len() == self.ss[1].len());
assert!(self.ss[0].len() == self.ss[2].len());

for i in 0..self.ss[0].len() {
result.push(self.ss[0][i] ^ self.ss[1][i] ^ self.ss[2][i]);
}

result
}

// TODO: Add Shamir's SS

fn xor<R: RngCore + CryptoRng>(data: &[u8], rng: &mut R) -> Self {
let mut ss = [Vec::new(), Vec::new(), Vec::new()];

for x in data {
let ss1 = rng.gen::<u8>();
let ss2 = rng.gen::<u8>();
let ss3 = ss1 ^ ss2 ^ x;

ss[0].push(ss1);
ss[1].push(ss2);
ss[2].push(ss3);
}

SecretShare { ss }
}
}

pub trait SecretSharable {
/// Splits the number into secret shares
fn xor_split<R: RngCore + CryptoRng>(&self, rng: &mut R) -> SecretShare;

/// Combines the given secret shares back to [Self]
/// # Errors
/// if the combined data overflows [Self]
fn combine(data: &SecretShare) -> Result<Self, IoError>
where
Self: Sized;
}

impl SecretSharable for u32 {
fn xor_split<R: RngCore + CryptoRng>(&self, rng: &mut R) -> SecretShare {
SecretShare::xor(&self.to_be_bytes(), rng)
}

fn combine(data: &SecretShare) -> Result<Self, IoError> {
let ss = data.combine();

let mut high = ss[0..ss.len() - 4].to_vec();
high.retain(|x| *x != 0);

if ss.len() > 4 && !high.is_empty() {
return Err(IoError::from(IoErrorKind::InvalidData));
}

let mut bytes = [0u8; 4];
for (i, v) in ss[ss.len() - 4..].iter().enumerate() {
bytes[i] = *v;
}

Ok(u32::from_be_bytes(bytes))
}
}

impl SecretSharable for u64 {
fn xor_split<R: RngCore + CryptoRng>(&self, rng: &mut R) -> SecretShare {
SecretShare::xor(&self.to_be_bytes(), rng)
}

fn combine(data: &SecretShare) -> Result<Self, IoError> {
let ss = data.combine();

let mut high = ss[0..ss.len() - 8].to_vec();
high.retain(|x| *x != 0);

if ss.len() > 8 && !high.is_empty() {
return Err(IoError::from(IoErrorKind::InvalidData));
}

let mut bytes = [0u8; 8];
for (i, v) in ss[ss.len() - 8..].iter().enumerate() {
bytes[i] = *v;
}

Ok(u64::from_be_bytes(bytes))
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
/// A timestamp of a source/trigger report represented by epoch and offset.
///
/// Internally, the time is stored in `u32`, but the value is capped at `(Epoch::MAX + 1) * SECONDS_IN_EPOCH - 1`.
Expand Down Expand Up @@ -206,8 +103,7 @@ impl From<EventTimestamp> for u32 {
}
}

#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct Event {
// An identifier, set in the user agent, which identifies an individual person. This must never be released (beyond
/// the match key provider) to any party in unencrypted form. For the purpose of this tool, however, the value is in
Expand All @@ -222,8 +118,7 @@ pub struct Event {
pub timestamp: EventTimestamp,
}

#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum GenericReport {
/// An event produced on websites/apps when a user interacts with an ad (i.e. impression, click).
Source {
Expand All @@ -242,22 +137,20 @@ pub enum GenericReport {
},
}

// TODO(taiki): Implement Serialize/Deserialize

#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[derive(Serialize, Deserialize)]
enum QueryType {
SourceFanout,
TriggerFanout,
}

#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[derive(Serialize, Deserialize)]
enum Node {
Helper1,
Helper2,
Helper3,
}

#[cfg_attr(feature = "enable-serde", derive(Serialize))]
#[derive(Serialize)]
struct IPAQuery {
/// Caller authentication token.
auth_token: PlainText,
Expand All @@ -278,7 +171,7 @@ struct IPAQuery {
reports: Vec<GenericReport>,
}

#[cfg_attr(feature = "enable-serde", derive(Serialize))]
#[derive(Serialize)]
struct SourceFanoutQuery {
query: IPAQuery,

Expand All @@ -299,7 +192,7 @@ impl Debug for SourceFanoutQuery {
}
}

#[cfg_attr(feature = "enable-serde", derive(Serialize))]
#[derive(Serialize)]
struct TriggerFanoutQuery {
query: IPAQuery,

Expand All @@ -319,8 +212,7 @@ impl Debug for TriggerFanoutQuery {

#[cfg(all(test, unit_test))]
mod tests {
use super::EventTimestamp;
use crate::models::Epoch;
use super::{Epoch, EventTimestamp};

#[test]
fn event_timestamp_new() {
Expand Down
5 changes: 3 additions & 2 deletions ipa-core/src/cli/ipa_output.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::time::Duration;

use serde::{Deserialize, Serialize};

use crate::helpers::query::{IpaQueryConfig, QuerySize};

#[derive(Debug)]
#[cfg_attr(feature = "enable-serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Serialize, Deserialize)]
pub struct QueryResult {
pub input_size: QuerySize,
pub config: IpaQueryConfig,
Expand Down
1 change: 0 additions & 1 deletion ipa-core/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub mod playbook;
#[cfg(feature = "web-app")]
mod test_setup;
mod verbosity;

#[cfg(feature = "web-app")]
pub use clientconf::{setup as client_config_setup, ConfGenArgs};
pub use csv::Serializer as CsvSerializer;
Expand Down
9 changes: 4 additions & 5 deletions ipa-core/src/cli/noise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
use clap::Args;
use rand::rngs::StdRng;
use rand_core::SeedableRng;
use serde::{Deserialize, Serialize, Serializer};

use crate::protocol::dp::InsecureDiscreteDp;

Expand All @@ -30,8 +31,7 @@ pub struct ApplyDpArgs {
cap: u32,
}

#[derive(Debug)]
#[cfg_attr(feature = "enable-serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Serialize, Deserialize)]
pub struct NoisyOutput {
/// Aggregated breakdowns with noise applied. It is important to use unsigned values here
/// to avoid bias/mean skew
Expand All @@ -45,11 +45,10 @@ pub struct NoisyOutput {
#[derive(Debug, Copy, Clone)]
pub struct EpsilonBits(f64);

#[cfg(feature = "enable-serde")]
impl serde::Serialize for EpsilonBits {
impl Serialize for EpsilonBits {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
S: Serializer,
{
serializer.serialize_str(&self.0.to_string())
}
Expand Down
1 change: 0 additions & 1 deletion ipa-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ pub enum Error {
#[error("runtime error")]
RuntimeError(JoinError),
#[error("failed to parse json: {0}")]
#[cfg(feature = "enable-serde")]
Serde(#[from] serde_json::Error),
#[error("MPC Infrastructure error: {0}")]
MpcInfraError(#[from] crate::helpers::Error<Role>),
Expand Down
4 changes: 2 additions & 2 deletions ipa-core/src/ff/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
ops::{Mul, MulAssign},
};

use serde::{Deserialize, Serialize};
use typenum::{U1, U4};

use crate::{
Expand Down Expand Up @@ -37,8 +38,7 @@ pub trait Field:
const ONE: Self;
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "enable-serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
pub enum FieldType {
#[cfg(any(test, feature = "weak-field"))]
Expand Down
Loading

0 comments on commit b0b700a

Please sign in to comment.