Skip to content

Commit 2412aa3

Browse files
committed
p2qrh: modified spend test to reflect p2qrh specification requirement that parity bit is always 1
1 parent 99dc227 commit 2412aa3

File tree

1 file changed

+10
-56
lines changed

1 file changed

+10
-56
lines changed

bip-0360/ref-impl/rust/tests/p2qrh_spend.rs

Lines changed: 10 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@ use p2qrh_ref::{
1818
serialize_script,
1919
};
2020

21-
static TEST_VECTORS: Lazy<TestVectors> = Lazy::new(|| {
22-
let bip360_test_vectors = include_str!("../tests/data/p2qrh_spend.json");
23-
let test_vectors: TestVectors = serde_json::from_str(bip360_test_vectors).unwrap();
24-
assert_eq!(test_vectors.version, 1);
25-
test_vectors
26-
});
27-
28-
static P2QRH_SINGLE_LEAF_SCRIPT_TREE_NO_SIGS_TEST: &str = "p2qrh_single_leaf_script_tree_no_sigs";
2921

3022
/* The rust-bitcoin crate does not provide a single high-level API that builds the full Taproot script-path witness stack for you.
3123
It does expose all the necessary types and primitives to build it manually and correctly.
@@ -68,10 +60,15 @@ fn test_script_path_spend_signatures() {
6860
let input_tx_id_bytes =
6961
hex::decode("d1c40446c65456a9b11a9dddede31ee34b8d3df83788d98f690225d2958bfe3c").unwrap();
7062

63+
// OP_PUSHBYTES_32 6d4ddc0e47d2e8f82cbe2fc2d0d749e7bd3338112cecdc76d8f831ae6620dbe0 OP_CHECKSIG
7164
let input_leaf_script_bytes: Vec<u8> =
7265
hex::decode("206d4ddc0e47d2e8f82cbe2fc2d0d749e7bd3338112cecdc76d8f831ae6620dbe0ac").unwrap();
66+
67+
// Modified from learnmeabitcoin example
68+
// Changed from c0 to c1 control byte to reflect p2qrh specification: The parity bit of the control byte is always 1 since P2QRH does not have a key-spend path.
7369
let input_control_block_bytes: Vec<u8> =
74-
hex::decode("c0924c163b385af7093440184af6fd6244936d1288cbb41cc3812286d3f83a3329").unwrap();
70+
hex::decode("c1924c163b385af7093440184af6fd6244936d1288cbb41cc3812286d3f83a3329").unwrap();
71+
7572
let input_script_pubkey_bytes: Vec<u8> =
7673
hex::decode("5120f3778defe5173a9bf7169575116224f961c03c725c0e98b8da8f15df29194b80")
7774
.unwrap();
@@ -81,7 +78,10 @@ fn test_script_path_spend_signatures() {
8178

8279
let test_sighash_bytes: Vec<u8> = hex::decode("752453d473e511a0da2097d664d69fe5eb89d8d9d00eab924b42fc0801a980c9").unwrap();
8380
let test_p2wpkh_signature_bytes: Vec<u8> = hex::decode("01769105cbcbdcaaee5e58cd201ba3152477fda31410df8b91b4aee2c4864c7700615efb425e002f146a39ca0a4f2924566762d9213bd33f825fad83977fba7f01").unwrap();
84-
let test_witness_bytes: Vec<u8> = hex::decode("034101769105cbcbdcaaee5e58cd201ba3152477fda31410df8b91b4aee2c4864c7700615efb425e002f146a39ca0a4f2924566762d9213bd33f825fad83977fba7f0122206d4ddc0e47d2e8f82cbe2fc2d0d749e7bd3338112cecdc76d8f831ae6620dbe0ac21c0924c163b385af7093440184af6fd6244936d1288cbb41cc3812286d3f83a3329").unwrap();
81+
82+
// Modified from learnmeabitcoin example
83+
// Changed from c0 to c1 control byte to reflect p2qrh specification: The parity bit of the control byte is always 1 since P2QRH does not have a key-spend path.
84+
let test_witness_bytes: Vec<u8> = hex::decode("034101769105cbcbdcaaee5e58cd201ba3152477fda31410df8b91b4aee2c4864c7700615efb425e002f146a39ca0a4f2924566762d9213bd33f825fad83977fba7f0122206d4ddc0e47d2e8f82cbe2fc2d0d749e7bd3338112cecdc76d8f831ae6620dbe0ac21c1924c163b385af7093440184af6fd6244936d1288cbb41cc3812286d3f83a3329").unwrap();
8585

8686
let mut txid_little_endian = input_tx_id_bytes.clone();
8787
txid_little_endian.reverse();
@@ -176,49 +176,3 @@ fn test_script_path_spend_signatures() {
176176
let derived_witness_hex = hex::encode(derived_witness_vec);
177177
info!("derived_witness_hex: {:?}", derived_witness_hex);
178178
}
179-
180-
#[test]
181-
fn test_p2qrh_single_leaf_script_tree_no_sigs() {
182-
let _ = env_logger::try_init(); // Use try_init to avoid reinitialization error
183-
184-
let test_vectors: &TestVectors = &*TEST_VECTORS;
185-
let test_vector: &TestVector = test_vectors
186-
.test_vector_map
187-
.get(P2QRH_SINGLE_LEAF_SCRIPT_TREE_NO_SIGS_TEST)
188-
.unwrap();
189-
190-
let mut witness: Witness = Witness::new();
191-
192-
test_vector
193-
.given
194-
.script_inputs
195-
.as_ref()
196-
.unwrap()
197-
.iter()
198-
.for_each(|tv_script_input| {
199-
let script_input_bytes = hex::decode(tv_script_input).unwrap();
200-
witness.push(script_input_bytes);
201-
});
202-
203-
// Hint: use https://learnmeabitcoin.com/technical/script/
204-
let tv_script_hex = test_vector.given.script_hex.as_ref().unwrap();
205-
let script_buf: ScriptBuf = ScriptBuf::from(hex::decode(tv_script_hex).unwrap());
206-
debug!("script asm: {}", script_buf.to_asm_string());
207-
witness.push(script_buf.to_bytes());
208-
209-
let tv_control_block = test_vector.given.control_block.as_ref().unwrap();
210-
let control_block_bytes = hex::decode(tv_control_block).unwrap();
211-
witness.push(control_block_bytes);
212-
213-
debug!("witness: {:?}", witness);
214-
215-
// Concatenate all witness elements into a single hex string
216-
let mut witness_hex_string = String::new();
217-
for element in witness.iter() {
218-
witness_hex_string.push_str(&hex::encode(element));
219-
}
220-
debug!("witness hex: {}", witness_hex_string);
221-
222-
let expected_witness = test_vector.expected.witness.as_ref().unwrap();
223-
assert_eq!(&witness_hex_string, expected_witness);
224-
}

0 commit comments

Comments
 (0)