lib/sharing/polynomial/shamir/v1.rb in sharing-0.1.1 vs lib/sharing/polynomial/shamir/v1.rb in sharing-0.2.0
- old
+ new
@@ -26,23 +26,59 @@
def self.sdiv(shares, scalar, prime)
shares.map { |s| [s[0], (s[1] * mod_inverse(scalar, prime)) % prime] }
end
+ def self.mul_first_round(shares, total_shares, threshold, lambda_, prime)
+ shares1, shares2 = shares
+ xs = shares1.map(&:first)
+ shares1.zip(shares2).map.with_index do |s, i|
+ share = prepare_share_for_multiplication(i, xs, prime, s)
+ reshares = create_shares(share, total_shares, threshold, lambda_, prime)
+ encode_reshares(reshares, prime, s)
+ end
+ end
+
+ def self.encode_reshares(reshares, prime, s_pair)
+ reshares_encoded = reshares.map do |ss|
+ [ss[0], (ss[1].numerator * mod_inverse(ss[1].denominator, prime)) % prime]
+ end
+ [s_pair[0][0], reshares_encoded]
+ end
+
+ def self.prepare_share_for_multiplication(index, xs_, prime, s_pair)
+ beta = lagrange_basis_polynomial_inner_loop(index, xs_)
+ (s_pair[0][1] * s_pair[1][1] * beta) % prime
+ end
+
+ def self.mul_second_round(mul_round1)
+ multiplication_shares = mul_round1.map(&:last).map { |m| m.map(&:last) }.transpose.map(&:sum)
+ multiplication_shares.map.with_index { |m, i| [i + 1, m] }
+ end
+
+ def self.select_mul_shares(total_shares, threshold, shares)
+ indices = (0..total_shares - 1).to_a.sample((2 * threshold) - 1)
+ shares.map { |shares_| shares_.values_at(*indices) }
+ end
+
def self.generate_random_coefficients(total_shares, lambda_)
random_distinct_numbers("integer", total_shares - 1, lambda_ - 1)
end
- def self.create_shares(secret, total_shares, lambda_, prime)
- random_coefficients = generate_random_coefficients(total_shares, lambda_)
+ def self.create_shares(secret, total_shares, threshold, lambda_, prime)
+ random_coefficients = generate_random_coefficients(threshold, lambda_)
(1..total_shares).map.with_index { |x, i| [i + 1, f(x, secret, random_coefficients) % prime] }
end
def initialize(params = {})
@lambda_ = params[:lambda_]
@total_shares = params[:total_shares]
@threshold = params[:threshold]
generate_prime
+ end
+
+ def params
+ [lambda_, p, total_shares, threshold]
end
def create_shares(secret)
random_coefficients = generate_random_coefficients
(1..total_shares).map { |x| [x, f(x, secret, random_coefficients) % p] }