Skip to content

Commit

Permalink
Merge pull request #1 from LedgerHQ/pubkey_tweak_add
Browse files Browse the repository at this point in the history
Use custom implementation from vanadium_sdk for PublicKey::add_exp_tweak
  • Loading branch information
bigspider authored Jul 28, 2023
2 parents f968b9f + 147d815 commit 25216e1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ serde = { version = "1.0.103", default-features = false, optional = true }
bitcoin_hashes = { version = "0.12", default-features = false, optional = true }
rand = { version = "0.8", default-features = false, optional = true }

vanadium_sdk = { path = "../../../rust-sdk" }

[dev-dependencies]
rand_core = "0.6"
serde_cbor = "0.10.0"
Expand Down
34 changes: 26 additions & 8 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use crate::{ecdsa, SECP256K1};
#[cfg(feature = "bitcoin_hashes")]
use crate::{hashes, ThirtyTwoByteHash};

use vanadium_sdk;

/// Secret 256-bit key used as `x` in an ECDSA signature.
///
/// # Side channel attacks
Expand Down Expand Up @@ -572,15 +574,31 @@ impl PublicKey {
secp: &Secp256k1<C>,
tweak: &Scalar,
) -> Result<PublicKey, Error> {
unsafe {
if ffi::secp256k1_ec_pubkey_tweak_add(secp.ctx.as_ptr(), &mut self.0, tweak.as_c_ptr())
== 1
{
Ok(self)
} else {
Err(Error::InvalidTweak)
}

let mut pk = self.serialize_uncompressed();
if vanadium_sdk::secp256k1::secp256k1_ec_pubkey_tweak_add(
secp.ctx.as_ptr() as *const (),
&mut pk,
tweak.as_c_ptr(),
) == 1
{

self = PublicKey::from_slice(&pk[..constants::UNCOMPRESSED_PUBLIC_KEY_SIZE])
.expect("We know the length is correct and the tweak operation succeeded, so we should have a valid public key");
Ok(self)
} else {
Err(Error::InvalidTweak)
}

// unsafe {
// if ffi::secp256k1_ec_pubkey_tweak_add(secp.ctx.as_ptr(), &mut self.0, tweak.as_c_ptr())
// == 1
// {
// Ok(self)
// } else {
// Err(Error::InvalidTweak)
// }
// }
}

/// Tweaks a [`PublicKey`] by multiplying by `tweak` modulo the curve order.
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ extern crate core;
#[cfg(bench)]
extern crate test;

extern crate vanadium_sdk;

#[macro_use]
mod macros;
#[macro_use]
Expand Down

0 comments on commit 25216e1

Please sign in to comment.