helium-crypto 0.9.3

Helium Blockchain cryptography library
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
use crate::*;
use base64::{engine::general_purpose::STANDARD, Engine};
use multihash::{Multihash, MultihashDigest};
use std::{
    convert::TryFrom,
    fmt,
    hash::{Hash, Hasher},
};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {
    #[error("insufficient signatures {0}, expected {1}")]
    InsufficientSignatures(usize, u8),
    #[error("insufficient keys {0}, expected {1}")]
    InsufficientKeys(usize, u8),
    #[error("too many keys {0}, expected {1}")]
    TooManyKeys(usize, u8),
    #[error("not a multisig key")]
    NotMultisig,
    #[error("multihash error")]
    Multihash(multihash::Error),
    #[error("key digest error")]
    KeyDigest,
    #[error("not a multisig member: {0}")]
    NotMember(public_key::PublicKey),
}

impl Error {
    pub fn insufficient_signatures(actual: usize, expected: u8) -> crate::Error {
        Self::InsufficientSignatures(actual, expected).into()
    }

    pub fn insufficient_keys(actual: usize, expected: u8) -> crate::Error {
        Self::InsufficientKeys(actual, expected).into()
    }

    pub fn too_many_keys(actual: usize, expected: u8) -> crate::Error {
        Self::TooManyKeys(actual, expected).into()
    }

    pub fn not_multisig() -> crate::Error {
        Self::NotMultisig.into()
    }

    pub fn multihash(err: multihash::Error) -> crate::Error {
        Self::Multihash(err).into()
    }

    pub fn key_digest() -> crate::Error {
        Self::KeyDigest.into()
    }

    pub fn not_member(public_key: public_key::PublicKey) -> crate::Error {
        Self::NotMember(public_key).into()
    }
}

#[derive(Clone, Eq)]
pub struct PublicKey {
    pub(crate) m: u8,
    pub(crate) n: u8,
    pub(crate) keys_digest: Vec<u8>,
}

pub const PUBLIC_KEY_LENGTH: usize = 37;

#[derive(Debug, PartialEq, Eq)]
pub struct Signature {
    public_keys: Vec<public_key::PublicKey>,
    key_signatures: Vec<KeySignature>,
}

#[derive(PartialEq, Eq, PartialOrd, Ord)]
struct KeySignature {
    index: u8,
    signature: Vec<u8>,
}

impl fmt::Debug for KeySignature {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("KeySignature")
            .field("index", &self.index)
            .field("signature", &STANDARD.encode(&self.signature))
            .finish()
    }
}

impl PublicKey {
    pub fn generate(
        network: Network,
        m: u8,
        hash: multihash::Code,
        public_keys: &[public_key::PublicKey],
    ) -> Result<public_key::PublicKey> {
        let mut public_keys = public_keys.to_owned();
        public_key_sort(&mut public_keys);
        if public_keys.len() > u8::MAX as usize {
            return Err(Error::too_many_keys(public_keys.len(), u8::MAX));
        }
        let n = public_keys.len() as u8;
        let keys_digest = public_key_digest(&network, &public_keys, &hash)?;
        Ok(public_key::PublicKey::for_network(
            network,
            Self { m, n, keys_digest },
        ))
    }
}

impl Signature {
    pub fn new(
        public_key: &public_key::PublicKey,
        keys: &[public_key::PublicKey],
        signatures: &[(public_key::PublicKey, Vec<u8>)],
    ) -> Result<Self> {
        let network = public_key.network;
        let public_key = to_multisig(public_key).ok_or_else(Error::not_multisig)?;
        let mut public_keys = keys.to_owned();
        public_key_sort(&mut public_keys);
        match public_keys.len() {
            l if usize::from(public_key.m) > l => {
                return Err(Error::insufficient_signatures(
                    public_keys.len(),
                    public_key.m,
                ))
            }
            l if usize::from(public_key.n) > l => {
                return Err(Error::insufficient_keys(public_keys.len(), public_key.n))
            }
            l if usize::from(public_key.n) < l => {
                return Err(Error::too_many_keys(public_keys.len(), public_key.n))
            }
            _ => (),
        }
        let hash_type = Multihash::from_bytes(&public_key.keys_digest)
            .and_then(|hash| multihash::Code::try_from(hash.code()))
            .map_err(Error::multihash)?;
        if public_key_digest(&network, &public_keys, &hash_type)? != public_key.keys_digest {
            return Err(Error::key_digest());
        }

        let mut key_signatures = Vec::with_capacity(signatures.len());
        for (public_key, signature) in signatures {
            let key_signature = KeySignature::new(&public_keys, public_key, signature.clone())?;
            key_signatures.push(key_signature);
        }
        key_signature_sort(&mut key_signatures);

        Ok(Self {
            public_keys,
            key_signatures,
        })
    }

