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
//! The hashing and signing algorithms.

/// The default hasher, the exact type depends on the feature flags enabled.
pub type DefaultHasher = DefaultHasherNoDoc;

/// The default signer, the exact type depends on the feature flags enabled.
pub type DefaultSigner = DefaultSignerNoDoc;

/// The default secret key, the exact type depends on the feature flags enabled.
pub type DefaultSecretKey = DefaultSecretKeyNoDoc;

include!("algorithms/macro_impl.rs");
macro_rules! algorithms {
    ($($tt:tt)+) => {
        algorithms_impl!($($tt)+);
    };
}

algorithms! {
    /// A hashing algorithm.
    HashAlgorithm {
        /// SHA2 256 bit hashing.
        Sha2 = (1, "SHA2_256"),

        /// SHA3 256 bit hashing.
        Sha3 = (3, "SHA3_256"),
    }

    /// A signature algorithm.
    SignatureAlgorithm {
        /// P256 / secp256r1 curve.
        P256 = (2, "ECDSA_P256"),

        /// secp256k1 curve.
        Secp256k1 = (3, "ECDSA_secp256k1"),
    }
}

/// A signature.
pub trait Signature {
    /// Serialized form of the signature
    type Serialized: AsRef<[u8]> + Clone;

    /// Serializes the signature.
    fn serialize(&self) -> Self::Serialized;
}

/// A hasher.
pub trait FlowHasher {
    /// The algorithm of this hasher.
    type Algorithm: HashAlgorithm;

    /// Creates a new hasher.
    fn new() -> Self;

    /// Updates the hasher with bytes.
    fn update<B: AsRef<[u8]> + ?Sized>(&mut self, bytes: &B);

    /// Finalize the hasher, returns the 256 bit hash.
    fn finalize(self) -> [u8; 32];
}

/// A signature signer.
pub trait FlowSigner {
    /// The algorithm of this signer.
    type Algorithm: SignatureAlgorithm;

    /// The secret key used by this signer.
    type SecretKey: Clone;

    /// The public key used by this signer.
    type PublicKey: Copy;

    /// The signature type produced by this signer.
    type Signature: Signature;

    /// Creates a new signer.
    fn new() -> Self;

    /// Creates a signature by consuming a populated hasher and a secret key.
    fn sign(&self, hasher: impl FlowHasher, secret_key: &Self::SecretKey) -> Self::Signature {
        self.sign_populated(hasher.finalize(), secret_key)
    }

    /// Signs a 256 bit hashed data with the secret key.
    fn sign_populated(&self, hashed: [u8; 32], secret_key: &Self::SecretKey) -> Self::Signature;

    /// Converts a secret key to its public counterpart.
    fn to_public_key(&self, secret_key: &Self::SecretKey) -> Self::PublicKey;

    /// Serializes a public key. Excluding the leading 0x04.
    fn serialize_public_key(&self, public_key: &Self::PublicKey) -> [u8; 64];
}

/// A secret key.
pub trait SecretKey {
    /// The signer associated to this secret key.
    type Signer: FlowSigner<SecretKey = Self>;
}

#[cfg(feature = "secp256k1-sign")]
/// Re-exports items from the `secp256k1` crate.
pub mod secp256k1 {
    pub use secp256k1::*;
}

#[cfg(feature = "sha3-hash")]
/// Re-exports items from the `tiny_keccak` crate.
pub mod sha3 {
    pub use tiny_keccak::*;
}

#[cfg(feature = "secp256k1-rand")]
/// Re-exports items from the `rand` crate.
pub mod rand {
    pub use rand::*;
}

#[cfg(feature = "sha3-hash")]
impl FlowHasher for tiny_keccak::Sha3 {
    type Algorithm = Sha3;
    fn new() -> Self {
        tiny_keccak::Sha3::v256()
    }

    fn update<B: AsRef<[u8]> + ?Sized>(&mut self, bytes: &B) {
        use tiny_keccak::Hasher;
        Hasher::update(self, bytes.as_ref())
    }

    fn finalize(self) -> [u8; 32] {
        use tiny_keccak::Hasher;
        let mut output = [0; 32];
        Hasher::finalize(self, &mut output);
        output
    }
}

#[cfg(feature = "secp256k1-sign")]
impl Signature for secp256k1::Signature {
    type Serialized = [u8; 64];

    fn serialize(&self) -> Self::Serialized {
        self.serialize_compact()
    }
}

#[cfg(feature = "secp256k1-sign")]
impl FlowSigner for secp256k1::Secp256k1<secp256k1::SignOnly> {
    type Algorithm = Secp256k1;

    type PublicKey = secp256k1::PublicKey;

    type SecretKey = secp256k1::SecretKey;

    type Signature = secp256k1::Signature;

    fn new() -> Self {
        Self::signing_only()
    }

    fn sign_populated(&self, hashed: [u8; 32], secret_key: &Self::SecretKey) -> Self::Signature {
        struct Ttbh([u8; 32]);
        impl secp256k1::ThirtyTwoByteHash for Ttbh {
            fn into_32(self) -> [u8; 32] {
                self.0
            }
        }
        self.sign(&secp256k1::Message::from(Ttbh(hashed)), secret_key)
    }

    fn to_public_key(&self, secret_key: &Self::SecretKey) -> Self::PublicKey {
        secp256k1::PublicKey::from_secret_key(self, secret_key)
    }

    fn serialize_public_key(&self, public_key: &Self::PublicKey) -> [u8; 64] {
        let [_, rest @ ..] = public_key.serialize_uncompressed();
        rest
    }
}

#[cfg(feature = "secp256k1-sign")]
impl SecretKey for secp256k1::SecretKey {
    type Signer = secp256k1::Secp256k1<secp256k1::SignOnly>;
}

#[cfg(feature = "sha3-hash")]
type DefaultHasherNoDoc = tiny_keccak::Sha3;

#[cfg(feature = "secp256k1-sign")]
type DefaultSignerNoDoc = secp256k1::Secp256k1<secp256k1::SignOnly>;

#[cfg(feature = "secp256k1-sign")]
type DefaultSecretKeyNoDoc = secp256k1::SecretKey;

#[cfg(not(any(feature = "sha3-hash")))]
type DefaultHasherNoDoc = NoDefaultHasherAvailable;

#[cfg(not(any(feature = "sha3-hash")))]
type DefaultSignerNoDoc = NoDefaultSignerAvailable;

#[cfg(not(any(feature = "sha3-hash")))]
#[doc(hidden)]
pub struct NoDefaultHasherAvailable;

#[cfg(not(any(feature = "secp256k1-sign")))]
#[doc(hidden)]
pub struct NoDefaultSignerAvailable;

#[cfg(not(any(feature = "secp256k1-sign")))]
#[doc(hidden)]
pub struct NoDefaultSecretKeyAvailable;