Example #1
0
 def qdiv(self, other):
     """
     Returns q, r the quotient and remainder polynomials respectively, such that
     f = q * g + r, where deg(r) < deg(g).
     * Assert that g is not the zero polynomial.
     """
     other = Polynomial.typecast(other)
     pol2 = trim_trailing_zeros(other.poly)
     assert pol2, 'Dividing by zero polynomial.'
     pol1 = trim_trailing_zeros(self.poly)
     if not pol1:
         return [], []
     rem = pol1
     deg_dif = len(rem) - len(pol2)
     quotient = [FieldElement.zero()] * (deg_dif + 1)
     g_msc_inv = pol2[-1].inverse()
     while deg_dif >= 0:
         tmp = rem[-1] * g_msc_inv
         quotient[deg_dif] = quotient[deg_dif] + tmp
         last_non_zero = deg_dif - 1
         for i, coef in enumerate(pol2, deg_dif):
             rem[i] = rem[i] - (tmp * coef)
             if rem[i] != FieldElement.zero():
                 last_non_zero = i
         # Eliminate trailing zeroes (i.e. make r end with its last non-zero coefficient).
         rem = rem[:last_non_zero + 1]
         deg_dif = len(rem) - len(pol2)
     return Polynomial(trim_trailing_zeros(quotient)), Polynomial(rem)
Example #2
0
 def get_nth_degree_coefficient(self, n):
     """
     Returns the coefficient of x**n
     """
     if n > self.degree():
         return FieldElement.zero()
     else:
         return self.poly[n]
Example #3
0
def random_polynomial(degree):
    """
    Returns a random polynomial of a prescribed degree which is NOT the zero polynomial.
    """
    leading = FieldElement.random_element(
        exclude_elements=[FieldElement.zero()])
    p = [FieldElement.random_element() for i in range(degree)] + [leading]
    return Polynomial(p)
Example #4
0
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()
Example #5
0
 def __init__(self, coefficients, var='x'):
     # Internally storing the coefficients in self.poly, least-significant (i.e. free term)
     # first, so $9 - 3x^2 + 19x^5$ is represented internally by the list  [9, 0, -3, 0, 0, 19].
     # Note that coefficients is copied, so the caller may freely modify the given argument.
     self.poly = remove_trailing_elements(coefficients, FieldElement.zero())
     self.var = var
Example #6
0
 def X(cls):
     """
     Returns the polynomial x.
     """
     return cls([FieldElement.zero(), FieldElement.one()])
Example #7
0
def trim_trailing_zeros(p):
    """
    Removes zeros from the end of a list.
    """
    return remove_trailing_elements(p, FieldElement.zero())
Example #8
0
 def gen_linear_term(point):
     """
     Generates the polynomial (x-p) for a given point p.
     """
     return Polynomial([FieldElement.zero() - point, FieldElement.one()])
Example #9
0
 def monomial(degree, coefficient):
     """
     Constructs the monomial coefficient * x**degree.
     """
     return Polynomial([FieldElement.zero()] * degree + [coefficient])
Example #10
0
 def __sub__(self, other):
     other = Polynomial.typecast(other)
     return Polynomial(
         two_lists_tuple_operation(self.poly, other.poly, operator.sub,
                                   FieldElement.zero()))