confidential-script-lib 0.3.2

Emulate Bitcoin script by converting script-path spends to key-path spends
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
// Written in 2025 by Joshua Doman <joshsdoman@gmail.com>
// SPDX-License-Identifier: CC0-1.0

//! # Confidential Script Library
//!
//! Emulate Bitcoin script by converting valid script-path spends to key-path spends. Intended for use within a Trusted Execution Environment (TEE), the library validates unlocking conditions and then authorizes the transaction using a deterministically derived private key.
//!
//! This approach enables confidential execution of complex script, including opcodes not yet supported by the Bitcoin protocol. The actual on-chain footprint is a minimal key-path spend, preserving privacy and efficiency.
//!
//! ## Overview
//!
//! The library operates on a two-step process: emulation and signing.
//!
//! 1.  **Emulation**: A transaction is constructed using an input spending a *real* `previous_outpoint` with a witness that is a script-path spend from an *emulated* P2TR `script_pubkey`. The library validates this emulated witness using a `Verifier`, which matches the API of `rust-bitcoinkernel`. If compiled with the `bitcoinkernel` feature, users can use the actual kernel as the default verifier, or they can provide an alternative verifier that enforces a different set of rules (ex: a fork of `bitcoinkernel` that supports Simplicity).
//!
//! 2.  **Signing**: If the transaction is valid, the library uses the provided parent private key and the merkle root of the *emulated* script path spend to derive a child private key, which corresponds to the internal public key of the *actual* UTXO being spent. The library then updates the transaction with a key-path spend signed with this child key.
//!
//! To facilitate offline generation of the real `script_pubkey`, the child key is derived from the parent key using a non-hardened HMAC-SHA512 derivation scheme. This lets users generate addresses using the parent _public_ key, while the parent private key is secured elsewhere.
//!
//! This library is intended to be run within a TEE, which is securely provisioned with the parent private key. This decouples script execution from on-chain settlement, keeping execution private and enabling new functionality with minimal trust assumptions.
//!
//! ## Failsafe Mechanism: Backup Script Path
//!
//! To prevent funds from being irrecoverably locked if the TEE becomes unavailable, the library allows for the inclusion of an optional `backup_merkle_root` when creating the actual on-chain address. This backup merkle root defines the alternative spending paths that are available independently of the TEE.
//!
//! A common use case for this feature is to include a timelocked recovery script (e.g., using `OP_CHECKSEQUENCEVERIFY`). If the primary TEE-based execution path becomes unavailable for any reason, the owner can wait for the timelock to expire and then recover the funds using a pre-defined backup script. This provides a crucial failsafe, ensuring that users retain ultimate control over their assets.
//!
//! ## Extensibility for Proposed Soft Forks
//!
//! This library can be used to emulate proposed upgrades, such as new opcodes like `OP_CAT` or `OP_CTV` or new scripting languages like Simplicity. It accepts any verifier that adheres to the `rust-bitcoinkernel` API, allowing developers to experiment with new functionality by forking the kernel, without waiting for a soft fork to gain adoption on mainnet.
//!

// Coding conventions
#![deny(unsafe_code)]
#![deny(non_upper_case_globals)]
#![deny(non_camel_case_types)]
#![deny(non_snake_case)]
#![deny(unused_mut)]
#![deny(dead_code)]
#![deny(unused_imports)]
#![deny(missing_docs)]

#[cfg(not(any(feature = "std")))]
compile_error!("`std` must be enabled");

use bitcoin::{
    Address, Network, ScriptBuf, TapNodeHash, TapSighashType, TapTweakHash, Transaction, TxOut,
    Witness, XOnlyPublicKey,
    hashes::Hash,
    key::Secp256k1,
    secp256k1,
    secp256k1::{Keypair, Message, PublicKey, Scalar, SecretKey, constants::CURVE_ORDER},
    sighash::{Annex, Prevouts, SighashCache},
    taproot::{ControlBlock, Signature},
};
use hmac::{Hmac, Mac};
use num_bigint::BigUint;
use sha2::{Digest, Sha256, Sha512};
use std::collections::HashMap;
use std::fmt;

pub use bitcoin;

/// The initial byte in a data-carrying taproot annex
pub const TAPROOT_ANNEX_DATA_CARRYING_TAG: u8 = 0;

/// Comprehensive error type for verify_and_sign operations
#[derive(Debug)]
pub enum Error {
    /// Verification failed
    VerificationFailed(String),
    /// Wrapped secp256k1 errors from cryptographic operations
    Secp256k1(secp256k1::Error),
    /// Invalid control block format or size
    InvalidControlBlock,
    /// Unable to calculate sighash
    InvalidSighash,
    /// Missing spent outputs
    MissingSpentOutputs,
    /// Unexpected input scriptPubKey
    UnexpectedInput,
}

/// Trait to abstract the behavior of the bitcoin script verifier, allowing
/// users to provide their own verifier.
pub trait Verifier {
    /// Verify one or more scripts in a bitcoin transaction.
    ///
    /// # Arguments
    /// * `script_pubkeys` - The scriptPubKeys to verify (by index).
    /// * `tx_to` - The transaction with emulated witness data.
    /// * `spent_outputs` - The outputs being spent by the transaction.
    ///
    /// # Errors
    /// Returns `Error` if verification fails.
    fn verify(
        &self,
        script_pubkeys: &HashMap<usize, ScriptBuf>,
        tx_to: &Transaction,
        spent_outputs: &[TxOut],
    ) -> Result<(), Error>;
}

/// The default `Verifier` implementation that uses `bitcoinkernel`.
#[cfg(feature = "bitcoinkernel")]
pub struct DefaultVerifier;

