fedimint_core/encoding/
secp256k1.rs

1use std::io::{Error, Read, Write};
2
3use crate::encoding::{Decodable, DecodeError, Encodable};
4use crate::module::registry::ModuleDecoderRegistry;
5
6impl Encodable for secp256k1::ecdsa::Signature {
7    fn consensus_encode<W: std::io::Write>(&self, writer: &mut W) -> Result<usize, std::io::Error> {
8        let bytes = self.serialize_compact();
9        writer.write_all(&bytes)?;
10        Ok(bytes.len())
11    }
12}
13
14impl Decodable for secp256k1::ecdsa::Signature {
15    fn consensus_decode_partial<D: std::io::Read>(
16        d: &mut D,
17        modules: &ModuleDecoderRegistry,
18    ) -> Result<Self, DecodeError> {
19        Self::from_compact(&<[u8; 64]>::consensus_decode_partial(d, modules)?)
20            .map_err(DecodeError::from_err)
21    }
22}
23
24impl Encodable for secp256k1::PublicKey {
25    fn consensus_encode<W: std::io::Write>(&self, writer: &mut W) -> Result<usize, std::io::Error> {
26        self.serialize().consensus_encode(writer)
27    }
28}
29
30impl Decodable for secp256k1::PublicKey {
31    fn consensus_decode_partial<D: std::io::Read>(
32        d: &mut D,
33        modules: &ModuleDecoderRegistry,
34    ) -> Result<Self, DecodeError> {
35        Self::from_slice(&<[u8; 33]>::consensus_decode_partial(d, modules)?)
36            .map_err(DecodeError::from_err)
37    }
38}
39
40impl Encodable for secp256k1::SecretKey {
41    fn consensus_encode<W: std::io::Write>(&self, writer: &mut W) -> Result<usize, std::io::Error> {
42        self.secret_bytes().consensus_encode(writer)
43    }
44}
45
46impl Decodable for secp256k1::SecretKey {
47    fn consensus_decode_partial<D: std::io::Read>(
48        d: &mut D,
49        modules: &ModuleDecoderRegistry,
50    ) -> Result<Self, DecodeError> {
51        Self::from_slice(&<[u8; 32]>::consensus_decode_partial(d, modules)?)
52            .map_err(DecodeError::from_err)
53    }
54}
55
56impl Encodable for secp256k1::schnorr::Signature {
57    fn consensus_encode<W: std::io::Write>(&self, writer: &mut W) -> Result<usize, std::io::Error> {
58        let bytes = &self[..];
59        assert_eq!(bytes.len(), secp256k1::constants::SCHNORR_SIGNATURE_SIZE);
60        writer.write_all(bytes)?;
61        Ok(secp256k1::constants::SCHNORR_SIGNATURE_SIZE)
62    }
63}
64
65impl Decodable for secp256k1::schnorr::Signature {
66    fn consensus_decode_partial<D: std::io::Read>(
67        d: &mut D,
68        modules: &ModuleDecoderRegistry,
69    ) -> Result<Self, DecodeError> {
70        let bytes = <[u8; secp256k1::constants::SCHNORR_SIGNATURE_SIZE]>::consensus_decode_partial(
71            d, modules,
72        )?;
73        Self::from_slice(&bytes).map_err(DecodeError::from_err)
74    }
75}
76
77impl Encodable for bitcoin::key::Keypair {
78    fn consensus_encode<W: Write>(&self, writer: &mut W) -> Result<usize, Error> {
79        self.secret_bytes().consensus_encode(writer)
80    }
81}
82
83impl Decodable for bitcoin::key::Keypair {
84    fn consensus_decode_partial<D: Read>(
85        d: &mut D,
86        modules: &ModuleDecoderRegistry,
87    ) -> Result<Self, DecodeError> {
88        let sec_bytes = <[u8; 32]>::consensus_decode_partial(d, modules)?;
89        Self::from_seckey_slice(bitcoin::secp256k1::global::SECP256K1, &sec_bytes) // FIXME: evaluate security risk of global ctx
90            .map_err(DecodeError::from_err)
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use secp256k1::hashes::Hash as BitcoinHash;
97    use secp256k1::Message;
98
99    use super::super::tests::test_roundtrip;
100
101    #[test_log::test]
102    fn test_ecdsa_sig() {
103        let ctx = secp256k1::Secp256k1::new();
104        let (sk, _pk) = ctx.generate_keypair(&mut rand::thread_rng());
105        let sig = ctx.sign_ecdsa(
106            &Message::from_digest(*secp256k1::hashes::sha256::Hash::hash(b"Hello World!").as_ref()),
107            &sk,
108        );
109
110        test_roundtrip(&sig);
111    }
112
113    #[test_log::test]
114    fn test_schnorr_pub_key() {
115        let ctx = secp256k1::global::SECP256K1;
116        let mut rng = rand::rngs::OsRng;
117        let sec_key = bitcoin::key::Keypair::new(ctx, &mut rng);
118        let pub_key = sec_key.public_key();
119        test_roundtrip(&pub_key);
120
121        let sig = ctx.sign_schnorr(
122            &Message::from_digest(*secp256k1::hashes::sha256::Hash::hash(b"Hello World!").as_ref()),
123            &sec_key,
124        );
125
126        test_roundtrip(&sig);
127    }
128}