def calculate_lagrange_polynomials(x_values): """ Given the x_values for evaluating some polynomials, it computes part of the lagrange polynomials required to interpolate a polynomial over this domain. """ lagrange_polynomials = [] monomials = [ Polynomial.monomial(1, FieldElement.one()) - Polynomial.monomial(0, x) for x in x_values ] numerator = prod(monomials) for j in tqdm(range(len(x_values))): # In the denominator, we have: # (x_j-x_0)(x_j-x_1)...(x_j-x_{j-1})(x_j-x_{j+1})...(x_j-x_{len(X)-1}) denominator = prod( [x_values[j] - x for i, x in enumerate(x_values) if i != j]) # Numerator is a bit more complicated, since we need to compute a poly multiplication here. # Similarly to the denominator, we have: # (x-x_0)(x-x_1)...(x-x_{j-1})(x-x_{j+1})...(x-x_{len(X)-1}) cur_poly, _ = numerator.qdiv(monomials[j].scalar_mul(denominator)) lagrange_polynomials.append(cur_poly) return lagrange_polynomials
def test_field_div(): for _ in range(100): t = FieldElement.random_element(exclude_elements=[FieldElement.zero()]) t_inv = FieldElement.one() / t assert t_inv == t.inverse() assert t_inv * t == FieldElement.one()
def X(cls): """ Returns the polynomial x. """ return cls([FieldElement.zero(), FieldElement.one()])
def gen_linear_term(point): """ Generates the polynomial (x-p) for a given point p. """ return Polynomial([FieldElement.zero() - point, FieldElement.one()])