ckb_crypto/secp/
generator.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
use super::privkey::Privkey;
use super::pubkey::Pubkey;
use super::SECP256K1;
use rand::{self, Rng, SeedableRng};
use secp256k1::{PublicKey, SecretKey};

/// A random secp keypair generator
pub struct Generator {
    rng: Box<dyn rand::RngCore>,
}

impl Default for Generator {
    fn default() -> Self {
        Self::new()
    }
}

impl Generator {
    /// Create a new Generator
    ///
    /// Default random number generator is `rand::rngs::ThreadRng`
    pub fn new() -> Self {
        let rng = rand::thread_rng();
        Generator { rng: Box::new(rng) }
    }

    /// Non crypto safe prng, should only used in tests
    pub fn non_crypto_safe_prng(seed: u64) -> Self {
        let rng = rand::rngs::SmallRng::seed_from_u64(seed);
        Generator { rng: Box::new(rng) }
    }

    /// Generate a SecretKey
    fn gen_secret_key(&mut self) -> SecretKey {
        let mut seed = vec![0; 32];
        loop {
            self.rng.fill(seed.as_mut_slice());
            if let Ok(key) = SecretKey::from_slice(&seed) {
                return key;
            }
        }
    }

    /// Generate a Privkey
    pub fn gen_privkey(&mut self) -> Privkey {
        self.gen_secret_key().into()
    }

    /// Generate a keypair
    pub fn gen_keypair(&mut self) -> (Privkey, Pubkey) {
        let secret_key = self.gen_secret_key();
        let pubkey = PublicKey::from_secret_key(&*SECP256K1, &secret_key);

        (secret_key.into(), pubkey.into())
    }

    /// Shortcuts construct temporary Generator, and generate a Privkey
    pub fn random_privkey() -> Privkey {
        Generator::new().gen_privkey()
    }

    /// Shortcuts construct temporary Generator, and generate a keypair
    pub fn random_keypair() -> (Privkey, Pubkey) {
        Generator::new().gen_keypair()
    }

    /// Shortcuts construct temporary Generator, and generate a SecretKey
    pub fn random_secret_key() -> SecretKey {
        Generator::new().gen_secret_key()
    }
}