Skip to content

Commit b60db79

Browse files
committed
Use un/tweaked public key types
We have two types for tweaked/untweaked schnorr public keys to help users of the taproot API not mix these two keys up. Currently the `taproot` module uses 'raw' `schnoor::PublicKey`s. Use the `schnoor` module's tweak/untweaked public key types for the `taproot` API.
1 parent 402bd99 commit b60db79

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

src/util/taproot.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use core::cmp::Reverse;
2525
use std::error;
2626

2727
use hashes::{sha256, sha256t, Hash, HashEngine};
28-
use schnorr;
28+
use schnorr::{TweakedPublicKey, UntweakedPublicKey};
2929
use Script;
3030

3131
use consensus::Encodable;
@@ -101,7 +101,7 @@ impl TapTweakHash {
101101
/// Create a new BIP341 [`TapTweakHash`] from key and tweak
102102
/// Produces H_taptweak(P||R) where P is internal key and R is the merkle root
103103
pub fn from_key_and_tweak(
104-
internal_key: schnorr::PublicKey,
104+
internal_key: UntweakedPublicKey,
105105
merkle_root: Option<TapBranchHash>,
106106
) -> TapTweakHash {
107107
let mut eng = TapTweakHash::engine();
@@ -171,13 +171,13 @@ type ScriptMerkleProofMap = BTreeMap<(Script, LeafVersion), BTreeSet<TaprootMerk
171171
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
172172
pub struct TaprootSpendInfo {
173173
/// The BIP341 internal key.
174-
internal_key: schnorr::PublicKey,
174+
internal_key: UntweakedPublicKey,
175175
/// The Merkle root of the script tree (None if there are no scripts)
176176
merkle_root: Option<TapBranchHash>,
177177
/// The sign final output pubkey as per BIP 341
178178
output_key_parity: bool,
179179
/// The tweaked output key
180-
output_key: schnorr::PublicKey,
180+
output_key: TweakedPublicKey,
181181
/// Map from (script, leaf_version) to (sets of) [`TaprootMerkleBranch`].
182182
/// More than one control block for a given script is only possible if it
183183
/// appears in multiple branches of the tree. In all cases, keeping one should
@@ -204,7 +204,7 @@ impl TaprootSpendInfo {
204204
/// dealing with numbers close to 2^64.
205205
pub fn with_huffman_tree<C, I>(
206206
secp: &Secp256k1<C>,
207-
internal_key: schnorr::PublicKey,
207+
internal_key: UntweakedPublicKey,
208208
script_weights: I,
209209
) -> Result<Self, TaprootBuilderError>
210210
where
@@ -250,7 +250,7 @@ impl TaprootSpendInfo {
250250
///
251251
pub fn new_key_spend<C: secp256k1::Verification>(
252252
secp: &Secp256k1<C>,
253-
internal_key: schnorr::PublicKey,
253+
internal_key: UntweakedPublicKey,
254254
merkle_root: Option<TapBranchHash>,
255255
) -> Self {
256256
let tweak = TapTweakHash::from_key_and_tweak(internal_key, merkle_root);
@@ -268,7 +268,7 @@ impl TaprootSpendInfo {
268268
internal_key: internal_key,
269269
merkle_root: merkle_root,
270270
output_key_parity: parity,
271-
output_key: output_key,
271+
output_key: TweakedPublicKey::new(output_key),
272272
script_map: BTreeMap::new(),
273273
}
274274
}
@@ -279,7 +279,7 @@ impl TaprootSpendInfo {
279279
}
280280

281281
/// Obtain the internal key
282-
pub fn internal_key(&self) -> schnorr::PublicKey {
282+
pub fn internal_key(&self) -> UntweakedPublicKey {
283283
self.internal_key
284284
}
285285

@@ -290,7 +290,7 @@ impl TaprootSpendInfo {
290290

291291
/// Output key(the key used in script pubkey) from Spend data. See also
292292
/// [`TaprootSpendInfo::output_key_parity`]
293-
pub fn output_key(&self) -> schnorr::PublicKey {
293+
pub fn output_key(&self) -> TweakedPublicKey {
294294
self.output_key
295295
}
296296

@@ -302,7 +302,7 @@ impl TaprootSpendInfo {
302302
// Internal function to compute [`TaprootSpendInfo`] from NodeInfo
303303
fn from_node_info<C: secp256k1::Verification>(
304304
secp: &Secp256k1<C>,
305-
internal_key: schnorr::PublicKey,
305+
internal_key: UntweakedPublicKey,
306306
node: NodeInfo,
307307
) -> TaprootSpendInfo {
308308
// Create as if it is a key spend path with the given merkle root
@@ -430,7 +430,7 @@ impl TaprootBuilder {
430430
pub fn finalize<C: secp256k1::Verification>(
431431
mut self,
432432
secp: &Secp256k1<C>,
433-
internal_key: schnorr::PublicKey,
433+
internal_key: UntweakedPublicKey,
434434
) -> Result<TaprootSpendInfo, TaprootBuilderError> {
435435
if self.branch.len() > 1 {
436436
return Err(TaprootBuilderError::IncompleteTree);
@@ -652,7 +652,7 @@ pub struct ControlBlock {
652652
/// The parity of the output key (NOT THE INTERNAL KEY WHICH IS ALWAYS XONLY)
653653
pub output_key_parity: bool,
654654
/// The internal key
655-
pub internal_key: schnorr::PublicKey,
655+
pub internal_key: UntweakedPublicKey,
656656
/// The merkle proof of a script associated with this leaf
657657
pub merkle_branch: TaprootMerkleBranch,
658658
}
@@ -674,7 +674,7 @@ impl ControlBlock {
674674
}
675675
let output_key_parity = (sl[0] & 1) == 1;
676676
let leaf_version = LeafVersion::from_u8(sl[0] & TAPROOT_LEAF_MASK)?;
677-
let internal_key = schnorr::PublicKey::from_slice(&sl[1..TAPROOT_CONTROL_BASE_SIZE])
677+
let internal_key = UntweakedPublicKey::from_slice(&sl[1..TAPROOT_CONTROL_BASE_SIZE])
678678
.map_err(TaprootError::InvalidInternalKey)?;
679679
let merkle_branch = TaprootMerkleBranch::from_slice(&sl[TAPROOT_CONTROL_BASE_SIZE..])?;
680680
Ok(ControlBlock {
@@ -719,7 +719,7 @@ impl ControlBlock {
719719
pub fn verify_taproot_commitment<C: secp256k1::Verification>(
720720
&self,
721721
secp: &Secp256k1<C>,
722-
output_key: &schnorr::PublicKey,
722+
output_key: &TweakedPublicKey,
723723
script: &Script,
724724
) -> bool {
725725
// compute the script hash
@@ -743,7 +743,7 @@ impl ControlBlock {
743743
let tweak = TapTweakHash::from_key_and_tweak(self.internal_key, Some(curr_hash));
744744
self.internal_key.tweak_add_check(
745745
secp,
746-
output_key,
746+
output_key.as_inner(),
747747
self.output_key_parity,
748748
tweak.into_inner(),
749749
)
@@ -900,6 +900,7 @@ mod test {
900900
use hashes::{sha256, Hash, HashEngine};
901901
use secp256k1::VerifyOnly;
902902
use core::str::FromStr;
903+
use schnorr;
903904

904905
fn tag_engine(tag_name: &str) -> sha256::HashEngine {
905906
let mut engine = sha256::Hash::engine();
@@ -984,6 +985,7 @@ mod test {
984985

985986
fn _verify_tap_commitments(secp: &Secp256k1<VerifyOnly>, out_spk_hex: &str, script_hex : &str, control_block_hex: &str) {
986987
let out_pk = schnorr::PublicKey::from_str(&out_spk_hex[4..]).unwrap();
988+
let out_pk = TweakedPublicKey::new(out_pk);
987989
let script = Script::from_hex(script_hex).unwrap();
988990
let control_block = ControlBlock::from_slice(&Vec::<u8>::from_hex(control_block_hex).unwrap()).unwrap();
989991
assert_eq!(control_block_hex, control_block.serialize().to_hex());
@@ -1025,7 +1027,7 @@ mod test {
10251027
#[test]
10261028
fn build_huffman_tree() {
10271029
let secp = Secp256k1::verification_only();
1028-
let internal_key = schnorr::PublicKey::from_str("93c7378d96518a75448821c4f7c8f4bae7ce60f804d03d1f0628dd5dd0f5de51").unwrap();
1030+
let internal_key = UntweakedPublicKey::from_str("93c7378d96518a75448821c4f7c8f4bae7ce60f804d03d1f0628dd5dd0f5de51").unwrap();
10291031

10301032
let script_weights = vec![
10311033
(10, Script::from_hex("51").unwrap()), // semantics of script don't matter for this test
@@ -1075,7 +1077,7 @@ mod test {
10751077
#[test]
10761078
fn taptree_builder() {
10771079
let secp = Secp256k1::verification_only();
1078-
let internal_key = schnorr::PublicKey::from_str("93c7378d96518a75448821c4f7c8f4bae7ce60f804d03d1f0628dd5dd0f5de51").unwrap();
1080+
let internal_key = UntweakedPublicKey::from_str("93c7378d96518a75448821c4f7c8f4bae7ce60f804d03d1f0628dd5dd0f5de51").unwrap();
10791081

10801082
let builder = TaprootBuilder::new();
10811083
// Create a tree as shown below

0 commit comments

Comments
 (0)