Skip to content

Commit 34aaacb

Browse files
committed
refactor: resolved all protocol's todo
1 parent 0917eae commit 34aaacb

File tree

1 file changed

+65
-61
lines changed

1 file changed

+65
-61
lines changed

src/protocol/musig_interface.rs

Lines changed: 65 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,92 +5,96 @@ use super::{
55
musig2::{aggregate_partial_signatures, generate_partial_signature},
66
*,
77
};
8+
use bitcoin::secp256k1 as btc_secp;
89
use musig2::{generate_new_nonce_pair, get_aggregated_pubkey};
10+
use secp256k1 as secp;
911

10-
//TODOS
11-
//- Use macro for converting between secp256k1 and bitcoin::secp256k1
12-
//- Use named imports for secp256k1 and bitcoin::secp256k1
12+
/// Convert a bitcoin::secp256k1 public key into secp256k1 public key
13+
macro_rules! btc_pubkey_to_secp {
14+
($pk:expr) => {
15+
secp::PublicKey::from_slice(&$pk.serialize())?
16+
};
17+
}
18+
19+
/// Convert a bitcoin::secp256k1 scalar into secp256k1 scalar
20+
macro_rules! btc_scalar_to_secp {
21+
($scalar:expr) => {
22+
secp::Scalar::from_be_bytes($scalar.to_be_bytes())?
23+
};
24+
}
25+
26+
/// Convert bitcoin::secp256k1::Message to secp256k1::Message
27+
macro_rules! btc_msg_to_secp {
28+
($msg:expr) => {
29+
secp::Message::from_digest(*$msg.as_ref())
30+
};
31+
}
1332

