dev-master
9999999-devLinear Feedback Shift Register (LFSR) for Symfony2
MIT
The Requires
symfony lfsr
Wallogit.com
2017 © Pedro Peláez
Linear Feedback Shift Register (LFSR) for Symfony2
Linear Feedback Shift Register (LFSR) for Symfony2, (*1)
Edit composer.json, (*2)
"require": {
...
"glanchow/wok-lfsr-bundle": "*"
Then update, (*3)
php composer.phar update
// src app/AppKernel.php
public function registerBundles()
...
$bundles = array(
...
new Wok\LfsrBundle\WokLfsrBundle(),
# src app/config/config.yml
# Wok LSFR Configuration
wok_lfsr:
feedback: 0xC
state: 1
base: null
pad: false
Here's a list with taps for n up to 4096: http://www.eej.ulst.ac.uk/~ian/modules/EEE515/files/old_files/lfsr/lfsr_table.pdf, (*4)
Here's a list with feedback terms for n up to 40, but with multiple and differents feedback terms: https://www.ece.cmu.edu/~koopman/lfsr/index.html, (*5)
Let's say you want a maximum-length feedback term for a 8 bit LFSR., (*6)
For n = 8, possible taps are 8, 6, 5, 4., (*7)
$ bc obase=16 ibase=2 10111000 B8
Or more simply:, (*8)
$ bc obase=16 2^7 + 2^5 + 2^4 + 2^3 B8
0xB8 is your feedback term., (*9)
| n | feedback |
|---|---|
| 4 | 0xC |
| 5 | 0x1E |
| 6 | 0x36 |
| 7 | 0x78 |
| 8 | 0xB8 |
| 16 | 0xB400 |
| 30 | 0x32800000 |
| 31 | 0x78000000 |
| 32 | 0xA3000000 |
| 63 | 0x6600000000000000 |
| 64 | 0xD800000000000000 |
Using default or global configuration:, (*10)
$lfsr = $this->get('wok_lfsr');
Using a custom configuration:, (*11)
$config = array(
'feedback' => 0xC,
'state' => '11',
'base' => '01',
'pad' => true
);
$lfsr = $this->get('wok_lfsr')->config($config);
$state = $lfsr->next();
$config = array(
'feedback' => 0xC,
'state' => 0x1,
'base' => null,
'pad' => false
);
$lfsr = $this->get('wok_lfsr')->config($config);
$lfsr->setState(4);
$end = 4;
$iterations = 0;
do {
$iterations++;
$state = $lfsr->next();
echo $state . "<br/>";
} while ($state != $end);
echo "$iterations iterations<br/>";
LFSR can be used to generate random looking identifiers for database records (mysql, postgresql, etc)., (*12)
Let's say you want a short random looking identifier for one of a billion records., (*13)
# src app/config/config.yml
wok_lfsr:
feedback: 0x32800000
state: 1
base: 0123456789bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ
pad: true
Let's explain this configuration., (*14)
For a billion record we need at least 30 bits., (*15)
We've found a 30 bits feedback term over the www: 0x32800000., (*16)
The object automatically finds that this is a feedback term for a 30 bit LFSR, it's easy since a n bit LFSR needs a n tap., (*17)
To get a short word, we selected a 50 symbols base., (*18)
The object automatically finds that we need 6 symbols (50^6) to write all (2^30) words, and takes for padding symbol, the first symbol of the base., (*19)
If your feedback term is greater than the server's PHP_INT_MAX, which is processor dependant, it will be converted to a float number and the current code will provide wrong results., (*20)
PHP doesn't use the unsigned int type. In the case you want only positive numbers or use a custom base, please use at most n -1 bits of your processor capacity., (*21)
Linear Feedback Shift Register (LFSR) for Symfony2
MIT
symfony lfsr