Last active
December 25, 2015 09:49
-
-
Save azet/6956568 to your computer and use it in GitHub Desktop.
Fuck Rubys 'Prime' Class!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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