dkls23/setup/
keys.rs

1// Copyright (c) Silence Laboratories Pte. Ltd. All Rights Reserved.
2// This software is licensed under the Silence Laboratories License Agreement.
3
4#![allow(missing_docs)]
5
6use std::sync::Arc;
7
8use signature::{Keypair, SignatureEncoding, Signer, Verifier};
9
10/// A zero-sized type representing an empty signature.
11/// This type is used when no actual signature is needed, but the type system
12/// requires a signature type. It implements `SignatureEncoding` with an empty
13/// byte array representation.
14#[derive(Clone)]
15pub struct NoSignature;
16
17impl SignatureEncoding for NoSignature {
18    type Repr = [u8; 0];
19}
20
21impl<'a> TryFrom<&'a [u8]> for NoSignature {
22    type Error = ();
23
24    /// Attempts to create a `NoSignature` from a byte slice.
25    ///
26    /// # Arguments
27    /// * `value` - The byte slice to convert
28    ///
29    /// # Returns
30    /// * `Ok(NoSignature)` if the slice is empty
31    /// * `Err(())` if the slice contains any bytes
32    fn try_from(value: &'a [u8]) -> Result<Self, Self::Error> {
33        if !value.is_empty() {
34            return Err(());
35        }
36        Ok(NoSignature)
37    }
38}
39
40impl TryInto<[u8; 0]> for NoSignature {
41    type Error = ();
42
43    /// Converts a `NoSignature` into an empty byte array.
44    ///
45    /// # Returns
46    /// * `Ok([0; 0])` - An empty byte array
47    fn try_into(self) -> Result<[u8; 0], Self::Error> {
48        Ok([0; 0])
49    }
50}
51
52/// A zero-sized type representing a signing key that produces no signatures.
53/// This type is used when no actual signing is needed, but the type system
54/// requires a signing key type. It implements `Signer<NoSignature>` and always
55/// returns `NoSignature` when signing.
56pub struct NoSigningKey;
57
58impl Signer<NoSignature> for NoSigningKey {
59    /// Attempts to sign a message, always returning `NoSignature`.
60    ///
61    /// # Arguments
62    /// * `_msg` - The message to sign (ignored)
63    ///
64    /// # Returns
65    /// * `Ok(NoSignature)` - Always succeeds
66    fn try_sign(&self, _msg: &[u8]) -> Result<NoSignature, signature::Error> {
67        Ok(NoSignature)
68    }
69}
70
71/// A wrapper around an `Arc<T>` that implements `Signer` for the inner type.
72///
73/// This allows passing `Arc<SK>` where `SK: Signer` is expected.
74///
75/// # Type Parameters
76/// * `T` - The type being wrapped in an `Arc`
77pub struct ArcSigner<T>(pub Arc<T>);
78
79impl<S, T: Signer<S>> Signer<S> for ArcSigner<T> {
80    /// Signs a message using the inner signer.
81    ///
82    /// # Arguments
83    /// * `msg` - The message to sign
84    ///
85    /// # Returns
86    /// The signature produced by the inner signer
87    fn sign(&self, msg: &[u8]) -> S {
88        self.0.sign(msg)
89    }
90
91    /// Attempts to sign a message using the inner signer.
92    ///
93    /// # Arguments
94    /// * `msg` - The message to sign
95    ///
96    /// # Returns
97    /// The signature produced by the inner signer, or an error if signing fails
98    fn try_sign(&self, msg: &[u8]) -> Result<S, signature::Error> {
99        self.0.try_sign(msg)
100    }
101}
102
103impl<T: Keypair> Keypair for ArcSigner<T> {
104    type VerifyingKey = T::VerifyingKey;
105
106    /// Returns the verifying key associated with the inner keypair.
107    fn verifying_key(&self) -> Self::VerifyingKey {
108        self.0.verifying_key()
109    }
110}
111
112/// A verifying key that always succeeds verification.
113/// This type is used when no actual verification is needed, but the type system
114/// requires a verifying key type. It's typically used when secure transport is
115/// already in place and message authenticity doesn't need to be verified.
116/// The inner `Vec<u8>` is used as an identity ID.
117#[derive(Clone)]
118pub struct NoVerifyingKey(Vec<u8>);
119
120impl NoVerifyingKey {
121    /// Creates a new `NoVerifyingKey` from a participant ID.
122    ///
123    /// # Arguments
124    /// * `id` - The participant ID to use as the identity
125    ///
126    /// # Returns
127    /// A new `NoVerifyingKey` with the ID encoded as big-endian bytes
128    pub fn new(id: usize) -> Self {
129        NoVerifyingKey((id as u64).to_be_bytes().into())
130    }
131}
132
133impl<T: Into<Vec<u8>>> From<T> for NoVerifyingKey {
134    /// Creates a `NoVerifyingKey` from any type that can be converted into a `Vec<u8>`.
135    ///
136    /// # Arguments
137    /// * `value` - The value to convert into a verifying key
138    fn from(value: T) -> Self {
139        NoVerifyingKey(value.into())
140    }
141}
142
143impl AsRef<[u8]> for NoVerifyingKey {
144    /// Returns a reference to the inner byte vector.
145    fn as_ref(&self) -> &[u8] {
146        &self.0
147    }
148}
149
150impl Verifier<NoSignature> for NoVerifyingKey {
151    /// Verifies a signature, always succeeding.
152    ///
153    /// # Arguments
154    /// * `_` - The message (ignored)
155    /// * `_` - The signature (ignored)
156    ///
157    /// # Returns
158    /// * `Ok(())` - Always succeeds
159    fn verify(
160        &self,
161        _: &[u8],
162        _: &NoSignature,
163    ) -> Result<(), signature::Error> {
164        Ok(())
165    }
166}