Sha256: 4c80510c2178a185fbaa95c46e3b45e307306b5fa131e8d6243809bea5e38cba

Contents?: true

Size: 704 Bytes

Versions: 4

Compression:

Stored size: 704 Bytes

Contents

# frozen_string_literal: true

module Sharing
  module Polynomial
    # helper tool for computations with polynomials
    module Tools
      def lagrange_basis_polynomial_inner_loop(index, xs_)
        product = 1
        (0..xs_.size - 1).reject { |l| l == index }.each do |j|
          product *= Rational(-xs_[j], xs_[index] - xs_[j])
        end
        product
      end

      def lagrange_basis_polynomial(xs_)
        (0..xs_.size - 1).map { |i| lagrange_basis_polynomial_inner_loop(i, xs_) }
      end

      def f(poly_var, secret, random_coefficients)
        secret + random_coefficients.each_with_index.inject(0) { |sum, (c, i)| sum + (c * (poly_var**(i + 1))) }
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sharing-0.3.0 lib/sharing/polynomial/tools.rb
sharing-0.2.0 lib/sharing/polynomial/tools.rb
sharing-0.1.1 lib/sharing/polynomial/tools.rb
sharing-0.1.0 lib/sharing/polynomial/tools.rb