Class: Sharing::CRT::AsmuthBloom::V2
- Inherits:
-
Object
- Object
- Sharing::CRT::AsmuthBloom::V2
- Includes:
- HenselCode::Tools
- Defined in:
- lib/sharing/crt/asmuth-bloom/v2.rb
Overview
second version of Asmuth-Bloom proposed by Ersoy et al.
Instance Attribute Summary collapse
-
#a ⇒ Object
Returns the value of attribute a.
-
#k_add ⇒ Object
Returns the value of attribute k_add.
-
#k_mul ⇒ Object
Returns the value of attribute k_mul.
-
#lambda_ ⇒ Object
Returns the value of attribute lambda_.
-
#m_r ⇒ Object
Returns the value of attribute m_r.
-
#m_to_s ⇒ Object
Returns the value of attribute m_to_s.
-
#p ⇒ Object
Returns the value of attribute p.
-
#primes ⇒ Object
Returns the value of attribute primes.
-
#secrecy ⇒ Object
Returns the value of attribute secrecy.
-
#threshold ⇒ Object
Returns the value of attribute threshold.
-
#total_shares ⇒ Object
Returns the value of attribute total_shares.
-
#upperbound ⇒ Object
Returns the value of attribute upperbound.
-
#y ⇒ Object
Returns the value of attribute y.
Class Method Summary collapse
Instance Method Summary collapse
- #compute_prime_products ⇒ Object
- #compute_shares(secret) ⇒ Object
- #compute_upperbound(secret) ⇒ Object
- #generate_primes ⇒ Object
- #generate_single_prime ⇒ Object
- #generate_unique_primes ⇒ Object
-
#initialize(params = {}) ⇒ V2
constructor
A new instance of V2.
- #reconstruct_secret(selected_shares) ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ V2
Returns a new instance of V2.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/sharing/crt/asmuth-bloom/v2.rb', line 26 def initialize(params = {}) @threshold = params[:threshold] @secrecy = params[:secrecy] @total_shares = params[:total_shares] @k_add = params[:k_add] @k_mul = params[:k_mul] @lambda_ = params[:lambda_] generate_unique_primes compute_prime_products generate_single_prime end |
Instance Attribute Details
#a ⇒ Object
Returns the value of attribute a.
13 14 15 |
# File 'lib/sharing/crt/asmuth-bloom/v2.rb', line 13 def a @a end |
#k_add ⇒ Object
Returns the value of attribute k_add.
13 14 15 |
# File 'lib/sharing/crt/asmuth-bloom/v2.rb', line 13 def k_add @k_add end |
#k_mul ⇒ Object
Returns the value of attribute k_mul.
13 14 15 |
# File 'lib/sharing/crt/asmuth-bloom/v2.rb', line 13 def k_mul @k_mul end |
#lambda_ ⇒ Object
Returns the value of attribute lambda_.
13 14 15 |
# File 'lib/sharing/crt/asmuth-bloom/v2.rb', line 13 def lambda_ @lambda_ end |
#m_r ⇒ Object
Returns the value of attribute m_r.
13 14 15 |
# File 'lib/sharing/crt/asmuth-bloom/v2.rb', line 13 def m_r @m_r end |
#m_to_s ⇒ Object
Returns the value of attribute m_to_s.
13 14 15 |
# File 'lib/sharing/crt/asmuth-bloom/v2.rb', line 13 def m_to_s @m_to_s end |
#p ⇒ Object
Returns the value of attribute p.
13 14 15 |
# File 'lib/sharing/crt/asmuth-bloom/v2.rb', line 13 def p @p end |
#primes ⇒ Object
Returns the value of attribute primes.
13 14 15 |
# File 'lib/sharing/crt/asmuth-bloom/v2.rb', line 13 def primes @primes end |
#secrecy ⇒ Object
Returns the value of attribute secrecy.
13 14 15 |
# File 'lib/sharing/crt/asmuth-bloom/v2.rb', line 13 def secrecy @secrecy end |
#threshold ⇒ Object
Returns the value of attribute threshold.
13 14 15 |
# File 'lib/sharing/crt/asmuth-bloom/v2.rb', line 13 def threshold @threshold end |
#total_shares ⇒ Object
Returns the value of attribute total_shares.
13 14 15 |
# File 'lib/sharing/crt/asmuth-bloom/v2.rb', line 13 def total_shares @total_shares end |
#upperbound ⇒ Object
Returns the value of attribute upperbound.
13 14 15 |
# File 'lib/sharing/crt/asmuth-bloom/v2.rb', line 13 def upperbound @upperbound end |
#y ⇒ Object
Returns the value of attribute y.
13 14 15 |
# File 'lib/sharing/crt/asmuth-bloom/v2.rb', line 13 def y @y end |
Class Method Details
.add(shares1, shares2) ⇒ Object
18 19 20 |
# File 'lib/sharing/crt/asmuth-bloom/v2.rb', line 18 def self.add(shares1, shares2) shares1.zip(shares2).map { |s| [s[0][0], s[0][1] + s[1][1]] } end |
.mul(shares1, shares2) ⇒ Object
22 23 24 |
# File 'lib/sharing/crt/asmuth-bloom/v2.rb', line 22 def self.mul(shares1, shares2) shares1.zip(shares2).map { |s| [s[0][0], s[0][1] * s[1][1]] } end |
Instance Method Details
#compute_prime_products ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/sharing/crt/asmuth-bloom/v2.rb', line 62 def compute_prime_products @m_r = primes[0..threshold - 1].inject(:*) @m_to_s = 1 (0..secrecy - 1).each do |i| @m_to_s *= primes[total_shares - (i + 2) + 1] end end |
#compute_shares(secret) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/sharing/crt/asmuth-bloom/v2.rb', line 42 def compute_shares(secret) compute_upperbound(secret) @a = rand(1..upperbound - 1) @y = secret + (a * p) primes.map.with_index { |prime, i| [i, y % prime] } end |
#compute_upperbound(secret) ⇒ Object
38 39 40 |
# File 'lib/sharing/crt/asmuth-bloom/v2.rb', line 38 def compute_upperbound(secret) @upperbound = ((p * m_to_s) - secret) / p end |
#generate_primes ⇒ Object
57 58 59 60 |
# File 'lib/sharing/crt/asmuth-bloom/v2.rb', line 57 def generate_primes @primes = random_distinct_numbers("prime", total_shares, lambda_) @primes.sort! end |
#generate_single_prime ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/sharing/crt/asmuth-bloom/v2.rb', line 70 def generate_single_prime bits = lambda_ @p = random_prime(lambda_) while m_r <= (k_add + 1) * ((@p * m_to_s)**(k_mul + 1)) bits -= 1 @p = random_prime(bits) end end |
#generate_unique_primes ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/sharing/crt/asmuth-bloom/v2.rb', line 79 def generate_unique_primes @primes = [random_prime(lambda_)] while primes.uniq.size < total_shares prime = random_prime(lambda_) @primes << prime unless @primes.include?(prime) && (@primes + [prime]).reduce(1, :gcd) != 1 end @primes = @primes.uniq.sort end |
#reconstruct_secret(selected_shares) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/sharing/crt/asmuth-bloom/v2.rb', line 49 def reconstruct_secret(selected_shares) indices = selected_shares.map(&:first) moduli = primes.values_at(*indices) shares = selected_shares.map(&:last) y = crt(moduli, shares) y % p end |