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
use SdaClient;
use crypto::*;
use errors::SdaClientResult;
use sda_protocol::*;
#[derive(Debug)]
pub struct RecipientOutput {
pub modulus: i64,
pub values: Vec<i64>,
}
impl RecipientOutput {
pub fn positive(&self) -> RecipientOutput {
let positive_values = self.values.iter().map(|&v| if v < 0 { v + self.modulus } else { v }).collect();
RecipientOutput {
modulus: self.modulus,
values: positive_values,
}
}
}
pub trait Receiving {
fn upload_aggregation(&self, aggregation: &Aggregation) -> SdaClientResult<()>;
fn begin_aggregation(&self, aggregation: &AggregationId) -> SdaClientResult<()>;
fn end_aggregation(&self, aggregation: &AggregationId) -> SdaClientResult<()>;
fn reveal_aggregation(&self, aggregation: &AggregationId) -> SdaClientResult<RecipientOutput>;
}
impl Receiving for SdaClient {
fn upload_aggregation(&self, aggregation: &Aggregation) -> SdaClientResult<()> {
Ok(self.service.create_aggregation(&self.agent, aggregation)?)
}
fn begin_aggregation(&self, aggregation_id: &AggregationId) -> SdaClientResult<()> {
let aggregation = self.service.get_aggregation(&self.agent, aggregation_id)?
.ok_or(format!("Unknown aggregation, {:?}", aggregation_id))?;
let candidates = self.service.suggest_committee(&self.agent, &aggregation_id)?;
let selected_clerks = candidates.iter()
.take(aggregation.committee_sharing_scheme.output_size())
.map(|candidate| (candidate.id, candidate.keys[0]) )
.collect();
let committee = Committee {
aggregation: aggregation_id.clone(),
clerks_and_keys: selected_clerks,
};
Ok(self.service.create_committee(&self.agent, &committee)?)
}
fn end_aggregation(&self, aggregation: &AggregationId) -> SdaClientResult<()> {
let status = self.service.get_aggregation_status(&self.agent, aggregation)?
.ok_or("Unknown aggregation")?;
if status.snapshots.len() >= 1 {
return Ok(());
}
let snapshot = Snapshot {
id: SnapshotId::random(),
aggregation: aggregation.clone(),
};
Ok(self.service.create_snapshot(&self.agent, &snapshot)?)
}
fn reveal_aggregation(&self, aggregation_id: &AggregationId) -> SdaClientResult<RecipientOutput> {
let aggregation = self.service.get_aggregation(&self.agent, aggregation_id)?
.ok_or(format!("Unknown aggregation, {:?}", aggregation_id))?;
let committee = self.service.get_committee(&self.agent, aggregation_id)?
.ok_or(format!("Unknown committee, {:?}", aggregation_id))?;
let status = self.service.get_aggregation_status(&self.agent, aggregation_id)?
.ok_or("Unknown aggregation")?;
let snapshot = status.snapshots.iter()
.filter(|snapshot| snapshot.result_ready)
.nth(0)
.ok_or("Aggregation not ready")?;
let result = self.service.get_snapshot_result(&self.agent, aggregation_id, &snapshot.id)?
.ok_or("Missing aggregation result")?;
let encrypted_masks = result.recipient_encryptions;
let encrypted_masked_output_shares = result.clerk_encryptions;
let mask: Vec<Mask> = match encrypted_masks {
None => vec![],
Some(encrypted_masks) => {
let mask_decryptor = self.crypto.new_share_decryptor(
&aggregation.recipient_key,
&aggregation.recipient_encryption_scheme)?;
let decrypted_masks = encrypted_masks.iter()
.map(|encryption| Ok(mask_decryptor.decrypt(encryption)?))
.collect::<SdaClientResult<Vec<Vec<Mask>>>>()?;
let mask_combiner = self.crypto.new_mask_combiner(
&aggregation.masking_scheme)?;
mask_combiner.combine(&decrypted_masks)
}
};
let masked_output: Vec<MaskedSecret> = {
let share_decryptor = self.crypto.new_share_decryptor(
&aggregation.recipient_key,
&aggregation.recipient_encryption_scheme)?;
let masked_output_shares: Vec<(usize, Vec<Share>)> = encrypted_masked_output_shares.iter()
.map(|clerking_result| {
let clerk_index = committee.clerks_and_keys.iter()
.position(|&(id,_)| clerking_result.clerk == id)
.ok_or(format!("Missing clerk, {:?}", clerking_result.clerk))?;
let shares = share_decryptor.decrypt(&clerking_result.encryption)?;
Ok((clerk_index, shares))
})
.collect::<SdaClientResult<Vec<(usize, Vec<Share>)>>>()?;
let secret_reconstructor = self.crypto.new_secret_reconstructor(
&aggregation.committee_sharing_scheme,
aggregation.vector_dimension)?;
let masked_output = secret_reconstructor.reconstruct(&masked_output_shares)?;
masked_output
};
let secret_unmasker = self.crypto.new_secret_unmasker(
&aggregation.masking_scheme)?;
let output = secret_unmasker.unmask(&(mask, masked_output));
Ok(RecipientOutput {
modulus: aggregation.modulus,
values: output,
})
}
}
impl SdaClient {
}