-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.rs
More file actions
213 lines (199 loc) · 7.65 KB
/
client.rs
File metadata and controls
213 lines (199 loc) · 7.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
use crate::report::report_vector::ReportVector;
use crate::schema::Schema;
use rand::Rng;
use std::result::Result;
pub fn create_report_shares<const REPORT_U32_SIZE: usize>(
rng: &mut (impl Rng + ?Sized),
input_json: &str,
) -> Result<
(
Schema,
ReportVector<REPORT_U32_SIZE>, // Original report vector.
ReportVector<REPORT_U32_SIZE>, // First half of shares.
ReportVector<REPORT_U32_SIZE>, // Second half of shares.
),
String,
> {
// Load the `ReportVector`. The client should never load dummy values in its data.
let (report_vector, schema) = ReportVector::from_json(input_json, false)?;
// Share the `ReportVector` and return the shares.
let mut report_vector_share1 = report_vector.clone();
let report_vector_share2 = report_vector_share1.share(rng);
Ok((
schema,
report_vector,
report_vector_share1,
report_vector_share2,
))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::report::plain_report::{PlainReport, PlainReportVector};
use crate::schema::*;
use rand::rngs::OsRng;
#[test]
fn test_categorical_serialize_deserialize_json() {
let schema = Schema(vec![
("attr1".to_string(), AttributeType::C2),
("attr2".to_string(), AttributeType::C3),
("attr3".to_string(), AttributeType::C4),
("attr4".to_string(), AttributeType::C15),
]);
let reports = vec![
PlainReport {
attributes: vec![
Attribute::C2(0b10),
Attribute::C3(0b011),
Attribute::C4(0b0101),
Attribute::C15(0b110_1101_1011_0110),
],
},
PlainReport {
attributes: vec![
Attribute::C2(0b00),
Attribute::C3(0b010),
Attribute::C4(0b1101),
Attribute::C15(0b000_1111_0011_0101),
],
},
PlainReport {
attributes: vec![
Attribute::C2(0b1),
Attribute::C3(0b100),
Attribute::C4(0b0101),
Attribute::C15(0b010_0011_0000_0010),
],
},
];
let raw_report_vector = PlainReportVector { schema, reports };
let json = serde_json::to_string(&raw_report_vector).unwrap();
let raw_report_vector2: PlainReportVector = serde_json::from_str(&json).unwrap();
assert_eq!(raw_report_vector, raw_report_vector2);
let json = r#"
{
"schema":[["attr1","c2"],["attr2","c3"],["attr3","c4"],["attr4","c15"]],
"reports":[
{"attributes":[{"c2":2},{"c3":3},{"c4":5},{"c15":28086}]},
{"attributes":[{"c2":0},{"c3":2},{"c4":13},{"c15":3893}]},
{"attributes":[{"c2":1},{"c3":4},{"c4":5},{"c15":8962}]}
]
}"#;
let raw_report_vector: PlainReportVector = serde_json::from_str(&json).unwrap();
assert_eq!(raw_report_vector, raw_report_vector2);
}
#[test]
fn test_mixed_serialize_deserialize_json() {
let schema = Schema(vec![
("attr1".to_string(), AttributeType::C2),
("attr2".to_string(), AttributeType::N3(6)),
("attr3".to_string(), AttributeType::C4),
("attr4".to_string(), AttributeType::N15(32767)),
]);
let reports = vec![
PlainReport {
attributes: vec![
Attribute::C2(0b10),
Attribute::N3(5, 6),
Attribute::C4(0b0101),
Attribute::N15(32766, 32767),
],
},
PlainReport {
attributes: vec![
Attribute::C2(0b00),
Attribute::N3(0, 6),
Attribute::C4(0b1101),
Attribute::N15(0, 32767),
],
},
PlainReport {
attributes: vec![
Attribute::C2(0b1),
Attribute::N3(3, 6),
Attribute::C4(0b0101),
Attribute::N15(16384, 32767),
],
},
];
let raw_report_vector = PlainReportVector { schema, reports };
let json = serde_json::to_string(&raw_report_vector).unwrap();
let raw_report_vector2: PlainReportVector = serde_json::from_str(&json).unwrap();
assert_eq!(raw_report_vector, raw_report_vector2);
let json = r#"
{
"schema":[["attr1","c2"],["attr2",{"n3":6}],["attr3","c4"],["attr4",{"n15":32767}]],
"reports":[
{"attributes":[{"c2":2},{"n3":[5,6]},{"c4":5},{"n15": [32766,32767]}]},
{"attributes":[{"c2":0},{"n3":[0,6]},{"c4":13},{"n15":[0,32767]}]},
{"attributes":[{"c2":1},{"n3":[3,6]},{"c4":5},{"n15":[16384,32767]}]}
]
}"#;
let raw_report_vector: PlainReportVector = serde_json::from_str(&json).unwrap();
assert_eq!(raw_report_vector, raw_report_vector2);
}
#[test]
fn test_categorical_create_report_shares() {
let json = r#"
{
"schema":[["attr1","c2"],["attr2","c3"],["attr3","c4"],["attr4","c15"]],
"reports":[
{"attributes":[{"c2":2},{"c3":3},{"c4":5},{"c15":28086}]},
{"attributes":[{"c2":0},{"c3":2},{"c4":13},{"c15":3893}]},
{"attributes":[{"c2":1},{"c3":4},{"c4":5},{"c15":8962}]}
]
}"#;
let mut rng = OsRng;
let (schema, _, mut share1, share2) = create_report_shares::<1>(&mut rng, json).unwrap();
assert!(schema.is_valid());
assert_ne!(share1, share2);
assert_eq!(share1.len(), share2.len());
share1.reveal(share2);
let report_handler = share1.report_handler();
assert_eq!(
*share1.get(0).unwrap(),
report_handler.create_report(&[2, 3, 5, 28086])
);
assert_eq!(
*share1.get(1).unwrap(),
report_handler.create_report(&[0, 2, 13, 3893])
);
assert_eq!(
*share1.get(2).unwrap(),
report_handler.create_report(&[1, 4, 5, 8962])
);
}
#[test]
fn test_mixed_create_report_shares() {
let json = r#"
{
"schema":[["attr1","c2"],["attr2",{"n3":6}],["attr3","c4"],["attr4",{"n15":32767}]],
"reports":[
{"attributes":[{"c2":2},{"n3":[5,6]},{"c4":5},{"n15": [32766,32767]}]},
{"attributes":[{"c2":0},{"n3":[0,6]},{"c4":13},{"n15":[0,32767]}]},
{"attributes":[{"c2":1},{"n3":[3,6]},{"c4":5},{"n15":[16384,32767]}]}
]
}"#;
let mut rng = OsRng;
let (schema, _, mut share1, share2) = create_report_shares::<1>(&mut rng, json).unwrap();
assert!(schema.is_valid());
assert_ne!(share1, share2);
assert_eq!(share1.len(), share2.len());
share1.reveal(share2);
let report_handler = share1.report_handler();
assert_eq!(
*share1.get(0).unwrap(),
report_handler.create_report(&[2, 5, 5, 32766])
);
assert_eq!(
*share1.get(1).unwrap(),
report_handler.create_report(&[0, 0, 13, 0])
);
assert_eq!(
*share1.get(2).unwrap(),
report_handler.create_report(&[1, 3, 5, 16384])
);
}
}