jbig2-decoder 0.3.2

Memory-safe JBIG2 decoder under development
Documentation
//! Isolated packed LPS/QE renormalization lookup tables with verified inline accessors.
//!
//! Keeping these static arrays in a dedicated module avoids loading their 384 array-element
//! axioms into Z3 contexts for `arithmetic::decoding`.

#[cfg(verus_keep_ghost)]
use vstd::prelude::*;

/// 128-entry packed next-LPS transition indexed by `usize::from(context.packed()) & 0x7f`.
#[cfg_attr(verus_keep_ghost, verus_verify)]
const NEXT_LPS_PACKED: [u8; 128] = [
    3, 2, 12, 13, 18, 19, 24, 25, 58, 59, 66, 67, 13, 12, 28, 29, 28, 29, 28, 29, 34, 35, 36, 37,
    40, 41, 42, 43, 29, 28, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 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, 92, 93, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];

/// 128-entry LPS renormalization shift count (`1..=15`) indexed by `usize::from(context.packed()) & 0x7f`.
#[cfg_attr(verus_keep_ghost, verus_verify)]
const QE_SHIFT_PACKED: [u8; 128] = [
    1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 1, 1, 1, 1,
    1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
    4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 15, 15,
    1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0,
];

/// 128-entry renormalized LPS interval (`qe << QE_SHIFT_PACKED[p]`, in `0x8000..=0xe008`).
#[cfg_attr(verus_keep_ghost, verus_verify)]
const QE_RENORM_PACKED: [u16; 128] = [
    0xac02, 0xac02, 0xd004, 0xd004, 0xc008, 0xc008, 0xac10, 0xac10, 0xa420, 0xa420, 0x8840, 0x8840,
    0xac02, 0xac02, 0xa802, 0xa802, 0x9002, 0x9002, 0xe004, 0xe004, 0xc004, 0xc004, 0x9004, 0x9004,
    0xe008, 0xe008, 0xb008, 0xb008, 0xac02, 0xac02, 0xa802, 0xa802, 0xa202, 0xa202, 0x9002, 0x9002,
    0xe004, 0xe004, 0xd004, 0xd004, 0xc004, 0xc004, 0xa004, 0xa004, 0x9004, 0x9004, 0x8804, 0x8804,
    0xe008, 0xe008, 0xc008, 0xc008, 0xb008, 0xb008, 0xa008, 0xa008, 0x9008, 0x9008, 0x8808, 0x8808,
    0xac10, 0xac10, 0x9c10, 0x9c10, 0x8a10, 0x8a10, 0xa420, 0xa420, 0x8820, 0x8820, 0xa840, 0xa840,
    0x8840, 0x8840, 0xa080, 0xa080, 0x8880, 0x8880, 0x8500, 0x8500, 0x9200, 0x9200, 0x9400, 0x9400,
    0xa800, 0xa800, 0x9000, 0x9000, 0xa000, 0xa000, 0x8000, 0x8000, 0xac02, 0xac02, 0x0000, 0x0000,
    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
];