#[cfg(feature = "bitcoinkernel")]
impl Verifier for DefaultVerifier {
    fn verify(
        &self,
        script_pubkeys: &HashMap<usize, ScriptBuf>,
        tx_to: &Transaction,
        spent_outputs: &[TxOut],
    ) -> Result<(), Error> {
        let mut amounts = Vec::new();
        let mut outputs = Vec::new();
        for txout in spent_outputs {
            let amount = txout
                .value
                .to_signed()
                .map_err(|_| Error::VerificationFailed("invalid amount".to_string()))?
                .to_sat();
            let script = bitcoinkernel::ScriptPubkey::try_from(txout.script_pubkey.as_bytes())
                .map_err(|e| Error::VerificationFailed(e.to_string()))?;

            amounts.push(amount);
            outputs.push(bitcoinkernel::TxOut::new(&script, amount));
        }

        let tx_bytes = bitcoin::consensus::serialize(tx_to);
        let tx_to = &bitcoinkernel::Transaction::try_from(tx_bytes.as_slice())
            .map_err(|e| Error::VerificationFailed(e.to_string()))?;

        for (&i, script_pubkey) in script_pubkeys {
            let amount = amounts.get(i).cloned();
            let script_pubkey = &bitcoinkernel::ScriptPubkey::try_from(script_pubkey.as_bytes())
                .map_err(|e| Error::VerificationFailed(e.to_string()))?;

            bitcoinkernel::verify(script_pubkey, amount, tx_to, i, None, &outputs)
                .map_err(|e| Error::VerificationFailed(e.to_string()))?;
        }

        Ok(())
    }
}

/// Verifies emulated Bitcoin script and signs the corresponding transaction.
///
/// This function performs script verification using a Verifier, which verifies one or
/// more emulated P2TR inputs. If successful, it derives for each emulated input an
/// XOnlyPublicKey from the parent key and the emulated merkle root, which is then tweaked
/// with an optional backup merkle root to derive the input's actual spent UTXO. This is
/// then key-path signed with `SIGHASH_DEFAULT`.
///
/// If the emulated script-path spend includes a data-carrying annex (begins with 0x50
/// followed by 0x00), the annex is included in the key-path spend. Otherwise, the annex
/// is dropped.
///
/// Non-emulated inputs are identified by the input type. An emulated input must be a
/// P2TR script-path spend, with a derived scriptPubKey that does not match that of the
/// actual spent output.
///
/// Each signature uses a unique `aux_rand` by hashing the provided `aux_rand` with the
/// index of the input, using SHA256.
///
/// # Arguments
/// * `verifier` - The verifier to use for script validation
/// * `emulated_tx_to` - Emulated transaction to verify and sign
/// * `actual_spent_outputs` - Actual outputs being spent
/// * `aux_rand` - Auxiliary random data for signing
/// * `parent_key` - Parent secret key used to derive child key for signing
/// * `backup_merkle_roots` - Optional merkle roots for backup script path spending
///
/// # Errors
/// Returns error if verification fails, key derivation fails, or signing fails
pub fn verify_and_sign<V: Verifier>(
    verifier: &V,
    emulated_tx_to: &Transaction,
    actual_spent_outputs: &[TxOut],
    aux_rand: &[u8; 32],
    parent_key: SecretKey,
    backup_merkle_roots: HashMap<usize, TapNodeHash>,
) -> Result<Transaction, Error> {
    // The spent script_pubkeys of the emulated inputs
    let mut emulated_script_pubkeys: HashMap<usize, ScriptBuf> = HashMap::new();

    // The child keys of each emulated input
    let mut child_keys_by_index: HashMap<usize, SecretKey> = HashMap::new();

    // Check if missing a spent output
    if actual_spent_outputs.len() < emulated_tx_to.input.len() {
        return Err(Error::MissingSpentOutputs);
    }

    // Loop through all inputs and update `emulated_script_pubkeys` and `child_keys_by_index`
    let secp = Secp256k1::new();
    for (i, input) in emulated_tx_to.input.clone().into_iter().enumerate() {
        // Must be P2TR script-path spend
        let (Some(true), Some(control_block), Some(tapleaf)) = (
            actual_spent_outputs[i]
                .script_pubkey
                .is_p2tr()
                .then_some(true),
            input.witness.taproot_control_block(),
            input.witness.taproot_leaf_script(),
        ) else {
            continue;
        };

        // Must be valid control block
        let Ok(control_block) = ControlBlock::decode(control_block) else {
            return Err(Error::InvalidControlBlock);
        };

        // Calculate merkle root
        let mut merkle_root = TapNodeHash::from_script(tapleaf.script, tapleaf.version);
        for elem in &control_block.merkle_branch {
            merkle_root = TapNodeHash::from_node_hashes(merkle_root, *elem);
        }

        // Create emulated script pubkey
        let emulated_address = Address::p2tr(
            &secp,
            control_block.internal_key,
            Some(merkle_root),
            Network::Bitcoin,
        );

        // Non-emulated input if actual scriptPubKey matches emulated scriptPubKey
        if actual_spent_outputs[i].script_pubkey == emulated_address.script_pubkey() {
            continue;
        }

        // Get actual internal key and child key to be tweaked for signing
        let child_key = derive_child_secret_key(parent_key, merkle_root.to_byte_array())?;
        let (internal_key, _) = child_key.public_key(&secp).x_only_public_key();
        child_keys_by_index.insert(i, child_key);

        // Actual input scriptPubKey must match expected actual scriptPubKey
        let backup_merkle_root = backup_merkle_roots.get(&i).cloned();
        let actual_address =
            Address::p2tr(&secp, internal_key, backup_merkle_root, Network::Bitcoin);
        if actual_spent_outputs[i].script_pubkey != actual_address.script_pubkey() {
            return Err(Error::UnexpectedInput);
        }

        // Add emulated spent script_pubkey
        emulated_script_pubkeys.insert(i, emulated_address.script_pubkey());
    }

    // Must satisfy verifier
    verifier.verify(
        &emulated_script_pubkeys,
        emulated_tx_to,
        actual_spent_outputs,
    )?;

    let mut tx = emulated_tx_to.clone();
    for &i in emulated_script_pubkeys.keys() {
        // Get annex if it is data-carrying (leading byte is 0x00)
        let annex = tx.input[i]
            .witness
            .taproot_annex()
            .filter(|bytes| bytes.len() > 1 && bytes[1] == TAPROOT_ANNEX_DATA_CARRYING_TAG)
            .and_then(|bytes| Annex::new(bytes).ok());

        // Create sighash for the input
        let mut sighash_cache = SighashCache::new(&tx);
        let sighash_bytes = sighash_cache
            .taproot_signature_hash(
                i,
                &Prevouts::All(actual_spent_outputs),
                annex.clone(),
                None,
                TapSighashType::Default,
            )
            .map_err(|_| Error::InvalidSighash)?;
        let mut sighash = [0u8; 32];
        sighash.copy_from_slice(sighash_bytes.as_byte_array());

        // Lookup child key and prepare for tweak
        let child_key = child_keys_by_index.get(&i).unwrap();
        let (internal_key, parity) = child_key.public_key(&secp).x_only_public_key();
        let child_key_for_tweak = if parity == secp256k1::Parity::Odd {
            child_key.negate()
        } else {
            *child_key
        };

        // Calculate the taproot tweaked private key for keypath spending
        let backup_merkle_root = backup_merkle_roots.get(&i).cloned();
        let tweak = TapTweakHash::from_key_and_tweak(internal_key, backup_merkle_root);
        let tweaked_secret_key = child_key_for_tweak.add_tweak(&tweak.to_scalar())?;
        let tweaked_keypair = Keypair::from_secret_key(&secp, &tweaked_secret_key);

        // Hash the original aux_rand with the index to create a unique aux_rand
        let mut hasher = Sha256::new();
        hasher.update(aux_rand);
        hasher.update((i as u64).to_le_bytes());
        let aux_rand: [u8; 32] = hasher.finalize().into();

        // Sign the sighash
        let message = Message::from_digest(sighash);
        let signature = secp.sign_schnorr_with_aux_rand(&message, &tweaked_keypair, &aux_rand);

        // Create taproot signature (schnorr signature + sighash type)
        let tap_signature = Signature {
            signature,
            sighash_type: TapSighashType::Default,
        };

        // Create witness for keypath spend (include annex if data-carrying annex is present)
        let mut witness = Witness::new();
        witness.push(tap_signature.to_vec());
        if let Some(annex) = annex {
            witness.push(annex.as_bytes());
        }
        tx.input[i].witness = witness;
    }

    Ok(tx)
}

