penumbra_custody/threshold/
config.rs

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
use anyhow::Result;
use decaf377::Fq;
use decaf377_frost as frost;
use ed25519_consensus::{SigningKey, VerificationKey};
use penumbra_keys::{keys::NullifierKey, FullViewingKey};
use rand_core::CryptoRngCore;
use serde::{Deserialize, Serialize};
use serde_with::{formats::Uppercase, hex::Hex, DisplayFromStr, TryFromInto};
use std::collections::{HashMap, HashSet};

/// A shim to serialize frost::keys::SigningShare
#[serde_as]
#[derive(Serialize, Deserialize)]
struct SigningShareWrapper(#[serde_as(as = "Hex<Uppercase>")] Vec<u8>);

impl From<frost::keys::SigningShare> for SigningShareWrapper {
    fn from(value: frost::keys::SigningShare) -> Self {
        Self(value.serialize())
    }
}

impl TryFrom<SigningShareWrapper> for frost::keys::SigningShare {
    type Error = anyhow::Error;

    fn try_from(value: SigningShareWrapper) -> std::result::Result<Self, Self::Error> {
        Ok(Self::deserialize(value.0)?)
    }
}

/// A shim to serialize frost::keys::VerifyingShare
#[serde_as]
#[derive(Serialize, Deserialize)]
struct VerifyingShareWrapper(#[serde_as(as = "Hex<Uppercase>")] Vec<u8>);

impl From<frost::keys::VerifyingShare> for VerifyingShareWrapper {
    fn from(value: frost::keys::VerifyingShare) -> Self {
        Self(value.serialize())
    }
}

impl TryFrom<VerifyingShareWrapper> for frost::keys::VerifyingShare {
    type Error = anyhow::Error;

    fn try_from(value: VerifyingShareWrapper) -> std::result::Result<Self, Self::Error> {
        Ok(Self::deserialize(value.0)?)
    }
}

/// A shim to serialize SigningKey
#[serde_as]
#[derive(Serialize, Deserialize)]
struct SigningKeyWrapper(#[serde_as(as = "Hex<Uppercase>")] Vec<u8>);

impl From<SigningKey> for SigningKeyWrapper {
    fn from(value: SigningKey) -> Self {
        Self(value.to_bytes().to_vec())
    }
}

impl TryFrom<SigningKeyWrapper> for SigningKey {
    type Error = anyhow::Error;

    fn try_from(value: SigningKeyWrapper) -> std::result::Result<Self, Self::Error> {
        Ok(Self::try_from(value.0.as_slice())?)
    }
}

/// A shim to serialize VerifyingKey
#[serde_as]
#[derive(Serialize, Deserialize)]
struct VerificationKeyWrapper(#[serde_as(as = "Hex<Uppercase>")] Vec<u8>);

impl From<VerificationKey> for VerificationKeyWrapper {
    fn from(value: VerificationKey) -> Self {
        Self(value.to_bytes().to_vec())
    }
}

impl TryFrom<VerificationKeyWrapper> for VerificationKey {
    type Error = anyhow::Error;

    fn try_from(value: VerificationKeyWrapper) -> std::result::Result<Self, Self::Error> {
        Ok(Self::try_from(value.0.as_slice())?)
    }
}

#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Config {
    threshold: u16,
    #[serde_as(as = "DisplayFromStr")]
    fvk: FullViewingKey,
    #[serde_as(as = "TryFromInto<SigningShareWrapper>")]
    spend_key_share: frost::keys::SigningShare,
    #[serde_as(as = "TryFromInto<SigningKeyWrapper>")]
    signing_key: SigningKey,
    #[serde_as(
        as = "HashMap<TryFromInto<VerificationKeyWrapper>, TryFromInto<VerifyingShareWrapper>>"
    )]
    verifying_shares: HashMap<VerificationKey, frost::keys::VerifyingShare>,
}

impl PartialEq for Config {
    fn eq(&self, other: &Self) -> bool {
        self.threshold == other.threshold
            && self.fvk == other.fvk
            && self.spend_key_share == other.spend_key_share
            // TIMING LEAK
            && self.signing_key.as_bytes() == other.signing_key.as_bytes()
            && self.verifying_shares == other.verifying_shares
    }
}

impl Eq for Config {}

impl Config {
    /// Create a config from the parts that get spit out by the DKG protocol.
    pub(crate) fn from_parts(
        key_package: frost::keys::KeyPackage,
        public_key_package: frost::keys::PublicKeyPackage,
        signing_key: SigningKey,
        verification_keys: Vec<VerificationKey>,
        nullifier_key: Fq,
    ) -> Self {
        let fvk = FullViewingKey::from_components(
            public_key_package
                .group_public()
                .serialize()
                .as_slice()
                .try_into()
                .expect("conversion of a group element to a VerifyingKey should not fail"),
            NullifierKey(nullifier_key),
        );
        let spend_key_share = key_package.secret_share().to_owned();
        let verifying_shares = verification_keys
            .into_iter()
            .map(|vk| {
                let id = frost::Identifier::derive(vk.as_bytes().as_slice())
                    .expect("deriving identifiers should not fail");
                (vk, public_key_package.signer_pubkeys()[&id])
            })
            .collect();
        Self {
            threshold: *key_package.min_signers(),
            fvk,
            spend_key_share,
            signing_key,
            verifying_shares,
        }
    }

    pub fn deal(mut rng: &mut impl CryptoRngCore, t: u16, n: u16) -> Result<Vec<Self>> {
        let signing_keys = (0..n)
            .map(|_| {
                let sk = SigningKey::new(&mut rng);
                let pk = sk.verification_key();
                (pk, sk)
            })
            .collect::<HashMap<_, _>>();
        let identifiers = signing_keys
            .keys()
            .cloned()
            .map(|pk| Ok((pk, frost::Identifier::derive(pk.as_bytes().as_slice())?)))
            .collect::<Result<HashMap<_, _>, frost::Error>>()?;

        let (share_map, public_key_package) = frost::keys::generate_with_dealer(
            n,
            t,
            frost::keys::IdentifierList::Custom(
                identifiers.values().cloned().collect::<Vec<_>>().as_slice(),
            ),
            &mut rng,
        )?;
        let verifying_shares = signing_keys
            .keys()
            .map(|pk| {
                let identifier = frost::Identifier::derive(pk.to_bytes().as_slice())
                    .expect("should be able to derive identifier");
                (pk.clone(), public_key_package.signer_pubkeys()[&identifier])
            })
            .collect::<HashMap<_, _>>();
        // Okay, this conversion is a bit of a hack, but it should work...
        // It's a hack cause we're going via the serialization, but, you know, that should be fine.
        let fvk = FullViewingKey::from_components(
            public_key_package
                .group_public()
                .serialize()
                .as_slice()
                .try_into()
                .expect("conversion of a group element to a VerifyingKey should not fail"),
            NullifierKey(Fq::rand(rng)),
        );

        Ok(signing_keys
            .into_iter()
            .map(|(verification_key, signing_key)| {
                let identifier = identifiers[&verification_key];
                let signing_share = share_map[&identifier].value().clone();
                Self {
                    threshold: t,
                    signing_key,
                    fvk: fvk.clone(),
                    spend_key_share: signing_share,
                    verifying_shares: verifying_shares.clone(),
                }
            })
            .collect())
    }

    pub fn threshold(&self) -> u16 {
        self.threshold
    }

    fn group_public(&self) -> frost::keys::VerifyingKey {
        frost::keys::VerifyingKey::deserialize(
            self.fvk.spend_verification_key().to_bytes().to_vec(),
        )
        .expect("should be able to parse out VerifyingKey from FullViewingKey")
    }

    pub fn key_package(&self) -> frost::keys::KeyPackage {
        let identifier =
            frost::Identifier::derive(&self.signing_key.verification_key().as_bytes().as_slice())
                .expect("deriving our identifier should not fail");

        frost::keys::KeyPackage::new(
            identifier,
            self.spend_key_share,
            self.spend_key_share.into(),
            self.group_public(),
            self.threshold,
        )
    }

    pub fn public_key_package(&self) -> frost::keys::PublicKeyPackage {
        let signer_pubkeys = self
            .verifying_shares
            .iter()
            .map(|(vk, share)| {
                (
                    frost::Identifier::derive(vk.to_bytes().as_slice())
                        .expect("deriving an identifier should not fail"),
                    share.clone(),
                )
            })
            .collect();
        frost::keys::PublicKeyPackage::new(signer_pubkeys, self.group_public())
    }

    pub fn signing_key(&self) -> &SigningKey {
        &self.signing_key
    }

    pub fn fvk(&self) -> &FullViewingKey {
        &self.fvk
    }

    pub fn verification_keys(&self) -> HashSet<VerificationKey> {
        self.verifying_shares.keys().cloned().collect()
    }
}

#[cfg(test)]
mod test {
    use rand_core::OsRng;

    use super::*;

    #[test]
    fn test_config_serialization_roundtrip() -> Result<()> {
        // You can't put 1, because no FUN is allowed
        let config = Config::deal(&mut OsRng, 2, 2)?.pop().unwrap();
        let config_str = serde_json::to_string(&config)?;
        let config2: Config = serde_json::from_str(&config_str)?;
        // Can't derive partial eq, so go field by field
        assert_eq!(config.threshold, config2.threshold);
        assert_eq!(config.fvk, config2.fvk);
        assert_eq!(config.spend_key_share, config2.spend_key_share);
        assert_eq!(config.verifying_shares, config2.verifying_shares);
        Ok(())
    }
}