lib/secretsharing/shamir.rb
def lagrange(x, shares)
      prime        = shares.first.prime
      other_shares = shares.reject { |s| s.x == x }

      results = other_shares.map do |s|
        minus_xi = -s.x
        # was OpenSSL::BN#mod_inverse
        one_over_xj_minus_xi = invmod(x - s.x, prime)
        # was OpenSSL::BN#mod_mul : (self * other) % m
        (minus_xi * one_over_xj_minus_xi) % prime
      end

      results.reduce { |a, e| (a * e) % prime }
    end
def lagrange(x, shares)
      prime        = shares.first.prime
      other_shares = shares.reject { |s| s.x == x }

      results = other_shares.map do |s|
        minus_xi = -s.x
        # was OpenSSL::BN#mod_inverse
        one_over_xj_minus_xi = invmod(x - s.x, prime)
        # was OpenSSL::BN#mod_mul : (self * other) % m
        (minus_xi * one_over_xj_minus_xi) % prime
      end

      results.reduce { |a, e| (a * e) % prime }
    end
FIXME : Needs focused tests
 Part of the Lagrange interpolation.
 This is l_j(0), i.e.
 \prod_{x_j \neq x_i} \frac{-x_i}{x_j - x_i}
 for more information compare Wikipedia:
 http://en.wikipedia.org/wiki/Lagrange_form