From c584744fa31dfa0d3f3f16805d32c62930a3a29e Mon Sep 17 00:00:00 2001 From: raph Date: Wed, 30 Oct 2024 09:38:41 +0100 Subject: [PATCH] Add option for auxillary randomness (#42) --- Cargo-minimal.lock | 101 +++++++++++++++++++++++++++++++++++++++++---- Cargo-recent.lock | 101 +++++++++++++++++++++++++++++++++++++++++---- Cargo.lock | 101 +++++++++++++++++++++++++++++++++++++++++---- Cargo.toml | 7 ++-- justfile | 3 ++ src/lib.rs | 19 ++++++++- src/sign.rs | 28 +++++++++---- www/Cargo.lock | 22 +--------- www/Cargo.toml | 2 +- www/index.html | 2 +- 10 files changed, 328 insertions(+), 58 deletions(-) diff --git a/Cargo-minimal.lock b/Cargo-minimal.lock index 6479e57..059f8ab 100644 --- a/Cargo-minimal.lock +++ b/Cargo-minimal.lock @@ -37,16 +37,16 @@ dependencies = [ "base64", "bitcoin", "hex", - "miniscript", "pretty_assertions", + "rand", "snafu", ] [[package]] name = "bitcoin" -version = "0.32.3" +version = "0.32.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0032b0e8ead7074cda7fc4f034409607e3f03a6f71d66ade8a307f79b4d99e73" +checksum = "788902099d47c8682efe6a7afb01c8d58b9794ba66c06affd81c3d6b560743eb" dependencies = [ "base58ck", "bech32", @@ -90,6 +90,12 @@ dependencies = [ "hex-conservative", ] +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "cc" version = "1.1.18" @@ -99,12 +105,29 @@ dependencies = [ "shlex", ] +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "diff" version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "heck" version = "0.5.0" @@ -133,13 +156,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3011d1213f159867b13cfd6ac92d2cd5f1345762c63be3554e84092d85a50bbd" [[package]] -name = "miniscript" -version = "12.2.0" +name = "libc" +version = "0.2.161" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" + +[[package]] +name = "ppv-lite86" +version = "0.2.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "add2d4aee30e4291ce5cffa3a322e441ff4d4bc57b38c8d9bf0e94faa50ab626" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" dependencies = [ - "bech32", - "bitcoin", + "zerocopy", ] [[package]] @@ -170,6 +198,36 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "secp256k1" version = "0.29.1" @@ -233,8 +291,35 @@ version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "yansi" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo-recent.lock b/Cargo-recent.lock index 6479e57..059f8ab 100644 --- a/Cargo-recent.lock +++ b/Cargo-recent.lock @@ -37,16 +37,16 @@ dependencies = [ "base64", "bitcoin", "hex", - "miniscript", "pretty_assertions", + "rand", "snafu", ] [[package]] name = "bitcoin" -version = "0.32.3" +version = "0.32.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0032b0e8ead7074cda7fc4f034409607e3f03a6f71d66ade8a307f79b4d99e73" +checksum = "788902099d47c8682efe6a7afb01c8d58b9794ba66c06affd81c3d6b560743eb" dependencies = [ "base58ck", "bech32", @@ -90,6 +90,12 @@ dependencies = [ "hex-conservative", ] +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "cc" version = "1.1.18" @@ -99,12 +105,29 @@ dependencies = [ "shlex", ] +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "diff" version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "heck" version = "0.5.0" @@ -133,13 +156,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3011d1213f159867b13cfd6ac92d2cd5f1345762c63be3554e84092d85a50bbd" [[package]] -name = "miniscript" -version = "12.2.0" +name = "libc" +version = "0.2.161" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" + +[[package]] +name = "ppv-lite86" +version = "0.2.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "add2d4aee30e4291ce5cffa3a322e441ff4d4bc57b38c8d9bf0e94faa50ab626" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" dependencies = [ - "bech32", - "bitcoin", + "zerocopy", ] [[package]] @@ -170,6 +198,36 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "secp256k1" version = "0.29.1" @@ -233,8 +291,35 @@ version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "yansi" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.lock b/Cargo.lock index 6479e57..059f8ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -37,16 +37,16 @@ dependencies = [ "base64", "bitcoin", "hex", - "miniscript", "pretty_assertions", + "rand", "snafu", ] [[package]] name = "bitcoin" -version = "0.32.3" +version = "0.32.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0032b0e8ead7074cda7fc4f034409607e3f03a6f71d66ade8a307f79b4d99e73" +checksum = "788902099d47c8682efe6a7afb01c8d58b9794ba66c06affd81c3d6b560743eb" dependencies = [ "base58ck", "bech32", @@ -90,6 +90,12 @@ dependencies = [ "hex-conservative", ] +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "cc" version = "1.1.18" @@ -99,12 +105,29 @@ dependencies = [ "shlex", ] +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "diff" version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "heck" version = "0.5.0" @@ -133,13 +156,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3011d1213f159867b13cfd6ac92d2cd5f1345762c63be3554e84092d85a50bbd" [[package]] -name = "miniscript" -version = "12.2.0" +name = "libc" +version = "0.2.161" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" + +[[package]] +name = "ppv-lite86" +version = "0.2.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "add2d4aee30e4291ce5cffa3a322e441ff4d4bc57b38c8d9bf0e94faa50ab626" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" dependencies = [ - "bech32", - "bitcoin", + "zerocopy", ] [[package]] @@ -170,6 +198,36 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "secp256k1" version = "0.29.1" @@ -233,8 +291,35 @@ version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "yansi" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml index ee451c1..132b468 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,10 +13,11 @@ default = [] [dependencies] base64 = "0.22.1" -bitcoin = "0.32.3" -hex = "0.4.3" -miniscript = "12.2.0" +bitcoin = "0.32.4" snafu = { version = "0.8.5", default-features = false, features = ["rust_1_61", "std"] } [dev-dependencies] +hex = "0.4.3" pretty_assertions = "1.4.1" +rand = "0.8.5" + diff --git a/justfile b/justfile index 3d81584..3a7d291 100644 --- a/justfile +++ b/justfile @@ -18,6 +18,9 @@ doc: outdated: cargo outdated -R --workspace +unused: + cargo +nightly udeps + coverage: cargo llvm-cov diff --git a/src/lib.rs b/src/lib.rs index 94ed2c2..8c78ac7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,7 +33,7 @@ type Result = std::result::Result; #[cfg(test)] mod tests { - use {super::*, pretty_assertions::assert_eq}; + use {super::*, pretty_assertions::assert_eq, rand::RngCore}; /// From https://github.com/bitcoin/bips/blob/master/bip-0322.mediawiki#test-vectors /// and https://github.com/ACken2/bip322-js/blob/main/test/Verifier.test.ts @@ -334,4 +334,21 @@ mod tests { ) .is_ok()); } + + #[test] + fn adding_aux_randomness_roundtrips() { + let address = Address::from_str(TAPROOT_ADDRESS).unwrap().assume_checked(); + let message = "Hello World with aux randomness".as_bytes(); + let to_spend = create_to_spend(&address, message).unwrap(); + let to_sign = create_to_sign(&to_spend, None).unwrap(); + let private_key = PrivateKey::from_wif(WIF_PRIVATE_KEY).unwrap(); + + let mut aux_rand = [0u8; 32]; + rand::thread_rng().fill_bytes(&mut aux_rand); + + let witness = + create_message_signature_taproot(&to_spend, &to_sign, private_key, Some(aux_rand)); + + assert!(verify_simple(&address, message, witness).is_ok()); + } } diff --git a/src/sign.rs b/src/sign.rs index 3cf84fe..8e55763 100644 --- a/src/sign.rs +++ b/src/sign.rs @@ -71,7 +71,7 @@ pub fn sign_full( if program_len != 32 { return Err(Error::NotKeyPathSpend); } - create_message_signature_taproot(&to_spend, &to_sign, private_key) + create_message_signature_taproot(&to_spend, &to_sign, private_key, None) } _ => { return Err(Error::UnsupportedAddress { @@ -95,7 +95,8 @@ pub fn sign_full( to_sign.extract_tx().context(error::TransactionExtract) } -fn create_message_signature_p2wpkh( +/// Sign for segwit inputs +pub fn create_message_signature_p2wpkh( to_spend_tx: &Transaction, to_sign: &Psbt, private_key: PrivateKey, @@ -143,10 +144,12 @@ fn create_message_signature_p2wpkh( witness.to_owned() } -fn create_message_signature_taproot( +/// Sign for taproot inputs +pub fn create_message_signature_taproot( to_spend_tx: &Transaction, to_sign: &Psbt, private_key: PrivateKey, + aux_rand: Option<[u8; 32]>, ) -> Witness { let mut to_sign = to_sign.clone(); @@ -175,11 +178,20 @@ fn create_message_signature_taproot( .tap_tweak(&secp, to_sign.inputs[0].tap_merkle_root) .to_inner(); - let signature = secp.sign_schnorr_no_aux_rand( - &secp256k1::Message::from_digest_slice(sighash.as_ref()) - .expect("should be cryptographically secure hash"), - &key_pair, - ); + let signature = if let Some(aux_rand) = aux_rand { + secp.sign_schnorr_with_aux_rand( + &secp256k1::Message::from_digest_slice(sighash.as_ref()) + .expect("should be cryptographically secure hash"), + &key_pair, + &aux_rand, + ) + } else { + secp.sign_schnorr_no_aux_rand( + &secp256k1::Message::from_digest_slice(sighash.as_ref()) + .expect("should be cryptographically secure hash"), + &key_pair, + ) + }; let witness = sighash_cache .witness_mut(0) diff --git a/www/Cargo.lock b/www/Cargo.lock index 923847d..d1e14ad 100644 --- a/www/Cargo.lock +++ b/www/Cargo.lock @@ -36,16 +36,14 @@ version = "0.0.8" dependencies = [ "base64", "bitcoin", - "hex", - "miniscript", "snafu", ] [[package]] name = "bitcoin" -version = "0.32.3" +version = "0.32.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0032b0e8ead7074cda7fc4f034409607e3f03a6f71d66ade8a307f79b4d99e73" +checksum = "788902099d47c8682efe6a7afb01c8d58b9794ba66c06affd81c3d6b560743eb" dependencies = [ "base58ck", "bech32", @@ -113,12 +111,6 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" -[[package]] -name = "hex" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" - [[package]] name = "hex-conservative" version = "0.2.1" @@ -140,16 +132,6 @@ version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" -[[package]] -name = "miniscript" -version = "12.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "add2d4aee30e4291ce5cffa3a322e441ff4d4bc57b38c8d9bf0e94faa50ab626" -dependencies = [ - "bech32", - "bitcoin", -] - [[package]] name = "once_cell" version = "1.19.0" diff --git a/www/Cargo.toml b/www/Cargo.toml index 5dc74af..1874ac4 100644 --- a/www/Cargo.toml +++ b/www/Cargo.toml @@ -8,7 +8,7 @@ crate-type = ["cdylib"] [dependencies] bip322 = { version = "0.0.8", path = "../" } -bitcoin = "0.32.3" +bitcoin = "0.32.4" wasm-bindgen = "0.2.95" [patch.crates-io] diff --git a/www/index.html b/www/index.html index e328f2a..22b47c7 100644 --- a/www/index.html +++ b/www/index.html @@ -21,7 +21,7 @@