/// Generates P2TR address from a parent public key and the emulated merkle root,
/// with an optional backup merkle root.
///
/// # Arguments
/// * `parent_key` - The parent public key
/// * `emulated_merkle_root` - The merkle root of the emulated input
/// * `backup_merkle_root` - Optional merkle root for backup script path spending
/// * `network` - The network to generate the address for
///
/// # Errors
/// Returns an error if key derivation fails
pub fn generate_address(
    parent_key: PublicKey,
    emulated_merkle_root: TapNodeHash,
    backup_merkle_root: Option<TapNodeHash>,
    network: Network,
) -> Result<Address, secp256k1::Error> {
    let secp = Secp256k1::new();
    let child_key = derive_child_public_key(parent_key, emulated_merkle_root.to_byte_array())?;
    let internal_key = XOnlyPublicKey::from(child_key);
    let address = Address::p2tr(&secp, internal_key, backup_merkle_root, network);

    Ok(address)
}

/// Derives a child secret key from a parent secret key and emulated merkle root
/// using HMAC-SHA512 based key derivation (non-hardened derivation).
fn derive_child_secret_key(
    parent_key: SecretKey,
    emulated_merkle_root: [u8; 32],
) -> Result<SecretKey, secp256k1::Error> {
    let secp = Secp256k1::new();

    // Derive parent public key from parent secret
    let parent_public = parent_key.public_key(&secp);

    // Create HMAC-SHA512 with parent public key and merkle root
    let mut mac = Hmac::<Sha512>::new_from_slice(&parent_public.serialize())
        .expect("PublicKey serialization should always be non-empty");
    mac.update(&emulated_merkle_root);
    let hmac_result = mac.finalize().into_bytes();

    // Use first 32 bytes for key material
    let mut key_material = [0u8; 32];
    key_material.copy_from_slice(&hmac_result[..32]);
    let scalar = reduce_mod_order(&key_material);

    // Add the key material to parent private key
    parent_key.add_tweak(&scalar)
}

/// Derives a child public key from a parent public key and emulated merkle root
/// This allows public key derivation without access to private keys.
fn derive_child_public_key(
    parent_public: PublicKey,
    emulated_merkle_root: [u8; 32],
) -> Result<PublicKey, secp256k1::Error> {
    let secp = Secp256k1::new();

    // Create HMAC-SHA512 with parent public key as key
    let mut mac = Hmac::<Sha512>::new_from_slice(&parent_public.serialize())
        .expect("PublicKey serialization should always be non-empty");
    mac.update(&emulated_merkle_root);
    let hmac_result = mac.finalize().into_bytes();

    // Use first 32 bytes as scalar for point multiplication
    let mut key_material = [0u8; 32];
    key_material.copy_from_slice(&hmac_result[..32]);
    let scalar = reduce_mod_order(&key_material);

    // Add scalar * G to parent public key
    parent_public.add_exp_tweak(&secp, &scalar)
}

