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
//! Fixed-sized (a.k.a. "raw") ECDSA signatures

use crate::curve::Curve;
use core::{
    convert::{TryFrom, TryInto},
    fmt::{self, Debug},
    ops::Add,
};
use generic_array::{typenum::Unsigned, ArrayLength, GenericArray};
use signature::Error;

/// Size of a fixed sized signature for the given elliptic curve.
pub type Size<ScalarSize> = <ScalarSize as Add>::Output;

/// Fixed-sized (a.k.a. "raw") ECDSA signatures generic over elliptic curves.
///
/// These signatures are serialized as fixed-sized big endian scalar values
/// with no additional framing.
#[derive(Clone, Eq, PartialEq)]
pub struct FixedSignature<C: Curve>
where
    Size<C::ScalarSize>: ArrayLength<u8>,
{
    bytes: GenericArray<u8, Size<C::ScalarSize>>,
}

impl<C: Curve> signature::Signature for FixedSignature<C>
where
    Size<C::ScalarSize>: ArrayLength<u8>,
{
    fn from_bytes(bytes: impl AsRef<[u8]>) -> Result<Self, Error> {
        bytes.as_ref().try_into()
    }
}

impl<C: Curve> AsRef<[u8]> for FixedSignature<C>
where
    Size<C::ScalarSize>: ArrayLength<u8>,
{
    fn as_ref(&self) -> &[u8] {
        self.bytes.as_slice()
    }
}

impl<C: Curve> Debug for FixedSignature<C>
where
    Size<C::ScalarSize>: ArrayLength<u8>,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "FixedSignature<{:?}> {{ bytes: {:?}) }}",
            C::default(),
            self.as_ref()
        )
    }
}

impl<'a, C: Curve> TryFrom<&'a [u8]> for FixedSignature<C>
where
    Size<C::ScalarSize>: ArrayLength<u8>,
{
    type Error = Error;

    fn try_from(bytes: &'a [u8]) -> Result<Self, Error> {
        if bytes.len() == <Size<C::ScalarSize>>::to_usize() {
            Ok(Self {
                bytes: GenericArray::clone_from_slice(bytes),
            })
        } else {
            Err(Error::new())
        }
    }
}

impl<C: Curve> From<GenericArray<u8, Size<C::ScalarSize>>> for FixedSignature<C>
where
    Size<C::ScalarSize>: ArrayLength<u8>,
{
    fn from(bytes: GenericArray<u8, Size<C::ScalarSize>>) -> Self {
        Self { bytes }
    }
}