Skip to content

Instantly share code, notes, and snippets.

@azet
Last active December 25, 2015 09:49
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 azet/6956568 to your computer and use it in GitHub Desktop.
Save azet/6956568 to your computer and use it in GitHub Desktop.
Fuck Rubys 'Prime' Class!
#!/usr/bin/env ruby
#
# W = Number of Bits (e.g. 1024, 2048,..)
# Generate W/2-bit primes p and q such that p != q and |pq| = W
#
require 'securerandom'
# constant W bits:
W = 1024
# subtract one integer to avoid confusion with bit-shifting later on
W - 1
def random_number
return SecureRandom.random_number(1 << W/2)
end
def is_prime?(number)
# Rubys Prime is incredibly slow.
return true unless `openssl prime #{number}`.include? "not"
end
def honest_prime
loop do
num = random_number
return num if num >= 2**(W/2 - 1) + 1 and is_prime?(num)
end
end
begin
p = honest_prime
q = honest_prime
end until p != q and (p * q).to_s(2).size == W
p, q = q, p if p > q
puts p, q
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment