Skip to content
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

feat(crypto): CRP-2591 Add BIP-341 sig support to secp256k1 utility crate #2756

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

randombit
Copy link
Contributor

No description provided.

@github-actions github-actions bot added the feat label Nov 21, 2024
@randombit randombit marked this pull request as ready for review November 22, 2024 18:56
@randombit randombit requested a review from a team as a code owner November 22, 2024 18:56
Copy link
Contributor

@altkdf altkdf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for implementing BIP341 @randombit , nice to see it working!

I have a few minor, mostly cosmetic comments.

@@ -330,6 +340,31 @@ fn pem_encode(raw: &[u8], label: &'static str) -> String {
})
}

fn bip341_hash(pk_x: &[u8], ttr: &[u8]) -> Result<Scalar, InvalidTaprootHash> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be useful to add descriptions for the added functions, e.g., what ttr is or that a BIP341 hash is actually the tweak. Maybe we should actually call it bip341_tweak. We could also add the assumptions that pk_x and ttr have a valid length or add debug_asserts.


let t = k256::ProjectivePoint::mul_by_generator(&bip341_hash(&pk[1..], ttr)?);
let pk_y_is_even = pk[0] == 0x02;
println!("y_is_even {}", pk_y_is_even);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove?

assert!(pk.verify_bip340_signature(&msg, &sig));

for ttr_len in [0, 32] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, shouldn't this be another test? Something like should_accept_bip341_signatures_that_we_generate?


let pk = XOnlyPublicKey::from_slice(&sk.public_key().serialize_sec1(true)[1..]).unwrap();

let tnh = TapBranchHash::from_hex(&hex::encode(ttr)).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh?


let tnh = TapBranchHash::from_hex(&hex::encode(ttr)).unwrap();

let dk = pk.tap_tweak(&secp256k1, Some(tnh)).0.to_inner();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does dk mean derived key? I think the BIPs always speak about this as tweaking. Maybe we could use that instead of "derive" in general.

@@ -837,6 +907,44 @@ impl PublicKey {
}
}

/// BIP341 derivation
fn derive_bip341(&self, ttr: &[u8]) -> Result<Self, InvalidTaprootHash> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also check in unit tests that functions produce correct results against third-party libraries. But not necessarily in this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Differential fuzzers might also make sense

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants