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
// Copyright 2021 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under the MIT license <LICENSE-MIT
// https://opensource.org/licenses/MIT> or the Modified BSD license <LICENSE-BSD
// https://opensource.org/licenses/BSD-3-Clause>, at your option. This file may not be copied,
// modified, or distributed except according to those terms. Please review the Licences for the
// specific language governing permissions and limitations relating to use of the SAFE Network
// Software.

//! Module providing keys, keypairs, and signatures.
//!
//! The easiest way to get a `PublicKey` is to create a random `Keypair` first through one of the
//! `new` functions. A `PublicKey` can't be generated by itself; it must always be derived from a
//! secret key.

use crate::{Error, Result};
use crate::{PublicKey, SecretKey, Signature, SignatureShare};

use ed25519_dalek::Signer;
use rand::{CryptoRng, Rng};
use serde::{Deserialize, Serialize};
use std::fmt::{self, Debug, Formatter};
use std::sync::Arc;
use threshold_crypto::{self, serde_impl::SerdeSecret, PublicKeySet};

#[derive(Clone, PartialEq, Eq)]
/// Entity that owns the data or tokens.
pub enum OwnerType {
    /// Single owner
    Single(PublicKey),
    /// Multi sig owner
    Multi(PublicKeySet),
}

impl OwnerType {
    /// Returns the owner public key
    pub fn public_key(&self) -> PublicKey {
        match self {
            Self::Single(key) => *key,
            Self::Multi(key_set) => PublicKey::Bls(key_set.public_key()),
        }
    }

    /// Tries to get the key set in case this is a Multi owner.
    pub fn public_key_set(&self) -> Result<PublicKeySet> {
        match self {
            Self::Single(_) => Err(Error::InvalidOwnerNotPublicKeySet),
            Self::Multi(key_set) => Ok(key_set.clone()),
        }
    }

    ///
    pub fn verify<T: Serialize>(&self, signature: &Signature, data: &T) -> bool {
        let data = match bincode::serialize(&data) {
            Err(_) => return false,
            Ok(data) => data,
        };
        match signature {
            Signature::Bls(sig) => {
                if let OwnerType::Multi(set) = self {
                    set.public_key().verify(&sig, data)
                } else {
                    false
                }
            }
            ed @ Signature::Ed25519(_) => self.public_key().verify(ed, data).is_ok(),
            Signature::BlsShare(share) => {
                if let OwnerType::Multi(set) = self {
                    let pubkey_share = set.public_key_share(share.index);
                    pubkey_share.verify(&share.share, data)
                } else {
                    false
                }
            }
        }
    }
}

impl Debug for OwnerType {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            OwnerType::Single(_) => Debug::fmt(&self, f),
            OwnerType::Multi(key_set) => write!(
                f,
                "OwnerType::Multi(PkSet {{ public_key: {:?} }})",
                key_set.public_key()
            ),
        }
    }
}
/// Ability to sign and validate data/tokens, as well as specify the type of ownership of that data
pub trait Signing {
    ///
    fn id(&self) -> OwnerType;
    ///
    fn sign<T: Serialize>(&self, data: &T) -> Result<Signature>;
    ///
    fn verify<T: Serialize>(&self, sig: &Signature, data: &T) -> bool;
}

impl Signing for Keypair {
    fn id(&self) -> OwnerType {
        match self {
            Keypair::Ed25519(pair) => OwnerType::Single(PublicKey::Ed25519(pair.public)),
            Keypair::BlsShare(share) => OwnerType::Multi(share.public_key_set.clone()),
        }
    }

    fn sign<T: Serialize>(&self, data: &T) -> Result<Signature> {
        let bytes = bincode::serialize(data).map_err(|e| Error::Serialisation(e.to_string()))?;
        Ok(self.sign(&bytes))
    }

    fn verify<T: Serialize>(&self, signature: &Signature, data: &T) -> bool {
        self.id().verify(signature, data)
    }
}

/// Wrapper for different keypair types.
#[derive(Serialize, Deserialize, Clone)]
pub enum Keypair {
    /// Ed25519 keypair.
    Ed25519(Arc<ed25519_dalek::Keypair>),
    /// BLS keypair share.
    BlsShare(Arc<BlsKeypairShare>),
}

impl Debug for Keypair {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        write!(formatter, "Keypair::")?;
        match self {
            Self::Ed25519(_) => write!(formatter, "Ed25519(..)"),
            Self::BlsShare(_) => write!(formatter, "BlsShare(..)"),
        }
    }
}

