paillier 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/paillier/primes.rb +29 -29
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7abfb860227e0f15e0f9f083884469f7615be645
4
- data.tar.gz: 8c93d15448f9e6caffb6ad9980ba1c958094facf
3
+ metadata.gz: 3d489649541fa6f023075161d2d2772bd0d05539
4
+ data.tar.gz: 8b6138fbebb78bcb2a8e3360b753bb2a43faa4ab
5
5
  SHA512:
6
- metadata.gz: e364613ba383aca3761cf242834d473f8f0704c8723c8072dc8ae9b14f8a0aab9bd61f5c8f530b660446145f0fda94ea81a100d196f7391dd3b167933d6c717e
7
- data.tar.gz: e429ec57015496f0767cdb28360702435efd75ffc6e39d6264b7a4c81ac6e3dcbb3101c7187677b3a4c9afac4011cd8615e8ea73e9b7105d00442fde2f3fc15f
6
+ metadata.gz: 823b4a093380c87f0403589bf9a982fdf59628ced3df9fdf0bc3c0fbcee0338113d049e6b3423468df3b70570a2540cf95c2f12e5bed958ac07552ea29cb63dd
7
+ data.tar.gz: 0b931043d6fefe9bae67ef4765f59098ec8b102244af1fc62a5502c32b8501779bc9fa4bb1879f07825473f85f315a6c9512f9386c537d548c808a55affdb41a
@@ -10,33 +10,37 @@ module Paillier
10
10
  return int.to_s(2).length
11
11
  end
12
12
 
13
- def self.defaultK(bits)
14
- double = bits * 2
15
- return (40 > double) ? 40 : double
16
- end
17
-
18
- # This is based on the Wikipedia article on the Fermat primality test
19
- # returns true if probably prime, false if definitely composite
20
- def self.probabilisticPrimeTest(target, k)
21
- for _ in (1 .. k)
22
- a = rand(2 .. (target-2))
23
- # We want to run "x = (a ** target-1) % target", but the values
24
- # are huge. Instead we call out to openssl and do it with mod_exp
25
- mod = a.to_bn.mod_exp(target-1, target)
26
- if( mod != 1 )
27
- return false # Def composite
28
- end
29
- end
30
- return true # probs prime
31
- end
13
+ # This is an implementation of the Rabin-Miller primality test.
14
+ # Previous versions used Little Fermat, but that is not effective
15
+ # in all cases; specifically, it can be thwarted by Carmichael
16
+ # numbers. We use 50 rounds as the default, in order to get a certainty
17
+ # of 2^-100 that we have found a prime. This implementation is adapted
18
+ # from https://rosettacode.org/wiki/Miller-Rabin_primality_test#Ruby
19
+ def self.probabilisticPrimeTest(target, k=50)
20
+ d = target-1
21
+ s = 0
22
+ while d % 2 == 0
23
+ d /= 2
24
+ s += 1
25
+ end
26
+ k.times do
27
+ a = 2 + rand(target-4)
28
+ x = a.to_bn.mod_exp(d, target)
29
+ next if x == 1 || x == target-1
30
+ for r in (1..s - 1)
31
+ x = x.to_bn.mod_exp(2, target)
32
+ return false if x == 1
33
+ break if x == target - 1
34
+ end
35
+ return false if x != target-1
36
+ end
37
+ return true # probs prime
38
+ end
32
39
 
33
- def self.isProbablyPrime?(possible, k=nil)
40
+ def self.isProbablyPrime?(possible, k=50)
34
41
  if( possible == 1 )
35
42
  return true
36
43
  end
37
- if( k.nil? )
38
- k = defaultK(bitLength(possible))
39
- end
40
44
  for i in SmallPrimes
41
45
  if( possible == i )
42
46
  return true
@@ -59,14 +63,10 @@ module Paillier
59
63
  end
60
64
 
61
65
  # Get a random prime of appropriate length
62
- def self.generatePrime(bits, k=nil)
66
+ def self.generatePrime(bits, k=50)
63
67
  if( bits < 8 )
64
68
  raise "Bits less than eight!"
65
69
  end
66
- if( k == nil )
67
- k = defaultK(bits)
68
- end
69
-
70
70
  while( true )
71
71
  lowerBound = (2 ** (bits-1) + 1)
72
72
  size = ((2 ** bits) - lowerBound)
@@ -82,7 +82,7 @@ module Paillier
82
82
  raise "Bits less than eight!"
83
83
  end
84
84
 
85
- # If we find a number not coprome to n then finding `p` and `q` is trivial.
85
+ # If we find a number not coprime to n then finding `p` and `q` is trivial.
86
86
  # This will almost never happen for keys of reasonable size, so if
87
87
  # `coprime_to` is big enough we won't bother running the expensive test.
88
88
  no_test_needed = false
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paillier
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daylighting Society
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-13 00:00:00.000000000 Z
11
+ date: 2018-11-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: An implementation of Paillier homomorphic addition public key system
14
14
  email: paillier@daylightingsociety.org