/// Safely reduces a 32-byte array modulo the secp256k1 curve order
fn reduce_mod_order(bytes: &[u8; 32]) -> Scalar {
    // Keep trying to create a scalar until we get a valid one
    // In practice, this loop will almost always execute only once
    let mut attempt = *bytes;
    loop {
        match Scalar::from_be_bytes(attempt) {
            Ok(scalar) => return scalar,
            Err(_) => {
                // If the value is too large, subtract the curve order
                // This is equivalent to modular reduction
                attempt = subtract_curve_order(&attempt);
            }
        }
    }
}

/// Subtract the secp256k1 curve order from a 32-byte big-endian number
fn subtract_curve_order(bytes: &[u8; 32]) -> [u8; 32] {
    let value = BigUint::from_bytes_be(bytes);
    let order = BigUint::from_bytes_be(&CURVE_ORDER);
    let reduced = value % order;

    let mut result = [0u8; 32];
    let reduced_bytes = reduced.to_bytes_be();
    let offset = 32 - reduced_bytes.len();
    result[offset..].copy_from_slice(&reduced_bytes);
    result
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::VerificationFailed(e) => {
                write!(f, "Verification failed: {e}")
            }
            Error::Secp256k1(e) => {
                write!(f, "Secp256k1 cryptographic operation failed: {e}")
            }
            Error::InvalidControlBlock => {
                write!(f, "Input has invalid control block")
            }
            Error::InvalidSighash => {
                write!(f, "Unable to calculate sighash for input")
            }
            Error::MissingSpentOutputs => {
                write!(f, "Missing spent outputs")
            }
            Error::UnexpectedInput => {
                write!(f, "Unexpected input scriptPubKey")
            }
        }
    }
}

impl From<secp256k1::Error> for Error {
    fn from(error: secp256k1::Error) -> Self {
        Error::Secp256k1(error)
    }
}

#[cfg(test)]
#[cfg(feature = "bitcoinkernel")]
mod kernel_tests {
    use super::*;
    use bitcoin::{
        Address, Amount, Network, OutPoint, Script, ScriptBuf, Transaction, TxIn, TxOut, Txid,
        Witness,
        consensus::encode::serialize,
        hashes::Hash,
        key::UntweakedPublicKey,
        taproot::{LeafVersion, TaprootBuilder},
    };

    fn create_test_transaction_single_input() -> Transaction {
        Transaction {
            version: bitcoin::transaction::Version::TWO,
            lock_time: bitcoin::locktime::absolute::LockTime::ZERO,
            input: vec![TxIn {
                previous_output: OutPoint::null(),
                script_sig: ScriptBuf::new(),
                sequence: bitcoin::Sequence::ENABLE_RBF_NO_LOCKTIME,
                witness: Witness::new(),
            }],
            output: vec![TxOut {
                value: Amount::from_sat(100000),
                script_pubkey: ScriptBuf::new_op_return([]),
            }],
        }
    }

    fn create_test_transaction_multi_input() -> Transaction {
        Transaction {
            version: bitcoin::transaction::Version::TWO,
            lock_time: bitcoin::locktime::absolute::LockTime::ZERO,
            input: vec![
                TxIn {
                    previous_output: OutPoint::null(),
                    script_sig: ScriptBuf::new(),
                    sequence: bitcoin::Sequence::ENABLE_RBF_NO_LOCKTIME,
                    witness: Witness::new(),
                },
                TxIn {
                    previous_output: OutPoint::new(Txid::all_zeros(), 1),
                    script_sig: ScriptBuf::new(),
                    sequence: bitcoin::Sequence::ENABLE_RBF_NO_LOCKTIME,
                    witness: Witness::new(),
                },
            ],
            output: vec![TxOut {
                value: Amount::from_sat(100000),
                script_pubkey: ScriptBuf::new_op_return([]),
            }],
        }
    }

    #[test]
    fn test_missing_spent_outputs() {
        let result = verify_and_sign(
            &DefaultVerifier,
            &create_test_transaction_single_input(),
            &[],
            &[1u8; 32],
            SecretKey::from_slice(&[1u8; 32]).unwrap(),
            HashMap::new(),
        );

        assert!(matches!(result, Err(Error::MissingSpentOutputs)));
    }

    #[test]
    fn test_unexpected_input_script_pubkey() {
        let secp = Secp256k1::new();

        // 1. Create a dummy internal key
        let internal_secret = SecretKey::from_slice(&[1u8; 32]).unwrap();
        let internal_key = UntweakedPublicKey::from(internal_secret.public_key(&secp));

        // 2. Create OP_TRUE script leaf
        let op_true_script = Script::builder()
            .push_opcode(bitcoin::opcodes::OP_TRUE)
            .into_script();

        // 3. Build the taproot tree with single OP_TRUE leaf
        let taproot_builder = TaprootBuilder::new()
            .add_leaf(0, op_true_script.clone())
            .unwrap();
        let taproot_spend_info = taproot_builder.finalize(&secp, internal_key).unwrap();

        // 4. Get the control block for our OP_TRUE leaf
        let control_block = taproot_spend_info
            .control_block(&(op_true_script.clone(), LeafVersion::TapScript))
            .unwrap();

        // 5. Create the witness stack for script path spending
        let mut witness = Witness::new();
        witness.push(op_true_script.as_bytes());
        witness.push(control_block.serialize());

        // 6. Create emulated transaction
        let mut emulated_tx = create_test_transaction_single_input();
        emulated_tx.input[0].witness = witness;

        // 7. Create input UTXO with unexpected scriptPubKey
        let dummy_p2tr_address = Address::p2tr(&secp, internal_key, None, Network::Bitcoin);
        let txout = TxOut {
            value: Amount::from_sat(100000),
            script_pubkey: dummy_p2tr_address.script_pubkey(),
        };

        let result = verify_and_sign(
            &DefaultVerifier,
            &emulated_tx,
            std::slice::from_ref(&txout),
            &[1u8; 32],
            SecretKey::from_slice(&[1u8; 32]).unwrap(),
            HashMap::new(),
        );

        assert!(matches!(result, Err(Error::UnexpectedInput)));
    }

