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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
//! A set of types and functions related to cryptography, that are widely used in the entire Simperby project.
use secp256k1::{
    ecdsa::{RecoverableSignature, RecoveryId},
    Message, Secp256k1, SecretKey,
};
use serde::{ser::SerializeTuple, Deserialize, Serialize};
use sha3::{Digest, Keccak256};
use std::fmt;
use thiserror::Error;

const EVM_EC_RECOVERY_OFFSET: u8 = 27;

#[derive(Error, Debug, Clone)]
pub enum CryptoError {
    /// When the data format is not valid.
    #[error("invalid format: {0}")]
    InvalidFormat(String),
    #[error("verification failed")]
    VerificationFailed,
}

type Error = CryptoError;

pub trait ToHash256 {
    fn to_hash256(&self) -> Hash256;
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Hash, Copy)]
pub struct HexSerializedBytes<const N: usize> {
    pub data: [u8; N],
}

impl<const N: usize> HexSerializedBytes<N> {
    const fn zero() -> Self {
        Self { data: [0; N] }
    }
}

impl<const N: usize> Serialize for HexSerializedBytes<N> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::ser::Serializer,
    {
        if serializer.is_human_readable() {
            serializer.serialize_str(hex::encode(self.data).as_str())
        } else {
            let mut seq = serializer.serialize_tuple(N)?;
            for e in self.data {
                seq.serialize_element(&e)?;
            }
            seq.end()
        }
    }
}

impl<const N: usize> fmt::Debug for HexSerializedBytes<N> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", hex::encode(self.data).as_str())
    }
}

impl<const N: usize> fmt::Display for HexSerializedBytes<N> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", hex::encode(self.data).as_str())
    }
}

impl<'de, const N: usize> Deserialize<'de> for HexSerializedBytes<N> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::de::Deserializer<'de>,
    {
        if deserializer.is_human_readable() {
            let s: String = Deserialize::deserialize(deserializer)?;
            let bytes = hex::decode(s).map_err(|e| serde::de::Error::custom(e.to_string()))?;
            if bytes.len() != N {
                return Err(serde::de::Error::custom("invalid length"));
            }
            let mut data = [0; N];
            data.copy_from_slice(&bytes);
            Ok(HexSerializedBytes { data })
        } else {
            struct V<const M: usize>;
            impl<'de, const M: usize> serde::de::Visitor<'de> for V<M> {
                type Value = [u8; M];

                fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                    formatter.write_str("byte")
                }

                fn visit_seq<S: serde::de::SeqAccess<'de>>(
                    self,
                    mut seq: S,
                ) -> Result<Self::Value, S::Error> {
                    let mut data = [0; M];
                    for (i, x) in data.iter_mut().enumerate() {
                        *x = seq
                            .next_element()?
                            .ok_or_else(|| serde::de::Error::invalid_length(i, &self))?;
                    }
                    Ok(data)
                }
            }
            let data = deserializer.deserialize_tuple(N, V::<N>)?;
            Ok(HexSerializedBytes { data })
        }
    }
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
pub struct HexSerializedVec {
    pub data: Vec<u8>,
}

impl From<Vec<u8>> for HexSerializedVec {
    fn from(data: Vec<u8>) -> Self {
        Self { data }
    }
}

impl<const N: usize> From<[u8; N]> for HexSerializedVec {
    fn from(data: [u8; N]) -> Self {
        Self {
            data: data.to_vec(),
        }
    }
}

impl<const N: usize> From<HexSerializedBytes<N>> for HexSerializedVec {
    fn from(data: HexSerializedBytes<N>) -> Self {
        Self {
            data: data.data.to_vec(),
        }
    }
}

impl Serialize for HexSerializedVec {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::ser::Serializer,
    {
        if serializer.is_human_readable() {
            serializer.serialize_str(hex::encode(&self.data).as_str())
        } else {
            let mut seq = serializer.serialize_tuple(self.data.len())?;
            for e in &self.data {
                seq.serialize_element(&e)?;
            }
            seq.end()
        }
    }
}

impl fmt::Debug for HexSerializedVec {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", hex::encode(&self.data).as_str())
    }
}

impl fmt::Display for HexSerializedVec {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", hex::encode(&self.data).as_str())
    }
}

impl<'de> Deserialize<'de> for HexSerializedVec {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::de::Deserializer<'de>,
    {
        if deserializer.is_human_readable() {
            let s: String = Deserialize::deserialize(deserializer)?;
            let data = hex::decode(s).map_err(|e| serde::de::Error::custom(e.to_string()))?;
            Ok(HexSerializedVec { data })
        } else {
            let data: Vec<u8> = Deserialize::deserialize(deserializer)?;
            Ok(HexSerializedVec { data })
        }
    }
}