    fn from_input<R: std::io::Read>(public_key: &PublicKey, input: &mut R) -> Result<Self> {
        let mut public_keys = Vec::with_capacity(public_key.n.into());
        for _ in 0..public_key.n {
            let public_key = public_key::PublicKey::read_from(input)?;
            if to_multisig(&public_key).is_some() {
                return Err(crate::Error::invalid_keytype(KeyType::MultiSig.into()));
            }
            public_keys.push(public_key);
        }
        let mut key_signatures = Vec::with_capacity(public_key.m.into());
        loop {
            match KeySignature::read_from(input) {
                Ok(key_signature) => key_signatures.push(key_signature),
                Err(crate::Error::Io(err)) if err.kind() == std::io::ErrorKind::UnexpectedEof => {
                    break
                }
                Err(err) => return Err(err),
            }
        }
        key_signature_sort(&mut key_signatures);

        Ok(Self {
            public_keys,
            key_signatures,
        })
    }

    /// Returns the number of key signatures that successfully verified the
    /// given message
    fn verify(&self, msg: &[u8]) -> u8 {
        self.key_signatures
            .iter()
            .filter(|key_signature| {
                let public_key = &self.public_keys[key_signature.index as usize];
                public_key.verify(msg, &key_signature.signature).is_ok()
            })
            .count() as u8
    }

    pub fn to_vec(&self) -> Vec<u8> {
        let mut data = Vec::new();
        let mut cursor = std::io::Cursor::new(&mut data);
        self.write_to(&mut cursor).unwrap();
        data.to_vec()
    }
}

impl KeySignature {
    fn new(
        public_keys: &[public_key::PublicKey],
        public_key: &public_key::PublicKey,
        signature: Vec<u8>,
    ) -> Result<Self> {
        if to_multisig(public_key).is_some() {
            return Err(crate::Error::invalid_keytype(public_key.key_type().into()));
        }
        let index = public_keys
            .iter()
            .position(|k| k == public_key)
            .ok_or_else(|| Error::not_member(public_key.clone()))?;
        Ok(Self {
            index: index as u8,
            signature,
        })
    }
}

fn public_key_digest(
    network: &Network,
    keys: &[public_key::PublicKey],
    hash_type: &multihash::Code,
) -> Result<Vec<u8>> {
    let mut keys_bin = Vec::new();
    for key in keys.iter() {
        if key.network != *network {
            return Err(crate::Error::invalid_network());
        }
        if key.key_type() == KeyType::MultiSig {
            return Err(crate::Error::invalid_keytype(key.key_type().into()));
        }
        keys_bin.extend_from_slice(&key.to_vec());
    }
    Ok(hash_type.digest(&keys_bin).to_bytes())
}

fn public_key_sort(keys: &mut Vec<public_key::PublicKey>) {
    keys.sort_unstable_by_key(|a| a.to_string());
    keys.dedup();
}

fn key_signature_sort(key_signatures: &mut Vec<KeySignature>) {
    key_signatures.sort_unstable();
    key_signatures.dedup();
}

fn to_multisig(public_key: &public_key::PublicKey) -> Option<&PublicKey> {
    match &public_key.inner {
        public_key::PublicKeyRepr::MultiSig(key) => Some(key),
        _ => None,
    }
}

impl PublicKeySize for PublicKey {
    const PUBLIC_KEY_SIZE: usize = PUBLIC_KEY_LENGTH;
}

impl AsRef<[u8]> for PublicKey {
    fn as_ref(&self) -> &[u8] {
        self.keys_digest.as_ref()
    }
}

impl WriteTo for PublicKey {
    fn write_to<W: std::io::Write>(&self, output: &mut W) -> std::io::Result<()> {
        output.write_all(&[self.m])?;
        output.write_all(&[self.n])?;
        output.write_all(&self.keys_digest)
    }
}

impl PartialEq for PublicKey {
    fn eq(&self, other: &Self) -> bool {
        self.keys_digest == other.keys_digest
    }
}

impl PartialOrd for PublicKey {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for PublicKey {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.keys_digest.cmp(&other.keys_digest)
    }
}

impl Hash for PublicKey {
    fn hash<H: Hasher>(&self, state: &mut H) {
        state.write(self.as_ref())
    }
}

impl fmt::Debug for PublicKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("PublicKey")
            .field("m", &self.m)
            .field("n", &self.n)
            .field("key_digest", &STANDARD.encode(&self.keys_digest))
            .finish()
    }
}

impl TryFrom<&[u8]> for PublicKey {
    type Error = crate::Error;

    fn try_from(input: &[u8]) -> Result<Self> {
        let mut input = std::io::Cursor::new(&input[1..]);
        Self::read_from(&mut input)
    }
}