    #[test]
    fn test_verify_and_sign_single_input_single_leaf() {
        let secp = Secp256k1::new();

        // 1. Create a dummy internal key
        let internal_secret = SecretKey::from_slice(&[1u8; 32]).unwrap();
        let internal_key = UntweakedPublicKey::from(internal_secret.public_key(&secp));

        // 2. Create OP_TRUE script leaf
        let op_true_script = Script::builder()
            .push_opcode(bitcoin::opcodes::OP_TRUE)
            .into_script();

        // 3. Build the taproot tree with single OP_TRUE leaf
        let taproot_builder = TaprootBuilder::new()
            .add_leaf(0, op_true_script.clone())
            .unwrap();
        let taproot_spend_info = taproot_builder.finalize(&secp, internal_key).unwrap();

        // 4. Get the control block for our OP_TRUE leaf
        let control_block = taproot_spend_info
            .control_block(&(op_true_script.clone(), LeafVersion::TapScript))
            .unwrap();

        // 5. Create the witness stack for script path spending
        let mut witness = Witness::new();
        witness.push(op_true_script.as_bytes());
        witness.push(control_block.serialize());

        // 6. Create emulated transaction
        let mut emulated_tx = create_test_transaction_single_input();
        emulated_tx.input[0].witness = witness;

        // 7. Create actual child secret
        let aux_rand = [1u8; 32];
        let parent_secret = SecretKey::from_slice(&[1u8; 32]).unwrap();
        let child_secret = derive_child_secret_key(
            parent_secret,
            taproot_spend_info.merkle_root().unwrap().to_byte_array(),
        )
        .unwrap();

        // 8. Create actual P2TR outputs
        let actual_internal_key = XOnlyPublicKey::from(child_secret.public_key(&secp));
        let actual_address = Address::p2tr(&secp, actual_internal_key, None, Network::Bitcoin);
        let actual_spent_outputs = [TxOut {
            value: Amount::from_sat(100_000),
            script_pubkey: actual_address.script_pubkey(),
        }];

        // 9. Verify and sign actual transaction
        let actual_tx = verify_and_sign(
            &DefaultVerifier,
            &emulated_tx,
            &actual_spent_outputs,
            &aux_rand,
            parent_secret,
            HashMap::new(),
        )
        .unwrap();

        let mut actual_outputs = Vec::new();
        for txout in actual_spent_outputs {
            let amount = txout.value.to_signed().unwrap().to_sat();
            let script =
                bitcoinkernel::ScriptPubkey::try_from(txout.script_pubkey.as_bytes()).unwrap();
            actual_outputs.push(bitcoinkernel::TxOut::new(&script, amount));
        }

        // 10. Verify the actual transaction was properly signed
        let verify_result = bitcoinkernel::verify(
            &bitcoinkernel::ScriptPubkey::try_from(actual_address.script_pubkey().as_bytes())
                .unwrap(),
            Some(100_000),
            &bitcoinkernel::Transaction::try_from(serialize(&actual_tx).as_slice()).unwrap(),
            0,
            None,
            &actual_outputs,
        );

        assert!(verify_result.is_ok());
        assert_eq!(actual_tx.input[0].witness.len(), 1);
    }

    #[test]
    fn test_verify_and_sign_single_input_multiple_leaves() {
        let secp = Secp256k1::new();

        // 1. Create a dummy internal key
        let internal_secret = SecretKey::from_slice(&[1u8; 32]).unwrap();
        let internal_key = UntweakedPublicKey::from(internal_secret.public_key(&secp));

        // 2. Create script leaves
        let op_true_script = Script::builder()
            .push_opcode(bitcoin::opcodes::OP_TRUE)
            .into_script();
        let op_false_script = Script::builder()
            .push_opcode(bitcoin::opcodes::OP_FALSE)
            .into_script();

        // 3. Build the taproot tree with two leaves
        let taproot_builder = TaprootBuilder::new()
            .add_leaf(1, op_true_script.clone())
            .unwrap()
            .add_leaf(1, op_false_script.clone())
            .unwrap();
        let taproot_spend_info = taproot_builder.finalize(&secp, internal_key).unwrap();

        // 4. Get the control block for our OP_TRUE leaf
        let control_block = taproot_spend_info
            .control_block(&(op_true_script.clone(), LeafVersion::TapScript))
            .unwrap();

        // 5. Create the witness stack for script path spending
        let mut witness = Witness::new();
        witness.push(op_true_script.as_bytes());
        witness.push(control_block.serialize());

        // 6. Create emulated transaction
        let mut emulated_tx = create_test_transaction_single_input();
        emulated_tx.input[0].witness = witness;

        // 7. Create actual child secret
        let aux_rand = [1u8; 32];
        let parent_secret = SecretKey::from_slice(&[1u8; 32]).unwrap();
        let child_secret = derive_child_secret_key(
            parent_secret,
            taproot_spend_info.merkle_root().unwrap().to_byte_array(),
        )
        .unwrap();

        // 8. Create actual P2TR outputs
        let actual_internal_key = XOnlyPublicKey::from(child_secret.public_key(&secp));
        let actual_address = Address::p2tr(&secp, actual_internal_key, None, Network::Bitcoin);
        let actual_spent_outputs = [TxOut {
            value: Amount::from_sat(100_000),
            script_pubkey: actual_address.script_pubkey(),
        }];

        // 9. Verify and sign actual transaction
        let actual_tx = verify_and_sign(
            &DefaultVerifier,
            &emulated_tx,
            &actual_spent_outputs,
            &aux_rand,
            parent_secret,
            HashMap::new(),
        )
        .unwrap();

        let mut actual_outputs = Vec::new();
        for txout in actual_spent_outputs {
            let amount = txout.value.to_signed().unwrap().to_sat();
            let script =
                bitcoinkernel::ScriptPubkey::try_from(txout.script_pubkey.as_bytes()).unwrap();
            actual_outputs.push(bitcoinkernel::TxOut::new(&script, amount));
        }

        // 10. Verify the actual transaction was properly signed
        let verify_result = bitcoinkernel::verify(
            &bitcoinkernel::ScriptPubkey::try_from(actual_address.script_pubkey().as_bytes())
                .unwrap(),
            Some(100_000),
            &bitcoinkernel::Transaction::try_from(serialize(&actual_tx).as_slice()).unwrap(),
            0,
            None,
            &actual_outputs,
        );

        assert!(verify_result.is_ok());
        assert_eq!(actual_tx.input[0].witness.len(), 1);
    }

