nam_reddsa/
signature.rs

1// -*- mode: rust; -*-
2//
3// This file is part of reddsa.
4// Copyright (c) 2019-2021 Zcash Foundation
5// See LICENSE for licensing information.
6//
7// Authors:
8// - Henry de Valence <hdevalence@hdevalence.ca>
9// - Conrado Gouvea <conradoplg@gmail.com>
10
11//! RedDSA Signatures
12use core::{fmt, marker::PhantomData};
13
14use crate::{hex_if_possible, SigType};
15
16/// A RedDSA signature.
17#[derive(Copy, Clone, Eq, PartialEq)]
18#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
19pub struct Signature<T: SigType> {
20    pub(crate) r_bytes: [u8; 32],
21    pub(crate) s_bytes: [u8; 32],
22    pub(crate) _marker: PhantomData<T>,
23}
24
25impl<T: SigType> fmt::Debug for Signature<T> {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        f.debug_struct("Signature")
28            .field("r_bytes", &hex_if_possible(&self.r_bytes))
29            .field("s_bytes", &hex_if_possible(&self.s_bytes))
30            .finish()
31    }
32}
33
34impl<T: SigType> From<[u8; 64]> for Signature<T> {
35    fn from(bytes: [u8; 64]) -> Signature<T> {
36        let mut r_bytes = [0; 32];
37        r_bytes.copy_from_slice(&bytes[0..32]);
38        let mut s_bytes = [0; 32];
39        s_bytes.copy_from_slice(&bytes[32..64]);
40        Signature {
41            r_bytes,
42            s_bytes,
43            _marker: PhantomData,
44        }
45    }
46}
47
48impl<T: SigType> From<Signature<T>> for [u8; 64] {
49    fn from(sig: Signature<T>) -> [u8; 64] {
50        let mut bytes = [0; 64];
51        bytes[0..32].copy_from_slice(&sig.r_bytes[..]);
52        bytes[32..64].copy_from_slice(&sig.s_bytes[..]);
53        bytes
54    }
55}