Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

Commit

Permalink
Consistent naming of types
Browse files Browse the repository at this point in the history
  • Loading branch information
mfukar committed Mar 26, 2013
1 parent 2d20e15 commit f7db900
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 53 deletions.
38 changes: 19 additions & 19 deletions lfsr.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
* @file /home/mfukar/src/lfsr/lfsr.c
* @file lfsr.c
* @author Michael Foukarakis
* @version 1.0
* @version 1.1
* @date
* Created: Wed Feb 02, 2011 18:47 EET
* Last Update: Wed Feb 02, 2011 18:58 EET
* Last Update: Tue Mar 26, 2013 12:40 GTB Standard Time
*------------------------------------------------------------------------
* Description: Galois LFSR software implementation
* WARNING:
Expand Down Expand Up @@ -32,7 +32,7 @@
* x^10 + x^3 + 1
* x^10 + x^9 + x^8 + x^6 + x^3 + x^2 + 1
*
* Source of secure polynoms:
* Source of polynomials:
* http:https://homepage.mac.com/afj/taplist.html
* http:https://www.xilinx.com/support/documentation/application_notes/xapp052.pdf
*------------------------------------------------------------------------
Expand All @@ -42,25 +42,25 @@
*/
#include "lfsr.h"

void GLFSR_init(lfsr_t *glfsr, lfsr_data polynom, lfsr_data seed_value)
void GLFSR_init(lfsr_t *glfsr, lfsr_data_t polynom, lfsr_data_t seed_value)
{
lfsr_data seed_mask;
unsigned int shift = 8 * sizeof(lfsr_data) - 1;
lfsr_data_t seed_mask;
unsigned int shift = 8 * sizeof(lfsr_data) - 1;

glfsr->polynomial = polynom | 1;
glfsr->data = seed_value;
glfsr->polynomial = polynom | 1;
glfsr->data = seed_value;

seed_mask = 1;
seed_mask <<= shift;
seed_mask = 1;
seed_mask <<= shift;

while(shift--) {
if(polynom & seed_mask) {
glfsr->mask = seed_mask;
break;
}
seed_mask >>= 1;
}
return;
while(shift--) {
if(polynom & seed_mask) {
glfsr->mask = seed_mask;
break;
}
seed_mask >>= 1;
}
return;
}

unsigned char GLFSR_next(lfsr_t *glfsr)
Expand Down
12 changes: 6 additions & 6 deletions lfsr.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
*/
#include <stdint.h>

typedef uint64_t lfsr_data;
typedef uint64_t lfsr_data_t;

typedef struct {
lfsr_data data;
lfsr_data polynomial;
lfsr_data mask;
lfsr_data_t data,
polynomial,
mask;
} lfsr_t;

void GLFSR_init(lfsr_t *, lfsr_data, lfsr_data);
unsigned char GLFSR_next(lfsr_t *);
void GLFSR_init(lfsr_t *, lfsr_data_t, lfsr_data_t);
unsigned char GLFSR_next(lfsr_t *);
44 changes: 21 additions & 23 deletions lfsr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# @author Michael Foukarakis
# @version 1.0
# @date Created: Thu Feb 03, 2011 08:19 GTB Standard Time
# Last Update: Thu Jun 30, 2011 00:33 PDT
# Last Update: Tue Mar 26, 2013 12:41 GTB Standard Time
#------------------------------------------------------------------------
# Description: Implementation of a Galois LFSR
# The bitstream is provided by the generator function
Expand Down Expand Up @@ -47,28 +47,26 @@ def states(self, repeat=False):
One = Polynomial([1])

def bm(seq):
'''
Implementation of the Berlekamp-Massey algorithm, the purpose
of which is to find a LFSR with the smallest possible length
that generates a given sequence.
A generator is returned that yields the current LFSR at
each call. At the k-th call the yielded LFSR is guaranteed
to generate the first k bits of S.
Input :
Output: A list of coefficients [c0, c1,..., c{m-1}]
Internally, if the state of the LFSR is (x0,x1,...,x{m-1})
then the output bit is x0, the register contents are shifted
to the left, and the new
x{m-1} = c0 * x0 + c1 * x1 +...+c{m-1} * x{m-1}
The generating function G(x) = s_0 + s_1 * x^1 + s_2 * x^2 + ...
of a LFSR is rational and (when written in lowest terms) the
denominator f(x) is called the characteristic polynomial of the
LFSR. Here we have f(x) = c0 * x^m + c1 * x^{m-1} +...+ c{m-1} * x + 1.
'''
'''Implementation of the Berlekamp-Massey algorithm, the purpose
of which is to find a LFSR with the smallest possible length
that generates a given sequence.
A generator is returned that yields the current LFSR at
each call. At the k-th call the yielded LFSR is guaranteed
to generate the first k bits of S.
Input :
Output: A list of coefficients [c0, c1,..., c{m-1}]
Internally, if the state of the LFSR is (x0,x1,...,x{m-1})
then the output bit is x0, the register contents are shifted
to the left, and the new
x{m-1} = c0 * x0 + c1 * x1 +...+c{m-1} * x{m-1}
The generating function G(x) = s_0 + s_1 * x^1 + s_2 * x^2 + ...
of a LFSR is rational and (when written in lowest terms) the
denominator f(x) is called the characteristic polynomial of the
LFSR. Here we have f(x) = c0 * x^m + c1 * x^{m-1} +...+ c{m-1} * x + 1.'''
# Allow for an input string along with a list or tuple
if type(S)==type("string"):
S = map(int,tuple(S))
Expand Down
8 changes: 3 additions & 5 deletions prng.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ lfsr_t glfsr_c0;

int main(int argc, char **argv)
{
unsigned char bit0;
unsigned char bitc0;
unsigned char bitr = 0;
unsigned char bit0, bitc0, bitr = 0;
char byte = 0, bitpos = 7;
unsigned long long bitcounter = 0, ones = 0, zeros = 0, dropped = 0;
lfsr_data polynom_d, init_value_d,
polynom_c, init_value_c;
lfsr_data_t polynom_d, init_value_d,
polynom_c, init_value_c;

if (argc < 5) {
fprintf(stderr, "Usage: %s <data_polynomial> <data_seed> <clock_polynomial> <clock_seed>\n", argv[0]);
Expand Down

0 comments on commit f7db900

Please sign in to comment.