Module: Bitcoin::Taproot
- Defined in:
- lib/bitcoin/taproot.rb,
lib/bitcoin/taproot/leaf_node.rb,
lib/bitcoin/taproot/control_block.rb,
lib/bitcoin/taproot/simple_builder.rb,
lib/bitcoin/taproot/custom_depth_builder.rb
Defined Under Namespace
Classes: ControlBlock, CustomDepthBuilder, Error, LeafNode, SimpleBuilder
Constant Summary collapse
- NUMS_H =
"50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0"
Class Method Summary collapse
-
.tweak(internal_key, merkle_root) ⇒ String
Calculate tweak value from
internal_pubkeyandmerkle_root. -
.tweak_private_key(internal_private_key, merkle_root) ⇒ Object
Generate tweak private key.
-
.tweak_public_key(internal_key, merkle_root) ⇒ Bitcoin::Key
Generate tweak public key form
internal_pubkeyandmerkle_root.
Class Method Details
.tweak(internal_key, merkle_root) ⇒ String
Calculate tweak value from internal_pubkey and merkle_root.
19 20 21 22 23 24 25 26 27 |
# File 'lib/bitcoin/taproot.rb', line 19 def tweak(internal_key, merkle_root) raise Error, 'internal_key must be Bitcoin::Key object.' unless internal_key.is_a?(Bitcoin::Key) merkle_root ||= '' t = Bitcoin.tagged_hash('TapTweak', internal_key.xonly_pubkey.htb + merkle_root.htb) raise Error, 'tweak value exceeds the curve order' if t.bti >= ECDSA::Group::Secp256k1.order t end |
.tweak_private_key(internal_private_key, merkle_root) ⇒ Object
Generate tweak private key
46 47 48 49 50 51 52 53 54 |
# File 'lib/bitcoin/taproot.rb', line 46 def tweak_private_key(internal_private_key, merkle_root) p = internal_private_key.to_point private_key = p.has_even_y? ? internal_private_key.priv_key.to_i(16) : ECDSA::Group::Secp256k1.order - internal_private_key.priv_key.to_i(16) t = tweak(internal_private_key, merkle_root) private_key = ECDSA::Format::IntegerOctetString.encode( (t.bti + private_key) % ECDSA::Group::Secp256k1.order, 32) Bitcoin::Key.new(priv_key: private_key.bth) end |
.tweak_public_key(internal_key, merkle_root) ⇒ Bitcoin::Key
Generate tweak public key form internal_pubkey and merkle_root.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/bitcoin/taproot.rb', line 33 def tweak_public_key(internal_key, merkle_root) t = tweak(internal_key, merkle_root) key = Bitcoin::Key.new(priv_key: t.bth, key_type: Key::TYPES[:compressed]) # BIP-341 tweaks lift_x(P), the point with even y. #tweak commits to the x-only key and # #tweak_private_key negates the private key to match, so the point is lifted here too. # An internal key with odd y would otherwise give a Q the tweaked private key cannot spend. internal_point = internal_key.to_point internal_point = internal_point.negate unless internal_point.has_even_y? Bitcoin::Key.from_point(key.to_point + internal_point) end |