Browse zlib specs (43 of 62)

Proofs / zlib / Checksums

ck_crc32

crc32_z equals the bit-by-bit CRC-32 of RFC 1952, written out in the spec, for any bytes and any starting value.

spec
29 lines of C
with its headers
69 lines
status
Proved
43 of 62
On this page

The spec

The spec is the C program harness/ck_crc32.c, 29 lines, shown whole as it is in the repository at 37189f4. It includes harness/laws_common.h (40 lines). Its first comment states the property in full, with every bound.

C
// Law ck_crc32: For any bytes (any length up to PTRDIFF_MAX that the allocator provides) and any CRC-32 value to
// continue from, crc32_z returns the CRC-32 of RFC 1952 as computed bit by bit below.
#include "laws_common.h"

int main(void) {
	size_t len = upto(PTRDIFF_MAX);
	unsigned char *buf = alloc_any_bytes(len);
	if (buf == NULL)
		return 0;
	uLong start = nondet_u32();
	// RFC 1952 2.3.1: CRC32 is "computed according to CRC-32 algorithm used in the ISO 3309 standard"; its section
	// 8 sample code: "c = crc ^ 0xffffffffL", per byte "c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8)", where a
	// table entry is 8 steps of "if (c & 1) { c = 0xedb88320L ^ (c >> 1); } else { c = c >> 1; }", "return c ^
	// 0xffffffffL"
	unsigned long crc = start ^ 0xffffffff;
	for (size_t i = 0; i < len; i++) {
		crc ^= buf[i];
		for (int step = 0; step < 8; step++) {
			if (crc & 1)
				crc = 0xedb88320 ^ (crc >> 1);
			else
				crc = crc >> 1;
		}
	}

	gp_assert(crc32_z(start, buf, len) == (crc ^ 0xffffffff));

	return 0;
}
harness/laws_common.h, 40 lines
C
// laws_common.h: trivial helpers shared by the zlib laws (each law includes it once). No models, no algorithms.
#include <limits.h>
#include <stdlib.h>
#include "quickproof.h"
#include "zlib.h"
static size_t upto(size_t max) {
	size_t value = nondet_size();
	return value < max ? value : max;
}
static uInt clip(size_t n) { return n < UINT_MAX ? (uInt) n : UINT_MAX; }
static unsigned char *alloc_any_bytes(size_t n) {
	unsigned char *bytes = malloc(n);
	if (bytes != NULL)
		nondet_bytes(bytes, n);
	return bytes;
}
static unsigned char *copy_of(const unsigned char *source, size_t n) {
	unsigned char *copy = malloc(n);
	for (size_t i = 0; copy != NULL && i < n; i++)
		copy[i] = source[i];
	return copy;
}
static int bytes_eq(const unsigned char *a, const unsigned char *b, size_t n) {
	for (size_t i = 0; i < n; i++)
		if (a[i] != b[i])
			return 0;
	return 1;
}
static void set_buffer(z_stream *strm, unsigned char *in, size_t in_len, unsigned char *out, size_t out_len) {
	strm->next_in = in;
	strm->avail_in = (uInt) in_len;
	strm->next_out = out;
	strm->avail_out = (uInt) out_len;
}
static void new_stream(z_stream *strm) {
	strm->zalloc = Z_NULL;
	strm->zfree = Z_NULL;
	strm->opaque = Z_NULL;
	set_buffer(strm, Z_NULL, 0, Z_NULL, 0);
}

The theorem

QuickProof generated this statement from the spec, in src/contracts/ck_crc32.v:

Rocq
Theorem ck_crc32 : harness_safe whole_prog.

src/contracts/laws.v states it on the spec's own program, whole_prog (the spec linked with the code under test), with its proof:

Rocq
Theorem ck_crc32 : harness_safe Zlib.contracts.ck_crc32.whole_prog.
Proof. exact (proj1 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 (proj2 Zlib.contracts.suite.suite)))))))))))))). Qed.

Read as a statement about runs, it says that no run of the program has undefined behaviour or a failing check, for every value the spec can draw. That reading rests on VST's adequacy theorem, which is not yet instantiated in Rocq (formerly Coq) for QuickProof's environment.

The C files (units) its program links
  • crc32.c

What it leaves out

  • What the first comment of the spec excludes, and its bounds.
  • Like every spec of this proof: termination (a run that never returns is not a counterexample), and binaries built by a compiler (the theorem is about the code as CompCert defines C for x86_64, LP64), one thread.
  • Where zlib reports Z_MEM_ERROR, most specs end the run and claim nothing about it; the first comment of the spec says so.
  • The edits to zlib's code, listed in the repository, are tested, not proved.

The summary page says what the proof as a whole covers.

How it was proved

AI agents wrote the proof, and Rocq checks it. qp verify passes on this repository: this statement is the one generated from the spec at the contract commit, and its proof uses only Rocq's standard axioms and VST's abstract malloc library.