| 1 | /* Copyright (c) 2010 by Michael J. Roberts. All Rights Reserved. */ |
| 2 | /* |
| 3 | Name |
| 4 | vmisaac.h - TADS 3 VM ISAAC random number generator implementation |
| 5 | Function |
| 6 | |
| 7 | Notes |
| 8 | |
| 9 | Modified |
| 10 | 04/11/10 MJRoberts - Creation |
| 11 | */ |
| 12 | |
| 13 | #ifndef VMISAAC_H |
| 14 | #define VMISAAC_H |
| 15 | |
| 16 | #include "t3std.h" |
| 17 | |
| 18 | /* data block size */ |
| 19 | #define ISAAC_RANDSIZL (8) |
| 20 | #define ISAAC_RANDSIZ (1<<ISAAC_RANDSIZL) |
| 21 | |
| 22 | /* ISAAC RNG context */ |
| 23 | struct isaacctx |
| 24 | { |
| 25 | /* RNG context */ |
| 26 | uint32_t cnt; |
| 27 | uint32_t rsl[ISAAC_RANDSIZ]; |
| 28 | uint32_t mem[ISAAC_RANDSIZ]; |
| 29 | uint32_t a; |
| 30 | uint32_t b; |
| 31 | uint32_t c; |
| 32 | }; |
| 33 | |
| 34 | #define isaac_rand(r) \ |
| 35 | ((r)->cnt-- == 0 ? \ |
| 36 | (isaac_gen_group(r), (r)->cnt=ISAAC_RANDSIZ-1, (r)->rsl[(r)->cnt]) : \ |
| 37 | (r)->rsl[(r)->cnt]) |
| 38 | |
| 39 | /* |
| 40 | * Initialize. If flag is true, then use the contents of ctx->rsl[] to |
| 41 | * initialize ctx->mm[]; otherwise, we'll use a fixed starting |
| 42 | * configuration. |
| 43 | */ |
| 44 | void isaac_init(isaacctx *ctx, int flag); |
| 45 | |
| 46 | /* |
| 47 | * Get/set the internal state. This allows saving and restoring the |
| 48 | * internal state of the RNG. 'get' returns the size of the byte buffer |
| 49 | * required; call with buf==0 to determine the size needed. |
| 50 | * |
| 51 | * 'get' and 'set' both expect the caller to know the correct size from a |
| 52 | * size-query call to 'get'. The size passed to 'set' must always exactly |
| 53 | * match the size returned from 'get', since anything else could corrupt |
| 54 | * the internal state. |
| 55 | */ |
| 56 | size_t isaac_get_state(isaacctx *ctx, char *buf); |
| 57 | void isaac_set_state(isaacctx *ctx, const char *buf); |
| 58 | |
| 59 | /* generate a group of random values */ |
| 60 | void isaac_gen_group(isaacctx *ctx); |
| 61 | |
| 62 | #endif /* VMISAAC_H */ |