sharing 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5c3ca365fc28e5bd00b60ddae38c473e7cc4fab0bd82601634380e58d823c966
4
- data.tar.gz: 839ded7393e627ae15fefcd4be3bb05d797ccc0b7a61af54e73232a57f594cdf
3
+ metadata.gz: b091eefc1c0e4656e732cfdbc983177df829e0c646467227871bf2a3dfc18203
4
+ data.tar.gz: 493c97755883f09df9fd19b9e9f9dcb09f4a15756cd22401bce780e4b574123c
5
5
  SHA512:
6
- metadata.gz: 75cc42e29de49f5a9391f021753869398b42bcdfd87204de288a37c27f85be3da0883772b9e2722847c78f870bad47558e37f6eff2c24d3f1f41bdeff4c2d44b
7
- data.tar.gz: 0b399903a1837bdee94707f7317b214b60339ab96b2d3684e62a7be04d93d5882d1b2e7ba238f8bb2146c73663c905e8ad69d667ceb353200cbcfd054154d473
6
+ metadata.gz: 6405b3a080f2a669b1f0882d9baec0e29c7ffaa93cafb5738a1636fdf58f596129c7a8436470dfbb4fd059e78b15b9b060a557bfb789e1316dbf1605b031545b
7
+ data.tar.gz: 5a8096e9f83b122550d62cd97417b928bd37d64a5902c857a7d7c7f515ddb97264ab1b87cbde0590e9e7bdcda3acdb4c95a75cdaab0fb0bb9bad120f91373e6a
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## [0.2.0] - 2022-03-25
2
+
3
+ - Add multiplication to Shamir's secret sharing implementation and overall improvements.
4
+
1
5
  ## [0.1.1] - 2022-03-25
2
6
 
3
7
  - Fix threshold setting on Shamir's scheme v1
data/Gemfile CHANGED
@@ -4,13 +4,9 @@ ruby ">= 2.6.0"
4
4
 
5
5
  source "https://rubygems.org"
6
6
 
7
- # Specify your gem's dependencies in secret_sharing.gemspec
7
+ # Specify your gem"s dependencies in sharing.gemspec
8
8
  gemspec
9
9
 
10
- gem "hensel_code", "~> 0.3.1"
11
-
12
- gem "prime", "~> 0.1.2"
13
-
14
10
  gem "rake", "~> 13.0"
15
11
 
16
12
  gem "minitest", "~> 5.0"
@@ -23,4 +19,8 @@ gem "rubocop-rake", "~> 0.6"
23
19
 
24
20
  gem "minitest-reporters", "~> 1.5"
25
21
 
26
- gem "codecov", require: false, group: :test
22
+ gem "simplecov", "~>0.21", require: false
23
+
24
+ gem "simplecov-console", "~>0.9", require: false
25
+
26
+ gem "codecov", require: false, group: "test"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sharing (0.1.0)
4
+ sharing (0.2.0)
5
5
  hensel_code (~> 0.3.1)
6
6
  prime (~> 0.1.2)
7
7
 
@@ -55,25 +55,32 @@ GEM
55
55
  docile (~> 1.1)
56
56
  simplecov-html (~> 0.11)
57
57
  simplecov_json_formatter (~> 0.1)
58
+ simplecov-console (0.9.1)
59
+ ansi
60
+ simplecov
61
+ terminal-table
58
62
  simplecov-html (0.12.3)
59
63
  simplecov_json_formatter (0.1.4)
60
64
  singleton (0.1.1)
65
+ terminal-table (3.0.2)
66
+ unicode-display_width (>= 1.1.1, < 3)
61
67
  unicode-display_width (2.1.0)
62
68
 
63
69
  PLATFORMS
64
70
  x86_64-darwin-19
71
+ x86_64-linux
65
72
 
66
73
  DEPENDENCIES
67
74
  codecov
68
- hensel_code (~> 0.3.1)
69
75
  minitest (~> 5.0)
70
76
  minitest-reporters (~> 1.5)
71
- prime (~> 0.1.2)
72
77
  rake (~> 13.0)
73
78
  rubocop (~> 1.21)
74
79
  rubocop-minitest (~> 0.17.2)
75
80
  rubocop-rake (~> 0.6)
76
81
  sharing!
82
+ simplecov (~> 0.21)
83
+ simplecov-console (~> 0.9)
77
84
 
78
85
  RUBY VERSION
79
86
  ruby 3.1.0p0
data/README.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # Sharing
2
2
 
