diff --git a/Cargo.toml b/Cargo.toml index 0f265559c..0818b5349 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/key.rs b/src/key.rs index 0eb747e55..c1f0cdbe7 100644 --- a/src/key.rs +++ b/src/key.rs @@ -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 @@ -572,15 +574,31 @@ impl PublicKey { secp: &Secp256k1, tweak: &Scalar, ) -> Result { - 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. diff --git a/src/lib.rs b/src/lib.rs index 14d65f224..64ec9ae31 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -151,6 +151,8 @@ extern crate core; #[cfg(bench)] extern crate test; +extern crate vanadium_sdk; + #[macro_use] mod macros; #[macro_use]