1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
/*
* lfsr.c
*
*/
/*
*
* Copyright (c) 2001-2006, Cisco Systems, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* Neither the name of the Cisco Systems, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <stdio.h>
#include "datatypes.h"
uint32_t
parity(uint32_t x) {
x ^= (x >> 16);
x ^= (x >> 8);
x ^= (x >> 4);
x ^= (x >> 2);
x ^= (x >> 1);
return x & 1;
}
/* typedef struct { */
/* uint32_t register[8]; */
/* } lfsr_t; */
void
compute_period(uint32_t feedback_polynomial) {
int i;
v32_t lfsr;
v32_t mask;
mask.value = feedback_polynomial;
lfsr.value = 1;
printf("polynomial: %s\t", v32_bit_string(mask));
for (i=0; i < 256; i++) {
/* printf("%s\n", v32_bit_string(lfsr)); */
if (parity(mask.value & lfsr.value))
lfsr.value = ((lfsr.value << 1) | 1) & 0xff;
else
lfsr.value = (lfsr.value << 1) & 0xff;
/* now halt if we're back at the initial state */
if (lfsr.value == 1) {
printf("period: %d\n", i);
break;
}
}
}
uint32_t poly0 = 223;
uint32_t polynomials[39] = {
31,
47,
55,
59,
61,
79,
87,
91,
103,
107,
109,
115,
117,
121,
143,
151,
157,
167,
171,
173,
179,
181,
185,
199,
203,
205,
211,
213,
227,
229,
233,
241,
127,
191,
223,
239,
247,
251,
253
};
char binary_string[32];
char *
u32_bit_string(uint32_t x, unsigned int length) {
unsigned int mask;
int index;
mask = 1 << length;
index = 0;
for (; mask > 0; mask >>= 1)
if ((x & mask) == 0)
binary_string[index++] = '0';
else
binary_string[index++] = '1';
binary_string[index++] = 0; /* NULL terminate string */
return binary_string;
}
extern int octet_weight[256];
unsigned int
weight(uint32_t poly) {
int wt = 0;
/* note: endian-ness makes no difference */
wt += octet_weight[poly & 0xff];
wt += octet_weight[(poly >> 8) & 0xff];
wt += octet_weight[(poly >> 16) & 0xff];
wt += octet_weight[(poly >> 24)];
return wt;
}
#define MAX_PERIOD 65535
#define debug_print 0
int
period(uint32_t poly) {
int i;
uint32_t x;
/* set lfsr to 1 */
x = 1;
#if debug_print
printf("%d:\t%s\n", 0, u32_bit_string(x,8));
#endif
for (i=1; i < MAX_PERIOD; i++) {
if (x & 1)
x = (x >> 1) ^ poly;
else
x = (x >> 1);
#if debug_print
/* print for a sanity check */
printf("%d:\t%s\n", i, u32_bit_string(x,8));
#endif
/* check for return to original value */
if (x == 1)
return i;
}
return i;
}
/*
* weight distribution computes the weight distribution of the
* code generated by the polynomial poly
*/
#define MAX_LEN 8
#define MAX_WEIGHT (1 << MAX_LEN)
int A[MAX_WEIGHT+1];
void
weight_distribution2(uint32_t poly, int *A) {
int i;
uint32_t x;
/* zeroize array */
for (i=0; i < MAX_WEIGHT+1; i++)
A[i] = 0;
/* loop over all input sequences */
/* set lfsr to 1 */
x = 1;
#if debug_print
printf("%d:\t%s\n", 0, u32_bit_string(x,8));
#endif
for (i=1; i < MAX_PERIOD; i++) {
if (x & 1)
x = (x >> 1) ^ poly;
else
x = (x >> 1);
#if debug_print
/* print for a sanity check */
printf("%d:\t%s\n", i, u32_bit_string(x,8));
#endif
/* increment weight */
wt += (x & 1);
/* check for return to original value */
if (x == 1)
break;
}
/* set zero */
A[0] = 0;
}
void
weight_distribution(uint32_t poly, int *A) {
int i;
uint32_t x;
/* zeroize array */
for (i=0; i < MAX_WEIGHT+1; i++)
A[i] = 0;
/* set lfsr to 1 */
x = 1;
#if debug_print
printf("%d:\t%s\n", 0, u32_bit_string(x,8));
#endif
for (i=1; i < MAX_PERIOD; i++) {
if (x & 1)
x = (x >> 1) ^ poly;
else
x = (x >> 1);
#if debug_print
/* print for a sanity check */
printf("%d:\t%s\n", i, u32_bit_string(x,8));
#endif
/* compute weight, increment proper element */
A[weight(x)]++;
/* check for return to original value */
if (x == 1)
break;
}
/* set zero */
A[0] = 0;
}
int
main () {
int i,j;
v32_t x;
v32_t p;
/* originally 0xaf */
p.value = 0x9;
printf("polynomial: %s\tperiod: %d\n",
u32_bit_string(p.value,8), period(p.value));
/* compute weight distribution */
weight_distribution(p.value, A);
/* print weight distribution */
for (i=0; i <= 8; i++) {
printf("A[%d]: %d\n", i, A[i]);
}
#if 0
for (i=0; i < 39; i++) {
printf("polynomial: %s\tperiod: %d\n",
u32_bit_string(polynomials[i],8), period(polynomials[i]));
/* compute weight distribution */
weight_distribution(p.value, A);
/* print weight distribution */
for (j=0; j <= 8; j++) {
printf("A[%d]: %d\n", j, A[j]);
}
}
#endif
{
int bits = 8;
uint32_t y;
for (y=0; y < (1 << bits); y++) {
printf("polynomial: %s\tweight: %d\tperiod: %d\n",
u32_bit_string(y,bits), weight(y), period(y));
/* compute weight distribution */
weight_distribution(y, A);
/* print weight distribution */
for (j=0; j <= 8; j++) {
printf("A[%d]: %d\n", j, A[j]);
}
}
}
return 0;
}
|