CodeSampleX

Ejemplo

github.com/klauspost/compress v1.19.1: zlib.BestSpeed

Muestra verificada para golang github.com/klauspost/compress v1.19.1: zlib.BestSpeed. El contrato se ejecutó en go 1.26 · linux debian/x64 · docker y pasó.

sha256:cb6da30dadd29523dbf837c7f1d3469862964a76c94f0599a20b16b05cb70a67

Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido. Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas. MIT-0

Evidencia de ejecución

El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.

Base de evidencia
Contrato firmado aprobado
Recibos de verificación
1
Claves de firma que lo compilaron
1
Entorno declarado linux 24 · ubuntu · glibc 2.39 x64 go

Entornos de las ejecuciones de verificación

Entorno Contrato Etapas Ejecución
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-10-11

Caso

HOW
Objetivo
verify github.com/klauspost/compress/zlib.BestSpeed in pkg:golang/github.com/klauspost/compress@v1.19.1
Paquetes
Símbolos
  • github.com/klauspost/compress/zlib.BestSpeed
Creado
2026-10-11T05:52:38Z

Contrato

  1. zlib.BestSpeed can be passed to zlib.NewWriterLevel to create a valid non-nil zlib writer
  2. zlib.NewWriterLevel with zlib.BestSpeed produces a valid compressed stream that round-trips through zlib.NewReader
  3. zlib.Writer configured with zlib.BestSpeed can be reused across multiple payloads via Reset
  4. zlib.NewWriterLevelDict accepts zlib.BestSpeed with a preset dictionary and successfully compresses data

Archivos

  • PROMPT.md
  • compressor.go
  • csx.json
  • go.mod
  • go.sum
  • spec.json
  • test/contract.go

Descargar el artefacto de código fuente (tar.gz)

Código fuente

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify github.com/klauspost/compress/zlib.BestSpeed in pkg:golang/github.com/klauspost/compress@v1.19.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/klauspost/compress@v1.19.1
Demonstrate these symbols/APIs:
  - github.com/klauspost/compress/zlib.BestSpeed

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
compressor.go
package sample

import (
	"bytes"
	"io"

	"github.com/klauspost/compress/zlib"
)

// NewBestSpeedWriter returns a new *zlib.Writer configured with zlib.BestSpeed.
func NewBestSpeedWriter(w io.Writer) (*zlib.Writer, error) {
	return zlib.NewWriterLevel(w, zlib.BestSpeed)
}

// NewBestSpeedWriterDict returns a new *zlib.Writer configured with zlib.BestSpeed and a dictionary.
func NewBestSpeedWriterDict(w io.Writer, dict []byte) (*zlib.Writer, error) {
	return zlib.NewWriterLevelDict(w, zlib.BestSpeed, dict)
}

// CompressBestSpeed compresses input data using zlib.BestSpeed compression level.
func CompressBestSpeed(data []byte) ([]byte, error) {
	var buf bytes.Buffer
	zw, err := zlib.NewWriterLevel(&buf, zlib.BestSpeed)
	if err != nil {
		return nil, err
	}
	if _, err := zw.Write(data); err != nil {
		_ = zw.Close()
		return nil, err
	}
	if err := zw.Close(); err != nil {
		return nil, err
	}
	return buf.Bytes(), nil
}

// Decompress decompresses a zlib-compressed byte slice.
func Decompress(compressed []byte) ([]byte, error) {
	zr, err := zlib.NewReader(bytes.NewReader(compressed))
	if err != nil {
		return nil, err
	}
	defer zr.Close()
	return io.ReadAll(zr)
}

// CompressWithDict compresses data using zlib.BestSpeed and a preset dictionary.
func CompressWithDict(data, dict []byte) ([]byte, error) {
	var buf bytes.Buffer
	zw, err := zlib.NewWriterLevelDict(&buf, zlib.BestSpeed, dict)
	if err != nil {
		return nil, err
	}
	if _, err := zw.Write(data); err != nil {
		_ = zw.Close()
		return nil, err
	}
	if err := zw.Close(); err != nil {
		return nil, err
	}
	return buf.Bytes(), nil
}

