permutation

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 30, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package permutation provides the shared F5 permutative-straddling primitives: PRNG seeding, Fisher-Yates permutation generation, and the JPEG de-zigzag index transform. Both the embed and extract capabilities depend on these behaving identically, so they live here exactly once.

The package depends only on the lightweight f5core, f5prng and fisheryates primitives; it pulls in neither a JPEG codec nor localization tables, so it is safe to import from the lightest build paths.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyDeZigZag

func ApplyDeZigZag(shuffledIndex int) int

ApplyDeZigZag maps a permuted (zigzag-order) coefficient index to its natural 8x8 block position using the JPEG de-zigzag table.

JPEG stores DCT coefficients in zigzag order; F5 addresses them in natural order. For a non-negative index the transform is:

block base   = shuffled &^ 63   (== shuffled - shuffled%64)
in-block slot = f5core.DeZigZag[shuffled & 63]
result        = block base + in-block slot

Permutation indices are always non-negative, so the branch-free masked form is exact and avoids per-coefficient integer division.

func GeneratePermutation

func GeneratePermutation(random f5prng.RandomSource, size int) ([]int, error)

GeneratePermutation returns a Fisher-Yates permutation of [0, size).

The permutation drives F5's permutative straddling, distributing coefficient changes uniformly across the carrier. It is deterministic in the PRNG state, so the same seed yields the same permutation for embedding and extraction, and it consumes PRNG state exactly as the Java reference expects.

It returns an error if size is negative or exceeds the permutator's maximum.

func InitializePRNG

func InitializePRNG(password string) f5prng.RandomSource

InitializePRNG creates and seeds an f5prng.RandomSource for F5.

The PRNG is seeded with the RAW password bytes. The reference Westfeld f5.jar constructs its generator as new SecureRandom(password.getBytes()); Java's SHA1PRNG then derives its state as SHA-1(password). f5prng's Seed applies that same internal SHA-1, so passing the raw password bytes yields byte-parity with f5.jar. Pre-hashing here would double-hash and derive the wrong permutation.

Seeding can only fail on a nil or broken hasher; the default factory always supplies a live SHA-1 hasher, so the error path is unreachable in practice. Any genuine failure is also recorded in the PRNG's LastError and surfaces on the first draw, so the seeded source is returned regardless.

password is encoded as UTF-8 ([]byte(password)). ASCII passwords are recommended for interoperability with the Java reference.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL