lfsr

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package lfsr provides stream-cipher / LFSR analysis primitives relevant to RF scramblers and keystream study: Berlekamp–Massey recovery of the shortest LFSR that produces an observed bit sequence, Fibonacci-LFSR generation, and known-plaintext keystream extraction.

All bit sequences are represented as []byte holding 0/1 values (one bit per element), which keeps the algorithms readable; BitsFromBytes/ BytesToBits convert to and from packed bytes (MSB-first).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BitsFromBytes

func BitsFromBytes(data []byte) []byte

BitsFromBytes expands packed bytes into a 0/1 bit slice, MSB first.

func BitsToBytes

func BitsToBytes(bits []byte) []byte

BitsToBytes packs a 0/1 bit slice into bytes, MSB first. Trailing bits that do not fill a byte are left-aligned (zero-padded on the right).

func Generate

func Generate(taps []int, seed []byte, n int) ([]byte, error)

Generate produces n output bits from a Fibonacci LFSR with the given 1-based tap positions and an initial state seed (seed[0] is the first bit shifted out). The register length is the max tap. Output bit = the bit leaving the register; feedback = XOR of the tapped stages. seed length must be at least the register length.

func Keystream

func Keystream(pt, ct []byte) []byte

Keystream returns plaintext XOR ciphertext over the common prefix — the keystream of an additive stream cipher under known plaintext. The result length is min(len(pt), len(ct)).

Types

type Result

type Result struct {
	LinearComplexity int
	Polynomial       []byte // length L+1, Polynomial[0] == 1
}

Result is the outcome of Berlekamp–Massey: the linear complexity L (the length of the shortest LFSR) and the connection polynomial C(x) = 1 + c1·x + ... + cL·x^L as coefficients [1, c1, ..., cL] over GF(2).

func BerlekampMassey

func BerlekampMassey(s []byte) Result

BerlekampMassey returns the shortest LFSR (over GF(2)) consistent with the bit sequence s (each element 0 or 1). The linear complexity is a strong fingerprint: a short LFSR means a simple scrambler; complexity ≈ len(s)/2 means no short linear generator (the sequence looks random to a linear model).

func (Result) Taps

func (r Result) Taps() []int

Taps returns the feedback tap positions (1-based powers of x with a non-zero coefficient), i.e. the indices into the connection polynomial excluding the constant term.

Jump to

Keyboard shortcuts

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