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
//
// Copyright 2020-2022 Signal Messenger, LLC.
// SPDX-License-Identifier: AGPL-3.0-only
//

use partial_default::PartialDefault;
use serde::{Deserialize, Serialize, Serializer};

use crate::common::constants::*;
use crate::common::errors::*;
use crate::common::serialization::VersionByte;
use crate::common::simple_types::*;
use crate::{api, crypto};

#[derive(Serialize, Deserialize, PartialDefault)]
pub struct ProfileKeyCredentialPresentationV1 {
    pub(crate) version: u8, // Not ReservedByte or VersionByte to allow deserializing a V2 presentation as V1.
    pub(crate) proof: crypto::proofs::ProfileKeyCredentialPresentationProofV1,
    pub(crate) uid_enc_ciphertext: crypto::uid_encryption::Ciphertext,
    pub(crate) profile_key_enc_ciphertext: crypto::profile_key_encryption::Ciphertext,
}

impl ProfileKeyCredentialPresentationV1 {
    pub fn get_uuid_ciphertext(&self) -> api::groups::UuidCiphertext {
        api::groups::UuidCiphertext {
            reserved: Default::default(),
            ciphertext: self.uid_enc_ciphertext,
        }
    }

    pub fn get_profile_key_ciphertext(&self) -> api::groups::ProfileKeyCiphertext {
        api::groups::ProfileKeyCiphertext {
            reserved: Default::default(),
            ciphertext: self.profile_key_enc_ciphertext,
        }
    }
}

/// Like [`ProfileKeyCredentialPresentationV1`], but with an optimized proof.
#[derive(Serialize, Deserialize, PartialDefault)]
pub struct ProfileKeyCredentialPresentationV2 {
    pub(crate) version: VersionByte<PRESENTATION_VERSION_2>,
    pub(crate) proof: crypto::proofs::ProfileKeyCredentialPresentationProofV2,
    pub(crate) uid_enc_ciphertext: crypto::uid_encryption::Ciphertext,
    pub(crate) profile_key_enc_ciphertext: crypto::profile_key_encryption::Ciphertext,
}

impl ProfileKeyCredentialPresentationV2 {
    pub fn get_uuid_ciphertext(&self) -> api::groups::UuidCiphertext {
        api::groups::UuidCiphertext {
            reserved: Default::default(),
            ciphertext: self.uid_enc_ciphertext,
        }
    }

    pub fn get_profile_key_ciphertext(&self) -> api::groups::ProfileKeyCiphertext {
        api::groups::ProfileKeyCiphertext {
            reserved: Default::default(),
            ciphertext: self.profile_key_enc_ciphertext,
        }
    }
}

#[derive(Serialize, Deserialize, PartialDefault)]
pub struct ExpiringProfileKeyCredentialPresentation {
    pub(crate) version: VersionByte<PRESENTATION_VERSION_3>,
    pub(crate) proof: crypto::proofs::ExpiringProfileKeyCredentialPresentationProof,
    pub(crate) uid_enc_ciphertext: crypto::uid_encryption::Ciphertext,
    pub(crate) profile_key_enc_ciphertext: crypto::profile_key_encryption::Ciphertext,
    pub(crate) credential_expiration_time: Timestamp,
}

impl ExpiringProfileKeyCredentialPresentation {
    pub fn get_uuid_ciphertext(&self) -> api::groups::UuidCiphertext {
        api::groups::UuidCiphertext {
            reserved: Default::default(),
            ciphertext: self.uid_enc_ciphertext,
        }
    }

    pub fn get_profile_key_ciphertext(&self) -> api::groups::ProfileKeyCiphertext {
        api::groups::ProfileKeyCiphertext {
            reserved: Default::default(),
            ciphertext: self.profile_key_enc_ciphertext,
        }
    }

    pub fn get_expiration_time(&self) -> Timestamp {
        self.credential_expiration_time
    }
}

pub enum AnyProfileKeyCredentialPresentation {
    V1(ProfileKeyCredentialPresentationV1),
    V2(ProfileKeyCredentialPresentationV2),
    V3(ExpiringProfileKeyCredentialPresentation),
}

impl AnyProfileKeyCredentialPresentation {
    pub fn new(presentation_bytes: &[u8]) -> Result<Self, ZkGroupDeserializationFailure> {
        match presentation_bytes[0] {
            PRESENTATION_VERSION_1 => {
                crate::deserialize::<ProfileKeyCredentialPresentationV1>(presentation_bytes)
                    .map(AnyProfileKeyCredentialPresentation::V1)
            }
            PRESENTATION_VERSION_2 => {
                crate::deserialize::<ProfileKeyCredentialPresentationV2>(presentation_bytes)
                    .map(AnyProfileKeyCredentialPresentation::V2)
            }
            PRESENTATION_VERSION_3 => {
                crate::deserialize::<ExpiringProfileKeyCredentialPresentation>(presentation_bytes)
                    .map(AnyProfileKeyCredentialPresentation::V3)
            }
            _ => Err(ZkGroupDeserializationFailure::new::<Self>()),
        }
    }

    pub fn get_uuid_ciphertext(&self) -> api::groups::UuidCiphertext {
        match self {
            AnyProfileKeyCredentialPresentation::V1(presentation) => {
                presentation.get_uuid_ciphertext()
            }
            AnyProfileKeyCredentialPresentation::V2(presentation) => {
                presentation.get_uuid_ciphertext()
            }
            AnyProfileKeyCredentialPresentation::V3(presentation) => {
                presentation.get_uuid_ciphertext()
            }
        }
    }

    pub fn get_profile_key_ciphertext(&self) -> api::groups::ProfileKeyCiphertext {
        match self {
            AnyProfileKeyCredentialPresentation::V1(presentation) => {
                presentation.get_profile_key_ciphertext()
            }
            AnyProfileKeyCredentialPresentation::V2(presentation) => {
                presentation.get_profile_key_ciphertext()
            }
            AnyProfileKeyCredentialPresentation::V3(presentation) => {
                presentation.get_profile_key_ciphertext()
            }
        }
    }

    pub fn to_structurally_valid_v1_presentation_bytes(&self) -> Vec<u8> {
        let v1 = ProfileKeyCredentialPresentationV1 {
            version: PRESENTATION_VERSION_1,
            proof: crypto::proofs::ProfileKeyCredentialPresentationProofV1::from_invalid_proof(
                // Hardcoded length of a valid v1 proof.
                vec![0; 0x0140],
            ),
            uid_enc_ciphertext: self.get_uuid_ciphertext().ciphertext,
            profile_key_enc_ciphertext: self.get_profile_key_ciphertext().ciphertext,
        };
        let result = crate::serialize(&v1);
        debug_assert_eq!(result.len(), PROFILE_KEY_CREDENTIAL_PRESENTATION_V1_LEN);
        result
    }
}

impl Serialize for AnyProfileKeyCredentialPresentation {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            AnyProfileKeyCredentialPresentation::V1(presentation) => {
                presentation.serialize(serializer)
            }
            AnyProfileKeyCredentialPresentation::V2(presentation) => {
                presentation.serialize(serializer)
            }
            AnyProfileKeyCredentialPresentation::V3(presentation) => {
                presentation.serialize(serializer)
            }
        }
    }
}

impl From<ProfileKeyCredentialPresentationV1> for AnyProfileKeyCredentialPresentation {
    fn from(presentation: ProfileKeyCredentialPresentationV1) -> Self {
        Self::V1(presentation)
    }
}
impl From<ProfileKeyCredentialPresentationV2> for AnyProfileKeyCredentialPresentation {
    fn from(presentation: ProfileKeyCredentialPresentationV2) -> Self {
        Self::V2(presentation)
    }
}
impl From<ExpiringProfileKeyCredentialPresentation> for AnyProfileKeyCredentialPresentation {
    fn from(presentation: ExpiringProfileKeyCredentialPresentation) -> Self {
        Self::V3(presentation)
    }
}