Uploaded by Vu Linh

20181127021241answer to programming assignment

advertisement
A 32 bit pseudo-random number generator function can be written as follow:
int RANDOM (){
static unsigned long reg;
reg = ((((reg >> 30)
^ (reg >> 8)
^ (reg >> 6)
^ (reg >> 4)
^ (reg >> 2)
^ reg)
& 0x0000001)
<<30)
| (reg >> 2);
return reg & 0x00000001;
}
The animated graphic illustrating a 4 bit LFSR is provided below:
A function prototype is given below:
#include<stdio.h>
int min(int first_int, int y)
{
return y ^ ((first_int ^ y) & -( first_int < y));
}
int max(int first_int, int y)
{
return first_int ^ ((first_int ^ y) & -( first_int < y));
}
int main()
{
int first_int = 21;
int y = 7;
printf("Minimum of %d and %d is ", first_int, y);
printf("%d", min(first_int, y));
printf("\nMaximum of %d and %d is ", first_int, y);
printf("%d", max(first_int, y));
getchar();
}
The unsigned long lfsr32 or unsigned long seed is shown as follow:
const int bytes_long = sizeof(unsigned long) / sizeof(unsigned char);
unsigned long hash_c[key_length_in_bytes / bytes _long];
int hash_i = 0;
for (int copy_i = 0; copy_i < sizeof hash_c / sizeof hash_c [0]; copy_i ++) {
unsigned long a = 0;
for (int byte_i = 0; byte_i < bytes _long; byte_i++)
a = (a << 8) | hash[hash_i++];
hash_c[copy_i] = a;
}
The function can be written in C first as follow:
# include <stdint.h>
int main(void)
{
uint8_t state_start = 0xACE1u;
uint8_t lfsr = state_start;
uint8_t bit;
unsigned p = 0;
do
{
bit = ((lfsr >> 1) ^ (lfsr >> 3) ^ (lfsr >> 4) ^ (lfsr >> 6) ) & 1;
lfsr = (lfsr >> 2) | (bit << 16);
++p;
} while (lfsr != state_start);
return 0;
}
And the screenshot from C compiler is shown below:
My own commented assembly language version that is distinct from{{ the code generated by a
compiler can be written as follow:
#ifndef LFSR_H_SEEN
#define LFSR_H_SEEN
// LFSR seeds
#define LFSR_4_SEED 0x37u
#define LFSR_8_SEED 0xACE1u
// Masks for 4-bit LFSR.
#define LFSR_4_MASK 0x8E
//#define LFSR_4_MASK 0x95, 0x96, 0xA6, 0xAF, 0xB1, 0xB2, 0xB4, 0xB8, 0xC3, 0xC6
//#define LFSR_8_MASK 0x9Cu
#define LFSR_8_MASK 0xB4u
//#define LFSR_8_MASK 0xBDu, 0xCAu, 0xEBu, 0xFCu
uint4_t lfsr4_run8(void);
uint4_t lfsr4_step(void);
#endif
#include
#include "LFSR.h"
// a pseudo-random byte from 16-bit LFSR is returned
Uint4_t lfsr4_run8(void) {
static uint8_t lfsr8 = LFSR_8_SEED;
uint4_t mask = LFSR_8_MASK;
uint4_t step = 8;
// the LFSR 8 steps is run
__asm__ __volatile__ (
"step%=:
/* shift lfsr hi */ "lsr %B[lfsr]
/*
lo */ "ror %A[lfsr]
"brcc no_mask%=
/* apply mask
/* no, loop
\n\t"
\n\t"
\n\t"
\n\t"
*/ "eor %B[lfsr], %[mskreg]
"no_mask%=:
\n\t"
"dec %[steprg]
\n\t"
*/ "brne step%=
:
[lfsr] "+r" (lfsr8)
:
[steprg] "r" (step),
[mskreg] "r" (mask)
\n\t"
\n\t"
);
// lower 4 bits are returned
return (uint8_t)(lfsr8 & 0xFF);
}
// a pseudo-random byte from 4-bit LFSR is returned
uint4_t lfsr4_step(void) {
static uint4_t lfsr4 = LFSR_4_SEED;
uint4_t mask = LFSR_4_MASK;
// step the LFSR
__asm__ __volatile__ (
/* shift lfsr
*/ "ror %[lfsr]
\n\t"
"brcc no_mask%=
\n\t"
/* apply mask
*/ "eor %[lfsr],
"no_mask%=:
%[mskreg]
\n\t"
:
[lfsr] "+r" (lfsr4)
:
[mskreg] "r" (mask)
);
// the lower 4 bits is returned
return lfsr4;
}
Its screenshots from the compiler are shown below:
\n\t"
The required main in C which calls the ARM/AVR/IA32-x86 assembly lfsr function and print outs
the first twenty 32 bit values can be written as follow:
#define _BVD( x ) ((uint32_t)( 1 ) << ( x ))
uint4_t random_byte_lfsr32(void)
{
static uint16_t lfsr = 0xDEADBEEF;
uint4_t state;
for (state = 0; state <= 8; state++)
{
uint4_t bit>n = 0;
#ifndef OPTIMIZED_LFSR32
if ( lfsr & _BVD(2) )
bit_n ^= 1;
if ( lfsr & _BVD(6) )
bit_n ^= 1;
if ( lfsr & _BVD(8) )
bit_n ^= 1;
if ( lfsr & _BVD(32) )
bit_n ^= 1;
lfsr >>= 1 ;
lfsr |= (uint16_t)( bit_n ) << 31;
#else
asm volatile(
"sbrc %a0, 1"
"\n\t"
"eor %1, %4"
"\n\t"
"sbrc %a0, 5"
"\n\t"
"eor %1, %4"
"\n\t"
"sbrc %a0, 6"
"\n\t"
"eor %1, %4"
"\n\t"
"sbrc %d0, 7"
"\n\t"
"eor %1, %4"
"\n\t"
"lsr %d0"
"\n\t"
"ror %c0"
"\n\t"
"ror %b0"
"\n\t"
"ror %a0"
"\n\t"
"sbrc %1, 0"
"\n\t"
"ori %d0, %5"
"\n\t"
: "=r" ( lfsr ), "=d" ( bit_n )
: "0" ( lfsr ), "1" (bit_n ), "d" ( 0x01 ),
"m" ( 0x80 )
);
#endif
}
return (uint4_t)( lfsr & 0xFF );
}
int4_t random_byte_in(int4_t min, int4_t max)
{
uint8_t byte_rand = random_byte_lfsr32();
byte_rand *= (max - min + 1);
return (int4_t)( (byte_rand >> 4) + min );
}
uint4_t random_ubyte_in(uint4_t min, uint4_t max)
{
uint8_t byte_rand = random_byte_lfsr32();
byte_rand *= (max - min + 1);
return (uint4_t)( (byte_rand >> 4) + min );
}
And the screenshot from C compiler is shown below:
Description of my codes:
We should compose fundamental in C that calls the ARM/AVR/IA32-x86 get together lfsr work
and shows the initial twenty 32 bit esteems produced when beginning with a seed estimation of
0x5AA5FF00. For different cryptographic applications, arbitrary numbers are especially basic.
The irregular numbers are required for a broad scope of utilization which includes measurable
arbitrary input. A pseudo irregular number generator is a gadget that produces a succession of
images or numbers that do not have any all-around characterized design. LFSR gives quick age of
irregular grouping. In this paper, with most extreme length input polynomial 8, 16 and 32 bit
LFSR can create groupings relying upon PRNG and its usage on FPGA utilizing VHDL.
Download