#[cfg(verus_keep_ghost)]
verus! {

spec fn packed_lps_entry_ok(packed: u8) -> bool {
    let p = packed as int;
    let s = super::QE_STATES@[p / 2];
    let next_lps = NEXT_LPS_PACKED@[p];
    let shift = QE_SHIFT_PACKED@[p];
    let renorm = QE_RENORM_PACKED@[p];
    let expected_mps: u8 = if s.switch_mps { (packed & 1u8) ^ 1u8 } else { packed & 1u8 };
    next_lps < 94u8 && (next_lps / 2u8) == s.next_lps && (next_lps % 2u8) == expected_mps
        && 1u8 <= shift && shift <= 15u8
        && (((s.qe as u32) << ((shift - 1u8) as u32)) < super::MIN_INTERVAL)
        && (((s.qe as u32) << (shift as u32)) == (renorm as u32))
        && super::MIN_INTERVAL <= (renorm as u32) && (renorm as u32) <= 0xe008u32
}

spec fn all_packed_lps_ok(i: int) -> bool
    decreases (if i < 94 { 94 - i } else { 0 }),
{ if i >= 94 { true } else { packed_lps_entry_ok(i as u8) && all_packed_lps_ok(i + 1) } }

proof fn extract_packed_lps_ok(start: int, target: int)
    requires 0 <= start <= target < 94, all_packed_lps_ok(start),
    ensures packed_lps_entry_ok(target as u8),
    decreases target - start,
{ if start < target { extract_packed_lps_ok(start + 1, target); } }

proof fn packed_lps_entry_valid(packed: usize)
    requires packed < 94usize,
    ensures
        NEXT_LPS_PACKED@[packed as int] < 94,
        (NEXT_LPS_PACKED@[packed as int] / 2) == super::QE_STATES@[(packed / 2) as int].next_lps,
        (NEXT_LPS_PACKED@[packed as int] % 2) == if super::QE_STATES@[(packed / 2) as int].switch_mps {
            ((packed as u8) % 2) ^ 1u8
        } else {
            (packed as u8) % 2
        },
        1 <= QE_SHIFT_PACKED@[packed as int] <= 15,
        (((super::QE_STATES@[(packed / 2) as int].qe as u32)
            << ((QE_SHIFT_PACKED@[packed as int] - 1) as u32)) < super::MIN_INTERVAL),
        (((super::QE_STATES@[(packed / 2) as int].qe as u32)
            << (QE_SHIFT_PACKED@[packed as int] as u32))
            == QE_RENORM_PACKED@[packed as int] as u32),
        super::MIN_INTERVAL <= QE_RENORM_PACKED@[packed as int] as u32 <= 0xe008u32,
{
    assert(all_packed_lps_ok(0)) by (compute_only);
    extract_packed_lps_ok(0, packed as int);
    let p8 = packed as u8;
    assert((p8 & 1u8) == (p8 % 2u8)) by (bit_vector);
}

}

#[cfg_attr(verus_keep_ghost, verus_spec(out =>
    requires idx < 94usize,
    ensures
        out < 94,
        (out / 2) == super::QE_STATES@[(idx / 2) as int].next_lps,
        (out % 2) == if super::QE_STATES@[(idx / 2) as int].switch_mps {
            ((idx as u8) % 2) ^ 1u8
        } else {
            (idx as u8) % 2
        },
))]
#[allow(clippy::inline_always)]
#[inline(always)]
pub(super) fn next_lps_packed(idx: usize) -> u8 {
    #[cfg(verus_keep_ghost)]
    proof! {
        assert((idx & 0x7fusize) == idx) by (bit_vector)
            requires idx < 94usize;
        packed_lps_entry_valid(idx);
    }
    NEXT_LPS_PACKED[idx & 0x7f]
}

#[cfg_attr(verus_keep_ghost, verus_spec(out =>
    requires idx < 94usize,
    ensures
        1 <= out.0 <= 15,
        (((super::QE_STATES@[(idx / 2) as int].qe as u32) << ((out.0 - 1) as u32))
            < super::MIN_INTERVAL),
        (((super::QE_STATES@[(idx / 2) as int].qe as u32) << (out.0 as u32)) == out.1),
        super::MIN_INTERVAL <= out.1 <= 0xe008u32,
))]
#[allow(clippy::inline_always)]
#[inline(always)]
pub(super) fn qe_shift_and_renorm_packed(idx: usize) -> (u8, u32) {
    #[cfg(verus_keep_ghost)]
    proof! {
        assert((idx & 0x7fusize) == idx) by (bit_vector)
            requires idx < 94usize;
        packed_lps_entry_valid(idx);
    }
    let shift = QE_SHIFT_PACKED[idx & 0x7f];
    let renorm = u32::from(QE_RENORM_PACKED[idx & 0x7f]);
    (shift, renorm)
}