    #[test]
    fn test_verify_and_sign_single_input_with_backup() {
        let secp = Secp256k1::new();

        // 1. Create a dummy internal key
        let internal_secret = SecretKey::from_slice(&[1u8; 32]).unwrap();
        let internal_key = UntweakedPublicKey::from(internal_secret.public_key(&secp));

        // 2. Create OP_TRUE script leaf
        let op_true_script = Script::builder()
            .push_opcode(bitcoin::opcodes::OP_TRUE)
            .into_script();

        // 3. Build the taproot tree with single OP_TRUE leaf
        let taproot_builder = TaprootBuilder::new()
            .add_leaf(0, op_true_script.clone())
            .unwrap();
        let taproot_spend_info = taproot_builder.finalize(&secp, internal_key).unwrap();

        // 4. Get the control block for our OP_TRUE leaf
        let control_block = taproot_spend_info
            .control_block(&(op_true_script.clone(), LeafVersion::TapScript))
            .unwrap();

        // 5. Create the witness stack for script path spending
        let mut witness = Witness::new();
        witness.push(op_true_script.as_bytes());
        witness.push(control_block.serialize());

        // 6. Create emulated transaction
        let mut emulated_tx = create_test_transaction_single_input();
        emulated_tx.input[0].witness = witness;

        // 7. Create actual child secret
        let aux_rand = [1u8; 32];
        let parent_secret = SecretKey::from_slice(&[1u8; 32]).unwrap();
        let child_secret = derive_child_secret_key(
            parent_secret,
            taproot_spend_info.merkle_root().unwrap().to_byte_array(),
        )
        .unwrap();

        // 8. Create actual P2TR outputs
        let actual_backup_merkle_root = taproot_spend_info.merkle_root();
        let actual_internal_key = XOnlyPublicKey::from(child_secret.public_key(&secp));
        let actual_address = Address::p2tr(
            &secp,
            actual_internal_key,
            actual_backup_merkle_root,
            Network::Bitcoin,
        );
        let actual_spent_outputs = [TxOut {
            value: Amount::from_sat(100_000),
            script_pubkey: actual_address.script_pubkey(),
        }];

        // 9. Verify and sign actual transaction
        let actual_tx = verify_and_sign(
            &DefaultVerifier,
            &emulated_tx,
            &actual_spent_outputs,
            &aux_rand,
            parent_secret,
            HashMap::from([(0, actual_backup_merkle_root.unwrap())]),
        )
        .unwrap();

        let mut actual_outputs = Vec::new();
        for txout in actual_spent_outputs {
            let amount = txout.value.to_signed().unwrap().to_sat();
            let script =
                bitcoinkernel::ScriptPubkey::try_from(txout.script_pubkey.as_bytes()).unwrap();
            actual_outputs.push(bitcoinkernel::TxOut::new(&script, amount));
        }

        // 10. Verify the actual transaction was properly signed
        let verify_result = bitcoinkernel::verify(
            &bitcoinkernel::ScriptPubkey::try_from(actual_address.script_pubkey().as_bytes())
                .unwrap(),
            Some(100_000),
            &bitcoinkernel::Transaction::try_from(serialize(&actual_tx).as_slice()).unwrap(),
            0,
            None,
            &actual_outputs,
        );

        assert!(verify_result.is_ok());
        assert_eq!(actual_tx.input[0].witness.len(), 1);
    }

