paillier 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/paillier.rb +40 -10
- data/lib/paillier/keys.rb +25 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7abfb860227e0f15e0f9f083884469f7615be645
|
4
|
+
data.tar.gz: 8c93d15448f9e6caffb6ad9980ba1c958094facf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e364613ba383aca3761cf242834d473f8f0704c8723c8072dc8ae9b14f8a0aab9bd61f5c8f530b660446145f0fda94ea81a100d196f7391dd3b167933d6c717e
|
7
|
+
data.tar.gz: e429ec57015496f0767cdb28360702435efd75ffc6e39d6264b7a4c81ac6e3dcbb3101c7187677b3a4c9afac4011cd8615e8ea73e9b7105d00442fde2f3fc15f
|
data/lib/paillier.rb
CHANGED
@@ -11,8 +11,6 @@ require_relative 'paillier/signatures'
|
|
11
11
|
|
12
12
|
module Paillier
|
13
13
|
|
14
|
-
KeySize = 2048
|
15
|
-
|
16
14
|
def self.gcd(u,v) # :nodoc:
|
17
15
|
while(v > 0)
|
18
16
|
u, v = v, u % v
|
@@ -123,8 +121,11 @@ module Paillier
|
|
123
121
|
#
|
124
122
|
# Arguments:
|
125
123
|
# publicKey: (Paillier::PublicKey)
|
126
|
-
# plaintext: (Int)
|
124
|
+
# plaintext: (Int, OpenSSL::BN, String)
|
127
125
|
def self.encrypt(publicKey, plaintext)
|
126
|
+
if( plaintext.is_a?(String) )
|
127
|
+
plaintext = OpenSSL::BN.new(plaintext)
|
128
|
+
end
|
128
129
|
return rEncrypt(publicKey, plaintext)[1]
|
129
130
|
end
|
130
131
|
|
@@ -136,9 +137,15 @@ module Paillier
|
|
136
137
|
#
|
137
138
|
# Arguments:
|
138
139
|
# publicKey: (Paillier::PublicKey)
|
139
|
-
# a: (Int)
|
140
|
-
# b: (Int)
|
140
|
+
# a: (Int, OpenSSL::BN, String)
|
141
|
+
# b: (Int, OpenSSL::BN, String)
|
141
142
|
def self.eAdd(publicKey, a, b)
|
143
|
+
if( a.is_a?(String) )
|
144
|
+
a = OpenSSL::BN.new(a)
|
145
|
+
end
|
146
|
+
if( b.is_a?(String) )
|
147
|
+
b = OpenSSL::BN.new(b)
|
148
|
+
end
|
142
149
|
return a.to_bn.mod_mul(b, publicKey.n_sq)
|
143
150
|
end
|
144
151
|
|
@@ -150,9 +157,15 @@ module Paillier
|
|
150
157
|
#
|
151
158
|
# Arguments:
|
152
159
|
# publicKey: (Paillier::PublicKey)
|
153
|
-
# a: (Int)
|
154
|
-
#
|
160
|
+
# a: (Int, OpenSSL::BN, String)
|
161
|
+
# n: (Int, OpenSSL::BN, String)
|
155
162
|
def self.eAddConst(publicKey, a, n)
|
163
|
+
if( a.is_a?(String) )
|
164
|
+
a = OpenSSL::BN.new(a)
|
165
|
+
end
|
166
|
+
if( n.is_a?(String) )
|
167
|
+
n = OpenSSL::BN.new(n)
|
168
|
+
end
|
156
169
|
return a.to_bn.mod_mul(modPow(publicKey.g, n, publicKey.n_sq), publicKey.n_sq)
|
157
170
|
end
|
158
171
|
|
@@ -164,7 +177,15 @@ module Paillier
|
|
164
177
|
#
|
165
178
|
# Arguments:
|
166
179
|
# publicKey: (Paillier::PublicKey)
|
180
|
+
# a: (Int, OpenSSL::BN, String)
|
181
|
+
# n: (Int, OpenSSL::BN, String)
|
167
182
|
def self.eMulConst(publicKey, a, n)
|
183
|
+
if( a.is_a?(String) )
|
184
|
+
a = OpenSSL::BN.new(a)
|
185
|
+
end
|
186
|
+
if( n.is_a?(String) )
|
187
|
+
n = OpenSSL::BN.new(n)
|
188
|
+
end
|
168
189
|
return modPow(a, n, publicKey.n_sq)
|
169
190
|
end
|
170
191
|
|
@@ -177,8 +198,11 @@ module Paillier
|
|
177
198
|
# Arguments:
|
178
199
|
# privKey: (Paillier::PrivateKey)
|
179
200
|
# pubKey: (Paillier::PublicKey)
|
180
|
-
# ciphertext: (Int)
|
201
|
+
# ciphertext: (Int, OpenSSL::BN, String)
|
181
202
|
def self.decrypt(privKey, pubKey, ciphertext)
|
203
|
+
if( ciphertext.is_a?(String) )
|
204
|
+
ciphertext = OpenSSL::BN.new(ciphertext)
|
205
|
+
end
|
182
206
|
# We want to run: x = ((cipher ** priv.l) % pub.n_sq) - 1
|
183
207
|
# But the numbers are too big, so we'll use openssl
|
184
208
|
x = ciphertext.to_bn.mod_exp(privKey.l, pubKey.n_sq) - 1
|
@@ -195,8 +219,11 @@ module Paillier
|
|
195
219
|
# Arguments:
|
196
220
|
# priv: (Paillier::PrivateKey)
|
197
221
|
# pub: (Paillier::PublicKey)
|
198
|
-
# data: (Int)
|
222
|
+
# data: (Int, OpenSSL::BN, String)
|
199
223
|
def self.sign(priv, pub, data)
|
224
|
+
if( data.is_a?(String) )
|
225
|
+
data = OpenSSL::BN.new(data)
|
226
|
+
end
|
200
227
|
hashData = hash(data)
|
201
228
|
# L(u) = (u-1)/n
|
202
229
|
numerators1 = ((hashData.to_bn.mod_exp(priv.l, pub.n_sq) - 1) / pub.n.to_bn)[0]
|
@@ -225,9 +252,12 @@ module Paillier
|
|
225
252
|
#
|
226
253
|
# Arguments:
|
227
254
|
# pub: (Paillier::PublicKey)
|
228
|
-
# message: (Int)
|
255
|
+
# message: (Int, OpenSSL::BN, String)
|
229
256
|
# sig: (Paillier::Signature)
|
230
257
|
def self.validSignature?(pub, message, sig)
|
258
|
+
if( message.is_a?(String) )
|
259
|
+
message = OpenSSL::BN.new(message)
|
260
|
+
end
|
231
261
|
hash = Digest::SHA256.hexdigest(message.to_s).to_i(16)
|
232
262
|
# We want to run (g ** s1) * (s2 ** n) % (n**2)
|
233
263
|
# But all those numbers are huge, so we approach it in stages
|
data/lib/paillier/keys.rb
CHANGED
@@ -6,6 +6,31 @@ module Paillier
|
|
6
6
|
@l = l
|
7
7
|
@m = m
|
8
8
|
end
|
9
|
+
|
10
|
+
# Serialize a private key to string form
|
11
|
+
#
|
12
|
+
# Example:
|
13
|
+
# >> priv, pub = Paillier.generateKeypair(2048)
|
14
|
+
# >> priv.to_s
|
15
|
+
# => "110107191408889682017277609474037601699496910..."
|
16
|
+
#
|
17
|
+
def to_s
|
18
|
+
return "#{@l},#{@m}"
|
19
|
+
end
|
20
|
+
|
21
|
+
# De-serialize a private key string back into object form
|
22
|
+
#
|
23
|
+
# Example:
|
24
|
+
# >> s = priv.to_s
|
25
|
+
# >> newPriv = Paillier::PrivateKey.from_s(s)
|
26
|
+
# => #<Paillier::PrivateKey>
|
27
|
+
#
|
28
|
+
# Arguments:
|
29
|
+
# string (String)
|
30
|
+
def PrivateKey.from_s(string)
|
31
|
+
l,m = string.split(",")
|
32
|
+
return PrivateKey.new(l.to_i, m.to_i)
|
33
|
+
end
|
9
34
|
end
|
10
35
|
|
11
36
|
class PublicKey
|
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.
|
4
|
+
version: 1.1.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:
|
11
|
+
date: 2017-09-13 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
|
@@ -40,7 +40,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
40
40
|
version: '0'
|
41
41
|
requirements: []
|
42
42
|
rubyforge_project:
|
43
|
-
rubygems_version: 2.6.
|
43
|
+
rubygems_version: 2.6.10
|
44
44
|
signing_key:
|
45
45
|
specification_version: 4
|
46
46
|
summary: Paillier Homomorphic Cryptosystem
|