3
- A secret sharing Ruby library.
3
+ ![GitHub Workflow Status](https://img.shields.io/github/workflow/status/davidwilliam/sharing/Ruby)
4
+
5
+ Sharing is a Ruby gem with implmementations of secret sharing schemes with homomorphic properties. Although secret sharing schemes and multiparty computation protocols are distinct notions, multiparty computation protocols are typically enabled by secret sharing schemes. In this setting, security comes from the use of multiple parties. If they collude, all security is lost, but satisfactory levels of security can be established by trusting a subset of them will not to collude. In many settings where corrupting security requires corrupting all the parties, and considering you are one of the computing parties, security is guaranteed if you are one of the parties.
6
+
7
+ Computing linear functions is trivial. Each non-linear operation however requires interaction between the parties/extra steps (for most secret sharing schemes).
4
8
 
5
9
  ## Installation
6
10
 
@@ -27,6 +31,13 @@ Secret Sharing currently supports two schemes:
27
31
 
28
32
  # Usage
29
33
 
34
+ In the examples below, there are two main levels of execution:
35
+
36
+ - Computations performed by the owner of the secrets (those are computations using instance methods)
37
+ - Computations performed over the secret shares (those are computations using class methods)
38
+
39
+ This distiction is important since we are showing everything at once here, for completeness and for clarity. However it is important to keep in mind that after the secret shares are generated, the computations over the shares are intended to be computed independetly by each participant (party), each one with their corresponding shares.
40
+
30
41
  ## Shamir's Secret Sharing V1
31
42
 
32
43
  The Shamir's secret sharing v1 scheme is based on the work of Adi Shamir in [How to Share a Secret](https://web.mit.edu/6.857/OldStuff/Fall03/ref/Shamir-HowToShareASecret.pdf).
@@ -58,9 +69,9 @@ We reconstruct the secrets as follows:
58
69
 
59
70
  ```ruby
60
71
  reconstructed_secret1 = sss.reconstruct_secret(shares1)
61
- # => (22/1)
72
+ # => 22
62
73
  reconstructed_secret2 = sss.reconstruct_secret(shares2)
63
- # => (36/1)
74
+ # => 36
64
75
  ```
65
76
 
66
77
  We can compute linear functions without requiring communication between the share holders:
@@ -80,18 +91,18 @@ and we can check that:
80
91
 
81
92
  ```ruby
82
93
  sss.reconstruct_secret(shares1_add_shares2)
83
- # => (58/1)
94
+ # => 58
84
95
  sss.reconstruct_secret(shares2_sub_shares1)
85
- # => (14/1)
96
+ # => 14
86
97
  sss.reconstruct_secret(shares1_smul_scalar)
87
- # => (44/1)
98
+ # => 44
88
99
  sss.reconstruct_secret(shares1_sdiv_scalar)
89
- # => (11/1)
100
+ # => 11
90
101
  ```
91
102
 
92
103
  ### Using Hensel Codes
93
104
 
94
- The gem Secret Sharing takes advantage of the gem [Hensel Codes](https://github.com/davidwilliam/hensel_code) for homomorphically encoding rational numbers as integers in order to compute over the integers and yet obtain results over the rationals.
105
+ The gem Secret Sharing takes advantage of the gem [Hensel Code](https://github.com/davidwilliam/hensel_code) for homomorphically encoding rational numbers as integers in order to compute over the integers and yet obtain results over the rationals.
95
106
 
96
107
  As most (if not all) of secret sharing schemes over finite fields `F_p` for `p > 2`, the secret inputs are naturally required to be positive integers in `F_p`. In this way, if we compute subtraction and we end up with a result that is negative, the reconstruction will fail (provided we don't have any econding in place). Same will occur if we compute a scalar division involving a scalar that is not a divisor of the corresponding secret. For addressing this and many other arithmetic problems, we can use Hensel codes to allow secret inputs to be positive and negative rational numbers.
97
108
 
@@ -143,13 +154,13 @@ We reconstruct the secrets:
143
154
 
144
155
  ```ruby
145
156
  reconstruct_secret1_add_secret2 = sss.reconstruct_secret(shares1_add_shares2)
146
- # => 3361138990/1
157
+ # => 3361138990
147
158
  reconstruct_secret1_sub_secret2 = sss.reconstruct_secret(shares1_sub_shares2)
148
- # => 2174854642/1
159
+ # => 2174854642
149
160
  reconstruct_shares1_smul_scalar = sss.reconstruct_secret(shares1_smul_scalar)
150
- # => 1383998411/1
161
+ # => 1383998411
151
162
  reconstruct_shares1_sdiv_scalar = sss.reconstruct_secret(shares1_sdiv_scalar)
152
- # => 3044796497/1
163
+ # => 3044796497
153
164
  ```
154
165
 
155
166
  and we can check that:
@@ -165,6 +176,94 @@ HenselCode::TruncatedFinitePadicExpansion.new(sss.p, 1, reconstructed_shares1_sd
165
176
  # => 2/15
166
177
  ```
167
178
 
179
+ ### Multiplication
180
+
181
+ As we previously saw, linear functions are easy to compute with shares created by an instance of Shamir's secret sharing scheme. Non-linear functions need some strategy that require extra steps in other to successfuly achieve the desired results. We implement multiplication in the context of Shamir's secret sharing scheme following the approach discussed by Dan Bognadov in [Foundations and properties of Shamir's secret sharing scheme - Research Seminar in Cryptography](https://uuslepo.it.da.ut.ee/~peeter_l/teaching/seminar07k/bogdanov.pdf).
182
+
183
+ We define an instance of Shamir's secret sharing scheme with the following parameters:
184
+
185
+ ```ruby
186
+ params = { lambda_: 16, total_shares: 6, threshold: 3 }
187
+ # => {:lambda_=>16, :total_shares=>6, :threshold=>3}
188
+ sss = Sharing::Polynomial::Shamir::V1.new params
189
+ # => #<Sharing::Polynomial::Shamir::V1:0x0000000105423640 @lambda_=16, @p=49367, @threshold=3, @total_shares=6>
190
+ ```
191
+
192
+ As before, we define the secrets and create shares for them:
193
+
194
+ ```ruby
195
+ secret1 = 13
196
+ secret2 = 28
197
+ shares1 = sss.create_shares(secret1)
198
+ # => [[1, 43064], [2, 20333], [3, 30554], [4, 24360], [5, 1751], [6, 12094]]
199
+ shares2 = sss.create_shares(secret2)
200
+ # => [[1, 7983], [2, 18517], [3, 31630], [4, 47322], [5, 16226], [6, 37076]]
201
+ ```
202
+
203
+ We combine both shares on a single array in preparation for the multiplication steps:
204
+
205
+ ```ruby
206
+ operands_shares = [shares1, shares2]
207
+ # => [[[1, 43064], [2, 20333], [3, 30554], [4, 24360], [5, 1751], [6, 12094]], [[1, 7983], [2, 18517], [3, 31630], [4, 47322], [5, 16226], [6, 37076]]]
208
+ ```
209
+
210
+ Recall we are working with a t-out-of-n secret sharing scheme and this is actually required in this setting. We have a total of `n = 6` shares and threshold `t = 3`. In order to correctly recover the result of the multiplication over shares, we need to select any combination of `2 * t - 1` shares out of the total number of shares:
211
+
212
+ ```ruby
213
+ selected_shares = Sharing::Polynomial::Shamir::V1.select_mul_shares(sss.total_shares, sss.threshold, operands_shares)
214
+ # => => [[[2, 20333], [1, 43064], [5, 1751], [3, 30554], [4, 24360]], [[2, 18517], [1, 7983], [5, 16226], [3, 31630], [4, 47322]]]
215
+ ```
216
+
217
+ Now we have everything we need to compute multiplication over the secret shares, which we do in two rounds. First round:
218
+
219
+ ```ruby
220
+ mul_round1 = Sharing::Polynomial::Shamir::V1.mul_first_round(selected_shares, sss.total_shares, sss.threshold, sss.lambda_, sss.p)
221
+ # => [[2, [[1, 25284], [2, 5881], [3, 2537], [4, 15252], [5, 44026], [6, 39492]]], [1, [[1, 36061], [2, 17299], [3, 32435], [4, 32102], [5, 16300], [6, 34396]]], [5, [[1, 30221], [2, 32724], [3, 33210], [4, 31679], [5, 28131], [6, 22566]]], [3, [[1, 46172], [2, 33017], [3, 8081], [4, 20731], [5, 21600], [6, 10688]]], [4, [[1, 12410], [2, 39133], [3, 5920], [4, 11505], [5, 6521], [6, 40335]]]]
222
+ ```
223
+
224
+ Then we perform the second round:
225
+
226
+ ```ruby
227
+ mul_round2 = Sharing::Polynomial::Shamir::V1.mul_second_round(mul_round1)
228
+ # => [[1, 150148], [2, 128054], [3, 82183], [4, 111269], [5, 116578], [6, 147477]]
229
+ ```
230
+
231
+ Then we only need a number equal to the threshold to reconstruct the result of the multipliction over the shares:
232
+
233
+ ```ruby
234
+ selected_multiplication_shares = mul_round2.sample(sss.threshold)
235
+ # => [[6, 147477], [2, 128054], [1, 150148]
236
+ sss.reconstruct_secret(selected_multiplication_shares)
237
+ # => 364
238
+ ```
239
+
240
+ and we can check that
241
+
242
+ ```ruby
243
+ secret1 * secret2
244
+ # => 364
245
+ ```
246
+
247
+ ### t-out-of-n Secret Sharing
248
+
249
+ Now we defined a threshold value that is less than the total number of shares:
250
+
251
+ ```ruby
252
+ params = {total_shares: 5, threshold: 3, lambda_: 16}
253
+ # => {:total_shares=>5, :threshold=>3, :lambda_=>16}
254
+ sss = Sharing::Polynomial::Shamir::V1.new params
255
+ # => #<Sharing::Polynomial::Shamir::V1:0x000000010b046e90 @lambda_=16, @p=61343, @threshold=3, @total_shares=5>
256
+ secret = 25
257
+ # => 25
258
+ shares = sss.create_shares(secret)
259
+ # => [[1, 54707], [2, 50401], [3, 48450], [4, 48854], [5, 51613]]
260
+ selected_shares = shares.sample(3)
261
+ reconstructed_secret = sss.reconstruct_secret(selected_shares)
262
+ # => 25
263
+ ```
264
+
265
+ Everything else works the sabe as before except the fact that only `3` shares are required to reconstruct the secret.
266
+
168
267
  ## Asmuth-Bloom V2
169
268
 
170
269
  The Asmuth-Bloom V2 was proposed by Ersoy et al. in in [Homomorphic extensions of CRT-based secret sharing](https://www.sciencedirect.com/science/article/pii/S0166218X20303012)). The reference is a CRT-based secret sharing scheme introduced by Asmuth-Bloom in [A modular approach to key safeguarding](https://ieeexplore.ieee.org/abstract/document/1056651).
@@ -239,4 +338,4 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/davidw
239
338
 
240
339
  ## License
241
340
 
242
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
341
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/codecov ADDED
Binary file
@@ -28,12 +28,44 @@ module Sharing
28
28
  shares.map { |s| [s[0], (s[1] * mod_inverse(scalar, prime)) % prime] }
29
29
  end
30
30
 
31
+ def self.mul_first_round(shares, total_shares, threshold, lambda_, prime)
32
+ shares1, shares2 = shares
33
+ xs = shares1.map(&:first)
34
+ shares1.zip(shares2).map.with_index do |s, i|
35
+ share = prepare_share_for_multiplication(i, xs, prime, s)
36
+ reshares = create_shares(share, total_shares, threshold, lambda_, prime)
37
+ encode_reshares(reshares, prime, s)
38
+ end
39
+ end
40
+
41
+ def self.encode_reshares(reshares, prime, s_pair)
42
+ reshares_encoded = reshares.map do |ss|
43
+ [ss[0], (ss[1].numerator * mod_inverse(ss[1].denominator, prime)) % prime]
44
+ end
45
+ [s_pair[0][0], reshares_encoded]
46
+ end
47
+
48
+ def self.prepare_share_for_multiplication(index, xs_, prime, s_pair)
49
+ beta = lagrange_basis_polynomial_inner_loop(index, xs_)
50
+ (s_pair[0][1] * s_pair[1][1] * beta) % prime
51
+ end
52
+
53
+ def self.mul_second_round(mul_round1)
54
+ multiplication_shares = mul_round1.map(&:last).map { |m| m.map(&:last) }.transpose.map(&:sum)
55
+ multiplication_shares.map.with_index { |m, i| [i + 1, m] }
56
+ end
57
+
58
+ def self.select_mul_shares(total_shares, threshold, shares)
59
+ indices = (0..total_shares - 1).to_a.sample((2 * threshold) - 1)
60
+ shares.map { |shares_| shares_.values_at(*indices) }
61
+ end
62
+
31
63
  def self.generate_random_coefficients(total_shares, lambda_)
32
64
  random_distinct_numbers("integer", total_shares - 1, lambda_ - 1)
33
65
  end
34
66
 
35
- def self.create_shares(secret, total_shares, lambda_, prime)
36
- random_coefficients = generate_random_coefficients(total_shares, lambda_)
67
+ def self.create_shares(secret, total_shares, threshold, lambda_, prime)
68
+ random_coefficients = generate_random_coefficients(threshold, lambda_)
37
69
  (1..total_shares).map.with_index { |x, i| [i + 1, f(x, secret, random_coefficients) % prime] }
38
70
  end
39
71
 
@@ -44,6 +76,10 @@ module Sharing
44
76
  generate_prime
45
77
  end
46
78
 
79
+ def params
80
+ [lambda_, p, total_shares, threshold]
81
+ end
82
+
47
83
  def create_shares(secret)
48
84
  random_coefficients = generate_random_coefficients
49
85
  (1..total_shares).map { |x| [x, f(x, secret, random_coefficients) % p] }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sharing
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -1,4 +1,4 @@
1
- module SecretSharing
1
+ module Sharing
2
2
  VERSION: String
3
3
  # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sharing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David William Silva
@@ -54,6 +54,7 @@ files:
54
54
  - Rakefile
55
55
  - bin/console
56
56
  - bin/setup
57
+ - codecov
57
58
  - lib/sharing.rb
58
59
  - lib/sharing/crt/asmuth-bloom/v2.rb
59
60
  - lib/sharing/polynomial/shamir/v1.rb