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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
use crate::{
    certificate::{CertificateSlice, VotePlanId},
    transaction::{
        Payload, PayloadAuthData, PayloadData, PayloadSlice, SingleAccountBindingSignature,
        TransactionBindingAuthData,
    },
    vote::{CommitteeId, PayloadType, TryFromIntError},
};
use chain_core::{
    packer::Codec,
    property::{Deserialize, DeserializeFromSlice, ReadError, Serialize, WriteError},
};
use chain_crypto::Verification;
use chain_vote::TallyDecryptShare;
use thiserror::Error;
use typed_bytes::{ByteArray, ByteBuilder};

#[derive(Debug, Eq, PartialEq, Hash, Clone)]
pub struct VoteTally {
    id: VotePlanId,
    payload: VoteTallyPayload,
}

#[derive(Debug, Eq, PartialEq, Hash, Clone)]
pub enum VoteTallyPayload {
    Public,
    Private { inner: DecryptedPrivateTally },
}

#[derive(Debug, Clone)]
pub enum TallyProof {
    Public {
        id: CommitteeId,
        signature: SingleAccountBindingSignature,
    },

    Private {
        id: CommitteeId,
        signature: SingleAccountBindingSignature,
    },
}

#[derive(Debug, Error)]
#[error("decrypt_shares in the proposal should have the same options amount")]
pub struct DecryptedPrivateTallyError {}

#[derive(Debug, Eq, PartialEq, Hash, Clone)]
pub struct DecryptedPrivateTally {
    inner: Box<[DecryptedPrivateTallyProposal]>,
}

#[derive(Debug, Eq, PartialEq, Hash, Clone)]
pub struct DecryptedPrivateTallyProposal {
    pub decrypt_shares: Box<[TallyDecryptShare]>,
    pub tally_result: Box<[u64]>,
}

impl VoteTallyPayload {
    pub fn payload_type(&self) -> PayloadType {
        match self {
            Self::Public => PayloadType::Public,
            Self::Private { .. } => PayloadType::Private,
        }
    }

    pub fn payload_decrypted(&self) -> Option<&DecryptedPrivateTally> {
        match self {
            Self::Public => None,
            Self::Private { inner } => Some(inner),
        }
    }
}

impl VoteTally {
    pub fn new_public(id: VotePlanId) -> Self {
        Self {
            id,
            payload: VoteTallyPayload::Public,
        }
    }

    pub fn new_private(id: VotePlanId, decrypted_tally: DecryptedPrivateTally) -> Self {
        Self {
            id,
            payload: VoteTallyPayload::Private {
                inner: decrypted_tally,
            },
        }
    }

    pub fn id(&self) -> &VotePlanId {
        &self.id
    }

    pub fn tally_type(&self) -> PayloadType {
        self.payload.payload_type()
    }

    pub fn tally_decrypted(&self) -> Option<&DecryptedPrivateTally> {
        self.payload.payload_decrypted()
    }

    pub fn serialize_in(&self, bb: ByteBuilder<Self>) -> ByteBuilder<Self> {
        let bb = bb.bytes(self.id().as_ref()).u8(self.tally_type() as u8);

        match &self.payload {
            VoteTallyPayload::Public => bb,
            VoteTallyPayload::Private { inner: proposals } => {
                bb.u8(proposals.inner.len().try_into().unwrap()).fold(
                    proposals.inner.iter(),
                    |bb, proposal| {
                        // Shares per proposal, n_members x n_options
                        let n_members = proposal.decrypt_shares.len().try_into().unwrap();
                        if n_members == 0 {
                            bb.u8(0).u8(0)
                        } else {
                            let n_options = proposal.tally_result.len().try_into().unwrap();
                            bb.u8(n_members)
                                .u8(n_options)
                                .fold(proposal.decrypt_shares.iter(), |bb, s| {
                                    bb.bytes(&s.to_bytes())
                                })
                                .fold(proposal.tally_result.iter(), |bb, count| bb.u64(*count))
                        }
                    },
                )
            }
        }
    }

    pub fn serialize(&self) -> ByteArray<Self> {
        self.serialize_in(ByteBuilder::new()).finalize()
    }
}

impl TallyProof {
    pub fn serialize_in(&self, bb: ByteBuilder<Self>) -> ByteBuilder<Self> {
        match self {
            Self::Public { id, signature } => bb.u8(0).bytes(id.as_ref()).bytes(signature.as_ref()),
            Self::Private { id, signature } => {
                bb.u8(1).bytes(id.as_ref()).bytes(signature.as_ref())
            }
        }
    }

    pub fn verify(
        &self,
        tally_type: PayloadType,
        verify_data: &TransactionBindingAuthData<'_>,
    ) -> Verification {
        match self {
            Self::Public { id, signature } => {
                if tally_type != PayloadType::Public {
                    Verification::Failed
                } else {
                    let pk = id.public_key();
                    signature.verify_slice(&pk, verify_data)
                }
            }
            Self::Private { id, signature } => {
                if tally_type != PayloadType::Private {
                    Verification::Failed
                } else {
                    let pk = id.public_key();
                    signature.verify_slice(&pk, verify_data)
                }
            }
        }
    }
}