impl ReadFrom for PublicKey {
    fn read_from<R: std::io::Read>(input: &mut R) -> Result<Self> {
        let mut buf = [0u8; PUBLIC_KEY_LENGTH - 1];
        input.read_exact(&mut buf)?;
        let m = buf[0];
        let n = buf[1];
        let keys_digest = buf[2..].to_vec();
        Ok(Self { m, n, keys_digest })
    }
}

impl public_key::Verify for PublicKey {
    fn verify(&self, msg: &[u8], signature: &[u8]) -> Result {
        let mut input = std::io::Cursor::new(signature);
        let signature = Signature::from_input(self, &mut input)?;
        if signature.verify(msg) >= self.m {
            return Ok(());
        }
        Err(signature::Error::new().into())
    }
}

impl WriteTo for KeySignature {
    fn write_to<W: std::io::Write>(&self, output: &mut W) -> std::io::Result<()> {
        output.write_all(&[self.index, self.signature.len() as u8])?;
        output.write_all(self.signature.as_ref())
    }
}

impl ReadFrom for KeySignature {
    fn read_from<R: std::io::Read>(input: &mut R) -> Result<Self> {
        let mut buf = [0u8; 2];
        input.read_exact(&mut buf)?;
        let index = buf[0];
        let mut signature = vec![0u8; usize::from(buf[1])];
        input.read_exact(&mut signature)?;
        Ok(Self { index, signature })
    }
}

impl WriteTo for Signature {
    fn write_to<W: std::io::Write>(&self, output: &mut W) -> std::io::Result<()> {
        for public_key in &self.public_keys {
            output.write_all(&public_key.to_vec())?;
        }
        for key_signature in &self.key_signatures {
            key_signature.write_to(output)?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rand::{rngs::OsRng, seq::SliceRandom};

    fn gen_keys(n: u8) -> Vec<crate::Keypair> {
        let key_type = [KeyType::EccCompact, KeyType::Ed25519]
            .choose(&mut OsRng)
            .unwrap();
        let key_tag = KeyTag {
            network: Network::MainNet,
            key_type: *key_type,
        };
        (0..n)
            .map(|_| crate::Keypair::generate(key_tag, &mut OsRng))
            .collect()
    }

    fn public_keys(keypairs: &[crate::Keypair]) -> Vec<public_key::PublicKey> {
        keypairs
            .iter()
            .map(|keypair| keypair.public_key().to_owned())
            .collect()
    }

    #[test]
    fn pubkey_bytes_roundtrip() {
        let public_keys = public_keys(&gen_keys(2));

        let pubkey = super::PublicKey::generate(
            Network::MainNet,
            1,
            multihash::Code::Sha2_256,
            &public_keys,
        )
        .expect("multisig pubkey");

        let bytes = pubkey.to_vec();
        assert_eq!(
            pubkey,
            public_key::PublicKey::try_from(&bytes[..]).expect("multisig pubkey")
        );
    }

    #[test]
    fn erlang_interop() {
        // Take two keys generated in the libp2p_crypto multisig implementation
        // and ensure it generates the same multisig key as the erlang side did
        let external_keys: Vec<public_key::PublicKey> = [
            "11MJXxoWFp2bMsqKM6QZin6ync9DQ3fjjFjUrFiRCaKunmBEBhK",
            "11x7jP9yAnyk5jeYywmsYDFdYq5xvKLKjP2zjhGzCwDSQtxcUDt",
        ]
        .iter()
        .map(|s| s.parse().expect("public key"))
        .collect();

        let pubkey = super::PublicKey::generate(
            Network::MainNet,
            1,
            multihash::Code::Sha2_256,
            &external_keys,
        )
        .expect("multisig pubkey");
        assert_eq!(
            "1SVRdbaAev7zSpUsMjvQrbRBGFHLXEa63SGntYCqChC4CTpqwftTPGbZ",
            pubkey.to_string()
        );
    }

    #[test]
    fn sign_test() {
        let keys = gen_keys(3);
        let public_keys = public_keys(&keys);
        const MSG: &[u8] = b"hello world";

        assert_eq!(3, public_keys.len());
        let pubkey = super::PublicKey::generate(
            Network::MainNet,
            2,
            multihash::Code::Sha2_256,
            &public_keys,
        )
        .expect("multisig pubkey");

        let signatures: Vec<(public_key::PublicKey, Vec<u8>)> = keys[0..2]
            .iter()
            .map(|key| {
                (
                    key.public_key().to_owned(),
                    key.sign(MSG).expect("signature"),
                )
            })
            .collect();
        assert_eq!(2, signatures.len());

        let signature = Signature::new(&pubkey, &public_keys, &signatures).expect("signature");

        pubkey
            .verify(MSG, &signature.to_vec())
            .expect("verify success");
    }
}