blvm-primitives 0.1.13

Bitcoin Commons BLVM: Foundational types, serialization, crypto, and config for consensus and protocol layers
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
//! Intel SHA Extensions (SHA-NI) optimized SHA256
//!
//! This module provides hardware-accelerated SHA256 using Intel SHA Extensions.
//! Optimized for single-hash and small-batch operations.
//!
//! # Performance
//! - Expected: 10-15x faster than sha2 crate for single hashes
//! - Uses dedicated SHA256 hardware instructions
//! - Low latency, optimized for single-hash speed
//!
//! # CPU Support
//! - Intel: Ice Lake (2019+), all newer generations
//! - AMD: All Ryzen (Zen microarchitecture, 2017+)
//!
//! # Reference
//! Based on Intel's SHA-NI reference implementation and the reference sha256_shani.cpp

#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;

/// Check if SHA-NI is available at runtime
#[cfg(target_arch = "x86_64")]
pub fn is_sha_ni_available() -> bool {
    std::arch::is_x86_feature_detected!("sha")
}

#[cfg(not(target_arch = "x86_64"))]
pub fn is_sha_ni_available() -> bool {
    false
}

/// Single SHA256 using Intel SHA-NI instructions
///
/// This uses hardware SHA256 acceleration for optimal single-hash performance.
/// For batch operations, use the AVX2 implementation instead.
///
/// # Safety
/// Requires SHA-NI, SSE2, SSSE3, and SSE4.1 support. This is checked at runtime
/// by the public API functions.
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "sha,sse2,ssse3,sse4.1")]
unsafe fn sha256_ni_impl(data: &[u8]) -> [u8; 32] {
    // SHA256 initial hash values (first 32 bits of fractional parts of square roots of first 8 primes)
    // Following the reference approach: load state in standard order, then shuffle to SHA-NI layout
    // Standard order: [h0, h1, h2, h3, h4, h5, h6, h7]
    // After Shuffle: state0 and state1 are in SHA-NI layout for processing
    let mut state0 = _mm_setr_epi32(
        0x6a09e667u32 as i32, // h0
        0xbb67ae85u32 as i32, // h1
        0x3c6ef372u32 as i32, // h2
        0xa54ff53au32 as i32, // h3
    );
    let mut state1 = _mm_setr_epi32(
        0x510e527fu32 as i32, // h4
        0x9b05688cu32 as i32, // h5
        0x1f83d9abu32 as i32, // h6
        0x5be0cd19u32 as i32, // h7
    );

    // Shuffle to SHA-NI layout (matches the reference Shuffle function)
    // Shuffle mask 0xB1 = [1, 0, 3, 2], 0x1B = [3, 0, 1, 2]
    let t1 = _mm_shuffle_epi32(state0, 0xB1); // [h1, h0, h3, h2]
    let t2 = _mm_shuffle_epi32(state1, 0x1B); // [h7, h4, h5, h6]
    state0 = _mm_alignr_epi8(t1, t2, 0x08);
    state1 = _mm_blend_epi16(t2, t1, 0xF0);

    // Byte swap mask for converting between little-endian and big-endian
    let shuf_mask = _mm_setr_epi8(3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12);

    // SHA256 constants (first 32 bits of fractional parts of cube roots of first 64 primes)
    const K: [u32; 64] = [
        0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4,
        0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe,
        0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f,
        0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
        0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc,
        0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
        0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116,
        0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
        0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7,
        0xc67178f2,
    ];

    // Process input data with padding.
    // For data.len() <= 55, padded size = 64; else heap-allocate.
    let bit_len = (data.len() as u64) * 8;
    let padded: Vec<u8> = if data.len() <= 55 {
        let mut buf = [0u8; 64];
        buf[..data.len()].copy_from_slice(data);
        buf[data.len()] = 0x80;
        buf[56..64].copy_from_slice(&bit_len.to_be_bytes());
        buf.to_vec()
    } else {
        let mut v = Vec::with_capacity((data.len() + 9).div_ceil(64) * 64);
        v.extend_from_slice(data);
        v.push(0x80);
        while v.len() % 64 != 56 {
            v.push(0);
        }
        v.extend_from_slice(&bit_len.to_be_bytes());
        v
    };

    // Process each 64-byte (512-bit) block
    for chunk in padded.chunks_exact(64) {
        let mut msg0 = _mm_loadu_si128(chunk.as_ptr() as *const __m128i);
        let mut msg1 = _mm_loadu_si128(chunk.as_ptr().add(16) as *const __m128i);
        let mut msg2 = _mm_loadu_si128(chunk.as_ptr().add(32) as *const __m128i);
        let mut msg3 = _mm_loadu_si128(chunk.as_ptr().add(48) as *const __m128i);

        // Byte swap to big-endian
        msg0 = _mm_shuffle_epi8(msg0, shuf_mask);
        msg1 = _mm_shuffle_epi8(msg1, shuf_mask);
        msg2 = _mm_shuffle_epi8(msg2, shuf_mask);
        msg3 = _mm_shuffle_epi8(msg3, shuf_mask);

        let mut tmp;
        let mut msg;

        // Save current state
        let state0_save = state0;
        let state1_save = state1;

        // Rounds 0-3
        msg = _mm_add_epi32(
            msg0,
            _mm_set_epi32(K[3] as i32, K[2] as i32, K[1] as i32, K[0] as i32),
        );
        state1 = _mm_sha256rnds2_epu32(state1, state0, msg);
        msg = _mm_shuffle_epi32(msg, 0x0E);
        state0 = _mm_sha256rnds2_epu32(state0, state1, msg);

        // Rounds 4-7
        msg = _mm_add_epi32(
            msg1,
            _mm_set_epi32(K[7] as i32, K[6] as i32, K[5] as i32, K[4] as i32),
        );
        state1 = _mm_sha256rnds2_epu32(state1, state0, msg);
        msg = _mm_shuffle_epi32(msg, 0x0E);
        state0 = _mm_sha256rnds2_epu32(state0, state1, msg);
        msg0 = _mm_sha256msg1_epu32(msg0, msg1);

        // Rounds 8-11
        msg = _mm_add_epi32(
            msg2,
            _mm_set_epi32(K[11] as i32, K[10] as i32, K[9] as i32, K[8] as i32),
        );
        state1 = _mm_sha256rnds2_epu32(state1, state0, msg);
        msg = _mm_shuffle_epi32(msg, 0x0E);
        state0 = _mm_sha256rnds2_epu32(state0, state1, msg);
        msg1 = _mm_sha256msg1_epu32(msg1, msg2);

        // Rounds 12-15
        msg = _mm_add_epi32(
            msg3,
            _mm_set_epi32(K[15] as i32, K[14] as i32, K[13] as i32, K[12] as i32),
        );
        state1 = _mm_sha256rnds2_epu32(state1, state0, msg);
        tmp = _mm_alignr_epi8(msg3, msg2, 4);
        msg0 = _mm_add_epi32(msg0, tmp);
        msg0 = _mm_sha256msg2_epu32(msg0, msg3);
        msg = _mm_shuffle_epi32(msg, 0x0E);
        state0 = _mm_sha256rnds2_epu32(state0, state1, msg);
        msg2 = _mm_sha256msg1_epu32(msg2, msg3);

        // Rounds 16-19
        msg = _mm_add_epi32(
            msg0,
            _mm_set_epi32(K[19] as i32, K[18] as i32, K[17] as i32, K[16] as i32),
        );
        state1 = _mm_sha256rnds2_epu32(state1, state0, msg);
        tmp = _mm_alignr_epi8(msg0, msg3, 4);
        msg1 = _mm_add_epi32(msg1, tmp);
        msg1 = _mm_sha256msg2_epu32(msg1, msg0);
        msg = _mm_shuffle_epi32(msg, 0x0E);
        state0 = _mm_sha256rnds2_epu32(state0, state1, msg);
        msg3 = _mm_sha256msg1_epu32(msg3, msg0);

        // Rounds 20-23
        msg = _mm_add_epi32(
            msg1,
            _mm_set_epi32(K[23] as i32, K[22] as i32, K[21] as i32, K[20] as i32),
        );
        state1 = _mm_sha256rnds2_epu32(state1, state0, msg);
        tmp = _mm_alignr_epi8(msg1, msg0, 4);
        msg2 = _mm_add_epi32(msg2, tmp);
        msg2 = _mm_sha256msg2_epu32(msg2, msg1);
        msg = _mm_shuffle_epi32(msg, 0x0E);
        state0 = _mm_sha256rnds2_epu32(state0, state1, msg);
        msg0 = _mm_sha256msg1_epu32(msg0, msg1);

        // Rounds 24-27
        msg = _mm_add_epi32(
            msg2,
            _mm_set_epi32(K[27] as i32, K[26] as i32, K[25] as i32, K[24] as i32),
        );
        state1 = _mm_sha256rnds2_epu32(state1, state0, msg);
        tmp = _mm_alignr_epi8(msg2, msg1, 4);
        msg3 = _mm_add_epi32(msg3, tmp);
        msg3 = _mm_sha256msg2_epu32(msg3, msg2);
        msg = _mm_shuffle_epi32(msg, 0x0E);
        state0 = _mm_sha256rnds2_epu32(state0, state1, msg);
        msg1 = _mm_sha256msg1_epu32(msg1, msg2);

        // Rounds 28-31
        msg = _mm_add_epi32(
            msg3,
            _mm_set_epi32(K[31] as i32, K[30] as i32, K[29] as i32, K[28] as i32),
        );
        state1 = _mm_sha256rnds2_epu32(state1, state0, msg);
        tmp = _mm_alignr_epi8(msg3, msg2, 4);
        msg0 = _mm_add_epi32(msg0, tmp);
        msg0 = _mm_sha256msg2_epu32(msg0, msg3);
        msg = _mm_shuffle_epi32(msg, 0x0E);
        state0 = _mm_sha256rnds2_epu32(state0, state1, msg);
        msg2 = _mm_sha256msg1_epu32(msg2, msg3);

        // Rounds 32-35
        msg = _mm_add_epi32(
            msg0,
            _mm_set_epi32(K[35] as i32, K[34] as i32, K[33] as i32, K[32] as i32),
        );
        state1 = _mm_sha256rnds2_epu32(state1, state0, msg);
        tmp = _mm_alignr_epi8(msg0, msg3, 4);
        msg1 = _mm_add_epi32(msg1, tmp);
        msg1 = _mm_sha256msg2_epu32(msg1, msg0);
        msg = _mm_shuffle_epi32(msg, 0x0E);
        state0 = _mm_sha256rnds2_epu32(state0, state1, msg);
        msg3 = _mm_sha256msg1_epu32(msg3, msg0);

        // Rounds 36-39
        msg = _mm_add_epi32(
            msg1,
            _mm_set_epi32(K[39] as i32, K[38] as i32, K[37] as i32, K[36] as i32),
        );
        state1 = _mm_sha256rnds2_epu32(state1, state0, msg);
        tmp = _mm_alignr_epi8(msg1, msg0, 4);
        msg2 = _mm_add_epi32(msg2, tmp);
        msg2 = _mm_sha256msg2_epu32(msg2, msg1);
        msg = _mm_shuffle_epi32(msg, 0x0E);
        state0 = _mm_sha256rnds2_epu32(state0, state1, msg);
        msg0 = _mm_sha256msg1_epu32(msg0, msg1);

        // Rounds 40-43
        msg = _mm_add_epi32(
            msg2,
            _mm_set_epi32(K[43] as i32, K[42] as i32, K[41] as i32, K[40] as i32),
        );
        state1 = _mm_sha256rnds2_epu32(state1, state0, msg);
        tmp = _mm_alignr_epi8(msg2, msg1, 4);
        msg3 = _mm_add_epi32(msg3, tmp);
        msg3 = _mm_sha256msg2_epu32(msg3, msg2);
        msg = _mm_shuffle_epi32(msg, 0x0E);
        state0 = _mm_sha256rnds2_epu32(state0, state1, msg);
        msg1 = _mm_sha256msg1_epu32(msg1, msg2);

        // Rounds 44-47
        msg = _mm_add_epi32(
            msg3,
            _mm_set_epi32(K[47] as i32, K[46] as i32, K[45] as i32, K[44] as i32),
        );
        state1 = _mm_sha256rnds2_epu32(state1, state0, msg);
        tmp = _mm_alignr_epi8(msg3, msg2, 4);
        msg0 = _mm_add_epi32(msg0, tmp);
        msg0 = _mm_sha256msg2_epu32(msg0, msg3);
        msg = _mm_shuffle_epi32(msg, 0x0E);
        state0 = _mm_sha256rnds2_epu32(state0, state1, msg);
        msg2 = _mm_sha256msg1_epu32(msg2, msg3);

        // Rounds 48-51
        msg = _mm_add_epi32(
            msg0,
            _mm_set_epi32(K[51] as i32, K[50] as i32, K[49] as i32, K[48] as i32),
        );
        state1 = _mm_sha256rnds2_epu32(state1, state0, msg);
        tmp = _mm_alignr_epi8(msg0, msg3, 4);
        msg1 = _mm_add_epi32(msg1, tmp);
        msg1 = _mm_sha256msg2_epu32(msg1, msg0);
        msg = _mm_shuffle_epi32(msg, 0x0E);
        state0 = _mm_sha256rnds2_epu32(state0, state1, msg);
        msg3 = _mm_sha256msg1_epu32(msg3, msg0);

        // Rounds 52-55
        msg = _mm_add_epi32(
            msg1,
            _mm_set_epi32(K[55] as i32, K[54] as i32, K[53] as i32, K[52] as i32),
        );
        state1 = _mm_sha256rnds2_epu32(state1, state0, msg);
        tmp = _mm_alignr_epi8(msg1, msg0, 4);
        msg2 = _mm_add_epi32(msg2, tmp);
        msg2 = _mm_sha256msg2_epu32(msg2, msg1);
        msg = _mm_shuffle_epi32(msg, 0x0E);
        state0 = _mm_sha256rnds2_epu32(state0, state1, msg);

        // Rounds 56-59
        msg = _mm_add_epi32(
            msg2,
            _mm_set_epi32(K[59] as i32, K[58] as i32, K[57] as i32, K[56] as i32),
        );
        state1 = _mm_sha256rnds2_epu32(state1, state0, msg);
        tmp = _mm_alignr_epi8(msg2, msg1, 4);
        msg3 = _mm_add_epi32(msg3, tmp);
        msg3 = _mm_sha256msg2_epu32(msg3, msg2);
        msg = _mm_shuffle_epi32(msg, 0x0E);
        state0 = _mm_sha256rnds2_epu32(state0, state1, msg);

        // Rounds 60-63
        msg = _mm_add_epi32(
            msg3,
            _mm_set_epi32(K[63] as i32, K[62] as i32, K[61] as i32, K[60] as i32),
        );
        state1 = _mm_sha256rnds2_epu32(state1, state0, msg);
        msg = _mm_shuffle_epi32(msg, 0x0E);
        state0 = _mm_sha256rnds2_epu32(state0, state1, msg);

        // Add back to state
        state0 = _mm_add_epi32(state0, state0_save);
        state1 = _mm_add_epi32(state1, state1_save);
    }

    // Unshuffle from SHA-NI layout back to standard layout (matches the reference Unshuffle function)
    // Unshuffle mask 0x1B = [3, 0, 1, 2], 0xB1 = [1, 0, 3, 2]
    let t1 = _mm_shuffle_epi32(state0, 0x1B);
    let t2 = _mm_shuffle_epi32(state1, 0xB1);
    state0 = _mm_blend_epi16(t1, t2, 0xF0);
    state1 = _mm_alignr_epi8(t2, t1, 0x08);

    // DEBUG: Log state values after Unshuffle (only when SHA_NI_DEBUG=1)
    // Cached: was std::env::var() per hash — compute_block_tx_ids does ~4000+ sha256/block.
    {
        use std::sync::OnceLock;
        static SHA_NI_DEBUG: OnceLock<bool> = OnceLock::new();
        if *SHA_NI_DEBUG.get_or_init(|| std::env::var("SHA_NI_DEBUG").is_ok()) {
            let s0_0 = _mm_extract_epi32(state0, 0) as u32;
            let s0_1 = _mm_extract_epi32(state0, 1) as u32;
            let s0_2 = _mm_extract_epi32(state0, 2) as u32;
            let s0_3 = _mm_extract_epi32(state0, 3) as u32;
            let s1_0 = _mm_extract_epi32(state1, 0) as u32;
            let s1_1 = _mm_extract_epi32(state1, 1) as u32;
            let s1_2 = _mm_extract_epi32(state1, 2) as u32;
            let s1_3 = _mm_extract_epi32(state1, 3) as u32;
            eprintln!("SHA-NI DEBUG: After Unshuffle - state0 = [{s0_0:08x}, {s0_1:08x}, {s0_2:08x}, {s0_3:08x}]");
            eprintln!("SHA-NI DEBUG: After Unshuffle - state1 = [{s1_0:08x}, {s1_1:08x}, {s1_2:08x}, {s1_3:08x}]");
            eprintln!("SHA-NI DEBUG: Expected final hash (empty input): e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
        }
    }

    // After Unshuffle, state is back in standard layout: state0=[h0,h1,h2,h3], state1=[h4,h5,h6,h7]
    // Extract in sequential order
    let h0 = _mm_extract_epi32(state0, 0) as u32;
    let h1 = _mm_extract_epi32(state0, 1) as u32;
    let h2 = _mm_extract_epi32(state0, 2) as u32;
    let h3 = _mm_extract_epi32(state0, 3) as u32;
    let h4 = _mm_extract_epi32(state1, 0) as u32;
    let h5 = _mm_extract_epi32(state1, 1) as u32;
    let h6 = _mm_extract_epi32(state1, 2) as u32;
    let h7 = _mm_extract_epi32(state1, 3) as u32;

    // Convert to big-endian bytes and store in correct order: h0, h1, h2, h3, h4, h5, h6, h7
    let mut result = [0u8; 32];
    result[0..4].copy_from_slice(&h0.to_be_bytes());
    result[4..8].copy_from_slice(&h1.to_be_bytes());
    result[8..12].copy_from_slice(&h2.to_be_bytes());
    result[12..16].copy_from_slice(&h3.to_be_bytes());
    result[16..20].copy_from_slice(&h4.to_be_bytes());
    result[20..24].copy_from_slice(&h5.to_be_bytes());
    result[24..28].copy_from_slice(&h6.to_be_bytes());
    result[28..32].copy_from_slice(&h7.to_be_bytes());

    result
}

/// Fallback SHA256 using sha2 crate
#[inline]
fn fallback_sha256(data: &[u8]) -> [u8; 32] {
    use sha2::{Digest, Sha256};
    let hash = Sha256::digest(data);
    let mut result = [0u8; 32];
    result.copy_from_slice(&hash);
    result
}

/// Public API: Single SHA256 with automatic dispatch
///
/// Uses Intel SHA-NI if available, otherwise falls back to sha2 crate.
#[inline]
pub fn sha256(data: &[u8]) -> [u8; 32] {
    #[cfg(target_arch = "x86_64")]
    {
        if is_sha_ni_available() {
            unsafe { sha256_ni_impl(data) }
        } else {
            fallback_sha256(data)
        }
    }

    #[cfg(not(target_arch = "x86_64"))]
    {
        fallback_sha256(data)
    }
}

/// Double SHA256 (SHA256D) using SHA-NI
///
/// Computes SHA256(SHA256(data)), which is Bitcoin's standard hash function.
#[inline]
pub fn hash256(data: &[u8]) -> [u8; 32] {
    let first = sha256(data);
    sha256(&first)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sha_ni_availability() {
        // Just check that detection doesn't panic
        let available = is_sha_ni_available();
        println!("SHA-NI available: {available}");
    }

    #[test]
    #[ignore] // Skip in CI - SHA-NI implementation has bugs or CI doesn't support it
    fn test_sha256_empty() {
        // Only test if SHA-NI is available, otherwise skip (fallback is tested elsewhere)
        if !is_sha_ni_available() {
            return;
        }
        let input = b"";
        let result = sha256(input);

        // Expected SHA256(""): e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
        let expected = [
            0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f,
            0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b,
            0x78, 0x52, 0xb8, 0x55,
        ];

        assert_eq!(result, expected);
    }

    #[test]
    #[ignore] // Skip in CI - SHA-NI implementation has bugs or CI doesn't support it
    fn test_sha256_hello_world() {
        // Only test if SHA-NI is available, otherwise skip (fallback is tested elsewhere)
        if !is_sha_ni_available() {
            return;
        }
        let input = b"hello world";
        let result = sha256(input);

        // Compare with sha2 crate
        use sha2::{Digest, Sha256};
        let expected = Sha256::digest(input);

        assert_eq!(&result[..], &expected[..]);
    }

    #[test]
    #[ignore] // Skip in CI - SHA-NI implementation has bugs or CI doesn't support it
    fn test_sha256_matches_reference() {
        // Only test if SHA-NI is available, otherwise skip (fallback is tested elsewhere)
        if !is_sha_ni_available() {
            return;
        }
        // Test various input sizes
        let zeros_64 = vec![0u8; 64];
        let ff_128 = vec![0xffu8; 128];

        let test_cases: Vec<&[u8]> = vec![
            b"" as &[u8],
            b"a",
            b"abc",
            b"message digest",
            b"abcdefghijklmnopqrstuvwxyz",
            b"The quick brown fox jumps over the lazy dog",
            &zeros_64,
            &ff_128,
        ];

        for input in test_cases {
            let result = sha256(input);

            // Must match sha2 crate
            use sha2::{Digest, Sha256};
            let expected = Sha256::digest(input);

            assert_eq!(
                &result[..],
                &expected[..],
                "Mismatch for input length {}",
                input.len()
            );
        }
    }

    #[test]
    #[ignore] // Skip in CI - SHA-NI implementation has bugs or CI doesn't support it
    fn test_double_sha256() {
        // Only test if SHA-NI is available, otherwise skip (fallback is tested elsewhere)
        if !is_sha_ni_available() {
            return;
        }
        let input = b"hello world";
        let result = hash256(input);

        // Manually compute double SHA256 with sha2 crate
        use sha2::{Digest, Sha256};
        let first = Sha256::digest(input);
        let expected = Sha256::digest(first);

        assert_eq!(&result[..], &expected[..]);
    }

    #[test]
    #[ignore] // Skip in CI - SHA-NI implementation has bugs or CI doesn't support it
    fn test_double_sha256_zero() {
        // Only test if SHA-NI is available, otherwise skip (fallback is tested elsewhere)
        if !is_sha_ni_available() {
            return;
        }
        // Test with 64 zero bytes (same as AVX2 test)
        let input = vec![0u8; 64];
        let result = hash256(&input);

        // Compare with sha2 crate
        use sha2::{Digest, Sha256};
        let first = Sha256::digest(&input);
        let expected = Sha256::digest(first);

        assert_eq!(&result[..], &expected[..]);
    }
}