/// A cryptographic hash.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Hash, Copy, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Hash256 {
    pub hash: HexSerializedBytes<32>,
}

impl Hash256 {
    pub const fn zero() -> Self {
        Hash256 {
            hash: HexSerializedBytes::zero(),
        }
    }

    /// Hashes the given data.
    pub fn hash(data: impl AsRef<[u8]>) -> Self {
        let mut hasher = Keccak256::new();
        hasher.update(data);
        let result = hasher.finalize();
        Hash256 {
            hash: HexSerializedBytes {
                data: result.as_slice().try_into().unwrap(),
            },
        }
    }

    pub fn from_array(data: [u8; 32]) -> Self {
        Hash256 {
            hash: HexSerializedBytes { data },
        }
    }

    pub fn aggregate(&self, other: &Self) -> Self {
        Self::hash([self.hash.data, other.hash.data].concat())
    }
}

impl std::convert::AsRef<[u8]> for Hash256 {
    fn as_ref(&self) -> &[u8] {
        &self.hash.data
    }
}

impl fmt::Display for Hash256 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.hash)
    }
}

/// A cryptographic signature.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Signature {
    signature: HexSerializedBytes<65>,
}

impl Signature {
    pub const fn zero() -> Self {
        Signature {
            signature: HexSerializedBytes { data: [0; 65] },
        }
    }

    /// Creates a new signature from the given data and keys.
    pub fn sign(data: Hash256, private_key: &PrivateKey) -> Result<Self, Error> {
        let private_key = secp256k1::SecretKey::from_slice(&private_key.key.data)
            .map_err(|_| Error::InvalidFormat("private key: [omitted]".to_owned()))?;
        let message = Message::from_slice(data.as_ref()).unwrap();
        let (recovery_id, rs) = Secp256k1::signing_only()
            .sign_ecdsa_recoverable(&message, &private_key)
            .serialize_compact();
        let v = recovery_id.to_i32() as u8;
        let bytes: [u8; 65] = {
            let mut whole: [u8; 65] = [0; 65];
            let (left, right) = whole.split_at_mut(rs.len());
            left.copy_from_slice(&rs);
            right.copy_from_slice(&[v + EVM_EC_RECOVERY_OFFSET; 1]);
            whole
        };
        Ok(Signature {
            signature: HexSerializedBytes { data: bytes },
        })
    }

    /// Verifies the signature against the given data and public key.
    pub fn verify(&self, data: Hash256, public_key: &PublicKey) -> Result<(), Error> {
        let signature = secp256k1::ecdsa::Signature::from_compact(&self.signature.data[0..64])
            .map_err(|_| Error::InvalidFormat(format!("signature: {self}")))?;
        let public_key = secp256k1::PublicKey::from_slice(&public_key.key.data)
            .map_err(|_| Error::InvalidFormat(format!("public_key: {public_key}")))?;
        let message = Message::from_slice(data.as_ref()).unwrap();
        Secp256k1::verification_only()
            .verify_ecdsa(&message, &signature, &public_key)
            .map_err(|_| Error::VerificationFailed)
    }

    /// Recover a public key from the given signature.
    pub fn recover(&self, data: Hash256) -> Result<PublicKey, Error> {
        let message = Message::from_slice(data.as_ref()).unwrap();
        let recovery_id = RecoveryId::from_i32(
            self.signature.data[64..65][0] as i32 - EVM_EC_RECOVERY_OFFSET as i32,
        )
        .map_err(|e| Error::InvalidFormat(e.to_string()))?;
        if recovery_id.to_i32() != 0 && recovery_id.to_i32() != 1 {
            return Err(Error::VerificationFailed);
        }
        let signature =
            RecoverableSignature::from_compact(&self.signature.data[0..64], recovery_id)
                .map_err(|e| Error::InvalidFormat(e.to_string()))?;
        let secp = Secp256k1::new();
        let public_key = secp
            .recover_ecdsa(&message, &signature)
            .map_err(|e| Error::InvalidFormat(e.to_string()))?
            .serialize();
        PublicKey::from_array(public_key)
    }

    /// Constructs a signature from the given bytes, but does not verify its validity.
    pub fn from_array(bytes: [u8; 65]) -> Self {
        Signature {
            signature: HexSerializedBytes { data: bytes },
        }
    }
}

/// A signature that is explicitly marked with the type of the signed data.
///
/// This implies that the signature is created on `Hash256::hash(serde_spb::to_vec(T).unwrap())`.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Hash, Serialize, Deserialize)]
pub struct TypedSignature<T> {
    signature: Signature,
    signer: PublicKey,
    #[serde(skip)]
    _mark: std::marker::PhantomData<T>,
}

