当前位置: 首页>>代码示例>>Python>>正文


Python PolynomialRing.lagrange_polynomial方法代码示例

本文整理汇总了Python中sage.rings.polynomial.polynomial_ring_constructor.PolynomialRing.lagrange_polynomial方法的典型用法代码示例。如果您正苦于以下问题:Python PolynomialRing.lagrange_polynomial方法的具体用法?Python PolynomialRing.lagrange_polynomial怎么用?Python PolynomialRing.lagrange_polynomial使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sage.rings.polynomial.polynomial_ring_constructor.PolynomialRing的用法示例。


在下文中一共展示了PolynomialRing.lagrange_polynomial方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _interpolate

# 需要导入模块: from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing [as 别名]
# 或者: from sage.rings.polynomial.polynomial_ring_constructor.PolynomialRing import lagrange_polynomial [as 别名]
 def _interpolate(evaluation, num_of_var, order):
     if num_of_var == 0 or order == 0:
         return evaluation[0]
     base_field = polynomial_ring.base_ring()
     q = base_field.cardinality()
     n_by_q = q**(num_of_var - 1)
     d = min(order + 1, q)
     multipoint_evaluation_list = []
     uni_poly_ring = PolynomialRing(base_field, 'x')
     base_field_zero = base_field.zero()
     for k in range(n_by_q):
         iterator = iter(base_field)
         points = []
         for i in range(d):
             xcoordinate = next(iterator)
             points.append((xcoordinate, evaluation[k + i * n_by_q]))
         polyVector = uni_poly_ring.lagrange_polynomial(
             points).coefficients(sparse=False)
         if len(polyVector) < d:
             # adding zeros to represet a (d-1) degree polynomial
             polyVector += [base_field_zero] * (d - len(polyVector))
         multipoint_evaluation_list.append(polyVector)
     poly = polynomial_ring.zero()
     z = 1
     x = polynomial_ring.gen(num_of_var - 1)
     for k in range(d):  # computing the polynomial
         poly = poly + z * _interpolate([multipoint_evaluation_list[i][k]
                                         for i in range(n_by_q)], num_of_var - 1, order - k)
         z *= x
     return poly
开发者ID:robertwb,项目名称:sage,代码行数:32,代码来源:reed_muller_code.py

示例2: interpolation_polynomial

# 需要导入模块: from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing [as 别名]
# 或者: from sage.rings.polynomial.polynomial_ring_constructor.PolynomialRing import lagrange_polynomial [as 别名]
    def interpolation_polynomial(self, k=None):
        r"""
        Return a univariate polynomial over an extension field
        representing this S-box.

        If ``m`` is the input length of this S-box then the extension
        field is of degree ``m``.

        If the output length does not match the input length then a
        ``TypeError`` is raised.

        INPUT:

        - ``k`` - an instance of `\GF{2^m}` (default: ``None``)

        EXAMPLES::

            sage: from sage.crypto.sbox import SBox
            sage: S = SBox(7,6,0,4,2,5,1,3)
            sage: f = S.interpolation_polynomial()
            sage: f
            x^6 + a*x^5 + (a + 1)*x^4 + (a^2 + a + 1)*x^3
              + (a^2 + 1)*x^2 + (a + 1)*x + a^2 + a + 1

            sage: a = f.base_ring().gen()

            sage: f(0), S(0)
            (a^2 + a + 1, 7)

            sage: f(a^2 + 1), S(5)
            (a^2 + 1, 5)
        """
        if self.m != self.n:
            raise TypeError("Lagrange interpolation only supported if self.m == self.n.")

        if k is None:
            k = GF(2**self.m,'a')
        l = []
        for i in range(2**self.m):
            i = self.to_bits(i, self.m)
            o = self(i)
            if self._big_endian:
                i = reversed(i)
                o = reversed(o)
            l.append( (k(vector(i)), k(vector(o))) )

        P = PolynomialRing(k,'x')
        return P.lagrange_polynomial(l)
开发者ID:mcognetta,项目名称:sage,代码行数:50,代码来源:sbox.py

示例3: ShamirSS

# 需要导入模块: from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing [as 别名]
# 或者: from sage.rings.polynomial.polynomial_ring_constructor.PolynomialRing import lagrange_polynomial [as 别名]

#.........这里部分代码省略.........


    def _rec_lagrange(self, points):
        r""" 
        Reconstruct with Lagrange interpolation.

        INPUT:

        - ``points`` --  shares as list of tuples.

        OUTPUT:

        Reconstructed secret.

        EXAMPLES::

            sage: from sage.crypto.smc.shamir_ss import ShamirSS

        Erasure decoding::

            sage: sss = ShamirSS()
            sage: secret = 42
            sage: shares = sss.share(secret)
            sage: secret == sss.reconstruct(shares)
            True

            sage: k = 4; n = 10
            sage: sss = ShamirSS(n,k)
            sage: secret = 42
            sage: shares = sss.share(secret)
            sage: secret == sss.reconstruct(shares)
            True
        """
        polycoeffs = self._P.lagrange_polynomial(points).coeffs()
        if len(polycoeffs) != self._k:
            raise ValueError("lagrange polynomial degree mismatch.")
        return polycoeffs


    def _repr_(self):
        r"""
        Return String representation of self.

        EXAMPLES::

            sage: from sage.crypto.smc.shamir_ss import ShamirSS
            sage: sss=ShamirSS()
            sage: print(sss)
            (7,3)-Shamir secret sharing over Finite Field in a of size 2^8
        """
        return "({},{})-Shamir secret sharing over {}".format(self._n, self._k, 
                                                              self._F)
    ### begin public api

    def reconstruct(self, shares, decoder='lg'):
        r"""
        Reconstruct shares.

        INPUT:

        - ``shares`` -- a list of shares ((x,y)-tuples of integer) or list of it.
        - ``decoder`` -- (default: ``'lg'``) decoder used to reconstruct secret. Must
            be one of the supported types ``'lg'`` or ``'bw'``.

        OUTPUT:
开发者ID:l0re,项目名称:sage-examples,代码行数:69,代码来源:shamir_ss.py


注:本文中的sage.rings.polynomial.polynomial_ring_constructor.PolynomialRing.lagrange_polynomial方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。