pocx_hashlib 1.0.5

Universal cryptographic library for Proof-of-Capacity consensus mechanism. Includes SIMD-optimized Shabal256 hashing and nonce generation with support for SSE2, AVX, AVX2, and AVX512.
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
// Copyright (c) 2025 Proof of Capacity Consortium
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

//! Batch proof validation for server-side PoC verification.
//!
//! Pools and wallets receive proofs from many miners — each with different
//! `(account, seed, nonce, compression)` parameters. This module provides
//! heterogeneous SIMD nonce generation where each lane processes a completely
//! independent triple, avoiding the 100% scalar fallback that occurs when
//! calling `generate_nonces(..., num_nonces=1)` for individual work units.

use crate::error::{PoCXHashError, Result};
use crate::noncegen_batch_32::generate_and_extract_scoop_32;
use crate::noncegen_common::*;
use crate::shabal256::shabal256_lite;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Mutex;

// SIMD batch imports
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
use crate::noncegen_batch_128::generate_and_extract_scoops_128;
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
use crate::noncegen_batch_256::generate_and_extract_scoops_256;
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
use crate::noncegen_batch_512::generate_and_extract_scoops_512;

#[cfg(target_arch = "aarch64")]
use crate::noncegen_batch_neon::generate_and_extract_scoops_neon;

// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------

/// Controls whether a quality mismatch stops the entire batch.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SurrenderPolicy {
    /// Stop batch on first mismatch (block validation).
    StopOnFirst,
    /// Mark individual mismatches but continue all (pool validation).
    ContinueAll,
}

/// Input for validating a single PoC proof.
#[derive(Clone, Debug)]
pub struct ProofInput {
    pub address_payload: [u8; 20],
    pub seed: [u8; 32],
    pub nonce: u64,
    pub compression: u8,
    pub block_height: u64,
    pub generation_signature: [u8; 32],
    pub base_target: u64,
    /// If set, enables early surrender when computed quality mismatches.
    pub claimed_quality: Option<u64>,
}

/// Result of validating a single PoC proof.
#[derive(Clone, Debug)]
pub struct ValidationResult {
    pub quality: u64,
    pub deadline: u64,
    pub is_valid: bool,
    pub error: Option<PoCXHashError>,
}

/// Batch result with early surrender tracking.
#[derive(Clone, Debug)]
pub struct BatchValidationResult {
    pub results: Vec<ValidationResult>,
    pub early_surrender: bool,
    pub surrender_index: Option<usize>,
}

// ---------------------------------------------------------------------------
// Internal types
// ---------------------------------------------------------------------------

#[derive(Clone, Debug)]
struct WorkUnit {
    proof_index: usize,
    address_payload: [u8; 20],
    seed: [u8; 32],
    base_nonce: u64,
    scoop: u64,
}

