-
Notifications
You must be signed in to change notification settings - Fork 25
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
Hybrid shuffle #1387
Open
eriktaubeneck
wants to merge
6
commits into
private-attribution:main
Choose a base branch
from
eriktaubeneck:hybrid-shuffle
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+264
−4
Open
Hybrid shuffle #1387
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cd90468
add shuffle step for hybrid protocol
eriktaubeneck d8f31f8
add test of shuffle conversion
eriktaubeneck 7f61f8e
test shuffle hybrid
eriktaubeneck fba348b
add Serializable and Add traits to IndistinguishableHybridReport
eriktaubeneck 9677ec9
add failing test
eriktaubeneck 3b1587f
fix failing test
eriktaubeneck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
use std::ops::Add; | ||
|
||
use generic_array::{ArrayLength, GenericArray}; | ||
use typenum::{Unsigned, U18}; | ||
|
||
use crate::{ | ||
error::{Error, UnwrapInfallible}, | ||
ff::{ | ||
boolean_array::{BooleanArray, BA64}, | ||
Serializable, | ||
}, | ||
protocol::{context::Context, ipa_prf::shuffle::Shuffle}, | ||
report::hybrid::IndistinguishableHybridReport, | ||
secret_sharing::{replicated::semi_honest::AdditiveShare as Replicated, Sendable, SharedValue}, | ||
}; | ||
|
||
impl<BK, V> Serializable for IndistinguishableHybridReport<BK, V> | ||
where | ||
BK: SharedValue, | ||
V: SharedValue, | ||
Replicated<BK>: Serializable, | ||
Replicated<V>: Serializable, | ||
<Replicated<BK> as Serializable>::Size: Add<U18>, | ||
<Replicated<V> as Serializable>::Size: | ||
Add<<<Replicated<BK> as Serializable>::Size as Add<U18>>::Output>, | ||
<<Replicated<V> as Serializable>::Size as Add< | ||
<<Replicated<BK> as Serializable>::Size as Add<U18>>::Output, | ||
>>::Output: ArrayLength, | ||
{ | ||
type Size = <<Replicated<V> as Serializable>::Size as Add< | ||
<<Replicated<BK> as Serializable>::Size as Add<U18>>::Output, | ||
>>::Output; | ||
|
||
type DeserializationError = Error; | ||
|
||
fn serialize(&self, buf: &mut GenericArray<u8, Self::Size>) { | ||
let mk_sz = <Replicated<BA64> as Serializable>::Size::USIZE; | ||
let bk_sz = <Replicated<BK> as Serializable>::Size::USIZE; | ||
let v_sz = <Replicated<V> as Serializable>::Size::USIZE; | ||
|
||
self.match_key | ||
.serialize(GenericArray::from_mut_slice(&mut buf[..mk_sz])); | ||
|
||
self.breakdown_key | ||
.serialize(GenericArray::from_mut_slice(&mut buf[mk_sz..mk_sz + bk_sz])); | ||
|
||
self.value.serialize(GenericArray::from_mut_slice( | ||
&mut buf[mk_sz + bk_sz..mk_sz + bk_sz + v_sz], | ||
)); | ||
} | ||
|
||
fn deserialize(buf: &GenericArray<u8, Self::Size>) -> Result<Self, Self::DeserializationError> { | ||
let mk_sz = <Replicated<BA64> as Serializable>::Size::USIZE; | ||
let bk_sz = <Replicated<BK> as Serializable>::Size::USIZE; | ||
let v_sz = <Replicated<V> as Serializable>::Size::USIZE; | ||
|
||
let match_key = Replicated::<BA64>::deserialize(GenericArray::from_slice(&buf[..mk_sz])) | ||
.unwrap_infallible(); | ||
let breakdown_key = | ||
Replicated::<BK>::deserialize(GenericArray::from_slice(&buf[mk_sz..mk_sz + bk_sz])) | ||
.map_err(|e| Error::ParseError(e.into()))?; | ||
let value = Replicated::<V>::deserialize(GenericArray::from_slice( | ||
&buf[mk_sz + bk_sz..mk_sz + bk_sz + v_sz], | ||
)) | ||
.map_err(|e| Error::ParseError(e.into()))?; | ||
|
||
Ok(Self { | ||
match_key, | ||
value, | ||
breakdown_key, | ||
}) | ||
} | ||
} | ||
|
||
impl<BK: SharedValue, V: SharedValue> Add for IndistinguishableHybridReport<BK, V> { | ||
type Output = Self; | ||
|
||
fn add(self, rhs: Self) -> Self { | ||
Self { | ||
match_key: self.match_key + rhs.match_key, | ||
value: self.value + rhs.value, | ||
breakdown_key: self.breakdown_key + rhs.breakdown_key, | ||
} | ||
} | ||
} | ||
|
||
impl<BK: SharedValue, V: SharedValue> Sendable for IndistinguishableHybridReport<BK, V> | ||
where | ||
<V as Serializable>::Size: Add, | ||
<BK as Serializable>::Size: Add, | ||
<<V as Serializable>::Size as Add>::Output: ArrayLength, | ||
<<BK as Serializable>::Size as Add>::Output: ArrayLength, | ||
<<BK as Serializable>::Size as Add>::Output: Add<U18>, | ||
<<V as Serializable>::Size as Add>::Output: | ||
Add<<<<BK as Serializable>::Size as Add>::Output as Add<U18>>::Output>, | ||
<<<V as Serializable>::Size as Add>::Output as Add< | ||
<<<BK as Serializable>::Size as Add>::Output as Add<U18>>::Output, | ||
>>::Output: ArrayLength, | ||
{ | ||
} | ||
|
||
/// Shuffles a Vec of IndistinguishableHybridReport | ||
/// # Errors | ||
/// Propogates errors from ctx.shuffle | ||
#[tracing::instrument(name = "shuffle_inputs", skip_all)] | ||
pub async fn shuffle_hybrid_inputs<C, BK, V>( | ||
_ctx: C, | ||
_input: Vec<IndistinguishableHybridReport<BK, V>>, | ||
) -> Result<Vec<IndistinguishableHybridReport<BK, V>>, Error> | ||
where | ||
C: Context + Shuffle, | ||
BK: BooleanArray, | ||
V: BooleanArray, | ||
{ | ||
unimplemented!("shuffle_hybrid_inputs is unimplemented"); | ||
} | ||
|
||
#[cfg(all(test, unit_test))] | ||
pub mod tests { | ||
use generic_array::GenericArray; | ||
use rand::Rng; | ||
|
||
use super::shuffle_hybrid_inputs; | ||
use crate::{ | ||
ff::{ | ||
boolean_array::{BA3, BA64, BA8}, | ||
Serializable, | ||
}, | ||
report::hybrid::IndistinguishableHybridReport, | ||
secret_sharing::replicated::{ | ||
semi_honest::AdditiveShare as Replicated, ReplicatedSecretSharing, | ||
}, | ||
test_executor::run, | ||
test_fixture::{hybrid::TestIndistinguishableHybridReport, Reconstruct, Runner, TestWorld}, | ||
}; | ||
|
||
#[tokio::test] | ||
async fn hybrid_serialize_deserialize() { | ||
let world = TestWorld::default(); | ||
let mut rng = world.rng(); | ||
let report = IndistinguishableHybridReport::<BA8, BA3> { | ||
match_key: Replicated::new(rng.gen(), rng.gen()), | ||
breakdown_key: Replicated::new(rng.gen(), rng.gen()), | ||
value: Replicated::new(rng.gen(), rng.gen()), | ||
}; | ||
|
||
let mut buf = GenericArray::default(); | ||
report.serialize(&mut buf); | ||
let deserialized_report = IndistinguishableHybridReport::<BA8, BA3>::deserialize(&buf); | ||
assert_eq!(report, deserialized_report.unwrap()); | ||
} | ||
|
||
#[test] | ||
fn hybrid_add() { | ||
let report1 = IndistinguishableHybridReport::<BA8, BA3> { | ||
match_key: Replicated::new(BA64::try_from(5).unwrap(), BA64::try_from(3).unwrap()), | ||
value: Replicated::new(BA3::try_from(1).unwrap(), BA3::try_from(2).unwrap()), | ||
breakdown_key: Replicated::new(BA8::try_from(2).unwrap(), BA8::try_from(4).unwrap()), | ||
}; | ||
|
||
assert_eq!( | ||
report1, | ||
report1.clone() + IndistinguishableHybridReport::<BA8, BA3>::ZERO | ||
); | ||
// report 1: (5,3), (1,2), (2,4) | ||
// report 2: (2,2), (0,1), (3,3) | ||
// XOR | ||
// expected: (7,1), (1,3), (1,7) | ||
let report2 = IndistinguishableHybridReport::<BA8, BA3> { | ||
match_key: Replicated::new(BA64::try_from(2).unwrap(), BA64::try_from(2).unwrap()), | ||
value: Replicated::new(BA3::try_from(0).unwrap(), BA3::try_from(1).unwrap()), | ||
breakdown_key: Replicated::new(BA8::try_from(3).unwrap(), BA8::try_from(3).unwrap()), | ||
}; | ||
|
||
let expected = IndistinguishableHybridReport::<BA8, BA3> { | ||
match_key: Replicated::new(BA64::try_from(7).unwrap(), BA64::try_from(1).unwrap()), | ||
value: Replicated::new(BA3::try_from(1).unwrap(), BA3::try_from(3).unwrap()), | ||
breakdown_key: Replicated::new(BA8::try_from(1).unwrap(), BA8::try_from(7).unwrap()), | ||
}; | ||
|
||
assert_eq!(report1 + report2, expected) | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "shuffle_hybrid_inputs is unimplemented")] | ||
fn test_shuffle_hybrid_inputs() { | ||
const BATCHSIZE: usize = 50; | ||
run(|| async { | ||
let world = TestWorld::default(); | ||
let mut rng = world.rng(); | ||
let mut records = Vec::new(); | ||
|
||
for _ in 0..BATCHSIZE { | ||
records.push({ | ||
TestIndistinguishableHybridReport { | ||
match_key: rng.gen::<u64>(), | ||
breakdown_key: rng.gen_range(0u32..1 << 8), | ||
value: rng.gen_range(0u32..1 << 3), | ||
} | ||
}); | ||
} | ||
|
||
let mut result: Vec<TestIndistinguishableHybridReport> = world | ||
.semi_honest(records.clone().into_iter(), |ctx, input_rows| async move { | ||
shuffle_hybrid_inputs::<_, BA8, BA3>(ctx, input_rows) | ||
.await | ||
.unwrap() | ||
}) | ||
.await | ||
.reconstruct(); | ||
assert_ne!(result, records); | ||
records.sort(); | ||
result.sort(); | ||
assert_eq!(result, records); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Can you do something like?