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