ref: d5c406d1a36d770c3d492b7412a149072d64412c
parent: e6758735f0b37beebb6b0a526680acf00f14bdd5
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Jan 17 17:09:54 EST 2015
Add randbytes() to libstd. TODO: actually fix the RNG to be a CSPRNG. This will mean that it will be harder to break by default.
--- a/libstd/Makefile
+++ b/libstd/Makefile
@@ -43,6 +43,7 @@
option.myr \
optparse.myr \
pathjoin.myr \
+ putint.myr \
rand.myr \
resolve.myr \
result.myr \
--- a/libstd/bldfile
+++ b/libstd/bldfile
@@ -48,6 +48,7 @@
option.myr
optparse.myr
pathjoin.myr
+ putint.myr
rand.myr
resolve.myr
result.myr
--- a/libstd/rand.myr
+++ b/libstd/rand.myr
@@ -52,6 +52,7 @@
const delrng : (rng : rng# -> void)
generic rand : (rng : rng#, lo : @a::(numeric,integral), hi : @a::(numeric,integral) -> @a::(numeric,integral))
generic randN : (rng : rng# -> @a::(numeric,integral))
+ const randbytes : (rng : rng#, buf : byte[:] -> size)
const rand32 : (rng : rng# -> uint32)
;;
@@ -146,6 +147,25 @@
-> x ^ (x >> 18)
}
+const randbytes = {rng, buf
+ var i, n, r
+
+ n = 0
+ for i = 0; i < buf.len/4; i++
+ r = rand32(rng)
+ buf[n++] = (r >> 0 & 0xff) castto(byte)
+ buf[n++] = (r >> 8 & 0xff) castto(byte)
+ buf[n++] = (r >> 16 & 0xff) castto(byte)
+ buf[n++] = (r >> 32 & 0xff) castto(byte)
+ ;;
+ r = rand32(rng)
+ for ; n != buf.len; n++
+ buf[n++] = (r & 0xff) castto(byte)
+ r >>= 8
+ ;;
+ -> n
+
+}
/* updates random number generator state when we tick over. */
const next = {rng