struct ProofAccumulator {
    xor_result: Mutex<[u8; SCOOP_SIZE]>,
    #[allow(dead_code)]
    units_completed: AtomicUsize,
    #[allow(dead_code)]
    units_expected: usize,
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Validate a batch of PoC proofs using SIMD-accelerated nonce generation.
///
/// Each proof is expanded into work units based on its compression level,
/// processed in SIMD-width batches, and finalized with quality computation.
/// Supports early surrender when `claimed_quality` is set and mismatches.
pub fn validate_proofs(
    inputs: &[ProofInput],
    policy: SurrenderPolicy,
) -> Result<BatchValidationResult> {
    if inputs.is_empty() {
        return Ok(BatchValidationResult {
            results: vec![],
            early_surrender: false,
            surrender_index: None,
        });
    }

    // Step 1: Work expansion
    let mut work_units = Vec::new();
    let mut accumulators = Vec::new();

    for (idx, input) in inputs.iter().enumerate() {
        let scoop = crate::calculate_scoop(input.block_height, &input.generation_signature);
        let units = expand_work_units(idx, input, scoop);
        let expected = units.len();
        work_units.extend(units);
        accumulators.push(ProofAccumulator {
            xor_result: Mutex::new([0u8; SCOOP_SIZE]),
            units_completed: AtomicUsize::new(0),
            units_expected: expected,
        });
    }

    let surrender = AtomicBool::new(false);

    // Step 2+3: SIMD batch processing + XOR accumulation
    let total_work = work_units.len();
    let simd_width = detect_simd_width();
    let hw_threads = std::thread::available_parallelism()
        .map(|n| n.get())
        .unwrap_or(1);
    let max_threads = hw_threads.saturating_sub(1).max(1);
    let min_work_per_thread = simd_width;
    let num_threads = max_threads.min(total_work.div_ceil(min_work_per_thread));

    if num_threads <= 1 {
        // Single-threaded fast path
        process_work_range(&work_units, &accumulators, &surrender);
    } else {
        // Multi-threaded via std::thread::scope
        let chunk_size = total_work.div_ceil(num_threads);
        std::thread::scope(|s| {
            for chunk_start in (0..total_work).step_by(chunk_size) {
                let chunk_end = std::cmp::min(chunk_start + chunk_size, total_work);
                let chunk = &work_units[chunk_start..chunk_end];
                let accs = &accumulators;
                let surr = &surrender;
                s.spawn(move || {
                    process_work_range(chunk, accs, surr);
                });
            }
        });
    }

    // Step 4: Quality finalization
    let mut results = Vec::with_capacity(inputs.len());
    let mut early_surrender_flag = false;
    let mut surrender_index = None;

    for (idx, input) in inputs.iter().enumerate() {
        let xor_result = accumulators[idx].xor_result.lock().unwrap();
        let quality = shabal256_lite(&xor_result[..], &input.generation_signature);
        let deadline = quality.checked_div(input.base_target).unwrap_or(u64::MAX);

        let (is_valid, error) = if let Some(claimed) = input.claimed_quality {
            if quality == claimed {
                (true, None)
            } else {
                if policy == SurrenderPolicy::StopOnFirst {
                    early_surrender_flag = true;
                    if surrender_index.is_none() {
                        surrender_index = Some(idx);
                    }
                }
                (
                    false,
                    Some(PoCXHashError::QualityMismatch {
                        expected: claimed,
                        actual: quality,
                        proof_index: idx,
                    }),
                )
            }
        } else {
            (true, None)
        };

        results.push(ValidationResult {
            quality,
            deadline,
            is_valid,
            error,
        });
    }

    Ok(BatchValidationResult {
        results,
        early_surrender: early_surrender_flag,
        surrender_index,
    })
}

/// Validate a single PoC proof (convenience wrapper).
pub fn validate_proof(input: &ProofInput) -> Result<ValidationResult> {
    let batch = validate_proofs(std::slice::from_ref(input), SurrenderPolicy::ContinueAll)?;
    Ok(batch.results.into_iter().next().unwrap())
}

// ---------------------------------------------------------------------------
// Work expansion
// ---------------------------------------------------------------------------

fn expand_work_units(proof_index: usize, input: &ProofInput, scoop: u64) -> Vec<WorkUnit> {
    let warp = input.nonce / NUM_SCOOPS as u64;
    let nonce_in_warp = input.nonce % NUM_SCOOPS as u64;
    let num_uncompressed = 1u64 << input.compression as u64;

    let mut units = Vec::with_capacity(num_uncompressed as usize);
    for i in 0..num_uncompressed {
        let (scoop_x, nonce_in_warp_x) = if i % 2 == 0 {
            (scoop, nonce_in_warp)
        } else {
            (nonce_in_warp, scoop)
        };
        let warp_x = num_uncompressed * warp + i;
        let nonce_x = warp_x * NUM_SCOOPS as u64 + nonce_in_warp_x;

        units.push(WorkUnit {
            proof_index,
            address_payload: input.address_payload,
            seed: input.seed,
            base_nonce: nonce_x,
            scoop: scoop_x,
        });
    }
    units
}

// ---------------------------------------------------------------------------
// SIMD dispatch + processing
// ---------------------------------------------------------------------------

fn detect_simd_width() -> usize {
    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    {
        if is_x86_feature_detected!("avx512f") {
            return 16;
        }
        if is_x86_feature_detected!("avx2") {
            return 8;
        }
        if is_x86_feature_detected!("avx") || is_x86_feature_detected!("sse2") {
            return 4;
        }
    }
    #[cfg(target_arch = "aarch64")]
    {
        return 4; // NEON always available
    }
    #[allow(unreachable_code)]
    1
}

fn process_work_range(
    work_units: &[WorkUnit],
    accumulators: &[ProofAccumulator],
    surrender: &AtomicBool,
) {
    let mut i = 0;
    while i < work_units.len() {
        if surrender.load(Ordering::Relaxed) {
            break;
        }
        let remaining = work_units.len() - i;

        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
        {
            if is_x86_feature_detected!("avx512f") && remaining >= 16 {
                process_batch_simd::<16>(&work_units[i..i + 16], accumulators);
                i += 16;
                continue;
            }
            if is_x86_feature_detected!("avx2") && remaining >= 8 {
                process_batch_simd::<8>(&work_units[i..i + 8], accumulators);
                i += 8;
                continue;
            }
            if (is_x86_feature_detected!("avx") || is_x86_feature_detected!("sse2"))
                && remaining >= 4
            {
                process_batch_simd::<4>(&work_units[i..i + 4], accumulators);
                i += 4;
                continue;
            }
        }

        #[cfg(target_arch = "aarch64")]
        {
            if remaining >= 4 {
                process_batch_simd::<4>(&work_units[i..i + 4], accumulators);
                i += 4;
                continue;
            }
        }

        // Scalar fallback
        process_single_scalar(&work_units[i], accumulators);
        i += 1;
    }
}

fn process_single_scalar(wu: &WorkUnit, accumulators: &[ProofAccumulator]) {
    let scoop_data =
        generate_and_extract_scoop_32(&wu.address_payload, &wu.seed, wu.base_nonce, wu.scoop);
    accumulate_scoop(wu.proof_index, &scoop_data, accumulators);
}

fn process_batch_simd<const N: usize>(work_units: &[WorkUnit], accumulators: &[ProofAccumulator]) {
    debug_assert_eq!(work_units.len(), N);

    match N {
        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
        16 => {
            let mut payloads = [[0u8; 20]; 16];
            let mut seeds = [[0u8; 32]; 16];
            let mut nonces = [0u64; 16];
            let mut scoops = [0u64; 16];
            for (j, wu) in work_units.iter().enumerate() {
                payloads[j] = wu.address_payload;
                seeds[j] = wu.seed;
                nonces[j] = wu.base_nonce;
                scoops[j] = wu.scoop;
            }
            let results = generate_and_extract_scoops_512(&payloads, &seeds, &nonces, &scoops);
            for (j, wu) in work_units.iter().enumerate() {
                accumulate_scoop(wu.proof_index, &results[j], accumulators);
            }
        }
        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
        8 => {
            let mut payloads = [[0u8; 20]; 8];
            let mut seeds = [[0u8; 32]; 8];
            let mut nonces = [0u64; 8];
            let mut scoops = [0u64; 8];
            for (j, wu) in work_units.iter().enumerate() {
                payloads[j] = wu.address_payload;
                seeds[j] = wu.seed;
                nonces[j] = wu.base_nonce;
                scoops[j] = wu.scoop;
            }
            let results = generate_and_extract_scoops_256(&payloads, &seeds, &nonces, &scoops);
            for (j, wu) in work_units.iter().enumerate() {
                accumulate_scoop(wu.proof_index, &results[j], accumulators);
            }
        }
        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
        4 => {
            process_batch_4lane_x86(work_units, accumulators);
        }
        #[cfg(target_arch = "aarch64")]
        4 => {
            let mut payloads = [[0u8; 20]; 4];
            let mut seeds = [[0u8; 32]; 4];
            let mut nonces = [0u64; 4];
            let mut scoops = [0u64; 4];
            for (j, wu) in work_units.iter().enumerate() {
                payloads[j] = wu.address_payload;
                seeds[j] = wu.seed;
                nonces[j] = wu.base_nonce;
                scoops[j] = wu.scoop;
            }
            let results = generate_and_extract_scoops_neon(&payloads, &seeds, &nonces, &scoops);
            for (j, wu) in work_units.iter().enumerate() {
                accumulate_scoop(wu.proof_index, &results[j], accumulators);
            }
        }
        _ => {
            // Scalar fallback for unsupported widths
            for wu in work_units {
                process_single_scalar(wu, accumulators);
            }
        }
    }
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn process_batch_4lane_x86(work_units: &[WorkUnit], accumulators: &[ProofAccumulator]) {
    let mut payloads = [[0u8; 20]; 4];
    let mut seeds = [[0u8; 32]; 4];
    let mut nonces = [0u64; 4];
    let mut scoops = [0u64; 4];
    for (j, wu) in work_units.iter().enumerate() {
        payloads[j] = wu.address_payload;
        seeds[j] = wu.seed;
        nonces[j] = wu.base_nonce;
        scoops[j] = wu.scoop;
    }
    let results = generate_and_extract_scoops_128(&payloads, &seeds, &nonces, &scoops);
    for (j, wu) in work_units.iter().enumerate() {
        accumulate_scoop(wu.proof_index, &results[j], accumulators);
    }
}

fn accumulate_scoop(
    proof_index: usize,
    scoop_data: &[u8; SCOOP_SIZE],
    accumulators: &[ProofAccumulator],
) {
    let acc = &accumulators[proof_index];
    let mut xor_guard = acc.xor_result.lock().unwrap();
    for (b1, b2) in xor_guard.iter_mut().zip(scoop_data.iter()) {
        *b1 ^= *b2;
    }
    drop(xor_guard);
    acc.units_completed.fetch_add(1, Ordering::Relaxed);
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn test_seed() -> [u8; 32] {
        hex::decode("AFFEAFFEAFFEAFFEAFFEAFFEAFFEAFFEAFFEAFFEAFFEAFFEAFFEAFFEAFFEAFFE")
            .unwrap()
            .try_into()
            .unwrap()
    }

    fn test_payload() -> [u8; 20] {
        hex::decode("99BC78BA577A95A11F1A344D4D2AE55F2F857B98")
            .unwrap()
            .try_into()
            .unwrap()
    }

    fn test_gensig() -> [u8; 32] {
        let mut gensig = [0u8; 32];
        hex::decode_to_slice(
            "9821beb3b34d9a3b30127c05f8d1e9006f8a02f565a3572145134bbe34d37a76",
            &mut gensig,
        )
        .unwrap();
        gensig
    }

    #[test]
    fn test_validate_proof_matches_calculate_quality_raw() {
        let seed = test_seed();
        let payload = test_payload();
        let gensig = test_gensig();
        let block_height = 0u64;
        let base_target = 1u64;

        for compression in 0..=3u8 {
            for nonce in [0, 1, 42, 1337] {
                let expected_quality = crate::calculate_quality_from_height(
                    &payload,
                    &seed,
                    nonce,
                    compression,
                    block_height,
                    &gensig,
                )
                .unwrap();

                let input = ProofInput {
                    address_payload: payload,
                    seed,
                    nonce,
                    compression,
                    block_height,
                    generation_signature: gensig,
                    base_target,
                    claimed_quality: None,
                };

                let result = validate_proof(&input).unwrap();
                assert_eq!(
                    result.quality, expected_quality,
                    "Quality mismatch: compression={}, nonce={}",
                    compression, nonce
                );
                assert!(result.is_valid);
                assert!(result.error.is_none());
            }
        }
    }

    #[test]
    fn test_validate_proofs_batch() {
        let gensig = test_gensig();
        let base_target = 1000u64;

        let inputs: Vec<ProofInput> = (0..8)
            .map(|i| ProofInput {
                address_payload: [i as u8 + 1; 20],
                seed: [(i as u8 + 1).wrapping_mul(0xAB); 32],
                nonce: 100 + i,
                compression: 0,
                block_height: 0,
                generation_signature: gensig,
                base_target,
                claimed_quality: None,
            })
            .collect();

        let batch_result = validate_proofs(&inputs, SurrenderPolicy::ContinueAll).unwrap();
        assert_eq!(batch_result.results.len(), 8);
        assert!(!batch_result.early_surrender);

        // Cross-validate each result
        for (idx, input) in inputs.iter().enumerate() {
            let expected = crate::calculate_quality_from_height(
                &input.address_payload,
                &input.seed,
                input.nonce,
                input.compression,
                input.block_height,
                &input.generation_signature,
            )
            .unwrap();
            assert_eq!(
                batch_result.results[idx].quality, expected,
                "Batch result {} mismatch",
                idx
            );
            assert!(batch_result.results[idx].is_valid);
        }
    }

    #[test]
    fn test_validate_proof_claimed_quality_match() {
        let seed = test_seed();
        let payload = test_payload();
        let gensig = test_gensig();

        // First compute the real quality
        let real_quality =
            crate::calculate_quality_from_height(&payload, &seed, 1337, 0, 0, &gensig).unwrap();

        let input = ProofInput {
            address_payload: payload,
            seed,
            nonce: 1337,
            compression: 0,
            block_height: 0,
            generation_signature: gensig,
            base_target: 1,
            claimed_quality: Some(real_quality),
        };

        let result = validate_proof(&input).unwrap();
        assert!(result.is_valid);
        assert_eq!(result.quality, real_quality);
        assert!(result.error.is_none());
    }

    #[test]
    fn test_validate_proof_claimed_quality_mismatch() {
        let seed = test_seed();
        let payload = test_payload();
        let gensig = test_gensig();

        let input = ProofInput {
            address_payload: payload,
            seed,
            nonce: 1337,
            compression: 0,
            block_height: 0,
            generation_signature: gensig,
            base_target: 1,
            claimed_quality: Some(12345), // wrong
        };

        let result = validate_proof(&input).unwrap();
        assert!(!result.is_valid);
        assert!(result.error.is_some());
        assert!(matches!(
            result.error,
            Some(PoCXHashError::QualityMismatch { .. })
        ));
    }

    #[test]
    fn test_validate_proofs_early_surrender_stop_on_first() {
        let gensig = test_gensig();

        let inputs = vec![
            ProofInput {
                address_payload: [0x01; 20],
                seed: [0xAA; 32],
                nonce: 100,
                compression: 0,
                block_height: 0,
                generation_signature: gensig,
                base_target: 1,
                claimed_quality: Some(999), // likely wrong
            },
            ProofInput {
                address_payload: [0x02; 20],
                seed: [0xBB; 32],
                nonce: 200,
                compression: 0,
                block_height: 0,
                generation_signature: gensig,
                base_target: 1,
                claimed_quality: None, // no claim, always valid
            },
        ];

        let result = validate_proofs(&inputs, SurrenderPolicy::StopOnFirst).unwrap();
        assert_eq!(result.results.len(), 2);
        assert!(!result.results[0].is_valid);
        assert!(result.results[1].is_valid);
        assert!(result.early_surrender);
        assert_eq!(result.surrender_index, Some(0));
    }

    #[test]
    fn test_validate_proofs_continue_all_on_mismatch() {
        let gensig = test_gensig();

        let inputs = vec![
            ProofInput {
                address_payload: [0x01; 20],
                seed: [0xAA; 32],
                nonce: 100,
                compression: 0,
                block_height: 0,
                generation_signature: gensig,
                base_target: 1,
                claimed_quality: Some(999), // wrong
            },
            ProofInput {
                address_payload: [0x02; 20],
                seed: [0xBB; 32],
                nonce: 200,
                compression: 0,
                block_height: 0,
                generation_signature: gensig,
                base_target: 1,
                claimed_quality: Some(888), // also wrong
            },
        ];

        let result = validate_proofs(&inputs, SurrenderPolicy::ContinueAll).unwrap();
        assert_eq!(result.results.len(), 2);
        // Both proofs marked invalid individually
        assert!(!result.results[0].is_valid);
        assert!(!result.results[1].is_valid);
        assert!(result.results[0].error.is_some());
        assert!(result.results[1].error.is_some());
        // But no batch-level surrender
        assert!(!result.early_surrender);
        assert!(result.surrender_index.is_none());
    }

    #[test]
    fn test_validate_proofs_empty() {
        let result = validate_proofs(&[], SurrenderPolicy::ContinueAll).unwrap();
        assert!(result.results.is_empty());
        assert!(!result.early_surrender);
    }

    #[test]
    fn test_validate_proof_deadline_calculation() {
        let seed = test_seed();
        let payload = test_payload();
        let gensig = test_gensig();
        let base_target = 1000u64;

        let input = ProofInput {
            address_payload: payload,
            seed,
            nonce: 1337,
            compression: 0,
            block_height: 0,
            generation_signature: gensig,
            base_target,
            claimed_quality: None,
        };

        let result = validate_proof(&input).unwrap();
        assert_eq!(result.deadline, result.quality / base_target);
    }

    #[test]
    fn test_validate_proof_zero_base_target() {
        let input = ProofInput {
            address_payload: [0x01; 20],
            seed: [0xAA; 32],
            nonce: 100,
            compression: 0,
            block_height: 0,
            generation_signature: [0u8; 32],
            base_target: 0,
            claimed_quality: None,
        };

        let result = validate_proof(&input).unwrap();
        assert_eq!(result.deadline, u64::MAX);
    }

    #[test]
    fn test_validate_proof_compression_levels() {
        let seed = test_seed();
        let payload = test_payload();
        let gensig = test_gensig();

        for compression in 0..=4u8 {
            let expected = crate::calculate_quality_from_height(
                &payload,
                &seed,
                1337,
                compression,
                0,
                &gensig,
            )
            .unwrap();

            let input = ProofInput {
                address_payload: payload,
                seed,
                nonce: 1337,
                compression,
                block_height: 0,
                generation_signature: gensig,
                base_target: 1,
                claimed_quality: None,
            };

            let result = validate_proof(&input).unwrap();
            assert_eq!(
                result.quality, expected,
                "Compression {} quality mismatch",
                compression
            );
        }
    }

    #[test]
    fn test_work_expansion_compression_0() {
        let input = ProofInput {
            address_payload: [0; 20],
            seed: [0; 32],
            nonce: 1337,
            compression: 0,
            block_height: 0,
            generation_signature: [0; 32],
            base_target: 1,
            claimed_quality: None,
        };
        let units = expand_work_units(0, &input, 667);
        assert_eq!(units.len(), 1); // 2^0 = 1
    }

    #[test]
    fn test_work_expansion_compression_3() {
        let input = ProofInput {
            address_payload: [0; 20],
            seed: [0; 32],
            nonce: 1337,
            compression: 3,
            block_height: 0,
            generation_signature: [0; 32],
            base_target: 1,
            claimed_quality: None,
        };
        let units = expand_work_units(0, &input, 667);
        assert_eq!(units.len(), 8); // 2^3 = 8
    }

    #[test]
    fn test_validate_proofs_large_batch() {
        let gensig = test_gensig();

        // 32 proofs with compression 0 — enough to exercise SIMD batching
        let inputs: Vec<ProofInput> = (0..32)
            .map(|i| ProofInput {
                address_payload: {
                    let mut p = [0u8; 20];
                    p[0] = (i & 0xFF) as u8;
                    p[1] = ((i >> 8) & 0xFF) as u8;
                    p
                },
                seed: {
                    let mut s = [0u8; 32];
                    s[0] = (i & 0xFF) as u8;
                    s[1] = ((i >> 8) & 0xFF) as u8;
                    s
                },
                nonce: 1000 + i,
                compression: 0,
                block_height: 0,
                generation_signature: gensig,
                base_target: 1,
                claimed_quality: None,
            })
            .collect();

        let batch = validate_proofs(&inputs, SurrenderPolicy::ContinueAll).unwrap();
        assert_eq!(batch.results.len(), 32);

        for (idx, input) in inputs.iter().enumerate() {
            let expected = crate::calculate_quality_from_height(
                &input.address_payload,
                &input.seed,
                input.nonce,
                input.compression,
                input.block_height,
                &input.generation_signature,
            )
            .unwrap();
            assert_eq!(
                batch.results[idx].quality, expected,
                "Large batch result {} mismatch",
                idx
            );
        }
    }
}