impl DecryptedPrivateTally {
    pub fn new(
        proposals: Vec<DecryptedPrivateTallyProposal>,
    ) -> Result<Self, DecryptedPrivateTallyError> {
        if proposals.iter().all(|proposal| {
            let mut shares = proposal.decrypt_shares.iter();
            match shares.next() {
                Some(first_share) => shares.all(|share| share.options() == first_share.options()),
                None => true,
            }
        }) {
            Ok(Self {
                inner: proposals.into_boxed_slice(),
            })
        } else {
            Err(DecryptedPrivateTallyError {})
        }
    }

    pub fn iter(&self) -> impl Iterator<Item = &DecryptedPrivateTallyProposal> {
        self.inner.iter()
    }
}

/* Auth/Payload ************************************************************* */

impl Payload for VoteTally {
    const HAS_DATA: bool = true;
    const HAS_AUTH: bool = true;
    type Auth = TallyProof;

    fn payload_data(&self) -> PayloadData<Self> {
        PayloadData(
            self.serialize_in(ByteBuilder::new())
                .finalize_as_vec()
                .into(),
            std::marker::PhantomData,
        )
    }

    fn payload_auth_data(auth: &Self::Auth) -> PayloadAuthData<Self> {
        PayloadAuthData(
            auth.serialize_in(ByteBuilder::new())
                .finalize_as_vec()
                .into(),
            std::marker::PhantomData,
        )
    }

    fn payload_to_certificate_slice(p: PayloadSlice<'_, Self>) -> Option<CertificateSlice<'_>> {
        Some(CertificateSlice::from(p))
    }
}

/* Ser/De ******************************************************************* */

impl Serialize for VoteTally {
    fn serialized_size(&self) -> usize {
        self.serialize().as_slice().len()
    }

    fn serialize<W: std::io::Write>(&self, codec: &mut Codec<W>) -> Result<(), WriteError> {
        codec.put_bytes(self.serialize().as_slice())
    }
}

impl DeserializeFromSlice for TallyProof {
    fn deserialize_from_slice(codec: &mut Codec<&[u8]>) -> Result<Self, ReadError> {
        match codec.get_u8()? {
            0 => {
                let id = CommitteeId::deserialize_from_slice(codec)?;
                let signature = SingleAccountBindingSignature::deserialize_from_slice(codec)?;
                Ok(Self::Public { id, signature })
            }
            1 => {
                let id = CommitteeId::deserialize_from_slice(codec)?;
                let signature = SingleAccountBindingSignature::deserialize_from_slice(codec)?;
                Ok(Self::Private { id, signature })
            }
            _ => Err(ReadError::StructureInvalid(
                "Unknown Tally proof type".to_owned(),
            )),
        }
    }
}

impl DeserializeFromSlice for VoteTally {
    fn deserialize_from_slice(codec: &mut Codec<&[u8]>) -> Result<Self, ReadError> {
        let id = <[u8; 32]>::deserialize(codec)?.into();
        let payload_type = codec
            .get_u8()?
            .try_into()
            .map_err(|e: TryFromIntError| ReadError::StructureInvalid(e.to_string()))?;

        let payload = match payload_type {
            PayloadType::Public => VoteTallyPayload::Public,
            PayloadType::Private => {
                let proposals_number = codec.get_u8()? as usize;
                let mut proposals = Vec::with_capacity(proposals_number);
                for _i in 0..proposals_number {
                    let shares_number = codec.get_u8()? as usize;
                    let options_number = codec.get_u8()? as usize;
                    let share_bytes = TallyDecryptShare::bytes_len(options_number);
                    let mut shares = Vec::with_capacity(shares_number);
                    for _j in 0..shares_number {
                        let s_buf = codec.get_slice(share_bytes)?;
                        let share = TallyDecryptShare::from_bytes(s_buf).ok_or_else(|| {
                            ReadError::StructureInvalid(
                                "invalid decrypt share structure".to_owned(),
                            )
                        })?;
                        shares.push(share);
                    }
                    let mut decrypted = Vec::with_capacity(options_number);
                    for _j in 0..options_number {
                        decrypted.push(codec.get_be_u64()?);
                    }
                    let shares = shares.into_boxed_slice();
                    let decrypted = decrypted.into_boxed_slice();
                    proposals.push(DecryptedPrivateTallyProposal {
                        decrypt_shares: shares,
                        tally_result: decrypted,
                    });
                }

                VoteTallyPayload::Private {
                    inner: DecryptedPrivateTally::new(proposals)
                        .map_err(|err| ReadError::InvalidData(err.to_string()))?,
                }
            }
        };

        Ok(Self { id, payload })
    }
}