[前][次][番号順一覧][スレッド一覧]

ruby-changes:35022

From: glass <ko1@a...>
Date: Fri, 8 Aug 2014 20:01:00 +0900 (JST)
Subject: [ruby-changes:35022] glass:r47104 (trunk): * lib/securerandom.rb: use OpenSSL::BN for performance improvement.

glass	2014-08-08 20:00:47 +0900 (Fri, 08 Aug 2014)

  New Revision: 47104

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=47104

  Log:
    * lib/securerandom.rb: use OpenSSL::BN for performance improvement.
    
    * benchmark/bm_securerandom.rb: benchmark script.

  Added files:
    trunk/benchmark/bm_securerandom.rb
  Modified files:
    trunk/ChangeLog
    trunk/lib/securerandom.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 47103)
+++ ChangeLog	(revision 47104)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Aug  8 01:53:37 2014  Masaki Matsushita <glass.saga@g...>
+
+	* lib/securerandom.rb: use OpenSSL::BN for performance improvement.
+
+	* benchmark/bm_securerandom.rb: benchmark script.
+
 Fri Aug  8 17:19:57 2014  SHIBATA Hiroshi  <shibata.hiroshi@g...>
 
 	* lib/open-uri.rb: remove needless condition for old ruby version.
Index: lib/securerandom.rb
===================================================================
--- lib/securerandom.rb	(revision 47103)
+++ lib/securerandom.rb	(revision 47104)
@@ -214,21 +214,29 @@ module SecureRandom https://github.com/ruby/ruby/blob/trunk/lib/securerandom.rb#L214
   #
   def self.random_number(n=0)
     if 0 < n
-      hex = n.to_s(16)
-      hex = '0' + hex if (hex.length & 1) == 1
-      bin = [hex].pack("H*")
-      mask = bin[0].ord
-      mask |= mask >> 1
-      mask |= mask >> 2
-      mask |= mask >> 4
-      begin
-        rnd = SecureRandom.random_bytes(bin.length)
-        rnd[0] = (rnd[0].ord & mask).chr
-      end until rnd < bin
-      rnd.unpack("H*")[0].hex
+      if defined? OpenSSL::BN
+        OpenSSL::BN.rand_range(n).to_i
+      else
+        hex = n.to_s(16)
+        hex = '0' + hex if (hex.length & 1) == 1
+        bin = [hex].pack("H*")
+        mask = bin[0].ord
+        mask |= mask >> 1
+        mask |= mask >> 2
+        mask |= mask >> 4
+        begin
+          rnd = SecureRandom.random_bytes(bin.length)
+          rnd[0] = (rnd[0].ord & mask).chr
+        end until rnd < bin
+        rnd.unpack("H*")[0].hex
+      end
     else
       # assumption: Float::MANT_DIG <= 64
-      i64 = SecureRandom.random_bytes(8).unpack("Q")[0]
+      if defined? OpenSSL::BN
+        i64 = OpenSSL::BN.rand(64, -1).to_i
+      else
+        i64 = SecureRandom.random_bytes(8).unpack("Q")[0]
+      end
       Math.ldexp(i64 >> (64-Float::MANT_DIG), -Float::MANT_DIG)
     end
   end
Index: benchmark/bm_securerandom.rb
===================================================================
--- benchmark/bm_securerandom.rb	(revision 0)
+++ benchmark/bm_securerandom.rb	(revision 47104)
@@ -0,0 +1,5 @@ https://github.com/ruby/ruby/blob/trunk/benchmark/bm_securerandom.rb#L1
+require "securerandom"
+
+20_0000.times do
+  SecureRandom.random_number(100)
+end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]