Skip to content

Commit

Permalink
Merge pull request private-attribution#1218 from akoshelev/mac-valida…
Browse files Browse the repository at this point in the history
…te-record

Add `validate_record` API to upgraded contexts
  • Loading branch information
akoshelev authored Aug 12, 2024
2 parents 70620c3 + 322e377 commit 04cf79a
Show file tree
Hide file tree
Showing 11 changed files with 534 additions and 375 deletions.
20 changes: 11 additions & 9 deletions ipa-core/src/protocol/basics/mul/malicious.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ where
mod test {
use crate::{
ff::Fp31,
protocol::{basics::SecureMul, context::Context, RecordId},
protocol::basics::SecureMul,
rand::{thread_rng, Rng},
test_fixture::{Reconstruct, Runner, TestWorld},
};
Expand All @@ -143,14 +143,16 @@ mod test {
let a = rng.gen::<Fp31>();
let b = rng.gen::<Fp31>();

let res = world
.upgraded_malicious((a, b), |ctx, (a, b)| async move {
a.multiply(&b, ctx.set_total_records(1), RecordId::from(0))
.await
.unwrap()
})
.await;
let res =
world
.upgraded_malicious(
vec![(a, b)].into_iter(),
|ctx, record_id, (a, b)| async move {
a.multiply(&b, ctx, record_id).await.unwrap()
},
)
.await;

assert_eq!(a * b, res.reconstruct());
assert_eq!(a * b, res.reconstruct()[0]);
}
}
26 changes: 14 additions & 12 deletions ipa-core/src/protocol/basics/reshare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ mod tests {
helpers::{in_memory_config::MaliciousHelper, Role},
protocol::{
basics::Reshare,
context::{upgrade::Upgradable, Context, UpgradableContext, Validator},
context::{
upgrade::Upgradable, Context, UpgradableContext, UpgradedContext, Validator,
},
RecordId,
},
rand::{thread_rng, Rng},
Expand All @@ -229,15 +231,15 @@ mod tests {
for &role in Role::all() {
let secret = thread_rng().gen::<Fp32BitPrime>();
let new_shares = world
.upgraded_malicious(secret, |ctx, share| async move {
share
.reshare(ctx.set_total_records(1), RecordId::from(0), role)
.await
.unwrap()
})
.upgraded_malicious(
vec![secret].into_iter(),
|ctx, record_id, share| async move {
share.reshare(ctx, record_id, role).await.unwrap()
},
)
.await;

assert_eq!(secret, new_shares.reconstruct());
assert_eq!(secret, new_shares.reconstruct()[0]);
}
}

Expand Down Expand Up @@ -299,16 +301,16 @@ mod tests {

world
.malicious(a, |ctx, a| async move {
let v = ctx.validator();
let m_ctx = v.context().set_total_records(1);
let v = ctx.set_total_records(1).validator();
let m_ctx = v.context();
let m_a = a.upgrade(m_ctx.clone(), RecordId::FIRST).await.unwrap();

let m_reshared_a = m_a
let _ = m_a
.reshare(m_ctx.narrow(STEP), RecordId::FIRST, to_helper)
.await
.unwrap();

match v.validate(m_reshared_a).await {
match m_ctx.validate_record(RecordId::FIRST).await {
Ok(result) => panic!("Got a result {result:?}"),
Err(err) => {
assert!(matches!(err, Error::MaliciousSecurityCheckFailed));
Expand Down
40 changes: 30 additions & 10 deletions ipa-core/src/protocol/basics/reveal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,10 @@ mod tests {
},
protocol::{
basics::{partial_reveal, reveal, Reveal},
context::{upgrade::Upgradable, Context, UpgradableContext, Validator},
context::{
upgrade::Upgradable, validator::BatchValidator, Context, UpgradableContext,
Validator,
},
RecordId,
},
rand::{thread_rng, Rng},
Expand Down Expand Up @@ -501,9 +504,12 @@ mod tests {

let mut rng = thread_rng();
let world = TestWorld::default();
let sh_ctx = world.malicious_contexts();
let sh_ctx = world
.malicious_contexts()
.each_ref()
.map(|c| c.set_total_records(1));
let v = sh_ctx.map(UpgradableContext::validator);
let m_ctx = v.each_ref().map(|v| v.context().set_total_records(1));
let m_ctx = v.each_ref().map(BatchValidator::context);

let record_id = RecordId::from(0);
let input: TestField = rng.gen();
Expand Down Expand Up @@ -537,9 +543,12 @@ mod tests {
let world = TestWorld::default();

for &excluded in Role::all() {
let sh_ctx = world.malicious_contexts();
let sh_ctx = world
.malicious_contexts()
.each_ref()
.map(|c| c.set_total_records(1));
let v = sh_ctx.map(UpgradableContext::validator);
let m_ctx = v.each_ref().map(|v| v.context().set_total_records(1));
let m_ctx = v.each_ref().map(BatchValidator::context);

let record_id = RecordId::from(0);
let input: TestField = rng.gen();
Expand Down Expand Up @@ -579,7 +588,6 @@ mod tests {
F: Field,
S: SecretSharing<F> + Reveal<C, Output = <F as Vectorizable<1>>::Array>,
{
let ctx = ctx.set_total_records(1);
let my_role = ctx.role();
let ctx = ctx.narrow(MALICIOUS_REVEAL_STEP);

Expand Down Expand Up @@ -620,7 +628,10 @@ mod tests {
let world = TestWorld::new_with(config);
let input: Fp31 = rng.gen();
world
.upgraded_malicious(input, |ctx, share| do_malicious_reveal(ctx, partial, share))
.upgraded_malicious(
vec![input].into_iter(),
|ctx, _record_id: RecordId, share| do_malicious_reveal(ctx, partial, share),
)
.await;
});
}
Expand All @@ -637,7 +648,10 @@ mod tests {
let world = TestWorld::new_with(config);
let input: Fp31 = rng.gen();
world
.upgraded_malicious(input, |ctx, share| do_malicious_reveal(ctx, partial, share))
.upgraded_malicious(
vec![input].into_iter(),
|ctx, _record_id: RecordId, share| do_malicious_reveal(ctx, partial, share),
)
.await;
});
}
Expand All @@ -653,8 +667,12 @@ mod tests {

let world = TestWorld::new_with(config);
let input: Boolean = rng.gen();
// ZKP malicious does not set the total records as `upgraded_malicious`
// something to think about how to bring them closer together.
world
.dzkp_malicious(input, |ctx, share| do_malicious_reveal(ctx, partial, share))
.dzkp_malicious(input, |ctx, share| {
do_malicious_reveal(ctx.set_total_records(1), partial, share)
})
.await;
});
}
Expand All @@ -671,7 +689,9 @@ mod tests {
let world = TestWorld::new_with(config);
let input: Boolean = rng.gen();
world
.dzkp_malicious(input, |ctx, share| do_malicious_reveal(ctx, partial, share))
.dzkp_malicious(input, |ctx, share| {
do_malicious_reveal(ctx.set_total_records(1), partial, share)
})
.await;
});
}
Expand Down
42 changes: 8 additions & 34 deletions ipa-core/src/protocol/basics/share_known_value.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use crate::{
helpers::Role,
protocol::context::{Context, UpgradedMaliciousContext},
protocol::context::Context,
secret_sharing::{
replicated::{
malicious::{AdditiveShare as MaliciousReplicated, ExtendableField},
semi_honest::AdditiveShare as Replicated,
ReplicatedSecretSharing,
},
replicated::{semi_honest::AdditiveShare as Replicated, ReplicatedSecretSharing},
SharedValue,
},
};
Expand All @@ -15,6 +11,11 @@ use crate::{
///
/// The context is only used to determine the helper role. It is not used for communication or PRSS,
/// and it is not necessary to use a uniquely narrowed context.
///
/// As of Aug 2024, this interface does not work for MAC malicious sharings as they
/// were defined before. Sharing known value requires `r` and it varies from one
/// record id to another. If we need to update this, [`Self::share_known_value`] needs
/// to have record id parameter.
pub trait ShareKnownValue<C: Context, V: SharedValue> {
fn share_known_value(ctx: &C, value: V) -> Self;
}
Expand All @@ -29,25 +30,14 @@ impl<C: Context, V: SharedValue> ShareKnownValue<C, V> for Replicated<V> {
}
}

impl<'a, F: ExtendableField> ShareKnownValue<UpgradedMaliciousContext<'a, F>, F>
for MaliciousReplicated<F>
{
fn share_known_value(ctx: &UpgradedMaliciousContext<'a, F>, value: F) -> Self {
ctx.share_known_value(value)
}
}

#[cfg(all(test, unit_test))]
mod tests {
use rand::Rng;

use super::ShareKnownValue;
use crate::{
ff::Fp31,
secret_sharing::replicated::{
malicious::AdditiveShare as MaliciousReplicated,
semi_honest::AdditiveShare as Replicated,
},
secret_sharing::replicated::semi_honest::AdditiveShare as Replicated,
test_fixture::{Reconstruct, Runner, TestWorld},
};

Expand All @@ -66,20 +56,4 @@ mod tests {
.reconstruct();
assert_eq!(result, a);
}

#[tokio::test]
pub async fn malicious_share_known_values() {
let world = TestWorld::default();

let mut rng = rand::thread_rng();
let a = rng.gen::<Fp31>();

let result = world
.upgraded_malicious((), |ctx, ()| async move {
MaliciousReplicated::<Fp31>::share_known_value(&ctx, a)
})
.await
.reconstruct();
assert_eq!(result, a);
}
}
18 changes: 7 additions & 11 deletions ipa-core/src/protocol/boolean/or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,16 @@ mod tests {
.await
.reconstruct();
let m_result = world
.upgraded_malicious((a, b), |ctx, (a_share, b_share)| async move {
or(
ctx.set_total_records(1),
RecordId::from(0_u32),
&a_share,
&b_share,
)
.await
.unwrap()
})
.upgraded_malicious(
vec![(a, b)].into_iter(),
|ctx, record_id, (a_share, b_share)| async move {
or(ctx, record_id, &a_share, &b_share).await.unwrap()
},
)
.await
.reconstruct();

assert_eq!(result, m_result);
assert_eq!(result, m_result[0]);
result
}

Expand Down
Loading

0 comments on commit 04cf79a

Please sign in to comment.