Skip to content

Commit

Permalink
perf(eisentein/half-GCD): only 1 remainder option
Browse files Browse the repository at this point in the history
  • Loading branch information
yelhousni committed Oct 3, 2024
1 parent 13fa612 commit 507de62
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions field/eisenstein/eisenstein.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func (z *ComplexNumber) Quo(x, y *ComplexNumber) *ComplexNumber {
return z
}

// Rem sets z to the minimum of all possible remainders of x by y, and returns z.
func (z *ComplexNumber) Rem(x, y, q *ComplexNumber) *ComplexNumber {
var t ComplexNumber
z.Sub(x, t.Mul(y, q))
Expand Down Expand Up @@ -147,7 +148,8 @@ func (z *ComplexNumber) QuoRem(x, y, r *ComplexNumber) (*ComplexNumber, *Complex
z.Mul(x, z)
z.A0.Div(&z.A0, norm)
z.A1.Div(&z.A1, norm)
r.Rem(x, y, z)
r.Mul(y, z)
r.Sub(x, r)

return z, r
}
Expand All @@ -167,7 +169,7 @@ func Min(z ...*ComplexNumber) *ComplexNumber {
// This outputs w, v, u s.t. w = a*u + b*v.
func HalfGCD(a, b *ComplexNumber) [3]*ComplexNumber {

var aRun, bRun, u, v, u_, v_, quotient, remainder, t1, t2 ComplexNumber
var aRun, bRun, u, v, u_, v_, quotient, remainder, t, t1, t2 ComplexNumber
var sqrt big.Int

aRun.Set(a)
Expand All @@ -179,10 +181,12 @@ func HalfGCD(a, b *ComplexNumber) [3]*ComplexNumber {

// Eisenstein integers form an Euclidean domain for the norm
sqrt.Sqrt(a.Norm())
for bRun.Norm().Cmp(&sqrt) == 1 {
for bRun.Norm().Cmp(&sqrt) >= 0 {
quotient.QuoRem(&aRun, &bRun, &remainder)
t1.Rem(&u, &u_, &quotient)
t2.Rem(&v, &v_, &quotient)
t.Mul(&u_, &quotient)
t1.Sub(&u, &t)
t.Mul(&v_, &quotient)
t2.Sub(&v, &t)
aRun.Set(&bRun)
u.Set(&u_)
v.Set(&v_)
Expand Down

0 comments on commit 507de62

Please sign in to comment.