Expand description
Pay-to-Contract / Sign-to-Contract
This library provides support for the Pay-to-Contract (P2C) construction, which transforms secp256k1 signing keys into cryptographic commitments to arbitrary data, as well as the Sign-to-Contract (S2C) construction, which does the same for ECDSA or BIP-0340 (Schnorr) signatures produced by the same keys.
It is intended for use within smart contracts unchained in which S2C signatures are used with P2C keys. However, in principle the two constructions are indepedent and there is no need to use them together.
The library provides three main tools:
- The
TweakedKeytype which represents a public key or keypair to which a P2C tweak has been applied; - The
TweakableKeytrait which allows ordinary key types to be tweaked to construct aTweakedKey; - The
TweakHashtrait which defines which hash types can be used to compute these tweaks.
We have provided the Pay2ContractHash and Pay2ContractFullHash which
implement the TweakHash trait, but for production uses, you should define
your own hash.
§Example (BIP-0340 Signing)
use p2c_s2c::secp256k1::{Secp256k1, SecretKey, Keypair};
use p2c_s2c::{TweakedKey, Pay2ContractHash};
let secp = Secp256k1::new();
// Untweaked key
let sk: SecretKey = "0101010101010101010101010101010101010101010101010101010101010101"
.parse()
.unwrap();
let keypair = sk.keypair(&secp);
// P2C-tweaked signing key
let prog = b"this is a thing I'm committing in the pubkey";
let tweaked_keypair = TweakedKey::<_, Pay2ContractHash>::new(&keypair, prog);
let tweaked_xpk = tweaked_keypair.to_x_only_public_key();
// Alternately, can compute the public key from the untweaked public key.
let untweaked_xpk = keypair.x_only_public_key().0;
assert_eq!(
tweaked_xpk,
TweakedKey::new(&untweaked_xpk, prog),
);
// Can verify P2C commitment.
assert!(tweaked_xpk.verify_key_commitment(&untweaked_xpk, prog));
// S2C-tweaked signature
let wit = b"this is a thing I'm committing in the signature";
let msg = b"this is the message I'm actually signing";
let (sig, nonce) = tweaked_keypair.sign_schnorr(&secp, msg, wit);
// Signature verifies as a normal signature
secp.verify_schnorr(&sig, msg, tweaked_xpk.as_inner())
.unwrap();
// With original nonce, can also verify S2C commitment
assert!(tweaked_xpk.verify_schnorr_commitment(&sig, &nonce, wit));Re-exports§
pub extern crate bitcoin_hashes;pub extern crate secp256k1;
Structs§
- Pay2
Contract Full Hash - Tagged hash used for key tweaking for full keys.
- Pay2
Contract Hash - Tagged hash used for key tweaking for x-only keys.
- Sign2
Contract Full Hash - Tagged hash used for nonce tweaking for ECDSA signatures.
- Sign2
Contract Hash - Tagged hash used for nonce tweaking for BIP-0340 (Schnorr) signatures.
- Tweaked
Key - A tweaked key.
Enums§
- Full
- Only allow full keys and ECDSA signatures.
- XOnly
- Only allow x-only keys and BIP-0340 (Schnorr) signatures.
Traits§
- Allowed
Keys Marker - Trait simulating a type-level boolean.
- Pubkey
Tweak Hash - Trait describing a hash which can be used specifically to tweak a public key, for use.
- Tweak
Hash - A trait describing a hash that can be used to tweak a point (either a public key or a signature nonce) in this library.
- Tweakable
Key - A public key or keypair that can be tweaked for use in P2C or S2C.