impl<T: ToHash256> TypedSignature<T> {
    /// Creates a new signature from the given data and keys.
    pub fn sign(data: &T, private_key: &PrivateKey) -> Result<Self, Error> {
        let data = data.to_hash256();
        Signature::sign(data, private_key).map(|signature| TypedSignature {
            signature,
            signer: private_key.public_key(),
            _mark: std::marker::PhantomData,
        })
    }

    pub fn new(signature: Signature, signer: PublicKey) -> Self {
        TypedSignature {
            signature,
            signer,
            _mark: std::marker::PhantomData,
        }
    }

    pub fn signer(&self) -> &PublicKey {
        &self.signer
    }

    /// Verifies the signature against the given data and public key.
    pub fn verify(&self, data: &T) -> Result<(), Error> {
        let data = data.to_hash256();
        self.signature.verify(data, &self.signer)
    }

    pub fn get_raw_signature(&self) -> Signature {
        self.signature.clone()
    }
}

impl std::convert::AsRef<[u8]> for Signature {
    fn as_ref(&self) -> &[u8] {
        &self.signature.data
    }
}

impl fmt::Display for Signature {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.signature)
    }
}

/// A public key.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct PublicKey {
    key: HexSerializedBytes<65>,
}

impl std::convert::AsRef<[u8]> for PublicKey {
    fn as_ref(&self) -> &[u8] {
        &self.key.data
    }
}

impl fmt::Display for PublicKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.key)
    }
}

impl PublicKey {
    pub fn zero() -> Self {
        Self {
            key: HexSerializedBytes::zero(),
        }
    }

    pub fn from_array_uncompressed(array: [u8; 65]) -> Result<Self, Error> {
        let key = secp256k1::PublicKey::from_slice(array.as_ref())
            .map_err(|_| Error::InvalidFormat(format!("given bytes: {}", hex::encode(array))))?
            .serialize_uncompressed();
        Ok(PublicKey {
            key: HexSerializedBytes { data: key },
        })
    }

    pub fn from_array(array: [u8; 33]) -> Result<Self, Error> {
        let key = secp256k1::PublicKey::from_slice(array.as_ref())
            .map_err(|_| Error::InvalidFormat(format!("given bytes: {}", hex::encode(array))))?
            .serialize_uncompressed();
        Ok(PublicKey {
            key: HexSerializedBytes { data: key },
        })
    }
}

/// A private key.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct PrivateKey {
    pub key: HexSerializedBytes<32>,
}

impl std::convert::AsRef<[u8]> for PrivateKey {
    fn as_ref(&self) -> &[u8] {
        &self.key.data
    }
}

impl PrivateKey {
    pub fn zero() -> Self {
        Self {
            key: HexSerializedBytes::zero(),
        }
    }

    pub fn from_array(array: [u8; 32]) -> Result<Self, Error> {
        let key = secp256k1::SecretKey::from_slice(&array)
            .map_err(|_| Error::InvalidFormat(format!("given bytes: {}", hex::encode(array))))?
            .secret_bytes();
        Ok(PrivateKey {
            key: HexSerializedBytes { data: key },
        })
    }

    pub fn public_key(&self) -> PublicKey {
        let private_key = SecretKey::from_slice(&self.key.data).expect("invalid private key");
        let secp = Secp256k1::new();
        let public_key = private_key.public_key(&secp);
        PublicKey::from_array(public_key.serialize()).expect("invalid public key")
    }
}

/// Checks whether the given public and private keys match.
pub fn check_keypair_match(public_key: &PublicKey, private_key: &PrivateKey) -> Result<(), Error> {
    let msg = "Some Random Message".as_bytes();
    let signature = Signature::sign(Hash256::hash(msg), private_key)?;
    signature.verify(Hash256::hash(msg), public_key)
}

/// Generates a new keypair using the seed.
pub fn generate_keypair(seed: impl AsRef<[u8]>) -> (PublicKey, PrivateKey) {
    let mut seed_: [u8; 32] = [0; 32];
    for (i, x) in Hash256::hash(seed).as_ref()[0..32].iter().enumerate() {
        seed_[i] = *x;
    }
    use secp256k1::rand::SeedableRng;
    let mut rng = secp256k1::rand::rngs::StdRng::from_seed(seed_);
    let secp = Secp256k1::new();
    let (private_key, public_key) = secp.generate_keypair(&mut rng);
    (
        PublicKey::from_array(public_key.serialize()).expect("invalid public key"),
        PrivateKey::from_array(private_key.secret_bytes()).expect("invalid private key"),
    )
}

