Skip to content

Commit

Permalink
Merge pull request private-attribution#674 from bmcase/origin/malicio…
Browse files Browse the repository at this point in the history
…us_network_test

malicious network tests
  • Loading branch information
akoshelev authored Jun 7, 2023
2 parents ff78d76 + c82bf0d commit 43e9fc8
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 41 deletions.
45 changes: 37 additions & 8 deletions src/bin/test_mpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use generic_array::ArrayLength;
use hyper::http::uri::Scheme;
use ipa::{
cli::{
playbook::{secure_mul, semi_honest, InputSource},
playbook::{playbook_ipa, secure_mul, InputSource},
CsvSerializer, Verbosity,
},
config::{ClientConfig, NetworkConfig, PeerConfig},
Expand All @@ -14,7 +14,7 @@ use ipa::{
protocol::{BreakdownKey, MatchKey, QueryId},
secret_sharing::{replicated::semi_honest::AdditiveShare, IntoShares},
test_fixture::{
ipa::{ipa_in_the_clear, TestRawDataRecord},
ipa::{ipa_in_the_clear, IpaSecurityModel, TestRawDataRecord},
EventGenerator, EventGeneratorConfig,
},
};
Expand Down Expand Up @@ -87,8 +87,10 @@ impl From<&CommandInput> for InputSource {
enum TestAction {
/// Execute end-to-end multiplication.
Multiply,
/// Execute IPA in semi-honest majority setting
/// Execute IPA in semi-honest honest majority setting
SemiHonestIpa(IpaQueryConfig),
/// Execute IPA in malicious honest majority setting
MaliciousIpa(IpaQueryConfig),
/// Generate inputs for IPA
GenIpaInputs {
/// Number of records to generate
Expand Down Expand Up @@ -207,7 +209,24 @@ async fn main() -> Result<(), Box<dyn Error>> {
match args.action {
TestAction::Multiply => multiply(&args, &make_clients().await).await,
TestAction::SemiHonestIpa(config) => {
semi_honest_ipa(&args, &config, &make_clients().await).await
// semi_honest_ipa(&args, &config, &make_clients().await).await
ipa(
&args,
IpaSecurityModel::SemiHonest,
&config,
&make_clients().await,
)
.await
}
TestAction::MaliciousIpa(config) => {
// malicious_ipa(&args, &config, &make_clients().await).await
ipa(
&args,
IpaSecurityModel::Malicious,
&config,
&make_clients().await,
)
.await
}
TestAction::GenIpaInputs {
count,
Expand Down Expand Up @@ -239,13 +258,23 @@ fn gen_inputs(
Ok(())
}

async fn semi_honest_ipa(
async fn ipa(
args: &Args,
security_model: IpaSecurityModel,
ipa_query_config: &IpaQueryConfig,
helper_clients: &[MpcHelperClient; 3],
) {
let input = InputSource::from(&args.input);
let query_type = QueryType::Ipa(ipa_query_config.clone());
let query_type: QueryType;
match security_model {
IpaSecurityModel::SemiHonest => {
query_type = QueryType::SemiHonestIpa(ipa_query_config.clone());
}
IpaSecurityModel::Malicious => {
query_type = QueryType::MaliciousIpa(ipa_query_config.clone())
}
};

let query_config = QueryConfig {
field_type: args.input.field,
query_type,
Expand All @@ -270,11 +299,11 @@ async fn semi_honest_ipa(

let actual = match args.input.field {
FieldType::Fp31 => {
semi_honest::<Fp31, MatchKey, BreakdownKey>(&input_rows, &helper_clients, query_id)
playbook_ipa::<Fp31, MatchKey, BreakdownKey>(&input_rows, &helper_clients, query_id)
.await
}
FieldType::Fp32BitPrime => {
semi_honest::<Fp32BitPrime, MatchKey, BreakdownKey>(
playbook_ipa::<Fp32BitPrime, MatchKey, BreakdownKey>(
&input_rows,
&helper_clients,
query_id,
Expand Down
2 changes: 1 addition & 1 deletion src/cli/playbook/ipa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use typenum::Unsigned;
/// Semi-honest IPA protocol.
/// Returns aggregated values per breakdown key represented as index in the returned vector
#[allow(clippy::missing_panics_doc)]
pub async fn semi_honest<F, MK, BK>(
pub async fn playbook_ipa<F, MK, BK>(
records: &[TestRawDataRecord],
clients: &[MpcHelperClient; 3],
query_id: QueryId,
Expand Down
2 changes: 1 addition & 1 deletion src/cli/playbook/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ mod input;
mod ipa;
mod multiply;

pub use self::ipa::semi_honest;
pub use self::ipa::playbook_ipa;
pub use input::InputSource;
pub use multiply::secure_mul;
17 changes: 7 additions & 10 deletions src/helpers/transport/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Default for QueryConfig {
#[cfg(any(test, feature = "test-fixture", feature = "cli"))]
query_type: QueryType::TestMultiply,
#[cfg(not(any(test, feature = "test-fixture", feature = "cli")))]
query_type: QueryType::Ipa(IpaQueryConfig::default()),
query_type: QueryType::SemiHonestIpa(IpaQueryConfig::default()),
}
}
}
Expand Down Expand Up @@ -160,12 +160,14 @@ impl QueryCommand {
pub enum QueryType {
#[cfg(any(test, feature = "test-fixture", feature = "cli"))]
TestMultiply,
Ipa(IpaQueryConfig),
SemiHonestIpa(IpaQueryConfig),
MaliciousIpa(IpaQueryConfig),
}

impl QueryType {
pub const TEST_MULTIPLY_STR: &'static str = "test-multiply";
pub const IPA_STR: &'static str = "ipa";
pub const SEMIHONEST_IPA_STR: &'static str = "semihonest-ipa";
pub const MALICIOUS_IPA_STR: &'static str = "malicious-ipa";
}

/// TODO: should this `AsRef` impl (used for `Substep`) take into account config of IPA?
Expand All @@ -174,7 +176,8 @@ impl AsRef<str> for QueryType {
match self {
#[cfg(any(test, feature = "cli", feature = "test-fixture"))]
QueryType::TestMultiply => Self::TEST_MULTIPLY_STR,
QueryType::Ipa(_) => Self::IPA_STR,
QueryType::SemiHonestIpa(_) => Self::SEMIHONEST_IPA_STR,
QueryType::MaliciousIpa(_) => Self::MALICIOUS_IPA_STR,
}
}
}
Expand Down Expand Up @@ -245,9 +248,3 @@ impl IpaQueryConfig {
}
}
}

impl From<IpaQueryConfig> for QueryType {
fn from(value: IpaQueryConfig) -> Self {
QueryType::Ipa(value)
}
}
34 changes: 24 additions & 10 deletions src/net/http_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub mod query {
let query_type = match query_type.as_str() {
#[cfg(any(test, feature = "cli", feature = "test-fixture"))]
QueryType::TEST_MULTIPLY_STR => Ok(QueryType::TestMultiply),
QueryType::IPA_STR => {
QueryType::SEMIHONEST_IPA_STR | QueryType::MALICIOUS_IPA_STR => {
#[derive(serde::Deserialize)]
struct IPAQueryConfigParam {
per_user_credit_cap: u32,
Expand All @@ -135,13 +135,27 @@ pub mod query {
num_multi_bits,
}) = req.extract().await?;

Ok(QueryType::Ipa(IpaQueryConfig {
per_user_credit_cap,
max_breakdown_key,
attribution_window_seconds,
num_multi_bits,
}))
match query_type.as_str() {
QueryType::SEMIHONEST_IPA_STR => {
Ok(QueryType::SemiHonestIpa(IpaQueryConfig {
per_user_credit_cap,
max_breakdown_key,
attribution_window_seconds,
num_multi_bits,
}))
}
QueryType::MALICIOUS_IPA_STR => {
Ok(QueryType::MaliciousIpa(IpaQueryConfig {
per_user_credit_cap,
max_breakdown_key,
attribution_window_seconds,
num_multi_bits,
}))
}
&_ => unreachable!(),
}
}

other => Err(Error::bad_query_value("query_type", other)),
}?;
Ok(QueryConfigQueryParams(QueryConfig {
Expand All @@ -157,14 +171,14 @@ pub mod query {
match self.query_type {
#[cfg(any(test, feature = "test-fixture", feature = "cli"))]
QueryType::TestMultiply => write!(f, "query_type={}", QueryType::TEST_MULTIPLY_STR),
QueryType::Ipa(config) => {
qt @ (QueryType::SemiHonestIpa(config) | QueryType::MaliciousIpa(config)) => {
write!(
f,
"query_type={}&per_user_credit_cap={}&max_breakdown_key={}&num_multi_bits={}",
QueryType::IPA_STR,
"query_type={qt}&per_user_credit_cap={}&max_breakdown_key={}&num_multi_bits={}",
config.per_user_credit_cap,
config.max_breakdown_key,
config.num_multi_bits,
qt=qt.as_ref(),
)?;

if let Some(window) = config.attribution_window_seconds {
Expand Down
6 changes: 3 additions & 3 deletions src/net/server/handlers/query/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ mod tests {
async fn create_test_ipa_no_attr_window() {
create_test(QueryConfig {
field_type: FieldType::Fp32BitPrime,
query_type: QueryType::Ipa(IpaQueryConfig {
query_type: QueryType::SemiHonestIpa(IpaQueryConfig {
per_user_credit_cap: 1,
max_breakdown_key: 1,
attribution_window_seconds: None,
Expand All @@ -105,7 +105,7 @@ mod tests {
async fn create_test_ipa_with_attr_window() {
create_test(QueryConfig {
field_type: FieldType::Fp32BitPrime,
query_type: QueryType::Ipa(IpaQueryConfig {
query_type: QueryType::SemiHonestIpa(IpaQueryConfig {
per_user_credit_cap: 1,
max_breakdown_key: 1,
attribution_window_seconds: NonZeroU32::new(86_400),
Expand Down Expand Up @@ -207,7 +207,7 @@ mod tests {
fn default() -> Self {
Self {
field_type: format!("{:?}", FieldType::Fp32BitPrime),
query_type: QueryType::IPA_STR.to_string(),
query_type: QueryType::SEMIHONEST_IPA_STR.to_string(),
per_user_credit_cap: "1".into(),
max_breakdown_key: "1".into(),
attribution_window_seconds: "0".into(),
Expand Down
24 changes: 19 additions & 5 deletions src/query/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
},
protocol::{
attribution::input::MCAggregateCreditOutputRow,
context::SemiHonestContext,
context::{MaliciousContext, SemiHonestContext},
step::{self, StepNarrow},
},
query::runner::IpaRunner,
Expand Down Expand Up @@ -75,20 +75,34 @@ pub fn start_query(
// Negotiate PRSS first
let step = step::Descriptive::default().narrow(&config.query_type);
let prss = negotiate_prss(&gateway, &step, &mut rng).await.unwrap();
let ctx = SemiHonestContext::new(&prss, &gateway);

match config.query_type {
#[cfg(any(test, feature = "cli", feature = "test-fixture"))]
QueryType::TestMultiply => {
super::runner::TestMultiplyRunner
.run(ctx, config.field_type, input)
.run(
SemiHonestContext::new(&prss, &gateway),
config.field_type,
input,
)
.await
}
QueryType::Ipa(ipa_query_config) => {
QueryType::SemiHonestIpa(ipa_query_config) => {
IpaRunner(ipa_query_config)
.run(ctx, config.field_type, input)
.run(
SemiHonestContext::new(&prss, &gateway),
config.field_type,
input,
)
.await
}
QueryType::MaliciousIpa(ipa_query_config) => Ok(IpaRunner(ipa_query_config)
.malicious_run(
MaliciousContext::new(&prss, &gateway),
config.field_type,
input,
)
.await),
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/query/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ mod tests {
records,
QueryConfig {
field_type: FieldType::Fp31,
query_type: QueryType::Ipa(IpaQueryConfig {
query_type: QueryType::SemiHonestIpa(IpaQueryConfig {
per_user_credit_cap: 3,
max_breakdown_key: 3,
attribution_window_seconds: None,
Expand Down
Loading

0 comments on commit 43e9fc8

Please sign in to comment.