    #[test]
    fn test_verify_and_sign_single_input_single_leaf_with_data_carrying_annex() {
        let secp = Secp256k1::new();

        // 1. Create a dummy internal key
        let internal_secret = SecretKey::from_slice(&[1u8; 32]).unwrap();
        let internal_key = UntweakedPublicKey::from(internal_secret.public_key(&secp));

        // 2. Create OP_TRUE script leaf
        let op_true_script = Script::builder()
            .push_opcode(bitcoin::opcodes::OP_TRUE)
            .into_script();

        // 3. Build the taproot tree with single OP_TRUE leaf
        let taproot_builder = TaprootBuilder::new()
            .add_leaf(0, op_true_script.clone())
            .unwrap();
        let taproot_spend_info = taproot_builder.finalize(&secp, internal_key).unwrap();

        // 4. Get the control block for our OP_TRUE leaf
        let control_block = taproot_spend_info
            .control_block(&(op_true_script.clone(), LeafVersion::TapScript))
            .unwrap();

        // 5. Create data-carrying annex
        let annex: &[u8] = &[0x50, TAPROOT_ANNEX_DATA_CARRYING_TAG, 0x01, 0x02, 0x03];

        // 6. Create the witness stack for script path spending
        let mut witness = Witness::new();
        witness.push(op_true_script.as_bytes());
        witness.push(control_block.serialize());
        witness.push(annex);

        // 7. Create emulated transaction
        let mut emulated_tx = create_test_transaction_single_input();
        emulated_tx.input[0].witness = witness;

        // 8. Create actual child secret
        let aux_rand = [1u8; 32];
        let parent_secret = SecretKey::from_slice(&[1u8; 32]).unwrap();
        let child_secret = derive_child_secret_key(
            parent_secret,
            taproot_spend_info.merkle_root().unwrap().to_byte_array(),
        )
        .unwrap();

        // 9. Create actual P2TR outputs
        let actual_internal_key = XOnlyPublicKey::from(child_secret.public_key(&secp));
        let actual_address = Address::p2tr(&secp, actual_internal_key, None, Network::Bitcoin);
        let actual_spent_outputs = [TxOut {
            value: Amount::from_sat(100_000),
            script_pubkey: actual_address.script_pubkey(),
        }];

        // 10. Verify and sign actual transaction
        let actual_tx = verify_and_sign(
            &DefaultVerifier,
            &emulated_tx,
            &actual_spent_outputs,
            &aux_rand,
            parent_secret,
            HashMap::new(),
        )
        .unwrap();

        let mut actual_outputs = Vec::new();
        for txout in actual_spent_outputs {
            let amount = txout.value.to_signed().unwrap().to_sat();
            let script =
                bitcoinkernel::ScriptPubkey::try_from(txout.script_pubkey.as_bytes()).unwrap();
            actual_outputs.push(bitcoinkernel::TxOut::new(&script, amount));
        }

        // 11. Verify the actual transaction was properly signed
        let verify_result = bitcoinkernel::verify(
            &bitcoinkernel::ScriptPubkey::try_from(actual_address.script_pubkey().as_bytes())
                .unwrap(),
            Some(100_000),
            &bitcoinkernel::Transaction::try_from(serialize(&actual_tx).as_slice()).unwrap(),
            0,
            None,
            &actual_outputs,
        );

        assert!(verify_result.is_ok());
        assert_eq!(actual_tx.input[0].witness.len(), 2);
        assert_eq!(&actual_tx.input[0].witness[1], annex);
    }

    #[test]
    fn test_verify_and_sign_single_input_single_leaf_with_non_data_carrying_annex() {
        let secp = Secp256k1::new();

        // 1. Create a dummy internal key
        let internal_secret = SecretKey::from_slice(&[1u8; 32]).unwrap();
        let internal_key = UntweakedPublicKey::from(internal_secret.public_key(&secp));

        // 2. Create OP_TRUE script leaf
        let op_true_script = Script::builder()
            .push_opcode(bitcoin::opcodes::OP_TRUE)
            .into_script();

        // 3. Build the taproot tree with single OP_TRUE leaf
        let taproot_builder = TaprootBuilder::new()
            .add_leaf(0, op_true_script.clone())
            .unwrap();
        let taproot_spend_info = taproot_builder.finalize(&secp, internal_key).unwrap();

        // 4. Get the control block for our OP_TRUE leaf
        let control_block = taproot_spend_info
            .control_block(&(op_true_script.clone(), LeafVersion::TapScript))
            .unwrap();

        // 5. Create non-data-carrying annex
        let annex: &[u8] = &[0x50, 0x01, 0x02, 0x03];

        // 6. Create the witness stack for script path spending
        let mut witness = Witness::new();
        witness.push(op_true_script.as_bytes());
        witness.push(control_block.serialize());
        witness.push(annex);

        // 7. Create emulated transaction
        let mut emulated_tx = create_test_transaction_single_input();
        emulated_tx.input[0].witness = witness;

        // 8. Create actual child secret
        let aux_rand = [1u8; 32];
        let parent_secret = SecretKey::from_slice(&[1u8; 32]).unwrap();
        let child_secret = derive_child_secret_key(
            parent_secret,
            taproot_spend_info.merkle_root().unwrap().to_byte_array(),
        )
        .unwrap();

        // 9. Create actual P2TR outputs
        let actual_internal_key = XOnlyPublicKey::from(child_secret.public_key(&secp));
        let actual_address = Address::p2tr(&secp, actual_internal_key, None, Network::Bitcoin);
        let actual_spent_outputs = [TxOut {
            value: Amount::from_sat(100_000),
            script_pubkey: actual_address.script_pubkey(),
        }];

        // 10. Verify and sign actual transaction
        let actual_tx = verify_and_sign(
            &DefaultVerifier,
            &emulated_tx,
            &actual_spent_outputs,
            &aux_rand,
            parent_secret,
            HashMap::new(),
        )
        .unwrap();

        let mut actual_outputs = Vec::new();
        for txout in actual_spent_outputs {
            let amount = txout.value.to_signed().unwrap().to_sat();
            let script =
                bitcoinkernel::ScriptPubkey::try_from(txout.script_pubkey.as_bytes()).unwrap();
            actual_outputs.push(bitcoinkernel::TxOut::new(&script, amount));
        }

        // 11. Verify the actual transaction was properly signed
        let verify_result = bitcoinkernel::verify(
            &bitcoinkernel::ScriptPubkey::try_from(actual_address.script_pubkey().as_bytes())
                .unwrap(),
            Some(100_000),
            &bitcoinkernel::Transaction::try_from(serialize(&actual_tx).as_slice()).unwrap(),
            0,
            None,
            &actual_outputs,
        );

        assert!(verify_result.is_ok());
        assert_eq!(actual_tx.input[0].witness.len(), 1);
    }