// Need to manually implement this due to a missing impl in `Ed25519::Keypair`.
impl PartialEq for Keypair {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Ed25519(keypair), Self::Ed25519(other_keypair)) => {
                // TODO: After const generics land, remove the `to_vec()` calls.
                keypair.to_bytes().to_vec() == other_keypair.to_bytes().to_vec()
            }
            (Self::BlsShare(keypair), Self::BlsShare(other_keypair)) => keypair == other_keypair,
            _ => false,
        }
    }
}

// Need to manually implement this due to a missing impl in `Ed25519::Keypair`.
impl Eq for Keypair {}

impl Keypair {
    /// Constructs a random Ed25519 keypair.
    pub fn new_ed25519<T: CryptoRng + Rng>(rng: &mut T) -> Self {
        let keypair = ed25519_dalek::Keypair::generate(rng);
        Self::Ed25519(Arc::new(keypair))
    }

    /// Constructs a BLS keypair share.
    pub fn new_bls_share(
        index: usize,
        secret_share: threshold_crypto::SecretKeyShare,
        public_key_set: PublicKeySet,
    ) -> Self {
        Self::BlsShare(Arc::new(BlsKeypairShare {
            index,
            secret: SerdeSecret(secret_share.clone()),
            public: secret_share.public_key_share(),
            public_key_set,
        }))
    }

    /// Returns the public key associated with this keypair.
    pub fn public_key(&self) -> PublicKey {
        match self {
            Self::Ed25519(keypair) => PublicKey::Ed25519(keypair.public),
            Self::BlsShare(keypair) => PublicKey::BlsShare(keypair.public),
        }
    }

    /// Returns the secret key associated with this keypair.
    pub fn secret_key(&self) -> Result<SecretKey> {
        match self {
            Self::Ed25519(keypair) => {
                let bytes = keypair.secret.to_bytes();
                match ed25519_dalek::SecretKey::from_bytes(&bytes) {
                    Ok(sk) => Ok(SecretKey::Ed25519(sk)),
                    Err(_) => Err(Error::FailedToParse(
                        "Could not deserialise Ed25519 secret key".to_string(),
                    )),
                }
            }
            Self::BlsShare(keypair) => Ok(SecretKey::BlsShare(keypair.secret.clone())),
        }
    }

    /// Signs with the underlying keypair.
    pub fn sign(&self, data: &[u8]) -> Signature {
        match self {
            Self::Ed25519(keypair) => Signature::Ed25519(keypair.sign(&data)),
            Self::BlsShare(keypair) => Signature::BlsShare(SignatureShare {
                index: keypair.index,
                share: keypair.secret.sign(data),
            }),
        }
    }
}

impl From<ed25519_dalek::SecretKey> for Keypair {
    fn from(secret: ed25519_dalek::SecretKey) -> Self {
        let keypair = ed25519_dalek::Keypair {
            public: (&secret).into(),
            secret,
        };

        Self::Ed25519(Arc::new(keypair))
    }
}

/// BLS keypair share.
#[derive(Clone, PartialEq, Serialize, Deserialize)]
pub struct BlsKeypairShare {
    /// Share index.
    pub index: usize,
    /// Secret key share.
    pub secret: SerdeSecret<threshold_crypto::SecretKeyShare>,
    /// Public key share.
    pub public: threshold_crypto::PublicKeyShare,
    /// Public key set. Necessary for producing proofs.
    pub public_key_set: PublicKeySet,
}

impl Debug for BlsKeypairShare {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "BlsKeyPairShare {{ index: {:?}, secret: {:?}, public: {:?}, public_key_set: PkSet {{ public_key: {:?} }} }}",
            self.index,
            self.secret,
            self.public,
            self.public_key_set.public_key()
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::utils;

    fn gen_keypairs() -> Vec<Keypair> {
        let mut rng = rand::thread_rng();
        let bls_secret_key = threshold_crypto::SecretKeySet::random(1, &mut rng);
        vec![
            Keypair::new_ed25519(&mut rng),
            Keypair::new_bls_share(
                0,
                bls_secret_key.secret_key_share(0),
                bls_secret_key.public_keys(),
            ),
        ]
    }

    // Test serialising and deserialising key pairs.
    #[test]
    fn serialisation_key_pair() -> Result<()> {
        let keypairs = gen_keypairs();

        for keypair in keypairs {
            let encoded = utils::serialise(&keypair)?;
            let decoded: Keypair = utils::deserialise(&encoded)?;

            assert_eq!(decoded, keypair);
        }

        Ok(())
    }
}