/// Generates a new keypair randomly
pub fn generate_keypair_random() -> (PublicKey, PrivateKey) {
    use secp256k1::rand::SeedableRng;
    let mut rng = secp256k1::rand::rngs::StdRng::from_entropy();
    let secp = Secp256k1::new();
    let (private_key, public_key) = secp.generate_keypair(&mut rng);
    (
        PublicKey::from_array(public_key.serialize()).expect("invalid public key"),
        PrivateKey::from_array(private_key.secret_bytes()).expect("invalid private key"),
    )
}

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

    #[test]
    fn pretty_format() {
        let hash = Hash256::hash("hello world");
        assert_eq!(hash.to_string().len(), 64);
        let encoded = serde_spb::to_string(&hash).unwrap();
        assert_eq!(encoded.len(), 66);
        let (public_key, private_key) = generate_keypair("hello world");
        let signature = Signature::sign(hash, &private_key).unwrap();
        let encoded = serde_spb::to_string(&signature).unwrap();
        assert_eq!(encoded.len(), 132);
        let encoded = serde_spb::to_string(&public_key).unwrap();
        assert_eq!(encoded.len(), 132);
        let encoded = serde_spb::to_string(&private_key).unwrap();
        assert_eq!(encoded.len(), 66);
    }

    #[test]
    fn hash_encode_decode() {
        let hash = Hash256::hash("hello world");
        let encoded = serde_spb::to_string(&hash).unwrap();
        let decoded = serde_spb::from_str(&encoded).unwrap();
        assert_eq!(hash, decoded);
    }

    #[test]
    fn hash_encode_decode_zero() {
        let hash = Hash256::zero();
        let encoded = serde_spb::to_string(&hash).unwrap();
        let decoded = serde_spb::from_str(&encoded).unwrap();
        assert_eq!(hash, decoded);
    }

    #[test]
    fn key_encode_decode() {
        let (public_key, private_key) = generate_keypair("hello world");
        let encoded = serde_spb::to_string(&public_key).unwrap();
        let decoded = serde_spb::from_str(&encoded).unwrap();
        assert_eq!(public_key, decoded);
        let encoded = serde_spb::to_string(&private_key).unwrap();
        let decoded = serde_spb::from_str(&encoded).unwrap();
        assert_eq!(private_key, decoded);
    }

    #[test]
    fn signature_encode_decode() {
        let (public_key, private_key) = generate_keypair("hello world");
        let signature = Signature::sign(Hash256::hash("hello world"), &private_key).unwrap();
        let encoded = serde_spb::to_string(&signature).unwrap();
        let decoded = serde_spb::from_str(&encoded).unwrap();
        assert_eq!(signature, decoded);
        signature
            .verify(Hash256::hash("hello world"), &public_key)
            .unwrap();
    }

    #[test]
    fn signature_verify() {
        let (public_key, private_key) = generate_keypair("hello world");
        let signature = Signature::sign(Hash256::hash("hello world"), &private_key).unwrap();
        signature
            .verify(Hash256::hash("hello world"), &public_key)
            .unwrap();
    }

    #[test]
    fn signature_verify_invalid() {
        let (public_key, private_key) = generate_keypair("hello world");
        let signature = Signature::sign(Hash256::hash("hello world2"), &private_key).unwrap();
        signature
            .verify(Hash256::hash("hello world"), &public_key)
            .unwrap_err();
    }

    #[test]
    fn compressed() {
        let public_key = "0479c0e6973634b801da80fdf9274c13e327880e6360ca7735877f16e6a903c811afc2f0bb2c17de59110b022956dee0d625a694132b0da03fbba8ccdca219657c";
        let private_key = "f54a850441ef31968ffda8ea2ebdd831f0764c6bd52cd5185c8cb35d407201a4";
        let public_key = hex::decode(public_key).unwrap();
        let private_key = hex::decode(private_key).unwrap();
        let public_key =
            PublicKey::from_array_uncompressed(public_key.as_slice().try_into().unwrap()).unwrap();
        let private_key =
            PrivateKey::from_array(private_key.as_slice().try_into().unwrap()).unwrap();
        check_keypair_match(&public_key, &private_key).unwrap();
    }

    #[test]
    fn recover_public_key() {
        let (public_key, private_key) = generate_keypair("hello world");
        let signature = Signature::sign(Hash256::hash("hello world2"), &private_key).unwrap();
        let recovered = signature.recover(Hash256::hash("hello world2")).unwrap();
        assert_eq!(
            hex::encode(public_key.as_ref()),
            hex::encode(recovered.as_ref())
        );
    }
}