f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 1999-2001, 2004, 2005, 2007, 2015, 2016 Internet Systems Consortium, Inc. ("ISC")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff */
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff
70e5a7403f0e0a3bd292b8287c5fed5772c15270Automatic Updater/* $Id: lfsr_test.c,v 1.16 2007/06/19 23:46:59 tbox Exp $ */
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*! \file */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <config.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff#include <stdio.h>
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff#include <isc/lfsr.h>
3759f10fc543747668b1ca4b4671f35b0dea8445Francis Dupont#include <isc/print.h>
b24e559dee9f6eef29d78c8686e3e99e4c570086Michael Graff#include <isc/util.h>
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graffisc_uint32_t state[1024 * 64];
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graffint
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencemain(int argc, char **argv) {
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff isc_lfsr_t lfsr1, lfsr2;
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff int i;
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff isc_uint32_t temp;
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff UNUSED(argc);
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff UNUSED(argv);
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff /*
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff * Verify that returned values are reproducable.
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff */
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff isc_lfsr_init(&lfsr1, 0, 32, 0x80000057U, 0, NULL, NULL);
f1b68725503ff3e46001eee5a1751e29a43a09d1Andreas Gustafsson for (i = 0; i < 32; i++) {
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff isc_lfsr_generate(&lfsr1, &state[i], 4);
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff printf("lfsr1: state[%2d] = %08x\n", i, state[i]);
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff }
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff isc_lfsr_init(&lfsr1, 0, 32, 0x80000057U, 0, NULL, NULL);
f1b68725503ff3e46001eee5a1751e29a43a09d1Andreas Gustafsson for (i = 0; i < 32; i++) {
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff isc_lfsr_generate(&lfsr1, &temp, 4);
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff if (state[i] != temp)
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence printf("lfsr1: state[%2d] = %08x, "
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence "but new state is %08x\n",
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff i, state[i], temp);
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff }
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff /*
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff * Now do the same with skipping.
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff */
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff isc_lfsr_init(&lfsr1, 0, 32, 0x80000057U, 0, NULL, NULL);
f1b68725503ff3e46001eee5a1751e29a43a09d1Andreas Gustafsson for (i = 0; i < 32; i++) {
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff isc_lfsr_generate(&lfsr1, &state[i], 4);
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff isc_lfsr_skip(&lfsr1, 32);
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff printf("lfsr1: state[%2d] = %08x\n", i, state[i]);
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff }
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff isc_lfsr_init(&lfsr1, 0, 32, 0x80000057U, 0, NULL, NULL);
f1b68725503ff3e46001eee5a1751e29a43a09d1Andreas Gustafsson for (i = 0; i < 32; i++) {
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff isc_lfsr_generate(&lfsr1, &temp, 4);
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff isc_lfsr_skip(&lfsr1, 32);
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff if (state[i] != temp)
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence printf("lfsr1: state[%2d] = %08x, "
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence "but new state is %08x\n",
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff i, state[i], temp);
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff }
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff /*
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff * Try to find the period of the LFSR.
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff *
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff * x^16 + x^5 + x^3 + x^2 + 1
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff */
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff isc_lfsr_init(&lfsr2, 0, 16, 0x00008016U, 0, NULL, NULL);
f1b68725503ff3e46001eee5a1751e29a43a09d1Andreas Gustafsson for (i = 0; i < 32; i++) {
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff isc_lfsr_generate(&lfsr2, &state[i], 4);
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff printf("lfsr2: state[%2d] = %08x\n", i, state[i]);
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff }
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff isc_lfsr_init(&lfsr2, 0, 16, 0x00008016U, 0, NULL, NULL);
f1b68725503ff3e46001eee5a1751e29a43a09d1Andreas Gustafsson for (i = 0; i < 32; i++) {
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff isc_lfsr_generate(&lfsr2, &temp, 4);
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff if (state[i] != temp)
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence printf("lfsr2: state[%2d] = %08x, "
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence "but new state is %08x\n",
b8ef99c3a7be2835b2395faff2d46e46f5c4adb3Michael Graff i, state[i], temp);
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff }
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff return (0);
f4ce616539dd81322fa4db9676f42ef2e0a19031Michael Graff}