-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merge MuSig2 functions into ekrem/new-architecture #206
Merged
ekrembal
merged 21 commits into
ekrem/new-architecture
from
ozan/new-architecture-musig2
Aug 9, 2024
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
efaf4d7
Add some musig2 functionalities
ozankaymak 866cbb7
Some changes in general and in MuSig2
ozankaymak b12350f
Change musig2 functions
ozankaymak b8e6238
Add one small test
ozankaymak 9c4901a
Added three more simple tests
ozankaymak 9aef433
Add test with dummy_tx key_spend
ozankaymak b30132e
Add test with dummy_tx script_spend
ozankaymak 8d417c3
Add rpc tests for key_spend and script_spend
ozankaymak d6be7f5
Add wrappers for aggregate_nonces and aggregate_partial_signatures, f…
ozankaymak fce1403
Cargo fmt
ozankaymak 6d556fd
Small change for config file
ozankaymak 6a58304
Some refactor
ozankaymak c0e2c4a
(WIP) Change XOnlyPublicKeys to PublicKeys in config files
ozankaymak a3a2776
Fix errors from pk-xonlypk change
ozankaymak 8e3adde
More refactor
ozankaymak 50850ca
Merge pull request #209 from chainwayxyz/ozan/config-pk-and-xonly-pk-fix
ozankaymak 9f483b6
Remove code duplication
ozankaymak 8b60a20
More refactor
ozankaymak faf3ad6
Commentation + more refactor
ozankaymak adcbbf4
Trying something
ozankaymak 298568a
Removed Cargo.lock + add to .gitignore
ozankaymak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,6 +138,9 @@ pub enum BridgeError { | |
#[error("InvalidKickoffUtxo")] | ||
InvalidKickoffUtxo, | ||
|
||
#[error("KeyAggContextError")] | ||
KeyAggContextError, | ||
|
||
#[error("NoncesNotFound")] | ||
NoncesNotFound, | ||
|
||
|
@@ -224,3 +227,9 @@ impl From<sqlx::Error> for BridgeError { | |
BridgeError::DatabaseError(err) | ||
} | ||
} | ||
|
||
impl From<musig2::errors::KeyAggError> for BridgeError { | ||
fn from(_err: musig2::errors::KeyAggError) -> Self { | ||
BridgeError::KeyAggContextError | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pls propogate the original message in this |
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,81 @@ | ||
use secp256k1::{schnorr, Keypair}; | ||
use bitcoin::Network; | ||
use crypto_bigint::rand_core::OsRng; | ||
use musig2::{ | ||
secp::Point, secp256k1::Scalar, sign_partial, AggNonce, FirstRound, KeyAggContext, | ||
PartialSignature, SecNonce, SecNonceSpices, | ||
}; | ||
use secp256k1::{rand::Rng, schnorr, Keypair, PublicKey}; | ||
|
||
pub type MusigPubNonce = [u8; 32]; | ||
pub type MusigSecNonce = [u8; 32]; | ||
pub type MusigAggNonce = [u8; 32]; | ||
use crate::{actor::Actor, errors::BridgeError}; | ||
|
||
// We can directly use the musig2 crate for this | ||
// No need for extra types etc. | ||
pub type MusigPubNonce = [u8; 66]; | ||
pub type MusigSecNonce = [u8; 64]; | ||
// pub type MusigAggNonce = [u8; 66]; | ||
pub type MusigPartialSignature = [u8; 32]; | ||
|
||
pub fn nonce_pair(_key: &secp256k1::Keypair) -> (MusigSecNonce, MusigPubNonce) { | ||
// let kp = keypair_to(key); | ||
// zkp::new_musig_nonce_pair( | ||
// &SECP, | ||
// MusigSessionId::assume_unique_per_nonce_gen(rand::random()), | ||
// None, | ||
// Some(kp.secret_key()), | ||
// kp.public_key(), | ||
// None, | ||
// Some(rand::random()), | ||
// ).expect("non-zero session id") | ||
([0u8; 32] as MusigSecNonce, [0u8; 32] as MusigPubNonce) | ||
pub fn create_key_agg_ctx(pks: Vec<PublicKey>) -> Result<KeyAggContext, BridgeError> { | ||
let musig_pks: Vec<musig2::secp256k1::PublicKey> = pks | ||
.iter() | ||
.map(|pk| musig2::secp256k1::PublicKey::from_slice(&pk.serialize()).unwrap()) | ||
.collect::<Vec<musig2::secp256k1::PublicKey>>(); | ||
Ok(KeyAggContext::new(musig_pks)?) | ||
} | ||
|
||
pub fn partial_sign( | ||
pubkeys: impl IntoIterator<Item = secp256k1::PublicKey>, | ||
agg_nonce: MusigAggNonce, | ||
key: &secp256k1::Keypair, | ||
sec_nonce: MusigSecNonce, | ||
sighash: [u8; 32], | ||
tweak: Option<[u8; 32]>, | ||
other_sigs: Option<&[MusigPartialSignature]>, | ||
) -> (MusigPartialSignature, Option<schnorr::Signature>) { | ||
// let agg = if let Some(tweak) = tweak { | ||
// tweaked_key_agg(pubkeys, tweak).0 | ||
// } else { | ||
// key_agg(pubkeys) | ||
// }; | ||
pub fn get_agg_pubkey(key_agg_ctx: &KeyAggContext) -> PublicKey { | ||
PublicKey::from_slice( | ||
&key_agg_ctx | ||
.aggregated_pubkey::<musig2::secp256k1::PublicKey>() | ||
.serialize(), | ||
) | ||
.unwrap() | ||
} | ||
|
||
// let msg = zkp::Message::from_digest(sighash); | ||
// let session = MusigSession::new(&SECP, &agg, agg_nonce, msg); | ||
// let my_sig = session.partial_sign(&SECP, sec_nonce, &keypair_to(&key), &agg) | ||
// .expect("nonce not reused"); | ||
// let final_sig = if let Some(others) = other_sigs { | ||
// let mut sigs = Vec::with_capacity(others.len() + 1); | ||
// sigs.extend_from_slice(others); | ||
// sigs.push(my_sig); | ||
// Some(session.partial_sig_agg(&sigs)) | ||
// } else { | ||
// None | ||
// }; | ||
([0u8;32] as MusigPartialSignature, None) | ||
} | ||
pub fn nonce_pair( | ||
keypair: &secp256k1::Keypair, | ||
rng: &mut impl Rng, | ||
pks: Vec<PublicKey>, | ||
) -> (MusigSecNonce, MusigPubNonce) { | ||
let key_agg_ctx = create_key_agg_ctx(pks).unwrap(); | ||
let musig_pubkey = | ||
musig2::secp256k1::PublicKey::from_slice(&keypair.public_key().serialize()).unwrap(); | ||
let idx = key_agg_ctx.pubkey_index(musig_pubkey).unwrap(); | ||
let agg_pubkey: musig2::secp256k1::PublicKey = key_agg_ctx.aggregated_pubkey(); | ||
let rnd = rng.gen::<[u8; 32]>(); | ||
let spices = SecNonceSpices::new().with_seckey( | ||
musig2::secp256k1::SecretKey::from_slice(&keypair.secret_key().secret_bytes()).unwrap(), | ||
); | ||
let first_round = FirstRound::new(key_agg_ctx, rnd, idx, spices.clone()).unwrap(); | ||
// This part is also done when generating the first round, so I guess we can make it more | ||
// efficient if we only store the nonce_seed since we can recreate everything using it. | ||
let sec_nonce = SecNonce::build(rnd) | ||
.with_pubkey(musig_pubkey) | ||
.with_aggregated_pubkey(agg_pubkey) | ||
.with_extra_input(&(idx as u32).to_be_bytes()) | ||
.with_spices(spices) | ||
.build(); | ||
(sec_nonce.into(), first_round.our_public_nonce().into()) | ||
} | ||
|
||
pub fn partial_sign( | ||
pks: Vec<PublicKey>, | ||
sec_nonce: MusigSecNonce, | ||
agg_nonce: AggNonce, | ||
keypair: &secp256k1::Keypair, | ||
sighash: [u8; 32], | ||
// tweak: Option<[u8; 32]>, | ||
// other_sigs: Option<&[MusigPartialSignature]>, | ||
) -> MusigPartialSignature { | ||
let key_agg_ctx = create_key_agg_ctx(pks).unwrap(); | ||
let musig_sec_nonce = SecNonce::from_bytes(&sec_nonce).unwrap(); | ||
let partial_signature: [u8; 32] = sign_partial( | ||
&key_agg_ctx, | ||
musig2::secp256k1::SecretKey::from_slice(&keypair.secret_key().secret_bytes()).unwrap(), | ||
musig_sec_nonce, | ||
&agg_nonce, | ||
&sighash, | ||
) | ||
.unwrap(); | ||
partial_signature | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can use workspace=true here