tribble.crypto
Class LFSR

java.lang.Object
  extended bytribble.crypto.LFSR

public class LFSR
extends java.lang.Object

Logical Feedback Shift Register (LFSR) methods.

A Logical Feedback Shift Register, or LFSR, is a binary word of N bits in a Galois configuration that implements a shift operation. The shift operation examines the lowest (least significant) bit of the current value of the register and then exclusive-or's it with the mask value established for the LFSR; it then rotates the bits of the LFSR one bit, resulting in a new state for the LFSR. The output of the shift operation is the low bit.

By shifting an LFSR M times and concatenating the resulting output bits, an M-bit pseudo-random integer can be produced. An LFSR can thus be used as a pseudo-random number generator.

Since:
2002-11-10
Version:
$Revision: 1.1 $ $Date: 2002/11/14 02:57:18 $
Author:
David R. Tribble (david@tribble.com).
Copyright ©2002 by David R. Tribble, all rights reserved.
Permission is granted to freely use and distribute this source code provided that the original copyright and authorship notices remain intact.

Field Summary
static short DFL_SIZE
          Default LFSR size (bits).
protected  int[] m_bits
          LFSR state (N bits, as N/32 32-bit words).
protected  int[] m_mask
          LFSR exclusive-or mask (N bits, as N/32 32-bit words).
protected  short m_size
          LFSR width (bits).
static int SERIES
          Class version number.
 
Constructor Summary
LFSR()
          Default constructor.
LFSR(int size)
          Constructor.
 
Method Summary
 byte[] getBits()
          Retrieve the contents (value) of this LFSR.
 byte[] getMask()
          Retrieve the contents (value) of the mask for this LFSR.
static void main(java.lang.String[] args)
          Test driver.
 void setBits(byte[] val)
          Set the contents (value) of this LFSR.
 void setMask(byte[] val)
          Set the contents (value) of the mask for this LFSR.
 int shift()
          Shift (rotate) this LFSR right one bit, returning the resulting output bit.
 long shift(int n)
          Shift (rotate) this LFSR multiple times, returning the concatenation of the resulting output bits.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SERIES

public static final int SERIES
Class version number.

See Also:
Constant Field Values

DFL_SIZE

public static final short DFL_SIZE
Default LFSR size (bits).

See Also:
Constant Field Values

m_bits

protected int[] m_bits
LFSR state (N bits, as N/32 32-bit words). The words are stored in little-endian order.


m_mask

protected int[] m_mask
LFSR exclusive-or mask (N bits, as N/32 32-bit words). The words are stored in little-endian order.


m_size

protected short m_size
LFSR width (bits).

Constructor Detail

LFSR

public LFSR()
Default constructor. Constructs an LFSR with a size of 32 bits and an initial vector of 0x00000001.

Since:
1.0, 2002-11-07

LFSR

public LFSR(int size)
Constructor. Constructs an LFSR with a given size and an initial vector of 0x00000001.

Parameters:
size - The number of bits in the LFSR. This cannot be less than one or greater than 32,767.
Throws:
java.lang.IllegalArgumentException - Thrown if size is less than one.
Since:
1.0, 2002-11-07
Method Detail

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Test driver.

Parameters:
args - Command line arguments.
Throws:
java.lang.Exception
Since:
1.0, 2002-11-07

getBits

public byte[] getBits()
Retrieve the contents (value) of this LFSR.

Returns:
An array of bytes containing the bit value for this LFSR. The bits are in big-endian order, i.e., element [0] holds the most significant bits and element [val.length-1] holds the least significant bits.
Since:
1.0, 2002-11-07

setBits

public void setBits(byte[] val)
Set the contents (value) of this LFSR.

Note that setting all of the bits to zero results in an LFSR that generates a repeating sequence of nothing but zero bits.

Parameters:
val - An array of bytes containing the bit value for this LFSR. The bits are in big-endian order, i.e., element [0] holds the most significant bits and element [val.length-1] holds the least significant bits. Most significant bits beyond the size of this LFSR are ignored.
Since:
1.0, 2002-11-07

getMask

public byte[] getMask()
Retrieve the contents (value) of the mask for this LFSR.

Returns:
An array of bytes containing the bit value of the exclusive-or mask for this LFSR. The bits are in big-endian order, i.e., element [0] holds the most significant bits and element [val.length-1] holds the least significant bits.
Since:
1.0, 2002-11-07

setMask

public void setMask(byte[] val)
Set the contents (value) of the mask for this LFSR.

Parameters:
val - An array of bytes containing the bit value of the exclusive-or mask for this LFSR. The bits are in big-endian order, i.e., element [0] holds the most significant bits and element [val.length-1] holds the least significant bits. Most significant bits beyond the size of this LFSR are ignored.
Since:
1.0, 2002-11-07

shift

public int shift()
Shift (rotate) this LFSR right one bit, returning the resulting output bit.

Returns:
The low order output bit that results from changing the state of this LFSR. This is a one-bit value returned as an int.
Since:
1.0, 2002-11-07

shift

public long shift(int n)
Shift (rotate) this LFSR multiple times, returning the concatenation of the resulting output bits.

Parameters:
n - The number of times to shift (rotate) this LFSR, which is also the number of bits in the return value.
Returns:
The concatenation of the output bits of n shifts (rotations) of this LFSR. Since a long value is returned, only the last 64 bits of the shifts can be returned.
Since:
1.0, 2002-11-07