Skip to content

Instantly share code, notes, and snippets.

@seabre
Created April 15, 2012 03:52
Show Gist options
  • Save seabre/2389826 to your computer and use it in GitHub Desktop.
Save seabre/2389826 to your computer and use it in GitHub Desktop.
Blum Blum Shub Pseudo Random Number Generator
# An implementation of assert.
AssertException = (message) ->
@message = message
assert = (exp, message) ->
throw new AssertException(message) unless exp
AssertException::toString = ->
"AssertException: " + @message
# Implementation of Blum Blum Shub
# p - Large Prime 1. Should be congruent to 3 mod 4.
# q - Large Prime 2. Should be to congruent to 3 mod 4.
# s - The seed. Should be a co-prime of the product of p and q.
blum_blum_shub = (p,q,s,i) ->
# Primes p and q must be congruent to 3 mod 4
assert((p % 4) == 3, "Error: Prime P is not congruent to 3 mod 4")
assert((q % 4) == 3, "Error: Prime Q is not congruent to 3 mod 4")
m = p * q
x = s
for step in [0..i]
x = (x * x) % m
x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment