Skip to content

Instantly share code, notes, and snippets.

@wasamasa
Last active November 20, 2015 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wasamasa/d0da246ff09712944d79 to your computer and use it in GitHub Desktop.
Save wasamasa/d0da246ff09712944d79 to your computer and use it in GitHub Desktop.
PoCorGTFO PRNG
;; Taken from PoCorGTFO 0x01
;; Original:
;; function millis() {return Date.now();}
;; function flip_coin() {n=0; then = millis()+1; while(millis()<=then) {n=!n;} return n;}
;; function get_fair_bit() {while(1) {a=flip_coin(); if(a!=flip_coin()) {return(a);}}}
;; function get_random_byte() {n=0; bits=8; while(bits--){n<<1; n|=get_fair_bit();} return n;}
(defun prng--milliseconds ()
(floor (* (float-time) 1000)))
(defun prng--coinflip ()
(let ((then (1+ (prng--milliseconds)))
value)
(while (<= (prng--milliseconds) then)
(setq value (not value)))
value))
(defun prng-bit ()
(catch 'done
(while t
(let ((value (prng--coinflip)))
(when (not (eq value (prng--coinflip)))
(throw 'done value))))))
(defun prng-bits (n)
(let ((value 0)
(bits n))
(while (> bits 0)
(setq value (logior (ash value 1) (if (prng-bit) 1 0))
bits (1- bits)))
value))
(defun prng-byte ()
(prng-bits 8))
(defvar prng-uint-size
(logb most-positive-fixnum))
(defvar prng-int-size
(1+ prng-uint-size))
(defun prng-uint ()
(prng-bits prng-uint-size))
(defun prng-int ()
(prng-bits prng-int-size))
;; NOTE Extremely slow, takes ~50s
(benchmark 100 '(prng-int))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment