ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 12 Apr 2022 11:02:29 +0200ExtGCD in Finite Fieldshttps://ask.sagemath.org/question/61929/extgcd-in-finite-fields/I want to implement Extended GCD with SageMath so that the internal can be printed. The below is a modification of [this](https://www.rookieslab.com/posts/extended-euclid-algorithm-to-find-gcd-bezouts-coefficients-python-cpp-code). This should be straightforward, however, I might miss something;
def extended_euclides(a,b):
s = 0; old_s = 1
t = 1; old_t = 0
r = b; old_r = a
while r != 0:
quotient,rem = old_r.quo_rem(r)
old_r, r = r, old_r - quotient*r
old_s, s = s, old_s - quotient*s
old_t, t = t, old_t - quotient*t
return [old_r, old_s, old_t]
K.<a> = GF(2^8, modulus=x^8+x^4+x^3+x+1)
print(K)
print("m = ", K.modulus())
g = a^7 + a^5 + a^4 + a
h = a^4 + a^2 + 1
r,s,t = extended_euclides(g,h)
print("The GCD of \n \t [ {} ] and \n \t [ {} ] is \n\t [ {} ]".format(g,h,r))
print("Its Bézout coefficients are {} and {}".format(s,t))
assert r == g.gcd(h), "The gcd should be {}!".format(g.gcd(h))
assert r == s*g + t*h, "The cofactors are wrong!"
In the end, I've got the assertion error `AssertionError: The gcd should be 1!`
Any idea to solve this?klxTue, 12 Apr 2022 11:02:29 +0200https://ask.sagemath.org/question/61929/finding inverse of en element wiht Ext-GCD fails due to defining polynomial converts zero in functionhttps://ask.sagemath.org/question/53183/finding-inverse-of-en-element-wiht-ext-gcd-fails-due-to-defining-polynomial-converts-zero-in-function/ I'm trying to implement a fast Ext-GCD to find the inverse of an element in the finite field $GF(2^8)$ of AES.
def egcd(a, b):
print(a)
print(b)
x,y, u,v = 0,1, 1,0
while a != 0:
q, r = b/a, b%a
m, n = x-u*q, y-v*q
b,a, x,y, u,v = a,r, u,v, m,n
gcd = b
return gcd, x, y
#Base field
R.<y> = PolynomialRing(GF(2), 'y')
#Defining polynomial
G = y^8+y^4+y^3+y+1
#The field extension
S.<x> = QuotientRing(R, R.ideal(G))
S.is_field()
print(egcd(x^8+x^4+x^3+x+1,x^7+x+1))
When the $x^8+x^4+x^3+x+1$ is sent to this function it is converted to zero, so the `print(a)` prints zero.
Is there a way to protect the variable from converting to zero so that this function can work as intended?
klxTue, 25 Aug 2020 22:44:40 +0200https://ask.sagemath.org/question/53183/Extended Euclidean Algorithm for Univariate Polynomials with Coefficients in a Finite Fieldhttps://ask.sagemath.org/question/44376/extended-euclidean-algorithm-for-univariate-polynomials-with-coefficients-in-a-finite-field/Consider the following snippet, intended to compute the extended euclidean algorithm for polynomials in $F_q[x]$.
def extended_euclides(a,b,quo=lambda a,b:a//b):
r0 = a; r1 = b
s0 = 1; s1 = 0
t0 = 0; t1 = 1
while r1 != 0:
q = quo(r0, r1)
r0, r1 = r1, r0 - q * r1
s0, s1 = s1, s0 - q * s1
t0, t1 = t1, t0 - q * t1
return r0, s0, t0
GFX.<x> = GF(5)[x]
g = x^3 + 2*x^2 - x - 2
h = x - 2
r,s,t = extended_euclides(f,g)
print("The GCD of {} and {} is {}".format(g,h,r))
print("Its Bézout coefficients are {} and {}".format(s,t))
assert r == gcd(g,h), "The gcd should be {}!".format(gcd(g,h))
assert r == s*g + t*h, "The cofactors are wrong!"
Executing this snippet produces the following output:
The GCD of x^3 + 2*x^2 + 4*x + 3 and x + 3 is 3*x + 1
Its Bézout coefficients are 2*x + 4 and 3*x^2 + 4
Error in lines 45-45
Traceback (most recent call last):
File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1188, in execute
flags=compile_flags) in namespace, locals
File "", line 1, in <module>
AssertionError: The gcd should be 1!
That is, my code disagrees with sagemath's `gcd` function, and I think sagemath's result is correct; `g(2)!=0`, so `g` and `h` should not have common factors.
Why is mine wrong?JsevillamolThu, 22 Nov 2018 10:46:07 +0100https://ask.sagemath.org/question/44376/