// DecompressWithDict decompresses data with a preset dictionary.
func DecompressWithDict(compressed, dict []byte) ([]byte, error) {
	zr, err := zlib.NewReaderDict(bytes.NewReader(compressed), dict)
	if err != nil {
		return nil, err
	}
	defer zr.Close()
	return io.ReadAll(zr)
}
csx.json
{"case":{"caseId":"case:sha256:26fad6107871e4272f4d5298ca8c84b8b1ea2b2c493c01fd51a5c4152010f0ce","contract":["zlib.BestSpeed can be passed to zlib.NewWriterLevel to create a valid non-nil zlib writer","zlib.NewWriterLevel with zlib.BestSpeed produces a valid compressed stream that round-trips through zlib.NewReader","zlib.Writer configured with zlib.BestSpeed can be reused across multiple payloads via Reset","zlib.NewWriterLevelDict accepts zlib.BestSpeed with a preset dictionary and successfully compresses data"],"goal":"verify github.com/klauspost/compress/zlib.BestSpeed in pkg:golang/github.com/klauspost/compress@v1.19.1","kind":"HOW","packages":["pkg:golang/github.com/klauspost/compress@v1.19.1"],"schemaVersion":1,"symbols":["github.com/klauspost/compress/zlib.BestSpeed"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/klauspost/compress@v1.19.1"],"schemaVersion":1,"subject":"pkg:golang/github.com/klauspost/compress@v1.19.1","symbols":["github.com/klauspost/compress/zlib.BestSpeed"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require github.com/klauspost/compress v1.19.1
go.sum
github.com/klauspost/compress v1.19.1 h1:VsB4HPswih7mmZ8WleSFQ75c/Ui1M4trX5oAsJnhSlk=
github.com/klauspost/compress v1.19.1/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/klauspost/compress/zlib.BestSpeed in pkg:golang/github.com/klauspost/compress@v1.19.1",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/klauspost/compress@v1.19.1"
  ],
  "symbols": [
    "github.com/klauspost/compress/zlib.BestSpeed"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"fmt"
	"os"

	"github.com/klauspost/compress/zlib"
	"sample"
)

func main() {
	// Assertion 1: zlib.BestSpeed can be passed to zlib.NewWriterLevel to create a valid non-nil zlib writer
	var buf1 bytes.Buffer
	w1, err := sample.NewBestSpeedWriter(&buf1)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL assertion 1: NewBestSpeedWriter failed: %v\n", err)
		os.Exit(1)
	}
	if w1 == nil {
		fmt.Fprintf(os.Stderr, "FAIL assertion 1: NewBestSpeedWriter returned nil writer\n")
		os.Exit(1)
	}

	// Verify zlib.BestSpeed constant value
	if zlib.BestSpeed != 1 {
		fmt.Fprintf(os.Stderr, "FAIL assertion 1: unexpected zlib.BestSpeed value %d (expected 1)\n", zlib.BestSpeed)
		os.Exit(1)
	}

	// Assertion 2: zlib.NewWriterLevel with zlib.BestSpeed produces a valid compressed stream that round-trips through zlib.NewReader
	testPayloads := [][]byte{
		[]byte(""),
		[]byte("hello world with klauspost compress best speed"),
		bytes.Repeat([]byte("The quick brown fox jumps over the lazy dog. "), 50),
	}
	for i, payload := range testPayloads {
		compressed, err := sample.CompressBestSpeed(payload)
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL assertion 2: payload %d compression failed: %v\n", i, err)
			os.Exit(1)
		}
		decompressed, err := sample.Decompress(compressed)
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL assertion 2: payload %d decompression failed: %v\n", i, err)
			os.Exit(1)
		}
		if !bytes.Equal(decompressed, payload) {
			fmt.Fprintf(os.Stderr, "FAIL assertion 2: payload %d round-trip mismatch\n", i)
			os.Exit(1)
		}
	}

	// Assertion 3: zlib.Writer configured with zlib.BestSpeed can be reused across multiple payloads via Reset
	var resetBuf1, resetBuf2 bytes.Buffer
	reuseWriter, err := zlib.NewWriterLevel(&resetBuf1, zlib.BestSpeed)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL assertion 3: NewWriterLevel failed: %v\n", err)
		os.Exit(1)
	}
	msg1 := []byte("stream payload one")
	if _, err := reuseWriter.Write(msg1); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL assertion 3: first stream write failed: %v\n", err)
		os.Exit(1)
	}
	if err := reuseWriter.Close(); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL assertion 3: first stream close failed: %v\n", err)
		os.Exit(1)
	}

	reuseWriter.Reset(&resetBuf2)
	msg2 := []byte("stream payload two with distinct content")
	if _, err := reuseWriter.Write(msg2); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL assertion 3: second stream write failed: %v\n", err)
		os.Exit(1)
	}
	if err := reuseWriter.Close(); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL assertion 3: second stream close failed: %v\n", err)
		os.Exit(1)
	}

	out1, err := sample.Decompress(resetBuf1.Bytes())
	if err != nil || !bytes.Equal(out1, msg1) {
		fmt.Fprintf(os.Stderr, "FAIL assertion 3: stream 1 verification failed: err=%v\n", err)
		os.Exit(1)
	}
	out2, err := sample.Decompress(resetBuf2.Bytes())
	if err != nil || !bytes.Equal(out2, msg2) {
		fmt.Fprintf(os.Stderr, "FAIL assertion 3: stream 2 verification failed: err=%v\n", err)
		os.Exit(1)
	}

	// Assertion 4: zlib.NewWriterLevelDict accepts zlib.BestSpeed with a preset dictionary and successfully compresses data
	dict := []byte("frequently recurring domain specific dictionary tokens")
	dictPayload := []byte("frequently recurring domain specific tokens in a test document")
	compressedDict, err := sample.CompressWithDict(dictPayload, dict)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL assertion 4: CompressWithDict failed: %v\n", err)
		os.Exit(1)
	}
	decompressedDict, err := sample.DecompressWithDict(compressedDict, dict)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL assertion 4: DecompressWithDict failed: %v\n", err)
		os.Exit(1)
	}
	if !bytes.Equal(decompressedDict, dictPayload) {
		fmt.Fprintf(os.Stderr, "FAIL assertion 4: dict round-trip mismatch\n")
		os.Exit(1)
	}

	fmt.Println("PASS: github.com/klauspost/compress/zlib.BestSpeed contract verified")
}

Seeder de origen

anónimo