1433
/// Aggregates the public keys
1534
pub fn get_aggregated_pubkey_compat(
16-
pubkey1: bitcoin::secp256k1::PublicKey,
17-
pubkey2: bitcoin::secp256k1::PublicKey,
18-
) -> Result<bitcoin::secp256k1::XOnlyPublicKey, ProtocolError> {
19-
let s_pubkey1 = pubkey1.serialize();
20-
let s_pubkey2 = pubkey2.serialize();
21-
let pubkey1 = secp256k1::PublicKey::from_slice(&s_pubkey1)?;
22-
let pubkey2 = secp256k1::PublicKey::from_slice(&s_pubkey2)?;
35+
pubkey1: btc_secp::PublicKey,
36+
pubkey2: btc_secp::PublicKey,
37+
) -> Result<btc_secp::XOnlyPublicKey, ProtocolError> {
38+
let pubkey1 = btc_pubkey_to_secp!(pubkey1);
39+
let pubkey2 = btc_pubkey_to_secp!(pubkey2);
2340
let agg_pubkey = get_aggregated_pubkey(&pubkey1, &pubkey2);
24-
let s_agg_pubkey = agg_pubkey.serialize();
25-
26-
Ok(bitcoin::secp256k1::XOnlyPublicKey::from_slice(
27-
&s_agg_pubkey,
41+
Ok(btc_secp::XOnlyPublicKey::from_slice(
42+
&agg_pubkey.serialize(),
2843
)?)
2944
}
3045

3146
/// Generates a new nonce pair
3247
pub fn generate_new_nonce_pair_compat(
33-
nonce_pubkey: bitcoin::secp256k1::PublicKey,
34-
) -> Result<(secp256k1::musig::SecretNonce, secp256k1::musig::PublicNonce), ProtocolError> {
35-
let nonce_pubkey = nonce_pubkey.serialize();
36-
let nonce_pubkey = secp256k1::PublicKey::from_slice(&nonce_pubkey)?;
37-
// Convert bitcoin::secp256k1::Message to secp256k1::Message directly from bytes
48+
nonce_pubkey: btc_secp::PublicKey,
49+
) -> Result<(secp::musig::SecretNonce, secp::musig::PublicNonce), ProtocolError> {
50+
let nonce_pubkey = btc_pubkey_to_secp!(nonce_pubkey);
3851
Ok(generate_new_nonce_pair(nonce_pubkey))
3952
}
4053

41-
/// get aggregated nonce
54+
/// Get aggregated nonce
4255
pub fn get_aggregated_nonce_compat(
43-
nonces: &[&secp256k1::musig::PublicNonce],
44-
) -> secp256k1::musig::AggregatedNonce {
45-
let secp = secp256k1::Secp256k1::new();
46-
secp256k1::musig::AggregatedNonce::new(&secp, nonces)
56+
nonces: &[&secp::musig::PublicNonce],
57+
) -> secp::musig::AggregatedNonce {
58+
let secp = secp::Secp256k1::new();
59+
secp::musig::AggregatedNonce::new(&secp, nonces)
4760
}
4861

4962
/// Generates a partial signature
5063
pub fn generate_partial_signature_compat(
51-
message: bitcoin::secp256k1::Message,
52-
agg_nonce: &secp256k1::musig::AggregatedNonce,
53-
sec_nonce: secp256k1::musig::SecretNonce,
54-
keypair: bitcoin::secp256k1::Keypair,
55-
tap_tweak: bitcoin::secp256k1::Scalar,
56-
pubkey1: bitcoin::secp256k1::PublicKey,
57-
pubkey2: bitcoin::secp256k1::PublicKey,
58-
) -> Result<secp256k1::musig::PartialSignature, ProtocolError> {
59-
let secp = secp256k1::Secp256k1::new();
60-
let tap_tweak = tap_tweak.to_be_bytes();
61-
let tap_tweak = secp256k1::Scalar::from_be_bytes(tap_tweak)?;
62-
let pubkey1 = pubkey1.serialize();
63-
let pubkey2 = pubkey2.serialize();
64-
let pubkey1 = secp256k1::PublicKey::from_slice(&pubkey1)?;
65-
let pubkey2 = secp256k1::PublicKey::from_slice(&pubkey2)?;
64+
message: btc_secp::Message,
65+
agg_nonce: &secp::musig::AggregatedNonce,
66+
sec_nonce: secp::musig::SecretNonce,
67+
keypair: btc_secp::Keypair,
68+
tap_tweak: btc_secp::Scalar,
69+
pubkey1: btc_secp::PublicKey,
70+
pubkey2: btc_secp::PublicKey,
71+
) -> Result<secp::musig::PartialSignature, ProtocolError> {
72+
let secp = secp::Secp256k1::new();
73+
let message = btc_msg_to_secp!(message);
74+
let tap_tweak = btc_scalar_to_secp!(tap_tweak);
75+
let pubkey1 = btc_pubkey_to_secp!(pubkey1);
76+
let pubkey2 = btc_pubkey_to_secp!(pubkey2);
6677
let pubkeys = [&pubkey1, &pubkey2];
67-
// Convert bitcoin::secp256k1::Message to secp256k1::Message directly from bytes
68-
let message_bytes = message.as_ref();
69-
let message = secp256k1::Message::from_digest(*message_bytes);
70-
let keypair_secret = keypair.secret_bytes();
71-
let secret_key = secp256k1::SecretKey::from_slice(&keypair_secret)?;
72-
let keypair = secp256k1::Keypair::from_secret_key(&secp, &secret_key);
78+
let secret_key = secp::SecretKey::from_slice(&keypair.secret_bytes())?;
79+
let keypair = secp::Keypair::from_secret_key(&secp, &secret_key);
80+
7381
generate_partial_signature(message, agg_nonce, sec_nonce, keypair, tap_tweak, &pubkeys)
7482
}
7583

7684
/// Aggregates the partial signatures
7785
pub fn aggregate_partial_signatures_compat(
78-
message: bitcoin::secp256k1::Message,
79-
agg_nonce: secp256k1::musig::AggregatedNonce,
80-
tap_tweak: bitcoin::secp256k1::Scalar,
81-
partial_sigs: Vec<&secp256k1::musig::PartialSignature>,
82-
pubkey_1: bitcoin::secp256k1::PublicKey,
83-
pubkey2: bitcoin::secp256k1::PublicKey,
84-
) -> Result<secp256k1::musig::AggregatedSignature, ProtocolError> {
85-
let tap_tweak = tap_tweak.to_be_bytes();
86-
let tap_tweak = secp256k1::Scalar::from_be_bytes(tap_tweak)?;
87-
// Convert bitcoin::secp256k1::Message to secp256k1::Message directly from bytes
88-
let message_bytes = message.as_ref();
89-
let message = secp256k1::Message::from_digest(*message_bytes);
90-
let pubkey1 = pubkey_1.serialize();
91-
let pubkey2 = pubkey2.serialize();
92-
let pubkey1 = secp256k1::PublicKey::from_slice(&pubkey1)?;
93-
let pubkey2 = secp256k1::PublicKey::from_slice(&pubkey2)?;
86+
message: btc_secp::Message,
87+
agg_nonce: secp::musig::AggregatedNonce,
88+
tap_tweak: btc_secp::Scalar,
89+
partial_sigs: Vec<&secp::musig::PartialSignature>,
90+
pubkey1: btc_secp::PublicKey,
91+
pubkey2: btc_secp::PublicKey,
92+
) -> Result<secp::musig::AggregatedSignature, ProtocolError> {
93+
let message = btc_msg_to_secp!(message);
94+
let tap_tweak = btc_scalar_to_secp!(tap_tweak);
95+
let pubkey1 = btc_pubkey_to_secp!(pubkey1);
96+
let pubkey2 = btc_pubkey_to_secp!(pubkey2);
97+
9498
aggregate_partial_signatures(
9599
message,
96100
agg_nonce,

0 commit comments

Comments
 (0)