Inaccuracy of ANSI clock() as ?-RNG
From: Juuso Hukkanen (juuso929_at_tele3d.net)
Date: 03/25/05
- Next message: Unruh: "Re: SHA-1 collisions"
- Previous message: Michael: "Re: Help decrypting"
- Next in thread: Dave Turner: "Re: Inaccuracy of ANSI clock() as ?-RNG"
- Reply: Dave Turner: "Re: Inaccuracy of ANSI clock() as ?-RNG"
- Maybe reply: Juuso Hukkanen: "Re: Inaccuracy of ANSI clock() as ?-RNG"
- Maybe reply: Juuso Hukkanen: "Re: Inaccuracy of ANSI clock() as ?-RNG"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: Fri, 25 Mar 2005 21:08:43 +0200
BACKGROUND:
I tried to build a portable ANSI C stopwatch with sub-/millisecond
accuracy, using the clock-function. Well - it can be build, but it is
not that accurate.
SUMMARY:
Inaccuracy is reflected as a varying number of iterations which fit
within one clock-tick. Possible this unwanted variance can be utilized
in some applications which need potentially undeterministic seed.
QUESTIONS:
1) How good or bad are these pseudo-randoms-values
a) numbers made by the code (without taking a block and hashing
it)
b) with recording a block and hashing it(->hash is xRNG seed
values)
c) are these values deterministic as a block or alone?
2) Does someone have a computer in which this does not work or at all
or which does not produce high randomness with this?
3) What causes the variance - computers seem not be doing anything -
any quesses anyone?
MY ERROROUS QUESSES:
1) User should always check the quality of produced pseudo-randoms by
some statistical tests
2) Code produces numbers at very slow pace, only practical use is
expected to be (at best) in production of seed values for other faster
algoritms
3) User should strongly concider taking a whole block of produced
numbers as an array and then hashing it using some secure hash
algorithm (e.g. WHIRLPOOL, Tiger).
CODE IS/DOES:
Demo program produces a block of a random-like values (0-255). Code
quarantees minimum quantity of iterations which are required for each
pre-seed - value i.e. it takes into account potential differences in
CLOCKS_PER_SEC - value. Code should work on platforms suporting ANSI C
standard library. Tested in win95/2000/XP/UNIX.
CODE DOES NOT:
Code does not try to control the quality of produced
pre-seed/pseudo-random values.
If you find any use for this thing, please concider calling it
"Juuso's inaccurate clock" - algorithm. Next week I will try to desing
& post some kind of ciphon or sipher, eh you know what I mean... those
grypto things. Perhaps some of you might then want to break the
design. I'll try make it simple, so shape your teeth ready JLC, Tom &
co :)
Juuso
(remove non-alphabetic tokens from mail-address)
<OT>
I am preparing to launch (on 2005/06/15) an open source project for
building a C based programming language and after that I need LOTS of
helping hands and minds. Language will include C (except the typical
overflow functions) and most of the modern stuff missing from C -
including encryption. If you are interested in being reminded/
participating drop me a line.
</OT>
/*
* Public domain by Juuso Hukkanen (2005/03/25)
*/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h> /* for abs() */
#define NUM_SEED 200
/* #define PRINT_TO_FILES */
#define MIN_CYCLES (512*256)-127 /* minimum iterations per
each-pre-seed value*/
int main(void)
{
int g=0;
long ini_tick=0;
long mid_tick=0;
long end_tick=0;
int timetable[NUM_SEED];
long subcycles=0;
FILE *seed_file; /* 8-bit (truncated) pre-seed values
*/
FILE *diffs; /* 8-bit (truncated) differences between individual
values */
clock();
ini_tick = clock();
/* first we wait for the beginning of a fresh clock-tick */
do
{
mid_tick= clock();
} while( ini_tick==mid_tick );
/* Now we have a fresh clock-tick, lets se how many iterations it
lasts */
/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
/* BEGIN a tight loop where all the data will be collected */
for (g=0; g < NUM_SEED; g++)
{
while (subcycles < MIN_CYCLES) /*(quantity control)enough
iterations?*/
{
do /* loop until tick changer */
{
end_tick = clock();
subcycles++;
} while(mid_tick==end_tick);
mid_tick = end_tick;
} /* is there now enough
iterations loop*/
timetable[g] = subcycles;
subcycles =0;
mid_tick= end_tick;
}
/* END OF a tight loop, where all the data was collected */
/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
#ifdef PRINT_TO_FILES
seed_file = fopen("seed_file.txt","wb");
if(!seed_file)
exit(EXIT_FAILURE);
diffs = fopen("diffs.txt","wb");
if(!diffs)
exit(EXIT_FAILURE);
#endif /* PRINT_TO_FILES */
printf("\nuntruncated pseudo-randoms values:\n");
printf("Iterations \tdiff-between\tdiff as UCHAR\n");
for (g=0; g < NUM_SEED; g++)
{
printf("%d",timetable[g]);
if(g+1 == NUM_SEED)
{
printf("\t\tnot available - Press enter");
break;
}
printf("\t\t%d",timetable[g]-timetable[g+1]);
printf("\t\t%d\n ",abs((timetable[g]-timetable[g+1])%256));
#ifdef PRINT_TO_FILES
fprintf(diffs,"%c",abs((timetable[g]-timetable[g+1])%256));
#endif /* PRINT_TO_FILES */
}
getchar();
printf("\npseudo-randoms truncated to 8 bit values:\n");
for (g=1; g <= NUM_SEED; g++)
{
printf("%d\t",timetable[g-1]%256);
if(g %8 == 0)
printf("\n");
#ifdef PRINT_TO_FILES
fprintf(seed_file,"%c",timetable[g-1]%256);
#endif /* PRINT_TO_FILES */
}
getchar();
#ifdef PRINT_TO_FILES
fclose(seed_file);
fclose(diffs);
#endif /* PRINT_TO_FILES */
return(0);
}
- Next message: Unruh: "Re: SHA-1 collisions"
- Previous message: Michael: "Re: Help decrypting"
- Next in thread: Dave Turner: "Re: Inaccuracy of ANSI clock() as ?-RNG"
- Reply: Dave Turner: "Re: Inaccuracy of ANSI clock() as ?-RNG"
- Maybe reply: Juuso Hukkanen: "Re: Inaccuracy of ANSI clock() as ?-RNG"
- Maybe reply: Juuso Hukkanen: "Re: Inaccuracy of ANSI clock() as ?-RNG"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|