Sage中计算有限域多项式平方根遇类型错误求助
错误信息:
TypeError: not in prime subfield处理上述异常期间,又发生新异常:
TypeError: 无法将z^3 + z + 1转换为有理数处理上述异常期间,又发生新异常:
ValueError: z^3 + z + 1不在( coercion系统内部映射——使用前复制)的像中
环态射:
源:大小为2的有限域
目标:大小为2^4的z上有限域
问题需求
在Sage中计算有限域内多项式的平方根,已知平方根存在,且所有指数都是偶数,可通过对每个系数开方后乘以变量(指数/2)实现。现有代码对二进制Goppa多项式有效,但对系数在GF(2m)中的通用Goppa多项式无效,问题出在类型转换上。
环境变量
- K的定义(即F₂ᵐ):
f = irreducibles_z[0] #Now we can initialise the field K.<z> = GF(2^m, modulus = f) return K
- sigma:
[z^2, z^3 + z^2 + 1, z^3 + z^2 + z, z, 0, z^3 + z, z^3 + z + 1, 1, z + 1, z^3, z^3 + 1, z^3 + z^2 + z + 1, z^3 + z^2, z^2 + z + 1, z^2 + z, z^2 + 1] - g:
(z^2 + z + 1)*x^3 + (z^3 + z^2 + z + 1)*x^2 + (z^3 + z^2 + 1)*x + z^3 + z + 1 - K:大小为2^4的z上有限域
- t:3
- m:4
- n:16
- k:4
- G:大小为2^4的z上有限域上的单变量商多项式环,模为
x^3 + z^2*x^2 + z^3*x + z^3 + z^2 + z + 1 - Sw:
(z^2 + z)*x^2 + z^2*x + z^2 + z + 1
调用代码
v = calculate_v(g, K, t, Sw, verbose)
函数实现及错误行
def calculate_v(g, K, t, Sw, verbose): #This helper field with indeterminate b is used # to check for all x's if they are a square, #as this functionality is not available for # F_{2^m}[x]/g(x,z), but is available for F_{2^t} G.<x> = K.extension(g) _.<b> = GF(2)[] # 错误行: SqrtField.<b> = GF(2^t, modulus = g(b)) #This helper function checks for all pairs of # C_i*x^i if c_i is a square and x^i is a square, #because only then c_i*x^i is a square def is_square_f2mx(el): coefs = list(el) result = True for index,coef in enumerate(coefs): if coef != 0: result = result and (b^index).is_square() and coef.is_square() return result #This helper function converts an element from helper # field SqrtField back to F_{2^m}[x]/g(x) def b_to_x(element): xcoefs = element.polynomial().coefficients(sparse = False) xvalue = 0 for exponent,xcoef in enumerate(xcoefs): if xcoef != 0: xvalue += x^exponent return xvalue #Using the observation from the previous cell, # this function calculates the square root # of an element in F_{2^m}[x] #by taking the square root of each coefficient c_i # and each x^i separately # and then multiplying them afterwards def sqrt_f2mx(element, Sw): # assert is_square_f2mx(element), "not a square" result = 0 for index,coef in enumerate(list(element)): if coef != 0: #bsquare_for_x = sqrt(b^index) #xsquare = b_to_x(bsquare_for_x) coefsqrt = sqrt(coef) result += coefsqrt*x ^(index/2) #xsquare return result v = sqrt_f2mx(x + Sw^-1, Sw) if verbose: latexprint(r"\text{This is our polynomial }V(x,z)") latexprint(latex(v)) return v
错误原因
问题出在SqrtField.<b> = GF(2^t, modulus = g(b))这一行:当g为二进制多项式(所有系数在GF(2)中)时代码可行,但当g为系数在GF(2m)中的通用Goppa多项式时,g的系数属于GF(24)而非GF(2),无法直接代入GF(2)上的多项式变量b生成GF(2^t)的模多项式,导致类型转换失败。
解决方案
由于已知平方根存在且所有指数都是偶数,可直接移除冗余的辅助域逻辑,按规则实现平方根计算:
修改后的calculate_v函数:
def calculate_v(g, K, t, Sw, verbose): G.<x> = K.extension(g) # 直接计算多项式平方根:系数开方,指数除以2 def sqrt_f2mx(element): result = 0 m_degree = K.degree() for index, coef in enumerate(list(element)): if coef != 0: # 特征为2的有限域中,平方根是元素的2^(m-1)次幂 coefsqrt = coef^(2^(m_degree - 1)) # 指数为偶数,直接整数除法取半 result += coefsqrt * x^(index // 2) return result v = sqrt_f2mx(x + Sw^-1) if verbose: latexprint(r"\text{This is our polynomial }V(x,z)") latexprint(latex(v)) return v
关键说明
- 系数开方:在特征为2的有限域GF(2m)中,每个元素都是平方元,平方根可通过计算元素的`2^(m-1)`次幂得到——因为域乘法群是循环群,阶为2m - 1,2与该阶互质,平方映射是自同构,平方根唯一存在。
- 指数处理:题目明确所有指数都是偶数,直接用整数除法
index // 2即可得到平方根的指数,无需辅助域判断。 - 冗余逻辑移除:原代码中的
SqrtField、is_square_f2mx、b_to_x均为二进制Goppa多项式的专属设计,通用场景下完全不需要,直接按规则计算即可规避类型转换问题。
内容的提问来源于stack exchange,提问作者fepaul
相关产品推荐
相关产品推荐