    #[test]
    fn test_verify_and_sign_multi_input_tx() {
        let secp = Secp256k1::new();

        // 1. Create a dummy internal key
        let internal_secret = SecretKey::from_slice(&[1u8; 32]).unwrap();
        let internal_key = UntweakedPublicKey::from(internal_secret.public_key(&secp));

        // 2. Create OP_TRUE script leaf
        let op_true_script = Script::builder()
            .push_opcode(bitcoin::opcodes::OP_TRUE)
            .into_script();

        // 3. Build the taproot tree with single OP_TRUE leaf
        let taproot_builder = TaprootBuilder::new()
            .add_leaf(0, op_true_script.clone())
            .unwrap();
        let taproot_spend_info = taproot_builder.finalize(&secp, internal_key).unwrap();

        // 4. Get the control block for our OP_TRUE leaf
        let control_block = taproot_spend_info
            .control_block(&(op_true_script.clone(), LeafVersion::TapScript))
            .unwrap();

        // 5. Create the witness stack for script path spending
        let mut witness = Witness::new();
        witness.push(op_true_script.as_bytes());
        witness.push(control_block.serialize());

        // 6. Create emulated transaction
        let mut emulated_tx = create_test_transaction_multi_input();
        emulated_tx.input[1].witness = witness;

        // 7. Create actual child secret
        let aux_rand = [1u8; 32];
        let parent_secret = SecretKey::from_slice(&[1u8; 32]).unwrap();
        let child_secret = derive_child_secret_key(
            parent_secret,
            taproot_spend_info.merkle_root().unwrap().to_byte_array(),
        )
        .unwrap();

        // 8. Create actual P2TR outputs
        let actual_internal_key = XOnlyPublicKey::from(child_secret.public_key(&secp));
        let actual_address = Address::p2tr(&secp, actual_internal_key, None, Network::Bitcoin);
        let actual_spent_outputs = [
            TxOut {
                value: Amount::from_sat(200_000),
                script_pubkey: actual_address.script_pubkey(),
            },
            TxOut {
                value: Amount::from_sat(100_000),
                script_pubkey: actual_address.script_pubkey(),
            },
        ];

        // 9. Verify and sign actual transaction
        let actual_tx = verify_and_sign(
            &DefaultVerifier,
            &emulated_tx,
            &actual_spent_outputs,
            &aux_rand,
            parent_secret,
            HashMap::new(),
        )
        .unwrap();

        let mut actual_outputs = Vec::new();
        for txout in actual_spent_outputs {
            let amount = txout.value.to_signed().unwrap().to_sat();
            let script =
                bitcoinkernel::ScriptPubkey::try_from(txout.script_pubkey.as_bytes()).unwrap();
            actual_outputs.push(bitcoinkernel::TxOut::new(&script, amount));
        }

        // 10. Verify the actual transaction was properly signed
        let verify_result = bitcoinkernel::verify(
            &bitcoinkernel::ScriptPubkey::try_from(actual_address.script_pubkey().as_bytes())
                .unwrap(),
            Some(100_000),
            &bitcoinkernel::Transaction::try_from(serialize(&actual_tx).as_slice()).unwrap(),
            1,
            None,
            &actual_outputs,
        );

        assert!(verify_result.is_ok());
        assert_eq!(actual_tx.input[1].witness.len(), 1);
    }
}

#[cfg(test)]
mod non_kernel_tests {
    use super::*;
    use bitcoin::{
        Script,
        key::{Secp256k1, UntweakedPublicKey},
        taproot::TaprootBuilder,
    };

    #[test]
    fn test_generate_address() {
        let secp = Secp256k1::new();

        // 1. Create emulated script
        let emulated_script = Script::builder()
            .push_opcode(bitcoin::opcodes::OP_TRUE)
            .into_script();

        // 2. Build the taproot tree and create emulated merkle root
        let taproot_builder = TaprootBuilder::new()
            .add_leaf(0, emulated_script.clone())
            .unwrap();
        let dummy_internal_secret = SecretKey::from_slice(&[1u8; 32]).unwrap();
        let dummy_internal_key = UntweakedPublicKey::from(dummy_internal_secret.public_key(&secp));
        let taproot_spend_info = taproot_builder.finalize(&secp, dummy_internal_key).unwrap();
        let emulated_merkle_root = taproot_spend_info.merkle_root().unwrap();

        // 3. Create backup merkle root
        let backup_merkle_root = emulated_merkle_root;

        // 4. Generate an on-chain address
        let internal_secret = SecretKey::from_slice(&[1u8; 32]).unwrap();
        let master_public_key: PublicKey = internal_secret.public_key(&secp);
        let onchain_address = generate_address(
            master_public_key,
            emulated_merkle_root,
            Some(backup_merkle_root),
            Network::Bitcoin,
        );

        assert!(onchain_address.is_ok());
    }

    #[test]
    fn test_public_private_key_derivation_consistency() {
        let parent_secret = SecretKey::from_slice(&[1u8; 32]).unwrap();
        let parent_public = parent_secret.public_key(&Secp256k1::new());
        let merkle_root = [42u8; 32];

        let child_secret = derive_child_secret_key(parent_secret, merkle_root).unwrap();
        let child_public_from_secret = child_secret.public_key(&Secp256k1::new());
        let child_public_direct = derive_child_public_key(parent_public, merkle_root).unwrap();

        assert_eq!(child_public_from_secret, child_public_direct);
    }

    #[test]
    fn test_curve_order_reduction() {
        let max_bytes = [0xFF; 32];
        let reduced = reduce_mod_order(&max_bytes);
        // Should not panic and should be valid scalar
        #[allow(clippy::useless_conversion)]
        let _ = Scalar::from(reduced);
    }
}