Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seeding routine and second call to isaac() #1

Open
BonzaiThePenguin opened this issue May 25, 2014 · 4 comments
Open

Seeding routine and second call to isaac() #1

BonzaiThePenguin opened this issue May 25, 2014 · 4 comments

Comments

@BonzaiThePenguin
Copy link

@BonzaiThePenguin BonzaiThePenguin commented May 25, 2014

The first step in the Use section says to do this:

pip install pyisaac

I checked Google to figure out what pip is, and successfully installed it, but it of course just says it couldn't find any packages named pyisaac:

Could not find any downloads that satisfy the requirement pyisaac
Cleaning up...
No distributions at all found for pyisaac

It didn't seem to matter which directory I cd'd into. Are these directions accurate?

@guilload
Copy link
Owner

@guilload guilload commented May 26, 2014

Hi Mike,

I haven't uploaded the package to PyPI (the Python Package Index) yet. I'm waiting for more feedback and reviews to do so. Can you take a look at the seed function in pyisaac.c and check If I have properly initialised the generator? Do I need to add an additional call to the isaac function, as suggested by your comments on reddit?

If you really want to install the package, clone the repository, cd into the pyisaac directory and run python setup.py build_ext --inplace. Then, in the very same directory, you can launch python and import pyisaac.

Thank you for your time!

@BonzaiThePenguin
Copy link
Author

@BonzaiThePenguin BonzaiThePenguin commented May 26, 2014

The recommended way of seeding ISAAC is to write the seed value over and over until it fills the internal arrays, rather than initializing with zeroes and writing the seed value once; and calling isaac() twice is a way to work around a potential security flaw someone discovered with some of the starting states. Basically if you don't call it twice there are some seed values that are within the realm of possibility of brute-forcing to recover the seed, which would break the cryptographic security.

@BonzaiThePenguin
Copy link
Author

@BonzaiThePenguin BonzaiThePenguin commented May 26, 2014

By the way, here's an interface for the RNG you may find useful:

#define return_random(type) \
   me->randcnt -= sizeof(type); \
   if (me->randcnt < 0) { isaac(me); me->randcnt = 256 * sizeof(me->randrsl[0]) - sizeof(type); } \
   return *((type *)((uint8 *)me->randrsl)[me->randcnt]); \

int8_t random_int8(randctx *me) { return_random(int8_t); }
int16_t random_int16(randctx *me) { return_random(int16_t); }
int32_t random_int32(randctx *me) { return_random(int32_t); }
int64_t random_int64(randctx *me) { return_random(int64_t); }
uint8_t random_uint8(randctx *me) { return_random(uint8_t); }
uint16_t random_uint16(randctx *me) { return_random(uint16_t); }
uint32_t random_uint32(randctx *me) { return_random(uint32_t); }
uint64_t random_uint64(randctx *me) { return_random(uint64_t); }

bool random_bool(randctx *me) { return (random_uint8(me) > 0x7F); }
float random_float(randctx *me) { return (random_uint32(me) % 0xFFFFFF80)/(float)0x100000000; } // 0xFFFFFF7F is the largest value that returns < 1.0 for this division
double random_double(randctx *me) { return random_uint64(me)/(double)1.84467440737096e19; } // even 0xFFFFFFFFFFFFFFFF returns a value < 1.0

(randcnt is initialized to 256 * sizeof(me->rsl[0]) at the end of set_seed, after calling isaac() once or twice)

It isn't perfect (if there are three bytes of random bits remaining in the randrsl array and you request a uint32, it calls isaac and reads four bytes from the new set of data, rather than using those three bytes and only needing one more), but the logic for the random floats and doubles was tested carefully and that's usually hard to get right.

@guilload
Copy link
Owner

@guilload guilload commented May 26, 2014

Great stuff! Thank you. I will commit within the week.

@guilload guilload changed the title Not entirely sure what I'm supposed to do? Seeding routine and second call to isaac() May 29, 2014
guilload added a commit that referenced this issue May 29, 2014
The seed value is copied over and over until it fills the internal array.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.