From f7db9006fd15b052518f3a385c5a374ecebed1f9 Mon Sep 17 00:00:00 2001 From: Michael Foukarakis Date: Tue, 26 Mar 2013 12:42:24 +0200 Subject: [PATCH] Consistent naming of types --- lfsr.c | 38 +++++++++++++++++++------------------- lfsr.h | 12 ++++++------ lfsr.py | 44 +++++++++++++++++++++----------------------- prng.c | 8 +++----- 4 files changed, 49 insertions(+), 53 deletions(-) diff --git a/lfsr.c b/lfsr.c index 851d14f..8c77335 100644 --- a/lfsr.c +++ b/lfsr.c @@ -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: @@ -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://homepage.mac.com/afj/taplist.html * http://www.xilinx.com/support/documentation/application_notes/xapp052.pdf *------------------------------------------------------------------------ @@ -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) diff --git a/lfsr.h b/lfsr.h index 12a8c22..0f5cb42 100644 --- a/lfsr.h +++ b/lfsr.h @@ -3,13 +3,13 @@ */ #include -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 *); diff --git a/lfsr.py b/lfsr.py index 91b3168..8b564dc 100644 --- a/lfsr.py +++ b/lfsr.py @@ -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 @@ -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)) diff --git a/prng.c b/prng.c index 97c46da..ffcbca9 100644 --- a/prng.c +++ b/prng.c @@ -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 \n", argv[0]);