Optimisation
Faster or smaller, proved correct.
Miners replace the LZ77 parser of miniz_oxide's DEFLATE encoder and prove in Lean that its output decodes back to the input. Every accepted parser is timed and measured on a fixed corpus.
Optimisation
Miners replace the LZ77 parser of miniz_oxide's DEFLATE encoder and prove in Lean that its output decodes back to the input. Every accepted parser is timed and measured on a fixed corpus.
Not on the frontier · Deregistered · 2.486 α of 3,600 α earned
Gate
Where it sits
Standing
Scoring limits
Admission
The lower confidence bound is above zero: the speed improvement passed.
Speed test
Estimate 0.65% · Lower bound 0.54% · needs to stay above 0.00% · 90% interval · 56 files across corpus-stage1, corpus-stage2
This submission
Reference: 5CURBM…NkCw17
The frontier it was judged against
Source
//! r9 N2 (honest) = m10 + lane R1 text loop gr_text (R1_TEXT) + FL_MC class routed to it (R1_MC); tokens = m10 except machine code
//! patch8: join=None tab=None inline=always noinline=['--noinline=g_scan,gh_scan']
//! r8 merge m9 (= m3 + G_ACC 4) = E2 parse-h (ac008b636ba09666) + E1 genome lane (lg_parse_d) + E1 high-entropy lane (lg_parse_h)
//! for sampled entropy >= FL_HIGH below the 1800 (1/256 bit) threshold of G's own high lane (f32 stays with G).
//! cx3: f82 batched parser with compact small-input tables and entropy-aware preparation.
//! The original 18 trusted helper bodies are unchanged. See README.md for the measured curve.
//! Dense text preparation, sparse high-entropy preparation, and a near-uniform literal lane.
pub const G_ACC: u64 = 4;
pub const G_AMAX: usize = 12;
pub const G_IH: usize = 2;
pub const G_IT: usize = 1;
pub const G_LZT: usize = 0;
pub const G_M3: usize = 1;
pub const G_MAX3: usize = 32768;
pub const G_WIN: usize = 32768;
pub const G_TS: usize = 16;
pub const G_MAX4: usize = 32768;
pub const G_M3Z: usize = 100;
pub const G_M3E: usize = 1800;
pub const G_HLMASK: u64 = 18446744073709551615;
pub const DEPTH: usize = 16;
pub const DEPTH2: usize = 8;
pub const GOOD: usize = 3;
pub const LAZY: usize = 258;
pub const NICE: usize = 258;
pub const IH: usize = 258;
pub const IT: usize = 64;
pub const ACC: u64 = 5;
pub const STEPMAX: usize = 32;
pub const LITQ: usize = 28;
pub const LADJ: usize = 2;
pub const HLOW: usize = 900;
pub const HSL: u32 = 0;
pub const ELO: usize = 400;
pub const H3HI: usize = 100;
pub const H3CTL: usize = 100;
pub const H3E: usize = 1700;
pub const ACCL: u64 = 63;
pub const H3: usize = 1;
pub const MAX3: usize = 4096;
pub const H3INS: usize = 1;
pub const C0Q: usize = 44;
pub const C0L: usize = 44;
pub const DEPTHL: usize = 12;
pub const M3: usize = 0;
pub const DEPTH3M: usize = 12;
pub const M3E: usize = 1900;
pub const M3Z: usize = 15;
pub const LZB: usize = 0;
pub const BONUS: usize = 0;
pub const EXT: usize = 1;
pub const DIGN: usize = 200;
pub const DN: usize = 4;
pub const P2N: usize = 8;
pub const LQN: usize = 20;
pub const NBIG: usize = 65536;
pub const GOODL: usize = 258;
pub const P2G: usize = 2;
pub const NSMALL: usize = 65536;
pub const BIAS: usize = 4096;
// ------------------------------------------------------------------ loads and compares (search only)
/// Little-endian 4-byte load at `p`; 0 when out of range.
pub fn ld4(input: &[u8], p: usize) -> u32 {
let n = input.len();
if n < 4 || p > n - 4 {
return 0;
}
((input[p + 3] as u32) << 24) | ((input[p + 2] as u32) << 16) | ((input[p + 1] as u32) << 8) | (input[p] as u32)
}
/// Little-endian 8-byte load at `p` (two u32 halves, highest byte read first); 0 when out of range.
pub fn ld8(input: &[u8], p: usize) -> u64 {
let n = input.len();
if n < 8 || p > n - 8 {
return 0;
}
let hi = ((input[p + 7] as u32) << 24) | ((input[p + 6] as u32) << 16) | ((input[p + 5] as u32) << 8) | (input[p + 4] as u32);
let lo = ((input[p + 3] as u32) << 24) | ((input[p + 2] as u32) << 16) | ((input[p + 1] as u32) << 8) | (input[p] as u32);
((hi as u64) << 32) | (lo as u64)
}
/// Index (0..7) of the first differing byte of two little-endian words, given their xor (!= 0).
pub fn first_diff(x: u64) -> usize {
let low = x & 0u64.wrapping_sub(x);
(63u32.wrapping_sub(low.leading_zeros()) / 8) as usize
}
/// Common prefix of positions `a < b`, up to `cap` (search only; never trusted).
#[inline(always)]
pub fn fast_len(input: &[u8], a: usize, b: usize, cap: usize) -> usize {
let mut l = 0usize;
let mut go = 1usize;
let mut it = 0usize;
while go == 1 && it < 40 {
if l < cap {
let x = ld8(input, a.wrapping_add(l)) ^ ld8(input, b.wrapping_add(l));
let k = if x == 0 { 8 } else { first_diff(x) };
l = l.wrapping_add(k);
go = if x == 0 { 1 } else { 0 };
} else {
go = 0;
}
it += 1;
}
if l > cap { cap } else { l }
}
/// Hash of the first (64 - hs) / 8 bytes at `p` into 16 bits.
#[inline(always)]
pub fn hashp(input: &[u8], p: usize, hs: u32) -> usize {
if hs == 32 {
return (ld4(input, p).wrapping_mul(2654435761) >> 16) as usize;
}
let v = ld8(input, p) << (hs % 64);
(v.wrapping_mul(0x9E3779B97F4A7C15) >> 48) as usize
}
/// Hash of the low 3 bytes of a word into 14 bits.
pub fn hash3(w: u32) -> usize {
((w << 8).wrapping_mul(2654435761) >> 18) as usize
}
// ------------------------------------------------------------------ cost model (search only)
/// DEFLATE distance extra bits of `d`.
pub fn dextra(d: usize) -> usize {
if d <= 4 {
0
} else {
let x = (d.wrapping_sub(1) as u32) | 1;
(31u32.wrapping_sub(x.leading_zeros()) as usize).wrapping_sub(1)
}
}
/// DEFLATE length extra bits of `l`.
pub fn lextra(l: usize) -> usize {
if l < 11 || l >= 258 {
0
} else {
let x = (l.wrapping_sub(3) as u32) | 1;
(31u32.wrapping_sub(x.leading_zeros()) as usize).wrapping_sub(2)
}
}
/// Estimated saving (quarter bits, offset by BIAS) of coding `l` bytes as a match at `d`.
/// `cq` packs the literal cost (low 8 bits) and the match base cost (bits 8..).
pub fn score(l: usize, d: usize, cq: usize) -> usize {
let cost = (cq / 256).wrapping_add(dextra(d).wrapping_add(lextra(l)).wrapping_mul(4));
l.wrapping_mul(cq % 256).wrapping_add(BIAS).wrapping_sub(cost)
}
/// Fixed-point log2 with 8 fractional bits (linear between powers of two).
pub fn lg8(x: u32) -> u64 {
let y = x | 1;
let e = 31u32.wrapping_sub(y.leading_zeros()) % 32;
let m = if e >= 8 { (y >> ((e.wrapping_sub(8)) % 32)) & 255 } else { (y << ((8u32.wrapping_sub(e)) % 32)) & 255 };
(e as u64).wrapping_mul(256).wrapping_add(m as u64)
}
/// Histogram of a strided sample of the input (about 4096 bytes).
pub fn sample(input: &[u8], h: &mut [u32; 256]) {
let n = input.len();
let st = n / 4096 + 1;
let mut k = 0usize;
let mut c = 0usize;
while k < n && c < 8192 {
let b = (input[k] as usize) % 256;
h[b] = h[b].wrapping_add(1);
k = k.wrapping_add(st);
c += 1;
}
}
/// Order-0 entropy of the histogram in 1/256 bits per symbol.
pub fn entropy256(h: &[u32; 256]) -> usize {
let mut tot = 0u64;
let mut acc = 0u64;
let mut i = 0usize;
while i < 256 {
let c = h[i];
tot = tot.wrapping_add(c as u64);
acc = if c > 0 { acc.wrapping_add((c as u64).wrapping_mul(lg8(c))) } else { acc };
i += 1;
}
let t32 = if tot > 4000000000 { 4000000000u32 } else { tot as u32 };
let a = lg8(t32);
let b = if tot == 0 { a } else { acc / tot };
if a > b { (a - b) as usize } else { 0 }
}
/// Per mille of the sampled bytes in `lo .. hi`.
pub fn share(h: &[u32; 256], lo: usize, hi: usize) -> usize {
let mut t = 0usize;
let mut s = 0usize;
let mut i = 0usize;
while i < 256 {
let c = h[i] as usize;
t = t.wrapping_add(c);
s = if i >= lo && i < hi { s.wrapping_add(c) } else { s };
i += 1;
}
if t == 0 { 0 } else { s.wrapping_mul(1000) / t }
}
/// 1 when the sampled entropy `e` marks a genome-like input (few cheap literals).
pub fn low_mode(e: usize) -> usize {
if e >= ELO && e < HLOW { 1 } else { 0 }
}
/// Literal cost (quarter bits): from the sampled entropy in low mode, else LITQ.
pub fn lit_cost(e: usize, low: usize) -> usize {
let q = (e / 64).wrapping_add(LADJ);
let q2 = if q < 6 { 6 } else if q > 36 { 36 } else { q };
if low == 1 { q2 } else { LITQ }
}
/// 1 when the input should use 3-byte hash chains (M3 = 2: always; 1: by rule).
pub fn m3_mode(h: &[u32; 256], e: usize) -> usize {
let z = share(h, 0, 1);
let a = if e >= M3E { 1usize } else { 0usize };
let b = if z >= M3Z { 1usize } else { 0usize };
if M3 == 2 { 1 } else if M3 == 1 { a & b } else { 0 }
}
/// 1 when the 3-byte table should be used: binary-ish or non-ASCII input of moderate entropy.
pub fn h3_mode(h: &[u32; 256], e: usize) -> usize {
let hi = share(h, 128, 256);
let ctl = share(h, 0, 9);
let a = if hi >= H3HI { 1usize } else { 0usize };
let b = if ctl >= H3CTL { 1usize } else { 0usize };
let c = if e < H3E { 1usize } else { 0usize };
if H3 == 2 { 1 } else if H3 == 1 { (a | b) & c } else { 0 }
}
/// 1 for large numeric text: at least DIGN per mille digits, few bytes >= 128 or < 9, not genome-like.
pub fn numeric(h: &[u32; 256], n: usize, low: usize) -> usize {
let dig = share(h, 48, 58);
let hi = share(h, 128, 256);
let ctl = share(h, 0, 9);
let txt = if hi < 50 && ctl < 50 { 1usize } else { 0usize };
let big = if n >= NBIG { 1usize } else { 0usize };
let d = if dig >= DIGN { 1usize } else { 0usize };
if low == 1 { 0 } else { txt & big & d }
}
// ------------------------------------------------------------------ match finding (search only)
/// Walk the chain from `start` (pos+1 encoded, 0 = none) for the candidate with the best score
/// among those longer than `bl0` and scoring more than `bs0`. Returns `len | dist << 9 | score << 25`
/// (0 if none). Candidates stay inside the 32K window; the walk stops at `NICE` or `cap`.
pub fn walk(input: &[u8], prev: &[u32; 65536], pos: usize, start: usize, cap: usize, probes: usize, bl0: usize, bs0: usize, litq: usize) -> usize {
let lim = pos.saturating_sub(32768);
let stop = if cap < NICE { cap } else { NICE };
let mut cur = start;
let mut k = 0usize;
let mut bl = bl0;
let mut bs = bs0;
let mut bd = 0usize;
let mut off = if bl >= 3 { bl - 3 } else { 0 };
let mut want = ld4(input, pos.wrapping_add(off));
while k < probes && cur > lim && bl < stop {
let c = cur - 1;
if ld4(input, c.wrapping_add(off)) == want {
let l = fast_len(input, c, pos, cap);
let d = pos.wrapping_sub(c);
let s = if l > bl { score(l, d, litq) } else { 0 };
let better = if l > bl { if s > bs { 1usize } else { 0usize } } else { 0usize };
bd = if better == 1 { d } else { bd };
bs = if better == 1 { s } else { bs };
bl = if better == 1 { l } else { bl };
if better == 1 {
off = if bl >= 3 { bl - 3 } else { 0 };
want = ld4(input, pos.wrapping_add(off));
}
}
cur = prev[c % 65536] as usize;
k += 1;
}
if bd > 0 {
bl | bd.wrapping_mul(512) | bs.wrapping_mul(33554432)
} else {
0
}
}
/// Lazy-path copy of `walk` (a separate function keeps both inlined at their single call sites).
/// Walk the chain from `start` (pos+1 encoded, 0 = none) for the candidate with the best score
/// among those longer than `bl0` and scoring more than `bs0`. Returns `len | dist << 9 | score << 25`
/// (0 if none). Candidates stay inside the 32K window; the walk stops at `NICE` or `cap`.
pub fn lwalk(input: &[u8], prev: &[u32; 65536], pos: usize, start: usize, cap: usize, probes: usize, bl0: usize, bs0: usize, litq: usize) -> usize {
let lim = pos.saturating_sub(32768);
let stop = if cap < NICE { cap } else { NICE };
let mut cur = start;
let mut k = 0usize;
let mut bl = bl0;
let mut bs = bs0;
let mut bd = 0usize;
let mut off = if bl >= 3 { bl - 3 } else { 0 };
let mut want = ld4(input, pos.wrapping_add(off));
while k < probes && cur > lim && bl < stop {
let c = cur - 1;
if ld4(input, c.wrapping_add(off)) == want {
let l = fast_len(input, c, pos, cap);
let d = pos.wrapping_sub(c);
let s = if l > bl { score(l, d, litq) } else { 0 };
let better = if l > bl { if s > bs { 1usize } else { 0usize } } else { 0usize };
bd = if better == 1 { d } else { bd };
bs = if better == 1 { s } else { bs };
bl = if better == 1 { l } else { bl };
if better == 1 {
off = if bl >= 3 { bl - 3 } else { 0 };
want = ld4(input, pos.wrapping_add(off));
}
}
cur = prev[c % 65536] as usize;
k += 1;
}
if bd > 0 {
bl | bd.wrapping_mul(512) | bs.wrapping_mul(33554432)
} else {
0
}
}
/// The 1-deep 3-byte candidate at `pos` from entry `s3` (pos+1 encoded): `3 | dist << 9 | score << 25`
/// or 0.
pub fn cand3(input: &[u8], pos: usize, s3: usize, cap: usize, bs0: usize, litq: usize) -> usize {
if s3 == 0 || s3 > pos || cap < 3 {
return 0;
}
let c = s3 - 1;
let d = pos - c;
if d > MAX3 || (ld4(input, c) ^ ld4(input, pos)) & 16777215 != 0 {
return 0;
}
let s = score(3, d, litq);
if s > bs0 {
3 | d.wrapping_mul(512) | s.wrapping_mul(33554432)
} else {
0
}
}
/// With `probes > 0`: insert `pos` into the chains, then walk them (see `walk`); with nothing found
/// and `h3on == 1`, try the 3-byte table. 0 when `probes == 0`.
pub fn search(input: &[u8], head: &mut [u32; 65536], prev: &mut [u32; 65536], head3: &mut [u32; 16384], shorts: &[u32; 4096], pos: usize, probes: usize, bl0: usize, bs0: usize, litq: usize, hs: u32, h3on: usize) -> usize {
let n = input.len();
if probes == 0 || n < 16 || pos > n - 16 {
return 0;
}
let node = prev[pos % 65536];
let start = node as usize;
let s3 = if h3on == 1 { shorts[pos % 4096] as usize } else { 0 };
let rem = n - pos - 8;
let cap = if rem < 258 { rem } else { 258 };
let w2 = walk(input, prev, pos, start, cap, probes, bl0, bs0, litq);
if w2 != 0 || s3 == 0 || bl0 >= 3 {
w2
} else {
cand3(input, pos, s3, cap, bs0, litq)
}
}
/// With `on == 1`: enter `pos` into the 3-byte table and return the entry it replaced; else 0.
pub fn upd3(input: &[u8], head3: &mut [u32; 16384], pos: usize, on: usize) -> usize {
if on == 1 {
let g = hash3(ld4(input, pos)) % 16384;
let s3 = head3[g] as usize;
head3[g] = (pos as u32).wrapping_add(1);
s3
} else {
0
}
}
/// Insert positions `from .. to` into the chains.
pub fn insert_range(input: &[u8], head: &mut [u32; 65536], prev: &mut [u32; 32768], from: usize, to: usize, hs: u32) {
let lim = input.len().saturating_sub(16);
let mut p = from;
while p < to && p <= lim {
let h = hashp(input, p, hs) % 65536;
prev[p % 32768] = head[h];
head[h] = (p as u32).wrapping_add(1);
p += 1;
}
}
/// `insert_range` that also updates the 3-byte table.
pub fn insert_range3(input: &[u8], head: &mut [u32; 65536], prev: &mut [u32; 32768], head3: &mut [u32; 16384], from: usize, to: usize, hs: u32) {
let lim = input.len().saturating_sub(16);
let mut p = from;
while p < to && p <= lim {
let h = hashp(input, p, hs) % 65536;
prev[p % 32768] = head[h];
head[h] = (p as u32).wrapping_add(1);
head3[hash3(ld4(input, p)) % 16384] = (p as u32).wrapping_add(1);
p += 1;
}
}
/// Insert the inside `from .. to` of an emitted match: its first IH and last IT positions.
pub fn insert_match(input: &[u8], head: &mut [u32; 65536], prev: &mut [u32; 32768], head3: &mut [u32; 16384], from: usize, to: usize, hs: u32, h3on: usize) {
let a = from.wrapping_add(IH);
let a2 = if a < to { a } else { to };
let b = to.wrapping_sub(IT);
let b2 = if b > a2 { if b < to { b } else { a2 } } else { a2 };
if h3on == 1 {
insert_range3(input, head, prev, head3, from, a2, hs);
if b2 < to {
insert_range3(input, head, prev, head3, b2, to, hs);
}
} else {
insert_range(input, head, prev, from, a2, hs);
if b2 < to {
insert_range(input, head, prev, b2, to, hs);
}
}
}
// ------------------------------------------------------------------ trusted core
/// How many bytes agree at `a` and `b`, up to `cap`. The emission's byte-wise check.
/// Trusted: little-endian 8-byte word at `p` (0 when out of range).
pub fn tw8(input: &[u8], p: usize) -> u64 {
let n = input.len();
if n < 8 || p > n - 8 {
return 0;
}
(input[p] as u64)
+ (input[p + 1] as u64) * 256
+ (input[p + 2] as u64) * 65536
+ (input[p + 3] as u64) * 16777216
+ (input[p + 4] as u64) * 4294967296
+ (input[p + 5] as u64) * 1099511627776
+ (input[p + 6] as u64) * 281474976710656
+ (input[p + 7] as u64) * 72057594037927936
}
/// Trusted: little-endian 4-byte word at `p` (0 when out of range).
pub fn tw4(input: &[u8], p: usize) -> u32 {
let n = input.len();
if n < 4 || p > n - 4 {
return 0;
}
(input[p] as u32) + (input[p + 1] as u32) * 256 + (input[p + 2] as u32) * 65536 + (input[p + 3] as u32) * 16777216
}
/// Trusted: 1 iff `cap >= 8`, `cap - l < 8` and the 8-byte words at `a + cap - 8` and `b + cap - 8` are equal.
pub fn tail_eq(input: &[u8], a: usize, b: usize, cap: usize, l: usize) -> usize {
if cap < 8 || cap - l >= 8 {
return 0;
}
if tw8(input, a + (cap - 8)) == tw8(input, b + (cap - 8)) {
1
} else {
0
}
}
/// Trusted: 1 iff `4 <= cap < 8` and the 4-byte words at `a`, `b` and at `a + cap - 4`, `b + cap - 4` are equal.
pub fn short_eq(input: &[u8], a: usize, b: usize, cap: usize) -> usize {
if cap < 4 || cap >= 8 {
return 0;
}
if tw4(input, a) != tw4(input, b) {
return 0;
}
if tw4(input, a + (cap - 4)) == tw4(input, b + (cap - 4)) {
1
} else {
0
}
}
/// Trusted: a length `l <= cap` over which the bytes at `a` and `b` provably agree, found with word compares:
/// 8-byte steps, then one overlapping 8-byte compare at `cap - 8` (or two 4-byte ones when 4 <= cap < 8); `cap`
/// itself when everything agreed.
pub fn words_eq(input: &[u8], a: usize, b: usize, cap: usize) -> usize {
let mut l = 0usize;
while cap - l >= 8 && tw8(input, a + l) == tw8(input, b + l) {
l += 8;
}
let big = tail_eq(input, a, b, cap, l);
let small = short_eq(input, a, b, cap);
if big == 1 {
cap
} else if small == 1 {
cap
} else {
l
}
}
/// Trusted: how many bytes agree at `a` and `b`, up to `cap` (word compares, then bytes).
pub fn match_len(input: &[u8], a: usize, b: usize, cap: usize) -> usize {
let mut l = words_eq(input, a, b, cap);
while l < cap && input[b + l] == input[a + l] {import Lz77
import Slot
import Mathlib.Data.Nat.Log
import Mathlib.Tactic.IntervalCases
import Mathlib.Tactic.FailIfNoProgress
attribute [local irreducible] slot.g_match slot.g_eval slot.g3_eval slot.gh_eval slot.g_lazy_eval slot.g3_lazy_eval slot.g_choose slot.g_plain_choose slot.g_scan slot.g3_scan slot.gh_scan slot.g_lazy slot.g3_lazy slot.gh_lazy slot.g_scan_loop slot.g3_scan_loop slot.gh_scan_loop slot.g_scan_loop.body slot.g3_scan_loop.body slot.gh_scan_loop.body slot.public_emit_join slot.emit_lits slot.emit_lits_loop slot.emit_lits_loop.body slot.main_parse_a_loop.body slot.main_parse_b_loop.body slot.main_parse_high_loop.body
namespace A2FastPureChoice
open Aeneas Aeneas.Std Result
@[scoped step]
theorem choose_ok {α : Type} (c : Prop) [Decidable c] (a b : α) :
(if c then ok a else ok b) ⦃ fun _ => True ⦄ := by
split <;> simp only [Std.WP.spec_ok]
end A2FastPureChoice
set_option linter.unusedTactic false
set_option linter.unreachableTactic false
set_option linter.unusedVariables false
set_option linter.unusedSimpArgs false
set_option linter.unusedTactic false
set_option linter.unreachableTactic false
set_option linter.unusedVariables false
set_option linter.unusedSimpArgs false
/- Completed development module: BackLemmas -/
namespace Submission
open Aeneas Aeneas.Std Result ControlFlow
open LZ77 (toks bytes bytes_length bytes_getElem! toks_update bytes_congr Matches emit_lit emit_match ite_ok)
set_option maxRecDepth 8192
set_option maxHeartbeats 10000000
def AMatchAt (s : Slice Std.U8) (p l d : Nat) : Prop :=
3 ≤ l ∧ l ≤ 258 ∧ p + l ≤ s.length ∧ 1 ≤ d ∧ d ≤ 32768 ∧ d ≤ p ∧ Matches s (p - d) p l
theorem a_copyN_take_prefix (acc : List Nat) (d : Nat) :
∀ n, (LZ77.copyN acc d n).take acc.length = acc := by
intro n
induction n with
| zero => simp [LZ77.copyN]
| succ m ih =>
simp only [LZ77.copyN]
rw [List.take_append_of_le_length (by simp)]
exact ih
/-- Every token decodes to at least one byte, so a stream is never longer than its output. -/
theorem a_foldlM_emit_length : ∀ (ts : List Nat) (init acc : List Nat),
ts.foldlM LZ77.emit init = some acc → init.length + ts.length ≤ acc.length := by
intro ts
induction ts with
| nil => intro init acc h; simp at h; subst h; simp
| cons t ts ih =>
intro init acc h
simp only [List.foldlM_cons] at h
cases he : LZ77.emit init t with
| none => rw [he] at h; simp at h
| some a =>
rw [he] at h
simp only [Option.bind_eq_bind, Option.bind_some] at h
have h1 := ih a acc h
have h2 : init.length + 1 ≤ a.length := by
simp only [LZ77.emit] at he
split at he
· simp at he; subst he; simp
· split at he
· split at he
· simp at he; subst he; rw [LZ77.copyN_length]; simp only [LZ77.tokLen]; omega
· simp at he
· simp at he
simp only [List.length_cons]
omega
theorem a_decode_length_le {ts acc : List Nat} (h : LZ77.decode ts = some acc) :
ts.length ≤ acc.length := by
have := a_foldlM_emit_length ts [] acc h
simpa using this
/-- Dropping a literal from the end of a stream that decodes to a prefix. -/
theorem a_decode_pop_lit {ts : List Nat} {t : Nat} {inp : List Nat} {p : Nat}
(hp : p ≤ inp.length) (hd : LZ77.decode (ts ++ [t]) = some (inp.take p)) (ht : t < 256) :
1 ≤ p ∧ LZ77.decode ts = some (inp.take (p - 1)) := by
rw [LZ77.decode_snoc] at hd
cases hts : LZ77.decode ts with
| none => rw [hts] at hd; simp at hd
| some acc =>
rw [hts] at hd
simp only [Option.bind_some, LZ77.emit, if_pos ht] at hd
have hacc : acc ++ [t] = inp.take p := Option.some.inj hd
have hlen : acc.length + 1 = p := by
have := congrArg List.length hacc
simp [List.length_take] at this; omega
refine ⟨by omega, ?_⟩
apply congrArg some
have := congrArg (List.take acc.length) hacc
rw [List.take_append_of_le_length (le_refl _), List.take_length, List.take_take] at this
rw [this, show min acc.length p = p - 1 by omega]
/-- Dropping a match token from the end of a stream that decodes to a prefix. -/
theorem a_decode_pop_match {ts : List Nat} {t : Nat} {inp : List Nat} {p : Nat}
(hp : p ≤ inp.length) (hd : LZ77.decode (ts ++ [t]) = some (inp.take p)) (ht : 256 ≤ t) :
LZ77.tokLen t ≤ p ∧ LZ77.decode ts = some (inp.take (p - LZ77.tokLen t)) := by
rw [LZ77.decode_snoc] at hd
cases hts : LZ77.decode ts with
| none => rw [hts] at hd; simp at hd
| some acc =>
rw [hts] at hd
simp only [Option.bind_some, LZ77.emit, if_neg (show ¬ t < 256 by omega)] at hd
split at hd
· split at hd
· have hacc : LZ77.copyN acc (LZ77.tokDist t) (LZ77.tokLen t) = inp.take p := Option.some.inj hd
have hlen : acc.length + LZ77.tokLen t = p := by
have := congrArg List.length hacc
simp [List.length_take] at this; omega
refine ⟨by omega, ?_⟩
apply congrArg some
have := congrArg (List.take acc.length) hacc
rw [a_copyN_take_prefix, List.take_take] at this
rw [this, show min acc.length p = p - LZ77.tokLen t by omega]
· simp at hd
· simp at hd
/-- The first `nt` tokens are the first `nt - 1` and token `nt - 1`. -/
theorem a_toks_pop (out : Slice Std.U32) (nt : Nat) (h0 : 0 < nt) (hle : nt ≤ out.length) :
toks out nt = toks out (nt - 1) ++ [(out.val[nt - 1]!).val] := by
have hlt : nt - 1 < out.val.length := by
have : out.length = out.val.length := rfl
omega
simp only [toks]
conv_lhs => rw [show nt = (nt - 1) + 1 by omega]
rw [List.take_add_one, List.getElem?_eq_getElem hlt, Option.toList_some, List.map_append]
simp [getElem!_pos out.val (nt - 1) hlt]
theorem a_toks_length (out : Slice Std.U32) (nt : Nat) (h : nt ≤ out.length) :
(toks out nt).length = nt := by
simp only [toks, List.length_map, List.length_take]
have : out.length = out.val.length := rfl
omega
/-! ## 8. The decoding invariant `ADec` and the two token writers
`ADec s out nt p`: `p ≤ n`, `nt ≤ p`, `n ≤ out.length`, and the first `nt` tokens decode to the
first `p` bytes. Every loop carries it (with `out.length` fixed); each write steps it
(`ADec.lit`, `ADec.match`), each popped token retracts it (`ADec.pop_lit`, `ADec.pop_match`),
and `ADec.done` is the obligation once `p = n`. The `_step` forms take exactly the facts
`step*` leaves after `put_lit`/`put_match`. -/
/-- `nt` tokens written into `out` decode to the first `p` input bytes (`out` long enough). -/
def ADec (s : Slice Std.U8) (out : Slice Std.U32) (nt p : Nat) : Prop :=
p ≤ s.length ∧ nt ≤ p ∧ s.length ≤ out.length ∧
LZ77.decode (toks out nt) = some ((bytes s).take p)
theorem ADec.init (input : Slice Std.U8) (out : Slice Std.U32) (h : input.length ≤ out.length) :
ADec input out (0#usize).val (0#usize).val :=
⟨by simp, by simp, h, by simp [toks, LZ77.decode]⟩
/-- Writing the literal `s[p]` at `out[nt]`. -/
theorem ADec.lit {s : Slice Std.U8} {out : Slice Std.U32} {nt p : Nat} (h : ADec s out nt p)
(hp : p < s.length) (ntU : Std.Usize) (hnt : ntU.val = nt) (v : Std.U32)
(hv : v.val = (s.val[p]!).val) :
ADec s (out.set ntU v) (nt + 1) (p + 1) := by
obtain ⟨hps, hntp, hso, hde⟩ := h
have hntl : ntU.val < out.length := by omega
refine ⟨by omega, by omega, by rw [Std.Slice.set_length]; exact hso, ?_⟩
rw [← hnt]
refine emit_lit s out ntU p v (by rw [hnt]; exact hde) hp hntl ?_
have hpl : p < s.val.length := hp
rw [hv, bytes_getElem! s p hpl, getElem!_pos s.val p hpl]
/-- Writing the match token `(d, l)` at `out[nt]`. -/
theorem ADec.match {s : Slice Std.U8} {out : Slice Std.U32} {nt p l d : Nat} (h : ADec s out nt p)
(hm : AMatchAt s p l d) (ntU : Std.Usize) (hnt : ntU.val = nt) (v : Std.U32)
(hv : v.val = LZ77.mkMatch d l) :
ADec s (out.set ntU v) (nt + 1) (p + l) := by
obtain ⟨hps, hntp, hso, hde⟩ := h
obtain ⟨h3, h258, hpl, hd1, hd32, hdp, hmm⟩ := hm
have hntl : ntU.val < out.length := by omega
refine ⟨by omega, by omega, by rw [Std.Slice.set_length]; exact hso, ?_⟩
rw [← hnt]
exact emit_match s out ntU p d l v (by rw [hnt]; exact hde) hntl hd1 hdp hd32 h3 h258 hpl hmm hv
/-- Retracting a trailing literal. -/
theorem ADec.pop_lit {s : Slice Std.U8} {out : Slice Std.U32} {nt p : Nat} (h : ADec s out nt p)
(h0 : 0 < nt) (hle : nt ≤ out.length) (ht : (out.val[nt - 1]!).val < 256) :
1 ≤ p ∧ ADec s out (nt - 1) (p - 1) := by
obtain ⟨hps, hntp, hso, hde⟩ := h
rw [a_toks_pop out nt h0 hle] at hde
obtain ⟨hp1, hde'⟩ := a_decode_pop_lit (by simpa using hps) hde ht
exact ⟨hp1, by omega, by omega, hso, hde'⟩
/-- Retracting a trailing match token (of length `tokLen t`). -/
theorem ADec.pop_match {s : Slice Std.U8} {out : Slice Std.U32} {nt p : Nat} (h : ADec s out nt p)
(h0 : 0 < nt) (hle : nt ≤ out.length) (ht : 256 ≤ (out.val[nt - 1]!).val) :
LZ77.tokLen (out.val[nt - 1]!).val ≤ p ∧
ADec s out (nt - 1) (p - LZ77.tokLen (out.val[nt - 1]!).val) := by
obtain ⟨hps, hntp, hso, hde⟩ := h
rw [a_toks_pop out nt h0 hle] at hde
obtain ⟨hw, hde'⟩ := a_decode_pop_match (by simpa using hps) hde ht
have hlen := a_decode_length_le hde'
rw [a_toks_length out (nt - 1) (by omega), List.length_take, LZ77.bytes_length] at hlen
exact ⟨hw, by omega, by omega, hso, hde'⟩
/-- All `n` bytes consumed: the stream is valid. -/
theorem ADec.done {s : Slice Std.U8} {out : Slice Std.U32} {nt p : Nat} (h : ADec s out nt p)
(hp : s.length ≤ p) : nt ≤ s.length ∧ LZ77.Valid (bytes s) (toks out nt) := by
obtain ⟨hps, hntp, hso, hde⟩ := h
refine ⟨by omega, ?_⟩
unfold LZ77.Valid
rw [hde, show p = s.length by omega, ← LZ77.bytes_length, List.take_length]
theorem AMatchAt.back_lit {s : Slice Std.U8} {p l d : Nat} (h : AMatchAt s p l d) (hl : l < 258)
(hdp : d < p) (heq : s.val[p - 1]! = s.val[p - 1 - d]!) : AMatchAt s (p - 1) (l + 1) d := by
obtain ⟨h3, h258, hpl, hd1, hd32, _, hm⟩ := h
refine ⟨by omega, by omega, by omega, hd1, hd32, by omega, ?_⟩
intro k hk
rcases Nat.eq_zero_or_pos k with hk0 | hk0
· subst hk0
rw [Nat.add_zero, Nat.add_zero, show p - 1 - d = p - 1 - d by rfl]
exact heq
· have := hm (k - 1) (by omega)
rw [show p - 1 + k = p + (k - 1) by omega, show p - 1 - d + k = p - d + (k - 1) by omega]
exact this
/-- A whole earlier match of `w` bytes whose bytes also match at distance `d`. -/
theorem AMatchAt.back_match {s : Slice Std.U8} {p l d w : Nat} (h : AMatchAt s p l d)
(hlw : l + w ≤ 258) (hw : w ≤ p) (hdw : d ≤ p - w) (hmw : Matches s (p - w - d) (p - w) w) :
AMatchAt s (p - w) (l + w) d := by
obtain ⟨h3, h258, hpl, hd1, hd32, _, hm⟩ := h
refine ⟨by omega, by omega, by omega, hd1, hd32, hdw, ?_⟩
intro k hk
rcases Nat.lt_or_ge k w with hkw | hkw
· have := hmw k hkw
rw [show p - w - d = p - w - d by rfl] at this
exact this
· have := hm (k - w) (by omega)
rw [show p - w + k = p + (k - w) by omega, show p - w - d + k = p - d + (k - w) by omega]
exact this
/-- The state of the backward merge: `nt` tokens decode to `p` bytes, `out` keeps its length
`L`, the pending match `(l, d)` at `p` is real and ends at `E`, and `p` never grew past `P0`. -/
def ABackInv (input : Slice Std.U8) (L d E P0 : Nat) (out : Slice Std.U32) (nt p l : Nat) : Prop :=
ADec input out nt p ∧ out.length = L ∧ AMatchAt input p l d ∧ p + l = E ∧ p ≤ P0
/-- Absorbing the literal `out[nt-1]` whose byte also matches at distance `d`. The hypotheses
are ordered for `step*`'s facts: the new state `(i1, i2, l1)` is fixed by the goal, `t`, `x`,
`y`, `i4` by `assumption`, everything else by `(first | omega | scalar_tac)`. -/
theorem ABackInv.lit {input : Slice Std.U8} {out : Slice Std.U32} {L d E P0 : Nat}
{nt p l i1 i2 i4 l1 : Std.Usize} {t : Std.U32} {x y : Std.U8}
(h : ABackInv input L d E P0 out nt.val p.val l.val)
(hi1 : i1.val = nt.val - 1) (hntl : nt.val ≤ out.length) (hnt0 : 1 ≤ nt.val)
(hi1l : i1.val < out.val.length) (ht : t = out.val[i1.val]) (ht256 : t < 256#u32)
(hi2 : i2.val = p.val - 1) (hi2l : i2.val < input.val.length) (hx : x = input.val[i2.val])
(hxy : x = y) (hi4 : i4.val = i2.val - d) (hi4l : i4.val < input.val.length)
(hy : y = input.val[i4.val]) (hl : l.val < 258) (hdp : d < p.val)
(hl1 : l1.val = l.val + 1) :
ABackInv input L d E P0 out i1.val i2.val l1.val := by
obtain ⟨hdec, hL, hm, hE, hP⟩ := h
have ht' : (out.val[nt.val - 1]!).val < 256 := by
rw [← hi1, getElem!_pos out.val i1.val hi1l, ← ht]; (first | omega | scalar_tac)
obtain ⟨hp1, hdec'⟩ := ADec.pop_lit hdec (by omega) hntl ht'
have heq : input.val[p.val - 1]! = input.val[p.val - 1 - d]! := by
rw [← hi2, getElem!_pos input.val i2.val hi2l, show i2.val - d = i4.val by omega,
getElem!_pos input.val i4.val hi4l, ← hx, ← hy, hxy]
have hm' := AMatchAt.back_lit hm hl hdp heq
rw [hi1, hi2, hl1]
exact ⟨hdec', hL, hm', by omega, by omega⟩
/-- Absorbing the whole match token `t = out[nt-1]` (length `w`) whose bytes also match at `d`.
The new state `(i1, i10, i9)` is fixed by the goal; `t`, and `(i12, i11, w)` through
`same`'s postcondition, by `assumption`; the rest by `(first | omega | scalar_tac)`. -/
theorem ABackInv.match {input : Slice Std.U8} {out : Slice Std.U32} {L d E P0 : Nat}
{nt p l i1 w i9 i10 i11 i12 : Std.Usize} {t : Std.U32}
(h : ABackInv input L d E P0 out nt.val p.val l.val)
(hi1 : i1.val = nt.val - 1) (hntl : nt.val ≤ out.length) (hnt0 : 1 ≤ nt.val)
(hi1l : i1.val < out.val.length) (ht : t = out.val[i1.val])
(hsame : i12.val = 1 → Matches input i11.val i10.val w.val) (hi12 : i12 = 1#usize)
(ht24 : 16777216 ≤ t.val) (hwv : w.val = (t.val - 16777216) % 256 + 3)
(hi9 : i9.val = l.val + w.val) (hi9' : i9.val ≤ 258) (hi10 : i10.val = p.val - w.val)
(hwp : w.val ≤ p.val) (hdw : d ≤ i10.val) (hi11 : i11.val = i10.val - d) :
ABackInv input L d E P0 out i1.val i10.val i9.val := by
obtain ⟨hdec, hL, hm, hE, hP⟩ := h
have htv : (out.val[nt.val - 1]!).val = t.val := by
rw [← hi1, getElem!_pos out.val i1.val hi1l, ← ht]
have hwv' : w.val = LZ77.tokLen (out.val[nt.val - 1]!).val := by
rw [htv, hwv]
simp only [LZ77.tokLen, LZ77.MATCH_BASE]
obtain ⟨hwl, hdec'⟩ := ADec.pop_match hdec (by omega) hntl (by omega)
rw [← hwv'] at hdec'
have hmw : Matches input (p.val - w.val - d) (p.val - w.val) w.val := by
have := hsame (by rw [hi12]; rfl)
rw [hi11, hi10] at this
exact this
have hm' := AMatchAt.back_match hm (by omega) hwp (by omega) hmw
rw [hi1, hi10, hi9]
exact ⟨hdec', hL, hm', by omega, by omega⟩
/-- The state after `part_merge` (a shortened earlier token, a longer pending match). -/
theorem ABackInv.part {input : Slice Std.U8} {out out1 : Slice Std.U32} {L d E P0 : Nat}
{nt nt1 p l : Nat} {p1 l1 : Std.Usize} (h : ABackInv input L d E P0 out nt p l)
(hlen : out1.length = out.length) (hdec : ADec input out1 nt1 p1.val)
(hm : AMatchAt input p1.val l1.val d) (hE : p1.val + l1.val = p + l) (hp : p1.val ≤ p) :
ABackInv input L d E P0 out1 nt1 p1.val l1.val := by
obtain ⟨_, hL, _, hE0, hP⟩ := h
exact ⟨hdec, by rw [hlen, hL], hm, by omega, by omega⟩
theorem Matches.a_cons {input : Slice Std.U8} {a b n : Nat} (h : Matches input (a + 1) (b + 1) n)
(heq : input.val[b]! = input.val[a]!) : Matches input a b (n + 1) := by
intro k hk
rcases Nat.eq_zero_or_pos k with h0 | hk0
· subst h0; simpa using heq
· have := h (k - 1) (by omega)
rw [show b + 1 + (k - 1) = b + k by omega, show a + 1 + (k - 1) = a + k by omega] at this
exact this
/-- Two adjacent matches at the same distance are one match. -/
theorem Matches.a_append {input : Slice Std.U8} {a b k l : Nat} (h1 : Matches input a b k)
(h2 : Matches input (a + k) (b + k) l) : Matches input a b (k + l) := by
intro j hj
rcases Nat.lt_or_ge j k with hjk | hjk
· exact h1 j hjk
· have := h2 (j - k) (by omega)
rw [show b + k + (j - k) = b + j by omega, show a + k + (j - k) = a + j by omega] at this
exact this
/-- The `k` bytes before `p` also match at distance `d`: the match starts `k` bytes earlier. -/
theorem AMatchAt.back_part {s : Slice Std.U8} {p l d k : Nat} (h : AMatchAt s p l d)
(hmk : Matches s (p - k - d) (p - k) k) (hlk : l + k ≤ 258) (hdk : d + k ≤ p) :
AMatchAt s (p - k) (l + k) d := by
obtain ⟨h3, h258, hpl, hd1, hd32, _, hm⟩ := h
refine ⟨by omega, hlk, by omega, hd1, hd32, by omega, ?_⟩
have hm' : Matches s (p - k - d + k) (p - k + k) l := by
rw [show p - k - d + k = p - d by omega, show p - k + k = p by omega]; exact hm
have := Matches.a_append hmk hm'
rw [show k + l = l + k by omega] at this
exact this
/-- One step of the counting loop: the byte at `p - 1 - k` matches at distance `d`. -/
theorem Matches.a_back_step {input : Slice Std.U8} {p d k : Nat} {i4 i7 k1 : Std.Usize}
{x y : Std.U8} (h : Matches input (p - k - d) (p - k) k) (hdk : d + k < p)
(hi4 : i4.val = p - 1 - k) (hi4l : i4.val < input.val.length) (hx : x = input.val[i4.val])
(hi7 : i7.val = p - 1 - k - d) (hi7l : i7.val < input.val.length)
(hy : y = input.val[i7.val]) (hxy : x = y) (hk1 : k1.val = k + 1) :
Matches input (p - k1.val - d) (p - k1.val) k1.val := by
rw [hk1]
apply Matches.a_cons
· rw [show p - (k + 1) - d + 1 = p - k - d by omega, show p - (k + 1) + 1 = p - k by omega]
exact h
· rw [show p - (k + 1) - d = i7.val by omega, show p - (k + 1) = i4.val by omega,
getElem!_pos input.val i4.val hi4l, getElem!_pos input.val i7.val hi7l, ← hx, ← hy, hxy]
/-! New lemmas: successful backward decoding recovers the old match equality. -/
theorem copyN_periodic (acc : List Nat) (d : Nat) (hd1 : 1 ≤ d) (hdp : d ≤ acc.length) :
∀ n k, k < n →
(LZ77.copyN acc d n)[acc.length + k]! =
(LZ77.copyN acc d n)[acc.length + k - d]! := by
intro n
induction n with
| zero => intro k hk; omega
| succ n ih =>
intro k hk
rw [LZ77.copyN]
have hlen : (LZ77.copyN acc d n).length = acc.length + n := LZ77.copyN_length ..
rcases Nat.lt_or_ge k n with hkn | hkn
· rw [List.getElem!_append_left _ _ _ (by omega),
List.getElem!_append_left _ _ _ (by omega)]
exact ih k hkn
· have hke : k = n := by omega
subst k
rw [List.getElem!_append_right _ _ _ (by omega),
List.getElem!_append_left _ _ _ (by omega)]
simp [hlen]
theorem getElemBang_take (inp : List Nat) (p k : Nat)
(hk : k < p) (hp : p ≤ inp.length) : (inp.take p)[k]! = inp[k]! := by
rw [getElem!_pos _ _ (by simp; omega), getElem!_pos _ _ (by omega), List.getElem_take]
theorem decode_pop_match_semantics {ts : List Nat} {t : Nat} {inp : List Nat} {p : Nat}
(hp : p ≤ inp.length) (hd : LZ77.decode (ts ++ [t]) = some (inp.take p)) (ht : 256 ≤ t) :
LZ77.tokLen t ≤ p ∧
LZ77.decode ts = some (inp.take (p - LZ77.tokLen t)) ∧
1 ≤ LZ77.tokDist t ∧ LZ77.tokDist t ≤ 32768 ∧
LZ77.tokDist t ≤ p - LZ77.tokLen t ∧
∀ k < LZ77.tokLen t,
inp[p - LZ77.tokLen t + k]! = inp[p - LZ77.tokLen t - LZ77.tokDist t + k]! := by
obtain ⟨hlp, hprev⟩ := a_decode_pop_match hp hd ht
refine ⟨hlp, hprev, ?_⟩
rw [LZ77.decode_snoc, hprev] at hd
simp only [Option.bind_some, LZ77.emit, if_neg (show ¬ t < 256 by omega)] at hd
split at hd
· rename_i htlegal
split at hd
· rename_i hdpos
have hcopy : LZ77.copyN (inp.take (p - LZ77.tokLen t)) (LZ77.tokDist t)
(LZ77.tokLen t) = inp.take p := Option.some.inj hd
have hdist1 : 1 ≤ LZ77.tokDist t := by unfold LZ77.tokDist; omega
have hdist32 : LZ77.tokDist t ≤ 32768 := by
simp only [LZ77.MATCH_BASE, LZ77.TOK_LIMIT] at htlegal
unfold LZ77.tokDist LZ77.MATCH_BASE
omega
have hlen : (inp.take (p - LZ77.tokLen t)).length = p - LZ77.tokLen t := by
simp only [List.length_take]; omega
refine ⟨hdist1, hdist32, by simpa [hlen] using hdpos, ?_⟩
intro k hk
have heq := copyN_periodic (inp.take (p - LZ77.tokLen t)) (LZ77.tokDist t)
hdist1 hdpos (LZ77.tokLen t) k hk
rw [hlen, hcopy, getElemBang_take inp p _ (by omega) hp,
getElemBang_take inp p _ (by omega) hp] at heq
rw [show p - LZ77.tokLen t + k - LZ77.tokDist t =
p - LZ77.tokLen t - LZ77.tokDist t + k by omega] at heq
exact heq
· simp at hd
· simp at hd
theorem ADec.pop_match_full {s : Slice Std.U8} {out : Slice Std.U32} {nt p : Nat}
(h : ADec s out nt p) (h0 : 0 < nt) (hle : nt ≤ out.length)
(ht : 256 ≤ (out.val[nt - 1]!).val) :
ADec s out (nt - 1) (p - LZ77.tokLen (out.val[nt - 1]!).val) ∧
AMatchAt s (p - LZ77.tokLen (out.val[nt - 1]!).val)
(LZ77.tokLen (out.val[nt - 1]!).val) (LZ77.tokDist (out.val[nt - 1]!).val) := by
have hde := h.2.2.2
rw [a_toks_pop out nt h0 hle] at hde
obtain ⟨hw, _, hd1, hd32, hdp, hm⟩ := decode_pop_match_semantics
(by simpa using h.1) hde ht
have hp : p ≤ s.length := h.1
have hpsum : p - LZ77.tokLen (out.val[nt - 1]!).val +
LZ77.tokLen (out.val[nt - 1]!).val = p := by omega
refine ⟨(ADec.pop_match h h0 hle ht).2, ?_⟩
have hl3 : 3 ≤ LZ77.tokLen (out.val[nt - 1]!).val := by
unfold LZ77.tokLen; omega
have hl258 : LZ77.tokLen (out.val[nt - 1]!).val ≤ 258 := by
unfold LZ77.tokLen; omega
refine ⟨hl3, hl258, by omega, hd1, hd32, hdp, ?_⟩
intro k hk
have heq := hm k hk
rw [LZ77.bytes_getElem! s _ (by omega), LZ77.bytes_getElem! s _ (by omega)] at heq
rw [getElem!_pos s.val (p - LZ77.tokLen (out.val[nt - 1]!).val + k) (by simpa using (show
p - LZ77.tokLen (out.val[nt - 1]!).val + k < s.length by omega)),
getElem!_pos s.val (p - LZ77.tokLen (out.val[nt - 1]!).val - LZ77.tokDist (out.val[nt - 1]!).val + k)
(by simpa using (show p - LZ77.tokLen (out.val[nt - 1]!).val -
LZ77.tokDist (out.val[nt - 1]!).val + k < s.length by omega))]
exact UScalar.eq_of_val_eq heq
theorem AMatchAt.prefix {s : Slice Std.U8} {p l d nl : Nat}
(h : AMatchAt s p l d) (hnl3 : 3 ≤ nl) (hnl : nl ≤ l) :
AMatchAt s p nl d := by
obtain ⟨h3, h258, hpl, hd1, hd32, hdp, hm⟩ := h
exact ⟨hnl3, by omega, by omega, hd1, hd32, hdp, Matches.mono hm hnl⟩
def NearPrefix (input : Slice Std.U8) (p l d : Nat) : Prop :=
d = 0 ∨ AMatchAt input p l d
theorem old_prefix_near {input : Slice Std.U8} {out : Slice Std.U32} {nt p w nl oldd : Nat}
(hdec : ADec input out nt p) (h0 : 0 < nt) (hle : nt ≤ out.length)
(ht : 256 ≤ (out.val[nt - 1]!).val)
(hw : w = LZ77.tokLen (out.val[nt - 1]!).val)
(hd : oldd = LZ77.tokDist (out.val[nt - 1]!).val)
(hnl3 : 3 ≤ nl) (hnl : nl ≤ w) : NearPrefix input (p - w) nl oldd := by
have hm := (ADec.pop_match_full hdec h0 hle ht).2
rw [← hw, ← hd] at hm
exact Or.inr (AMatchAt.prefix hm hnl3 hnl)
theorem fold_part_post {input : Slice Std.U8} {out out1 : Slice Std.U32}
{nt p l d w k c nl pp p1 l1 : Nat} {ntU : Std.Usize} {v : Std.U32}
(hdec : ADec input out nt p) (hm : AMatchAt input p l d)
(h0 : 1 ≤ nt) (hntl : nt ≤ out.length) (ht : 256 ≤ (out.val[nt - 1]!).val)
(hw : w = LZ77.tokLen (out.val[nt - 1]!).val)
(hmk : Matches input (p - k - d) (p - k) k) (hc : NearPrefix input pp nl c)
(hout1 : out1 = out.set ntU v)
(hlk : l + k ≤ 258) (hdk : d + k ≤ p) (hk : k ≤ w) (hpp : pp = p - w) (hnl : nl = w - k)
(hc0 : 0 < c) (hntU : ntU.val = nt - 1) (hv : v.val = LZ77.mkMatch c nl)
(hp1 : p1 = p - k) (hl1 : l1 = l + k) :
out1.length = out.length ∧ ADec input out1 nt p1 ∧ AMatchAt input p1 l1 d ∧
p1 + l1 = p + l ∧ p1 ≤ p := by
subst hp1 hl1 hpp
obtain ⟨hwp, hdec'⟩ := ADec.pop_match hdec (by omega) hntl ht
rw [← hw] at hdec' hwp
have hmc : AMatchAt input (p - w) nl c := by
rcases hc with hc | hc
· omega
· exact hc
have hd2 := ADec.match hdec' hmc ntU hntU v hv
rw [show nt - 1 + 1 = nt by omega, show p - w + nl = p - k by omega, ← hout1] at hd2
refine ⟨by rw [hout1, Std.Slice.set_length], hd2, AMatchAt.back_part hm hmk hlk hdk,
by omega, by omega⟩
/-- Any shorter suffix of the bytes immediately before p remains checked. -/
theorem Matches.back_trim {input : Slice Std.U8} {p d k c : Nat}
(h : Matches input (p - k - d) (p - k) k) (hc : c ≤ k) (hd : d + k ≤ p) :
Matches input (p - c - d) (p - c) c := by
intro j hj
have he := h (k - c + j) (by omega)
rw [show p - k + (k - c + j) = p - c + j by omega,
show p - k - d + (k - c + j) = p - c - d + j by omega] at he
exact he
/-- Replacing the last decoded match by one literal preserves its decoded prefix. -/Gate report
verifying [internal-path]
workspace: [internal-path]
0 intake ok — 407: parse.rs, Parse.lean
1 policy ok — source prefilters passed
2 static ok — resolved operations: core::num::{impl}::leading_zeros, core::num::{impl}::saturating_add, core::num::{impl}::saturating_sub, core::num::{impl}::wrapping_add, core::num::{impl}::wrapping_mul, core::num::{impl}::wrapping_sub, core::slice::{impl}::len
3 extract ok — charon+aeneas re-run by the verifier, no new axioms
4 statement ok — `LZ77.Obligation slot.parse` typechecks (bwrap, 900s, 16384 MB)
5 axioms ok — ['Classical.choice', 'Quot.sound', 'propext']; extraction intact
6 score running…
Corpus: corpus-stage1
file raw incumbent submission
-----------------------------------------------------
binary.db.bin 500000 76338 79919
bundle.min.js.txt 1000000 301618 315925
catalog.xml.txt 500000 62314 67515
compressed.bin 300000 300194 300095
config.yaml.txt 400000 85384 89185
docs.md.txt 800000 226973 239558
dump.sql.txt 150000 20817 21910
genome.fasta 900000 292479 289537
images.bin 250000 223767 230608
lean.txt 1000000 235059 251461
machine-code.bin 500000 167357 181583
metrics.csv.txt 1300000 287282 295594
multibyte.txt 1100000 273630 288834
page.html.txt 600000 103618 108496
prose.txt 1500000 579041 606499
records.json.txt 400000 72255 74819
server.log 300000 29404 29089
source.c.txt 1400000 354325 372225
source.py.txt 600000 132748 138891
source.rs.txt 700000 134069 141529
sourcemap.map.txt 350000 68389 73627
sparse.bin 40000 1234 1249
tiny-app.log 28000 965 1651
tiny-config.json.txt 12000 2107 2797
weights-bf16.bin 450000 362200 365670
weights-f16.bin 250000 216274 212660
weights-f32.bin 350000 284747 282753
weights-q8.bin 250000 236246 235572
-----------------------------------------------------
TOTAL 15930000 5130834 5299251
method bytes ratio lz77 encode total slowdown
-------------------------------------------------------------------------------------------------------------------
incumbent 5130834 1.00000x 0.628s 0.227s 0.855s 1.00x the incumbent
submission 5299251 1.03282x 0.066s 0.239s 0.305s 0.36x no improvement — 3.282% larger than the incumbent.
no improvement — 3.282% larger than the incumbent.
Corpus: corpus-stage2
corpus corpus-stage2 is held out; totals only.
method bytes ratio lz77 encode total slowdown
-------------------------------------------------------------------------------------------------------------------
incumbent 5218788 1.00000x 0.628s 0.230s 0.858s 1.00x the incumbent
submission 5391697 1.03313x 0.066s 0.244s 0.310s 0.36x no improvement — 3.313% larger than the incumbent.
no improvement — 3.313% larger than the incumbent.